xref: /openbmc/linux/net/mac80211/sta_info.c (revision cb71f1d136a635decf43c3b502ee34fb05640fcd)
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 
67*cb71f1d1SJohannes Berg struct sta_link_alloc {
68*cb71f1d1SJohannes Berg 	struct link_sta_info info;
69*cb71f1d1SJohannes Berg 	struct ieee80211_link_sta sta;
70*cb71f1d1SJohannes Berg };
71*cb71f1d1SJohannes Berg 
727bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
737bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
74caf22d31SJohannes Berg 	.automatic_shrinking = true,
757bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
76ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
777bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
78ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
797bedd0cfSJohannes Berg };
807bedd0cfSJohannes Berg 
814d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
82be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
83f0706e82SJiri Benc 			     struct sta_info *sta)
84f0706e82SJiri Benc {
8583e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
867bedd0cfSJohannes Berg 			       sta_rht_params);
87f0706e82SJiri Benc }
88f0706e82SJiri Benc 
895108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
90b22cfcfcSEliad Peller {
91b22cfcfcSEliad Peller 	int ac, i;
92b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
93b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
94b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
95d012a605SMarco Porsch 	struct ps_data *ps;
96b22cfcfcSEliad Peller 
97e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
985ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
995ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
100d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
101d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
102d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1033f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1043f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
105d012a605SMarco Porsch 		else
106d012a605SMarco Porsch 			return;
107b22cfcfcSEliad Peller 
108b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
109e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1105ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
111b22cfcfcSEliad Peller 
112d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
113b22cfcfcSEliad Peller 	}
114b22cfcfcSEliad Peller 
115ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
116ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
117adf8ed01SJohannes Berg 			struct txq_info *txqi;
118adf8ed01SJohannes Berg 
119adf8ed01SJohannes Berg 			if (!sta->sta.txq[i])
120adf8ed01SJohannes Berg 				continue;
121adf8ed01SJohannes Berg 
122adf8ed01SJohannes Berg 			txqi = to_txq_info(sta->sta.txq[i]);
123ba8c3d6fSFelix Fietkau 
124fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
125ba8c3d6fSFelix Fietkau 		}
126ba8c3d6fSFelix Fietkau 	}
127ba8c3d6fSFelix Fietkau 
128b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
129b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1301f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1311f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
132b22cfcfcSEliad Peller 	}
133b22cfcfcSEliad Peller 
13445b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
13545b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
136b22cfcfcSEliad Peller 
1375ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
138b22cfcfcSEliad Peller 
139b22cfcfcSEliad Peller 	/*
140b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
141b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
142b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
143b22cfcfcSEliad Peller 	 * directly by station destruction.
144b22cfcfcSEliad Peller 	 */
1455a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
146661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
147b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
148b22cfcfcSEliad Peller 		if (!tid_tx)
149b22cfcfcSEliad Peller 			continue;
1501f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
151b22cfcfcSEliad Peller 		kfree(tid_tx);
152b22cfcfcSEliad Peller 	}
1535108ca82SJohannes Berg }
154b22cfcfcSEliad Peller 
1555108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1565108ca82SJohannes Berg {
1575108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1585108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1595108ca82SJohannes Berg 
1605108ca82SJohannes Berg 	__cleanup_single_sta(sta);
161b22cfcfcSEliad Peller 	sta_info_free(local, sta);
162b22cfcfcSEliad Peller }
163b22cfcfcSEliad Peller 
16483e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
16583e7e4ceSHerbert Xu 					 const u8 *addr)
16683e7e4ceSHerbert Xu {
16783e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
16883e7e4ceSHerbert Xu }
16983e7e4ceSHerbert Xu 
170d0709a65SJohannes Berg /* protected by RCU */
171abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
172abe60632SJohannes Berg 			      const u8 *addr)
17343ba7e95SJohannes Berg {
174abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
17583e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
17660f4b626SJohannes Berg 	struct sta_info *sta;
17743ba7e95SJohannes Berg 
17860f4b626SJohannes Berg 	rcu_read_lock();
17983e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
18060f4b626SJohannes Berg 		if (sta->sdata == sdata) {
18160f4b626SJohannes Berg 			rcu_read_unlock();
18260f4b626SJohannes Berg 			/* this is safe as the caller must already hold
18360f4b626SJohannes Berg 			 * another rcu read section or the mutex
18460f4b626SJohannes Berg 			 */
18560f4b626SJohannes Berg 			return sta;
18660f4b626SJohannes Berg 		}
18760f4b626SJohannes Berg 	}
18860f4b626SJohannes Berg 	rcu_read_unlock();
18960f4b626SJohannes Berg 	return NULL;
19043ba7e95SJohannes Berg }
19143ba7e95SJohannes Berg 
1920e5ded5aSFelix Fietkau /*
1930e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1940e5ded5aSFelix Fietkau  * or from one of its vlans
1950e5ded5aSFelix Fietkau  */
1960e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1970e5ded5aSFelix Fietkau 				  const u8 *addr)
1980e5ded5aSFelix Fietkau {
1990e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
20083e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
2010e5ded5aSFelix Fietkau 	struct sta_info *sta;
2020e5ded5aSFelix Fietkau 
2037bedd0cfSJohannes Berg 	rcu_read_lock();
20483e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2057bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2067bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2077bedd0cfSJohannes Berg 			rcu_read_unlock();
2087bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2097bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2107bedd0cfSJohannes Berg 			 */
2110e5ded5aSFelix Fietkau 			return sta;
2120e5ded5aSFelix Fietkau 		}
2137bedd0cfSJohannes Berg 	}
2147bedd0cfSJohannes Berg 	rcu_read_unlock();
2157bedd0cfSJohannes Berg 	return NULL;
2167bedd0cfSJohannes Berg }
2170e5ded5aSFelix Fietkau 
2185072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
2195072f73cSToke Høiland-Jørgensen 				       const u8 *sta_addr, const u8 *vif_addr)
2205072f73cSToke Høiland-Jørgensen {
2215072f73cSToke Høiland-Jørgensen 	struct rhlist_head *tmp;
2225072f73cSToke Høiland-Jørgensen 	struct sta_info *sta;
2235072f73cSToke Høiland-Jørgensen 
2245072f73cSToke Høiland-Jørgensen 	for_each_sta_info(local, sta_addr, sta, tmp) {
2255072f73cSToke Høiland-Jørgensen 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
2265072f73cSToke Høiland-Jørgensen 			return sta;
2275072f73cSToke Høiland-Jørgensen 	}
2285072f73cSToke Høiland-Jørgensen 
2295072f73cSToke Høiland-Jørgensen 	return NULL;
2305072f73cSToke Høiland-Jørgensen }
2315072f73cSToke Høiland-Jørgensen 
2323b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2333b53fde8SJohannes Berg 				     int idx)
234ee385855SLuis Carlos Cobo {
2353b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
236ee385855SLuis Carlos Cobo 	struct sta_info *sta;
237ee385855SLuis Carlos Cobo 	int i = 0;
238ee385855SLuis Carlos Cobo 
2398ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
2408ca47eb9SMadhuparna Bhowmik 				lockdep_is_held(&local->sta_mtx)) {
2413b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2422a8ca29aSLuis Carlos Cobo 			continue;
243ee385855SLuis Carlos Cobo 		if (i < idx) {
244ee385855SLuis Carlos Cobo 			++i;
245ee385855SLuis Carlos Cobo 			continue;
246ee385855SLuis Carlos Cobo 		}
2472a8ca29aSLuis Carlos Cobo 		return sta;
248ee385855SLuis Carlos Cobo 	}
249ee385855SLuis Carlos Cobo 
250ee385855SLuis Carlos Cobo 	return NULL;
251ee385855SLuis Carlos Cobo }
252f0706e82SJiri Benc 
253*cb71f1d1SJohannes Berg static void sta_info_free_link(struct link_sta_info *link_sta)
254246b39e4SJohannes Berg {
255*cb71f1d1SJohannes Berg 	free_percpu(link_sta->pcpu_rx_stats);
256*cb71f1d1SJohannes Berg }
257246b39e4SJohannes Berg 
258*cb71f1d1SJohannes Berg static void sta_remove_link(struct sta_info *sta, unsigned int link_id)
259*cb71f1d1SJohannes Berg {
260*cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc = NULL;
261*cb71f1d1SJohannes Berg 
262*cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->link[link_id]))
263*cb71f1d1SJohannes Berg 		return;
264246b39e4SJohannes Berg 
265246b39e4SJohannes Berg 	if (sta->link[link_id] != &sta->deflink)
266*cb71f1d1SJohannes Berg 		alloc = container_of(sta->link[link_id], typeof(*alloc), info);
267*cb71f1d1SJohannes Berg 
268*cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
269*cb71f1d1SJohannes Berg 	sta->link[link_id] = NULL;
270*cb71f1d1SJohannes Berg 	sta->sta.link[link_id] = NULL;
271*cb71f1d1SJohannes Berg 	if (alloc) {
272*cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
273*cb71f1d1SJohannes Berg 		kfree(alloc);
274246b39e4SJohannes Berg 	}
275246b39e4SJohannes Berg }
276246b39e4SJohannes Berg 
27793e5deb1SJohannes Berg /**
278d9a7ddb0SJohannes Berg  * sta_info_free - free STA
27993e5deb1SJohannes Berg  *
2806ef307bcSRandy Dunlap  * @local: pointer to the global information
28193e5deb1SJohannes Berg  * @sta: STA info to free
28293e5deb1SJohannes Berg  *
28393e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
284d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
285d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
286d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
28793e5deb1SJohannes Berg  */
288d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
28993e5deb1SJohannes Berg {
290*cb71f1d1SJohannes Berg 	int i;
291*cb71f1d1SJohannes Berg 
292*cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
293*cb71f1d1SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
294*cb71f1d1SJohannes Berg 			continue;
295*cb71f1d1SJohannes Berg 
296*cb71f1d1SJohannes Berg 		sta_remove_link(sta, i);
297*cb71f1d1SJohannes Berg 	}
298*cb71f1d1SJohannes Berg 
299dcd479e1SJohannes Berg 	/*
300dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
301dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
302dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
303dcd479e1SJohannes Berg 	 *
304dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
305dcd479e1SJohannes Berg 	 * allocated during state transitions.
306dcd479e1SJohannes Berg 	 */
307dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
308dcd479e1SJohannes Berg 		int ret;
309dcd479e1SJohannes Berg 
310dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
311dcd479e1SJohannes Berg 
312dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
313dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
314dcd479e1SJohannes Berg 			break;
315dcd479e1SJohannes Berg 	}
316dcd479e1SJohannes Berg 
317889cbb91SJohannes Berg 	if (sta->rate_ctrl)
3184b7679a5SJohannes Berg 		rate_control_free_sta(sta);
31993e5deb1SJohannes Berg 
320bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
32193e5deb1SJohannes Berg 
322ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
323ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
32453d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
325433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
326433f5bc1SJohannes Berg 	kfree(sta->mesh);
327433f5bc1SJohannes Berg #endif
328246b39e4SJohannes Berg 
329*cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
33093e5deb1SJohannes Berg 	kfree(sta);
33193e5deb1SJohannes Berg }
33293e5deb1SJohannes Berg 
3334d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
33462b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
335d0709a65SJohannes Berg 			     struct sta_info *sta)
336f0706e82SJiri Benc {
33783e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
3387bedd0cfSJohannes Berg 			       sta_rht_params);
339f0706e82SJiri Benc }
340f0706e82SJiri Benc 
3415ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
342af818581SJohannes Berg {
343af818581SJohannes Berg 	struct sta_info *sta;
344af818581SJohannes Berg 
3455ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
346af818581SJohannes Berg 
347af818581SJohannes Berg 	if (sta->dead)
348af818581SJohannes Berg 		return;
349af818581SJohannes Berg 
35054420473SHelmut Schaa 	local_bh_disable();
3515ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
352af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
3535ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
354af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
3555ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
35647086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
357ce662b44SJohannes Berg 	local_bh_enable();
358af818581SJohannes Berg }
359af818581SJohannes Berg 
360af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
361af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
362af65cd96SJohannes Berg {
36330686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
364af65cd96SJohannes Berg 		return 0;
365af65cd96SJohannes Berg 
366889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
367af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
36835c347acSJohannes Berg 						     sta, gfp);
369889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
370af65cd96SJohannes Berg 		return -ENOMEM;
371af65cd96SJohannes Berg 
372af65cd96SJohannes Berg 	return 0;
373af65cd96SJohannes Berg }
374af65cd96SJohannes Berg 
375*cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
376246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
377246b39e4SJohannes Berg 			       gfp_t gfp)
378246b39e4SJohannes Berg {
379246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
380246b39e4SJohannes Berg 	int i;
381246b39e4SJohannes Berg 
382246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
383246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
384246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
385246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
386246b39e4SJohannes Berg 			return -ENOMEM;
387246b39e4SJohannes Berg 	}
388246b39e4SJohannes Berg 
389246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
390246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
391246b39e4SJohannes Berg 
392246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
393246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
394246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
395246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
396246b39e4SJohannes Berg 
397246b39e4SJohannes Berg 	return 0;
398246b39e4SJohannes Berg }
399246b39e4SJohannes Berg 
400*cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
401*cb71f1d1SJohannes Berg 			      unsigned int link_id,
402*cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
403*cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
404*cb71f1d1SJohannes Berg {
405*cb71f1d1SJohannes Berg 	link_info->sta = sta;
406*cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
407*cb71f1d1SJohannes Berg 	sta->link[link_id] = link_info;
408*cb71f1d1SJohannes Berg 	sta->sta.link[link_id] = link_sta;
409*cb71f1d1SJohannes Berg }
410*cb71f1d1SJohannes Berg 
41173651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
412*cb71f1d1SJohannes Berg 				const u8 *addr, int link_id, gfp_t gfp)
413f0706e82SJiri Benc {
414d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
415ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
416f0706e82SJiri Benc 	struct sta_info *sta;
41716c5f15cSRon Rindjunsky 	int i;
418f0706e82SJiri Benc 
419ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
420f0706e82SJiri Benc 	if (!sta)
42173651ee6SJohannes Berg 		return NULL;
422f0706e82SJiri Benc 
423246b39e4SJohannes Berg 	sta->local = local;
424246b39e4SJohannes Berg 	sta->sdata = sdata;
425246b39e4SJohannes Berg 
426*cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
427246b39e4SJohannes Berg 		return NULL;
428c9c5962bSJohannes Berg 
429*cb71f1d1SJohannes Berg 	if (link_id >= 0) {
430*cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
431*cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
432*cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
433*cb71f1d1SJohannes Berg 	} else {
434*cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
435*cb71f1d1SJohannes Berg 	}
436*cb71f1d1SJohannes Berg 
43707346f81SJohannes Berg 	spin_lock_init(&sta->lock);
4381d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
4395ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
44067c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
441a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
44287f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
443433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
444433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
445433f5bc1SJohannes Berg 		if (!sta->mesh)
446433f5bc1SJohannes Berg 			goto free;
4474c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
448433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
44945d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
4504c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
4514c02d62fSKees Cook 				    0);
452433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
453433f5bc1SJohannes Berg 	}
45487f59c70SThomas Pedersen #endif
45507346f81SJohannes Berg 
456ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
45717741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
458480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
459480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
460480dd46bSMaxim Altshul 
461046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
462046d2e7cSSriram R 
46396fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
46496fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
46596fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
46696fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
46796fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
46896fc6efbSAlexander Wetzel 	 */
46996fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
47096fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
47196fc6efbSAlexander Wetzel 
4720f9c5a61SJohannes Berg 
4733a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
4743a11ce08SJohannes Berg 
47571ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
47671ec375cSJohannes Berg 
477b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
478b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
479b6da911bSLiad Kaufman 
48084b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
481541a45a1SBruno Randolf 
482ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
483ba8c3d6fSFelix Fietkau 		void *txq_data;
484ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
485ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
486ba8c3d6fSFelix Fietkau 
487ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
488ba8c3d6fSFelix Fietkau 		if (!txq_data)
489ba8c3d6fSFelix Fietkau 			goto free;
490ba8c3d6fSFelix Fietkau 
491ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
492ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
493ba8c3d6fSFelix Fietkau 
494adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
495fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
496abfbc3afSJohannes Berg 		}
497ba8c3d6fSFelix Fietkau 	}
498ba8c3d6fSFelix Fietkau 
499ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
500ba8c3d6fSFelix Fietkau 		goto free_txq;
501f0706e82SJiri Benc 
502b4809e94SToke Høiland-Jørgensen 
503948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
504948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
505948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
5062433647bSToke Høiland-Jørgensen 		init_airtime_info(&sta->airtime[i], &local->airtime[i]);
507948d887dSJohannes Berg 	}
50873651ee6SJohannes Berg 
5095a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
5104be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
511cccaec98SSenthil Balasubramanian 
512bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
513bd718fc1SJohannes Berg 		u32 mandatory = 0;
514bd718fc1SJohannes Berg 		int r;
515bd718fc1SJohannes Berg 
516bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
517bd718fc1SJohannes Berg 			continue;
518bd718fc1SJohannes Berg 
519bd718fc1SJohannes Berg 		switch (i) {
520bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
52163fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
522bd718fc1SJohannes Berg 			/*
523bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
524bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
525bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
526bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
527bd718fc1SJohannes Berg 			 * possible rate anyway.
528bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
529bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
530bd718fc1SJohannes Berg 			 */
531bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
532bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
533bd718fc1SJohannes Berg 			break;
534bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
535bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
536bd718fc1SJohannes Berg 			break;
537bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
538bd718fc1SJohannes Berg 			WARN_ON(1);
539bd718fc1SJohannes Berg 			mandatory = 0;
540bd718fc1SJohannes Berg 			break;
541bd718fc1SJohannes Berg 		}
542bd718fc1SJohannes Berg 
543bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
544bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
545bd718fc1SJohannes Berg 
546bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
547bd718fc1SJohannes Berg 
548bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
549bd718fc1SJohannes Berg 				continue;
550046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
551bd718fc1SJohannes Berg 		}
552bd718fc1SJohannes Berg 	}
553bd718fc1SJohannes Berg 
554af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
555687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
556687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
55721a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
55821a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
55921a8e9ddSMohammed Shafi Shajakhan 
56021a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
56121a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
56221a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
56321a8e9ddSMohammed Shafi Shajakhan 
56421a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
565687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
566687da132SEmmanuel Grumbach 		/*
567687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
568687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
569687da132SEmmanuel Grumbach 		 */
570687da132SEmmanuel Grumbach 		switch (smps) {
571687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
572687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
573687da132SEmmanuel Grumbach 			break;
574687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
575687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
576687da132SEmmanuel Grumbach 			break;
577687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
578687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
579687da132SEmmanuel Grumbach 			break;
580687da132SEmmanuel Grumbach 		default:
581687da132SEmmanuel Grumbach 			WARN_ON(1);
582687da132SEmmanuel Grumbach 		}
583687da132SEmmanuel Grumbach 	}
584af0ed69bSJohannes Berg 
5856e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
5866e0456b5SFelix Fietkau 
587484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
588484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
589484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
590484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
591dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
592dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
593484a54c2SToke Høiland-Jørgensen 
594bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
595ef04a297SJohannes Berg 
596abfbc3afSJohannes Berg 	return sta;
597ba8c3d6fSFelix Fietkau 
598ba8c3d6fSFelix Fietkau free_txq:
599ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
600ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
601ba8c3d6fSFelix Fietkau free:
602*cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
603433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
604433f5bc1SJohannes Berg 	kfree(sta->mesh);
605433f5bc1SJohannes Berg #endif
606ba8c3d6fSFelix Fietkau 	kfree(sta);
607ba8c3d6fSFelix Fietkau 	return NULL;
60873651ee6SJohannes Berg }
60973651ee6SJohannes Berg 
6108c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
61134e89507SJohannes Berg {
61234e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
61334e89507SJohannes Berg 
61403e4497eSJohannes Berg 	/*
61503e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
61603e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
61703e4497eSJohannes Berg 	 * and another CPU turns off the net device.
61803e4497eSJohannes Berg 	 */
6198c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
6208c71df7aSGuy Eilam 		return -ENETDOWN;
62103e4497eSJohannes Berg 
622b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
623deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
6248c71df7aSGuy Eilam 		return -EINVAL;
6258c71df7aSGuy Eilam 
62683e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
62783e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
62883e7e4ceSHerbert Xu 	 * for correctness.
62931104891SJohannes Berg 	 */
63031104891SJohannes Berg 	rcu_read_lock();
63131104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
63231104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
63331104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
63431104891SJohannes Berg 		rcu_read_unlock();
63531104891SJohannes Berg 		return -ENOTUNIQ;
63631104891SJohannes Berg 	}
63731104891SJohannes Berg 	rcu_read_unlock();
63831104891SJohannes Berg 
6398c71df7aSGuy Eilam 	return 0;
64093e5deb1SJohannes Berg }
64144213b5eSJohannes Berg 
642f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
643f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
644f09603a2SJohannes Berg 				     struct sta_info *sta)
645f09603a2SJohannes Berg {
646f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
647f09603a2SJohannes Berg 	int err = 0;
648f09603a2SJohannes Berg 
649f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
650f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
651f09603a2SJohannes Berg 		if (err)
652f09603a2SJohannes Berg 			break;
653f09603a2SJohannes Berg 	}
654f09603a2SJohannes Berg 
655f09603a2SJohannes Berg 	if (!err) {
656a4ec45a4SJohannes Berg 		/*
657a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
658a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
659a4ec45a4SJohannes Berg 		 */
660a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
661f09603a2SJohannes Berg 			sta->uploaded = true;
662f09603a2SJohannes Berg 		return 0;
663f09603a2SJohannes Berg 	}
664f09603a2SJohannes Berg 
665f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
666bdcbd8e0SJohannes Berg 		sdata_info(sdata,
667bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
668bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
669f09603a2SJohannes Berg 		err = 0;
670f09603a2SJohannes Berg 	}
671f09603a2SJohannes Berg 
672f09603a2SJohannes Berg 	/* unwind on error */
673f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
674f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
675f09603a2SJohannes Berg 
676f09603a2SJohannes Berg 	return err;
677f09603a2SJohannes Berg }
678f09603a2SJohannes Berg 
679d405fd8cSGregory Greenman static void
680d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
681d405fd8cSGregory Greenman {
682d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
683d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
684d405fd8cSGregory Greenman 	struct sta_info *sta;
685d405fd8cSGregory Greenman 
686d405fd8cSGregory Greenman 	rcu_read_lock();
687d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
688d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
689d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
690d405fd8cSGregory Greenman 			continue;
691d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
692d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
693d405fd8cSGregory Greenman 			break;
694d405fd8cSGregory Greenman 		}
695d405fd8cSGregory Greenman 	}
696d405fd8cSGregory Greenman 	rcu_read_unlock();
697d405fd8cSGregory Greenman 
698d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
699d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
7007b7090b4SJohannes Berg 		ieee80211_link_info_change_notify(sdata, 0, BSS_CHANGED_P2P_PS);
701d405fd8cSGregory Greenman 	}
702d405fd8cSGregory Greenman }
703d405fd8cSGregory Greenman 
70434e89507SJohannes Berg /*
7058c71df7aSGuy Eilam  * should be called with sta_mtx locked
7068c71df7aSGuy Eilam  * this function replaces the mutex lock
7078c71df7aSGuy Eilam  * with a RCU lock
7088c71df7aSGuy Eilam  */
7094d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
7108c71df7aSGuy Eilam {
7118c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
7128c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
7130c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
7148c71df7aSGuy Eilam 	int err = 0;
7158c71df7aSGuy Eilam 
7168c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
7178c71df7aSGuy Eilam 
7187852e361SJohannes Berg 	/* check if STA exists already */
7197852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
7204d33960bSJohannes Berg 		err = -EEXIST;
7218f9dcc29SAhmed Zaki 		goto out_cleanup;
72234e89507SJohannes Berg 	}
72334e89507SJohannes Berg 
7240c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
7250c2e3842SKoen Vandeputte 	if (!sinfo) {
7260c2e3842SKoen Vandeputte 		err = -ENOMEM;
7278f9dcc29SAhmed Zaki 		goto out_cleanup;
7280c2e3842SKoen Vandeputte 	}
7290c2e3842SKoen Vandeputte 
7304d33960bSJohannes Berg 	local->num_sta++;
7314d33960bSJohannes Berg 	local->sta_generation++;
7324d33960bSJohannes Berg 	smp_mb();
7334d33960bSJohannes Berg 
7345108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
7355108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
7365108ca82SJohannes Berg 
7374d33960bSJohannes Berg 	/* make the station visible */
73862b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
73962b14b24SJohannes Berg 	if (err)
74062b14b24SJohannes Berg 		goto out_drop_sta;
7414d33960bSJohannes Berg 
7422bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
74383d5cc01SJohannes Berg 
7444dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
7454dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
7464dde3c36SMordechay Goodstein 	 */
7474dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
7484dde3c36SMordechay Goodstein 		ieee80211_recalc_min_chandef(sta->sdata);
7494dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
7504dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
7514dde3c36SMordechay Goodstein 	}
7524dde3c36SMordechay Goodstein 
7535108ca82SJohannes Berg 	/* notify driver */
7545108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
7555108ca82SJohannes Berg 	if (err)
7565108ca82SJohannes Berg 		goto out_remove;
7575108ca82SJohannes Berg 
75883d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
759d405fd8cSGregory Greenman 
7605108ca82SJohannes Berg 	/* accept BA sessions now */
7615108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
7624d33960bSJohannes Berg 
7634d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
7644d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
7654d33960bSJohannes Berg 
7660ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
7670ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
7680ef049dcSArnd Bergmann 	kfree(sinfo);
769d0709a65SJohannes Berg 
770bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
771f0706e82SJiri Benc 
77234e89507SJohannes Berg 	/* move reference to rcu-protected */
77334e89507SJohannes Berg 	rcu_read_lock();
77434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
775e9f207f0SJiri Benc 
77673651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
77773651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
77873651ee6SJohannes Berg 
77973651ee6SJohannes Berg 	return 0;
7805108ca82SJohannes Berg  out_remove:
7815108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
7825108ca82SJohannes Berg 	list_del_rcu(&sta->list);
78362b14b24SJohannes Berg  out_drop_sta:
7845108ca82SJohannes Berg 	local->num_sta--;
7855108ca82SJohannes Berg 	synchronize_net();
7868f9dcc29SAhmed Zaki  out_cleanup:
7877bc40aedSJohannes Berg 	cleanup_single_sta(sta);
7884d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
789ea32f065SSudip Mukherjee 	kfree(sinfo);
7904d33960bSJohannes Berg 	rcu_read_lock();
7914d33960bSJohannes Berg 	return err;
7928c71df7aSGuy Eilam }
7938c71df7aSGuy Eilam 
7948c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
7958c71df7aSGuy Eilam {
7968c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
797308f7fcfSZhao, Gang 	int err;
7988c71df7aSGuy Eilam 
7994d33960bSJohannes Berg 	might_sleep();
8004d33960bSJohannes Berg 
80131104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
80231104891SJohannes Berg 
8038c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
8048c71df7aSGuy Eilam 	if (err) {
8057bc40aedSJohannes Berg 		sta_info_free(local, sta);
80631104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
8078c71df7aSGuy Eilam 		rcu_read_lock();
8087bc40aedSJohannes Berg 		return err;
8098c71df7aSGuy Eilam 	}
8108c71df7aSGuy Eilam 
8117bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
812f0706e82SJiri Benc }
813f0706e82SJiri Benc 
81434e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
81534e89507SJohannes Berg {
81634e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
81734e89507SJohannes Berg 
81834e89507SJohannes Berg 	rcu_read_unlock();
81934e89507SJohannes Berg 
82034e89507SJohannes Berg 	return err;
82134e89507SJohannes Berg }
82234e89507SJohannes Berg 
823d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
824004c872eSJohannes Berg {
825004c872eSJohannes Berg 	/*
826004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
827004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
828004c872eSJohannes Berg 	 */
829d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
830004c872eSJohannes Berg }
831004c872eSJohannes Berg 
832d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
833004c872eSJohannes Berg {
834004c872eSJohannes Berg 	/*
835004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
836004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
837004c872eSJohannes Berg 	 */
838d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
839004c872eSJohannes Berg }
840004c872eSJohannes Berg 
8413d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
8423d5839b6SIlan Peer {
8433d5839b6SIlan Peer 	/*
8443d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
8453d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
8463d5839b6SIlan Peer 	 */
8473d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
8483d5839b6SIlan Peer }
8493d5839b6SIlan Peer 
850948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
851004c872eSJohannes Berg {
852948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
853948d887dSJohannes Berg 	switch (ac) {
854948d887dSJohannes Berg 	case IEEE80211_AC_VO:
855948d887dSJohannes Berg 		return BIT(6) | BIT(7);
856948d887dSJohannes Berg 	case IEEE80211_AC_VI:
857948d887dSJohannes Berg 		return BIT(4) | BIT(5);
858948d887dSJohannes Berg 	case IEEE80211_AC_BE:
859948d887dSJohannes Berg 		return BIT(0) | BIT(3);
860948d887dSJohannes Berg 	case IEEE80211_AC_BK:
861948d887dSJohannes Berg 		return BIT(1) | BIT(2);
862948d887dSJohannes Berg 	default:
863948d887dSJohannes Berg 		WARN_ON(1);
864948d887dSJohannes Berg 		return 0;
865d0709a65SJohannes Berg 	}
866004c872eSJohannes Berg }
867004c872eSJohannes Berg 
8689b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
869004c872eSJohannes Berg {
870c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
871d012a605SMarco Porsch 	struct ps_data *ps;
872948d887dSJohannes Berg 	bool indicate_tim = false;
873948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
874948d887dSJohannes Berg 	int ac;
875a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
876004c872eSJohannes Berg 
877d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
878d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
879c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
880c868cb35SJohannes Berg 			return;
8813e122be0SJohannes Berg 
882d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
8833f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
8843f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
8853f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
8863f52b7e3SMarco Porsch #endif
887d012a605SMarco Porsch 	} else {
888d012a605SMarco Porsch 		return;
889d012a605SMarco Porsch 	}
890d012a605SMarco Porsch 
891c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
892d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
893c868cb35SJohannes Berg 		return;
894004c872eSJohannes Berg 
895c868cb35SJohannes Berg 	if (sta->dead)
896c868cb35SJohannes Berg 		goto done;
8973e122be0SJohannes Berg 
898948d887dSJohannes Berg 	/*
899948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
900948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
901948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
902948d887dSJohannes Berg 	 * non-enabled ones.
903948d887dSJohannes Berg 	 */
904948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
905948d887dSJohannes Berg 		ignore_for_tim = 0;
906948d887dSJohannes Berg 
9079b7a86f3SJohannes Berg 	if (ignore_pending)
9089b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
9099b7a86f3SJohannes Berg 
910948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
911948d887dSJohannes Berg 		unsigned long tids;
912948d887dSJohannes Berg 
913f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
914948d887dSJohannes Berg 			continue;
915948d887dSJohannes Berg 
916948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
917948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
918948d887dSJohannes Berg 		if (indicate_tim)
919948d887dSJohannes Berg 			break;
920948d887dSJohannes Berg 
921948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
922948d887dSJohannes Berg 
923948d887dSJohannes Berg 		indicate_tim |=
924948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
925ba8c3d6fSFelix Fietkau 		indicate_tim |=
926ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
927004c872eSJohannes Berg 	}
928004c872eSJohannes Berg 
929c868cb35SJohannes Berg  done:
93065f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
931004c872eSJohannes Berg 
9323d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
9333d5839b6SIlan Peer 		goto out_unlock;
9343d5839b6SIlan Peer 
935948d887dSJohannes Berg 	if (indicate_tim)
936d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
937c868cb35SJohannes Berg 	else
938d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
9393e122be0SJohannes Berg 
9409b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
941c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
942948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
943c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
944004c872eSJohannes Berg 	}
945004c872eSJohannes Berg 
9463d5839b6SIlan Peer out_unlock:
94765f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
948004c872eSJohannes Berg }
949004c872eSJohannes Berg 
9509b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
9519b7a86f3SJohannes Berg {
9529b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
9539b7a86f3SJohannes Berg }
9549b7a86f3SJohannes Berg 
955cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
956f0706e82SJiri Benc {
957e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
958f0706e82SJiri Benc 	int timeout;
959f0706e82SJiri Benc 
960f0706e82SJiri Benc 	if (!skb)
961cd0b8d89SJohannes Berg 		return false;
962f0706e82SJiri Benc 
963e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
964f0706e82SJiri Benc 
965f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
96657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
96757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
96857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
969f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
970f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
971e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
972f0706e82SJiri Benc }
973f0706e82SJiri Benc 
974f0706e82SJiri Benc 
975948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
976948d887dSJohannes Berg 						struct sta_info *sta, int ac)
977f0706e82SJiri Benc {
978f0706e82SJiri Benc 	unsigned long flags;
979f0706e82SJiri Benc 	struct sk_buff *skb;
980f0706e82SJiri Benc 
98160750397SJohannes Berg 	/*
98260750397SJohannes Berg 	 * First check for frames that should expire on the filtered
98360750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
98460750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
98560750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
98660750397SJohannes Berg 	 * total_ps_buffered counter.
98760750397SJohannes Berg 	 */
988f0706e82SJiri Benc 	for (;;) {
989948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
990948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
99157c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
992948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
993836341a7SJohannes Berg 		else
994f0706e82SJiri Benc 			skb = NULL;
995948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
996f0706e82SJiri Benc 
99760750397SJohannes Berg 		/*
99860750397SJohannes Berg 		 * Frames are queued in order, so if this one
99960750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
100060750397SJohannes Berg 		 * we actually reached the end of the queue we
100160750397SJohannes Berg 		 * also need to stop, of course.
100260750397SJohannes Berg 		 */
100360750397SJohannes Berg 		if (!skb)
100460750397SJohannes Berg 			break;
1005d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
100660750397SJohannes Berg 	}
100760750397SJohannes Berg 
100860750397SJohannes Berg 	/*
100960750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
101060750397SJohannes Berg 	 * only find something if the filtered queue was emptied
101160750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
101260750397SJohannes Berg 	 * buffered frames.
101360750397SJohannes Berg 	 */
1014f0706e82SJiri Benc 	for (;;) {
1015948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1016948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1017f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1018948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1019f0706e82SJiri Benc 		else
1020f0706e82SJiri Benc 			skb = NULL;
1021948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1022f0706e82SJiri Benc 
102360750397SJohannes Berg 		/*
102460750397SJohannes Berg 		 * frames are queued in order, so if this one
102560750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
102660750397SJohannes Berg 		 * the queue) we can stop testing
102760750397SJohannes Berg 		 */
1028836341a7SJohannes Berg 		if (!skb)
1029836341a7SJohannes Berg 			break;
1030836341a7SJohannes Berg 
1031f0706e82SJiri Benc 		local->total_ps_buffered--;
1032bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1033bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1034d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1035f0706e82SJiri Benc 	}
10363393a608SJuuso Oikarinen 
103760750397SJohannes Berg 	/*
103860750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
103960750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
104060750397SJohannes Berg 	 * frames.
104160750397SJohannes Berg 	 */
104260750397SJohannes Berg 	sta_info_recalc_tim(sta);
104360750397SJohannes Berg 
104460750397SJohannes Berg 	/*
104560750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
104660750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
104760750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
104860750397SJohannes Berg 	 */
1049948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1050948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1051948d887dSJohannes Berg }
1052948d887dSJohannes Berg 
1053948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1054948d887dSJohannes Berg 					     struct sta_info *sta)
1055948d887dSJohannes Berg {
1056948d887dSJohannes Berg 	bool have_buffered = false;
1057948d887dSJohannes Berg 	int ac;
1058948d887dSJohannes Berg 
10593f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
10603f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
10613f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1062948d887dSJohannes Berg 		return false;
1063948d887dSJohannes Berg 
1064948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1065948d887dSJohannes Berg 		have_buffered |=
1066948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1067948d887dSJohannes Berg 
1068948d887dSJohannes Berg 	return have_buffered;
1069f0706e82SJiri Benc }
1070f0706e82SJiri Benc 
1071d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
107234e89507SJohannes Berg {
107334e89507SJohannes Berg 	struct ieee80211_local *local;
107434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
10756d10e46bSJohannes Berg 	int ret;
107634e89507SJohannes Berg 
107734e89507SJohannes Berg 	might_sleep();
107834e89507SJohannes Berg 
107934e89507SJohannes Berg 	if (!sta)
108034e89507SJohannes Berg 		return -ENOENT;
108134e89507SJohannes Berg 
108234e89507SJohannes Berg 	local = sta->local;
108334e89507SJohannes Berg 	sdata = sta->sdata;
108434e89507SJohannes Berg 
108583d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
108683d5cc01SJohannes Berg 
1087098a6070SJohannes Berg 	/*
1088098a6070SJohannes Berg 	 * Before removing the station from the driver and
1089098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1090098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1091098a6070SJohannes Berg 	 * will be sufficient.
1092098a6070SJohannes Berg 	 */
1093c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1094c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1095098a6070SJohannes Berg 
1096f59374ebSSara Sharon 	/*
1097f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1098f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1099f59374ebSSara Sharon 	 * all such frames to be processed.
1100f59374ebSSara Sharon 	 */
1101f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1102f59374ebSSara Sharon 
110334e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1104b01711beSJohannes Berg 	if (WARN_ON(ret))
110534e89507SJohannes Berg 		return ret;
110634e89507SJohannes Berg 
1107a7a6bdd0SArik Nemtsov 	/*
1108a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1109a7a6bdd0SArik Nemtsov 	 * removal.
1110a7a6bdd0SArik Nemtsov 	 */
1111a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1112a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1113a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1114a7a6bdd0SArik Nemtsov 	}
1115a7a6bdd0SArik Nemtsov 
1116794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1117ef044763SEliad Peller 	sta->removed = true;
11184d33960bSJohannes Berg 
11196a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
11206a9d1b91SJohannes Berg 
1121a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1122a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1123a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1124a710c816SJohannes Berg 
1125d778207bSJohannes Berg 	return 0;
1126d778207bSJohannes Berg }
1127d778207bSJohannes Berg 
1128d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1129d778207bSJohannes Berg {
1130d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1131d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
11320ef049dcSArnd Bergmann 	struct station_info *sinfo;
1133d778207bSJohannes Berg 	int ret;
1134d778207bSJohannes Berg 
1135d778207bSJohannes Berg 	/*
1136d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1137d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1138d778207bSJohannes Berg 	 */
1139d778207bSJohannes Berg 
1140d778207bSJohannes Berg 	might_sleep();
1141d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1142d778207bSJohannes Berg 
11435981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1144b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1145b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1146b16798f5SJohannes Berg 	}
1147b16798f5SJohannes Berg 
1148c8782078SJohannes Berg 	/* now keys can no longer be reached */
11496d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
115034e89507SJohannes Berg 
11519b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
11529b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
11539b7a86f3SJohannes Berg 
115434e89507SJohannes Berg 	sta->dead = true;
115534e89507SJohannes Berg 
115634e89507SJohannes Berg 	local->num_sta--;
115734e89507SJohannes Berg 	local->sta_generation++;
115834e89507SJohannes Berg 
115983d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1160f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1161f09603a2SJohannes Berg 		if (ret) {
116283d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
116383d5cc01SJohannes Berg 			break;
116483d5cc01SJohannes Berg 		}
116583d5cc01SJohannes Berg 	}
1166d9a7ddb0SJohannes Berg 
1167f09603a2SJohannes Berg 	if (sta->uploaded) {
1168f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1169f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1170f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1171f09603a2SJohannes Berg 	}
117234e89507SJohannes Berg 
1173bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1174bdcbd8e0SJohannes Berg 
11750ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
11760ef049dcSArnd Bergmann 	if (sinfo)
11770fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
11780ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
11790ef049dcSArnd Bergmann 	kfree(sinfo);
1180ec15e68bSJouni Malinen 
118134e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
118234e89507SJohannes Berg 
11833a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
11843a11ce08SJohannes Berg 
1185d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1186d778207bSJohannes Berg }
1187d778207bSJohannes Berg 
1188d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1189d778207bSJohannes Berg {
1190d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1191d778207bSJohannes Berg 
1192d778207bSJohannes Berg 	if (err)
1193d778207bSJohannes Berg 		return err;
1194d778207bSJohannes Berg 
1195d778207bSJohannes Berg 	synchronize_net();
1196d778207bSJohannes Berg 
1197d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
119834e89507SJohannes Berg 
119934e89507SJohannes Berg 	return 0;
120034e89507SJohannes Berg }
120134e89507SJohannes Berg 
120234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
120334e89507SJohannes Berg {
120434e89507SJohannes Berg 	struct sta_info *sta;
120534e89507SJohannes Berg 	int ret;
120634e89507SJohannes Berg 
120734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12087852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
120934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
121034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
121134e89507SJohannes Berg 
121234e89507SJohannes Berg 	return ret;
121334e89507SJohannes Berg }
121434e89507SJohannes Berg 
121534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
121634e89507SJohannes Berg 			      const u8 *addr)
121734e89507SJohannes Berg {
121834e89507SJohannes Berg 	struct sta_info *sta;
121934e89507SJohannes Berg 	int ret;
122034e89507SJohannes Berg 
122134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12227852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
122334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
122434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
122534e89507SJohannes Berg 
122634e89507SJohannes Berg 	return ret;
122734e89507SJohannes Berg }
1228f0706e82SJiri Benc 
122934f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1230f0706e82SJiri Benc {
123134f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1232f0706e82SJiri Benc 	struct sta_info *sta;
12333393a608SJuuso Oikarinen 	bool timer_needed = false;
1234f0706e82SJiri Benc 
1235d0709a65SJohannes Berg 	rcu_read_lock();
1236d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
12373393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
12383393a608SJuuso Oikarinen 			timer_needed = true;
1239d0709a65SJohannes Berg 	rcu_read_unlock();
1240f0706e82SJiri Benc 
12415bb644a0SJohannes Berg 	if (local->quiescing)
12425bb644a0SJohannes Berg 		return;
12435bb644a0SJohannes Berg 
12443393a608SJuuso Oikarinen 	if (!timer_needed)
12453393a608SJuuso Oikarinen 		return;
12463393a608SJuuso Oikarinen 
124726d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
124826d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1249f0706e82SJiri Benc }
1250f0706e82SJiri Benc 
12517bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
12527bedd0cfSJohannes Berg {
12537bedd0cfSJohannes Berg 	int err;
12547bedd0cfSJohannes Berg 
125583e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
12567bedd0cfSJohannes Berg 	if (err)
12577bedd0cfSJohannes Berg 		return err;
12587bedd0cfSJohannes Berg 
12594d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
126034e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1261f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1262f0706e82SJiri Benc 
126334f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
12647bedd0cfSJohannes Berg 	return 0;
1265f0706e82SJiri Benc }
1266f0706e82SJiri Benc 
1267f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1268f0706e82SJiri Benc {
1269a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
127083e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1271f0706e82SJiri Benc }
1272f0706e82SJiri Benc 
1273051007d9SJohannes Berg 
1274e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1275f0706e82SJiri Benc {
1276b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1277f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1278d778207bSJohannes Berg 	LIST_HEAD(free_list);
127944213b5eSJohannes Berg 	int ret = 0;
1280f0706e82SJiri Benc 
1281d0709a65SJohannes Berg 	might_sleep();
1282d0709a65SJohannes Berg 
1283e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1284e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1285e716251dSJohannes Berg 
128634e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
128734e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1288e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1289e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1290d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1291d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
129234316837SJohannes Berg 			ret++;
129334316837SJohannes Berg 		}
129434e89507SJohannes Berg 	}
1295d778207bSJohannes Berg 
1296d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1297d778207bSJohannes Berg 		synchronize_net();
1298d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1299d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1300d778207bSJohannes Berg 	}
130134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
130244213b5eSJohannes Berg 
1303051007d9SJohannes Berg 	return ret;
1304051007d9SJohannes Berg }
1305051007d9SJohannes Berg 
130624723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
130724723d1bSJohannes Berg 			  unsigned long exp_time)
130824723d1bSJohannes Berg {
130924723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
131024723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
131124723d1bSJohannes Berg 
131234e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1313e46a2cf9SMohammed Shafi Shajakhan 
1314e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1315b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1316b8da6b6aSJohannes Berg 
1317ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1318ec2b774eSMarek Lindner 			continue;
1319ec2b774eSMarek Lindner 
1320b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1321eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1322bdcbd8e0SJohannes Berg 				sta->sta.addr);
13233f52b7e3SMarco Porsch 
13243f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
13253f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
13263f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
13273f52b7e3SMarco Porsch 
132834e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
132924723d1bSJohannes Berg 		}
1330e46a2cf9SMohammed Shafi Shajakhan 	}
1331e46a2cf9SMohammed Shafi Shajakhan 
133234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
133324723d1bSJohannes Berg }
133417741cdcSJohannes Berg 
1335686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1336686b9cb9SBen Greear 						   const u8 *addr,
1337686b9cb9SBen Greear 						   const u8 *localaddr)
133817741cdcSJohannes Berg {
13397bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
134083e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
13417bedd0cfSJohannes Berg 	struct sta_info *sta;
134217741cdcSJohannes Berg 
1343686b9cb9SBen Greear 	/*
1344686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1345686b9cb9SBen Greear 	 * ... first in list.
1346686b9cb9SBen Greear 	 */
134783e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1348686b9cb9SBen Greear 		if (localaddr &&
1349b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1350686b9cb9SBen Greear 			continue;
1351f7c65594SJohannes Berg 		if (!sta->uploaded)
1352f7c65594SJohannes Berg 			return NULL;
135317741cdcSJohannes Berg 		return &sta->sta;
1354f7c65594SJohannes Berg 	}
1355f7c65594SJohannes Berg 
1356abe60632SJohannes Berg 	return NULL;
135717741cdcSJohannes Berg }
1358686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
13595ed176e1SJohannes Berg 
13605ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
13615ed176e1SJohannes Berg 					 const u8 *addr)
13625ed176e1SJohannes Berg {
1363f7c65594SJohannes Berg 	struct sta_info *sta;
13645ed176e1SJohannes Berg 
13655ed176e1SJohannes Berg 	if (!vif)
13665ed176e1SJohannes Berg 		return NULL;
13675ed176e1SJohannes Berg 
1368f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1369f7c65594SJohannes Berg 	if (!sta)
1370f7c65594SJohannes Berg 		return NULL;
13715ed176e1SJohannes Berg 
1372f7c65594SJohannes Berg 	if (!sta->uploaded)
1373f7c65594SJohannes Berg 		return NULL;
1374f7c65594SJohannes Berg 
1375f7c65594SJohannes Berg 	return &sta->sta;
13765ed176e1SJohannes Berg }
137717741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1378af818581SJohannes Berg 
1379e3685e03SJohannes Berg /* powersave support code */
1380e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
138150a9432dSJohannes Berg {
1382608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1383e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1384e3685e03SJohannes Berg 	struct sk_buff_head pending;
1385ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1386e3685e03SJohannes Berg 	unsigned long flags;
1387d012a605SMarco Porsch 	struct ps_data *ps;
1388d012a605SMarco Porsch 
13893918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
13903918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
13913918edb0SFelix Fietkau 				     u.ap);
13923918edb0SFelix Fietkau 
13933918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1394d012a605SMarco Porsch 		ps = &sdata->bss->ps;
13953f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
13963f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1397d012a605SMarco Porsch 	else
1398d012a605SMarco Porsch 		return;
139950a9432dSJohannes Berg 
1400c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
140147086fc5SJohannes Berg 
14025a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1403948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1404ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1405948d887dSJohannes Berg 
140630686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
140712375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1408af818581SJohannes Berg 
1409ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1410adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1411ba8c3d6fSFelix Fietkau 			continue;
1412ba8c3d6fSFelix Fietkau 
141318667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1414ba8c3d6fSFelix Fietkau 	}
1415ba8c3d6fSFelix Fietkau 
1416948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1417af818581SJohannes Berg 
14181d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
14191d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1420af818581SJohannes Berg 	/* Send all buffered frames to the station */
1421948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1422948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1423948d887dSJohannes Berg 
1424987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1425948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1426987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1427948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1428948d887dSJohannes Berg 		filtered += tmp - count;
1429948d887dSJohannes Berg 		count = tmp;
1430948d887dSJohannes Berg 
1431987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1432948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1433987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1434948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1435948d887dSJohannes Berg 		buffered += tmp - count;
1436948d887dSJohannes Berg 	}
1437948d887dSJohannes Berg 
1438e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
14395ac2e350SJohannes Berg 
14405ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
14415ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
14425ac2e350SJohannes Berg 
14435ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
14445ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
14455ac2e350SJohannes Berg 	 */
14465ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
14475ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
14481d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1449948d887dSJohannes Berg 
1450e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1451e3685e03SJohannes Berg 
1452af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1453af818581SJohannes Berg 
1454c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1455c868cb35SJohannes Berg 
1456bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
14572595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1458bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
145917c18bf8SJohannes Berg 
146017c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1461af818581SJohannes Berg }
1462af818581SJohannes Berg 
14630ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1464b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
14650ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1466ce662b44SJohannes Berg {
14670ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1468ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1469ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1470ce662b44SJohannes Berg 	struct sk_buff *skb;
1471ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1472ce662b44SJohannes Berg 	__le16 fc;
1473a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1474ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
147555de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1476ce662b44SJohannes Berg 
1477ce662b44SJohannes Berg 	if (qos) {
1478ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1479ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1480ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1481ce662b44SJohannes Berg 	} else {
1482ce662b44SJohannes Berg 		size -= 2;
1483ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1484ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1485ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1486ce662b44SJohannes Berg 	}
1487ce662b44SJohannes Berg 
1488ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1489ce662b44SJohannes Berg 	if (!skb)
1490ce662b44SJohannes Berg 		return;
1491ce662b44SJohannes Berg 
1492ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1493ce662b44SJohannes Berg 
14944df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1495ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1496ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1497ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1498ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1499ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1500864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1501ce662b44SJohannes Berg 
1502ce662b44SJohannes Berg 	skb->priority = tid;
1503ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
150459b66255SJohannes Berg 	if (qos) {
1505ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1506ce662b44SJohannes Berg 
15070ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1508ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1509ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
15100ead2510SEmmanuel Grumbach 			if (more_data)
15110ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
15120ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
15130ead2510SEmmanuel Grumbach 		}
1514ce662b44SJohannes Berg 	}
1515ce662b44SJohannes Berg 
1516ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1517ce662b44SJohannes Berg 
1518ce662b44SJohannes Berg 	/*
1519ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1520ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1521deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1522deeaee19SJohannes Berg 	 * ends the poll/service period.
1523ce662b44SJohannes Berg 	 */
152402f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1525deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1526ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1527ce662b44SJohannes Berg 
15286b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
15296b127c71SSujith Manoharan 
1530b77cf4f8SJohannes Berg 	if (call_driver)
1531b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1532b77cf4f8SJohannes Berg 					  reason, false);
153340b96408SJohannes Berg 
153489afe614SJohannes Berg 	skb->dev = sdata->dev;
153589afe614SJohannes Berg 
153655de908aSJohannes Berg 	rcu_read_lock();
1537d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
153855de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
153955de908aSJohannes Berg 		rcu_read_unlock();
154055de908aSJohannes Berg 		kfree_skb(skb);
154155de908aSJohannes Berg 		return;
154255de908aSJohannes Berg 	}
154355de908aSJohannes Berg 
154473c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
154508aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
154655de908aSJohannes Berg 	rcu_read_unlock();
1547ce662b44SJohannes Berg }
1548ce662b44SJohannes Berg 
15490a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
15500a1cb809SJohannes Berg {
15510a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
15520a1cb809SJohannes Berg 	if (tids & 0xF8)
15530a1cb809SJohannes Berg 		return fls(tids) - 1;
15540a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
15550a1cb809SJohannes Berg 	if (tids & BIT(0))
15560a1cb809SJohannes Berg 		return 0;
15570a1cb809SJohannes Berg 	return fls(tids) - 1;
15580a1cb809SJohannes Berg }
15590a1cb809SJohannes Berg 
15600ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
15610ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
15620ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
15630ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
15640ead2510SEmmanuel Grumbach  */
15650ead2510SEmmanuel Grumbach static bool
15660ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
15670ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
15680ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
15690ead2510SEmmanuel Grumbach {
15700ead2510SEmmanuel Grumbach 	int ac;
15710ead2510SEmmanuel Grumbach 
15720ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
15730ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
15740ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
15750ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
15760ead2510SEmmanuel Grumbach 	 */
15770ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
15780ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
15790ead2510SEmmanuel Grumbach 		return true;
15800ead2510SEmmanuel Grumbach 
15810ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1582f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
15830ead2510SEmmanuel Grumbach 			continue;
15840ead2510SEmmanuel Grumbach 
15850ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15860ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
15870ead2510SEmmanuel Grumbach 			return true;
15880ead2510SEmmanuel Grumbach 	}
15890ead2510SEmmanuel Grumbach 
15900ead2510SEmmanuel Grumbach 	return false;
15910ead2510SEmmanuel Grumbach }
15920ead2510SEmmanuel Grumbach 
159347086fc5SJohannes Berg static void
15940ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
15950ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
15960ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
15970ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1598af818581SJohannes Berg {
1599af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1600af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1601948d887dSJohannes Berg 	int ac;
1602af818581SJohannes Berg 
1603f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1604948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
16054049e09aSJohannes Berg 		unsigned long tids;
16064049e09aSJohannes Berg 
1607f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1608948d887dSJohannes Berg 			continue;
1609948d887dSJohannes Berg 
16104049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
16114049e09aSJohannes Berg 
1612f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1613f9f760b4SJohannes Berg 		 * release from hardware queues
1614f9f760b4SJohannes Berg 		 */
16150ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
16160ead2510SEmmanuel Grumbach 			*driver_release_tids |=
16170ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
16180ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1619ba8c3d6fSFelix Fietkau 		}
1620f9f760b4SJohannes Berg 
16210ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
162247086fc5SJohannes Berg 			struct sk_buff *skb;
162347086fc5SJohannes Berg 
162447086fc5SJohannes Berg 			while (n_frames > 0) {
1625948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1626948d887dSJohannes Berg 				if (!skb) {
162747086fc5SJohannes Berg 					skb = skb_dequeue(
162847086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1629af818581SJohannes Berg 					if (skb)
1630af818581SJohannes Berg 						local->total_ps_buffered--;
1631af818581SJohannes Berg 				}
163247086fc5SJohannes Berg 				if (!skb)
163347086fc5SJohannes Berg 					break;
163447086fc5SJohannes Berg 				n_frames--;
16350ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
163647086fc5SJohannes Berg 			}
1637948d887dSJohannes Berg 		}
1638948d887dSJohannes Berg 
16390ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
16400ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
16410ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
16424049e09aSJohannes Berg 		 */
1643948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
16440ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1645948d887dSJohannes Berg 			break;
1646948d887dSJohannes Berg 	}
1647948d887dSJohannes Berg }
1648af818581SJohannes Berg 
16490ead2510SEmmanuel Grumbach static void
16500ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
16510ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
16520ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
16530ead2510SEmmanuel Grumbach {
16540ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
16550ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
16560ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
16570ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
16580ead2510SEmmanuel Grumbach 	bool more_data;
16590ead2510SEmmanuel Grumbach 
16600ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
16610ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
16620ead2510SEmmanuel Grumbach 
16630ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
16640ead2510SEmmanuel Grumbach 
16650ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
16660ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
16670ead2510SEmmanuel Grumbach 
16680ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
16690ead2510SEmmanuel Grumbach 
16701a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
16710ead2510SEmmanuel Grumbach 		driver_release_tids =
16720ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
16730ead2510SEmmanuel Grumbach 
1674f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1675f438ceb8SEmmanuel Grumbach 		int tid, ac;
16764049e09aSJohannes Berg 
1677ce662b44SJohannes Berg 		/*
1678ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1679ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1680ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1681ce662b44SJohannes Berg 		 *
1682ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1683ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1684ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1685ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1686ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1687ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1688ce662b44SJohannes Berg 		 *
1689ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1690ce662b44SJohannes Berg 		 */
1691ce662b44SJohannes Berg 
1692ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1693f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1694d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1695d7f84244SEmmanuel Grumbach 				break;
1696f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1697ce662b44SJohannes Berg 
16980ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1699f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
170047086fc5SJohannes Berg 		struct sk_buff_head pending;
170147086fc5SJohannes Berg 		struct sk_buff *skb;
170240b96408SJohannes Berg 		int num = 0;
170340b96408SJohannes Berg 		u16 tids = 0;
1704b77cf4f8SJohannes Berg 		bool need_null = false;
170547086fc5SJohannes Berg 
170647086fc5SJohannes Berg 		skb_queue_head_init(&pending);
170747086fc5SJohannes Berg 
170847086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1709af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
171047086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
171140b96408SJohannes Berg 			u8 *qoshdr = NULL;
171240b96408SJohannes Berg 
171340b96408SJohannes Berg 			num++;
1714af818581SJohannes Berg 
1715af818581SJohannes Berg 			/*
171647086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
171747086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
171847086fc5SJohannes Berg 			 * exchange.
1719af818581SJohannes Berg 			 */
17206b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
17216b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1722af818581SJohannes Berg 
172347086fc5SJohannes Berg 			/*
172447086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
172547086fc5SJohannes Berg 			 * more buffered frames for this STA
172647086fc5SJohannes Berg 			 */
172724b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
172847086fc5SJohannes Berg 				hdr->frame_control |=
172947086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
173024b9c373SJanusz.Dziedzic@tieto.com 			else
173124b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
173224b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1733af818581SJohannes Berg 
173440b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
173540b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
173640b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
173740b96408SJohannes Berg 
1738b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1739b77cf4f8SJohannes Berg 
1740b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1741b77cf4f8SJohannes Berg 
1742b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1743b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1744b77cf4f8SJohannes Berg 				continue;
1745b77cf4f8SJohannes Berg 
1746b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1747b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1748b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1749b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1750b77cf4f8SJohannes Berg 				break;
1751b77cf4f8SJohannes Berg 			}
1752b77cf4f8SJohannes Berg 
1753b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1754b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1755b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1756b77cf4f8SJohannes Berg 			 * and be done.
1757b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1758b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1759b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1760b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1761b77cf4f8SJohannes Berg 			 *
1762b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1763b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1764b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1765b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1766b77cf4f8SJohannes Berg 			 *
1767b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1768b77cf4f8SJohannes Berg 			 */
1769b77cf4f8SJohannes Berg 			if (qoshdr) {
177040b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1771deeaee19SJohannes Berg 
177247086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
177347086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1774b77cf4f8SJohannes Berg 			} else {
1775b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1776b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1777b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1778b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1779b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1780b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1781b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1782b77cf4f8SJohannes Berg 				 */
1783b77cf4f8SJohannes Berg 				hdr->frame_control |=
1784b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1785b77cf4f8SJohannes Berg 				need_null = true;
1786b77cf4f8SJohannes Berg 				num++;
178752a3f20cSMarco Porsch 			}
1788b77cf4f8SJohannes Berg 			break;
178947086fc5SJohannes Berg 		}
179047086fc5SJohannes Berg 
179140b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
179240b96408SJohannes Berg 					  reason, more_data);
179340b96408SJohannes Berg 
179447086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1795af818581SJohannes Berg 
1796b77cf4f8SJohannes Berg 		if (need_null)
1797b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
17980ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
17990ead2510SEmmanuel Grumbach 				reason, false, false);
1800b77cf4f8SJohannes Berg 
1801c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1802af818581SJohannes Berg 	} else {
1803ba8c3d6fSFelix Fietkau 		int tid;
1804ba8c3d6fSFelix Fietkau 
1805af818581SJohannes Berg 		/*
18064049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
18074049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1808f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1809f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1810f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1811f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1812f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1813f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1814af818581SJohannes Berg 		 */
18154049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
181647086fc5SJohannes Berg 					    n_frames, reason, more_data);
18174049e09aSJohannes Berg 
18184049e09aSJohannes Berg 		/*
18194049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
18204049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1821f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
18224049e09aSJohannes Berg 		 * release function.
1823f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1824ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1825ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
18264049e09aSJohannes Berg 		 */
1827ba8c3d6fSFelix Fietkau 
1828ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1829ba8c3d6fSFelix Fietkau 			return;
1830ba8c3d6fSFelix Fietkau 
1831ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1832adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1833adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
18341e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1835ba8c3d6fSFelix Fietkau 				continue;
1836ba8c3d6fSFelix Fietkau 
1837ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1838ba8c3d6fSFelix Fietkau 			break;
1839ba8c3d6fSFelix Fietkau 		}
1840af818581SJohannes Berg 	}
1841af818581SJohannes Berg }
1842af818581SJohannes Berg 
1843af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1844af818581SJohannes Berg {
184547086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1846af818581SJohannes Berg 
1847af818581SJohannes Berg 	/*
184847086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
184947086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
185047086fc5SJohannes Berg 	 * only from the non-enabled ones.
1851af818581SJohannes Berg 	 */
185247086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
185347086fc5SJohannes Berg 		ignore_for_response = 0;
1854af818581SJohannes Berg 
185547086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
185647086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1857af818581SJohannes Berg }
185847086fc5SJohannes Berg 
185947086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
186047086fc5SJohannes Berg {
186147086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
186247086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
186347086fc5SJohannes Berg 
186447086fc5SJohannes Berg 	/*
186547086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
186647086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
186747086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
186847086fc5SJohannes Berg 	 * actually getting called.
186947086fc5SJohannes Berg 	 */
187047086fc5SJohannes Berg 	if (!delivery_enabled)
187147086fc5SJohannes Berg 		return;
187247086fc5SJohannes Berg 
187347086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
187447086fc5SJohannes Berg 	case 1:
187547086fc5SJohannes Berg 		n_frames = 2;
187647086fc5SJohannes Berg 		break;
187747086fc5SJohannes Berg 	case 2:
187847086fc5SJohannes Berg 		n_frames = 4;
187947086fc5SJohannes Berg 		break;
188047086fc5SJohannes Berg 	case 3:
188147086fc5SJohannes Berg 		n_frames = 6;
188247086fc5SJohannes Berg 		break;
188347086fc5SJohannes Berg 	case 0:
188447086fc5SJohannes Berg 		/* XXX: what is a good value? */
188513a8098aSAndrei Otcheretianski 		n_frames = 128;
188647086fc5SJohannes Berg 		break;
188747086fc5SJohannes Berg 	}
188847086fc5SJohannes Berg 
188947086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
189047086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1891af818581SJohannes Berg }
1892af818581SJohannes Berg 
1893af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1894af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1895af818581SJohannes Berg {
1896af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1897af818581SJohannes Berg 
1898b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1899b5878a2dSJohannes Berg 
19005ac2e350SJohannes Berg 	if (block) {
1901c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
190217c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
19035ac2e350SJohannes Berg 		return;
19045ac2e350SJohannes Berg 	}
19055ac2e350SJohannes Berg 
19065ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
19075ac2e350SJohannes Berg 		return;
19085ac2e350SJohannes Berg 
19095ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
19105ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
19115ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
19125ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
19135ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
19145ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
19155ac2e350SJohannes Berg 		/* must be asleep in this case */
19165ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
19175ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
19185ac2e350SJohannes Berg 	} else {
19195ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
192017c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
19215ac2e350SJohannes Berg 	}
1922af818581SJohannes Berg }
1923af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1924dcf55fb5SFelix Fietkau 
1925e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
192637fbd908SJohannes Berg {
192737fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
192837fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
192937fbd908SJohannes Berg 
193037fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
193137fbd908SJohannes Berg 
193237fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
193337fbd908SJohannes Berg }
1934e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
193537fbd908SJohannes Berg 
19360ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
19370ead2510SEmmanuel Grumbach {
19380ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
19390ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
19400ead2510SEmmanuel Grumbach 	bool more_data;
19410ead2510SEmmanuel Grumbach 
19420ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
19430ead2510SEmmanuel Grumbach 
19440ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
19450ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
19460ead2510SEmmanuel Grumbach 					       reason, 0);
19470ead2510SEmmanuel Grumbach 
19480ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
19490ead2510SEmmanuel Grumbach }
19500ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
19510ead2510SEmmanuel Grumbach 
1952042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1953042ec453SJohannes Berg 				u8 tid, bool buffered)
1954dcf55fb5SFelix Fietkau {
1955dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1956dcf55fb5SFelix Fietkau 
19575a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1958042ec453SJohannes Berg 		return;
1959042ec453SJohannes Berg 
19601b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
19611b000789SJohannes Berg 
1962948d887dSJohannes Berg 	if (buffered)
1963948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1964948d887dSJohannes Berg 	else
1965948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1966948d887dSJohannes Berg 
1967c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1968dcf55fb5SFelix Fietkau }
1969042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1970d9a7ddb0SJohannes Berg 
19712433647bSToke Høiland-Jørgensen void ieee80211_register_airtime(struct ieee80211_txq *txq,
19722433647bSToke Høiland-Jørgensen 				u32 tx_airtime, u32 rx_airtime)
19732433647bSToke Høiland-Jørgensen {
19742433647bSToke Høiland-Jørgensen 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
19752433647bSToke Høiland-Jørgensen 	struct ieee80211_local *local = sdata->local;
19762433647bSToke Høiland-Jørgensen 	u64 weight_sum, weight_sum_reciprocal;
19772433647bSToke Høiland-Jørgensen 	struct airtime_sched_info *air_sched;
19782433647bSToke Høiland-Jørgensen 	struct airtime_info *air_info;
19792433647bSToke Høiland-Jørgensen 	u32 airtime = 0;
19802433647bSToke Høiland-Jørgensen 
19812433647bSToke Høiland-Jørgensen 	air_sched = &local->airtime[txq->ac];
19822433647bSToke Høiland-Jørgensen 	air_info = to_airtime_info(txq);
19832433647bSToke Høiland-Jørgensen 
19842433647bSToke Høiland-Jørgensen 	if (local->airtime_flags & AIRTIME_USE_TX)
19852433647bSToke Høiland-Jørgensen 		airtime += tx_airtime;
19862433647bSToke Høiland-Jørgensen 	if (local->airtime_flags & AIRTIME_USE_RX)
19872433647bSToke Høiland-Jørgensen 		airtime += rx_airtime;
19882433647bSToke Høiland-Jørgensen 
19892433647bSToke Høiland-Jørgensen 	/* Weights scale so the unit weight is 256 */
19902433647bSToke Høiland-Jørgensen 	airtime <<= 8;
19912433647bSToke Høiland-Jørgensen 
19922433647bSToke Høiland-Jørgensen 	spin_lock_bh(&air_sched->lock);
19932433647bSToke Høiland-Jørgensen 
19942433647bSToke Høiland-Jørgensen 	air_info->tx_airtime += tx_airtime;
19952433647bSToke Høiland-Jørgensen 	air_info->rx_airtime += rx_airtime;
19962433647bSToke Høiland-Jørgensen 
19972433647bSToke Høiland-Jørgensen 	if (air_sched->weight_sum) {
19982433647bSToke Høiland-Jørgensen 		weight_sum = air_sched->weight_sum;
19992433647bSToke Høiland-Jørgensen 		weight_sum_reciprocal = air_sched->weight_sum_reciprocal;
20002433647bSToke Høiland-Jørgensen 	} else {
20012433647bSToke Høiland-Jørgensen 		weight_sum = air_info->weight;
20022433647bSToke Høiland-Jørgensen 		weight_sum_reciprocal = air_info->weight_reciprocal;
20032433647bSToke Høiland-Jørgensen 	}
20042433647bSToke Høiland-Jørgensen 
20052433647bSToke Høiland-Jørgensen 	/* Round the calculation of global vt */
20062433647bSToke Høiland-Jørgensen 	air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) *
20072433647bSToke Høiland-Jørgensen 				weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64;
20082433647bSToke Høiland-Jørgensen 	air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) *
20092433647bSToke Høiland-Jørgensen 			       air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32;
20102433647bSToke Høiland-Jørgensen 	ieee80211_resort_txq(&local->hw, txq);
20112433647bSToke Høiland-Jørgensen 
20122433647bSToke Høiland-Jørgensen 	spin_unlock_bh(&air_sched->lock);
20132433647bSToke Høiland-Jørgensen }
20142433647bSToke Høiland-Jørgensen 
2015b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2016b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2017b4809e94SToke Høiland-Jørgensen {
20182433647bSToke Høiland-Jørgensen 	struct ieee80211_txq *txq = pubsta->txq[tid];
2019b4809e94SToke Høiland-Jørgensen 
20202433647bSToke Høiland-Jørgensen 	if (!txq)
20212433647bSToke Høiland-Jørgensen 		return;
2022b4809e94SToke Høiland-Jørgensen 
20232433647bSToke Høiland-Jørgensen 	ieee80211_register_airtime(txq, tx_airtime, rx_airtime);
2024b4809e94SToke Høiland-Jørgensen }
2025b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2026b4809e94SToke Høiland-Jørgensen 
20273ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
20283ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
20293ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
20303ace10f5SKan Yan {
20313ace10f5SKan Yan 	int tx_pending;
20323ace10f5SKan Yan 
2033911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2034911bde0fSToke Høiland-Jørgensen 		return;
2035911bde0fSToke Høiland-Jørgensen 
20363ace10f5SKan Yan 	if (!tx_completed) {
20373ace10f5SKan Yan 		if (sta)
20383ace10f5SKan Yan 			atomic_add(tx_airtime,
20393ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
20403ace10f5SKan Yan 
20413ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
20423ace10f5SKan Yan 		return;
20433ace10f5SKan Yan 	}
20443ace10f5SKan Yan 
20453ace10f5SKan Yan 	if (sta) {
20463ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
20473ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
204804e35caaSFelix Fietkau 		if (tx_pending < 0)
20493ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
20503ace10f5SKan Yan 				       tx_pending, 0);
20513ace10f5SKan Yan 	}
20523ace10f5SKan Yan 
20533ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
20543ace10f5SKan Yan 				       &local->aql_total_pending_airtime);
20553ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
20563ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
20573ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
20583ace10f5SKan Yan 		      tx_airtime))
20593ace10f5SKan Yan 		atomic_cmpxchg(&local->aql_total_pending_airtime,
20603ace10f5SKan Yan 			       tx_pending, 0);
20613ace10f5SKan Yan }
20623ace10f5SKan Yan 
206383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2064d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2065d9a7ddb0SJohannes Berg {
20668bf11d8dSJohannes Berg 	might_sleep();
2067d9a7ddb0SJohannes Berg 
2068d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2069d9a7ddb0SJohannes Berg 		return 0;
2070d9a7ddb0SJohannes Berg 
2071f09603a2SJohannes Berg 	/* check allowed transitions first */
2072f09603a2SJohannes Berg 
2073d9a7ddb0SJohannes Berg 	switch (new_state) {
2074d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2075f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2076d9a7ddb0SJohannes Berg 			return -EINVAL;
2077d9a7ddb0SJohannes Berg 		break;
2078d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2079f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2080f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2081d9a7ddb0SJohannes Berg 			return -EINVAL;
2082d9a7ddb0SJohannes Berg 		break;
2083d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2084f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2085f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2086d9a7ddb0SJohannes Berg 			return -EINVAL;
2087d9a7ddb0SJohannes Berg 		break;
2088d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2089f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2090d9a7ddb0SJohannes Berg 			return -EINVAL;
2091d9a7ddb0SJohannes Berg 		break;
2092d9a7ddb0SJohannes Berg 	default:
2093d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2094d9a7ddb0SJohannes Berg 		return -EINVAL;
2095d9a7ddb0SJohannes Berg 	}
2096d9a7ddb0SJohannes Berg 
2097bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2098bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2099f09603a2SJohannes Berg 
2100f09603a2SJohannes Berg 	/*
2101f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2102f09603a2SJohannes Berg 	 * fail the transition
2103f09603a2SJohannes Berg 	 */
2104f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2105f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2106f09603a2SJohannes Berg 					sta->sta_state, new_state);
2107f09603a2SJohannes Berg 		if (err)
2108f09603a2SJohannes Berg 			return err;
2109f09603a2SJohannes Berg 	}
2110f09603a2SJohannes Berg 
2111f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2112f09603a2SJohannes Berg 
2113f09603a2SJohannes Berg 	switch (new_state) {
2114f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2115f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2116f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2117f09603a2SJohannes Berg 		break;
2118f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2119a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2120f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2121a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2122f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2123a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
212452cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
212552cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2126a7201a6cSIlan Peer 		}
2127f09603a2SJohannes Berg 		break;
2128f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2129f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2130f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
21319cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
2132a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
213352cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
213452cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2135f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
213672f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2137f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
213817c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
213949ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2140f09603a2SJohannes Berg 		}
2141f09603a2SJohannes Berg 		break;
2142f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2143f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
214472f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2145f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
214617c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
214749ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2148f09603a2SJohannes Berg 		}
21493e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
21503e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
21513e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
21523e493173SJouni Malinen 						    sta->sta.addr);
2153f09603a2SJohannes Berg 		break;
2154f09603a2SJohannes Berg 	default:
2155f09603a2SJohannes Berg 		break;
2156f09603a2SJohannes Berg 	}
2157f09603a2SJohannes Berg 
2158d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2159d9a7ddb0SJohannes Berg 
2160d9a7ddb0SJohannes Berg 	return 0;
2161d9a7ddb0SJohannes Berg }
2162687da132SEmmanuel Grumbach 
2163c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2164c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2165c9c5962bSJohannes Berg {
2166046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2167c9c5962bSJohannes Berg 	int cpu;
2168c9c5962bSJohannes Berg 
2169046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2170c9c5962bSJohannes Berg 		return stats;
2171c9c5962bSJohannes Berg 
2172c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2173c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2174c9c5962bSJohannes Berg 
2175046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2176c9c5962bSJohannes Berg 
2177c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2178c9c5962bSJohannes Berg 			stats = cpustats;
2179c9c5962bSJohannes Berg 	}
2180c9c5962bSJohannes Berg 
2181c9c5962bSJohannes Berg 	return stats;
2182c9c5962bSJohannes Berg }
2183c9c5962bSJohannes Berg 
218441cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
21854f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2186fbd6ff5cSJohannes Berg {
2187dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2188fbd6ff5cSJohannes Berg 
2189dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
21907f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
21914f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2192dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2193dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2194dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2195dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
21967f406cd1SJohannes Berg 		break;
21977f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
21984f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2199dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2200dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2201dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22027f406cd1SJohannes Berg 		break;
22037f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2204fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2205fbd6ff5cSJohannes Berg 		u16 brate;
22064f6b1b3dSJohannes Berg 		unsigned int shift;
2207dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2208dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2209fbd6ff5cSJohannes Berg 
2210dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
22118b783d10SThomas Pedersen 
22128b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
22138b783d10SThomas Pedersen 			break;
22148b783d10SThomas Pedersen 
2215dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
22164f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
22174f6b1b3dSJohannes Berg 			shift = 2;
22184f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
22194f6b1b3dSJohannes Berg 			shift = 1;
22204f6b1b3dSJohannes Berg 		else
22214f6b1b3dSJohannes Berg 			shift = 0;
2222fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
22237f406cd1SJohannes Berg 		break;
22247f406cd1SJohannes Berg 		}
222541cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
222641cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
222741cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
222841cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
222941cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
223041cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
223141cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
223241cbb0f5SLuca Coelho 		break;
2233fbd6ff5cSJohannes Berg 	}
22344f6b1b3dSJohannes Berg }
2235fbd6ff5cSJohannes Berg 
2236a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
22374f6b1b3dSJohannes Berg {
22386aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
22394f6b1b3dSJohannes Berg 
22404f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2241a17d93ffSBen Greear 		return -EINVAL;
2242a17d93ffSBen Greear 
22434f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2244a17d93ffSBen Greear 	return 0;
2245fbd6ff5cSJohannes Berg }
2246fbd6ff5cSJohannes Berg 
2247b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2248b255b72bSSeevalamuthu Mariappan 					int tid)
2249b255b72bSSeevalamuthu Mariappan {
2250b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2251b255b72bSSeevalamuthu Mariappan 	u64 value;
2252b255b72bSSeevalamuthu Mariappan 
2253b255b72bSSeevalamuthu Mariappan 	do {
2254b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2255b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2256b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2257b255b72bSSeevalamuthu Mariappan 
2258b255b72bSSeevalamuthu Mariappan 	return value;
2259b255b72bSSeevalamuthu Mariappan }
2260b255b72bSSeevalamuthu Mariappan 
22610f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
22620f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
22630f9c5a61SJohannes Berg 			     int tid)
22640f9c5a61SJohannes Berg {
22650f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2266b255b72bSSeevalamuthu Mariappan 	int cpu;
22670f9c5a61SJohannes Berg 
22680f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2269046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2270046d2e7cSSriram R 							   tid);
22710f9c5a61SJohannes Berg 
2272046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2273b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2274b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2275b255b72bSSeevalamuthu Mariappan 
2276046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2277046d2e7cSSriram R 						     cpu);
2278b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2279b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2280b255b72bSSeevalamuthu Mariappan 			}
2281b255b72bSSeevalamuthu Mariappan 		}
22820f9c5a61SJohannes Berg 
22830f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
22840f9c5a61SJohannes Berg 	}
22850f9c5a61SJohannes Berg 
22860f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
22870f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2288046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
22890f9c5a61SJohannes Berg 	}
22900f9c5a61SJohannes Berg 
22910f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
22920f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22930f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2294046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
22950f9c5a61SJohannes Berg 	}
22960f9c5a61SJohannes Berg 
22970f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
22980f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22990f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2300046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
23010f9c5a61SJohannes Berg 	}
23022fe4a29aSToke Høiland-Jørgensen 
23032fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
23042fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
23052fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
23062fe4a29aSToke Høiland-Jørgensen 
23072fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
23082fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
23092fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
23102fe4a29aSToke Høiland-Jørgensen 
23112fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
23122fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
23132fe4a29aSToke Høiland-Jørgensen 	}
23140f9c5a61SJohannes Berg }
23150f9c5a61SJohannes Berg 
2316c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2317c9c5962bSJohannes Berg {
2318c9c5962bSJohannes Berg 	unsigned int start;
2319c9c5962bSJohannes Berg 	u64 value;
2320c9c5962bSJohannes Berg 
2321c9c5962bSJohannes Berg 	do {
2322c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2323c9c5962bSJohannes Berg 		value = rxstats->bytes;
2324c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2325c9c5962bSJohannes Berg 
2326c9c5962bSJohannes Berg 	return value;
2327c9c5962bSJohannes Berg }
2328c9c5962bSJohannes Berg 
23290fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
23300fdf1493SJohannes Berg 		   bool tidstats)
2331b7ffbd7eSJohannes Berg {
2332b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2333b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2334b7ffbd7eSJohannes Berg 	u32 thr = 0;
2335c9c5962bSJohannes Berg 	int i, ac, cpu;
2336c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2337c9c5962bSJohannes Berg 
2338c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2339b7ffbd7eSJohannes Berg 
2340b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2341b7ffbd7eSJohannes Berg 
2342225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2343225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2344225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2345225b8189SJohannes Berg 	 */
2346225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2347bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2348225b8189SJohannes Berg 
23492b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2350a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2351a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2352a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2353a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
23549cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2355a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2356976bd9efSJohannes Berg 
2357976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2358bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2359bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2360a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2361976bd9efSJohannes Berg 	}
2362b7ffbd7eSJohannes Berg 
236384b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
23649cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2365e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2366b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
23672b9a7e1bSJohannes Berg 
2368a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2369a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2370b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
23712b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2372046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2373a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2374b7ffbd7eSJohannes Berg 	}
23752b9a7e1bSJohannes Berg 
2376a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
23772b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
23782b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2379046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2380a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
23812b9a7e1bSJohannes Berg 	}
23822b9a7e1bSJohannes Berg 
2383a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2384a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2385046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
23860f9c5a61SJohannes Berg 
2387046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2388c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2389c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2390c9c5962bSJohannes Berg 
2391046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2392046d2e7cSSriram R 						     cpu);
2393c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2394c9c5962bSJohannes Berg 			}
2395c9c5962bSJohannes Berg 		}
2396c9c5962bSJohannes Berg 
2397a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
23982b9a7e1bSJohannes Berg 	}
23992b9a7e1bSJohannes Berg 
2400a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2401046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2402046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2403c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2404c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2405c9c5962bSJohannes Berg 
2406046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2407046d2e7cSSriram R 						     cpu);
2408c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2409c9c5962bSJohannes Berg 			}
2410c9c5962bSJohannes Berg 		}
2411a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
24122b9a7e1bSJohannes Berg 	}
24132b9a7e1bSJohannes Berg 
2414a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2415046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2416a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
24172b9a7e1bSJohannes Berg 	}
24182b9a7e1bSJohannes Berg 
2419a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2420046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2421a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
24222b9a7e1bSJohannes Berg 	}
24232b9a7e1bSJohannes Berg 
2424b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2425b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2426b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2427b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2428b4809e94SToke Høiland-Jørgensen 	}
2429b4809e94SToke Høiland-Jørgensen 
2430b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2431b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2432b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2433b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2434b4809e94SToke Høiland-Jørgensen 	}
2435b4809e94SToke Høiland-Jørgensen 
2436b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
24372433647bSToke Høiland-Jørgensen 		sinfo->airtime_weight = sta->airtime[0].weight;
2438b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2439b4809e94SToke Høiland-Jørgensen 	}
2440b4809e94SToke Høiland-Jørgensen 
2441046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2442046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2443c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2444c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2445c9c5962bSJohannes Berg 
2446046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2447e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2448c9c5962bSJohannes Berg 		}
2449c9c5962bSJohannes Berg 	}
2450b7ffbd7eSJohannes Berg 
2451225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2452225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2453a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2454a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2455225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2456225b8189SJohannes Berg 	}
2457225b8189SJohannes Berg 
245830686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
245930686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2460a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2461c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2462a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2463b7ffbd7eSJohannes Berg 		}
24642b9a7e1bSJohannes Berg 
2465046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2466a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
246740d9a38aSJohannes Berg 			sinfo->signal_avg =
2468046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2469a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
24702b9a7e1bSJohannes Berg 		}
24712b9a7e1bSJohannes Berg 	}
24722b9a7e1bSJohannes Berg 
2473c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2474c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2475c9c5962bSJohannes Berg 	 * pcpu statistics
2476c9c5962bSJohannes Berg 	 */
2477c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2478a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2479a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2480a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2481046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2482a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2483b7ffbd7eSJohannes Berg 
2484c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2485c9c5962bSJohannes Berg 
2486b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2487e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2488c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2489b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2490046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2491b7ffbd7eSJohannes Berg 		}
2492b7ffbd7eSJohannes Berg 	}
2493b7ffbd7eSJohannes Berg 
2494a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2495046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2496e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2497a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
24982b9a7e1bSJohannes Berg 	}
24992b9a7e1bSJohannes Berg 
2500a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2501a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2502a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
25032b9a7e1bSJohannes Berg 	}
2504b7ffbd7eSJohannes Berg 
25050fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
25066af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
25076af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
25088689c051SArend van Spriel 	}
250979c892b8SJohannes Berg 
2510b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2511b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2512a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2513a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2514a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2515a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2516a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2517dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
25181303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
25191303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2520b7ffbd7eSJohannes Berg 
2521433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2522433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2523433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2524b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2525a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2526433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2527b7ffbd7eSJohannes Berg 		}
2528433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2529433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2530433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2531dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
25321303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2533b7ffbd7eSJohannes Berg #endif
2534b7ffbd7eSJohannes Berg 	}
2535b7ffbd7eSJohannes Berg 
2536b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2537b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2538b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2539b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2540b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2541b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2542b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2543785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2544b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2545b7ffbd7eSJohannes Berg 
2546b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2547b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2548b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2549b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2550b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2551b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2552b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2553b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2554b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2555b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2556b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2557b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2558a74a8c84SJohannes Berg 	if (sta->sta.wme)
2559b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2560b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2561b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2562b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2563b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2564b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2565b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2566b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2567b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2568b7ffbd7eSJohannes Berg 
25693b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
25703b17fbf8SMaxim Altshul 
25713b17fbf8SMaxim Altshul 	if (thr != 0) {
2572a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
25733b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
25743b17fbf8SMaxim Altshul 	}
2575a78b26ffSVenkateswara Naralasetty 
2576a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2577046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2578046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2579a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2580a78b26ffSVenkateswara Naralasetty 	}
2581cc60dbbfSBalaji Pothunoori 
25829c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2583046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2584cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2585cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2586046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2587cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
25889c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2589cc60dbbfSBalaji Pothunoori 	}
2590ab60633cSNarayanraddi Masti 
2591ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2592ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2593ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2594ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2595ab60633cSNarayanraddi Masti 	}
25963b17fbf8SMaxim Altshul }
25973b17fbf8SMaxim Altshul 
25983b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
25993b17fbf8SMaxim Altshul {
26003b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
26013b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
26023b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
26033b17fbf8SMaxim Altshul 	u32 thr = 0;
26043b17fbf8SMaxim Altshul 
26053b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
26063b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
26073b17fbf8SMaxim Altshul 
2608b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2609b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2610b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2611b7ffbd7eSJohannes Berg 	else
26124fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2613b7ffbd7eSJohannes Berg 
26143b17fbf8SMaxim Altshul 	return thr;
2615b7ffbd7eSJohannes Berg }
2616b8da6b6aSJohannes Berg 
2617b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2618b8da6b6aSJohannes Berg {
2619c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2620c9c5962bSJohannes Berg 
2621046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2622046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2623c9c5962bSJohannes Berg 		return stats->last_rx;
2624046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2625b8da6b6aSJohannes Berg }
2626484a54c2SToke Høiland-Jørgensen 
2627484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2628484a54c2SToke Høiland-Jørgensen {
2629484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2630484a54c2SToke Høiland-Jørgensen 		return;
2631484a54c2SToke Høiland-Jørgensen 
2632484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2633484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2634484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2635484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2636484a54c2SToke Høiland-Jørgensen 	} else {
2637484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2638484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2639484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2640484a54c2SToke Høiland-Jørgensen 	}
2641484a54c2SToke Høiland-Jørgensen }
2642484a54c2SToke Høiland-Jørgensen 
2643484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2644484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2645484a54c2SToke Høiland-Jørgensen {
2646484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2647484a54c2SToke Høiland-Jørgensen 
2648484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2649484a54c2SToke Høiland-Jørgensen }
2650*cb71f1d1SJohannes Berg 
2651*cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2652*cb71f1d1SJohannes Berg {
2653*cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2654*cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2655*cb71f1d1SJohannes Berg 	int ret;
2656*cb71f1d1SJohannes Berg 
2657*cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2658*cb71f1d1SJohannes Berg 
2659*cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2660*cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2661*cb71f1d1SJohannes Berg 		return -EINVAL;
2662*cb71f1d1SJohannes Berg 
2663*cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2664*cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2665*cb71f1d1SJohannes Berg 		return -EBUSY;
2666*cb71f1d1SJohannes Berg 
2667*cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2668*cb71f1d1SJohannes Berg 	if (!alloc)
2669*cb71f1d1SJohannes Berg 		return -ENOMEM;
2670*cb71f1d1SJohannes Berg 
2671*cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2672*cb71f1d1SJohannes Berg 	if (ret) {
2673*cb71f1d1SJohannes Berg 		kfree(alloc);
2674*cb71f1d1SJohannes Berg 		return ret;
2675*cb71f1d1SJohannes Berg 	}
2676*cb71f1d1SJohannes Berg 
2677*cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2678*cb71f1d1SJohannes Berg 
2679*cb71f1d1SJohannes Berg 	return 0;
2680*cb71f1d1SJohannes Berg }
2681*cb71f1d1SJohannes Berg 
2682*cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2683*cb71f1d1SJohannes Berg {
2684*cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2685*cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2686*cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2687*cb71f1d1SJohannes Berg 	int ret;
2688*cb71f1d1SJohannes Berg 
2689*cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2690*cb71f1d1SJohannes Berg 
2691*cb71f1d1SJohannes Berg 	if (WARN_ON(old_links == new_links || !sta->link[link_id]))
2692*cb71f1d1SJohannes Berg 		return -EINVAL;
2693*cb71f1d1SJohannes Berg 
2694*cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2695*cb71f1d1SJohannes Berg 
2696*cb71f1d1SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2697*cb71f1d1SJohannes Berg 		return 0;
2698*cb71f1d1SJohannes Berg 
2699*cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2700*cb71f1d1SJohannes Berg 				   old_links, new_links);
2701*cb71f1d1SJohannes Berg 	if (ret) {
2702*cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2703*cb71f1d1SJohannes Berg 		sta_remove_link(sta, link_id);
2704*cb71f1d1SJohannes Berg 	}
2705*cb71f1d1SJohannes Berg 
2706*cb71f1d1SJohannes Berg 	return ret;
2707*cb71f1d1SJohannes Berg }
2708*cb71f1d1SJohannes Berg 
2709*cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2710*cb71f1d1SJohannes Berg {
2711*cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2712*cb71f1d1SJohannes Berg 
2713*cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2714*cb71f1d1SJohannes Berg 
2715*cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2716*cb71f1d1SJohannes Berg 
2717*cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2718*cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2719*cb71f1d1SJohannes Berg 				     sta->sta.valid_links,
2720*cb71f1d1SJohannes Berg 				     sta->sta.valid_links & ~BIT(link_id));
2721*cb71f1d1SJohannes Berg 
2722*cb71f1d1SJohannes Berg 	sta_remove_link(sta, link_id);
2723*cb71f1d1SJohannes Berg }
2724