xref: /openbmc/linux/net/mac80211/sta_info.c (revision 2433647bc8d983a543e7d31b41ca2de1c7e2c198)
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
290c9c5962bSJohannes Berg 	free_percpu(sta->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)) {
349c9c5962bSJohannes Berg 		sta->pcpu_rx_stats =
35095f3ce6aSSara Sharon 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
351c9c5962bSJohannes Berg 		if (!sta->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);
36787f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
36887f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
3694c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
3704c02d62fSKees Cook 				    0);
371433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
372433f5bc1SJohannes Berg 	}
37387f59c70SThomas Pedersen #endif
37407346f81SJohannes Berg 
375ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
37617741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
377480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
378480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
379480dd46bSMaxim Altshul 
38096fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
38196fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
38296fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
38396fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
38496fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
38596fc6efbSAlexander Wetzel 	 */
38696fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
38796fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
38896fc6efbSAlexander Wetzel 
389d0709a65SJohannes Berg 	sta->local = local;
390d0709a65SJohannes Berg 	sta->sdata = sdata;
391e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
392f0706e82SJiri Benc 
3930f9c5a61SJohannes Berg 	u64_stats_init(&sta->rx_stats.syncp);
3940f9c5a61SJohannes Berg 
3953a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
3963a11ce08SJohannes Berg 
39771ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
39871ec375cSJohannes Berg 
399b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
400b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
401b6da911bSLiad Kaufman 
40284b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
4030be6ed13SJohannes Berg 	ewma_signal_init(&sta->rx_stats_avg.signal);
404cc60dbbfSBalaji Pothunoori 	ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
4050be6ed13SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
4060be6ed13SJohannes Berg 		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
407541a45a1SBruno Randolf 
408ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
409ba8c3d6fSFelix Fietkau 		void *txq_data;
410ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
411ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
412ba8c3d6fSFelix Fietkau 
413ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
414ba8c3d6fSFelix Fietkau 		if (!txq_data)
415ba8c3d6fSFelix Fietkau 			goto free;
416ba8c3d6fSFelix Fietkau 
417ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
418ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
419ba8c3d6fSFelix Fietkau 
420adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
421fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
422abfbc3afSJohannes Berg 		}
423ba8c3d6fSFelix Fietkau 	}
424ba8c3d6fSFelix Fietkau 
425ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
426ba8c3d6fSFelix Fietkau 		goto free_txq;
427f0706e82SJiri Benc 
428b4809e94SToke Høiland-Jørgensen 
429948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
430948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
431948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
432*2433647bSToke Høiland-Jørgensen 		init_airtime_info(&sta->airtime[i], &local->airtime[i]);
433948d887dSJohannes Berg 	}
43473651ee6SJohannes Berg 
4355a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
4364be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
437cccaec98SSenthil Balasubramanian 
438bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
439bd718fc1SJohannes Berg 		u32 mandatory = 0;
440bd718fc1SJohannes Berg 		int r;
441bd718fc1SJohannes Berg 
442bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
443bd718fc1SJohannes Berg 			continue;
444bd718fc1SJohannes Berg 
445bd718fc1SJohannes Berg 		switch (i) {
446bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
447bd718fc1SJohannes Berg 			/*
448bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
449bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
450bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
451bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
452bd718fc1SJohannes Berg 			 * possible rate anyway.
453bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
454bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
455bd718fc1SJohannes Berg 			 */
456bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
457bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
458bd718fc1SJohannes Berg 			break;
459bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
460bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
461bd718fc1SJohannes Berg 			break;
462bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
463bd718fc1SJohannes Berg 			WARN_ON(1);
464bd718fc1SJohannes Berg 			mandatory = 0;
465bd718fc1SJohannes Berg 			break;
466bd718fc1SJohannes Berg 		}
467bd718fc1SJohannes Berg 
468bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
469bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
470bd718fc1SJohannes Berg 
471bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
472bd718fc1SJohannes Berg 
473bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
474bd718fc1SJohannes Berg 				continue;
475bd718fc1SJohannes Berg 			sta->sta.supp_rates[i] |= BIT(r);
476bd718fc1SJohannes Berg 		}
477bd718fc1SJohannes Berg 	}
478bd718fc1SJohannes Berg 
479af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
480687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
481687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
48221a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
48321a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
48421a8e9ddSMohammed Shafi Shajakhan 
48521a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
48621a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
48721a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
48821a8e9ddSMohammed Shafi Shajakhan 
48921a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
490687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
491687da132SEmmanuel Grumbach 		/*
492687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
493687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
494687da132SEmmanuel Grumbach 		 */
495687da132SEmmanuel Grumbach 		switch (smps) {
496687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
497687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
498687da132SEmmanuel Grumbach 			break;
499687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
500687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
501687da132SEmmanuel Grumbach 			break;
502687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
503687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
504687da132SEmmanuel Grumbach 			break;
505687da132SEmmanuel Grumbach 		default:
506687da132SEmmanuel Grumbach 			WARN_ON(1);
507687da132SEmmanuel Grumbach 		}
508687da132SEmmanuel Grumbach 	}
509af0ed69bSJohannes Berg 
5106e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
5116e0456b5SFelix Fietkau 
512484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
513484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
514484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
515484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
516484a54c2SToke Høiland-Jørgensen 
517bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
518ef04a297SJohannes Berg 
519abfbc3afSJohannes Berg 	return sta;
520ba8c3d6fSFelix Fietkau 
521ba8c3d6fSFelix Fietkau free_txq:
522ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
523ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
524ba8c3d6fSFelix Fietkau free:
525d78d9ee9SSara Sharon 	free_percpu(sta->pcpu_rx_stats);
526433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
527433f5bc1SJohannes Berg 	kfree(sta->mesh);
528433f5bc1SJohannes Berg #endif
529ba8c3d6fSFelix Fietkau 	kfree(sta);
530ba8c3d6fSFelix Fietkau 	return NULL;
53173651ee6SJohannes Berg }
53273651ee6SJohannes Berg 
5338c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
53434e89507SJohannes Berg {
53534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
53634e89507SJohannes Berg 
53703e4497eSJohannes Berg 	/*
53803e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
53903e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
54003e4497eSJohannes Berg 	 * and another CPU turns off the net device.
54103e4497eSJohannes Berg 	 */
5428c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
5438c71df7aSGuy Eilam 		return -ENETDOWN;
54403e4497eSJohannes Berg 
545b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
5468c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
5478c71df7aSGuy Eilam 		return -EINVAL;
5488c71df7aSGuy Eilam 
54983e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
55083e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
55183e7e4ceSHerbert Xu 	 * for correctness.
55231104891SJohannes Berg 	 */
55331104891SJohannes Berg 	rcu_read_lock();
55431104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
55531104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
55631104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
55731104891SJohannes Berg 		rcu_read_unlock();
55831104891SJohannes Berg 		return -ENOTUNIQ;
55931104891SJohannes Berg 	}
56031104891SJohannes Berg 	rcu_read_unlock();
56131104891SJohannes Berg 
5628c71df7aSGuy Eilam 	return 0;
56393e5deb1SJohannes Berg }
56444213b5eSJohannes Berg 
565f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
566f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
567f09603a2SJohannes Berg 				     struct sta_info *sta)
568f09603a2SJohannes Berg {
569f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
570f09603a2SJohannes Berg 	int err = 0;
571f09603a2SJohannes Berg 
572f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
573f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
574f09603a2SJohannes Berg 		if (err)
575f09603a2SJohannes Berg 			break;
576f09603a2SJohannes Berg 	}
577f09603a2SJohannes Berg 
578f09603a2SJohannes Berg 	if (!err) {
579a4ec45a4SJohannes Berg 		/*
580a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
581a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
582a4ec45a4SJohannes Berg 		 */
583a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
584f09603a2SJohannes Berg 			sta->uploaded = true;
585f09603a2SJohannes Berg 		return 0;
586f09603a2SJohannes Berg 	}
587f09603a2SJohannes Berg 
588f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
589bdcbd8e0SJohannes Berg 		sdata_info(sdata,
590bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
591bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
592f09603a2SJohannes Berg 		err = 0;
593f09603a2SJohannes Berg 	}
594f09603a2SJohannes Berg 
595f09603a2SJohannes Berg 	/* unwind on error */
596f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
597f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
598f09603a2SJohannes Berg 
599f09603a2SJohannes Berg 	return err;
600f09603a2SJohannes Berg }
601f09603a2SJohannes Berg 
602d405fd8cSGregory Greenman static void
603d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
604d405fd8cSGregory Greenman {
605d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
606d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
607d405fd8cSGregory Greenman 	struct sta_info *sta;
608d405fd8cSGregory Greenman 
609d405fd8cSGregory Greenman 	rcu_read_lock();
610d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
611d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
612d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
613d405fd8cSGregory Greenman 			continue;
614d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
615d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
616d405fd8cSGregory Greenman 			break;
617d405fd8cSGregory Greenman 		}
618d405fd8cSGregory Greenman 	}
619d405fd8cSGregory Greenman 	rcu_read_unlock();
620d405fd8cSGregory Greenman 
621d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
622d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
623d405fd8cSGregory Greenman 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
624d405fd8cSGregory Greenman 	}
625d405fd8cSGregory Greenman }
626d405fd8cSGregory Greenman 
62734e89507SJohannes Berg /*
6288c71df7aSGuy Eilam  * should be called with sta_mtx locked
6298c71df7aSGuy Eilam  * this function replaces the mutex lock
6308c71df7aSGuy Eilam  * with a RCU lock
6318c71df7aSGuy Eilam  */
6324d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
6338c71df7aSGuy Eilam {
6348c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
6358c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
6360c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
6378c71df7aSGuy Eilam 	int err = 0;
6388c71df7aSGuy Eilam 
6398c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
6408c71df7aSGuy Eilam 
6417852e361SJohannes Berg 	/* check if STA exists already */
6427852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
6434d33960bSJohannes Berg 		err = -EEXIST;
6444d33960bSJohannes Berg 		goto out_err;
64534e89507SJohannes Berg 	}
64634e89507SJohannes Berg 
6470c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
6480c2e3842SKoen Vandeputte 	if (!sinfo) {
6490c2e3842SKoen Vandeputte 		err = -ENOMEM;
6500c2e3842SKoen Vandeputte 		goto out_err;
6510c2e3842SKoen Vandeputte 	}
6520c2e3842SKoen Vandeputte 
6534d33960bSJohannes Berg 	local->num_sta++;
6544d33960bSJohannes Berg 	local->sta_generation++;
6554d33960bSJohannes Berg 	smp_mb();
6564d33960bSJohannes Berg 
6575108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
6585108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
6595108ca82SJohannes Berg 
6604d33960bSJohannes Berg 	/* make the station visible */
66162b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
66262b14b24SJohannes Berg 	if (err)
66362b14b24SJohannes Berg 		goto out_drop_sta;
6644d33960bSJohannes Berg 
6652bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
66683d5cc01SJohannes Berg 
6675108ca82SJohannes Berg 	/* notify driver */
6685108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
6695108ca82SJohannes Berg 	if (err)
6705108ca82SJohannes Berg 		goto out_remove;
6715108ca82SJohannes Berg 
67283d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
673d405fd8cSGregory Greenman 
674d405fd8cSGregory Greenman 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
675d405fd8cSGregory Greenman 		ieee80211_recalc_min_chandef(sta->sdata);
676d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps)
677d405fd8cSGregory Greenman 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
678d405fd8cSGregory Greenman 	}
679d405fd8cSGregory Greenman 
6805108ca82SJohannes Berg 	/* accept BA sessions now */
6815108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
6824d33960bSJohannes Berg 
6834d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
6844d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
6854d33960bSJohannes Berg 
6860ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
6870ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
6880ef049dcSArnd Bergmann 	kfree(sinfo);
689d0709a65SJohannes Berg 
690bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
691f0706e82SJiri Benc 
69234e89507SJohannes Berg 	/* move reference to rcu-protected */
69334e89507SJohannes Berg 	rcu_read_lock();
69434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
695e9f207f0SJiri Benc 
69673651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
69773651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
69873651ee6SJohannes Berg 
69973651ee6SJohannes Berg 	return 0;
7005108ca82SJohannes Berg  out_remove:
7015108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
7025108ca82SJohannes Berg 	list_del_rcu(&sta->list);
70362b14b24SJohannes Berg  out_drop_sta:
7045108ca82SJohannes Berg 	local->num_sta--;
7055108ca82SJohannes Berg 	synchronize_net();
7067bc40aedSJohannes Berg 	cleanup_single_sta(sta);
7074d33960bSJohannes Berg  out_err:
7084d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
709ea32f065SSudip Mukherjee 	kfree(sinfo);
7104d33960bSJohannes Berg 	rcu_read_lock();
7114d33960bSJohannes Berg 	return err;
7128c71df7aSGuy Eilam }
7138c71df7aSGuy Eilam 
7148c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
7158c71df7aSGuy Eilam {
7168c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
717308f7fcfSZhao, Gang 	int err;
7188c71df7aSGuy Eilam 
7194d33960bSJohannes Berg 	might_sleep();
7204d33960bSJohannes Berg 
72131104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
72231104891SJohannes Berg 
7238c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
7248c71df7aSGuy Eilam 	if (err) {
7257bc40aedSJohannes Berg 		sta_info_free(local, sta);
72631104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
7278c71df7aSGuy Eilam 		rcu_read_lock();
7287bc40aedSJohannes Berg 		return err;
7298c71df7aSGuy Eilam 	}
7308c71df7aSGuy Eilam 
7317bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
732f0706e82SJiri Benc }
733f0706e82SJiri Benc 
73434e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
73534e89507SJohannes Berg {
73634e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
73734e89507SJohannes Berg 
73834e89507SJohannes Berg 	rcu_read_unlock();
73934e89507SJohannes Berg 
74034e89507SJohannes Berg 	return err;
74134e89507SJohannes Berg }
74234e89507SJohannes Berg 
743d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
744004c872eSJohannes Berg {
745004c872eSJohannes Berg 	/*
746004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
747004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
748004c872eSJohannes Berg 	 */
749d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
750004c872eSJohannes Berg }
751004c872eSJohannes Berg 
752d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
753004c872eSJohannes Berg {
754004c872eSJohannes Berg 	/*
755004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
756004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
757004c872eSJohannes Berg 	 */
758d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
759004c872eSJohannes Berg }
760004c872eSJohannes Berg 
7613d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
7623d5839b6SIlan Peer {
7633d5839b6SIlan Peer 	/*
7643d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
7653d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
7663d5839b6SIlan Peer 	 */
7673d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
7683d5839b6SIlan Peer }
7693d5839b6SIlan Peer 
770948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
771004c872eSJohannes Berg {
772948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
773948d887dSJohannes Berg 	switch (ac) {
774948d887dSJohannes Berg 	case IEEE80211_AC_VO:
775948d887dSJohannes Berg 		return BIT(6) | BIT(7);
776948d887dSJohannes Berg 	case IEEE80211_AC_VI:
777948d887dSJohannes Berg 		return BIT(4) | BIT(5);
778948d887dSJohannes Berg 	case IEEE80211_AC_BE:
779948d887dSJohannes Berg 		return BIT(0) | BIT(3);
780948d887dSJohannes Berg 	case IEEE80211_AC_BK:
781948d887dSJohannes Berg 		return BIT(1) | BIT(2);
782948d887dSJohannes Berg 	default:
783948d887dSJohannes Berg 		WARN_ON(1);
784948d887dSJohannes Berg 		return 0;
785d0709a65SJohannes Berg 	}
786004c872eSJohannes Berg }
787004c872eSJohannes Berg 
7889b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
789004c872eSJohannes Berg {
790c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
791d012a605SMarco Porsch 	struct ps_data *ps;
792948d887dSJohannes Berg 	bool indicate_tim = false;
793948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
794948d887dSJohannes Berg 	int ac;
795a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
796004c872eSJohannes Berg 
797d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
798d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
799c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
800c868cb35SJohannes Berg 			return;
8013e122be0SJohannes Berg 
802d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
8033f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
8043f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
8053f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
8063f52b7e3SMarco Porsch #endif
807d012a605SMarco Porsch 	} else {
808d012a605SMarco Porsch 		return;
809d012a605SMarco Porsch 	}
810d012a605SMarco Porsch 
811c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
812d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
813c868cb35SJohannes Berg 		return;
814004c872eSJohannes Berg 
815c868cb35SJohannes Berg 	if (sta->dead)
816c868cb35SJohannes Berg 		goto done;
8173e122be0SJohannes Berg 
818948d887dSJohannes Berg 	/*
819948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
820948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
821948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
822948d887dSJohannes Berg 	 * non-enabled ones.
823948d887dSJohannes Berg 	 */
824948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
825948d887dSJohannes Berg 		ignore_for_tim = 0;
826948d887dSJohannes Berg 
8279b7a86f3SJohannes Berg 	if (ignore_pending)
8289b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
8299b7a86f3SJohannes Berg 
830948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
831948d887dSJohannes Berg 		unsigned long tids;
832948d887dSJohannes Berg 
833f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
834948d887dSJohannes Berg 			continue;
835948d887dSJohannes Berg 
836948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
837948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
838948d887dSJohannes Berg 		if (indicate_tim)
839948d887dSJohannes Berg 			break;
840948d887dSJohannes Berg 
841948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
842948d887dSJohannes Berg 
843948d887dSJohannes Berg 		indicate_tim |=
844948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
845ba8c3d6fSFelix Fietkau 		indicate_tim |=
846ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
847004c872eSJohannes Berg 	}
848004c872eSJohannes Berg 
849c868cb35SJohannes Berg  done:
85065f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
851004c872eSJohannes Berg 
8523d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
8533d5839b6SIlan Peer 		goto out_unlock;
8543d5839b6SIlan Peer 
855948d887dSJohannes Berg 	if (indicate_tim)
856d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
857c868cb35SJohannes Berg 	else
858d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
8593e122be0SJohannes Berg 
8609b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
861c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
862948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
863c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
864004c872eSJohannes Berg 	}
865004c872eSJohannes Berg 
8663d5839b6SIlan Peer out_unlock:
86765f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
868004c872eSJohannes Berg }
869004c872eSJohannes Berg 
8709b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
8719b7a86f3SJohannes Berg {
8729b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
8739b7a86f3SJohannes Berg }
8749b7a86f3SJohannes Berg 
875cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
876f0706e82SJiri Benc {
877e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
878f0706e82SJiri Benc 	int timeout;
879f0706e82SJiri Benc 
880f0706e82SJiri Benc 	if (!skb)
881cd0b8d89SJohannes Berg 		return false;
882f0706e82SJiri Benc 
883e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
884f0706e82SJiri Benc 
885f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
88657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
88757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
88857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
889f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
890f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
891e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
892f0706e82SJiri Benc }
893f0706e82SJiri Benc 
894f0706e82SJiri Benc 
895948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
896948d887dSJohannes Berg 						struct sta_info *sta, int ac)
897f0706e82SJiri Benc {
898f0706e82SJiri Benc 	unsigned long flags;
899f0706e82SJiri Benc 	struct sk_buff *skb;
900f0706e82SJiri Benc 
90160750397SJohannes Berg 	/*
90260750397SJohannes Berg 	 * First check for frames that should expire on the filtered
90360750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
90460750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
90560750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
90660750397SJohannes Berg 	 * total_ps_buffered counter.
90760750397SJohannes Berg 	 */
908f0706e82SJiri Benc 	for (;;) {
909948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
910948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
91157c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
912948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
913836341a7SJohannes Berg 		else
914f0706e82SJiri Benc 			skb = NULL;
915948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
916f0706e82SJiri Benc 
91760750397SJohannes Berg 		/*
91860750397SJohannes Berg 		 * Frames are queued in order, so if this one
91960750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
92060750397SJohannes Berg 		 * we actually reached the end of the queue we
92160750397SJohannes Berg 		 * also need to stop, of course.
92260750397SJohannes Berg 		 */
92360750397SJohannes Berg 		if (!skb)
92460750397SJohannes Berg 			break;
925d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
92660750397SJohannes Berg 	}
92760750397SJohannes Berg 
92860750397SJohannes Berg 	/*
92960750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
93060750397SJohannes Berg 	 * only find something if the filtered queue was emptied
93160750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
93260750397SJohannes Berg 	 * buffered frames.
93360750397SJohannes Berg 	 */
934f0706e82SJiri Benc 	for (;;) {
935948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
936948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
937f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
938948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
939f0706e82SJiri Benc 		else
940f0706e82SJiri Benc 			skb = NULL;
941948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
942f0706e82SJiri Benc 
94360750397SJohannes Berg 		/*
94460750397SJohannes Berg 		 * frames are queued in order, so if this one
94560750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
94660750397SJohannes Berg 		 * the queue) we can stop testing
94760750397SJohannes Berg 		 */
948836341a7SJohannes Berg 		if (!skb)
949836341a7SJohannes Berg 			break;
950836341a7SJohannes Berg 
951f0706e82SJiri Benc 		local->total_ps_buffered--;
952bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
953bdcbd8e0SJohannes Berg 		       sta->sta.addr);
954d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
955f0706e82SJiri Benc 	}
9563393a608SJuuso Oikarinen 
95760750397SJohannes Berg 	/*
95860750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
95960750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
96060750397SJohannes Berg 	 * frames.
96160750397SJohannes Berg 	 */
96260750397SJohannes Berg 	sta_info_recalc_tim(sta);
96360750397SJohannes Berg 
96460750397SJohannes Berg 	/*
96560750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
96660750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
96760750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
96860750397SJohannes Berg 	 */
969948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
970948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
971948d887dSJohannes Berg }
972948d887dSJohannes Berg 
973948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
974948d887dSJohannes Berg 					     struct sta_info *sta)
975948d887dSJohannes Berg {
976948d887dSJohannes Berg 	bool have_buffered = false;
977948d887dSJohannes Berg 	int ac;
978948d887dSJohannes Berg 
9793f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
9803f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
9813f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
982948d887dSJohannes Berg 		return false;
983948d887dSJohannes Berg 
984948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
985948d887dSJohannes Berg 		have_buffered |=
986948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
987948d887dSJohannes Berg 
988948d887dSJohannes Berg 	return have_buffered;
989f0706e82SJiri Benc }
990f0706e82SJiri Benc 
991d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
99234e89507SJohannes Berg {
99334e89507SJohannes Berg 	struct ieee80211_local *local;
99434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
9956d10e46bSJohannes Berg 	int ret;
99634e89507SJohannes Berg 
99734e89507SJohannes Berg 	might_sleep();
99834e89507SJohannes Berg 
99934e89507SJohannes Berg 	if (!sta)
100034e89507SJohannes Berg 		return -ENOENT;
100134e89507SJohannes Berg 
100234e89507SJohannes Berg 	local = sta->local;
100334e89507SJohannes Berg 	sdata = sta->sdata;
100434e89507SJohannes Berg 
100583d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
100683d5cc01SJohannes Berg 
1007098a6070SJohannes Berg 	/*
1008098a6070SJohannes Berg 	 * Before removing the station from the driver and
1009098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1010098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1011098a6070SJohannes Berg 	 * will be sufficient.
1012098a6070SJohannes Berg 	 */
1013c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1014c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1015098a6070SJohannes Berg 
1016f59374ebSSara Sharon 	/*
1017f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1018f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1019f59374ebSSara Sharon 	 * all such frames to be processed.
1020f59374ebSSara Sharon 	 */
1021f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1022f59374ebSSara Sharon 
102334e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1024b01711beSJohannes Berg 	if (WARN_ON(ret))
102534e89507SJohannes Berg 		return ret;
102634e89507SJohannes Berg 
1027a7a6bdd0SArik Nemtsov 	/*
1028a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1029a7a6bdd0SArik Nemtsov 	 * removal.
1030a7a6bdd0SArik Nemtsov 	 */
1031a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1032a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1033a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1034a7a6bdd0SArik Nemtsov 	}
1035a7a6bdd0SArik Nemtsov 
1036794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1037ef044763SEliad Peller 	sta->removed = true;
10384d33960bSJohannes Berg 
10396a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
10406a9d1b91SJohannes Berg 
1041a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1042a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1043a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1044a710c816SJohannes Berg 
1045d778207bSJohannes Berg 	return 0;
1046d778207bSJohannes Berg }
1047d778207bSJohannes Berg 
1048d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1049d778207bSJohannes Berg {
1050d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1051d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
10520ef049dcSArnd Bergmann 	struct station_info *sinfo;
1053d778207bSJohannes Berg 	int ret;
1054d778207bSJohannes Berg 
1055d778207bSJohannes Berg 	/*
1056d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1057d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1058d778207bSJohannes Berg 	 */
1059d778207bSJohannes Berg 
1060d778207bSJohannes Berg 	might_sleep();
1061d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1062d778207bSJohannes Berg 
10635981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1064b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1065b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1066b16798f5SJohannes Berg 	}
1067b16798f5SJohannes Berg 
1068c8782078SJohannes Berg 	/* now keys can no longer be reached */
10696d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
107034e89507SJohannes Berg 
10719b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
10729b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
10739b7a86f3SJohannes Berg 
107434e89507SJohannes Berg 	sta->dead = true;
107534e89507SJohannes Berg 
107634e89507SJohannes Berg 	local->num_sta--;
107734e89507SJohannes Berg 	local->sta_generation++;
107834e89507SJohannes Berg 
107983d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1080f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1081f09603a2SJohannes Berg 		if (ret) {
108283d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
108383d5cc01SJohannes Berg 			break;
108483d5cc01SJohannes Berg 		}
108583d5cc01SJohannes Berg 	}
1086d9a7ddb0SJohannes Berg 
1087f09603a2SJohannes Berg 	if (sta->uploaded) {
1088f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1089f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1090f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1091f09603a2SJohannes Berg 	}
109234e89507SJohannes Berg 
1093bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1094bdcbd8e0SJohannes Berg 
10950ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
10960ef049dcSArnd Bergmann 	if (sinfo)
10970fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
10980ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
10990ef049dcSArnd Bergmann 	kfree(sinfo);
1100ec15e68bSJouni Malinen 
110134e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
110234e89507SJohannes Berg 
11033a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
11043a11ce08SJohannes Berg 
1105d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1106d778207bSJohannes Berg }
1107d778207bSJohannes Berg 
1108d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1109d778207bSJohannes Berg {
1110d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1111d778207bSJohannes Berg 
1112d778207bSJohannes Berg 	if (err)
1113d778207bSJohannes Berg 		return err;
1114d778207bSJohannes Berg 
1115d778207bSJohannes Berg 	synchronize_net();
1116d778207bSJohannes Berg 
1117d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
111834e89507SJohannes Berg 
111934e89507SJohannes Berg 	return 0;
112034e89507SJohannes Berg }
112134e89507SJohannes Berg 
112234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
112334e89507SJohannes Berg {
112434e89507SJohannes Berg 	struct sta_info *sta;
112534e89507SJohannes Berg 	int ret;
112634e89507SJohannes Berg 
112734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
11287852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
112934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
113034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
113134e89507SJohannes Berg 
113234e89507SJohannes Berg 	return ret;
113334e89507SJohannes Berg }
113434e89507SJohannes Berg 
113534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
113634e89507SJohannes Berg 			      const u8 *addr)
113734e89507SJohannes Berg {
113834e89507SJohannes Berg 	struct sta_info *sta;
113934e89507SJohannes Berg 	int ret;
114034e89507SJohannes Berg 
114134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
11427852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
114334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
114434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
114534e89507SJohannes Berg 
114634e89507SJohannes Berg 	return ret;
114734e89507SJohannes Berg }
1148f0706e82SJiri Benc 
114934f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1150f0706e82SJiri Benc {
115134f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1152f0706e82SJiri Benc 	struct sta_info *sta;
11533393a608SJuuso Oikarinen 	bool timer_needed = false;
1154f0706e82SJiri Benc 
1155d0709a65SJohannes Berg 	rcu_read_lock();
1156d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
11573393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
11583393a608SJuuso Oikarinen 			timer_needed = true;
1159d0709a65SJohannes Berg 	rcu_read_unlock();
1160f0706e82SJiri Benc 
11615bb644a0SJohannes Berg 	if (local->quiescing)
11625bb644a0SJohannes Berg 		return;
11635bb644a0SJohannes Berg 
11643393a608SJuuso Oikarinen 	if (!timer_needed)
11653393a608SJuuso Oikarinen 		return;
11663393a608SJuuso Oikarinen 
116726d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
116826d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1169f0706e82SJiri Benc }
1170f0706e82SJiri Benc 
11717bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
11727bedd0cfSJohannes Berg {
11737bedd0cfSJohannes Berg 	int err;
11747bedd0cfSJohannes Berg 
117583e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
11767bedd0cfSJohannes Berg 	if (err)
11777bedd0cfSJohannes Berg 		return err;
11787bedd0cfSJohannes Berg 
11794d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
118034e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1181f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1182f0706e82SJiri Benc 
118334f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
11847bedd0cfSJohannes Berg 	return 0;
1185f0706e82SJiri Benc }
1186f0706e82SJiri Benc 
1187f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1188f0706e82SJiri Benc {
1189a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
119083e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1191f0706e82SJiri Benc }
1192f0706e82SJiri Benc 
1193051007d9SJohannes Berg 
1194e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1195f0706e82SJiri Benc {
1196b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1197f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1198d778207bSJohannes Berg 	LIST_HEAD(free_list);
119944213b5eSJohannes Berg 	int ret = 0;
1200f0706e82SJiri Benc 
1201d0709a65SJohannes Berg 	might_sleep();
1202d0709a65SJohannes Berg 
1203e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1204e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1205e716251dSJohannes Berg 
120634e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
120734e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1208e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1209e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1210d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1211d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
121234316837SJohannes Berg 			ret++;
121334316837SJohannes Berg 		}
121434e89507SJohannes Berg 	}
1215d778207bSJohannes Berg 
1216d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1217d778207bSJohannes Berg 		synchronize_net();
1218d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1219d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1220d778207bSJohannes Berg 	}
122134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
122244213b5eSJohannes Berg 
1223051007d9SJohannes Berg 	return ret;
1224051007d9SJohannes Berg }
1225051007d9SJohannes Berg 
122624723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
122724723d1bSJohannes Berg 			  unsigned long exp_time)
122824723d1bSJohannes Berg {
122924723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
123024723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
123124723d1bSJohannes Berg 
123234e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1233e46a2cf9SMohammed Shafi Shajakhan 
1234e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1235b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1236b8da6b6aSJohannes Berg 
1237ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1238ec2b774eSMarek Lindner 			continue;
1239ec2b774eSMarek Lindner 
1240b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1241eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1242bdcbd8e0SJohannes Berg 				sta->sta.addr);
12433f52b7e3SMarco Porsch 
12443f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
12453f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
12463f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
12473f52b7e3SMarco Porsch 
124834e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
124924723d1bSJohannes Berg 		}
1250e46a2cf9SMohammed Shafi Shajakhan 	}
1251e46a2cf9SMohammed Shafi Shajakhan 
125234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
125324723d1bSJohannes Berg }
125417741cdcSJohannes Berg 
1255686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1256686b9cb9SBen Greear 						   const u8 *addr,
1257686b9cb9SBen Greear 						   const u8 *localaddr)
125817741cdcSJohannes Berg {
12597bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
126083e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
12617bedd0cfSJohannes Berg 	struct sta_info *sta;
126217741cdcSJohannes Berg 
1263686b9cb9SBen Greear 	/*
1264686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1265686b9cb9SBen Greear 	 * ... first in list.
1266686b9cb9SBen Greear 	 */
126783e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1268686b9cb9SBen Greear 		if (localaddr &&
1269b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1270686b9cb9SBen Greear 			continue;
1271f7c65594SJohannes Berg 		if (!sta->uploaded)
1272f7c65594SJohannes Berg 			return NULL;
127317741cdcSJohannes Berg 		return &sta->sta;
1274f7c65594SJohannes Berg 	}
1275f7c65594SJohannes Berg 
1276abe60632SJohannes Berg 	return NULL;
127717741cdcSJohannes Berg }
1278686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
12795ed176e1SJohannes Berg 
12805ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
12815ed176e1SJohannes Berg 					 const u8 *addr)
12825ed176e1SJohannes Berg {
1283f7c65594SJohannes Berg 	struct sta_info *sta;
12845ed176e1SJohannes Berg 
12855ed176e1SJohannes Berg 	if (!vif)
12865ed176e1SJohannes Berg 		return NULL;
12875ed176e1SJohannes Berg 
1288f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1289f7c65594SJohannes Berg 	if (!sta)
1290f7c65594SJohannes Berg 		return NULL;
12915ed176e1SJohannes Berg 
1292f7c65594SJohannes Berg 	if (!sta->uploaded)
1293f7c65594SJohannes Berg 		return NULL;
1294f7c65594SJohannes Berg 
1295f7c65594SJohannes Berg 	return &sta->sta;
12965ed176e1SJohannes Berg }
129717741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1298af818581SJohannes Berg 
1299e3685e03SJohannes Berg /* powersave support code */
1300e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
130150a9432dSJohannes Berg {
1302608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1303e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1304e3685e03SJohannes Berg 	struct sk_buff_head pending;
1305ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1306e3685e03SJohannes Berg 	unsigned long flags;
1307d012a605SMarco Porsch 	struct ps_data *ps;
1308d012a605SMarco Porsch 
13093918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
13103918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
13113918edb0SFelix Fietkau 				     u.ap);
13123918edb0SFelix Fietkau 
13133918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1314d012a605SMarco Porsch 		ps = &sdata->bss->ps;
13153f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
13163f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1317d012a605SMarco Porsch 	else
1318d012a605SMarco Porsch 		return;
131950a9432dSJohannes Berg 
1320c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
132147086fc5SJohannes Berg 
13225a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1323948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1324ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1325948d887dSJohannes Berg 
132630686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
132712375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1328af818581SJohannes Berg 
1329ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1330adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1331ba8c3d6fSFelix Fietkau 			continue;
1332ba8c3d6fSFelix Fietkau 
133318667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1334ba8c3d6fSFelix Fietkau 	}
1335ba8c3d6fSFelix Fietkau 
1336948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1337af818581SJohannes Berg 
13381d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
13391d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1340af818581SJohannes Berg 	/* Send all buffered frames to the station */
1341948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1342948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1343948d887dSJohannes Berg 
1344987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1345948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1346987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1347948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1348948d887dSJohannes Berg 		filtered += tmp - count;
1349948d887dSJohannes Berg 		count = tmp;
1350948d887dSJohannes Berg 
1351987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1352948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1353987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1354948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1355948d887dSJohannes Berg 		buffered += tmp - count;
1356948d887dSJohannes Berg 	}
1357948d887dSJohannes Berg 
1358e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
13595ac2e350SJohannes Berg 
13605ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
13615ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
13625ac2e350SJohannes Berg 
13635ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
13645ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
13655ac2e350SJohannes Berg 	 */
13665ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
13675ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
13681d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1369948d887dSJohannes Berg 
1370e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1371e3685e03SJohannes Berg 
1372af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1373af818581SJohannes Berg 
1374c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1375c868cb35SJohannes Berg 
1376bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
13772595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1378bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
137917c18bf8SJohannes Berg 
138017c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1381af818581SJohannes Berg }
1382af818581SJohannes Berg 
13830ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1384b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
13850ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1386ce662b44SJohannes Berg {
13870ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1388ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1389ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1390ce662b44SJohannes Berg 	struct sk_buff *skb;
1391ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1392ce662b44SJohannes Berg 	__le16 fc;
1393a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1394ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
139555de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1396ce662b44SJohannes Berg 
1397ce662b44SJohannes Berg 	if (qos) {
1398ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1399ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1400ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1401ce662b44SJohannes Berg 	} else {
1402ce662b44SJohannes Berg 		size -= 2;
1403ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1404ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1405ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1406ce662b44SJohannes Berg 	}
1407ce662b44SJohannes Berg 
1408ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1409ce662b44SJohannes Berg 	if (!skb)
1410ce662b44SJohannes Berg 		return;
1411ce662b44SJohannes Berg 
1412ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1413ce662b44SJohannes Berg 
14144df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1415ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1416ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1417ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1418ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1419ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1420864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1421ce662b44SJohannes Berg 
1422ce662b44SJohannes Berg 	skb->priority = tid;
1423ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
142459b66255SJohannes Berg 	if (qos) {
1425ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1426ce662b44SJohannes Berg 
14270ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1428ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1429ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
14300ead2510SEmmanuel Grumbach 			if (more_data)
14310ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
14320ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
14330ead2510SEmmanuel Grumbach 		}
1434ce662b44SJohannes Berg 	}
1435ce662b44SJohannes Berg 
1436ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1437ce662b44SJohannes Berg 
1438ce662b44SJohannes Berg 	/*
1439ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1440ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1441deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1442deeaee19SJohannes Berg 	 * ends the poll/service period.
1443ce662b44SJohannes Berg 	 */
144402f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1445deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1446ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1447ce662b44SJohannes Berg 
14486b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
14496b127c71SSujith Manoharan 
1450b77cf4f8SJohannes Berg 	if (call_driver)
1451b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1452b77cf4f8SJohannes Berg 					  reason, false);
145340b96408SJohannes Berg 
145489afe614SJohannes Berg 	skb->dev = sdata->dev;
145589afe614SJohannes Berg 
145655de908aSJohannes Berg 	rcu_read_lock();
145755de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
145855de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
145955de908aSJohannes Berg 		rcu_read_unlock();
146055de908aSJohannes Berg 		kfree_skb(skb);
146155de908aSJohannes Berg 		return;
146255de908aSJohannes Berg 	}
146355de908aSJohannes Berg 
146473c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
146508aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
146655de908aSJohannes Berg 	rcu_read_unlock();
1467ce662b44SJohannes Berg }
1468ce662b44SJohannes Berg 
14690a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
14700a1cb809SJohannes Berg {
14710a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
14720a1cb809SJohannes Berg 	if (tids & 0xF8)
14730a1cb809SJohannes Berg 		return fls(tids) - 1;
14740a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
14750a1cb809SJohannes Berg 	if (tids & BIT(0))
14760a1cb809SJohannes Berg 		return 0;
14770a1cb809SJohannes Berg 	return fls(tids) - 1;
14780a1cb809SJohannes Berg }
14790a1cb809SJohannes Berg 
14800ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
14810ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
14820ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
14830ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
14840ead2510SEmmanuel Grumbach  */
14850ead2510SEmmanuel Grumbach static bool
14860ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
14870ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
14880ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
14890ead2510SEmmanuel Grumbach {
14900ead2510SEmmanuel Grumbach 	int ac;
14910ead2510SEmmanuel Grumbach 
14920ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
14930ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
14940ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
14950ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
14960ead2510SEmmanuel Grumbach 	 */
14970ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
14980ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
14990ead2510SEmmanuel Grumbach 		return true;
15000ead2510SEmmanuel Grumbach 
15010ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1502f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
15030ead2510SEmmanuel Grumbach 			continue;
15040ead2510SEmmanuel Grumbach 
15050ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15060ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
15070ead2510SEmmanuel Grumbach 			return true;
15080ead2510SEmmanuel Grumbach 	}
15090ead2510SEmmanuel Grumbach 
15100ead2510SEmmanuel Grumbach 	return false;
15110ead2510SEmmanuel Grumbach }
15120ead2510SEmmanuel Grumbach 
151347086fc5SJohannes Berg static void
15140ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
15150ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
15160ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
15170ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1518af818581SJohannes Berg {
1519af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1520af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1521948d887dSJohannes Berg 	int ac;
1522af818581SJohannes Berg 
1523f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1524948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
15254049e09aSJohannes Berg 		unsigned long tids;
15264049e09aSJohannes Berg 
1527f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1528948d887dSJohannes Berg 			continue;
1529948d887dSJohannes Berg 
15304049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
15314049e09aSJohannes Berg 
1532f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1533f9f760b4SJohannes Berg 		 * release from hardware queues
1534f9f760b4SJohannes Berg 		 */
15350ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
15360ead2510SEmmanuel Grumbach 			*driver_release_tids |=
15370ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
15380ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1539ba8c3d6fSFelix Fietkau 		}
1540f9f760b4SJohannes Berg 
15410ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
154247086fc5SJohannes Berg 			struct sk_buff *skb;
154347086fc5SJohannes Berg 
154447086fc5SJohannes Berg 			while (n_frames > 0) {
1545948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1546948d887dSJohannes Berg 				if (!skb) {
154747086fc5SJohannes Berg 					skb = skb_dequeue(
154847086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1549af818581SJohannes Berg 					if (skb)
1550af818581SJohannes Berg 						local->total_ps_buffered--;
1551af818581SJohannes Berg 				}
155247086fc5SJohannes Berg 				if (!skb)
155347086fc5SJohannes Berg 					break;
155447086fc5SJohannes Berg 				n_frames--;
15550ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
155647086fc5SJohannes Berg 			}
1557948d887dSJohannes Berg 		}
1558948d887dSJohannes Berg 
15590ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
15600ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
15610ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
15624049e09aSJohannes Berg 		 */
1563948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15640ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1565948d887dSJohannes Berg 			break;
1566948d887dSJohannes Berg 	}
1567948d887dSJohannes Berg }
1568af818581SJohannes Berg 
15690ead2510SEmmanuel Grumbach static void
15700ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
15710ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
15720ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
15730ead2510SEmmanuel Grumbach {
15740ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
15750ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
15760ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
15770ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
15780ead2510SEmmanuel Grumbach 	bool more_data;
15790ead2510SEmmanuel Grumbach 
15800ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
15810ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
15820ead2510SEmmanuel Grumbach 
15830ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
15840ead2510SEmmanuel Grumbach 
15850ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
15860ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
15870ead2510SEmmanuel Grumbach 
15880ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
15890ead2510SEmmanuel Grumbach 
15901a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
15910ead2510SEmmanuel Grumbach 		driver_release_tids =
15920ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
15930ead2510SEmmanuel Grumbach 
1594f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1595f438ceb8SEmmanuel Grumbach 		int tid, ac;
15964049e09aSJohannes Berg 
1597ce662b44SJohannes Berg 		/*
1598ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1599ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1600ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1601ce662b44SJohannes Berg 		 *
1602ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1603ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1604ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1605ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1606ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1607ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1608ce662b44SJohannes Berg 		 *
1609ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1610ce662b44SJohannes Berg 		 */
1611ce662b44SJohannes Berg 
1612ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1613f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1614d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1615d7f84244SEmmanuel Grumbach 				break;
1616f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1617ce662b44SJohannes Berg 
16180ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1619f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
162047086fc5SJohannes Berg 		struct sk_buff_head pending;
162147086fc5SJohannes Berg 		struct sk_buff *skb;
162240b96408SJohannes Berg 		int num = 0;
162340b96408SJohannes Berg 		u16 tids = 0;
1624b77cf4f8SJohannes Berg 		bool need_null = false;
162547086fc5SJohannes Berg 
162647086fc5SJohannes Berg 		skb_queue_head_init(&pending);
162747086fc5SJohannes Berg 
162847086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1629af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
163047086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
163140b96408SJohannes Berg 			u8 *qoshdr = NULL;
163240b96408SJohannes Berg 
163340b96408SJohannes Berg 			num++;
1634af818581SJohannes Berg 
1635af818581SJohannes Berg 			/*
163647086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
163747086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
163847086fc5SJohannes Berg 			 * exchange.
1639af818581SJohannes Berg 			 */
16406b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
16416b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1642af818581SJohannes Berg 
164347086fc5SJohannes Berg 			/*
164447086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
164547086fc5SJohannes Berg 			 * more buffered frames for this STA
164647086fc5SJohannes Berg 			 */
164724b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
164847086fc5SJohannes Berg 				hdr->frame_control |=
164947086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
165024b9c373SJanusz.Dziedzic@tieto.com 			else
165124b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
165224b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1653af818581SJohannes Berg 
165440b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
165540b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
165640b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
165740b96408SJohannes Berg 
1658b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1659b77cf4f8SJohannes Berg 
1660b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1661b77cf4f8SJohannes Berg 
1662b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1663b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1664b77cf4f8SJohannes Berg 				continue;
1665b77cf4f8SJohannes Berg 
1666b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1667b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1668b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1669b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1670b77cf4f8SJohannes Berg 				break;
1671b77cf4f8SJohannes Berg 			}
1672b77cf4f8SJohannes Berg 
1673b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1674b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1675b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1676b77cf4f8SJohannes Berg 			 * and be done.
1677b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1678b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1679b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1680b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1681b77cf4f8SJohannes Berg 			 *
1682b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1683b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1684b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1685b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1686b77cf4f8SJohannes Berg 			 *
1687b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1688b77cf4f8SJohannes Berg 			 */
1689b77cf4f8SJohannes Berg 			if (qoshdr) {
169040b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1691deeaee19SJohannes Berg 
169247086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
169347086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1694b77cf4f8SJohannes Berg 			} else {
1695b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1696b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1697b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1698b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1699b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1700b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1701b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1702b77cf4f8SJohannes Berg 				 */
1703b77cf4f8SJohannes Berg 				hdr->frame_control |=
1704b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1705b77cf4f8SJohannes Berg 				need_null = true;
1706b77cf4f8SJohannes Berg 				num++;
170752a3f20cSMarco Porsch 			}
1708b77cf4f8SJohannes Berg 			break;
170947086fc5SJohannes Berg 		}
171047086fc5SJohannes Berg 
171140b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
171240b96408SJohannes Berg 					  reason, more_data);
171340b96408SJohannes Berg 
171447086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1715af818581SJohannes Berg 
1716b77cf4f8SJohannes Berg 		if (need_null)
1717b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
17180ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
17190ead2510SEmmanuel Grumbach 				reason, false, false);
1720b77cf4f8SJohannes Berg 
1721c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1722af818581SJohannes Berg 	} else {
1723ba8c3d6fSFelix Fietkau 		int tid;
1724ba8c3d6fSFelix Fietkau 
1725af818581SJohannes Berg 		/*
17264049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
17274049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1728f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1729f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1730f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1731f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1732f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1733f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1734af818581SJohannes Berg 		 */
17354049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
173647086fc5SJohannes Berg 					    n_frames, reason, more_data);
17374049e09aSJohannes Berg 
17384049e09aSJohannes Berg 		/*
17394049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
17404049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1741f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
17424049e09aSJohannes Berg 		 * release function.
1743f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1744ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1745ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
17464049e09aSJohannes Berg 		 */
1747ba8c3d6fSFelix Fietkau 
1748ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1749ba8c3d6fSFelix Fietkau 			return;
1750ba8c3d6fSFelix Fietkau 
1751ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1752adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1753adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
17541e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1755ba8c3d6fSFelix Fietkau 				continue;
1756ba8c3d6fSFelix Fietkau 
1757ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1758ba8c3d6fSFelix Fietkau 			break;
1759ba8c3d6fSFelix Fietkau 		}
1760af818581SJohannes Berg 	}
1761af818581SJohannes Berg }
1762af818581SJohannes Berg 
1763af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1764af818581SJohannes Berg {
176547086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1766af818581SJohannes Berg 
1767af818581SJohannes Berg 	/*
176847086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
176947086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
177047086fc5SJohannes Berg 	 * only from the non-enabled ones.
1771af818581SJohannes Berg 	 */
177247086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
177347086fc5SJohannes Berg 		ignore_for_response = 0;
1774af818581SJohannes Berg 
177547086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
177647086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1777af818581SJohannes Berg }
177847086fc5SJohannes Berg 
177947086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
178047086fc5SJohannes Berg {
178147086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
178247086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
178347086fc5SJohannes Berg 
178447086fc5SJohannes Berg 	/*
178547086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
178647086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
178747086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
178847086fc5SJohannes Berg 	 * actually getting called.
178947086fc5SJohannes Berg 	 */
179047086fc5SJohannes Berg 	if (!delivery_enabled)
179147086fc5SJohannes Berg 		return;
179247086fc5SJohannes Berg 
179347086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
179447086fc5SJohannes Berg 	case 1:
179547086fc5SJohannes Berg 		n_frames = 2;
179647086fc5SJohannes Berg 		break;
179747086fc5SJohannes Berg 	case 2:
179847086fc5SJohannes Berg 		n_frames = 4;
179947086fc5SJohannes Berg 		break;
180047086fc5SJohannes Berg 	case 3:
180147086fc5SJohannes Berg 		n_frames = 6;
180247086fc5SJohannes Berg 		break;
180347086fc5SJohannes Berg 	case 0:
180447086fc5SJohannes Berg 		/* XXX: what is a good value? */
180513a8098aSAndrei Otcheretianski 		n_frames = 128;
180647086fc5SJohannes Berg 		break;
180747086fc5SJohannes Berg 	}
180847086fc5SJohannes Berg 
180947086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
181047086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1811af818581SJohannes Berg }
1812af818581SJohannes Berg 
1813af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1814af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1815af818581SJohannes Berg {
1816af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1817af818581SJohannes Berg 
1818b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1819b5878a2dSJohannes Berg 
18205ac2e350SJohannes Berg 	if (block) {
1821c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
182217c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
18235ac2e350SJohannes Berg 		return;
18245ac2e350SJohannes Berg 	}
18255ac2e350SJohannes Berg 
18265ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
18275ac2e350SJohannes Berg 		return;
18285ac2e350SJohannes Berg 
18295ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
18305ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
18315ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18325ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18335ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
18345ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
18355ac2e350SJohannes Berg 		/* must be asleep in this case */
18365ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18375ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18385ac2e350SJohannes Berg 	} else {
18395ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
184017c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
18415ac2e350SJohannes Berg 	}
1842af818581SJohannes Berg }
1843af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1844dcf55fb5SFelix Fietkau 
1845e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
184637fbd908SJohannes Berg {
184737fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
184837fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
184937fbd908SJohannes Berg 
185037fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
185137fbd908SJohannes Berg 
185237fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
185337fbd908SJohannes Berg }
1854e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
185537fbd908SJohannes Berg 
18560ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
18570ead2510SEmmanuel Grumbach {
18580ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
18590ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
18600ead2510SEmmanuel Grumbach 	bool more_data;
18610ead2510SEmmanuel Grumbach 
18620ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
18630ead2510SEmmanuel Grumbach 
18640ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
18650ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
18660ead2510SEmmanuel Grumbach 					       reason, 0);
18670ead2510SEmmanuel Grumbach 
18680ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
18690ead2510SEmmanuel Grumbach }
18700ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
18710ead2510SEmmanuel Grumbach 
1872042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1873042ec453SJohannes Berg 				u8 tid, bool buffered)
1874dcf55fb5SFelix Fietkau {
1875dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1876dcf55fb5SFelix Fietkau 
18775a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1878042ec453SJohannes Berg 		return;
1879042ec453SJohannes Berg 
18801b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
18811b000789SJohannes Berg 
1882948d887dSJohannes Berg 	if (buffered)
1883948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1884948d887dSJohannes Berg 	else
1885948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1886948d887dSJohannes Berg 
1887c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1888dcf55fb5SFelix Fietkau }
1889042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1890d9a7ddb0SJohannes Berg 
1891*2433647bSToke Høiland-Jørgensen void ieee80211_register_airtime(struct ieee80211_txq *txq,
1892*2433647bSToke Høiland-Jørgensen 				u32 tx_airtime, u32 rx_airtime)
1893*2433647bSToke Høiland-Jørgensen {
1894*2433647bSToke Høiland-Jørgensen 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
1895*2433647bSToke Høiland-Jørgensen 	struct ieee80211_local *local = sdata->local;
1896*2433647bSToke Høiland-Jørgensen 	u64 weight_sum, weight_sum_reciprocal;
1897*2433647bSToke Høiland-Jørgensen 	struct airtime_sched_info *air_sched;
1898*2433647bSToke Høiland-Jørgensen 	struct airtime_info *air_info;
1899*2433647bSToke Høiland-Jørgensen 	u32 airtime = 0;
1900*2433647bSToke Høiland-Jørgensen 
1901*2433647bSToke Høiland-Jørgensen 	air_sched = &local->airtime[txq->ac];
1902*2433647bSToke Høiland-Jørgensen 	air_info = to_airtime_info(txq);
1903*2433647bSToke Høiland-Jørgensen 
1904*2433647bSToke Høiland-Jørgensen 	if (local->airtime_flags & AIRTIME_USE_TX)
1905*2433647bSToke Høiland-Jørgensen 		airtime += tx_airtime;
1906*2433647bSToke Høiland-Jørgensen 	if (local->airtime_flags & AIRTIME_USE_RX)
1907*2433647bSToke Høiland-Jørgensen 		airtime += rx_airtime;
1908*2433647bSToke Høiland-Jørgensen 
1909*2433647bSToke Høiland-Jørgensen 	/* Weights scale so the unit weight is 256 */
1910*2433647bSToke Høiland-Jørgensen 	airtime <<= 8;
1911*2433647bSToke Høiland-Jørgensen 
1912*2433647bSToke Høiland-Jørgensen 	spin_lock_bh(&air_sched->lock);
1913*2433647bSToke Høiland-Jørgensen 
1914*2433647bSToke Høiland-Jørgensen 	air_info->tx_airtime += tx_airtime;
1915*2433647bSToke Høiland-Jørgensen 	air_info->rx_airtime += rx_airtime;
1916*2433647bSToke Høiland-Jørgensen 
1917*2433647bSToke Høiland-Jørgensen 	if (air_sched->weight_sum) {
1918*2433647bSToke Høiland-Jørgensen 		weight_sum = air_sched->weight_sum;
1919*2433647bSToke Høiland-Jørgensen 		weight_sum_reciprocal = air_sched->weight_sum_reciprocal;
1920*2433647bSToke Høiland-Jørgensen 	} else {
1921*2433647bSToke Høiland-Jørgensen 		weight_sum = air_info->weight;
1922*2433647bSToke Høiland-Jørgensen 		weight_sum_reciprocal = air_info->weight_reciprocal;
1923*2433647bSToke Høiland-Jørgensen 	}
1924*2433647bSToke Høiland-Jørgensen 
1925*2433647bSToke Høiland-Jørgensen 	/* Round the calculation of global vt */
1926*2433647bSToke Høiland-Jørgensen 	air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) *
1927*2433647bSToke Høiland-Jørgensen 				weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64;
1928*2433647bSToke Høiland-Jørgensen 	air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) *
1929*2433647bSToke Høiland-Jørgensen 			       air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32;
1930*2433647bSToke Høiland-Jørgensen 	ieee80211_resort_txq(&local->hw, txq);
1931*2433647bSToke Høiland-Jørgensen 
1932*2433647bSToke Høiland-Jørgensen 	spin_unlock_bh(&air_sched->lock);
1933*2433647bSToke Høiland-Jørgensen }
1934*2433647bSToke Høiland-Jørgensen 
1935b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
1936b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
1937b4809e94SToke Høiland-Jørgensen {
1938*2433647bSToke Høiland-Jørgensen 	struct ieee80211_txq *txq = pubsta->txq[tid];
1939b4809e94SToke Høiland-Jørgensen 
1940*2433647bSToke Høiland-Jørgensen 	if (!txq)
1941*2433647bSToke Høiland-Jørgensen 		return;
1942b4809e94SToke Høiland-Jørgensen 
1943*2433647bSToke Høiland-Jørgensen 	ieee80211_register_airtime(txq, tx_airtime, rx_airtime);
1944b4809e94SToke Høiland-Jørgensen }
1945b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
1946b4809e94SToke Høiland-Jørgensen 
19473ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
19483ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
19493ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
19503ace10f5SKan Yan {
19513ace10f5SKan Yan 	int tx_pending;
19523ace10f5SKan Yan 
1953911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
1954911bde0fSToke Høiland-Jørgensen 		return;
1955911bde0fSToke Høiland-Jørgensen 
19563ace10f5SKan Yan 	if (!tx_completed) {
19573ace10f5SKan Yan 		if (sta)
19583ace10f5SKan Yan 			atomic_add(tx_airtime,
19593ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
19603ace10f5SKan Yan 
19613ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
19623ace10f5SKan Yan 		return;
19633ace10f5SKan Yan 	}
19643ace10f5SKan Yan 
19653ace10f5SKan Yan 	if (sta) {
19663ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
19673ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
196804e35caaSFelix Fietkau 		if (tx_pending < 0)
19693ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
19703ace10f5SKan Yan 				       tx_pending, 0);
19713ace10f5SKan Yan 	}
19723ace10f5SKan Yan 
19733ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
19743ace10f5SKan Yan 				       &local->aql_total_pending_airtime);
19753ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
19763ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
19773ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
19783ace10f5SKan Yan 		      tx_airtime))
19793ace10f5SKan Yan 		atomic_cmpxchg(&local->aql_total_pending_airtime,
19803ace10f5SKan Yan 			       tx_pending, 0);
19813ace10f5SKan Yan }
19823ace10f5SKan Yan 
198383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1984d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1985d9a7ddb0SJohannes Berg {
19868bf11d8dSJohannes Berg 	might_sleep();
1987d9a7ddb0SJohannes Berg 
1988d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1989d9a7ddb0SJohannes Berg 		return 0;
1990d9a7ddb0SJohannes Berg 
1991f09603a2SJohannes Berg 	/* check allowed transitions first */
1992f09603a2SJohannes Berg 
1993d9a7ddb0SJohannes Berg 	switch (new_state) {
1994d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1995f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1996d9a7ddb0SJohannes Berg 			return -EINVAL;
1997d9a7ddb0SJohannes Berg 		break;
1998d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1999f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2000f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2001d9a7ddb0SJohannes Berg 			return -EINVAL;
2002d9a7ddb0SJohannes Berg 		break;
2003d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2004f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2005f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2006d9a7ddb0SJohannes Berg 			return -EINVAL;
2007d9a7ddb0SJohannes Berg 		break;
2008d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2009f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2010d9a7ddb0SJohannes Berg 			return -EINVAL;
2011d9a7ddb0SJohannes Berg 		break;
2012d9a7ddb0SJohannes Berg 	default:
2013d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2014d9a7ddb0SJohannes Berg 		return -EINVAL;
2015d9a7ddb0SJohannes Berg 	}
2016d9a7ddb0SJohannes Berg 
2017bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2018bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2019f09603a2SJohannes Berg 
2020f09603a2SJohannes Berg 	/*
2021f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2022f09603a2SJohannes Berg 	 * fail the transition
2023f09603a2SJohannes Berg 	 */
2024f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2025f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2026f09603a2SJohannes Berg 					sta->sta_state, new_state);
2027f09603a2SJohannes Berg 		if (err)
2028f09603a2SJohannes Berg 			return err;
2029f09603a2SJohannes Berg 	}
2030f09603a2SJohannes Berg 
2031f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2032f09603a2SJohannes Berg 
2033f09603a2SJohannes Berg 	switch (new_state) {
2034f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2035f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2036f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2037f09603a2SJohannes Berg 		break;
2038f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2039a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2040f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2041a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2042f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2043a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
204452cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
204552cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2046a7201a6cSIlan Peer 		}
2047f09603a2SJohannes Berg 		break;
2048f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2049f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2050f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
20519cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
2052a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
205352cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
205452cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2055f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
205672f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2057f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
205817c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
205949ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2060f09603a2SJohannes Berg 		}
2061f09603a2SJohannes Berg 		break;
2062f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2063f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
206472f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2065f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
206617c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
206749ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2068f09603a2SJohannes Berg 		}
20693e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
20703e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
20713e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
20723e493173SJouni Malinen 						    sta->sta.addr);
2073f09603a2SJohannes Berg 		break;
2074f09603a2SJohannes Berg 	default:
2075f09603a2SJohannes Berg 		break;
2076f09603a2SJohannes Berg 	}
2077f09603a2SJohannes Berg 
2078d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2079d9a7ddb0SJohannes Berg 
2080d9a7ddb0SJohannes Berg 	return 0;
2081d9a7ddb0SJohannes Berg }
2082687da132SEmmanuel Grumbach 
2083687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
2084687da132SEmmanuel Grumbach {
2085687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
2086687da132SEmmanuel Grumbach 	u8 rx_streams;
2087687da132SEmmanuel Grumbach 
2088687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
2089687da132SEmmanuel Grumbach 		return 1;
2090687da132SEmmanuel Grumbach 
2091687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
2092687da132SEmmanuel Grumbach 		int i;
2093687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
2094687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
2095687da132SEmmanuel Grumbach 
2096687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
2097687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
2098687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
2099687da132SEmmanuel Grumbach 				return i + 1;
2100687da132SEmmanuel Grumbach 	}
2101687da132SEmmanuel Grumbach 
2102687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
2103687da132SEmmanuel Grumbach 		rx_streams = 4;
2104687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
2105687da132SEmmanuel Grumbach 		rx_streams = 3;
2106687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
2107687da132SEmmanuel Grumbach 		rx_streams = 2;
2108687da132SEmmanuel Grumbach 	else
2109687da132SEmmanuel Grumbach 		rx_streams = 1;
2110687da132SEmmanuel Grumbach 
2111687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
2112687da132SEmmanuel Grumbach 		return rx_streams;
2113687da132SEmmanuel Grumbach 
2114687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
2115687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
2116687da132SEmmanuel Grumbach }
2117b7ffbd7eSJohannes Berg 
2118c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2119c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2120c9c5962bSJohannes Berg {
2121c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
2122c9c5962bSJohannes Berg 	int cpu;
2123c9c5962bSJohannes Berg 
2124d656a4c6SJohannes Berg 	if (!sta->pcpu_rx_stats)
2125c9c5962bSJohannes Berg 		return stats;
2126c9c5962bSJohannes Berg 
2127c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2128c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2129c9c5962bSJohannes Berg 
2130c9c5962bSJohannes Berg 		cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2131c9c5962bSJohannes Berg 
2132c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2133c9c5962bSJohannes Berg 			stats = cpustats;
2134c9c5962bSJohannes Berg 	}
2135c9c5962bSJohannes Berg 
2136c9c5962bSJohannes Berg 	return stats;
2137c9c5962bSJohannes Berg }
2138c9c5962bSJohannes Berg 
213941cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
21404f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2141fbd6ff5cSJohannes Berg {
2142dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2143fbd6ff5cSJohannes Berg 
2144dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
21457f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
21464f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2147dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2148dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2149dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2150dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
21517f406cd1SJohannes Berg 		break;
21527f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
21534f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2154dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2155dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2156dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
21577f406cd1SJohannes Berg 		break;
21587f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2159fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2160fbd6ff5cSJohannes Berg 		u16 brate;
21614f6b1b3dSJohannes Berg 		unsigned int shift;
2162dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2163dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2164fbd6ff5cSJohannes Berg 
2165dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
21668b783d10SThomas Pedersen 
21678b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
21688b783d10SThomas Pedersen 			break;
21698b783d10SThomas Pedersen 
2170dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
21714f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
21724f6b1b3dSJohannes Berg 			shift = 2;
21734f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
21744f6b1b3dSJohannes Berg 			shift = 1;
21754f6b1b3dSJohannes Berg 		else
21764f6b1b3dSJohannes Berg 			shift = 0;
2177fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
21787f406cd1SJohannes Berg 		break;
21797f406cd1SJohannes Berg 		}
218041cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
218141cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
218241cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
218341cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
218441cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
218541cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
218641cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
218741cbb0f5SLuca Coelho 		break;
2188fbd6ff5cSJohannes Berg 	}
21894f6b1b3dSJohannes Berg }
2190fbd6ff5cSJohannes Berg 
2191a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
21924f6b1b3dSJohannes Berg {
21936aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
21944f6b1b3dSJohannes Berg 
21954f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2196a17d93ffSBen Greear 		return -EINVAL;
2197a17d93ffSBen Greear 
21984f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2199a17d93ffSBen Greear 	return 0;
2200fbd6ff5cSJohannes Berg }
2201fbd6ff5cSJohannes Berg 
2202b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2203b255b72bSSeevalamuthu Mariappan 					int tid)
2204b255b72bSSeevalamuthu Mariappan {
2205b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2206b255b72bSSeevalamuthu Mariappan 	u64 value;
2207b255b72bSSeevalamuthu Mariappan 
2208b255b72bSSeevalamuthu Mariappan 	do {
2209b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2210b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2211b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2212b255b72bSSeevalamuthu Mariappan 
2213b255b72bSSeevalamuthu Mariappan 	return value;
2214b255b72bSSeevalamuthu Mariappan }
2215b255b72bSSeevalamuthu Mariappan 
22160f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
22170f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
22180f9c5a61SJohannes Berg 			     int tid)
22190f9c5a61SJohannes Berg {
22200f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2221b255b72bSSeevalamuthu Mariappan 	int cpu;
22220f9c5a61SJohannes Berg 
22230f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2224d656a4c6SJohannes Berg 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->rx_stats, tid);
22250f9c5a61SJohannes Berg 
2226b255b72bSSeevalamuthu Mariappan 		if (sta->pcpu_rx_stats) {
2227b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2228b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2229b255b72bSSeevalamuthu Mariappan 
2230b255b72bSSeevalamuthu Mariappan 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2231b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2232b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2233b255b72bSSeevalamuthu Mariappan 			}
2234b255b72bSSeevalamuthu Mariappan 		}
22350f9c5a61SJohannes Berg 
22360f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
22370f9c5a61SJohannes Berg 	}
22380f9c5a61SJohannes Berg 
22390f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
22400f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
22410f9c5a61SJohannes Berg 		tidstats->tx_msdu = sta->tx_stats.msdu[tid];
22420f9c5a61SJohannes Berg 	}
22430f9c5a61SJohannes Berg 
22440f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
22450f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22460f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
22470f9c5a61SJohannes Berg 		tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
22480f9c5a61SJohannes Berg 	}
22490f9c5a61SJohannes Berg 
22500f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
22510f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22520f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
22530f9c5a61SJohannes Berg 		tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
22540f9c5a61SJohannes Berg 	}
22552fe4a29aSToke Høiland-Jørgensen 
22562fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
22572fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
22582fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
22592fe4a29aSToke Høiland-Jørgensen 
22602fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
22612fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
22622fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
22632fe4a29aSToke Høiland-Jørgensen 
22642fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
22652fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
22662fe4a29aSToke Høiland-Jørgensen 	}
22670f9c5a61SJohannes Berg }
22680f9c5a61SJohannes Berg 
2269c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2270c9c5962bSJohannes Berg {
2271c9c5962bSJohannes Berg 	unsigned int start;
2272c9c5962bSJohannes Berg 	u64 value;
2273c9c5962bSJohannes Berg 
2274c9c5962bSJohannes Berg 	do {
2275c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2276c9c5962bSJohannes Berg 		value = rxstats->bytes;
2277c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2278c9c5962bSJohannes Berg 
2279c9c5962bSJohannes Berg 	return value;
2280c9c5962bSJohannes Berg }
2281c9c5962bSJohannes Berg 
22820fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
22830fdf1493SJohannes Berg 		   bool tidstats)
2284b7ffbd7eSJohannes Berg {
2285b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2286b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2287b7ffbd7eSJohannes Berg 	u32 thr = 0;
2288c9c5962bSJohannes Berg 	int i, ac, cpu;
2289c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2290c9c5962bSJohannes Berg 
2291c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2292b7ffbd7eSJohannes Berg 
2293b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2294b7ffbd7eSJohannes Berg 
2295225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2296225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2297225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2298225b8189SJohannes Berg 	 */
2299225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2300225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2301225b8189SJohannes Berg 
23022b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2303a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2304a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2305a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2306a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
23079cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2308a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2309976bd9efSJohannes Berg 
2310976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2311976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2312a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2313976bd9efSJohannes Berg 	}
2314b7ffbd7eSJohannes Berg 
231584b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
23169cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2317e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2318b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
23192b9a7e1bSJohannes Berg 
2320a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2321a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2322b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
23232b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2324e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2325a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2326b7ffbd7eSJohannes Berg 	}
23272b9a7e1bSJohannes Berg 
2328a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
23292b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
23302b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2331e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
2332a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
23332b9a7e1bSJohannes Berg 	}
23342b9a7e1bSJohannes Berg 
2335a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2336a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2337c9c5962bSJohannes Berg 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
23380f9c5a61SJohannes Berg 
2339c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2340c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2341c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2342c9c5962bSJohannes Berg 
2343c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2344c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2345c9c5962bSJohannes Berg 			}
2346c9c5962bSJohannes Berg 		}
2347c9c5962bSJohannes Berg 
2348a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
23492b9a7e1bSJohannes Berg 	}
23502b9a7e1bSJohannes Berg 
2351a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2352e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
2353c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2354c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2355c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2356c9c5962bSJohannes Berg 
2357c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2358c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2359c9c5962bSJohannes Berg 			}
2360c9c5962bSJohannes Berg 		}
2361a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
23622b9a7e1bSJohannes Berg 	}
23632b9a7e1bSJohannes Berg 
2364a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2365e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
2366a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
23672b9a7e1bSJohannes Berg 	}
23682b9a7e1bSJohannes Berg 
2369a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2370e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
2371a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
23722b9a7e1bSJohannes Berg 	}
23732b9a7e1bSJohannes Berg 
2374b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2375b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2376b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2377b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2378b4809e94SToke Høiland-Jørgensen 	}
2379b4809e94SToke Høiland-Jørgensen 
2380b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2381b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2382b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2383b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2384b4809e94SToke Høiland-Jørgensen 	}
2385b4809e94SToke Høiland-Jørgensen 
2386b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2387*2433647bSToke Høiland-Jørgensen 		sinfo->airtime_weight = sta->airtime[0].weight;
2388b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2389b4809e94SToke Høiland-Jørgensen 	}
2390b4809e94SToke Høiland-Jørgensen 
2391e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2392c9c5962bSJohannes Berg 	if (sta->pcpu_rx_stats) {
2393c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2394c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2395c9c5962bSJohannes Berg 
2396c9c5962bSJohannes Berg 			cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2397e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2398c9c5962bSJohannes Berg 		}
2399c9c5962bSJohannes Berg 	}
2400b7ffbd7eSJohannes Berg 
2401225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2402225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2403a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2404a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2405225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2406225b8189SJohannes Berg 	}
2407225b8189SJohannes Berg 
240830686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
240930686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2410a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2411c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2412a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2413b7ffbd7eSJohannes Berg 		}
24142b9a7e1bSJohannes Berg 
2415c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats &&
2416a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
241740d9a38aSJohannes Berg 			sinfo->signal_avg =
24180be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.signal);
2419a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
24202b9a7e1bSJohannes Berg 		}
24212b9a7e1bSJohannes Berg 	}
24222b9a7e1bSJohannes Berg 
2423c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2424c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2425c9c5962bSJohannes Berg 	 * pcpu statistics
2426c9c5962bSJohannes Berg 	 */
2427c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2428a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2429a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2430a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2431c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
2432a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2433b7ffbd7eSJohannes Berg 
2434c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2435c9c5962bSJohannes Berg 
2436b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2437e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2438c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2439b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
24400be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2441b7ffbd7eSJohannes Berg 		}
2442b7ffbd7eSJohannes Berg 	}
2443b7ffbd7eSJohannes Berg 
2444a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2445e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2446e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2447a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
24482b9a7e1bSJohannes Berg 	}
24492b9a7e1bSJohannes Berg 
2450a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2451a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2452a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
24532b9a7e1bSJohannes Berg 	}
2454b7ffbd7eSJohannes Berg 
24550fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
24566af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
24576af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
24588689c051SArend van Spriel 	}
245979c892b8SJohannes Berg 
2460b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2461b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2462a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2463a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2464a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2465a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2466a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2467dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
24681303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
24691303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2470b7ffbd7eSJohannes Berg 
2471433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2472433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2473433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2474b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2475a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2476433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2477b7ffbd7eSJohannes Berg 		}
2478433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2479433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2480433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2481dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
24821303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2483b7ffbd7eSJohannes Berg #endif
2484b7ffbd7eSJohannes Berg 	}
2485b7ffbd7eSJohannes Berg 
2486b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2487b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2488b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2489b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2490b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2491b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2492b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2493785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2494b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2495b7ffbd7eSJohannes Berg 
2496b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2497b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2498b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2499b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2500b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2501b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2502b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2503b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2504b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2505b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2506b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2507b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2508a74a8c84SJohannes Berg 	if (sta->sta.wme)
2509b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2510b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2511b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2512b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2513b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2514b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2515b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2516b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2517b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2518b7ffbd7eSJohannes Berg 
25193b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
25203b17fbf8SMaxim Altshul 
25213b17fbf8SMaxim Altshul 	if (thr != 0) {
2522a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
25233b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
25243b17fbf8SMaxim Altshul 	}
2525a78b26ffSVenkateswara Naralasetty 
2526a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2527a78b26ffSVenkateswara Naralasetty 	    sta->status_stats.ack_signal_filled) {
2528a78b26ffSVenkateswara Naralasetty 		sinfo->ack_signal = sta->status_stats.last_ack_signal;
2529a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2530a78b26ffSVenkateswara Naralasetty 	}
2531cc60dbbfSBalaji Pothunoori 
25329c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
25339c06602bSBalaji Pothunoori 	    sta->status_stats.ack_signal_filled) {
2534cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2535cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2536cc60dbbfSBalaji Pothunoori 				&sta->status_stats.avg_ack_signal);
2537cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
25389c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2539cc60dbbfSBalaji Pothunoori 	}
2540ab60633cSNarayanraddi Masti 
2541ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2542ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2543ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2544ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2545ab60633cSNarayanraddi Masti 	}
25463b17fbf8SMaxim Altshul }
25473b17fbf8SMaxim Altshul 
25483b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
25493b17fbf8SMaxim Altshul {
25503b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
25513b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
25523b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
25533b17fbf8SMaxim Altshul 	u32 thr = 0;
25543b17fbf8SMaxim Altshul 
25553b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
25563b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
25573b17fbf8SMaxim Altshul 
2558b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2559b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2560b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2561b7ffbd7eSJohannes Berg 	else
25624fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2563b7ffbd7eSJohannes Berg 
25643b17fbf8SMaxim Altshul 	return thr;
2565b7ffbd7eSJohannes Berg }
2566b8da6b6aSJohannes Berg 
2567b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2568b8da6b6aSJohannes Berg {
2569c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2570c9c5962bSJohannes Berg 
2571285531f9SAhmed Zaki 	if (!sta->status_stats.last_ack ||
2572285531f9SAhmed Zaki 	    time_after(stats->last_rx, sta->status_stats.last_ack))
2573c9c5962bSJohannes Berg 		return stats->last_rx;
2574b8da6b6aSJohannes Berg 	return sta->status_stats.last_ack;
2575b8da6b6aSJohannes Berg }
2576484a54c2SToke Høiland-Jørgensen 
2577484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2578484a54c2SToke Høiland-Jørgensen {
2579484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2580484a54c2SToke Høiland-Jørgensen 		return;
2581484a54c2SToke Høiland-Jørgensen 
2582484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2583484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2584484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2585484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2586484a54c2SToke Høiland-Jørgensen 	} else {
2587484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2588484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2589484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2590484a54c2SToke Høiland-Jørgensen 	}
2591484a54c2SToke Høiland-Jørgensen }
2592484a54c2SToke Høiland-Jørgensen 
2593484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2594484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2595484a54c2SToke Høiland-Jørgensen {
2596484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2597484a54c2SToke Høiland-Jørgensen 
2598484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2599484a54c2SToke Høiland-Jørgensen }
2600