xref: /openbmc/linux/net/mac80211/sta_info.c (revision d0a9123ef548def5c8880e83e5df948eb5b55c62)
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
73a11ce08SJohannes Berg  * Copyright (C) 2018-2021 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 
2348ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
2358ca47eb9SMadhuparna 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 {
261dcd479e1SJohannes Berg 	/*
262dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
263dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
264dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
265dcd479e1SJohannes Berg 	 *
266dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
267dcd479e1SJohannes Berg 	 * allocated during state transitions.
268dcd479e1SJohannes Berg 	 */
269dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
270dcd479e1SJohannes Berg 		int ret;
271dcd479e1SJohannes Berg 
272dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
273dcd479e1SJohannes Berg 
274dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
275dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
276dcd479e1SJohannes Berg 			break;
277dcd479e1SJohannes Berg 	}
278dcd479e1SJohannes Berg 
279889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2804b7679a5SJohannes Berg 		rate_control_free_sta(sta);
28193e5deb1SJohannes Berg 
282bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
28393e5deb1SJohannes Berg 
284ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
285ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
28653d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
287433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
288433f5bc1SJohannes Berg 	kfree(sta->mesh);
289433f5bc1SJohannes Berg #endif
290046d2e7cSSriram R 	free_percpu(sta->deflink.pcpu_rx_stats);
29193e5deb1SJohannes Berg 	kfree(sta);
29293e5deb1SJohannes Berg }
29393e5deb1SJohannes Berg 
2944d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
29562b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
296d0709a65SJohannes Berg 			     struct sta_info *sta)
297f0706e82SJiri Benc {
29883e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
2997bedd0cfSJohannes Berg 			       sta_rht_params);
300f0706e82SJiri Benc }
301f0706e82SJiri Benc 
3025ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
303af818581SJohannes Berg {
304af818581SJohannes Berg 	struct sta_info *sta;
305af818581SJohannes Berg 
3065ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
307af818581SJohannes Berg 
308af818581SJohannes Berg 	if (sta->dead)
309af818581SJohannes Berg 		return;
310af818581SJohannes Berg 
31154420473SHelmut Schaa 	local_bh_disable();
3125ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
313af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
3145ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
315af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
3165ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
31747086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
318ce662b44SJohannes Berg 	local_bh_enable();
319af818581SJohannes Berg }
320af818581SJohannes Berg 
321af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
322af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
323af65cd96SJohannes Berg {
32430686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
325af65cd96SJohannes Berg 		return 0;
326af65cd96SJohannes Berg 
327889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
328af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
32935c347acSJohannes Berg 						     sta, gfp);
330889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
331af65cd96SJohannes Berg 		return -ENOMEM;
332af65cd96SJohannes Berg 
333af65cd96SJohannes Berg 	return 0;
334af65cd96SJohannes Berg }
335af65cd96SJohannes Berg 
33673651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
33756544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
338f0706e82SJiri Benc {
339d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
340ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
341f0706e82SJiri Benc 	struct sta_info *sta;
34216c5f15cSRon Rindjunsky 	int i;
343f0706e82SJiri Benc 
344ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
345f0706e82SJiri Benc 	if (!sta)
34673651ee6SJohannes Berg 		return NULL;
347f0706e82SJiri Benc 
348c9c5962bSJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
349046d2e7cSSriram R 		sta->deflink.pcpu_rx_stats =
35095f3ce6aSSara Sharon 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
351046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
352c9c5962bSJohannes Berg 			goto free;
353c9c5962bSJohannes Berg 	}
354c9c5962bSJohannes Berg 
35507346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3561d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3575ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
35867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
359a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
36087f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
361433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
362433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
363433f5bc1SJohannes Berg 		if (!sta->mesh)
364433f5bc1SJohannes Berg 			goto free;
3654c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
366433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
36745d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
3684c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
3694c02d62fSKees Cook 				    0);
370433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
371433f5bc1SJohannes Berg 	}
37287f59c70SThomas Pedersen #endif
37307346f81SJohannes Berg 
374ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
37517741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
376480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
377480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
378480dd46bSMaxim Altshul 
379046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
380046d2e7cSSriram R 
381046d2e7cSSriram R 	/* For non MLO STA, link info can be accessed either via deflink
382046d2e7cSSriram R 	 * or link[0]
383046d2e7cSSriram R 	 */
384046d2e7cSSriram R 	sta->link[0] = &sta->deflink;
385046d2e7cSSriram R 	sta->sta.link[0] = &sta->sta.deflink;
386046d2e7cSSriram R 
38796fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
38896fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
38996fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
39096fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
39196fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
39296fc6efbSAlexander Wetzel 	 */
39396fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
39496fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
39596fc6efbSAlexander Wetzel 
396d0709a65SJohannes Berg 	sta->local = local;
397d0709a65SJohannes Berg 	sta->sdata = sdata;
398046d2e7cSSriram R 	sta->deflink.rx_stats.last_rx = jiffies;
399f0706e82SJiri Benc 
400046d2e7cSSriram R 	u64_stats_init(&sta->deflink.rx_stats.syncp);
4010f9c5a61SJohannes Berg 
4023a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
4033a11ce08SJohannes Berg 
40471ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
40571ec375cSJohannes Berg 
406b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
407b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
408b6da911bSLiad Kaufman 
40984b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
410046d2e7cSSriram R 	ewma_signal_init(&sta->deflink.rx_stats_avg.signal);
411046d2e7cSSriram R 	ewma_avg_signal_init(&sta->deflink.status_stats.avg_ack_signal);
412046d2e7cSSriram R 	for (i = 0; i < ARRAY_SIZE(sta->deflink.rx_stats_avg.chain_signal); i++)
413046d2e7cSSriram R 		ewma_signal_init(&sta->deflink.rx_stats_avg.chain_signal[i]);
414541a45a1SBruno Randolf 
415ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
416ba8c3d6fSFelix Fietkau 		void *txq_data;
417ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
418ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
419ba8c3d6fSFelix Fietkau 
420ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
421ba8c3d6fSFelix Fietkau 		if (!txq_data)
422ba8c3d6fSFelix Fietkau 			goto free;
423ba8c3d6fSFelix Fietkau 
424ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
425ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
426ba8c3d6fSFelix Fietkau 
427adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
428fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
429abfbc3afSJohannes Berg 		}
430ba8c3d6fSFelix Fietkau 	}
431ba8c3d6fSFelix Fietkau 
432ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
433ba8c3d6fSFelix Fietkau 		goto free_txq;
434f0706e82SJiri Benc 
435b4809e94SToke Høiland-Jørgensen 
436948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
437948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
438948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
4392433647bSToke Høiland-Jørgensen 		init_airtime_info(&sta->airtime[i], &local->airtime[i]);
440948d887dSJohannes Berg 	}
44173651ee6SJohannes Berg 
4425a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
4434be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
444cccaec98SSenthil Balasubramanian 
445bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
446bd718fc1SJohannes Berg 		u32 mandatory = 0;
447bd718fc1SJohannes Berg 		int r;
448bd718fc1SJohannes Berg 
449bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
450bd718fc1SJohannes Berg 			continue;
451bd718fc1SJohannes Berg 
452bd718fc1SJohannes Berg 		switch (i) {
453bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
45463fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
455bd718fc1SJohannes Berg 			/*
456bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
457bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
458bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
459bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
460bd718fc1SJohannes Berg 			 * possible rate anyway.
461bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
462bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
463bd718fc1SJohannes Berg 			 */
464bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
465bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
466bd718fc1SJohannes Berg 			break;
467bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
468bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
469bd718fc1SJohannes Berg 			break;
470bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
471bd718fc1SJohannes Berg 			WARN_ON(1);
472bd718fc1SJohannes Berg 			mandatory = 0;
473bd718fc1SJohannes Berg 			break;
474bd718fc1SJohannes Berg 		}
475bd718fc1SJohannes Berg 
476bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
477bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
478bd718fc1SJohannes Berg 
479bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
480bd718fc1SJohannes Berg 
481bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
482bd718fc1SJohannes Berg 				continue;
483046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
484bd718fc1SJohannes Berg 		}
485bd718fc1SJohannes Berg 	}
486bd718fc1SJohannes Berg 
487af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
488687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
489687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
49021a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
49121a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
49221a8e9ddSMohammed Shafi Shajakhan 
49321a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
49421a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
49521a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
49621a8e9ddSMohammed Shafi Shajakhan 
49721a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
498687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
499687da132SEmmanuel Grumbach 		/*
500687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
501687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
502687da132SEmmanuel Grumbach 		 */
503687da132SEmmanuel Grumbach 		switch (smps) {
504687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
505687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
506687da132SEmmanuel Grumbach 			break;
507687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
508687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
509687da132SEmmanuel Grumbach 			break;
510687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
511687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
512687da132SEmmanuel Grumbach 			break;
513687da132SEmmanuel Grumbach 		default:
514687da132SEmmanuel Grumbach 			WARN_ON(1);
515687da132SEmmanuel Grumbach 		}
516687da132SEmmanuel Grumbach 	}
517af0ed69bSJohannes Berg 
5186e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
5196e0456b5SFelix Fietkau 
520484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
521484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
522484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
523484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
524dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
525dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
526484a54c2SToke Høiland-Jørgensen 
527bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
528ef04a297SJohannes Berg 
529abfbc3afSJohannes Berg 	return sta;
530ba8c3d6fSFelix Fietkau 
531ba8c3d6fSFelix Fietkau free_txq:
532ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
533ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
534ba8c3d6fSFelix Fietkau free:
535046d2e7cSSriram R 	free_percpu(sta->deflink.pcpu_rx_stats);
536433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
537433f5bc1SJohannes Berg 	kfree(sta->mesh);
538433f5bc1SJohannes Berg #endif
539ba8c3d6fSFelix Fietkau 	kfree(sta);
540ba8c3d6fSFelix Fietkau 	return NULL;
54173651ee6SJohannes Berg }
54273651ee6SJohannes Berg 
5438c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
54434e89507SJohannes Berg {
54534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
54634e89507SJohannes Berg 
54703e4497eSJohannes Berg 	/*
54803e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
54903e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
55003e4497eSJohannes Berg 	 * and another CPU turns off the net device.
55103e4497eSJohannes Berg 	 */
5528c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
5538c71df7aSGuy Eilam 		return -ENETDOWN;
55403e4497eSJohannes Berg 
555b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
556deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
5578c71df7aSGuy Eilam 		return -EINVAL;
5588c71df7aSGuy Eilam 
55983e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
56083e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
56183e7e4ceSHerbert Xu 	 * for correctness.
56231104891SJohannes Berg 	 */
56331104891SJohannes Berg 	rcu_read_lock();
56431104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
56531104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
56631104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
56731104891SJohannes Berg 		rcu_read_unlock();
56831104891SJohannes Berg 		return -ENOTUNIQ;
56931104891SJohannes Berg 	}
57031104891SJohannes Berg 	rcu_read_unlock();
57131104891SJohannes Berg 
5728c71df7aSGuy Eilam 	return 0;
57393e5deb1SJohannes Berg }
57444213b5eSJohannes Berg 
575f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
576f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
577f09603a2SJohannes Berg 				     struct sta_info *sta)
578f09603a2SJohannes Berg {
579f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
580f09603a2SJohannes Berg 	int err = 0;
581f09603a2SJohannes Berg 
582f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
583f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
584f09603a2SJohannes Berg 		if (err)
585f09603a2SJohannes Berg 			break;
586f09603a2SJohannes Berg 	}
587f09603a2SJohannes Berg 
588f09603a2SJohannes Berg 	if (!err) {
589a4ec45a4SJohannes Berg 		/*
590a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
591a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
592a4ec45a4SJohannes Berg 		 */
593a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
594f09603a2SJohannes Berg 			sta->uploaded = true;
595f09603a2SJohannes Berg 		return 0;
596f09603a2SJohannes Berg 	}
597f09603a2SJohannes Berg 
598f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
599bdcbd8e0SJohannes Berg 		sdata_info(sdata,
600bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
601bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
602f09603a2SJohannes Berg 		err = 0;
603f09603a2SJohannes Berg 	}
604f09603a2SJohannes Berg 
605f09603a2SJohannes Berg 	/* unwind on error */
606f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
607f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
608f09603a2SJohannes Berg 
609f09603a2SJohannes Berg 	return err;
610f09603a2SJohannes Berg }
611f09603a2SJohannes Berg 
612d405fd8cSGregory Greenman static void
613d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
614d405fd8cSGregory Greenman {
615d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
616d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
617d405fd8cSGregory Greenman 	struct sta_info *sta;
618d405fd8cSGregory Greenman 
619d405fd8cSGregory Greenman 	rcu_read_lock();
620d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
621d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
622d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
623d405fd8cSGregory Greenman 			continue;
624d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
625d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
626d405fd8cSGregory Greenman 			break;
627d405fd8cSGregory Greenman 		}
628d405fd8cSGregory Greenman 	}
629d405fd8cSGregory Greenman 	rcu_read_unlock();
630d405fd8cSGregory Greenman 
631d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
632d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
633d405fd8cSGregory Greenman 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
634d405fd8cSGregory Greenman 	}
635d405fd8cSGregory Greenman }
636d405fd8cSGregory Greenman 
63734e89507SJohannes Berg /*
6388c71df7aSGuy Eilam  * should be called with sta_mtx locked
6398c71df7aSGuy Eilam  * this function replaces the mutex lock
6408c71df7aSGuy Eilam  * with a RCU lock
6418c71df7aSGuy Eilam  */
6424d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
6438c71df7aSGuy Eilam {
6448c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
6458c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
6460c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
6478c71df7aSGuy Eilam 	int err = 0;
6488c71df7aSGuy Eilam 
6498c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
6508c71df7aSGuy Eilam 
6517852e361SJohannes Berg 	/* check if STA exists already */
6527852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
6534d33960bSJohannes Berg 		err = -EEXIST;
6548f9dcc29SAhmed Zaki 		goto out_cleanup;
65534e89507SJohannes Berg 	}
65634e89507SJohannes Berg 
6570c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
6580c2e3842SKoen Vandeputte 	if (!sinfo) {
6590c2e3842SKoen Vandeputte 		err = -ENOMEM;
6608f9dcc29SAhmed Zaki 		goto out_cleanup;
6610c2e3842SKoen Vandeputte 	}
6620c2e3842SKoen Vandeputte 
6634d33960bSJohannes Berg 	local->num_sta++;
6644d33960bSJohannes Berg 	local->sta_generation++;
6654d33960bSJohannes Berg 	smp_mb();
6664d33960bSJohannes Berg 
6675108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
6685108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
6695108ca82SJohannes Berg 
6704d33960bSJohannes Berg 	/* make the station visible */
67162b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
67262b14b24SJohannes Berg 	if (err)
67362b14b24SJohannes Berg 		goto out_drop_sta;
6744d33960bSJohannes Berg 
6752bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
67683d5cc01SJohannes Berg 
6774dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
6784dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
6794dde3c36SMordechay Goodstein 	 */
6804dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
6814dde3c36SMordechay Goodstein 		ieee80211_recalc_min_chandef(sta->sdata);
6824dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
6834dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
6844dde3c36SMordechay Goodstein 	}
6854dde3c36SMordechay Goodstein 
6865108ca82SJohannes Berg 	/* notify driver */
6875108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
6885108ca82SJohannes Berg 	if (err)
6895108ca82SJohannes Berg 		goto out_remove;
6905108ca82SJohannes Berg 
69183d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
692d405fd8cSGregory Greenman 
6935108ca82SJohannes Berg 	/* accept BA sessions now */
6945108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
6954d33960bSJohannes Berg 
6964d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
6974d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
6984d33960bSJohannes Berg 
6990ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
7000ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
7010ef049dcSArnd Bergmann 	kfree(sinfo);
702d0709a65SJohannes Berg 
703bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
704f0706e82SJiri Benc 
70534e89507SJohannes Berg 	/* move reference to rcu-protected */
70634e89507SJohannes Berg 	rcu_read_lock();
70734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
708e9f207f0SJiri Benc 
70973651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
71073651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
71173651ee6SJohannes Berg 
71273651ee6SJohannes Berg 	return 0;
7135108ca82SJohannes Berg  out_remove:
7145108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
7155108ca82SJohannes Berg 	list_del_rcu(&sta->list);
71662b14b24SJohannes Berg  out_drop_sta:
7175108ca82SJohannes Berg 	local->num_sta--;
7185108ca82SJohannes Berg 	synchronize_net();
7198f9dcc29SAhmed Zaki  out_cleanup:
7207bc40aedSJohannes Berg 	cleanup_single_sta(sta);
7214d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
722ea32f065SSudip Mukherjee 	kfree(sinfo);
7234d33960bSJohannes Berg 	rcu_read_lock();
7244d33960bSJohannes Berg 	return err;
7258c71df7aSGuy Eilam }
7268c71df7aSGuy Eilam 
7278c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
7288c71df7aSGuy Eilam {
7298c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
730308f7fcfSZhao, Gang 	int err;
7318c71df7aSGuy Eilam 
7324d33960bSJohannes Berg 	might_sleep();
7334d33960bSJohannes Berg 
73431104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
73531104891SJohannes Berg 
7368c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
7378c71df7aSGuy Eilam 	if (err) {
7387bc40aedSJohannes Berg 		sta_info_free(local, sta);
73931104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
7408c71df7aSGuy Eilam 		rcu_read_lock();
7417bc40aedSJohannes Berg 		return err;
7428c71df7aSGuy Eilam 	}
7438c71df7aSGuy Eilam 
7447bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
745f0706e82SJiri Benc }
746f0706e82SJiri Benc 
74734e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
74834e89507SJohannes Berg {
74934e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
75034e89507SJohannes Berg 
75134e89507SJohannes Berg 	rcu_read_unlock();
75234e89507SJohannes Berg 
75334e89507SJohannes Berg 	return err;
75434e89507SJohannes Berg }
75534e89507SJohannes Berg 
756d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
757004c872eSJohannes Berg {
758004c872eSJohannes Berg 	/*
759004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
760004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
761004c872eSJohannes Berg 	 */
762d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
763004c872eSJohannes Berg }
764004c872eSJohannes Berg 
765d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
766004c872eSJohannes Berg {
767004c872eSJohannes Berg 	/*
768004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
769004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
770004c872eSJohannes Berg 	 */
771d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
772004c872eSJohannes Berg }
773004c872eSJohannes Berg 
7743d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
7753d5839b6SIlan Peer {
7763d5839b6SIlan Peer 	/*
7773d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
7783d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
7793d5839b6SIlan Peer 	 */
7803d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
7813d5839b6SIlan Peer }
7823d5839b6SIlan Peer 
783948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
784004c872eSJohannes Berg {
785948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
786948d887dSJohannes Berg 	switch (ac) {
787948d887dSJohannes Berg 	case IEEE80211_AC_VO:
788948d887dSJohannes Berg 		return BIT(6) | BIT(7);
789948d887dSJohannes Berg 	case IEEE80211_AC_VI:
790948d887dSJohannes Berg 		return BIT(4) | BIT(5);
791948d887dSJohannes Berg 	case IEEE80211_AC_BE:
792948d887dSJohannes Berg 		return BIT(0) | BIT(3);
793948d887dSJohannes Berg 	case IEEE80211_AC_BK:
794948d887dSJohannes Berg 		return BIT(1) | BIT(2);
795948d887dSJohannes Berg 	default:
796948d887dSJohannes Berg 		WARN_ON(1);
797948d887dSJohannes Berg 		return 0;
798d0709a65SJohannes Berg 	}
799004c872eSJohannes Berg }
800004c872eSJohannes Berg 
8019b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
802004c872eSJohannes Berg {
803c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
804d012a605SMarco Porsch 	struct ps_data *ps;
805948d887dSJohannes Berg 	bool indicate_tim = false;
806948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
807948d887dSJohannes Berg 	int ac;
808a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
809004c872eSJohannes Berg 
810d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
811d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
812c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
813c868cb35SJohannes Berg 			return;
8143e122be0SJohannes Berg 
815d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
8163f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
8173f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
8183f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
8193f52b7e3SMarco Porsch #endif
820d012a605SMarco Porsch 	} else {
821d012a605SMarco Porsch 		return;
822d012a605SMarco Porsch 	}
823d012a605SMarco Porsch 
824c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
825d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
826c868cb35SJohannes Berg 		return;
827004c872eSJohannes Berg 
828c868cb35SJohannes Berg 	if (sta->dead)
829c868cb35SJohannes Berg 		goto done;
8303e122be0SJohannes Berg 
831948d887dSJohannes Berg 	/*
832948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
833948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
834948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
835948d887dSJohannes Berg 	 * non-enabled ones.
836948d887dSJohannes Berg 	 */
837948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
838948d887dSJohannes Berg 		ignore_for_tim = 0;
839948d887dSJohannes Berg 
8409b7a86f3SJohannes Berg 	if (ignore_pending)
8419b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
8429b7a86f3SJohannes Berg 
843948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
844948d887dSJohannes Berg 		unsigned long tids;
845948d887dSJohannes Berg 
846f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
847948d887dSJohannes Berg 			continue;
848948d887dSJohannes Berg 
849948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
850948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
851948d887dSJohannes Berg 		if (indicate_tim)
852948d887dSJohannes Berg 			break;
853948d887dSJohannes Berg 
854948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
855948d887dSJohannes Berg 
856948d887dSJohannes Berg 		indicate_tim |=
857948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
858ba8c3d6fSFelix Fietkau 		indicate_tim |=
859ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
860004c872eSJohannes Berg 	}
861004c872eSJohannes Berg 
862c868cb35SJohannes Berg  done:
86365f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
864004c872eSJohannes Berg 
8653d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
8663d5839b6SIlan Peer 		goto out_unlock;
8673d5839b6SIlan Peer 
868948d887dSJohannes Berg 	if (indicate_tim)
869d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
870c868cb35SJohannes Berg 	else
871d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
8723e122be0SJohannes Berg 
8739b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
874c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
875948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
876c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
877004c872eSJohannes Berg 	}
878004c872eSJohannes Berg 
8793d5839b6SIlan Peer out_unlock:
88065f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
881004c872eSJohannes Berg }
882004c872eSJohannes Berg 
8839b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
8849b7a86f3SJohannes Berg {
8859b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
8869b7a86f3SJohannes Berg }
8879b7a86f3SJohannes Berg 
888cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
889f0706e82SJiri Benc {
890e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
891f0706e82SJiri Benc 	int timeout;
892f0706e82SJiri Benc 
893f0706e82SJiri Benc 	if (!skb)
894cd0b8d89SJohannes Berg 		return false;
895f0706e82SJiri Benc 
896e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
897f0706e82SJiri Benc 
898f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
89957c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
90057c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
90157c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
902f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
903f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
904e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
905f0706e82SJiri Benc }
906f0706e82SJiri Benc 
907f0706e82SJiri Benc 
908948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
909948d887dSJohannes Berg 						struct sta_info *sta, int ac)
910f0706e82SJiri Benc {
911f0706e82SJiri Benc 	unsigned long flags;
912f0706e82SJiri Benc 	struct sk_buff *skb;
913f0706e82SJiri Benc 
91460750397SJohannes Berg 	/*
91560750397SJohannes Berg 	 * First check for frames that should expire on the filtered
91660750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
91760750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
91860750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
91960750397SJohannes Berg 	 * total_ps_buffered counter.
92060750397SJohannes Berg 	 */
921f0706e82SJiri Benc 	for (;;) {
922948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
923948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
92457c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
925948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
926836341a7SJohannes Berg 		else
927f0706e82SJiri Benc 			skb = NULL;
928948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
929f0706e82SJiri Benc 
93060750397SJohannes Berg 		/*
93160750397SJohannes Berg 		 * Frames are queued in order, so if this one
93260750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
93360750397SJohannes Berg 		 * we actually reached the end of the queue we
93460750397SJohannes Berg 		 * also need to stop, of course.
93560750397SJohannes Berg 		 */
93660750397SJohannes Berg 		if (!skb)
93760750397SJohannes Berg 			break;
938d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
93960750397SJohannes Berg 	}
94060750397SJohannes Berg 
94160750397SJohannes Berg 	/*
94260750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
94360750397SJohannes Berg 	 * only find something if the filtered queue was emptied
94460750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
94560750397SJohannes Berg 	 * buffered frames.
94660750397SJohannes Berg 	 */
947f0706e82SJiri Benc 	for (;;) {
948948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
949948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
950f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
951948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
952f0706e82SJiri Benc 		else
953f0706e82SJiri Benc 			skb = NULL;
954948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
955f0706e82SJiri Benc 
95660750397SJohannes Berg 		/*
95760750397SJohannes Berg 		 * frames are queued in order, so if this one
95860750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
95960750397SJohannes Berg 		 * the queue) we can stop testing
96060750397SJohannes Berg 		 */
961836341a7SJohannes Berg 		if (!skb)
962836341a7SJohannes Berg 			break;
963836341a7SJohannes Berg 
964f0706e82SJiri Benc 		local->total_ps_buffered--;
965bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
966bdcbd8e0SJohannes Berg 		       sta->sta.addr);
967d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
968f0706e82SJiri Benc 	}
9693393a608SJuuso Oikarinen 
97060750397SJohannes Berg 	/*
97160750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
97260750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
97360750397SJohannes Berg 	 * frames.
97460750397SJohannes Berg 	 */
97560750397SJohannes Berg 	sta_info_recalc_tim(sta);
97660750397SJohannes Berg 
97760750397SJohannes Berg 	/*
97860750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
97960750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
98060750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
98160750397SJohannes Berg 	 */
982948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
983948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
984948d887dSJohannes Berg }
985948d887dSJohannes Berg 
986948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
987948d887dSJohannes Berg 					     struct sta_info *sta)
988948d887dSJohannes Berg {
989948d887dSJohannes Berg 	bool have_buffered = false;
990948d887dSJohannes Berg 	int ac;
991948d887dSJohannes Berg 
9923f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
9933f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
9943f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
995948d887dSJohannes Berg 		return false;
996948d887dSJohannes Berg 
997948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
998948d887dSJohannes Berg 		have_buffered |=
999948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1000948d887dSJohannes Berg 
1001948d887dSJohannes Berg 	return have_buffered;
1002f0706e82SJiri Benc }
1003f0706e82SJiri Benc 
1004d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
100534e89507SJohannes Berg {
100634e89507SJohannes Berg 	struct ieee80211_local *local;
100734e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
10086d10e46bSJohannes Berg 	int ret;
100934e89507SJohannes Berg 
101034e89507SJohannes Berg 	might_sleep();
101134e89507SJohannes Berg 
101234e89507SJohannes Berg 	if (!sta)
101334e89507SJohannes Berg 		return -ENOENT;
101434e89507SJohannes Berg 
101534e89507SJohannes Berg 	local = sta->local;
101634e89507SJohannes Berg 	sdata = sta->sdata;
101734e89507SJohannes Berg 
101883d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
101983d5cc01SJohannes Berg 
1020098a6070SJohannes Berg 	/*
1021098a6070SJohannes Berg 	 * Before removing the station from the driver and
1022098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1023098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1024098a6070SJohannes Berg 	 * will be sufficient.
1025098a6070SJohannes Berg 	 */
1026c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1027c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1028098a6070SJohannes Berg 
1029f59374ebSSara Sharon 	/*
1030f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1031f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1032f59374ebSSara Sharon 	 * all such frames to be processed.
1033f59374ebSSara Sharon 	 */
1034f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1035f59374ebSSara Sharon 
103634e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1037b01711beSJohannes Berg 	if (WARN_ON(ret))
103834e89507SJohannes Berg 		return ret;
103934e89507SJohannes Berg 
1040a7a6bdd0SArik Nemtsov 	/*
1041a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1042a7a6bdd0SArik Nemtsov 	 * removal.
1043a7a6bdd0SArik Nemtsov 	 */
1044a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1045a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1046a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1047a7a6bdd0SArik Nemtsov 	}
1048a7a6bdd0SArik Nemtsov 
1049794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1050ef044763SEliad Peller 	sta->removed = true;
10514d33960bSJohannes Berg 
10526a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
10536a9d1b91SJohannes Berg 
1054a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1055a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1056a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1057a710c816SJohannes Berg 
1058d778207bSJohannes Berg 	return 0;
1059d778207bSJohannes Berg }
1060d778207bSJohannes Berg 
1061d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1062d778207bSJohannes Berg {
1063d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1064d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
10650ef049dcSArnd Bergmann 	struct station_info *sinfo;
1066d778207bSJohannes Berg 	int ret;
1067d778207bSJohannes Berg 
1068d778207bSJohannes Berg 	/*
1069d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1070d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1071d778207bSJohannes Berg 	 */
1072d778207bSJohannes Berg 
1073d778207bSJohannes Berg 	might_sleep();
1074d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1075d778207bSJohannes Berg 
10765981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1077b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1078b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1079b16798f5SJohannes Berg 	}
1080b16798f5SJohannes Berg 
1081c8782078SJohannes Berg 	/* now keys can no longer be reached */
10826d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
108334e89507SJohannes Berg 
10849b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
10859b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
10869b7a86f3SJohannes Berg 
108734e89507SJohannes Berg 	sta->dead = true;
108834e89507SJohannes Berg 
108934e89507SJohannes Berg 	local->num_sta--;
109034e89507SJohannes Berg 	local->sta_generation++;
109134e89507SJohannes Berg 
109283d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1093f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1094f09603a2SJohannes Berg 		if (ret) {
109583d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
109683d5cc01SJohannes Berg 			break;
109783d5cc01SJohannes Berg 		}
109883d5cc01SJohannes Berg 	}
1099d9a7ddb0SJohannes Berg 
1100f09603a2SJohannes Berg 	if (sta->uploaded) {
1101f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1102f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1103f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1104f09603a2SJohannes Berg 	}
110534e89507SJohannes Berg 
1106bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1107bdcbd8e0SJohannes Berg 
11080ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
11090ef049dcSArnd Bergmann 	if (sinfo)
11100fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
11110ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
11120ef049dcSArnd Bergmann 	kfree(sinfo);
1113ec15e68bSJouni Malinen 
111434e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
111534e89507SJohannes Berg 
11163a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
11173a11ce08SJohannes Berg 
1118d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1119d778207bSJohannes Berg }
1120d778207bSJohannes Berg 
1121d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1122d778207bSJohannes Berg {
1123d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1124d778207bSJohannes Berg 
1125d778207bSJohannes Berg 	if (err)
1126d778207bSJohannes Berg 		return err;
1127d778207bSJohannes Berg 
1128d778207bSJohannes Berg 	synchronize_net();
1129d778207bSJohannes Berg 
1130d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
113134e89507SJohannes Berg 
113234e89507SJohannes Berg 	return 0;
113334e89507SJohannes Berg }
113434e89507SJohannes Berg 
113534e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
113634e89507SJohannes Berg {
113734e89507SJohannes Berg 	struct sta_info *sta;
113834e89507SJohannes Berg 	int ret;
113934e89507SJohannes Berg 
114034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
11417852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
114234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
114334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
114434e89507SJohannes Berg 
114534e89507SJohannes Berg 	return ret;
114634e89507SJohannes Berg }
114734e89507SJohannes Berg 
114834e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
114934e89507SJohannes Berg 			      const u8 *addr)
115034e89507SJohannes Berg {
115134e89507SJohannes Berg 	struct sta_info *sta;
115234e89507SJohannes Berg 	int ret;
115334e89507SJohannes Berg 
115434e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
11557852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
115634e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
115734e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
115834e89507SJohannes Berg 
115934e89507SJohannes Berg 	return ret;
116034e89507SJohannes Berg }
1161f0706e82SJiri Benc 
116234f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1163f0706e82SJiri Benc {
116434f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1165f0706e82SJiri Benc 	struct sta_info *sta;
11663393a608SJuuso Oikarinen 	bool timer_needed = false;
1167f0706e82SJiri Benc 
1168d0709a65SJohannes Berg 	rcu_read_lock();
1169d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
11703393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
11713393a608SJuuso Oikarinen 			timer_needed = true;
1172d0709a65SJohannes Berg 	rcu_read_unlock();
1173f0706e82SJiri Benc 
11745bb644a0SJohannes Berg 	if (local->quiescing)
11755bb644a0SJohannes Berg 		return;
11765bb644a0SJohannes Berg 
11773393a608SJuuso Oikarinen 	if (!timer_needed)
11783393a608SJuuso Oikarinen 		return;
11793393a608SJuuso Oikarinen 
118026d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
118126d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1182f0706e82SJiri Benc }
1183f0706e82SJiri Benc 
11847bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
11857bedd0cfSJohannes Berg {
11867bedd0cfSJohannes Berg 	int err;
11877bedd0cfSJohannes Berg 
118883e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
11897bedd0cfSJohannes Berg 	if (err)
11907bedd0cfSJohannes Berg 		return err;
11917bedd0cfSJohannes Berg 
11924d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
119334e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1194f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1195f0706e82SJiri Benc 
119634f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
11977bedd0cfSJohannes Berg 	return 0;
1198f0706e82SJiri Benc }
1199f0706e82SJiri Benc 
1200f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1201f0706e82SJiri Benc {
1202a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
120383e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1204f0706e82SJiri Benc }
1205f0706e82SJiri Benc 
1206051007d9SJohannes Berg 
1207e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1208f0706e82SJiri Benc {
1209b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1210f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1211d778207bSJohannes Berg 	LIST_HEAD(free_list);
121244213b5eSJohannes Berg 	int ret = 0;
1213f0706e82SJiri Benc 
1214d0709a65SJohannes Berg 	might_sleep();
1215d0709a65SJohannes Berg 
1216e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1217e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1218e716251dSJohannes Berg 
121934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
122034e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1221e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1222e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1223d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1224d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
122534316837SJohannes Berg 			ret++;
122634316837SJohannes Berg 		}
122734e89507SJohannes Berg 	}
1228d778207bSJohannes Berg 
1229d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1230d778207bSJohannes Berg 		synchronize_net();
1231d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1232d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1233d778207bSJohannes Berg 	}
123434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
123544213b5eSJohannes Berg 
1236051007d9SJohannes Berg 	return ret;
1237051007d9SJohannes Berg }
1238051007d9SJohannes Berg 
123924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
124024723d1bSJohannes Berg 			  unsigned long exp_time)
124124723d1bSJohannes Berg {
124224723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
124324723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
124424723d1bSJohannes Berg 
124534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1246e46a2cf9SMohammed Shafi Shajakhan 
1247e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1248b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1249b8da6b6aSJohannes Berg 
1250ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1251ec2b774eSMarek Lindner 			continue;
1252ec2b774eSMarek Lindner 
1253b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1254eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1255bdcbd8e0SJohannes Berg 				sta->sta.addr);
12563f52b7e3SMarco Porsch 
12573f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
12583f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
12593f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
12603f52b7e3SMarco Porsch 
126134e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
126224723d1bSJohannes Berg 		}
1263e46a2cf9SMohammed Shafi Shajakhan 	}
1264e46a2cf9SMohammed Shafi Shajakhan 
126534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
126624723d1bSJohannes Berg }
126717741cdcSJohannes Berg 
1268686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1269686b9cb9SBen Greear 						   const u8 *addr,
1270686b9cb9SBen Greear 						   const u8 *localaddr)
127117741cdcSJohannes Berg {
12727bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
127383e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
12747bedd0cfSJohannes Berg 	struct sta_info *sta;
127517741cdcSJohannes Berg 
1276686b9cb9SBen Greear 	/*
1277686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1278686b9cb9SBen Greear 	 * ... first in list.
1279686b9cb9SBen Greear 	 */
128083e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1281686b9cb9SBen Greear 		if (localaddr &&
1282b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1283686b9cb9SBen Greear 			continue;
1284f7c65594SJohannes Berg 		if (!sta->uploaded)
1285f7c65594SJohannes Berg 			return NULL;
128617741cdcSJohannes Berg 		return &sta->sta;
1287f7c65594SJohannes Berg 	}
1288f7c65594SJohannes Berg 
1289abe60632SJohannes Berg 	return NULL;
129017741cdcSJohannes Berg }
1291686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
12925ed176e1SJohannes Berg 
12935ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
12945ed176e1SJohannes Berg 					 const u8 *addr)
12955ed176e1SJohannes Berg {
1296f7c65594SJohannes Berg 	struct sta_info *sta;
12975ed176e1SJohannes Berg 
12985ed176e1SJohannes Berg 	if (!vif)
12995ed176e1SJohannes Berg 		return NULL;
13005ed176e1SJohannes Berg 
1301f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1302f7c65594SJohannes Berg 	if (!sta)
1303f7c65594SJohannes Berg 		return NULL;
13045ed176e1SJohannes Berg 
1305f7c65594SJohannes Berg 	if (!sta->uploaded)
1306f7c65594SJohannes Berg 		return NULL;
1307f7c65594SJohannes Berg 
1308f7c65594SJohannes Berg 	return &sta->sta;
13095ed176e1SJohannes Berg }
131017741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1311af818581SJohannes Berg 
1312e3685e03SJohannes Berg /* powersave support code */
1313e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
131450a9432dSJohannes Berg {
1315608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1316e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1317e3685e03SJohannes Berg 	struct sk_buff_head pending;
1318ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1319e3685e03SJohannes Berg 	unsigned long flags;
1320d012a605SMarco Porsch 	struct ps_data *ps;
1321d012a605SMarco Porsch 
13223918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
13233918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
13243918edb0SFelix Fietkau 				     u.ap);
13253918edb0SFelix Fietkau 
13263918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1327d012a605SMarco Porsch 		ps = &sdata->bss->ps;
13283f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
13293f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1330d012a605SMarco Porsch 	else
1331d012a605SMarco Porsch 		return;
133250a9432dSJohannes Berg 
1333c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
133447086fc5SJohannes Berg 
13355a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1336948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1337ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1338948d887dSJohannes Berg 
133930686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
134012375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1341af818581SJohannes Berg 
1342ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1343adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1344ba8c3d6fSFelix Fietkau 			continue;
1345ba8c3d6fSFelix Fietkau 
134618667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1347ba8c3d6fSFelix Fietkau 	}
1348ba8c3d6fSFelix Fietkau 
1349948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1350af818581SJohannes Berg 
13511d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
13521d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1353af818581SJohannes Berg 	/* Send all buffered frames to the station */
1354948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1355948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1356948d887dSJohannes Berg 
1357987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1358948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1359987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1360948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1361948d887dSJohannes Berg 		filtered += tmp - count;
1362948d887dSJohannes Berg 		count = tmp;
1363948d887dSJohannes Berg 
1364987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1365948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1366987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1367948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1368948d887dSJohannes Berg 		buffered += tmp - count;
1369948d887dSJohannes Berg 	}
1370948d887dSJohannes Berg 
1371e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
13725ac2e350SJohannes Berg 
13735ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
13745ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
13755ac2e350SJohannes Berg 
13765ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
13775ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
13785ac2e350SJohannes Berg 	 */
13795ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
13805ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
13811d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1382948d887dSJohannes Berg 
1383e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1384e3685e03SJohannes Berg 
1385af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1386af818581SJohannes Berg 
1387c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1388c868cb35SJohannes Berg 
1389bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
13902595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1391bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
139217c18bf8SJohannes Berg 
139317c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1394af818581SJohannes Berg }
1395af818581SJohannes Berg 
13960ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1397b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
13980ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1399ce662b44SJohannes Berg {
14000ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1401ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1402ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1403ce662b44SJohannes Berg 	struct sk_buff *skb;
1404ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1405ce662b44SJohannes Berg 	__le16 fc;
1406a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1407ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
140855de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1409ce662b44SJohannes Berg 
1410ce662b44SJohannes Berg 	if (qos) {
1411ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1412ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1413ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1414ce662b44SJohannes Berg 	} else {
1415ce662b44SJohannes Berg 		size -= 2;
1416ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1417ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1418ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1419ce662b44SJohannes Berg 	}
1420ce662b44SJohannes Berg 
1421ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1422ce662b44SJohannes Berg 	if (!skb)
1423ce662b44SJohannes Berg 		return;
1424ce662b44SJohannes Berg 
1425ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1426ce662b44SJohannes Berg 
14274df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1428ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1429ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1430ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1431ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1432ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1433864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1434ce662b44SJohannes Berg 
1435ce662b44SJohannes Berg 	skb->priority = tid;
1436ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
143759b66255SJohannes Berg 	if (qos) {
1438ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1439ce662b44SJohannes Berg 
14400ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1441ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1442ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
14430ead2510SEmmanuel Grumbach 			if (more_data)
14440ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
14450ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
14460ead2510SEmmanuel Grumbach 		}
1447ce662b44SJohannes Berg 	}
1448ce662b44SJohannes Berg 
1449ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1450ce662b44SJohannes Berg 
1451ce662b44SJohannes Berg 	/*
1452ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1453ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1454deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1455deeaee19SJohannes Berg 	 * ends the poll/service period.
1456ce662b44SJohannes Berg 	 */
145702f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1458deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1459ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1460ce662b44SJohannes Berg 
14616b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
14626b127c71SSujith Manoharan 
1463b77cf4f8SJohannes Berg 	if (call_driver)
1464b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1465b77cf4f8SJohannes Berg 					  reason, false);
146640b96408SJohannes Berg 
146789afe614SJohannes Berg 	skb->dev = sdata->dev;
146889afe614SJohannes Berg 
146955de908aSJohannes Berg 	rcu_read_lock();
1470*d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
147155de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
147255de908aSJohannes Berg 		rcu_read_unlock();
147355de908aSJohannes Berg 		kfree_skb(skb);
147455de908aSJohannes Berg 		return;
147555de908aSJohannes Berg 	}
147655de908aSJohannes Berg 
147773c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
147808aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
147955de908aSJohannes Berg 	rcu_read_unlock();
1480ce662b44SJohannes Berg }
1481ce662b44SJohannes Berg 
14820a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
14830a1cb809SJohannes Berg {
14840a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
14850a1cb809SJohannes Berg 	if (tids & 0xF8)
14860a1cb809SJohannes Berg 		return fls(tids) - 1;
14870a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
14880a1cb809SJohannes Berg 	if (tids & BIT(0))
14890a1cb809SJohannes Berg 		return 0;
14900a1cb809SJohannes Berg 	return fls(tids) - 1;
14910a1cb809SJohannes Berg }
14920a1cb809SJohannes Berg 
14930ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
14940ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
14950ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
14960ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
14970ead2510SEmmanuel Grumbach  */
14980ead2510SEmmanuel Grumbach static bool
14990ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
15000ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
15010ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
15020ead2510SEmmanuel Grumbach {
15030ead2510SEmmanuel Grumbach 	int ac;
15040ead2510SEmmanuel Grumbach 
15050ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
15060ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
15070ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
15080ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
15090ead2510SEmmanuel Grumbach 	 */
15100ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
15110ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
15120ead2510SEmmanuel Grumbach 		return true;
15130ead2510SEmmanuel Grumbach 
15140ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1515f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
15160ead2510SEmmanuel Grumbach 			continue;
15170ead2510SEmmanuel Grumbach 
15180ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15190ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
15200ead2510SEmmanuel Grumbach 			return true;
15210ead2510SEmmanuel Grumbach 	}
15220ead2510SEmmanuel Grumbach 
15230ead2510SEmmanuel Grumbach 	return false;
15240ead2510SEmmanuel Grumbach }
15250ead2510SEmmanuel Grumbach 
152647086fc5SJohannes Berg static void
15270ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
15280ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
15290ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
15300ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1531af818581SJohannes Berg {
1532af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1533af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1534948d887dSJohannes Berg 	int ac;
1535af818581SJohannes Berg 
1536f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1537948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
15384049e09aSJohannes Berg 		unsigned long tids;
15394049e09aSJohannes Berg 
1540f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1541948d887dSJohannes Berg 			continue;
1542948d887dSJohannes Berg 
15434049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
15444049e09aSJohannes Berg 
1545f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1546f9f760b4SJohannes Berg 		 * release from hardware queues
1547f9f760b4SJohannes Berg 		 */
15480ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
15490ead2510SEmmanuel Grumbach 			*driver_release_tids |=
15500ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
15510ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1552ba8c3d6fSFelix Fietkau 		}
1553f9f760b4SJohannes Berg 
15540ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
155547086fc5SJohannes Berg 			struct sk_buff *skb;
155647086fc5SJohannes Berg 
155747086fc5SJohannes Berg 			while (n_frames > 0) {
1558948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1559948d887dSJohannes Berg 				if (!skb) {
156047086fc5SJohannes Berg 					skb = skb_dequeue(
156147086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1562af818581SJohannes Berg 					if (skb)
1563af818581SJohannes Berg 						local->total_ps_buffered--;
1564af818581SJohannes Berg 				}
156547086fc5SJohannes Berg 				if (!skb)
156647086fc5SJohannes Berg 					break;
156747086fc5SJohannes Berg 				n_frames--;
15680ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
156947086fc5SJohannes Berg 			}
1570948d887dSJohannes Berg 		}
1571948d887dSJohannes Berg 
15720ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
15730ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
15740ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
15754049e09aSJohannes Berg 		 */
1576948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15770ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1578948d887dSJohannes Berg 			break;
1579948d887dSJohannes Berg 	}
1580948d887dSJohannes Berg }
1581af818581SJohannes Berg 
15820ead2510SEmmanuel Grumbach static void
15830ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
15840ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
15850ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
15860ead2510SEmmanuel Grumbach {
15870ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
15880ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
15890ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
15900ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
15910ead2510SEmmanuel Grumbach 	bool more_data;
15920ead2510SEmmanuel Grumbach 
15930ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
15940ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
15950ead2510SEmmanuel Grumbach 
15960ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
15970ead2510SEmmanuel Grumbach 
15980ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
15990ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
16000ead2510SEmmanuel Grumbach 
16010ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
16020ead2510SEmmanuel Grumbach 
16031a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
16040ead2510SEmmanuel Grumbach 		driver_release_tids =
16050ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
16060ead2510SEmmanuel Grumbach 
1607f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1608f438ceb8SEmmanuel Grumbach 		int tid, ac;
16094049e09aSJohannes Berg 
1610ce662b44SJohannes Berg 		/*
1611ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1612ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1613ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1614ce662b44SJohannes Berg 		 *
1615ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1616ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1617ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1618ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1619ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1620ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1621ce662b44SJohannes Berg 		 *
1622ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1623ce662b44SJohannes Berg 		 */
1624ce662b44SJohannes Berg 
1625ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1626f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1627d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1628d7f84244SEmmanuel Grumbach 				break;
1629f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1630ce662b44SJohannes Berg 
16310ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1632f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
163347086fc5SJohannes Berg 		struct sk_buff_head pending;
163447086fc5SJohannes Berg 		struct sk_buff *skb;
163540b96408SJohannes Berg 		int num = 0;
163640b96408SJohannes Berg 		u16 tids = 0;
1637b77cf4f8SJohannes Berg 		bool need_null = false;
163847086fc5SJohannes Berg 
163947086fc5SJohannes Berg 		skb_queue_head_init(&pending);
164047086fc5SJohannes Berg 
164147086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1642af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
164347086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
164440b96408SJohannes Berg 			u8 *qoshdr = NULL;
164540b96408SJohannes Berg 
164640b96408SJohannes Berg 			num++;
1647af818581SJohannes Berg 
1648af818581SJohannes Berg 			/*
164947086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
165047086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
165147086fc5SJohannes Berg 			 * exchange.
1652af818581SJohannes Berg 			 */
16536b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
16546b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1655af818581SJohannes Berg 
165647086fc5SJohannes Berg 			/*
165747086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
165847086fc5SJohannes Berg 			 * more buffered frames for this STA
165947086fc5SJohannes Berg 			 */
166024b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
166147086fc5SJohannes Berg 				hdr->frame_control |=
166247086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
166324b9c373SJanusz.Dziedzic@tieto.com 			else
166424b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
166524b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1666af818581SJohannes Berg 
166740b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
166840b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
166940b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
167040b96408SJohannes Berg 
1671b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1672b77cf4f8SJohannes Berg 
1673b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1674b77cf4f8SJohannes Berg 
1675b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1676b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1677b77cf4f8SJohannes Berg 				continue;
1678b77cf4f8SJohannes Berg 
1679b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1680b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1681b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1682b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1683b77cf4f8SJohannes Berg 				break;
1684b77cf4f8SJohannes Berg 			}
1685b77cf4f8SJohannes Berg 
1686b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1687b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1688b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1689b77cf4f8SJohannes Berg 			 * and be done.
1690b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1691b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1692b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1693b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1694b77cf4f8SJohannes Berg 			 *
1695b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1696b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1697b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1698b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1699b77cf4f8SJohannes Berg 			 *
1700b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1701b77cf4f8SJohannes Berg 			 */
1702b77cf4f8SJohannes Berg 			if (qoshdr) {
170340b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1704deeaee19SJohannes Berg 
170547086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
170647086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1707b77cf4f8SJohannes Berg 			} else {
1708b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1709b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1710b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1711b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1712b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1713b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1714b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1715b77cf4f8SJohannes Berg 				 */
1716b77cf4f8SJohannes Berg 				hdr->frame_control |=
1717b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1718b77cf4f8SJohannes Berg 				need_null = true;
1719b77cf4f8SJohannes Berg 				num++;
172052a3f20cSMarco Porsch 			}
1721b77cf4f8SJohannes Berg 			break;
172247086fc5SJohannes Berg 		}
172347086fc5SJohannes Berg 
172440b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
172540b96408SJohannes Berg 					  reason, more_data);
172640b96408SJohannes Berg 
172747086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1728af818581SJohannes Berg 
1729b77cf4f8SJohannes Berg 		if (need_null)
1730b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
17310ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
17320ead2510SEmmanuel Grumbach 				reason, false, false);
1733b77cf4f8SJohannes Berg 
1734c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1735af818581SJohannes Berg 	} else {
1736ba8c3d6fSFelix Fietkau 		int tid;
1737ba8c3d6fSFelix Fietkau 
1738af818581SJohannes Berg 		/*
17394049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
17404049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1741f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1742f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1743f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1744f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1745f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1746f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1747af818581SJohannes Berg 		 */
17484049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
174947086fc5SJohannes Berg 					    n_frames, reason, more_data);
17504049e09aSJohannes Berg 
17514049e09aSJohannes Berg 		/*
17524049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
17534049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1754f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
17554049e09aSJohannes Berg 		 * release function.
1756f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1757ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1758ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
17594049e09aSJohannes Berg 		 */
1760ba8c3d6fSFelix Fietkau 
1761ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1762ba8c3d6fSFelix Fietkau 			return;
1763ba8c3d6fSFelix Fietkau 
1764ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1765adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1766adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
17671e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1768ba8c3d6fSFelix Fietkau 				continue;
1769ba8c3d6fSFelix Fietkau 
1770ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1771ba8c3d6fSFelix Fietkau 			break;
1772ba8c3d6fSFelix Fietkau 		}
1773af818581SJohannes Berg 	}
1774af818581SJohannes Berg }
1775af818581SJohannes Berg 
1776af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1777af818581SJohannes Berg {
177847086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1779af818581SJohannes Berg 
1780af818581SJohannes Berg 	/*
178147086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
178247086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
178347086fc5SJohannes Berg 	 * only from the non-enabled ones.
1784af818581SJohannes Berg 	 */
178547086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
178647086fc5SJohannes Berg 		ignore_for_response = 0;
1787af818581SJohannes Berg 
178847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
178947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1790af818581SJohannes Berg }
179147086fc5SJohannes Berg 
179247086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
179347086fc5SJohannes Berg {
179447086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
179547086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
179647086fc5SJohannes Berg 
179747086fc5SJohannes Berg 	/*
179847086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
179947086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
180047086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
180147086fc5SJohannes Berg 	 * actually getting called.
180247086fc5SJohannes Berg 	 */
180347086fc5SJohannes Berg 	if (!delivery_enabled)
180447086fc5SJohannes Berg 		return;
180547086fc5SJohannes Berg 
180647086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
180747086fc5SJohannes Berg 	case 1:
180847086fc5SJohannes Berg 		n_frames = 2;
180947086fc5SJohannes Berg 		break;
181047086fc5SJohannes Berg 	case 2:
181147086fc5SJohannes Berg 		n_frames = 4;
181247086fc5SJohannes Berg 		break;
181347086fc5SJohannes Berg 	case 3:
181447086fc5SJohannes Berg 		n_frames = 6;
181547086fc5SJohannes Berg 		break;
181647086fc5SJohannes Berg 	case 0:
181747086fc5SJohannes Berg 		/* XXX: what is a good value? */
181813a8098aSAndrei Otcheretianski 		n_frames = 128;
181947086fc5SJohannes Berg 		break;
182047086fc5SJohannes Berg 	}
182147086fc5SJohannes Berg 
182247086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
182347086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1824af818581SJohannes Berg }
1825af818581SJohannes Berg 
1826af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1827af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1828af818581SJohannes Berg {
1829af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1830af818581SJohannes Berg 
1831b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1832b5878a2dSJohannes Berg 
18335ac2e350SJohannes Berg 	if (block) {
1834c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
183517c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
18365ac2e350SJohannes Berg 		return;
18375ac2e350SJohannes Berg 	}
18385ac2e350SJohannes Berg 
18395ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
18405ac2e350SJohannes Berg 		return;
18415ac2e350SJohannes Berg 
18425ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
18435ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
18445ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18455ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18465ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
18475ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
18485ac2e350SJohannes Berg 		/* must be asleep in this case */
18495ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18505ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18515ac2e350SJohannes Berg 	} else {
18525ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
185317c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
18545ac2e350SJohannes Berg 	}
1855af818581SJohannes Berg }
1856af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1857dcf55fb5SFelix Fietkau 
1858e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
185937fbd908SJohannes Berg {
186037fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
186137fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
186237fbd908SJohannes Berg 
186337fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
186437fbd908SJohannes Berg 
186537fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
186637fbd908SJohannes Berg }
1867e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
186837fbd908SJohannes Berg 
18690ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
18700ead2510SEmmanuel Grumbach {
18710ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
18720ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
18730ead2510SEmmanuel Grumbach 	bool more_data;
18740ead2510SEmmanuel Grumbach 
18750ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
18760ead2510SEmmanuel Grumbach 
18770ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
18780ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
18790ead2510SEmmanuel Grumbach 					       reason, 0);
18800ead2510SEmmanuel Grumbach 
18810ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
18820ead2510SEmmanuel Grumbach }
18830ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
18840ead2510SEmmanuel Grumbach 
1885042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1886042ec453SJohannes Berg 				u8 tid, bool buffered)
1887dcf55fb5SFelix Fietkau {
1888dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1889dcf55fb5SFelix Fietkau 
18905a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1891042ec453SJohannes Berg 		return;
1892042ec453SJohannes Berg 
18931b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
18941b000789SJohannes Berg 
1895948d887dSJohannes Berg 	if (buffered)
1896948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1897948d887dSJohannes Berg 	else
1898948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1899948d887dSJohannes Berg 
1900c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1901dcf55fb5SFelix Fietkau }
1902042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1903d9a7ddb0SJohannes Berg 
19042433647bSToke Høiland-Jørgensen void ieee80211_register_airtime(struct ieee80211_txq *txq,
19052433647bSToke Høiland-Jørgensen 				u32 tx_airtime, u32 rx_airtime)
19062433647bSToke Høiland-Jørgensen {
19072433647bSToke Høiland-Jørgensen 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
19082433647bSToke Høiland-Jørgensen 	struct ieee80211_local *local = sdata->local;
19092433647bSToke Høiland-Jørgensen 	u64 weight_sum, weight_sum_reciprocal;
19102433647bSToke Høiland-Jørgensen 	struct airtime_sched_info *air_sched;
19112433647bSToke Høiland-Jørgensen 	struct airtime_info *air_info;
19122433647bSToke Høiland-Jørgensen 	u32 airtime = 0;
19132433647bSToke Høiland-Jørgensen 
19142433647bSToke Høiland-Jørgensen 	air_sched = &local->airtime[txq->ac];
19152433647bSToke Høiland-Jørgensen 	air_info = to_airtime_info(txq);
19162433647bSToke Høiland-Jørgensen 
19172433647bSToke Høiland-Jørgensen 	if (local->airtime_flags & AIRTIME_USE_TX)
19182433647bSToke Høiland-Jørgensen 		airtime += tx_airtime;
19192433647bSToke Høiland-Jørgensen 	if (local->airtime_flags & AIRTIME_USE_RX)
19202433647bSToke Høiland-Jørgensen 		airtime += rx_airtime;
19212433647bSToke Høiland-Jørgensen 
19222433647bSToke Høiland-Jørgensen 	/* Weights scale so the unit weight is 256 */
19232433647bSToke Høiland-Jørgensen 	airtime <<= 8;
19242433647bSToke Høiland-Jørgensen 
19252433647bSToke Høiland-Jørgensen 	spin_lock_bh(&air_sched->lock);
19262433647bSToke Høiland-Jørgensen 
19272433647bSToke Høiland-Jørgensen 	air_info->tx_airtime += tx_airtime;
19282433647bSToke Høiland-Jørgensen 	air_info->rx_airtime += rx_airtime;
19292433647bSToke Høiland-Jørgensen 
19302433647bSToke Høiland-Jørgensen 	if (air_sched->weight_sum) {
19312433647bSToke Høiland-Jørgensen 		weight_sum = air_sched->weight_sum;
19322433647bSToke Høiland-Jørgensen 		weight_sum_reciprocal = air_sched->weight_sum_reciprocal;
19332433647bSToke Høiland-Jørgensen 	} else {
19342433647bSToke Høiland-Jørgensen 		weight_sum = air_info->weight;
19352433647bSToke Høiland-Jørgensen 		weight_sum_reciprocal = air_info->weight_reciprocal;
19362433647bSToke Høiland-Jørgensen 	}
19372433647bSToke Høiland-Jørgensen 
19382433647bSToke Høiland-Jørgensen 	/* Round the calculation of global vt */
19392433647bSToke Høiland-Jørgensen 	air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) *
19402433647bSToke Høiland-Jørgensen 				weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64;
19412433647bSToke Høiland-Jørgensen 	air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) *
19422433647bSToke Høiland-Jørgensen 			       air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32;
19432433647bSToke Høiland-Jørgensen 	ieee80211_resort_txq(&local->hw, txq);
19442433647bSToke Høiland-Jørgensen 
19452433647bSToke Høiland-Jørgensen 	spin_unlock_bh(&air_sched->lock);
19462433647bSToke Høiland-Jørgensen }
19472433647bSToke Høiland-Jørgensen 
1948b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
1949b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
1950b4809e94SToke Høiland-Jørgensen {
19512433647bSToke Høiland-Jørgensen 	struct ieee80211_txq *txq = pubsta->txq[tid];
1952b4809e94SToke Høiland-Jørgensen 
19532433647bSToke Høiland-Jørgensen 	if (!txq)
19542433647bSToke Høiland-Jørgensen 		return;
1955b4809e94SToke Høiland-Jørgensen 
19562433647bSToke Høiland-Jørgensen 	ieee80211_register_airtime(txq, tx_airtime, rx_airtime);
1957b4809e94SToke Høiland-Jørgensen }
1958b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
1959b4809e94SToke Høiland-Jørgensen 
19603ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
19613ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
19623ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
19633ace10f5SKan Yan {
19643ace10f5SKan Yan 	int tx_pending;
19653ace10f5SKan Yan 
1966911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
1967911bde0fSToke Høiland-Jørgensen 		return;
1968911bde0fSToke Høiland-Jørgensen 
19693ace10f5SKan Yan 	if (!tx_completed) {
19703ace10f5SKan Yan 		if (sta)
19713ace10f5SKan Yan 			atomic_add(tx_airtime,
19723ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
19733ace10f5SKan Yan 
19743ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
19753ace10f5SKan Yan 		return;
19763ace10f5SKan Yan 	}
19773ace10f5SKan Yan 
19783ace10f5SKan Yan 	if (sta) {
19793ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
19803ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
198104e35caaSFelix Fietkau 		if (tx_pending < 0)
19823ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
19833ace10f5SKan Yan 				       tx_pending, 0);
19843ace10f5SKan Yan 	}
19853ace10f5SKan Yan 
19863ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
19873ace10f5SKan Yan 				       &local->aql_total_pending_airtime);
19883ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
19893ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
19903ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
19913ace10f5SKan Yan 		      tx_airtime))
19923ace10f5SKan Yan 		atomic_cmpxchg(&local->aql_total_pending_airtime,
19933ace10f5SKan Yan 			       tx_pending, 0);
19943ace10f5SKan Yan }
19953ace10f5SKan Yan 
199683d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1997d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1998d9a7ddb0SJohannes Berg {
19998bf11d8dSJohannes Berg 	might_sleep();
2000d9a7ddb0SJohannes Berg 
2001d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2002d9a7ddb0SJohannes Berg 		return 0;
2003d9a7ddb0SJohannes Berg 
2004f09603a2SJohannes Berg 	/* check allowed transitions first */
2005f09603a2SJohannes Berg 
2006d9a7ddb0SJohannes Berg 	switch (new_state) {
2007d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2008f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2009d9a7ddb0SJohannes Berg 			return -EINVAL;
2010d9a7ddb0SJohannes Berg 		break;
2011d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2012f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2013f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2014d9a7ddb0SJohannes Berg 			return -EINVAL;
2015d9a7ddb0SJohannes Berg 		break;
2016d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2017f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2018f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2019d9a7ddb0SJohannes Berg 			return -EINVAL;
2020d9a7ddb0SJohannes Berg 		break;
2021d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2022f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2023d9a7ddb0SJohannes Berg 			return -EINVAL;
2024d9a7ddb0SJohannes Berg 		break;
2025d9a7ddb0SJohannes Berg 	default:
2026d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2027d9a7ddb0SJohannes Berg 		return -EINVAL;
2028d9a7ddb0SJohannes Berg 	}
2029d9a7ddb0SJohannes Berg 
2030bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2031bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2032f09603a2SJohannes Berg 
2033f09603a2SJohannes Berg 	/*
2034f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2035f09603a2SJohannes Berg 	 * fail the transition
2036f09603a2SJohannes Berg 	 */
2037f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2038f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2039f09603a2SJohannes Berg 					sta->sta_state, new_state);
2040f09603a2SJohannes Berg 		if (err)
2041f09603a2SJohannes Berg 			return err;
2042f09603a2SJohannes Berg 	}
2043f09603a2SJohannes Berg 
2044f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2045f09603a2SJohannes Berg 
2046f09603a2SJohannes Berg 	switch (new_state) {
2047f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2048f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2049f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2050f09603a2SJohannes Berg 		break;
2051f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2052a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2053f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2054a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2055f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2056a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
205752cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
205852cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2059a7201a6cSIlan Peer 		}
2060f09603a2SJohannes Berg 		break;
2061f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2062f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2063f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
20649cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
2065a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
206652cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
206752cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2068f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
206972f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2070f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
207117c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
207249ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2073f09603a2SJohannes Berg 		}
2074f09603a2SJohannes Berg 		break;
2075f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2076f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
207772f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2078f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
207917c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
208049ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2081f09603a2SJohannes Berg 		}
20823e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
20833e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
20843e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
20853e493173SJouni Malinen 						    sta->sta.addr);
2086f09603a2SJohannes Berg 		break;
2087f09603a2SJohannes Berg 	default:
2088f09603a2SJohannes Berg 		break;
2089f09603a2SJohannes Berg 	}
2090f09603a2SJohannes Berg 
2091d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2092d9a7ddb0SJohannes Berg 
2093d9a7ddb0SJohannes Berg 	return 0;
2094d9a7ddb0SJohannes Berg }
2095687da132SEmmanuel Grumbach 
2096687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
2097687da132SEmmanuel Grumbach {
2098046d2e7cSSriram R 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.deflink.ht_cap;
2099687da132SEmmanuel Grumbach 	u8 rx_streams;
2100687da132SEmmanuel Grumbach 
2101046d2e7cSSriram R 	if (!sta->sta.deflink.ht_cap.ht_supported)
2102687da132SEmmanuel Grumbach 		return 1;
2103687da132SEmmanuel Grumbach 
2104046d2e7cSSriram R 	if (sta->sta.deflink.vht_cap.vht_supported) {
2105687da132SEmmanuel Grumbach 		int i;
2106687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
2107046d2e7cSSriram R 			le16_to_cpu(sta->sta.deflink.vht_cap.vht_mcs.tx_mcs_map);
2108687da132SEmmanuel Grumbach 
2109687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
2110687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
2111687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
2112687da132SEmmanuel Grumbach 				return i + 1;
2113687da132SEmmanuel Grumbach 	}
2114687da132SEmmanuel Grumbach 
2115687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
2116687da132SEmmanuel Grumbach 		rx_streams = 4;
2117687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
2118687da132SEmmanuel Grumbach 		rx_streams = 3;
2119687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
2120687da132SEmmanuel Grumbach 		rx_streams = 2;
2121687da132SEmmanuel Grumbach 	else
2122687da132SEmmanuel Grumbach 		rx_streams = 1;
2123687da132SEmmanuel Grumbach 
2124687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
2125687da132SEmmanuel Grumbach 		return rx_streams;
2126687da132SEmmanuel Grumbach 
2127687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
2128687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
2129687da132SEmmanuel Grumbach }
2130b7ffbd7eSJohannes Berg 
2131c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2132c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2133c9c5962bSJohannes Berg {
2134046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2135c9c5962bSJohannes Berg 	int cpu;
2136c9c5962bSJohannes Berg 
2137046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2138c9c5962bSJohannes Berg 		return stats;
2139c9c5962bSJohannes Berg 
2140c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2141c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2142c9c5962bSJohannes Berg 
2143046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2144c9c5962bSJohannes Berg 
2145c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2146c9c5962bSJohannes Berg 			stats = cpustats;
2147c9c5962bSJohannes Berg 	}
2148c9c5962bSJohannes Berg 
2149c9c5962bSJohannes Berg 	return stats;
2150c9c5962bSJohannes Berg }
2151c9c5962bSJohannes Berg 
215241cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
21534f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2154fbd6ff5cSJohannes Berg {
2155dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2156fbd6ff5cSJohannes Berg 
2157dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
21587f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
21594f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2160dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2161dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2162dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2163dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
21647f406cd1SJohannes Berg 		break;
21657f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
21664f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2167dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2168dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2169dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
21707f406cd1SJohannes Berg 		break;
21717f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2172fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2173fbd6ff5cSJohannes Berg 		u16 brate;
21744f6b1b3dSJohannes Berg 		unsigned int shift;
2175dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2176dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2177fbd6ff5cSJohannes Berg 
2178dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
21798b783d10SThomas Pedersen 
21808b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
21818b783d10SThomas Pedersen 			break;
21828b783d10SThomas Pedersen 
2183dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
21844f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
21854f6b1b3dSJohannes Berg 			shift = 2;
21864f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
21874f6b1b3dSJohannes Berg 			shift = 1;
21884f6b1b3dSJohannes Berg 		else
21894f6b1b3dSJohannes Berg 			shift = 0;
2190fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
21917f406cd1SJohannes Berg 		break;
21927f406cd1SJohannes Berg 		}
219341cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
219441cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
219541cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
219641cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
219741cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
219841cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
219941cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
220041cbb0f5SLuca Coelho 		break;
2201fbd6ff5cSJohannes Berg 	}
22024f6b1b3dSJohannes Berg }
2203fbd6ff5cSJohannes Berg 
2204a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
22054f6b1b3dSJohannes Berg {
22066aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
22074f6b1b3dSJohannes Berg 
22084f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2209a17d93ffSBen Greear 		return -EINVAL;
2210a17d93ffSBen Greear 
22114f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2212a17d93ffSBen Greear 	return 0;
2213fbd6ff5cSJohannes Berg }
2214fbd6ff5cSJohannes Berg 
2215b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2216b255b72bSSeevalamuthu Mariappan 					int tid)
2217b255b72bSSeevalamuthu Mariappan {
2218b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2219b255b72bSSeevalamuthu Mariappan 	u64 value;
2220b255b72bSSeevalamuthu Mariappan 
2221b255b72bSSeevalamuthu Mariappan 	do {
2222b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2223b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2224b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2225b255b72bSSeevalamuthu Mariappan 
2226b255b72bSSeevalamuthu Mariappan 	return value;
2227b255b72bSSeevalamuthu Mariappan }
2228b255b72bSSeevalamuthu Mariappan 
22290f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
22300f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
22310f9c5a61SJohannes Berg 			     int tid)
22320f9c5a61SJohannes Berg {
22330f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2234b255b72bSSeevalamuthu Mariappan 	int cpu;
22350f9c5a61SJohannes Berg 
22360f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2237046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2238046d2e7cSSriram R 							   tid);
22390f9c5a61SJohannes Berg 
2240046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2241b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2242b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2243b255b72bSSeevalamuthu Mariappan 
2244046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2245046d2e7cSSriram R 						     cpu);
2246b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2247b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2248b255b72bSSeevalamuthu Mariappan 			}
2249b255b72bSSeevalamuthu Mariappan 		}
22500f9c5a61SJohannes Berg 
22510f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
22520f9c5a61SJohannes Berg 	}
22530f9c5a61SJohannes Berg 
22540f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
22550f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2256046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
22570f9c5a61SJohannes Berg 	}
22580f9c5a61SJohannes Berg 
22590f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
22600f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22610f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2262046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
22630f9c5a61SJohannes Berg 	}
22640f9c5a61SJohannes Berg 
22650f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
22660f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22670f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2268046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
22690f9c5a61SJohannes Berg 	}
22702fe4a29aSToke Høiland-Jørgensen 
22712fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
22722fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
22732fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
22742fe4a29aSToke Høiland-Jørgensen 
22752fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
22762fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
22772fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
22782fe4a29aSToke Høiland-Jørgensen 
22792fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
22802fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
22812fe4a29aSToke Høiland-Jørgensen 	}
22820f9c5a61SJohannes Berg }
22830f9c5a61SJohannes Berg 
2284c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2285c9c5962bSJohannes Berg {
2286c9c5962bSJohannes Berg 	unsigned int start;
2287c9c5962bSJohannes Berg 	u64 value;
2288c9c5962bSJohannes Berg 
2289c9c5962bSJohannes Berg 	do {
2290c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2291c9c5962bSJohannes Berg 		value = rxstats->bytes;
2292c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2293c9c5962bSJohannes Berg 
2294c9c5962bSJohannes Berg 	return value;
2295c9c5962bSJohannes Berg }
2296c9c5962bSJohannes Berg 
22970fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
22980fdf1493SJohannes Berg 		   bool tidstats)
2299b7ffbd7eSJohannes Berg {
2300b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2301b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2302b7ffbd7eSJohannes Berg 	u32 thr = 0;
2303c9c5962bSJohannes Berg 	int i, ac, cpu;
2304c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2305c9c5962bSJohannes Berg 
2306c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2307b7ffbd7eSJohannes Berg 
2308b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2309b7ffbd7eSJohannes Berg 
2310225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2311225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2312225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2313225b8189SJohannes Berg 	 */
2314225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2315225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2316225b8189SJohannes Berg 
23172b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2318a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2319a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2320a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2321a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
23229cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2323a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2324976bd9efSJohannes Berg 
2325976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2326976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2327a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2328976bd9efSJohannes Berg 	}
2329b7ffbd7eSJohannes Berg 
233084b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
23319cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2332e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2333b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
23342b9a7e1bSJohannes Berg 
2335a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2336a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2337b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
23382b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2339046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2340a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2341b7ffbd7eSJohannes Berg 	}
23422b9a7e1bSJohannes Berg 
2343a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
23442b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
23452b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2346046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2347a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
23482b9a7e1bSJohannes Berg 	}
23492b9a7e1bSJohannes Berg 
2350a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2351a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2352046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
23530f9c5a61SJohannes Berg 
2354046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2355c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2356c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2357c9c5962bSJohannes Berg 
2358046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2359046d2e7cSSriram R 						     cpu);
2360c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2361c9c5962bSJohannes Berg 			}
2362c9c5962bSJohannes Berg 		}
2363c9c5962bSJohannes Berg 
2364a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
23652b9a7e1bSJohannes Berg 	}
23662b9a7e1bSJohannes Berg 
2367a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2368046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2369046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2370c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2371c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2372c9c5962bSJohannes Berg 
2373046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2374046d2e7cSSriram R 						     cpu);
2375c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2376c9c5962bSJohannes Berg 			}
2377c9c5962bSJohannes Berg 		}
2378a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
23792b9a7e1bSJohannes Berg 	}
23802b9a7e1bSJohannes Berg 
2381a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2382046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2383a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
23842b9a7e1bSJohannes Berg 	}
23852b9a7e1bSJohannes Berg 
2386a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2387046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2388a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
23892b9a7e1bSJohannes Berg 	}
23902b9a7e1bSJohannes Berg 
2391b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2392b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2393b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2394b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2395b4809e94SToke Høiland-Jørgensen 	}
2396b4809e94SToke Høiland-Jørgensen 
2397b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2398b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2399b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2400b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2401b4809e94SToke Høiland-Jørgensen 	}
2402b4809e94SToke Høiland-Jørgensen 
2403b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
24042433647bSToke Høiland-Jørgensen 		sinfo->airtime_weight = sta->airtime[0].weight;
2405b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2406b4809e94SToke Høiland-Jørgensen 	}
2407b4809e94SToke Høiland-Jørgensen 
2408046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2409046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2410c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2411c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2412c9c5962bSJohannes Berg 
2413046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2414e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2415c9c5962bSJohannes Berg 		}
2416c9c5962bSJohannes Berg 	}
2417b7ffbd7eSJohannes Berg 
2418225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2419225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2420a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2421a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2422225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2423225b8189SJohannes Berg 	}
2424225b8189SJohannes Berg 
242530686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
242630686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2427a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2428c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2429a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2430b7ffbd7eSJohannes Berg 		}
24312b9a7e1bSJohannes Berg 
2432046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2433a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
243440d9a38aSJohannes Berg 			sinfo->signal_avg =
2435046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2436a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
24372b9a7e1bSJohannes Berg 		}
24382b9a7e1bSJohannes Berg 	}
24392b9a7e1bSJohannes Berg 
2440c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2441c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2442c9c5962bSJohannes Berg 	 * pcpu statistics
2443c9c5962bSJohannes Berg 	 */
2444c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2445a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2446a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2447a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2448046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2449a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2450b7ffbd7eSJohannes Berg 
2451c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2452c9c5962bSJohannes Berg 
2453b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2454e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2455c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2456b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2457046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2458b7ffbd7eSJohannes Berg 		}
2459b7ffbd7eSJohannes Berg 	}
2460b7ffbd7eSJohannes Berg 
2461a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2462046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2463e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2464a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
24652b9a7e1bSJohannes Berg 	}
24662b9a7e1bSJohannes Berg 
2467a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2468a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2469a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
24702b9a7e1bSJohannes Berg 	}
2471b7ffbd7eSJohannes Berg 
24720fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
24736af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
24746af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
24758689c051SArend van Spriel 	}
247679c892b8SJohannes Berg 
2477b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2478b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2479a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2480a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2481a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2482a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2483a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2484dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
24851303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
24861303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2487b7ffbd7eSJohannes Berg 
2488433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2489433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2490433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2491b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2492a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2493433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2494b7ffbd7eSJohannes Berg 		}
2495433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2496433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2497433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2498dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
24991303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2500b7ffbd7eSJohannes Berg #endif
2501b7ffbd7eSJohannes Berg 	}
2502b7ffbd7eSJohannes Berg 
2503b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2504b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2505b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2506b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2507b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2508b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2509b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2510785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2511b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2512b7ffbd7eSJohannes Berg 
2513b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2514b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2515b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2516b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2517b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2518b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2519b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2520b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2521b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2522b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2523b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2524b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2525a74a8c84SJohannes Berg 	if (sta->sta.wme)
2526b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2527b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2528b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2529b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2530b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2531b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2532b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2533b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2534b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2535b7ffbd7eSJohannes Berg 
25363b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
25373b17fbf8SMaxim Altshul 
25383b17fbf8SMaxim Altshul 	if (thr != 0) {
2539a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
25403b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
25413b17fbf8SMaxim Altshul 	}
2542a78b26ffSVenkateswara Naralasetty 
2543a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2544046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2545046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2546a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2547a78b26ffSVenkateswara Naralasetty 	}
2548cc60dbbfSBalaji Pothunoori 
25499c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2550046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2551cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2552cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2553046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2554cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
25559c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2556cc60dbbfSBalaji Pothunoori 	}
2557ab60633cSNarayanraddi Masti 
2558ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2559ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2560ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2561ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2562ab60633cSNarayanraddi Masti 	}
25633b17fbf8SMaxim Altshul }
25643b17fbf8SMaxim Altshul 
25653b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
25663b17fbf8SMaxim Altshul {
25673b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
25683b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
25693b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
25703b17fbf8SMaxim Altshul 	u32 thr = 0;
25713b17fbf8SMaxim Altshul 
25723b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
25733b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
25743b17fbf8SMaxim Altshul 
2575b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2576b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2577b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2578b7ffbd7eSJohannes Berg 	else
25794fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2580b7ffbd7eSJohannes Berg 
25813b17fbf8SMaxim Altshul 	return thr;
2582b7ffbd7eSJohannes Berg }
2583b8da6b6aSJohannes Berg 
2584b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2585b8da6b6aSJohannes Berg {
2586c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2587c9c5962bSJohannes Berg 
2588046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2589046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2590c9c5962bSJohannes Berg 		return stats->last_rx;
2591046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2592b8da6b6aSJohannes Berg }
2593484a54c2SToke Høiland-Jørgensen 
2594484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2595484a54c2SToke Høiland-Jørgensen {
2596484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2597484a54c2SToke Høiland-Jørgensen 		return;
2598484a54c2SToke Høiland-Jørgensen 
2599484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2600484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2601484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2602484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2603484a54c2SToke Høiland-Jørgensen 	} else {
2604484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2605484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2606484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2607484a54c2SToke Høiland-Jørgensen 	}
2608484a54c2SToke Høiland-Jørgensen }
2609484a54c2SToke Høiland-Jørgensen 
2610484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2611484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2612484a54c2SToke Høiland-Jørgensen {
2613484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2614484a54c2SToke Høiland-Jørgensen 
2615484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2616484a54c2SToke Høiland-Jørgensen }
2617