xref: /openbmc/linux/net/mac80211/sta_info.c (revision 8ca47eb9f9e4e10e7e7fa695731a88941732c38d)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f0706e82SJiri Benc /*
3f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
4f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
6dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
752b4810bSIlan Peer  * Copyright (C) 2018-2020 Intel Corporation
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
677bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
687bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
69caf22d31SJohannes Berg 	.automatic_shrinking = true,
707bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
71ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
727bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
73ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
747bedd0cfSJohannes Berg };
757bedd0cfSJohannes Berg 
764d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
77be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
78f0706e82SJiri Benc 			     struct sta_info *sta)
79f0706e82SJiri Benc {
8083e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
817bedd0cfSJohannes Berg 			       sta_rht_params);
82f0706e82SJiri Benc }
83f0706e82SJiri Benc 
845108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
85b22cfcfcSEliad Peller {
86b22cfcfcSEliad Peller 	int ac, i;
87b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
88b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
89b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
90d012a605SMarco Porsch 	struct ps_data *ps;
91b22cfcfcSEliad Peller 
92e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
935ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
945ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
95d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
96d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
97d012a605SMarco Porsch 			ps = &sdata->bss->ps;
983f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
993f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
100d012a605SMarco Porsch 		else
101d012a605SMarco Porsch 			return;
102b22cfcfcSEliad Peller 
103b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
104e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1055ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
106b22cfcfcSEliad Peller 
107d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
108b22cfcfcSEliad Peller 	}
109b22cfcfcSEliad Peller 
110ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
111ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
112adf8ed01SJohannes Berg 			struct txq_info *txqi;
113adf8ed01SJohannes Berg 
114adf8ed01SJohannes Berg 			if (!sta->sta.txq[i])
115adf8ed01SJohannes Berg 				continue;
116adf8ed01SJohannes Berg 
117adf8ed01SJohannes Berg 			txqi = to_txq_info(sta->sta.txq[i]);
118ba8c3d6fSFelix Fietkau 
119fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
120ba8c3d6fSFelix Fietkau 		}
121ba8c3d6fSFelix Fietkau 	}
122ba8c3d6fSFelix Fietkau 
123b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1251f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1261f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
127b22cfcfcSEliad Peller 	}
128b22cfcfcSEliad Peller 
12945b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
13045b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
131b22cfcfcSEliad Peller 
1325ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
133b22cfcfcSEliad Peller 
134b22cfcfcSEliad Peller 	/*
135b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
136b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
137b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
138b22cfcfcSEliad Peller 	 * directly by station destruction.
139b22cfcfcSEliad Peller 	 */
1405a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
141661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
142b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
143b22cfcfcSEliad Peller 		if (!tid_tx)
144b22cfcfcSEliad Peller 			continue;
1451f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
146b22cfcfcSEliad Peller 		kfree(tid_tx);
147b22cfcfcSEliad Peller 	}
1485108ca82SJohannes Berg }
149b22cfcfcSEliad Peller 
1505108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1515108ca82SJohannes Berg {
1525108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1535108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1545108ca82SJohannes Berg 
1555108ca82SJohannes Berg 	__cleanup_single_sta(sta);
156b22cfcfcSEliad Peller 	sta_info_free(local, sta);
157b22cfcfcSEliad Peller }
158b22cfcfcSEliad Peller 
15983e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
16083e7e4ceSHerbert Xu 					 const u8 *addr)
16183e7e4ceSHerbert Xu {
16283e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
16383e7e4ceSHerbert Xu }
16483e7e4ceSHerbert Xu 
165d0709a65SJohannes Berg /* protected by RCU */
166abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
167abe60632SJohannes Berg 			      const u8 *addr)
16843ba7e95SJohannes Berg {
169abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
17083e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
17160f4b626SJohannes Berg 	struct sta_info *sta;
17243ba7e95SJohannes Berg 
17360f4b626SJohannes Berg 	rcu_read_lock();
17483e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
17560f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17660f4b626SJohannes Berg 			rcu_read_unlock();
17760f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17860f4b626SJohannes Berg 			 * another rcu read section or the mutex
17960f4b626SJohannes Berg 			 */
18060f4b626SJohannes Berg 			return sta;
18160f4b626SJohannes Berg 		}
18260f4b626SJohannes Berg 	}
18360f4b626SJohannes Berg 	rcu_read_unlock();
18460f4b626SJohannes Berg 	return NULL;
18543ba7e95SJohannes Berg }
18643ba7e95SJohannes Berg 
1870e5ded5aSFelix Fietkau /*
1880e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1890e5ded5aSFelix Fietkau  * or from one of its vlans
1900e5ded5aSFelix Fietkau  */
1910e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1920e5ded5aSFelix Fietkau 				  const u8 *addr)
1930e5ded5aSFelix Fietkau {
1940e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
19583e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
1960e5ded5aSFelix Fietkau 	struct sta_info *sta;
1970e5ded5aSFelix Fietkau 
1987bedd0cfSJohannes Berg 	rcu_read_lock();
19983e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2007bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2017bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2027bedd0cfSJohannes Berg 			rcu_read_unlock();
2037bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2047bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2057bedd0cfSJohannes Berg 			 */
2060e5ded5aSFelix Fietkau 			return sta;
2070e5ded5aSFelix Fietkau 		}
2087bedd0cfSJohannes Berg 	}
2097bedd0cfSJohannes Berg 	rcu_read_unlock();
2107bedd0cfSJohannes Berg 	return NULL;
2117bedd0cfSJohannes Berg }
2120e5ded5aSFelix Fietkau 
2135072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
2145072f73cSToke Høiland-Jørgensen 				       const u8 *sta_addr, const u8 *vif_addr)
2155072f73cSToke Høiland-Jørgensen {
2165072f73cSToke Høiland-Jørgensen 	struct rhlist_head *tmp;
2175072f73cSToke Høiland-Jørgensen 	struct sta_info *sta;
2185072f73cSToke Høiland-Jørgensen 
2195072f73cSToke Høiland-Jørgensen 	for_each_sta_info(local, sta_addr, sta, tmp) {
2205072f73cSToke Høiland-Jørgensen 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
2215072f73cSToke Høiland-Jørgensen 			return sta;
2225072f73cSToke Høiland-Jørgensen 	}
2235072f73cSToke Høiland-Jørgensen 
2245072f73cSToke Høiland-Jørgensen 	return NULL;
2255072f73cSToke Høiland-Jørgensen }
2265072f73cSToke Høiland-Jørgensen 
2273b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2283b53fde8SJohannes Berg 				     int idx)
229ee385855SLuis Carlos Cobo {
2303b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
231ee385855SLuis Carlos Cobo 	struct sta_info *sta;
232ee385855SLuis Carlos Cobo 	int i = 0;
233ee385855SLuis Carlos Cobo 
234*8ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
235*8ca47eb9SMadhuparna Bhowmik 				lockdep_is_held(&local->sta_mtx)) {
2363b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2372a8ca29aSLuis Carlos Cobo 			continue;
238ee385855SLuis Carlos Cobo 		if (i < idx) {
239ee385855SLuis Carlos Cobo 			++i;
240ee385855SLuis Carlos Cobo 			continue;
241ee385855SLuis Carlos Cobo 		}
2422a8ca29aSLuis Carlos Cobo 		return sta;
243ee385855SLuis Carlos Cobo 	}
244ee385855SLuis Carlos Cobo 
245ee385855SLuis Carlos Cobo 	return NULL;
246ee385855SLuis Carlos Cobo }
247f0706e82SJiri Benc 
24893e5deb1SJohannes Berg /**
249d9a7ddb0SJohannes Berg  * sta_info_free - free STA
25093e5deb1SJohannes Berg  *
2516ef307bcSRandy Dunlap  * @local: pointer to the global information
25293e5deb1SJohannes Berg  * @sta: STA info to free
25393e5deb1SJohannes Berg  *
25493e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
255d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
256d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
257d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
25893e5deb1SJohannes Berg  */
259d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
26093e5deb1SJohannes Berg {
261889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2624b7679a5SJohannes Berg 		rate_control_free_sta(sta);
26393e5deb1SJohannes Berg 
264bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
26593e5deb1SJohannes Berg 
266ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
267ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
26853d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
269433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
270433f5bc1SJohannes Berg 	kfree(sta->mesh);
271433f5bc1SJohannes Berg #endif
272c9c5962bSJohannes Berg 	free_percpu(sta->pcpu_rx_stats);
27393e5deb1SJohannes Berg 	kfree(sta);
27493e5deb1SJohannes Berg }
27593e5deb1SJohannes Berg 
2764d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
27762b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
278d0709a65SJohannes Berg 			     struct sta_info *sta)
279f0706e82SJiri Benc {
28083e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
2817bedd0cfSJohannes Berg 			       sta_rht_params);
282f0706e82SJiri Benc }
283f0706e82SJiri Benc 
2845ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
285af818581SJohannes Berg {
286af818581SJohannes Berg 	struct sta_info *sta;
287af818581SJohannes Berg 
2885ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
289af818581SJohannes Berg 
290af818581SJohannes Berg 	if (sta->dead)
291af818581SJohannes Berg 		return;
292af818581SJohannes Berg 
29354420473SHelmut Schaa 	local_bh_disable();
2945ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
295af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2965ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
297af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2985ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
29947086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
300ce662b44SJohannes Berg 	local_bh_enable();
301af818581SJohannes Berg }
302af818581SJohannes Berg 
303af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
304af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
305af65cd96SJohannes Berg {
30630686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
307af65cd96SJohannes Berg 		return 0;
308af65cd96SJohannes Berg 
309889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
310af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
31135c347acSJohannes Berg 						     sta, gfp);
312889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
313af65cd96SJohannes Berg 		return -ENOMEM;
314af65cd96SJohannes Berg 
315af65cd96SJohannes Berg 	return 0;
316af65cd96SJohannes Berg }
317af65cd96SJohannes Berg 
31873651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
31956544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
320f0706e82SJiri Benc {
321d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
322ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
323f0706e82SJiri Benc 	struct sta_info *sta;
32416c5f15cSRon Rindjunsky 	int i;
325f0706e82SJiri Benc 
326ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
327f0706e82SJiri Benc 	if (!sta)
32873651ee6SJohannes Berg 		return NULL;
329f0706e82SJiri Benc 
330c9c5962bSJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
331c9c5962bSJohannes Berg 		sta->pcpu_rx_stats =
33295f3ce6aSSara Sharon 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
333c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
334c9c5962bSJohannes Berg 			goto free;
335c9c5962bSJohannes Berg 	}
336c9c5962bSJohannes Berg 
33707346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3381d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3395ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
34067c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
341a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
34287f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
343433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
344433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
345433f5bc1SJohannes Berg 		if (!sta->mesh)
346433f5bc1SJohannes Berg 			goto free;
3474c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
348433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
34987f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
35087f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
3514c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
3524c02d62fSKees Cook 				    0);
353433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
354433f5bc1SJohannes Berg 	}
35587f59c70SThomas Pedersen #endif
35607346f81SJohannes Berg 
357ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
35817741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
359480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
360480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
361480dd46bSMaxim Altshul 
36296fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
36396fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
36496fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
36596fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
36696fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
36796fc6efbSAlexander Wetzel 	 */
36896fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
36996fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
37096fc6efbSAlexander Wetzel 
371d0709a65SJohannes Berg 	sta->local = local;
372d0709a65SJohannes Berg 	sta->sdata = sdata;
373e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
374f0706e82SJiri Benc 
3750f9c5a61SJohannes Berg 	u64_stats_init(&sta->rx_stats.syncp);
3760f9c5a61SJohannes Berg 
37771ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
37871ec375cSJohannes Berg 
379b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
380b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
381b6da911bSLiad Kaufman 
38284b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
3830be6ed13SJohannes Berg 	ewma_signal_init(&sta->rx_stats_avg.signal);
384cc60dbbfSBalaji Pothunoori 	ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
3850be6ed13SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
3860be6ed13SJohannes Berg 		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
387541a45a1SBruno Randolf 
388ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
389ba8c3d6fSFelix Fietkau 		void *txq_data;
390ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
391ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
392ba8c3d6fSFelix Fietkau 
393ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
394ba8c3d6fSFelix Fietkau 		if (!txq_data)
395ba8c3d6fSFelix Fietkau 			goto free;
396ba8c3d6fSFelix Fietkau 
397ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
398ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
399ba8c3d6fSFelix Fietkau 
400adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
401fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
402abfbc3afSJohannes Berg 		}
403ba8c3d6fSFelix Fietkau 	}
404ba8c3d6fSFelix Fietkau 
405ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
406ba8c3d6fSFelix Fietkau 		goto free_txq;
407f0706e82SJiri Benc 
408b4809e94SToke Høiland-Jørgensen 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
409b4809e94SToke Høiland-Jørgensen 
410948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
411948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
412948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
413b4809e94SToke Høiland-Jørgensen 		sta->airtime[i].deficit = sta->airtime_weight;
4143ace10f5SKan Yan 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
4153ace10f5SKan Yan 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
4163ace10f5SKan Yan 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
417948d887dSJohannes Berg 	}
41873651ee6SJohannes Berg 
4195a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
4204be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
421cccaec98SSenthil Balasubramanian 
422bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
423bd718fc1SJohannes Berg 		u32 mandatory = 0;
424bd718fc1SJohannes Berg 		int r;
425bd718fc1SJohannes Berg 
426bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
427bd718fc1SJohannes Berg 			continue;
428bd718fc1SJohannes Berg 
429bd718fc1SJohannes Berg 		switch (i) {
430bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
431bd718fc1SJohannes Berg 			/*
432bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
433bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
434bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
435bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
436bd718fc1SJohannes Berg 			 * possible rate anyway.
437bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
438bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
439bd718fc1SJohannes Berg 			 */
440bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
441bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
442bd718fc1SJohannes Berg 			break;
443bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
444bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
445bd718fc1SJohannes Berg 			break;
446bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
447bd718fc1SJohannes Berg 			WARN_ON(1);
448bd718fc1SJohannes Berg 			mandatory = 0;
449bd718fc1SJohannes Berg 			break;
450bd718fc1SJohannes Berg 		}
451bd718fc1SJohannes Berg 
452bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
453bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
454bd718fc1SJohannes Berg 
455bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
456bd718fc1SJohannes Berg 
457bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
458bd718fc1SJohannes Berg 				continue;
459bd718fc1SJohannes Berg 			sta->sta.supp_rates[i] |= BIT(r);
460bd718fc1SJohannes Berg 		}
461bd718fc1SJohannes Berg 	}
462bd718fc1SJohannes Berg 
463af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
464687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
465687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
46621a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
46721a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
46821a8e9ddSMohammed Shafi Shajakhan 
46921a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
47021a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
47121a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
47221a8e9ddSMohammed Shafi Shajakhan 
47321a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
474687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
475687da132SEmmanuel Grumbach 		/*
476687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
477687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
478687da132SEmmanuel Grumbach 		 */
479687da132SEmmanuel Grumbach 		switch (smps) {
480687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
481687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
482687da132SEmmanuel Grumbach 			break;
483687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
484687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
485687da132SEmmanuel Grumbach 			break;
486687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
487687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
488687da132SEmmanuel Grumbach 			break;
489687da132SEmmanuel Grumbach 		default:
490687da132SEmmanuel Grumbach 			WARN_ON(1);
491687da132SEmmanuel Grumbach 		}
492687da132SEmmanuel Grumbach 	}
493af0ed69bSJohannes Berg 
4946e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
4956e0456b5SFelix Fietkau 
496484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
497484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
498484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
499484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
500484a54c2SToke Høiland-Jørgensen 
501bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
502ef04a297SJohannes Berg 
503abfbc3afSJohannes Berg 	return sta;
504ba8c3d6fSFelix Fietkau 
505ba8c3d6fSFelix Fietkau free_txq:
506ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
507ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
508ba8c3d6fSFelix Fietkau free:
509d78d9ee9SSara Sharon 	free_percpu(sta->pcpu_rx_stats);
510433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
511433f5bc1SJohannes Berg 	kfree(sta->mesh);
512433f5bc1SJohannes Berg #endif
513ba8c3d6fSFelix Fietkau 	kfree(sta);
514ba8c3d6fSFelix Fietkau 	return NULL;
51573651ee6SJohannes Berg }
51673651ee6SJohannes Berg 
5178c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
51834e89507SJohannes Berg {
51934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
52034e89507SJohannes Berg 
52103e4497eSJohannes Berg 	/*
52203e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
52303e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
52403e4497eSJohannes Berg 	 * and another CPU turns off the net device.
52503e4497eSJohannes Berg 	 */
5268c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
5278c71df7aSGuy Eilam 		return -ENETDOWN;
52803e4497eSJohannes Berg 
529b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
5308c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
5318c71df7aSGuy Eilam 		return -EINVAL;
5328c71df7aSGuy Eilam 
53383e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
53483e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
53583e7e4ceSHerbert Xu 	 * for correctness.
53631104891SJohannes Berg 	 */
53731104891SJohannes Berg 	rcu_read_lock();
53831104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
53931104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
54031104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
54131104891SJohannes Berg 		rcu_read_unlock();
54231104891SJohannes Berg 		return -ENOTUNIQ;
54331104891SJohannes Berg 	}
54431104891SJohannes Berg 	rcu_read_unlock();
54531104891SJohannes Berg 
5468c71df7aSGuy Eilam 	return 0;
54793e5deb1SJohannes Berg }
54844213b5eSJohannes Berg 
549f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
550f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
551f09603a2SJohannes Berg 				     struct sta_info *sta)
552f09603a2SJohannes Berg {
553f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
554f09603a2SJohannes Berg 	int err = 0;
555f09603a2SJohannes Berg 
556f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
557f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
558f09603a2SJohannes Berg 		if (err)
559f09603a2SJohannes Berg 			break;
560f09603a2SJohannes Berg 	}
561f09603a2SJohannes Berg 
562f09603a2SJohannes Berg 	if (!err) {
563a4ec45a4SJohannes Berg 		/*
564a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
565a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
566a4ec45a4SJohannes Berg 		 */
567a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
568f09603a2SJohannes Berg 			sta->uploaded = true;
569f09603a2SJohannes Berg 		return 0;
570f09603a2SJohannes Berg 	}
571f09603a2SJohannes Berg 
572f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
573bdcbd8e0SJohannes Berg 		sdata_info(sdata,
574bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
575bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
576f09603a2SJohannes Berg 		err = 0;
577f09603a2SJohannes Berg 	}
578f09603a2SJohannes Berg 
579f09603a2SJohannes Berg 	/* unwind on error */
580f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
581f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
582f09603a2SJohannes Berg 
583f09603a2SJohannes Berg 	return err;
584f09603a2SJohannes Berg }
585f09603a2SJohannes Berg 
586d405fd8cSGregory Greenman static void
587d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
588d405fd8cSGregory Greenman {
589d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
590d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
591d405fd8cSGregory Greenman 	struct sta_info *sta;
592d405fd8cSGregory Greenman 
593d405fd8cSGregory Greenman 	rcu_read_lock();
594d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
595d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
596d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
597d405fd8cSGregory Greenman 			continue;
598d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
599d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
600d405fd8cSGregory Greenman 			break;
601d405fd8cSGregory Greenman 		}
602d405fd8cSGregory Greenman 	}
603d405fd8cSGregory Greenman 	rcu_read_unlock();
604d405fd8cSGregory Greenman 
605d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
606d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
607d405fd8cSGregory Greenman 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
608d405fd8cSGregory Greenman 	}
609d405fd8cSGregory Greenman }
610d405fd8cSGregory Greenman 
61134e89507SJohannes Berg /*
6128c71df7aSGuy Eilam  * should be called with sta_mtx locked
6138c71df7aSGuy Eilam  * this function replaces the mutex lock
6148c71df7aSGuy Eilam  * with a RCU lock
6158c71df7aSGuy Eilam  */
6164d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
6178c71df7aSGuy Eilam {
6188c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
6198c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
6200c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
6218c71df7aSGuy Eilam 	int err = 0;
6228c71df7aSGuy Eilam 
6238c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
6248c71df7aSGuy Eilam 
6257852e361SJohannes Berg 	/* check if STA exists already */
6267852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
6274d33960bSJohannes Berg 		err = -EEXIST;
6284d33960bSJohannes Berg 		goto out_err;
62934e89507SJohannes Berg 	}
63034e89507SJohannes Berg 
6310c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
6320c2e3842SKoen Vandeputte 	if (!sinfo) {
6330c2e3842SKoen Vandeputte 		err = -ENOMEM;
6340c2e3842SKoen Vandeputte 		goto out_err;
6350c2e3842SKoen Vandeputte 	}
6360c2e3842SKoen Vandeputte 
6374d33960bSJohannes Berg 	local->num_sta++;
6384d33960bSJohannes Berg 	local->sta_generation++;
6394d33960bSJohannes Berg 	smp_mb();
6404d33960bSJohannes Berg 
6415108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
6425108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
6435108ca82SJohannes Berg 
6444d33960bSJohannes Berg 	/* make the station visible */
64562b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
64662b14b24SJohannes Berg 	if (err)
64762b14b24SJohannes Berg 		goto out_drop_sta;
6484d33960bSJohannes Berg 
6492bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
65083d5cc01SJohannes Berg 
6515108ca82SJohannes Berg 	/* notify driver */
6525108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
6535108ca82SJohannes Berg 	if (err)
6545108ca82SJohannes Berg 		goto out_remove;
6555108ca82SJohannes Berg 
65683d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
657d405fd8cSGregory Greenman 
658d405fd8cSGregory Greenman 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
659d405fd8cSGregory Greenman 		ieee80211_recalc_min_chandef(sta->sdata);
660d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps)
661d405fd8cSGregory Greenman 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
662d405fd8cSGregory Greenman 	}
663d405fd8cSGregory Greenman 
6645108ca82SJohannes Berg 	/* accept BA sessions now */
6655108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
6664d33960bSJohannes Berg 
6674d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
6684d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
6694d33960bSJohannes Berg 
6700ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
6710ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
6720ef049dcSArnd Bergmann 	kfree(sinfo);
673d0709a65SJohannes Berg 
674bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
675f0706e82SJiri Benc 
67634e89507SJohannes Berg 	/* move reference to rcu-protected */
67734e89507SJohannes Berg 	rcu_read_lock();
67834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
679e9f207f0SJiri Benc 
68073651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
68173651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
68273651ee6SJohannes Berg 
68373651ee6SJohannes Berg 	return 0;
6845108ca82SJohannes Berg  out_remove:
6855108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
6865108ca82SJohannes Berg 	list_del_rcu(&sta->list);
68762b14b24SJohannes Berg  out_drop_sta:
6885108ca82SJohannes Berg 	local->num_sta--;
6895108ca82SJohannes Berg 	synchronize_net();
6905108ca82SJohannes Berg 	__cleanup_single_sta(sta);
6914d33960bSJohannes Berg  out_err:
6924d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
693ea32f065SSudip Mukherjee 	kfree(sinfo);
6944d33960bSJohannes Berg 	rcu_read_lock();
6954d33960bSJohannes Berg 	return err;
6968c71df7aSGuy Eilam }
6978c71df7aSGuy Eilam 
6988c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
6998c71df7aSGuy Eilam {
7008c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
701308f7fcfSZhao, Gang 	int err;
7028c71df7aSGuy Eilam 
7034d33960bSJohannes Berg 	might_sleep();
7044d33960bSJohannes Berg 
70531104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
70631104891SJohannes Berg 
7078c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
7088c71df7aSGuy Eilam 	if (err) {
70931104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
7108c71df7aSGuy Eilam 		rcu_read_lock();
7118c71df7aSGuy Eilam 		goto out_free;
7128c71df7aSGuy Eilam 	}
7138c71df7aSGuy Eilam 
7144d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
7158c71df7aSGuy Eilam 	if (err)
716004c872eSJohannes Berg 		goto out_free;
717d0709a65SJohannes Berg 
718004c872eSJohannes Berg 	return 0;
71993e5deb1SJohannes Berg  out_free:
720d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
72193e5deb1SJohannes Berg 	return err;
722f0706e82SJiri Benc }
723f0706e82SJiri Benc 
72434e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
72534e89507SJohannes Berg {
72634e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
72734e89507SJohannes Berg 
72834e89507SJohannes Berg 	rcu_read_unlock();
72934e89507SJohannes Berg 
73034e89507SJohannes Berg 	return err;
73134e89507SJohannes Berg }
73234e89507SJohannes Berg 
733d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
734004c872eSJohannes Berg {
735004c872eSJohannes Berg 	/*
736004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
737004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
738004c872eSJohannes Berg 	 */
739d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
740004c872eSJohannes Berg }
741004c872eSJohannes Berg 
742d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
743004c872eSJohannes Berg {
744004c872eSJohannes Berg 	/*
745004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
746004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
747004c872eSJohannes Berg 	 */
748d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
749004c872eSJohannes Berg }
750004c872eSJohannes Berg 
7513d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
7523d5839b6SIlan Peer {
7533d5839b6SIlan Peer 	/*
7543d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
7553d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
7563d5839b6SIlan Peer 	 */
7573d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
7583d5839b6SIlan Peer }
7593d5839b6SIlan Peer 
760948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
761004c872eSJohannes Berg {
762948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
763948d887dSJohannes Berg 	switch (ac) {
764948d887dSJohannes Berg 	case IEEE80211_AC_VO:
765948d887dSJohannes Berg 		return BIT(6) | BIT(7);
766948d887dSJohannes Berg 	case IEEE80211_AC_VI:
767948d887dSJohannes Berg 		return BIT(4) | BIT(5);
768948d887dSJohannes Berg 	case IEEE80211_AC_BE:
769948d887dSJohannes Berg 		return BIT(0) | BIT(3);
770948d887dSJohannes Berg 	case IEEE80211_AC_BK:
771948d887dSJohannes Berg 		return BIT(1) | BIT(2);
772948d887dSJohannes Berg 	default:
773948d887dSJohannes Berg 		WARN_ON(1);
774948d887dSJohannes Berg 		return 0;
775d0709a65SJohannes Berg 	}
776004c872eSJohannes Berg }
777004c872eSJohannes Berg 
7789b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
779004c872eSJohannes Berg {
780c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
781d012a605SMarco Porsch 	struct ps_data *ps;
782948d887dSJohannes Berg 	bool indicate_tim = false;
783948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
784948d887dSJohannes Berg 	int ac;
785a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
786004c872eSJohannes Berg 
787d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
788d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
789c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
790c868cb35SJohannes Berg 			return;
7913e122be0SJohannes Berg 
792d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
7933f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
7943f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
7953f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
7963f52b7e3SMarco Porsch #endif
797d012a605SMarco Porsch 	} else {
798d012a605SMarco Porsch 		return;
799d012a605SMarco Porsch 	}
800d012a605SMarco Porsch 
801c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
802d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
803c868cb35SJohannes Berg 		return;
804004c872eSJohannes Berg 
805c868cb35SJohannes Berg 	if (sta->dead)
806c868cb35SJohannes Berg 		goto done;
8073e122be0SJohannes Berg 
808948d887dSJohannes Berg 	/*
809948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
810948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
811948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
812948d887dSJohannes Berg 	 * non-enabled ones.
813948d887dSJohannes Berg 	 */
814948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
815948d887dSJohannes Berg 		ignore_for_tim = 0;
816948d887dSJohannes Berg 
8179b7a86f3SJohannes Berg 	if (ignore_pending)
8189b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
8199b7a86f3SJohannes Berg 
820948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
821948d887dSJohannes Berg 		unsigned long tids;
822948d887dSJohannes Berg 
823f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
824948d887dSJohannes Berg 			continue;
825948d887dSJohannes Berg 
826948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
827948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
828948d887dSJohannes Berg 		if (indicate_tim)
829948d887dSJohannes Berg 			break;
830948d887dSJohannes Berg 
831948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
832948d887dSJohannes Berg 
833948d887dSJohannes Berg 		indicate_tim |=
834948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
835ba8c3d6fSFelix Fietkau 		indicate_tim |=
836ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
837004c872eSJohannes Berg 	}
838004c872eSJohannes Berg 
839c868cb35SJohannes Berg  done:
84065f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
841004c872eSJohannes Berg 
8423d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
8433d5839b6SIlan Peer 		goto out_unlock;
8443d5839b6SIlan Peer 
845948d887dSJohannes Berg 	if (indicate_tim)
846d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
847c868cb35SJohannes Berg 	else
848d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
8493e122be0SJohannes Berg 
8509b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
851c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
852948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
853c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
854004c872eSJohannes Berg 	}
855004c872eSJohannes Berg 
8563d5839b6SIlan Peer out_unlock:
85765f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
858004c872eSJohannes Berg }
859004c872eSJohannes Berg 
8609b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
8619b7a86f3SJohannes Berg {
8629b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
8639b7a86f3SJohannes Berg }
8649b7a86f3SJohannes Berg 
865cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
866f0706e82SJiri Benc {
867e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
868f0706e82SJiri Benc 	int timeout;
869f0706e82SJiri Benc 
870f0706e82SJiri Benc 	if (!skb)
871cd0b8d89SJohannes Berg 		return false;
872f0706e82SJiri Benc 
873e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
874f0706e82SJiri Benc 
875f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
87657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
87757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
87857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
879f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
880f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
881e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
882f0706e82SJiri Benc }
883f0706e82SJiri Benc 
884f0706e82SJiri Benc 
885948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
886948d887dSJohannes Berg 						struct sta_info *sta, int ac)
887f0706e82SJiri Benc {
888f0706e82SJiri Benc 	unsigned long flags;
889f0706e82SJiri Benc 	struct sk_buff *skb;
890f0706e82SJiri Benc 
89160750397SJohannes Berg 	/*
89260750397SJohannes Berg 	 * First check for frames that should expire on the filtered
89360750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
89460750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
89560750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
89660750397SJohannes Berg 	 * total_ps_buffered counter.
89760750397SJohannes Berg 	 */
898f0706e82SJiri Benc 	for (;;) {
899948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
900948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
90157c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
902948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
903836341a7SJohannes Berg 		else
904f0706e82SJiri Benc 			skb = NULL;
905948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
906f0706e82SJiri Benc 
90760750397SJohannes Berg 		/*
90860750397SJohannes Berg 		 * Frames are queued in order, so if this one
90960750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
91060750397SJohannes Berg 		 * we actually reached the end of the queue we
91160750397SJohannes Berg 		 * also need to stop, of course.
91260750397SJohannes Berg 		 */
91360750397SJohannes Berg 		if (!skb)
91460750397SJohannes Berg 			break;
915d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
91660750397SJohannes Berg 	}
91760750397SJohannes Berg 
91860750397SJohannes Berg 	/*
91960750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
92060750397SJohannes Berg 	 * only find something if the filtered queue was emptied
92160750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
92260750397SJohannes Berg 	 * buffered frames.
92360750397SJohannes Berg 	 */
924f0706e82SJiri Benc 	for (;;) {
925948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
926948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
927f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
928948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
929f0706e82SJiri Benc 		else
930f0706e82SJiri Benc 			skb = NULL;
931948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
932f0706e82SJiri Benc 
93360750397SJohannes Berg 		/*
93460750397SJohannes Berg 		 * frames are queued in order, so if this one
93560750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
93660750397SJohannes Berg 		 * the queue) we can stop testing
93760750397SJohannes Berg 		 */
938836341a7SJohannes Berg 		if (!skb)
939836341a7SJohannes Berg 			break;
940836341a7SJohannes Berg 
941f0706e82SJiri Benc 		local->total_ps_buffered--;
942bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
943bdcbd8e0SJohannes Berg 		       sta->sta.addr);
944d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
945f0706e82SJiri Benc 	}
9463393a608SJuuso Oikarinen 
94760750397SJohannes Berg 	/*
94860750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
94960750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
95060750397SJohannes Berg 	 * frames.
95160750397SJohannes Berg 	 */
95260750397SJohannes Berg 	sta_info_recalc_tim(sta);
95360750397SJohannes Berg 
95460750397SJohannes Berg 	/*
95560750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
95660750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
95760750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
95860750397SJohannes Berg 	 */
959948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
960948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
961948d887dSJohannes Berg }
962948d887dSJohannes Berg 
963948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
964948d887dSJohannes Berg 					     struct sta_info *sta)
965948d887dSJohannes Berg {
966948d887dSJohannes Berg 	bool have_buffered = false;
967948d887dSJohannes Berg 	int ac;
968948d887dSJohannes Berg 
9693f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
9703f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
9713f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
972948d887dSJohannes Berg 		return false;
973948d887dSJohannes Berg 
974948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
975948d887dSJohannes Berg 		have_buffered |=
976948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
977948d887dSJohannes Berg 
978948d887dSJohannes Berg 	return have_buffered;
979f0706e82SJiri Benc }
980f0706e82SJiri Benc 
981d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
98234e89507SJohannes Berg {
98334e89507SJohannes Berg 	struct ieee80211_local *local;
98434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
9856d10e46bSJohannes Berg 	int ret;
98634e89507SJohannes Berg 
98734e89507SJohannes Berg 	might_sleep();
98834e89507SJohannes Berg 
98934e89507SJohannes Berg 	if (!sta)
99034e89507SJohannes Berg 		return -ENOENT;
99134e89507SJohannes Berg 
99234e89507SJohannes Berg 	local = sta->local;
99334e89507SJohannes Berg 	sdata = sta->sdata;
99434e89507SJohannes Berg 
99583d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
99683d5cc01SJohannes Berg 
997098a6070SJohannes Berg 	/*
998098a6070SJohannes Berg 	 * Before removing the station from the driver and
999098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1000098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1001098a6070SJohannes Berg 	 * will be sufficient.
1002098a6070SJohannes Berg 	 */
1003c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1004c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1005098a6070SJohannes Berg 
1006f59374ebSSara Sharon 	/*
1007f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1008f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1009f59374ebSSara Sharon 	 * all such frames to be processed.
1010f59374ebSSara Sharon 	 */
1011f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1012f59374ebSSara Sharon 
101334e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1014b01711beSJohannes Berg 	if (WARN_ON(ret))
101534e89507SJohannes Berg 		return ret;
101634e89507SJohannes Berg 
1017a7a6bdd0SArik Nemtsov 	/*
1018a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1019a7a6bdd0SArik Nemtsov 	 * removal.
1020a7a6bdd0SArik Nemtsov 	 */
1021a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1022a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1023a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1024a7a6bdd0SArik Nemtsov 	}
1025a7a6bdd0SArik Nemtsov 
1026794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1027ef044763SEliad Peller 	sta->removed = true;
10284d33960bSJohannes Berg 
10296a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
10306a9d1b91SJohannes Berg 
1031a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1032a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1033a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1034a710c816SJohannes Berg 
1035d778207bSJohannes Berg 	return 0;
1036d778207bSJohannes Berg }
1037d778207bSJohannes Berg 
1038d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1039d778207bSJohannes Berg {
1040d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1041d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
10420ef049dcSArnd Bergmann 	struct station_info *sinfo;
1043d778207bSJohannes Berg 	int ret;
1044d778207bSJohannes Berg 
1045d778207bSJohannes Berg 	/*
1046d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1047d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1048d778207bSJohannes Berg 	 */
1049d778207bSJohannes Berg 
1050d778207bSJohannes Berg 	might_sleep();
1051d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1052d778207bSJohannes Berg 
1053b16798f5SJohannes Berg 	while (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1054b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1055b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1056b16798f5SJohannes Berg 	}
1057b16798f5SJohannes Berg 
1058c8782078SJohannes Berg 	/* now keys can no longer be reached */
10596d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
106034e89507SJohannes Berg 
10619b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
10629b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
10639b7a86f3SJohannes Berg 
106434e89507SJohannes Berg 	sta->dead = true;
106534e89507SJohannes Berg 
106634e89507SJohannes Berg 	local->num_sta--;
106734e89507SJohannes Berg 	local->sta_generation++;
106834e89507SJohannes Berg 
106983d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1070f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1071f09603a2SJohannes Berg 		if (ret) {
107283d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
107383d5cc01SJohannes Berg 			break;
107483d5cc01SJohannes Berg 		}
107583d5cc01SJohannes Berg 	}
1076d9a7ddb0SJohannes Berg 
1077f09603a2SJohannes Berg 	if (sta->uploaded) {
1078f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1079f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1080f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1081f09603a2SJohannes Berg 	}
108234e89507SJohannes Berg 
1083bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1084bdcbd8e0SJohannes Berg 
10850ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
10860ef049dcSArnd Bergmann 	if (sinfo)
10870fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
10880ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
10890ef049dcSArnd Bergmann 	kfree(sinfo);
1090ec15e68bSJouni Malinen 
109134e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
109234e89507SJohannes Berg 
1093d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1094d778207bSJohannes Berg }
1095d778207bSJohannes Berg 
1096d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1097d778207bSJohannes Berg {
1098d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1099d778207bSJohannes Berg 
1100d778207bSJohannes Berg 	if (err)
1101d778207bSJohannes Berg 		return err;
1102d778207bSJohannes Berg 
1103d778207bSJohannes Berg 	synchronize_net();
1104d778207bSJohannes Berg 
1105d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
110634e89507SJohannes Berg 
110734e89507SJohannes Berg 	return 0;
110834e89507SJohannes Berg }
110934e89507SJohannes Berg 
111034e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
111134e89507SJohannes Berg {
111234e89507SJohannes Berg 	struct sta_info *sta;
111334e89507SJohannes Berg 	int ret;
111434e89507SJohannes Berg 
111534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
11167852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
111734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
111834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
111934e89507SJohannes Berg 
112034e89507SJohannes Berg 	return ret;
112134e89507SJohannes Berg }
112234e89507SJohannes Berg 
112334e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
112434e89507SJohannes Berg 			      const u8 *addr)
112534e89507SJohannes Berg {
112634e89507SJohannes Berg 	struct sta_info *sta;
112734e89507SJohannes Berg 	int ret;
112834e89507SJohannes Berg 
112934e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
11307852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
113134e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
113234e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
113334e89507SJohannes Berg 
113434e89507SJohannes Berg 	return ret;
113534e89507SJohannes Berg }
1136f0706e82SJiri Benc 
113734f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1138f0706e82SJiri Benc {
113934f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1140f0706e82SJiri Benc 	struct sta_info *sta;
11413393a608SJuuso Oikarinen 	bool timer_needed = false;
1142f0706e82SJiri Benc 
1143d0709a65SJohannes Berg 	rcu_read_lock();
1144d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
11453393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
11463393a608SJuuso Oikarinen 			timer_needed = true;
1147d0709a65SJohannes Berg 	rcu_read_unlock();
1148f0706e82SJiri Benc 
11495bb644a0SJohannes Berg 	if (local->quiescing)
11505bb644a0SJohannes Berg 		return;
11515bb644a0SJohannes Berg 
11523393a608SJuuso Oikarinen 	if (!timer_needed)
11533393a608SJuuso Oikarinen 		return;
11543393a608SJuuso Oikarinen 
115526d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
115626d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1157f0706e82SJiri Benc }
1158f0706e82SJiri Benc 
11597bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
11607bedd0cfSJohannes Berg {
11617bedd0cfSJohannes Berg 	int err;
11627bedd0cfSJohannes Berg 
116383e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
11647bedd0cfSJohannes Berg 	if (err)
11657bedd0cfSJohannes Berg 		return err;
11667bedd0cfSJohannes Berg 
11674d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
116834e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1169f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1170f0706e82SJiri Benc 
117134f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
11727bedd0cfSJohannes Berg 	return 0;
1173f0706e82SJiri Benc }
1174f0706e82SJiri Benc 
1175f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1176f0706e82SJiri Benc {
1177a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
117883e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1179f0706e82SJiri Benc }
1180f0706e82SJiri Benc 
1181051007d9SJohannes Berg 
1182e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1183f0706e82SJiri Benc {
1184b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1185f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1186d778207bSJohannes Berg 	LIST_HEAD(free_list);
118744213b5eSJohannes Berg 	int ret = 0;
1188f0706e82SJiri Benc 
1189d0709a65SJohannes Berg 	might_sleep();
1190d0709a65SJohannes Berg 
1191e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1192e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1193e716251dSJohannes Berg 
119434e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
119534e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1196e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1197e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1198d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1199d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
120034316837SJohannes Berg 			ret++;
120134316837SJohannes Berg 		}
120234e89507SJohannes Berg 	}
1203d778207bSJohannes Berg 
1204d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1205d778207bSJohannes Berg 		synchronize_net();
1206d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1207d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1208d778207bSJohannes Berg 	}
120934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
121044213b5eSJohannes Berg 
1211051007d9SJohannes Berg 	return ret;
1212051007d9SJohannes Berg }
1213051007d9SJohannes Berg 
121424723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
121524723d1bSJohannes Berg 			  unsigned long exp_time)
121624723d1bSJohannes Berg {
121724723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
121824723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
121924723d1bSJohannes Berg 
122034e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1221e46a2cf9SMohammed Shafi Shajakhan 
1222e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1223b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1224b8da6b6aSJohannes Berg 
1225ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1226ec2b774eSMarek Lindner 			continue;
1227ec2b774eSMarek Lindner 
1228b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1229eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1230bdcbd8e0SJohannes Berg 				sta->sta.addr);
12313f52b7e3SMarco Porsch 
12323f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
12333f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
12343f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
12353f52b7e3SMarco Porsch 
123634e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
123724723d1bSJohannes Berg 		}
1238e46a2cf9SMohammed Shafi Shajakhan 	}
1239e46a2cf9SMohammed Shafi Shajakhan 
124034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
124124723d1bSJohannes Berg }
124217741cdcSJohannes Berg 
1243686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1244686b9cb9SBen Greear 						   const u8 *addr,
1245686b9cb9SBen Greear 						   const u8 *localaddr)
124617741cdcSJohannes Berg {
12477bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
124883e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
12497bedd0cfSJohannes Berg 	struct sta_info *sta;
125017741cdcSJohannes Berg 
1251686b9cb9SBen Greear 	/*
1252686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1253686b9cb9SBen Greear 	 * ... first in list.
1254686b9cb9SBen Greear 	 */
125583e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1256686b9cb9SBen Greear 		if (localaddr &&
1257b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1258686b9cb9SBen Greear 			continue;
1259f7c65594SJohannes Berg 		if (!sta->uploaded)
1260f7c65594SJohannes Berg 			return NULL;
126117741cdcSJohannes Berg 		return &sta->sta;
1262f7c65594SJohannes Berg 	}
1263f7c65594SJohannes Berg 
1264abe60632SJohannes Berg 	return NULL;
126517741cdcSJohannes Berg }
1266686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
12675ed176e1SJohannes Berg 
12685ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
12695ed176e1SJohannes Berg 					 const u8 *addr)
12705ed176e1SJohannes Berg {
1271f7c65594SJohannes Berg 	struct sta_info *sta;
12725ed176e1SJohannes Berg 
12735ed176e1SJohannes Berg 	if (!vif)
12745ed176e1SJohannes Berg 		return NULL;
12755ed176e1SJohannes Berg 
1276f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1277f7c65594SJohannes Berg 	if (!sta)
1278f7c65594SJohannes Berg 		return NULL;
12795ed176e1SJohannes Berg 
1280f7c65594SJohannes Berg 	if (!sta->uploaded)
1281f7c65594SJohannes Berg 		return NULL;
1282f7c65594SJohannes Berg 
1283f7c65594SJohannes Berg 	return &sta->sta;
12845ed176e1SJohannes Berg }
128517741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1286af818581SJohannes Berg 
1287e3685e03SJohannes Berg /* powersave support code */
1288e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
128950a9432dSJohannes Berg {
1290608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1291e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1292e3685e03SJohannes Berg 	struct sk_buff_head pending;
1293ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1294e3685e03SJohannes Berg 	unsigned long flags;
1295d012a605SMarco Porsch 	struct ps_data *ps;
1296d012a605SMarco Porsch 
12973918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
12983918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
12993918edb0SFelix Fietkau 				     u.ap);
13003918edb0SFelix Fietkau 
13013918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1302d012a605SMarco Porsch 		ps = &sdata->bss->ps;
13033f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
13043f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1305d012a605SMarco Porsch 	else
1306d012a605SMarco Porsch 		return;
130750a9432dSJohannes Berg 
1308c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
130947086fc5SJohannes Berg 
13105a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1311948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1312ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1313948d887dSJohannes Berg 
131430686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
131512375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1316af818581SJohannes Berg 
1317ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1318adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1319ba8c3d6fSFelix Fietkau 			continue;
1320ba8c3d6fSFelix Fietkau 
132118667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1322ba8c3d6fSFelix Fietkau 	}
1323ba8c3d6fSFelix Fietkau 
1324948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1325af818581SJohannes Berg 
13261d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
13271d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1328af818581SJohannes Berg 	/* Send all buffered frames to the station */
1329948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1330948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1331948d887dSJohannes Berg 
1332987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1333948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1334987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1335948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1336948d887dSJohannes Berg 		filtered += tmp - count;
1337948d887dSJohannes Berg 		count = tmp;
1338948d887dSJohannes Berg 
1339987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1340948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1341987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1342948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1343948d887dSJohannes Berg 		buffered += tmp - count;
1344948d887dSJohannes Berg 	}
1345948d887dSJohannes Berg 
1346e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
13475ac2e350SJohannes Berg 
13485ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
13495ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
13505ac2e350SJohannes Berg 
13515ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
13525ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
13535ac2e350SJohannes Berg 	 */
13545ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
13555ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
13561d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1357948d887dSJohannes Berg 
1358e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1359e3685e03SJohannes Berg 
1360af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1361af818581SJohannes Berg 
1362c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1363c868cb35SJohannes Berg 
1364bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
13652595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1366bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
136717c18bf8SJohannes Berg 
136817c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1369af818581SJohannes Berg }
1370af818581SJohannes Berg 
13710ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1372b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
13730ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1374ce662b44SJohannes Berg {
13750ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1376ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1377ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1378ce662b44SJohannes Berg 	struct sk_buff *skb;
1379ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1380ce662b44SJohannes Berg 	__le16 fc;
1381a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1382ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
138355de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1384ce662b44SJohannes Berg 
138541cbb0f5SLuca Coelho 	/* Don't send NDPs when STA is connected HE */
138641cbb0f5SLuca Coelho 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
138741cbb0f5SLuca Coelho 	    !(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE))
138841cbb0f5SLuca Coelho 		return;
138941cbb0f5SLuca Coelho 
1390ce662b44SJohannes Berg 	if (qos) {
1391ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1392ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1393ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1394ce662b44SJohannes Berg 	} else {
1395ce662b44SJohannes Berg 		size -= 2;
1396ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1397ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1398ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1399ce662b44SJohannes Berg 	}
1400ce662b44SJohannes Berg 
1401ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1402ce662b44SJohannes Berg 	if (!skb)
1403ce662b44SJohannes Berg 		return;
1404ce662b44SJohannes Berg 
1405ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1406ce662b44SJohannes Berg 
14074df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1408ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1409ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1410ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1411ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1412ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1413864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1414ce662b44SJohannes Berg 
1415ce662b44SJohannes Berg 	skb->priority = tid;
1416ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
141759b66255SJohannes Berg 	if (qos) {
1418ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1419ce662b44SJohannes Berg 
14200ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1421ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1422ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
14230ead2510SEmmanuel Grumbach 			if (more_data)
14240ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
14250ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
14260ead2510SEmmanuel Grumbach 		}
1427ce662b44SJohannes Berg 	}
1428ce662b44SJohannes Berg 
1429ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1430ce662b44SJohannes Berg 
1431ce662b44SJohannes Berg 	/*
1432ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1433ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1434deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1435deeaee19SJohannes Berg 	 * ends the poll/service period.
1436ce662b44SJohannes Berg 	 */
143702f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1438deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1439ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1440ce662b44SJohannes Berg 
14416b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
14426b127c71SSujith Manoharan 
1443b77cf4f8SJohannes Berg 	if (call_driver)
1444b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1445b77cf4f8SJohannes Berg 					  reason, false);
144640b96408SJohannes Berg 
144789afe614SJohannes Berg 	skb->dev = sdata->dev;
144889afe614SJohannes Berg 
144955de908aSJohannes Berg 	rcu_read_lock();
145055de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
145155de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
145255de908aSJohannes Berg 		rcu_read_unlock();
145355de908aSJohannes Berg 		kfree_skb(skb);
145455de908aSJohannes Berg 		return;
145555de908aSJohannes Berg 	}
145655de908aSJohannes Berg 
145773c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
1458b9771d41SJohannes Berg 	ieee80211_xmit(sdata, sta, skb, 0);
145955de908aSJohannes Berg 	rcu_read_unlock();
1460ce662b44SJohannes Berg }
1461ce662b44SJohannes Berg 
14620a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
14630a1cb809SJohannes Berg {
14640a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
14650a1cb809SJohannes Berg 	if (tids & 0xF8)
14660a1cb809SJohannes Berg 		return fls(tids) - 1;
14670a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
14680a1cb809SJohannes Berg 	if (tids & BIT(0))
14690a1cb809SJohannes Berg 		return 0;
14700a1cb809SJohannes Berg 	return fls(tids) - 1;
14710a1cb809SJohannes Berg }
14720a1cb809SJohannes Berg 
14730ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
14740ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
14750ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
14760ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
14770ead2510SEmmanuel Grumbach  */
14780ead2510SEmmanuel Grumbach static bool
14790ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
14800ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
14810ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
14820ead2510SEmmanuel Grumbach {
14830ead2510SEmmanuel Grumbach 	int ac;
14840ead2510SEmmanuel Grumbach 
14850ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
14860ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
14870ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
14880ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
14890ead2510SEmmanuel Grumbach 	 */
14900ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
14910ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
14920ead2510SEmmanuel Grumbach 		return true;
14930ead2510SEmmanuel Grumbach 
14940ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1495f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
14960ead2510SEmmanuel Grumbach 			continue;
14970ead2510SEmmanuel Grumbach 
14980ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14990ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
15000ead2510SEmmanuel Grumbach 			return true;
15010ead2510SEmmanuel Grumbach 	}
15020ead2510SEmmanuel Grumbach 
15030ead2510SEmmanuel Grumbach 	return false;
15040ead2510SEmmanuel Grumbach }
15050ead2510SEmmanuel Grumbach 
150647086fc5SJohannes Berg static void
15070ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
15080ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
15090ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
15100ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1511af818581SJohannes Berg {
1512af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1513af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1514948d887dSJohannes Berg 	int ac;
1515af818581SJohannes Berg 
1516f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1517948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
15184049e09aSJohannes Berg 		unsigned long tids;
15194049e09aSJohannes Berg 
1520f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1521948d887dSJohannes Berg 			continue;
1522948d887dSJohannes Berg 
15234049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
15244049e09aSJohannes Berg 
1525f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1526f9f760b4SJohannes Berg 		 * release from hardware queues
1527f9f760b4SJohannes Berg 		 */
15280ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
15290ead2510SEmmanuel Grumbach 			*driver_release_tids |=
15300ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
15310ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1532ba8c3d6fSFelix Fietkau 		}
1533f9f760b4SJohannes Berg 
15340ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
153547086fc5SJohannes Berg 			struct sk_buff *skb;
153647086fc5SJohannes Berg 
153747086fc5SJohannes Berg 			while (n_frames > 0) {
1538948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1539948d887dSJohannes Berg 				if (!skb) {
154047086fc5SJohannes Berg 					skb = skb_dequeue(
154147086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1542af818581SJohannes Berg 					if (skb)
1543af818581SJohannes Berg 						local->total_ps_buffered--;
1544af818581SJohannes Berg 				}
154547086fc5SJohannes Berg 				if (!skb)
154647086fc5SJohannes Berg 					break;
154747086fc5SJohannes Berg 				n_frames--;
15480ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
154947086fc5SJohannes Berg 			}
1550948d887dSJohannes Berg 		}
1551948d887dSJohannes Berg 
15520ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
15530ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
15540ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
15554049e09aSJohannes Berg 		 */
1556948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15570ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1558948d887dSJohannes Berg 			break;
1559948d887dSJohannes Berg 	}
1560948d887dSJohannes Berg }
1561af818581SJohannes Berg 
15620ead2510SEmmanuel Grumbach static void
15630ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
15640ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
15650ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
15660ead2510SEmmanuel Grumbach {
15670ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
15680ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
15690ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
15700ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
15710ead2510SEmmanuel Grumbach 	bool more_data;
15720ead2510SEmmanuel Grumbach 
15730ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
15740ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
15750ead2510SEmmanuel Grumbach 
15760ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
15770ead2510SEmmanuel Grumbach 
15780ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
15790ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
15800ead2510SEmmanuel Grumbach 
15810ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
15820ead2510SEmmanuel Grumbach 
15831a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
15840ead2510SEmmanuel Grumbach 		driver_release_tids =
15850ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
15860ead2510SEmmanuel Grumbach 
1587f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1588f438ceb8SEmmanuel Grumbach 		int tid, ac;
15894049e09aSJohannes Berg 
1590ce662b44SJohannes Berg 		/*
1591ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1592ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1593ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1594ce662b44SJohannes Berg 		 *
1595ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1596ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1597ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1598ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1599ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1600ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1601ce662b44SJohannes Berg 		 *
1602ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1603ce662b44SJohannes Berg 		 */
1604ce662b44SJohannes Berg 
1605ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1606f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1607d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1608d7f84244SEmmanuel Grumbach 				break;
1609f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1610ce662b44SJohannes Berg 
16110ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1612f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
161347086fc5SJohannes Berg 		struct sk_buff_head pending;
161447086fc5SJohannes Berg 		struct sk_buff *skb;
161540b96408SJohannes Berg 		int num = 0;
161640b96408SJohannes Berg 		u16 tids = 0;
1617b77cf4f8SJohannes Berg 		bool need_null = false;
161847086fc5SJohannes Berg 
161947086fc5SJohannes Berg 		skb_queue_head_init(&pending);
162047086fc5SJohannes Berg 
162147086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1622af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
162347086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
162440b96408SJohannes Berg 			u8 *qoshdr = NULL;
162540b96408SJohannes Berg 
162640b96408SJohannes Berg 			num++;
1627af818581SJohannes Berg 
1628af818581SJohannes Berg 			/*
162947086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
163047086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
163147086fc5SJohannes Berg 			 * exchange.
1632af818581SJohannes Berg 			 */
16336b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
16346b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1635af818581SJohannes Berg 
163647086fc5SJohannes Berg 			/*
163747086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
163847086fc5SJohannes Berg 			 * more buffered frames for this STA
163947086fc5SJohannes Berg 			 */
164024b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
164147086fc5SJohannes Berg 				hdr->frame_control |=
164247086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
164324b9c373SJanusz.Dziedzic@tieto.com 			else
164424b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
164524b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1646af818581SJohannes Berg 
164740b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
164840b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
164940b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
165040b96408SJohannes Berg 
1651b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1652b77cf4f8SJohannes Berg 
1653b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1654b77cf4f8SJohannes Berg 
1655b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1656b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1657b77cf4f8SJohannes Berg 				continue;
1658b77cf4f8SJohannes Berg 
1659b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1660b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1661b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1662b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1663b77cf4f8SJohannes Berg 				break;
1664b77cf4f8SJohannes Berg 			}
1665b77cf4f8SJohannes Berg 
1666b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1667b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1668b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1669b77cf4f8SJohannes Berg 			 * and be done.
1670b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1671b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1672b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1673b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1674b77cf4f8SJohannes Berg 			 *
1675b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1676b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1677b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1678b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1679b77cf4f8SJohannes Berg 			 *
1680b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1681b77cf4f8SJohannes Berg 			 */
1682b77cf4f8SJohannes Berg 			if (qoshdr) {
168340b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1684deeaee19SJohannes Berg 
168547086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
168647086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1687b77cf4f8SJohannes Berg 			} else {
1688b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1689b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1690b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1691b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1692b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1693b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1694b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1695b77cf4f8SJohannes Berg 				 */
1696b77cf4f8SJohannes Berg 				hdr->frame_control |=
1697b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1698b77cf4f8SJohannes Berg 				need_null = true;
1699b77cf4f8SJohannes Berg 				num++;
170052a3f20cSMarco Porsch 			}
1701b77cf4f8SJohannes Berg 			break;
170247086fc5SJohannes Berg 		}
170347086fc5SJohannes Berg 
170440b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
170540b96408SJohannes Berg 					  reason, more_data);
170640b96408SJohannes Berg 
170747086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1708af818581SJohannes Berg 
1709b77cf4f8SJohannes Berg 		if (need_null)
1710b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
17110ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
17120ead2510SEmmanuel Grumbach 				reason, false, false);
1713b77cf4f8SJohannes Berg 
1714c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1715af818581SJohannes Berg 	} else {
1716ba8c3d6fSFelix Fietkau 		int tid;
1717ba8c3d6fSFelix Fietkau 
1718af818581SJohannes Berg 		/*
17194049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
17204049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1721f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1722f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1723f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1724f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1725f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1726f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1727af818581SJohannes Berg 		 */
17284049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
172947086fc5SJohannes Berg 					    n_frames, reason, more_data);
17304049e09aSJohannes Berg 
17314049e09aSJohannes Berg 		/*
17324049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
17334049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1734f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
17354049e09aSJohannes Berg 		 * release function.
1736f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1737ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1738ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
17394049e09aSJohannes Berg 		 */
1740ba8c3d6fSFelix Fietkau 
1741ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1742ba8c3d6fSFelix Fietkau 			return;
1743ba8c3d6fSFelix Fietkau 
1744ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1745adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1746adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
17471e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1748ba8c3d6fSFelix Fietkau 				continue;
1749ba8c3d6fSFelix Fietkau 
1750ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1751ba8c3d6fSFelix Fietkau 			break;
1752ba8c3d6fSFelix Fietkau 		}
1753af818581SJohannes Berg 	}
1754af818581SJohannes Berg }
1755af818581SJohannes Berg 
1756af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1757af818581SJohannes Berg {
175847086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1759af818581SJohannes Berg 
1760af818581SJohannes Berg 	/*
176147086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
176247086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
176347086fc5SJohannes Berg 	 * only from the non-enabled ones.
1764af818581SJohannes Berg 	 */
176547086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
176647086fc5SJohannes Berg 		ignore_for_response = 0;
1767af818581SJohannes Berg 
176847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
176947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1770af818581SJohannes Berg }
177147086fc5SJohannes Berg 
177247086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
177347086fc5SJohannes Berg {
177447086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
177547086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
177647086fc5SJohannes Berg 
177747086fc5SJohannes Berg 	/*
177847086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
177947086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
178047086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
178147086fc5SJohannes Berg 	 * actually getting called.
178247086fc5SJohannes Berg 	 */
178347086fc5SJohannes Berg 	if (!delivery_enabled)
178447086fc5SJohannes Berg 		return;
178547086fc5SJohannes Berg 
178647086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
178747086fc5SJohannes Berg 	case 1:
178847086fc5SJohannes Berg 		n_frames = 2;
178947086fc5SJohannes Berg 		break;
179047086fc5SJohannes Berg 	case 2:
179147086fc5SJohannes Berg 		n_frames = 4;
179247086fc5SJohannes Berg 		break;
179347086fc5SJohannes Berg 	case 3:
179447086fc5SJohannes Berg 		n_frames = 6;
179547086fc5SJohannes Berg 		break;
179647086fc5SJohannes Berg 	case 0:
179747086fc5SJohannes Berg 		/* XXX: what is a good value? */
179813a8098aSAndrei Otcheretianski 		n_frames = 128;
179947086fc5SJohannes Berg 		break;
180047086fc5SJohannes Berg 	}
180147086fc5SJohannes Berg 
180247086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
180347086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1804af818581SJohannes Berg }
1805af818581SJohannes Berg 
1806af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1807af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1808af818581SJohannes Berg {
1809af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1810af818581SJohannes Berg 
1811b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1812b5878a2dSJohannes Berg 
18135ac2e350SJohannes Berg 	if (block) {
1814c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
181517c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
18165ac2e350SJohannes Berg 		return;
18175ac2e350SJohannes Berg 	}
18185ac2e350SJohannes Berg 
18195ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
18205ac2e350SJohannes Berg 		return;
18215ac2e350SJohannes Berg 
18225ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
18235ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
18245ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18255ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18265ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
18275ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
18285ac2e350SJohannes Berg 		/* must be asleep in this case */
18295ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18305ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18315ac2e350SJohannes Berg 	} else {
18325ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
183317c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
18345ac2e350SJohannes Berg 	}
1835af818581SJohannes Berg }
1836af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1837dcf55fb5SFelix Fietkau 
1838e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
183937fbd908SJohannes Berg {
184037fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
184137fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
184237fbd908SJohannes Berg 
184337fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
184437fbd908SJohannes Berg 
184537fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
184637fbd908SJohannes Berg }
1847e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
184837fbd908SJohannes Berg 
18490ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
18500ead2510SEmmanuel Grumbach {
18510ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
18520ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
18530ead2510SEmmanuel Grumbach 	bool more_data;
18540ead2510SEmmanuel Grumbach 
18550ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
18560ead2510SEmmanuel Grumbach 
18570ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
18580ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
18590ead2510SEmmanuel Grumbach 					       reason, 0);
18600ead2510SEmmanuel Grumbach 
18610ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
18620ead2510SEmmanuel Grumbach }
18630ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
18640ead2510SEmmanuel Grumbach 
1865042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1866042ec453SJohannes Berg 				u8 tid, bool buffered)
1867dcf55fb5SFelix Fietkau {
1868dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1869dcf55fb5SFelix Fietkau 
18705a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1871042ec453SJohannes Berg 		return;
1872042ec453SJohannes Berg 
18731b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
18741b000789SJohannes Berg 
1875948d887dSJohannes Berg 	if (buffered)
1876948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1877948d887dSJohannes Berg 	else
1878948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1879948d887dSJohannes Berg 
1880c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1881dcf55fb5SFelix Fietkau }
1882042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1883d9a7ddb0SJohannes Berg 
1884b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
1885b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
1886b4809e94SToke Høiland-Jørgensen {
1887b4809e94SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1888b4809e94SToke Høiland-Jørgensen 	struct ieee80211_local *local = sta->sdata->local;
1889b4809e94SToke Høiland-Jørgensen 	u8 ac = ieee80211_ac_from_tid(tid);
1890b4809e94SToke Høiland-Jørgensen 	u32 airtime = 0;
1891b4809e94SToke Høiland-Jørgensen 
1892b4809e94SToke Høiland-Jørgensen 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
1893b4809e94SToke Høiland-Jørgensen 		airtime += tx_airtime;
1894b4809e94SToke Høiland-Jørgensen 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
1895b4809e94SToke Høiland-Jørgensen 		airtime += rx_airtime;
1896b4809e94SToke Høiland-Jørgensen 
1897b4809e94SToke Høiland-Jørgensen 	spin_lock_bh(&local->active_txq_lock[ac]);
1898b4809e94SToke Høiland-Jørgensen 	sta->airtime[ac].tx_airtime += tx_airtime;
1899b4809e94SToke Høiland-Jørgensen 	sta->airtime[ac].rx_airtime += rx_airtime;
1900b4809e94SToke Høiland-Jørgensen 	sta->airtime[ac].deficit -= airtime;
1901b4809e94SToke Høiland-Jørgensen 	spin_unlock_bh(&local->active_txq_lock[ac]);
1902b4809e94SToke Høiland-Jørgensen }
1903b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
1904b4809e94SToke Høiland-Jørgensen 
19053ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
19063ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
19073ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
19083ace10f5SKan Yan {
19093ace10f5SKan Yan 	int tx_pending;
19103ace10f5SKan Yan 
1911911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
1912911bde0fSToke Høiland-Jørgensen 		return;
1913911bde0fSToke Høiland-Jørgensen 
19143ace10f5SKan Yan 	if (!tx_completed) {
19153ace10f5SKan Yan 		if (sta)
19163ace10f5SKan Yan 			atomic_add(tx_airtime,
19173ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
19183ace10f5SKan Yan 
19193ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
19203ace10f5SKan Yan 		return;
19213ace10f5SKan Yan 	}
19223ace10f5SKan Yan 
19233ace10f5SKan Yan 	if (sta) {
19243ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
19253ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
19263ace10f5SKan Yan 		if (WARN_ONCE(tx_pending < 0,
19273ace10f5SKan Yan 			      "STA %pM AC %d txq pending airtime underflow: %u, %u",
19283ace10f5SKan Yan 			      sta->addr, ac, tx_pending, tx_airtime))
19293ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
19303ace10f5SKan Yan 				       tx_pending, 0);
19313ace10f5SKan Yan 	}
19323ace10f5SKan Yan 
19333ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
19343ace10f5SKan Yan 				       &local->aql_total_pending_airtime);
19353ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
19363ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
19373ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
19383ace10f5SKan Yan 		      tx_airtime))
19393ace10f5SKan Yan 		atomic_cmpxchg(&local->aql_total_pending_airtime,
19403ace10f5SKan Yan 			       tx_pending, 0);
19413ace10f5SKan Yan }
19423ace10f5SKan Yan 
194383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1944d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1945d9a7ddb0SJohannes Berg {
19468bf11d8dSJohannes Berg 	might_sleep();
1947d9a7ddb0SJohannes Berg 
1948d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1949d9a7ddb0SJohannes Berg 		return 0;
1950d9a7ddb0SJohannes Berg 
1951f09603a2SJohannes Berg 	/* check allowed transitions first */
1952f09603a2SJohannes Berg 
1953d9a7ddb0SJohannes Berg 	switch (new_state) {
1954d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1955f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1956d9a7ddb0SJohannes Berg 			return -EINVAL;
1957d9a7ddb0SJohannes Berg 		break;
1958d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1959f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1960f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1961d9a7ddb0SJohannes Berg 			return -EINVAL;
1962d9a7ddb0SJohannes Berg 		break;
1963d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1964f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1965f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1966d9a7ddb0SJohannes Berg 			return -EINVAL;
1967d9a7ddb0SJohannes Berg 		break;
1968d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1969f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1970d9a7ddb0SJohannes Berg 			return -EINVAL;
1971d9a7ddb0SJohannes Berg 		break;
1972d9a7ddb0SJohannes Berg 	default:
1973d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1974d9a7ddb0SJohannes Berg 		return -EINVAL;
1975d9a7ddb0SJohannes Berg 	}
1976d9a7ddb0SJohannes Berg 
1977bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1978bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1979f09603a2SJohannes Berg 
1980f09603a2SJohannes Berg 	/*
1981f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1982f09603a2SJohannes Berg 	 * fail the transition
1983f09603a2SJohannes Berg 	 */
1984f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1985f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1986f09603a2SJohannes Berg 					sta->sta_state, new_state);
1987f09603a2SJohannes Berg 		if (err)
1988f09603a2SJohannes Berg 			return err;
1989f09603a2SJohannes Berg 	}
1990f09603a2SJohannes Berg 
1991f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1992f09603a2SJohannes Berg 
1993f09603a2SJohannes Berg 	switch (new_state) {
1994f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1995f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1996f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1997f09603a2SJohannes Berg 		break;
1998f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1999a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2000f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2001a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2002f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2003a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
200452cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
200552cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2006a7201a6cSIlan Peer 		}
2007f09603a2SJohannes Berg 		break;
2008f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2009f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2010f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
20119cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
2012a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
201352cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
201452cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2015f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
201672f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2017f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
201817c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
201949ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2020f09603a2SJohannes Berg 		}
2021f09603a2SJohannes Berg 		break;
2022f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2023f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
202472f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2025f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
202617c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
202749ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2028f09603a2SJohannes Berg 		}
20293e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
20303e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
20313e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
20323e493173SJouni Malinen 						    sta->sta.addr);
2033f09603a2SJohannes Berg 		break;
2034f09603a2SJohannes Berg 	default:
2035f09603a2SJohannes Berg 		break;
2036f09603a2SJohannes Berg 	}
2037f09603a2SJohannes Berg 
2038d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2039d9a7ddb0SJohannes Berg 
2040d9a7ddb0SJohannes Berg 	return 0;
2041d9a7ddb0SJohannes Berg }
2042687da132SEmmanuel Grumbach 
2043687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
2044687da132SEmmanuel Grumbach {
2045687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
2046687da132SEmmanuel Grumbach 	u8 rx_streams;
2047687da132SEmmanuel Grumbach 
2048687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
2049687da132SEmmanuel Grumbach 		return 1;
2050687da132SEmmanuel Grumbach 
2051687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
2052687da132SEmmanuel Grumbach 		int i;
2053687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
2054687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
2055687da132SEmmanuel Grumbach 
2056687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
2057687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
2058687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
2059687da132SEmmanuel Grumbach 				return i + 1;
2060687da132SEmmanuel Grumbach 	}
2061687da132SEmmanuel Grumbach 
2062687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
2063687da132SEmmanuel Grumbach 		rx_streams = 4;
2064687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
2065687da132SEmmanuel Grumbach 		rx_streams = 3;
2066687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
2067687da132SEmmanuel Grumbach 		rx_streams = 2;
2068687da132SEmmanuel Grumbach 	else
2069687da132SEmmanuel Grumbach 		rx_streams = 1;
2070687da132SEmmanuel Grumbach 
2071687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
2072687da132SEmmanuel Grumbach 		return rx_streams;
2073687da132SEmmanuel Grumbach 
2074687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
2075687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
2076687da132SEmmanuel Grumbach }
2077b7ffbd7eSJohannes Berg 
2078c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2079c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2080c9c5962bSJohannes Berg {
2081c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
2082c9c5962bSJohannes Berg 	struct ieee80211_local *local = sta->local;
2083c9c5962bSJohannes Berg 	int cpu;
2084c9c5962bSJohannes Berg 
2085c9c5962bSJohannes Berg 	if (!ieee80211_hw_check(&local->hw, USES_RSS))
2086c9c5962bSJohannes Berg 		return stats;
2087c9c5962bSJohannes Berg 
2088c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2089c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2090c9c5962bSJohannes Berg 
2091c9c5962bSJohannes Berg 		cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2092c9c5962bSJohannes Berg 
2093c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2094c9c5962bSJohannes Berg 			stats = cpustats;
2095c9c5962bSJohannes Berg 	}
2096c9c5962bSJohannes Berg 
2097c9c5962bSJohannes Berg 	return stats;
2098c9c5962bSJohannes Berg }
2099c9c5962bSJohannes Berg 
210041cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
21014f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2102fbd6ff5cSJohannes Berg {
2103dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2104fbd6ff5cSJohannes Berg 
2105dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
21067f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
21074f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2108dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2109dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2110dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2111dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
21127f406cd1SJohannes Berg 		break;
21137f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
21144f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2115dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2116dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2117dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
21187f406cd1SJohannes Berg 		break;
21197f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2120fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2121fbd6ff5cSJohannes Berg 		u16 brate;
21224f6b1b3dSJohannes Berg 		unsigned int shift;
2123dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2124dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2125fbd6ff5cSJohannes Berg 
2126dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
2127dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
21284f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
21294f6b1b3dSJohannes Berg 			shift = 2;
21304f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
21314f6b1b3dSJohannes Berg 			shift = 1;
21324f6b1b3dSJohannes Berg 		else
21334f6b1b3dSJohannes Berg 			shift = 0;
2134fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
21357f406cd1SJohannes Berg 		break;
21367f406cd1SJohannes Berg 		}
213741cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
213841cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
213941cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
214041cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
214141cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
214241cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
214341cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
214441cbb0f5SLuca Coelho 		break;
2145fbd6ff5cSJohannes Berg 	}
21464f6b1b3dSJohannes Berg }
2147fbd6ff5cSJohannes Berg 
2148a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
21494f6b1b3dSJohannes Berg {
21506aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
21514f6b1b3dSJohannes Berg 
21524f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2153a17d93ffSBen Greear 		return -EINVAL;
2154a17d93ffSBen Greear 
21554f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2156a17d93ffSBen Greear 	return 0;
2157fbd6ff5cSJohannes Berg }
2158fbd6ff5cSJohannes Berg 
2159b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2160b255b72bSSeevalamuthu Mariappan 					int tid)
2161b255b72bSSeevalamuthu Mariappan {
2162b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2163b255b72bSSeevalamuthu Mariappan 	u64 value;
2164b255b72bSSeevalamuthu Mariappan 
2165b255b72bSSeevalamuthu Mariappan 	do {
2166b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2167b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2168b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2169b255b72bSSeevalamuthu Mariappan 
2170b255b72bSSeevalamuthu Mariappan 	return value;
2171b255b72bSSeevalamuthu Mariappan }
2172b255b72bSSeevalamuthu Mariappan 
21730f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
21740f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
21750f9c5a61SJohannes Berg 			     int tid)
21760f9c5a61SJohannes Berg {
21770f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2178b255b72bSSeevalamuthu Mariappan 	int cpu;
21790f9c5a61SJohannes Berg 
21800f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2181b255b72bSSeevalamuthu Mariappan 		if (!ieee80211_hw_check(&local->hw, USES_RSS))
2182b255b72bSSeevalamuthu Mariappan 			tidstats->rx_msdu +=
2183b255b72bSSeevalamuthu Mariappan 				sta_get_tidstats_msdu(&sta->rx_stats, tid);
21840f9c5a61SJohannes Berg 
2185b255b72bSSeevalamuthu Mariappan 		if (sta->pcpu_rx_stats) {
2186b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2187b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2188b255b72bSSeevalamuthu Mariappan 
2189b255b72bSSeevalamuthu Mariappan 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2190b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2191b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2192b255b72bSSeevalamuthu Mariappan 			}
2193b255b72bSSeevalamuthu Mariappan 		}
21940f9c5a61SJohannes Berg 
21950f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
21960f9c5a61SJohannes Berg 	}
21970f9c5a61SJohannes Berg 
21980f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
21990f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
22000f9c5a61SJohannes Berg 		tidstats->tx_msdu = sta->tx_stats.msdu[tid];
22010f9c5a61SJohannes Berg 	}
22020f9c5a61SJohannes Berg 
22030f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
22040f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22050f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
22060f9c5a61SJohannes Berg 		tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
22070f9c5a61SJohannes Berg 	}
22080f9c5a61SJohannes Berg 
22090f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
22100f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22110f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
22120f9c5a61SJohannes Berg 		tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
22130f9c5a61SJohannes Berg 	}
22142fe4a29aSToke Høiland-Jørgensen 
22152fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
22162fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
22172fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
22182fe4a29aSToke Høiland-Jørgensen 
22192fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
22202fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
22212fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
22222fe4a29aSToke Høiland-Jørgensen 
22232fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
22242fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
22252fe4a29aSToke Høiland-Jørgensen 	}
22260f9c5a61SJohannes Berg }
22270f9c5a61SJohannes Berg 
2228c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2229c9c5962bSJohannes Berg {
2230c9c5962bSJohannes Berg 	unsigned int start;
2231c9c5962bSJohannes Berg 	u64 value;
2232c9c5962bSJohannes Berg 
2233c9c5962bSJohannes Berg 	do {
2234c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2235c9c5962bSJohannes Berg 		value = rxstats->bytes;
2236c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2237c9c5962bSJohannes Berg 
2238c9c5962bSJohannes Berg 	return value;
2239c9c5962bSJohannes Berg }
2240c9c5962bSJohannes Berg 
22410fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
22420fdf1493SJohannes Berg 		   bool tidstats)
2243b7ffbd7eSJohannes Berg {
2244b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2245b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2246b7ffbd7eSJohannes Berg 	u32 thr = 0;
2247c9c5962bSJohannes Berg 	int i, ac, cpu;
2248c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2249c9c5962bSJohannes Berg 
2250c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2251b7ffbd7eSJohannes Berg 
2252b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2253b7ffbd7eSJohannes Berg 
2254225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2255225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2256225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2257225b8189SJohannes Berg 	 */
2258225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2259225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2260225b8189SJohannes Berg 
22612b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
22622b9a7e1bSJohannes Berg 
2263a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2264a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2265a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2266a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
22679cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2268a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2269976bd9efSJohannes Berg 
2270976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2271976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2272a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2273976bd9efSJohannes Berg 	}
2274b7ffbd7eSJohannes Berg 
227584b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
22769cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2277e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2278b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
22792b9a7e1bSJohannes Berg 
2280a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2281a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2282b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
22832b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2284e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2285a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2286b7ffbd7eSJohannes Berg 	}
22872b9a7e1bSJohannes Berg 
2288a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
22892b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
22902b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2291e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
2292a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
22932b9a7e1bSJohannes Berg 	}
22942b9a7e1bSJohannes Berg 
2295a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2296a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2297b255b72bSSeevalamuthu Mariappan 		if (!ieee80211_hw_check(&local->hw, USES_RSS))
2298c9c5962bSJohannes Berg 			sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
22990f9c5a61SJohannes Berg 
2300c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2301c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2302c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2303c9c5962bSJohannes Berg 
2304c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2305c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2306c9c5962bSJohannes Berg 			}
2307c9c5962bSJohannes Berg 		}
2308c9c5962bSJohannes Berg 
2309a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
23102b9a7e1bSJohannes Berg 	}
23112b9a7e1bSJohannes Berg 
2312a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2313e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
2314c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2315c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2316c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2317c9c5962bSJohannes Berg 
2318c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2319c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2320c9c5962bSJohannes Berg 			}
2321c9c5962bSJohannes Berg 		}
2322a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
23232b9a7e1bSJohannes Berg 	}
23242b9a7e1bSJohannes Berg 
2325a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2326e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
2327a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
23282b9a7e1bSJohannes Berg 	}
23292b9a7e1bSJohannes Berg 
2330a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2331e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
2332a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
23332b9a7e1bSJohannes Berg 	}
23342b9a7e1bSJohannes Berg 
2335b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2336b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2337b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2338b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2339b4809e94SToke Høiland-Jørgensen 	}
2340b4809e94SToke Høiland-Jørgensen 
2341b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2342b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2343b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2344b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2345b4809e94SToke Høiland-Jørgensen 	}
2346b4809e94SToke Høiland-Jørgensen 
2347b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2348b4809e94SToke Høiland-Jørgensen 		sinfo->airtime_weight = sta->airtime_weight;
2349b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2350b4809e94SToke Høiland-Jørgensen 	}
2351b4809e94SToke Høiland-Jørgensen 
2352e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2353c9c5962bSJohannes Berg 	if (sta->pcpu_rx_stats) {
2354c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2355c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2356c9c5962bSJohannes Berg 
2357c9c5962bSJohannes Berg 			cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2358e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2359c9c5962bSJohannes Berg 		}
2360c9c5962bSJohannes Berg 	}
2361b7ffbd7eSJohannes Berg 
2362225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2363225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2364a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2365a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2366225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2367225b8189SJohannes Berg 	}
2368225b8189SJohannes Berg 
236930686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
237030686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2371a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2372c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2373a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2374b7ffbd7eSJohannes Berg 		}
23752b9a7e1bSJohannes Berg 
2376c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats &&
2377a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
237840d9a38aSJohannes Berg 			sinfo->signal_avg =
23790be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.signal);
2380a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
23812b9a7e1bSJohannes Berg 		}
23822b9a7e1bSJohannes Berg 	}
23832b9a7e1bSJohannes Berg 
2384c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2385c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2386c9c5962bSJohannes Berg 	 * pcpu statistics
2387c9c5962bSJohannes Berg 	 */
2388c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2389a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2390a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2391a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2392c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
2393a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2394b7ffbd7eSJohannes Berg 
2395c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2396c9c5962bSJohannes Berg 
2397b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2398e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2399c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2400b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
24010be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2402b7ffbd7eSJohannes Berg 		}
2403b7ffbd7eSJohannes Berg 	}
2404b7ffbd7eSJohannes Berg 
2405a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2406e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2407e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2408a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
24092b9a7e1bSJohannes Berg 	}
24102b9a7e1bSJohannes Berg 
2411a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2412a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2413a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
24142b9a7e1bSJohannes Berg 	}
2415b7ffbd7eSJohannes Berg 
24160fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
24176af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
24186af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
24198689c051SArend van Spriel 	}
242079c892b8SJohannes Berg 
2421b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2422b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2423a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2424a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2425a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2426a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2427a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2428dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2429dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE);
2430b7ffbd7eSJohannes Berg 
2431433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2432433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2433433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2434b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2435a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2436433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2437b7ffbd7eSJohannes Berg 		}
2438433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2439433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2440433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2441dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2442b7ffbd7eSJohannes Berg #endif
2443b7ffbd7eSJohannes Berg 	}
2444b7ffbd7eSJohannes Berg 
2445b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2446b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2447b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2448b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2449b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2450b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2451b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2452785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2453b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2454b7ffbd7eSJohannes Berg 
2455b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2456b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2457b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2458b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2459b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2460b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2461b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2462b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2463b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2464b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2465b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2466b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2467a74a8c84SJohannes Berg 	if (sta->sta.wme)
2468b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2469b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2470b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2471b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2472b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2473b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2474b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2475b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2476b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2477b7ffbd7eSJohannes Berg 
24783b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
24793b17fbf8SMaxim Altshul 
24803b17fbf8SMaxim Altshul 	if (thr != 0) {
2481a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
24823b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
24833b17fbf8SMaxim Altshul 	}
2484a78b26ffSVenkateswara Naralasetty 
2485a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2486a78b26ffSVenkateswara Naralasetty 	    sta->status_stats.ack_signal_filled) {
2487a78b26ffSVenkateswara Naralasetty 		sinfo->ack_signal = sta->status_stats.last_ack_signal;
2488a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2489a78b26ffSVenkateswara Naralasetty 	}
2490cc60dbbfSBalaji Pothunoori 
24919c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
24929c06602bSBalaji Pothunoori 	    sta->status_stats.ack_signal_filled) {
2493cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2494cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2495cc60dbbfSBalaji Pothunoori 				&sta->status_stats.avg_ack_signal);
2496cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
24979c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2498cc60dbbfSBalaji Pothunoori 	}
2499ab60633cSNarayanraddi Masti 
2500ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2501ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2502ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2503ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2504ab60633cSNarayanraddi Masti 	}
25053b17fbf8SMaxim Altshul }
25063b17fbf8SMaxim Altshul 
25073b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
25083b17fbf8SMaxim Altshul {
25093b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
25103b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
25113b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
25123b17fbf8SMaxim Altshul 	u32 thr = 0;
25133b17fbf8SMaxim Altshul 
25143b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
25153b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
25163b17fbf8SMaxim Altshul 
2517b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2518b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2519b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2520b7ffbd7eSJohannes Berg 	else
25214fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2522b7ffbd7eSJohannes Berg 
25233b17fbf8SMaxim Altshul 	return thr;
2524b7ffbd7eSJohannes Berg }
2525b8da6b6aSJohannes Berg 
2526b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2527b8da6b6aSJohannes Berg {
2528c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2529c9c5962bSJohannes Berg 
2530285531f9SAhmed Zaki 	if (!sta->status_stats.last_ack ||
2531285531f9SAhmed Zaki 	    time_after(stats->last_rx, sta->status_stats.last_ack))
2532c9c5962bSJohannes Berg 		return stats->last_rx;
2533b8da6b6aSJohannes Berg 	return sta->status_stats.last_ack;
2534b8da6b6aSJohannes Berg }
2535484a54c2SToke Høiland-Jørgensen 
2536484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2537484a54c2SToke Høiland-Jørgensen {
2538484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2539484a54c2SToke Høiland-Jørgensen 		return;
2540484a54c2SToke Høiland-Jørgensen 
2541484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2542484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2543484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2544484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2545484a54c2SToke Høiland-Jørgensen 	} else {
2546484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2547484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2548484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2549484a54c2SToke Høiland-Jørgensen 	}
2550484a54c2SToke Høiland-Jørgensen }
2551484a54c2SToke Høiland-Jørgensen 
2552484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2553484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2554484a54c2SToke Høiland-Jørgensen {
2555484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2556484a54c2SToke Høiland-Jørgensen 
2557484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2558484a54c2SToke Høiland-Jørgensen }
2559