xref: /openbmc/linux/net/mac80211/sta_info.c (revision b8b80770b26c4591f20f1cde3328e5f1489c4488)
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
710a7ba92SJohannes Berg  * Copyright (C) 2018-2023 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 
67cb71f1d1SJohannes Berg struct sta_link_alloc {
68cb71f1d1SJohannes Berg 	struct link_sta_info info;
69cb71f1d1SJohannes Berg 	struct ieee80211_link_sta sta;
70c71420dbSJohannes Berg 	struct rcu_head rcu_head;
71cb71f1d1SJohannes Berg };
72cb71f1d1SJohannes Berg 
737bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
747bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
75caf22d31SJohannes Berg 	.automatic_shrinking = true,
767bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
77ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
787bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
79ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
807bedd0cfSJohannes Berg };
817bedd0cfSJohannes Berg 
82ba6ddab9SJohannes Berg static const struct rhashtable_params link_sta_rht_params = {
83ba6ddab9SJohannes Berg 	.nelem_hint = 3, /* start small */
84ba6ddab9SJohannes Berg 	.automatic_shrinking = true,
85ba6ddab9SJohannes Berg 	.head_offset = offsetof(struct link_sta_info, link_hash_node),
86ba6ddab9SJohannes Berg 	.key_offset = offsetof(struct link_sta_info, addr),
87ba6ddab9SJohannes Berg 	.key_len = ETH_ALEN,
88ba6ddab9SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89ba6ddab9SJohannes Berg };
90ba6ddab9SJohannes Berg 
914d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
92be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
93f0706e82SJiri Benc 			     struct sta_info *sta)
94f0706e82SJiri Benc {
9583e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
967bedd0cfSJohannes Berg 			       sta_rht_params);
97f0706e82SJiri Benc }
98f0706e82SJiri Benc 
99f36fe0a2SJohannes Berg static int link_sta_info_hash_add(struct ieee80211_local *local,
100f36fe0a2SJohannes Berg 				  struct link_sta_info *link_sta)
101f36fe0a2SJohannes Berg {
102177577dbSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
103f36fe0a2SJohannes Berg 	return rhltable_insert(&local->link_sta_hash,
104f36fe0a2SJohannes Berg 			       &link_sta->link_hash_node,
105f36fe0a2SJohannes Berg 			       link_sta_rht_params);
106f36fe0a2SJohannes Berg }
107f36fe0a2SJohannes Berg 
108ba6ddab9SJohannes Berg static int link_sta_info_hash_del(struct ieee80211_local *local,
109ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
110ba6ddab9SJohannes Berg {
111177577dbSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
112ba6ddab9SJohannes Berg 	return rhltable_remove(&local->link_sta_hash,
113ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
114ba6ddab9SJohannes Berg 			       link_sta_rht_params);
115ba6ddab9SJohannes Berg }
116ba6ddab9SJohannes Berg 
1175108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
118b22cfcfcSEliad Peller {
119b22cfcfcSEliad Peller 	int ac, i;
120b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
121b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
122b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
123d012a605SMarco Porsch 	struct ps_data *ps;
124b22cfcfcSEliad Peller 
125e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1265ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1275ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
128d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
129d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
130d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1313f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1323f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
133d012a605SMarco Porsch 		else
134d012a605SMarco Porsch 			return;
135b22cfcfcSEliad Peller 
136b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
137e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1385ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
139b22cfcfcSEliad Peller 
140d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
141b22cfcfcSEliad Peller 	}
142b22cfcfcSEliad Peller 
143ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
144adf8ed01SJohannes Berg 		struct txq_info *txqi;
145adf8ed01SJohannes Berg 
146adf8ed01SJohannes Berg 		if (!sta->sta.txq[i])
147adf8ed01SJohannes Berg 			continue;
148adf8ed01SJohannes Berg 
149adf8ed01SJohannes Berg 		txqi = to_txq_info(sta->sta.txq[i]);
150ba8c3d6fSFelix Fietkau 
151fa962b92SMichal Kazior 		ieee80211_txq_purge(local, txqi);
152ba8c3d6fSFelix Fietkau 	}
153ba8c3d6fSFelix Fietkau 
154b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
155b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1561f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1571f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
158b22cfcfcSEliad Peller 	}
159b22cfcfcSEliad Peller 
16045b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
16145b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
162b22cfcfcSEliad Peller 
1635ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
164b22cfcfcSEliad Peller 
165b22cfcfcSEliad Peller 	/*
166b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
167b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
168b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
169b22cfcfcSEliad Peller 	 * directly by station destruction.
170b22cfcfcSEliad Peller 	 */
1715a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
172661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
173b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
174b22cfcfcSEliad Peller 		if (!tid_tx)
175b22cfcfcSEliad Peller 			continue;
1761f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
177b22cfcfcSEliad Peller 		kfree(tid_tx);
178b22cfcfcSEliad Peller 	}
1795108ca82SJohannes Berg }
180b22cfcfcSEliad Peller 
1815108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1825108ca82SJohannes Berg {
1835108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1845108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1855108ca82SJohannes Berg 
1865108ca82SJohannes Berg 	__cleanup_single_sta(sta);
187b22cfcfcSEliad Peller 	sta_info_free(local, sta);
188b22cfcfcSEliad Peller }
189b22cfcfcSEliad Peller 
19083e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
19183e7e4ceSHerbert Xu 					 const u8 *addr)
19283e7e4ceSHerbert Xu {
19383e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
19483e7e4ceSHerbert Xu }
19583e7e4ceSHerbert Xu 
196d0709a65SJohannes Berg /* protected by RCU */
197abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
198abe60632SJohannes Berg 			      const u8 *addr)
19943ba7e95SJohannes Berg {
200abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
20183e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
20260f4b626SJohannes Berg 	struct sta_info *sta;
20343ba7e95SJohannes Berg 
20460f4b626SJohannes Berg 	rcu_read_lock();
20583e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
20660f4b626SJohannes Berg 		if (sta->sdata == sdata) {
20760f4b626SJohannes Berg 			rcu_read_unlock();
20860f4b626SJohannes Berg 			/* this is safe as the caller must already hold
20960f4b626SJohannes Berg 			 * another rcu read section or the mutex
21060f4b626SJohannes Berg 			 */
21160f4b626SJohannes Berg 			return sta;
21260f4b626SJohannes Berg 		}
21360f4b626SJohannes Berg 	}
21460f4b626SJohannes Berg 	rcu_read_unlock();
21560f4b626SJohannes Berg 	return NULL;
21643ba7e95SJohannes Berg }
21743ba7e95SJohannes Berg 
2180e5ded5aSFelix Fietkau /*
2190e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
2200e5ded5aSFelix Fietkau  * or from one of its vlans
2210e5ded5aSFelix Fietkau  */
2220e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
2230e5ded5aSFelix Fietkau 				  const u8 *addr)
2240e5ded5aSFelix Fietkau {
2250e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
22683e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
2270e5ded5aSFelix Fietkau 	struct sta_info *sta;
2280e5ded5aSFelix Fietkau 
2297bedd0cfSJohannes Berg 	rcu_read_lock();
23083e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2317bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2327bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2337bedd0cfSJohannes Berg 			rcu_read_unlock();
2347bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2357bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2367bedd0cfSJohannes Berg 			 */
2370e5ded5aSFelix Fietkau 			return sta;
2380e5ded5aSFelix Fietkau 		}
2397bedd0cfSJohannes Berg 	}
2407bedd0cfSJohannes Berg 	rcu_read_unlock();
2417bedd0cfSJohannes Berg 	return NULL;
2427bedd0cfSJohannes Berg }
2430e5ded5aSFelix Fietkau 
244ba6ddab9SJohannes Berg struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
245ba6ddab9SJohannes Berg 					      const u8 *addr)
246ba6ddab9SJohannes Berg {
247ba6ddab9SJohannes Berg 	return rhltable_lookup(&local->link_sta_hash, addr,
248ba6ddab9SJohannes Berg 			       link_sta_rht_params);
249ba6ddab9SJohannes Berg }
250ba6ddab9SJohannes Berg 
251ba6ddab9SJohannes Berg struct link_sta_info *
252ba6ddab9SJohannes Berg link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
253ba6ddab9SJohannes Berg {
254ba6ddab9SJohannes Berg 	struct ieee80211_local *local = sdata->local;
255ba6ddab9SJohannes Berg 	struct rhlist_head *tmp;
256ba6ddab9SJohannes Berg 	struct link_sta_info *link_sta;
257ba6ddab9SJohannes Berg 
258ba6ddab9SJohannes Berg 	rcu_read_lock();
259ba6ddab9SJohannes Berg 	for_each_link_sta_info(local, addr, link_sta, tmp) {
260ba6ddab9SJohannes Berg 		struct sta_info *sta = link_sta->sta;
261ba6ddab9SJohannes Berg 
262ba6ddab9SJohannes Berg 		if (sta->sdata == sdata ||
263ba6ddab9SJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
264ba6ddab9SJohannes Berg 			rcu_read_unlock();
265ba6ddab9SJohannes Berg 			/* this is safe as the caller must already hold
266ba6ddab9SJohannes Berg 			 * another rcu read section or the mutex
267ba6ddab9SJohannes Berg 			 */
268ba6ddab9SJohannes Berg 			return link_sta;
269ba6ddab9SJohannes Berg 		}
270ba6ddab9SJohannes Berg 	}
271ba6ddab9SJohannes Berg 	rcu_read_unlock();
272ba6ddab9SJohannes Berg 	return NULL;
273ba6ddab9SJohannes Berg }
274ba6ddab9SJohannes Berg 
275ffa9598eSJohannes Berg struct ieee80211_sta *
276ffa9598eSJohannes Berg ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
277ffa9598eSJohannes Berg 				 const u8 *addr,
278ffa9598eSJohannes Berg 				 const u8 *localaddr,
279ffa9598eSJohannes Berg 				 unsigned int *link_id)
280ffa9598eSJohannes Berg {
281ffa9598eSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
282ffa9598eSJohannes Berg 	struct link_sta_info *link_sta;
283ffa9598eSJohannes Berg 	struct rhlist_head *tmp;
284ffa9598eSJohannes Berg 
285ffa9598eSJohannes Berg 	for_each_link_sta_info(local, addr, link_sta, tmp) {
286ffa9598eSJohannes Berg 		struct sta_info *sta = link_sta->sta;
287ffa9598eSJohannes Berg 		struct ieee80211_link_data *link;
288ffa9598eSJohannes Berg 		u8 _link_id = link_sta->link_id;
289ffa9598eSJohannes Berg 
290ffa9598eSJohannes Berg 		if (!localaddr) {
291ffa9598eSJohannes Berg 			if (link_id)
292ffa9598eSJohannes Berg 				*link_id = _link_id;
293ffa9598eSJohannes Berg 			return &sta->sta;
294ffa9598eSJohannes Berg 		}
295ffa9598eSJohannes Berg 
296ffa9598eSJohannes Berg 		link = rcu_dereference(sta->sdata->link[_link_id]);
297ffa9598eSJohannes Berg 		if (!link)
298ffa9598eSJohannes Berg 			continue;
299ffa9598eSJohannes Berg 
300ffa9598eSJohannes Berg 		if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
301ffa9598eSJohannes Berg 			continue;
302ffa9598eSJohannes Berg 
303ffa9598eSJohannes Berg 		if (link_id)
304ffa9598eSJohannes Berg 			*link_id = _link_id;
305ffa9598eSJohannes Berg 		return &sta->sta;
306ffa9598eSJohannes Berg 	}
307ffa9598eSJohannes Berg 
308ffa9598eSJohannes Berg 	return NULL;
309ffa9598eSJohannes Berg }
310ffa9598eSJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
311ffa9598eSJohannes Berg 
3125072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
3135072f73cSToke Høiland-Jørgensen 				       const u8 *sta_addr, const u8 *vif_addr)
3145072f73cSToke Høiland-Jørgensen {
3155072f73cSToke Høiland-Jørgensen 	struct rhlist_head *tmp;
3165072f73cSToke Høiland-Jørgensen 	struct sta_info *sta;
3175072f73cSToke Høiland-Jørgensen 
3185072f73cSToke Høiland-Jørgensen 	for_each_sta_info(local, sta_addr, sta, tmp) {
3195072f73cSToke Høiland-Jørgensen 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
3205072f73cSToke Høiland-Jørgensen 			return sta;
3215072f73cSToke Høiland-Jørgensen 	}
3225072f73cSToke Høiland-Jørgensen 
3235072f73cSToke Høiland-Jørgensen 	return NULL;
3245072f73cSToke Høiland-Jørgensen }
3255072f73cSToke Høiland-Jørgensen 
3263b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
3273b53fde8SJohannes Berg 				     int idx)
328ee385855SLuis Carlos Cobo {
3293b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
330ee385855SLuis Carlos Cobo 	struct sta_info *sta;
331ee385855SLuis Carlos Cobo 	int i = 0;
332ee385855SLuis Carlos Cobo 
3338ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
3348ca47eb9SMadhuparna Bhowmik 				lockdep_is_held(&local->sta_mtx)) {
3353b53fde8SJohannes Berg 		if (sdata != sta->sdata)
3362a8ca29aSLuis Carlos Cobo 			continue;
337ee385855SLuis Carlos Cobo 		if (i < idx) {
338ee385855SLuis Carlos Cobo 			++i;
339ee385855SLuis Carlos Cobo 			continue;
340ee385855SLuis Carlos Cobo 		}
3412a8ca29aSLuis Carlos Cobo 		return sta;
342ee385855SLuis Carlos Cobo 	}
343ee385855SLuis Carlos Cobo 
344ee385855SLuis Carlos Cobo 	return NULL;
345ee385855SLuis Carlos Cobo }
346f0706e82SJiri Benc 
347cb71f1d1SJohannes Berg static void sta_info_free_link(struct link_sta_info *link_sta)
348246b39e4SJohannes Berg {
349cb71f1d1SJohannes Berg 	free_percpu(link_sta->pcpu_rx_stats);
350cb71f1d1SJohannes Berg }
351246b39e4SJohannes Berg 
352ba6ddab9SJohannes Berg static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
353ba6ddab9SJohannes Berg 			    bool unhash)
354cb71f1d1SJohannes Berg {
355cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc = NULL;
356c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
357cb71f1d1SJohannes Berg 
358*b8b80770SBenjamin Berg 	link_sta = rcu_access_pointer(sta->link[link_id]);
359*b8b80770SBenjamin Berg 	if (link_sta != &sta->deflink)
360*b8b80770SBenjamin Berg 		lockdep_assert_held(&sta->local->sta_mtx);
361c71420dbSJohannes Berg 
362c71420dbSJohannes Berg 	if (WARN_ON(!link_sta))
363cb71f1d1SJohannes Berg 		return;
364246b39e4SJohannes Berg 
365ba6ddab9SJohannes Berg 	if (unhash)
366ba6ddab9SJohannes Berg 		link_sta_info_hash_del(sta->local, link_sta);
367ba6ddab9SJohannes Berg 
368d2caad52SBenjamin Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
369d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_remove(link_sta);
370d2caad52SBenjamin Berg 
371c71420dbSJohannes Berg 	if (link_sta != &sta->deflink)
372c71420dbSJohannes Berg 		alloc = container_of(link_sta, typeof(*alloc), info);
373cb71f1d1SJohannes Berg 
374cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
375c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->link[link_id], NULL);
376c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
377cb71f1d1SJohannes Berg 	if (alloc) {
378cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
379c71420dbSJohannes Berg 		kfree_rcu(alloc, rcu_head);
380246b39e4SJohannes Berg 	}
3814c51541dSBenjamin Berg 
3824c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
383246b39e4SJohannes Berg }
384246b39e4SJohannes Berg 
38593e5deb1SJohannes Berg /**
386d9a7ddb0SJohannes Berg  * sta_info_free - free STA
38793e5deb1SJohannes Berg  *
3886ef307bcSRandy Dunlap  * @local: pointer to the global information
38993e5deb1SJohannes Berg  * @sta: STA info to free
39093e5deb1SJohannes Berg  *
39193e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
392d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
393d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
394d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
39593e5deb1SJohannes Berg  */
396d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
39793e5deb1SJohannes Berg {
398cb71f1d1SJohannes Berg 	int i;
399cb71f1d1SJohannes Berg 
400cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
401cb71f1d1SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
402cb71f1d1SJohannes Berg 			continue;
403cb71f1d1SJohannes Berg 
4040ad49045SJohannes Berg 		sta_remove_link(sta, i, false);
405cb71f1d1SJohannes Berg 	}
406cb71f1d1SJohannes Berg 
407dcd479e1SJohannes Berg 	/*
408dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
409dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
410dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
411dcd479e1SJohannes Berg 	 *
412dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
413dcd479e1SJohannes Berg 	 * allocated during state transitions.
414dcd479e1SJohannes Berg 	 */
415dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
416dcd479e1SJohannes Berg 		int ret;
417dcd479e1SJohannes Berg 
418dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
419dcd479e1SJohannes Berg 
420dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
421dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
422dcd479e1SJohannes Berg 			break;
423dcd479e1SJohannes Berg 	}
424dcd479e1SJohannes Berg 
425889cbb91SJohannes Berg 	if (sta->rate_ctrl)
4264b7679a5SJohannes Berg 		rate_control_free_sta(sta);
42793e5deb1SJohannes Berg 
428bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
42993e5deb1SJohannes Berg 
430ba8c3d6fSFelix Fietkau 	kfree(to_txq_info(sta->sta.txq[0]));
43153d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
432433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
433433f5bc1SJohannes Berg 	kfree(sta->mesh);
434433f5bc1SJohannes Berg #endif
435246b39e4SJohannes Berg 
436cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
43793e5deb1SJohannes Berg 	kfree(sta);
43893e5deb1SJohannes Berg }
43993e5deb1SJohannes Berg 
4404d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
44162b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
442d0709a65SJohannes Berg 			     struct sta_info *sta)
443f0706e82SJiri Benc {
44483e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
4457bedd0cfSJohannes Berg 			       sta_rht_params);
446f0706e82SJiri Benc }
447f0706e82SJiri Benc 
4485ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
449af818581SJohannes Berg {
450af818581SJohannes Berg 	struct sta_info *sta;
451af818581SJohannes Berg 
4525ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
453af818581SJohannes Berg 
454af818581SJohannes Berg 	if (sta->dead)
455af818581SJohannes Berg 		return;
456af818581SJohannes Berg 
45754420473SHelmut Schaa 	local_bh_disable();
4585ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
459af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
4605ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
461af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
4625ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
46347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
464ce662b44SJohannes Berg 	local_bh_enable();
465af818581SJohannes Berg }
466af818581SJohannes Berg 
467af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
468af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
469af65cd96SJohannes Berg {
47030686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
471af65cd96SJohannes Berg 		return 0;
472af65cd96SJohannes Berg 
473889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
474af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
47535c347acSJohannes Berg 						     sta, gfp);
476889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
477af65cd96SJohannes Berg 		return -ENOMEM;
478af65cd96SJohannes Berg 
479af65cd96SJohannes Berg 	return 0;
480af65cd96SJohannes Berg }
481af65cd96SJohannes Berg 
482cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
483246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
484246b39e4SJohannes Berg 			       gfp_t gfp)
485246b39e4SJohannes Berg {
486246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
487246b39e4SJohannes Berg 	int i;
488246b39e4SJohannes Berg 
489246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
490246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
491246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
492246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
493246b39e4SJohannes Berg 			return -ENOMEM;
494246b39e4SJohannes Berg 	}
495246b39e4SJohannes Berg 
496246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
497246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
498246b39e4SJohannes Berg 
499246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
500246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
501246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
502246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
503246b39e4SJohannes Berg 
504246b39e4SJohannes Berg 	return 0;
505246b39e4SJohannes Berg }
506246b39e4SJohannes Berg 
507cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
508cb71f1d1SJohannes Berg 			      unsigned int link_id,
509cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
510cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
511cb71f1d1SJohannes Berg {
512cb71f1d1SJohannes Berg 	link_info->sta = sta;
513cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
514c71420dbSJohannes Berg 	link_info->pub = link_sta;
5151d9e4c91SBenjamin Berg 	link_info->pub->sta = &sta->sta;
516c73993b8SJohannes Berg 	link_sta->link_id = link_id;
517c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
518c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
519261ce887SBenjamin Berg 
520261ce887SBenjamin Berg 	link_sta->smps_mode = IEEE80211_SMPS_OFF;
5214c51541dSBenjamin Berg 	link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
522cb71f1d1SJohannes Berg }
523cb71f1d1SJohannes Berg 
524f36fe0a2SJohannes Berg static struct sta_info *
525f36fe0a2SJohannes Berg __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
526f36fe0a2SJohannes Berg 		 const u8 *addr, int link_id, const u8 *link_addr,
527f36fe0a2SJohannes Berg 		 gfp_t gfp)
528f0706e82SJiri Benc {
529d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
530ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
531f0706e82SJiri Benc 	struct sta_info *sta;
532107395f9SAlexander Wetzel 	void *txq_data;
533107395f9SAlexander Wetzel 	int size;
53416c5f15cSRon Rindjunsky 	int i;
535f0706e82SJiri Benc 
536ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
537f0706e82SJiri Benc 	if (!sta)
53873651ee6SJohannes Berg 		return NULL;
539f0706e82SJiri Benc 
540246b39e4SJohannes Berg 	sta->local = local;
541246b39e4SJohannes Berg 	sta->sdata = sdata;
542246b39e4SJohannes Berg 
543cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
54436fe8e4eSLorenzo Bianconi 		goto free;
545c9c5962bSJohannes Berg 
546cb71f1d1SJohannes Berg 	if (link_id >= 0) {
547cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
548cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
549cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
550cb71f1d1SJohannes Berg 	} else {
551cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
552cb71f1d1SJohannes Berg 	}
553cb71f1d1SJohannes Berg 
5544c51541dSBenjamin Berg 	sta->sta.cur = &sta->sta.deflink.agg;
5554c51541dSBenjamin Berg 
55607346f81SJohannes Berg 	spin_lock_init(&sta->lock);
5571d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
5585ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
55967c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
560a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
56187f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
562433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
563433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
564433f5bc1SJohannes Berg 		if (!sta->mesh)
565433f5bc1SJohannes Berg 			goto free;
5664c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
567433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
56845d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5694c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5704c02d62fSKees Cook 				    0);
571433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
572433f5bc1SJohannes Berg 	}
57387f59c70SThomas Pedersen #endif
57407346f81SJohannes Berg 
575ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
57617741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
577f36fe0a2SJohannes Berg 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
578f36fe0a2SJohannes Berg 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
579480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
580480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
581480dd46bSMaxim Altshul 
582046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
583046d2e7cSSriram R 
58496fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
58596fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
58696fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
58796fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
58896fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
58996fc6efbSAlexander Wetzel 	 */
59096fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
59196fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
59296fc6efbSAlexander Wetzel 
5930f9c5a61SJohannes Berg 
5943a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5953a11ce08SJohannes Berg 
59671ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
59771ec375cSJohannes Berg 
5986e4c0d04SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
5996e4c0d04SFelix Fietkau 		sta->amsdu_mesh_control = -1;
6006e4c0d04SFelix Fietkau 
601b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
602b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
603b6da911bSLiad Kaufman 
60484b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
605541a45a1SBruno Randolf 
606107395f9SAlexander Wetzel 	size = sizeof(struct txq_info) +
607ba8c3d6fSFelix Fietkau 	       ALIGN(hw->txq_data_size, sizeof(void *));
608ba8c3d6fSFelix Fietkau 
609ba8c3d6fSFelix Fietkau 	txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
610ba8c3d6fSFelix Fietkau 	if (!txq_data)
611ba8c3d6fSFelix Fietkau 		goto free;
612ba8c3d6fSFelix Fietkau 
613ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
614ba8c3d6fSFelix Fietkau 		struct txq_info *txq = txq_data + i * size;
615ba8c3d6fSFelix Fietkau 
616107395f9SAlexander Wetzel 		/* might not do anything for the (bufferable) MMPDU TXQ */
617fa962b92SMichal Kazior 		ieee80211_txq_init(sdata, sta, txq, i);
618abfbc3afSJohannes Berg 	}
619ba8c3d6fSFelix Fietkau 
620ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
621ba8c3d6fSFelix Fietkau 		goto free_txq;
622f0706e82SJiri Benc 
623942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
624b4809e94SToke Høiland-Jørgensen 
625948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
626948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
627948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
628942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
629942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
630942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
631942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
632948d887dSJohannes Berg 	}
63373651ee6SJohannes Berg 
6345a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
6354be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
636cccaec98SSenthil Balasubramanian 
637bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
638bd718fc1SJohannes Berg 		u32 mandatory = 0;
639bd718fc1SJohannes Berg 		int r;
640bd718fc1SJohannes Berg 
641bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
642bd718fc1SJohannes Berg 			continue;
643bd718fc1SJohannes Berg 
644bd718fc1SJohannes Berg 		switch (i) {
645bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
64663fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
647bd718fc1SJohannes Berg 			/*
648bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
649bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
650bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
651bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
652bd718fc1SJohannes Berg 			 * possible rate anyway.
653bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
654bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
655bd718fc1SJohannes Berg 			 */
656bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
657bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
658bd718fc1SJohannes Berg 			break;
659bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
660bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
661bd718fc1SJohannes Berg 			break;
662bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
663bd718fc1SJohannes Berg 			WARN_ON(1);
664bd718fc1SJohannes Berg 			mandatory = 0;
665bd718fc1SJohannes Berg 			break;
666bd718fc1SJohannes Berg 		}
667bd718fc1SJohannes Berg 
668bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
669bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
670bd718fc1SJohannes Berg 
671bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
672bd718fc1SJohannes Berg 
673bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
674bd718fc1SJohannes Berg 				continue;
675046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
676bd718fc1SJohannes Berg 		}
677bd718fc1SJohannes Berg 	}
678bd718fc1SJohannes Berg 
679484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
680484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
681484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
682484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
683dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
684dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
685484a54c2SToke Høiland-Jørgensen 
686bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
687ef04a297SJohannes Berg 
688abfbc3afSJohannes Berg 	return sta;
689ba8c3d6fSFelix Fietkau 
690ba8c3d6fSFelix Fietkau free_txq:
691ba8c3d6fSFelix Fietkau 	kfree(to_txq_info(sta->sta.txq[0]));
692ba8c3d6fSFelix Fietkau free:
693cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
694433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
695433f5bc1SJohannes Berg 	kfree(sta->mesh);
696433f5bc1SJohannes Berg #endif
697ba8c3d6fSFelix Fietkau 	kfree(sta);
698ba8c3d6fSFelix Fietkau 	return NULL;
69973651ee6SJohannes Berg }
70073651ee6SJohannes Berg 
701f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
702f36fe0a2SJohannes Berg 				const u8 *addr, gfp_t gfp)
703f36fe0a2SJohannes Berg {
704f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
705f36fe0a2SJohannes Berg }
706f36fe0a2SJohannes Berg 
707f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
708f36fe0a2SJohannes Berg 					  const u8 *mld_addr,
709f36fe0a2SJohannes Berg 					  unsigned int link_id,
710f36fe0a2SJohannes Berg 					  const u8 *link_addr,
711f36fe0a2SJohannes Berg 					  gfp_t gfp)
712f36fe0a2SJohannes Berg {
713f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
714f36fe0a2SJohannes Berg }
715f36fe0a2SJohannes Berg 
7168c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
71734e89507SJohannes Berg {
71834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
71934e89507SJohannes Berg 
72003e4497eSJohannes Berg 	/*
72103e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
72203e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
72303e4497eSJohannes Berg 	 * and another CPU turns off the net device.
72403e4497eSJohannes Berg 	 */
7258c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
7268c71df7aSGuy Eilam 		return -ENETDOWN;
72703e4497eSJohannes Berg 
728b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
729deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
7308c71df7aSGuy Eilam 		return -EINVAL;
7318c71df7aSGuy Eilam 
73283e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
73383e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
73483e7e4ceSHerbert Xu 	 * for correctness.
73531104891SJohannes Berg 	 */
73631104891SJohannes Berg 	rcu_read_lock();
73731104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
73831104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
73931104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
74031104891SJohannes Berg 		rcu_read_unlock();
74131104891SJohannes Berg 		return -ENOTUNIQ;
74231104891SJohannes Berg 	}
74331104891SJohannes Berg 	rcu_read_unlock();
74431104891SJohannes Berg 
7458c71df7aSGuy Eilam 	return 0;
74693e5deb1SJohannes Berg }
74744213b5eSJohannes Berg 
748f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
749f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
750f09603a2SJohannes Berg 				     struct sta_info *sta)
751f09603a2SJohannes Berg {
752f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
753f09603a2SJohannes Berg 	int err = 0;
754f09603a2SJohannes Berg 
755f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
756f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
757f09603a2SJohannes Berg 		if (err)
758f09603a2SJohannes Berg 			break;
759f09603a2SJohannes Berg 	}
760f09603a2SJohannes Berg 
761f09603a2SJohannes Berg 	if (!err) {
762a4ec45a4SJohannes Berg 		/*
763a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
764a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
765a4ec45a4SJohannes Berg 		 */
766a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
767f09603a2SJohannes Berg 			sta->uploaded = true;
768f09603a2SJohannes Berg 		return 0;
769f09603a2SJohannes Berg 	}
770f09603a2SJohannes Berg 
771f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
772bdcbd8e0SJohannes Berg 		sdata_info(sdata,
773bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
774bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
775f09603a2SJohannes Berg 		err = 0;
776f09603a2SJohannes Berg 	}
777f09603a2SJohannes Berg 
778f09603a2SJohannes Berg 	/* unwind on error */
779f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
780f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
781f09603a2SJohannes Berg 
782f09603a2SJohannes Berg 	return err;
783f09603a2SJohannes Berg }
784f09603a2SJohannes Berg 
785d405fd8cSGregory Greenman static void
786d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
787d405fd8cSGregory Greenman {
788d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
789d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
790d405fd8cSGregory Greenman 	struct sta_info *sta;
791d405fd8cSGregory Greenman 
792d405fd8cSGregory Greenman 	rcu_read_lock();
793d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
794d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
795d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
796d405fd8cSGregory Greenman 			continue;
797d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
798d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
799d405fd8cSGregory Greenman 			break;
800d405fd8cSGregory Greenman 		}
801d405fd8cSGregory Greenman 	}
802d405fd8cSGregory Greenman 	rcu_read_unlock();
803d405fd8cSGregory Greenman 
804d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
805d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
806d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
807d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
808d405fd8cSGregory Greenman 	}
809d405fd8cSGregory Greenman }
810d405fd8cSGregory Greenman 
81134e89507SJohannes Berg /*
8128c71df7aSGuy Eilam  * should be called with sta_mtx locked
8138c71df7aSGuy Eilam  * this function replaces the mutex lock
8148c71df7aSGuy Eilam  * with a RCU lock
8158c71df7aSGuy Eilam  */
8164d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
8178c71df7aSGuy Eilam {
8188c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
8198c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8200c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
8218c71df7aSGuy Eilam 	int err = 0;
8228c71df7aSGuy Eilam 
8238c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
8248c71df7aSGuy Eilam 
8257852e361SJohannes Berg 	/* check if STA exists already */
8267852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
8274d33960bSJohannes Berg 		err = -EEXIST;
8288f9dcc29SAhmed Zaki 		goto out_cleanup;
82934e89507SJohannes Berg 	}
83034e89507SJohannes Berg 
8310c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
8320c2e3842SKoen Vandeputte 	if (!sinfo) {
8330c2e3842SKoen Vandeputte 		err = -ENOMEM;
8348f9dcc29SAhmed Zaki 		goto out_cleanup;
8350c2e3842SKoen Vandeputte 	}
8360c2e3842SKoen Vandeputte 
8374d33960bSJohannes Berg 	local->num_sta++;
8384d33960bSJohannes Berg 	local->sta_generation++;
8394d33960bSJohannes Berg 	smp_mb();
8404d33960bSJohannes Berg 
8415108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
8425108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
8435108ca82SJohannes Berg 
8444d33960bSJohannes Berg 	/* make the station visible */
84562b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
84662b14b24SJohannes Berg 	if (err)
84762b14b24SJohannes Berg 		goto out_drop_sta;
8484d33960bSJohannes Berg 
849f36fe0a2SJohannes Berg 	if (sta->sta.valid_links) {
850f36fe0a2SJohannes Berg 		err = link_sta_info_hash_add(local, &sta->deflink);
851f36fe0a2SJohannes Berg 		if (err) {
852f36fe0a2SJohannes Berg 			sta_info_hash_del(local, sta);
853f36fe0a2SJohannes Berg 			goto out_drop_sta;
854f36fe0a2SJohannes Berg 		}
855f36fe0a2SJohannes Berg 	}
856f36fe0a2SJohannes Berg 
8572bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
85883d5cc01SJohannes Berg 
8594dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8604dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8614dde3c36SMordechay Goodstein 	 */
8624dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8630cbf348aSAndrei Otcheretianski 		ieee80211_recalc_min_chandef(sta->sdata, -1);
8644dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8654dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8664dde3c36SMordechay Goodstein 	}
8674dde3c36SMordechay Goodstein 
8685108ca82SJohannes Berg 	/* notify driver */
8695108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8705108ca82SJohannes Berg 	if (err)
8715108ca82SJohannes Berg 		goto out_remove;
8725108ca82SJohannes Berg 
87383d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
874d405fd8cSGregory Greenman 
8755108ca82SJohannes Berg 	/* accept BA sessions now */
8765108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8774d33960bSJohannes Berg 
8784d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8794d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
880d2caad52SBenjamin Berg 	if (sta->sta.valid_links) {
881d2caad52SBenjamin Berg 		int i;
882d2caad52SBenjamin Berg 
883d2caad52SBenjamin Berg 		for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
884d2caad52SBenjamin Berg 			struct link_sta_info *link_sta;
885d2caad52SBenjamin Berg 
886d2caad52SBenjamin Berg 			link_sta = rcu_dereference_protected(sta->link[i],
887d2caad52SBenjamin Berg 							     lockdep_is_held(&local->sta_mtx));
888d2caad52SBenjamin Berg 
889d2caad52SBenjamin Berg 			if (!link_sta)
890d2caad52SBenjamin Berg 				continue;
891d2caad52SBenjamin Berg 
892d2caad52SBenjamin Berg 			ieee80211_link_sta_debugfs_add(link_sta);
893d2caad52SBenjamin Berg 			if (sdata->vif.active_links & BIT(i))
894d2caad52SBenjamin Berg 				ieee80211_link_sta_debugfs_drv_add(link_sta);
895d2caad52SBenjamin Berg 		}
896d2caad52SBenjamin Berg 	} else {
897d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_add(&sta->deflink);
898d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
899d2caad52SBenjamin Berg 	}
9004d33960bSJohannes Berg 
9010ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
9020ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
9030ef049dcSArnd Bergmann 	kfree(sinfo);
904d0709a65SJohannes Berg 
905bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
906f0706e82SJiri Benc 
90734e89507SJohannes Berg 	/* move reference to rcu-protected */
90834e89507SJohannes Berg 	rcu_read_lock();
90934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
910e9f207f0SJiri Benc 
91173651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
91273651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
91373651ee6SJohannes Berg 
91473651ee6SJohannes Berg 	return 0;
9155108ca82SJohannes Berg  out_remove:
9160ad49045SJohannes Berg 	if (sta->sta.valid_links)
9170ad49045SJohannes Berg 		link_sta_info_hash_del(local, &sta->deflink);
9185108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
9195108ca82SJohannes Berg 	list_del_rcu(&sta->list);
92062b14b24SJohannes Berg  out_drop_sta:
9215108ca82SJohannes Berg 	local->num_sta--;
9225108ca82SJohannes Berg 	synchronize_net();
9238f9dcc29SAhmed Zaki  out_cleanup:
9247bc40aedSJohannes Berg 	cleanup_single_sta(sta);
9254d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
926ea32f065SSudip Mukherjee 	kfree(sinfo);
9274d33960bSJohannes Berg 	rcu_read_lock();
9284d33960bSJohannes Berg 	return err;
9298c71df7aSGuy Eilam }
9308c71df7aSGuy Eilam 
9318c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
9328c71df7aSGuy Eilam {
9338c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
934308f7fcfSZhao, Gang 	int err;
9358c71df7aSGuy Eilam 
9364d33960bSJohannes Berg 	might_sleep();
9374d33960bSJohannes Berg 
93831104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
93931104891SJohannes Berg 
9408c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
9418c71df7aSGuy Eilam 	if (err) {
9427bc40aedSJohannes Berg 		sta_info_free(local, sta);
94331104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
9448c71df7aSGuy Eilam 		rcu_read_lock();
9457bc40aedSJohannes Berg 		return err;
9468c71df7aSGuy Eilam 	}
9478c71df7aSGuy Eilam 
9487bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
949f0706e82SJiri Benc }
950f0706e82SJiri Benc 
95134e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
95234e89507SJohannes Berg {
95334e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
95434e89507SJohannes Berg 
95534e89507SJohannes Berg 	rcu_read_unlock();
95634e89507SJohannes Berg 
95734e89507SJohannes Berg 	return err;
95834e89507SJohannes Berg }
95934e89507SJohannes Berg 
960d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
961004c872eSJohannes Berg {
962004c872eSJohannes Berg 	/*
963004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
964004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
965004c872eSJohannes Berg 	 */
966d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
967004c872eSJohannes Berg }
968004c872eSJohannes Berg 
969d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
970004c872eSJohannes Berg {
971004c872eSJohannes Berg 	/*
972004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
973004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
974004c872eSJohannes Berg 	 */
975d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
976004c872eSJohannes Berg }
977004c872eSJohannes Berg 
9783d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9793d5839b6SIlan Peer {
9803d5839b6SIlan Peer 	/*
9813d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9823d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9833d5839b6SIlan Peer 	 */
9843d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9853d5839b6SIlan Peer }
9863d5839b6SIlan Peer 
987948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
988004c872eSJohannes Berg {
989948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
990948d887dSJohannes Berg 	switch (ac) {
991948d887dSJohannes Berg 	case IEEE80211_AC_VO:
992948d887dSJohannes Berg 		return BIT(6) | BIT(7);
993948d887dSJohannes Berg 	case IEEE80211_AC_VI:
994948d887dSJohannes Berg 		return BIT(4) | BIT(5);
995948d887dSJohannes Berg 	case IEEE80211_AC_BE:
996948d887dSJohannes Berg 		return BIT(0) | BIT(3);
997948d887dSJohannes Berg 	case IEEE80211_AC_BK:
998948d887dSJohannes Berg 		return BIT(1) | BIT(2);
999948d887dSJohannes Berg 	default:
1000948d887dSJohannes Berg 		WARN_ON(1);
1001948d887dSJohannes Berg 		return 0;
1002d0709a65SJohannes Berg 	}
1003004c872eSJohannes Berg }
1004004c872eSJohannes Berg 
10059b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
1006004c872eSJohannes Berg {
1007c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
1008d012a605SMarco Porsch 	struct ps_data *ps;
1009948d887dSJohannes Berg 	bool indicate_tim = false;
1010948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
1011948d887dSJohannes Berg 	int ac;
1012a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
1013004c872eSJohannes Berg 
1014d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1015d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1016c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
1017c868cb35SJohannes Berg 			return;
10183e122be0SJohannes Berg 
1019d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
10203f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
10213f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
10223f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
10233f52b7e3SMarco Porsch #endif
1024d012a605SMarco Porsch 	} else {
1025d012a605SMarco Porsch 		return;
1026d012a605SMarco Porsch 	}
1027d012a605SMarco Porsch 
1028c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
1029d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1030c868cb35SJohannes Berg 		return;
1031004c872eSJohannes Berg 
1032c868cb35SJohannes Berg 	if (sta->dead)
1033c868cb35SJohannes Berg 		goto done;
10343e122be0SJohannes Berg 
1035948d887dSJohannes Berg 	/*
1036948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
1037948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
1038948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
1039948d887dSJohannes Berg 	 * non-enabled ones.
1040948d887dSJohannes Berg 	 */
1041948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1042948d887dSJohannes Berg 		ignore_for_tim = 0;
1043948d887dSJohannes Berg 
10449b7a86f3SJohannes Berg 	if (ignore_pending)
10459b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
10469b7a86f3SJohannes Berg 
1047948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1048948d887dSJohannes Berg 		unsigned long tids;
1049948d887dSJohannes Berg 
1050f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1051948d887dSJohannes Berg 			continue;
1052948d887dSJohannes Berg 
1053948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1054948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
1055948d887dSJohannes Berg 		if (indicate_tim)
1056948d887dSJohannes Berg 			break;
1057948d887dSJohannes Berg 
1058948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
1059948d887dSJohannes Berg 
1060948d887dSJohannes Berg 		indicate_tim |=
1061948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
1062ba8c3d6fSFelix Fietkau 		indicate_tim |=
1063ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
1064004c872eSJohannes Berg 	}
1065004c872eSJohannes Berg 
1066c868cb35SJohannes Berg  done:
106765f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
1068004c872eSJohannes Berg 
10693d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
10703d5839b6SIlan Peer 		goto out_unlock;
10713d5839b6SIlan Peer 
1072948d887dSJohannes Berg 	if (indicate_tim)
1073d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1074c868cb35SJohannes Berg 	else
1075d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10763e122be0SJohannes Berg 
10779b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1078c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1079948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1080c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1081004c872eSJohannes Berg 	}
1082004c872eSJohannes Berg 
10833d5839b6SIlan Peer out_unlock:
108465f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1085004c872eSJohannes Berg }
1086004c872eSJohannes Berg 
10879b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10889b7a86f3SJohannes Berg {
10899b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10909b7a86f3SJohannes Berg }
10919b7a86f3SJohannes Berg 
1092cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1093f0706e82SJiri Benc {
1094e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1095f0706e82SJiri Benc 	int timeout;
1096f0706e82SJiri Benc 
1097f0706e82SJiri Benc 	if (!skb)
1098cd0b8d89SJohannes Berg 		return false;
1099f0706e82SJiri Benc 
1100e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1101f0706e82SJiri Benc 
1102f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
110357c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
110457c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
110557c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1106f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1107f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1108e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1109f0706e82SJiri Benc }
1110f0706e82SJiri Benc 
1111f0706e82SJiri Benc 
1112948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1113948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1114f0706e82SJiri Benc {
1115f0706e82SJiri Benc 	unsigned long flags;
1116f0706e82SJiri Benc 	struct sk_buff *skb;
1117f0706e82SJiri Benc 
111860750397SJohannes Berg 	/*
111960750397SJohannes Berg 	 * First check for frames that should expire on the filtered
112060750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
112160750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
112260750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
112360750397SJohannes Berg 	 * total_ps_buffered counter.
112460750397SJohannes Berg 	 */
1125f0706e82SJiri Benc 	for (;;) {
1126948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1127948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
112857c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1129948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1130836341a7SJohannes Berg 		else
1131f0706e82SJiri Benc 			skb = NULL;
1132948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1133f0706e82SJiri Benc 
113460750397SJohannes Berg 		/*
113560750397SJohannes Berg 		 * Frames are queued in order, so if this one
113660750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
113760750397SJohannes Berg 		 * we actually reached the end of the queue we
113860750397SJohannes Berg 		 * also need to stop, of course.
113960750397SJohannes Berg 		 */
114060750397SJohannes Berg 		if (!skb)
114160750397SJohannes Berg 			break;
1142d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
114360750397SJohannes Berg 	}
114460750397SJohannes Berg 
114560750397SJohannes Berg 	/*
114660750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
114760750397SJohannes Berg 	 * only find something if the filtered queue was emptied
114860750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
114960750397SJohannes Berg 	 * buffered frames.
115060750397SJohannes Berg 	 */
1151f0706e82SJiri Benc 	for (;;) {
1152948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1153948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1154f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1155948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1156f0706e82SJiri Benc 		else
1157f0706e82SJiri Benc 			skb = NULL;
1158948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1159f0706e82SJiri Benc 
116060750397SJohannes Berg 		/*
116160750397SJohannes Berg 		 * frames are queued in order, so if this one
116260750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
116360750397SJohannes Berg 		 * the queue) we can stop testing
116460750397SJohannes Berg 		 */
1165836341a7SJohannes Berg 		if (!skb)
1166836341a7SJohannes Berg 			break;
1167836341a7SJohannes Berg 
1168f0706e82SJiri Benc 		local->total_ps_buffered--;
1169bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1170bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1171d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1172f0706e82SJiri Benc 	}
11733393a608SJuuso Oikarinen 
117460750397SJohannes Berg 	/*
117560750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
117660750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
117760750397SJohannes Berg 	 * frames.
117860750397SJohannes Berg 	 */
117960750397SJohannes Berg 	sta_info_recalc_tim(sta);
118060750397SJohannes Berg 
118160750397SJohannes Berg 	/*
118260750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
118360750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
118460750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
118560750397SJohannes Berg 	 */
1186948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1187948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1188948d887dSJohannes Berg }
1189948d887dSJohannes Berg 
1190948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1191948d887dSJohannes Berg 					     struct sta_info *sta)
1192948d887dSJohannes Berg {
1193948d887dSJohannes Berg 	bool have_buffered = false;
1194948d887dSJohannes Berg 	int ac;
1195948d887dSJohannes Berg 
11963f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11973f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11983f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1199948d887dSJohannes Berg 		return false;
1200948d887dSJohannes Berg 
1201948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1202948d887dSJohannes Berg 		have_buffered |=
1203948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1204948d887dSJohannes Berg 
1205948d887dSJohannes Berg 	return have_buffered;
1206f0706e82SJiri Benc }
1207f0706e82SJiri Benc 
1208d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
120934e89507SJohannes Berg {
121034e89507SJohannes Berg 	struct ieee80211_local *local;
121134e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
12120ad49045SJohannes Berg 	int ret, i;
121334e89507SJohannes Berg 
121434e89507SJohannes Berg 	might_sleep();
121534e89507SJohannes Berg 
121634e89507SJohannes Berg 	if (!sta)
121734e89507SJohannes Berg 		return -ENOENT;
121834e89507SJohannes Berg 
121934e89507SJohannes Berg 	local = sta->local;
122034e89507SJohannes Berg 	sdata = sta->sdata;
122134e89507SJohannes Berg 
122283d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
122383d5cc01SJohannes Berg 
1224098a6070SJohannes Berg 	/*
1225098a6070SJohannes Berg 	 * Before removing the station from the driver and
1226098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1227098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1228098a6070SJohannes Berg 	 * will be sufficient.
1229098a6070SJohannes Berg 	 */
1230c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1231c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1232098a6070SJohannes Berg 
1233f59374ebSSara Sharon 	/*
1234f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1235f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1236f59374ebSSara Sharon 	 * all such frames to be processed.
1237f59374ebSSara Sharon 	 */
1238f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1239f59374ebSSara Sharon 
12400ad49045SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
12410ad49045SJohannes Berg 		struct link_sta_info *link_sta;
12420ad49045SJohannes Berg 
12430ad49045SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
12440ad49045SJohannes Berg 			continue;
12450ad49045SJohannes Berg 
12460ad49045SJohannes Berg 		link_sta = rcu_dereference_protected(sta->link[i],
12470ad49045SJohannes Berg 						     lockdep_is_held(&local->sta_mtx));
12480ad49045SJohannes Berg 
12490ad49045SJohannes Berg 		link_sta_info_hash_del(local, link_sta);
12500ad49045SJohannes Berg 	}
12510ad49045SJohannes Berg 
125234e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1253b01711beSJohannes Berg 	if (WARN_ON(ret))
125434e89507SJohannes Berg 		return ret;
125534e89507SJohannes Berg 
1256a7a6bdd0SArik Nemtsov 	/*
1257a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1258a7a6bdd0SArik Nemtsov 	 * removal.
1259a7a6bdd0SArik Nemtsov 	 */
1260a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1261a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1262a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1263a7a6bdd0SArik Nemtsov 	}
1264a7a6bdd0SArik Nemtsov 
1265794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1266ef044763SEliad Peller 	sta->removed = true;
12674d33960bSJohannes Berg 
126812b220a6SFelix Fietkau 	if (sta->uploaded)
12696a9d1b91SJohannes Berg 		drv_sta_pre_rcu_remove(local, sta->sdata, sta);
12706a9d1b91SJohannes Berg 
1271a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1272a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1273a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1274a710c816SJohannes Berg 
1275d778207bSJohannes Berg 	return 0;
1276d778207bSJohannes Berg }
1277d778207bSJohannes Berg 
127892747f17SJohannes Berg static int _sta_info_move_state(struct sta_info *sta,
127992747f17SJohannes Berg 				enum ieee80211_sta_state new_state,
128092747f17SJohannes Berg 				bool recalc)
128110a7ba92SJohannes Berg {
128210a7ba92SJohannes Berg 	might_sleep();
128310a7ba92SJohannes Berg 
128410a7ba92SJohannes Berg 	if (sta->sta_state == new_state)
128510a7ba92SJohannes Berg 		return 0;
128610a7ba92SJohannes Berg 
128710a7ba92SJohannes Berg 	/* check allowed transitions first */
128810a7ba92SJohannes Berg 
128910a7ba92SJohannes Berg 	switch (new_state) {
129010a7ba92SJohannes Berg 	case IEEE80211_STA_NONE:
129110a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
129210a7ba92SJohannes Berg 			return -EINVAL;
129310a7ba92SJohannes Berg 		break;
129410a7ba92SJohannes Berg 	case IEEE80211_STA_AUTH:
129510a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
129610a7ba92SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
129710a7ba92SJohannes Berg 			return -EINVAL;
129810a7ba92SJohannes Berg 		break;
129910a7ba92SJohannes Berg 	case IEEE80211_STA_ASSOC:
130010a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
130110a7ba92SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
130210a7ba92SJohannes Berg 			return -EINVAL;
130310a7ba92SJohannes Berg 		break;
130410a7ba92SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
130510a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
130610a7ba92SJohannes Berg 			return -EINVAL;
130710a7ba92SJohannes Berg 		break;
130810a7ba92SJohannes Berg 	default:
130910a7ba92SJohannes Berg 		WARN(1, "invalid state %d", new_state);
131010a7ba92SJohannes Berg 		return -EINVAL;
131110a7ba92SJohannes Berg 	}
131210a7ba92SJohannes Berg 
131310a7ba92SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
131410a7ba92SJohannes Berg 		sta->sta.addr, new_state);
131510a7ba92SJohannes Berg 
131610a7ba92SJohannes Berg 	/* notify the driver before the actual changes so it can
131710a7ba92SJohannes Berg 	 * fail the transition
131810a7ba92SJohannes Berg 	 */
131910a7ba92SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
132010a7ba92SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
132110a7ba92SJohannes Berg 					sta->sta_state, new_state);
132210a7ba92SJohannes Berg 		if (err)
132310a7ba92SJohannes Berg 			return err;
132410a7ba92SJohannes Berg 	}
132510a7ba92SJohannes Berg 
132610a7ba92SJohannes Berg 	/* reflect the change in all state variables */
132710a7ba92SJohannes Berg 
132810a7ba92SJohannes Berg 	switch (new_state) {
132910a7ba92SJohannes Berg 	case IEEE80211_STA_NONE:
133010a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
133110a7ba92SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
133210a7ba92SJohannes Berg 		break;
133310a7ba92SJohannes Berg 	case IEEE80211_STA_AUTH:
133410a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE) {
133510a7ba92SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
133610a7ba92SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
133710a7ba92SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
133892747f17SJohannes Berg 			if (recalc) {
133910a7ba92SJohannes Berg 				ieee80211_recalc_min_chandef(sta->sdata, -1);
134010a7ba92SJohannes Berg 				if (!sta->sta.support_p2p_ps)
134110a7ba92SJohannes Berg 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
134210a7ba92SJohannes Berg 			}
134392747f17SJohannes Berg 		}
134410a7ba92SJohannes Berg 		break;
134510a7ba92SJohannes Berg 	case IEEE80211_STA_ASSOC:
134610a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
134710a7ba92SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
134810a7ba92SJohannes Berg 			sta->assoc_at = ktime_get_boottime_ns();
134992747f17SJohannes Berg 			if (recalc) {
135010a7ba92SJohannes Berg 				ieee80211_recalc_min_chandef(sta->sdata, -1);
135110a7ba92SJohannes Berg 				if (!sta->sta.support_p2p_ps)
135210a7ba92SJohannes Berg 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
135392747f17SJohannes Berg 			}
135410a7ba92SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
135510a7ba92SJohannes Berg 			ieee80211_vif_dec_num_mcast(sta->sdata);
135610a7ba92SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
135710a7ba92SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
135810a7ba92SJohannes Berg 			ieee80211_clear_fast_rx(sta);
135910a7ba92SJohannes Berg 		}
136010a7ba92SJohannes Berg 		break;
136110a7ba92SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
136210a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
136310a7ba92SJohannes Berg 			ieee80211_vif_inc_num_mcast(sta->sdata);
136410a7ba92SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
136510a7ba92SJohannes Berg 			ieee80211_check_fast_xmit(sta);
136610a7ba92SJohannes Berg 			ieee80211_check_fast_rx(sta);
136710a7ba92SJohannes Berg 		}
136810a7ba92SJohannes Berg 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
136910a7ba92SJohannes Berg 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
137010a7ba92SJohannes Berg 			cfg80211_send_layer2_update(sta->sdata->dev,
137110a7ba92SJohannes Berg 						    sta->sta.addr);
137210a7ba92SJohannes Berg 		break;
137310a7ba92SJohannes Berg 	default:
137410a7ba92SJohannes Berg 		break;
137510a7ba92SJohannes Berg 	}
137610a7ba92SJohannes Berg 
137710a7ba92SJohannes Berg 	sta->sta_state = new_state;
137810a7ba92SJohannes Berg 
137910a7ba92SJohannes Berg 	return 0;
138010a7ba92SJohannes Berg }
138110a7ba92SJohannes Berg 
138292747f17SJohannes Berg int sta_info_move_state(struct sta_info *sta,
138392747f17SJohannes Berg 			enum ieee80211_sta_state new_state)
138492747f17SJohannes Berg {
138592747f17SJohannes Berg 	return _sta_info_move_state(sta, new_state, true);
138692747f17SJohannes Berg }
138792747f17SJohannes Berg 
138892747f17SJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc)
1389d778207bSJohannes Berg {
1390d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1391d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
13920ef049dcSArnd Bergmann 	struct station_info *sinfo;
1393d778207bSJohannes Berg 	int ret;
1394d778207bSJohannes Berg 
1395d778207bSJohannes Berg 	/*
1396d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1397d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1398d778207bSJohannes Berg 	 */
1399d778207bSJohannes Berg 
1400d778207bSJohannes Berg 	might_sleep();
1401d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1402d778207bSJohannes Berg 
14035981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
140492747f17SJohannes Berg 		ret = _sta_info_move_state(sta, IEEE80211_STA_ASSOC, recalc);
1405b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1406b16798f5SJohannes Berg 	}
1407b16798f5SJohannes Berg 
14080b75a1b1SJohannes Berg 	/* Flush queues before removing keys, as that might remove them
14090b75a1b1SJohannes Berg 	 * from hardware, and then depending on the offload method, any
14100b75a1b1SJohannes Berg 	 * frames sitting on hardware queues might be sent out without
14110b75a1b1SJohannes Berg 	 * any encryption at all.
14120b75a1b1SJohannes Berg 	 */
1413d00800a2SJohannes Berg 	if (local->ops->set_key) {
1414d00800a2SJohannes Berg 		if (local->ops->flush_sta)
1415d00800a2SJohannes Berg 			drv_flush_sta(local, sta->sdata, sta);
1416d00800a2SJohannes Berg 		else
14170b75a1b1SJohannes Berg 			ieee80211_flush_queues(local, sta->sdata, false);
1418d00800a2SJohannes Berg 	}
14190b75a1b1SJohannes Berg 
1420c8782078SJohannes Berg 	/* now keys can no longer be reached */
14216d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
142234e89507SJohannes Berg 
14239b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
14249b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
14259b7a86f3SJohannes Berg 
142634e89507SJohannes Berg 	sta->dead = true;
142734e89507SJohannes Berg 
142834e89507SJohannes Berg 	local->num_sta--;
142934e89507SJohannes Berg 	local->sta_generation++;
143034e89507SJohannes Berg 
143183d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
143292747f17SJohannes Berg 		ret = _sta_info_move_state(sta, sta->sta_state - 1, recalc);
1433f09603a2SJohannes Berg 		if (ret) {
143483d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
143583d5cc01SJohannes Berg 			break;
143683d5cc01SJohannes Berg 		}
143783d5cc01SJohannes Berg 	}
1438d9a7ddb0SJohannes Berg 
1439f09603a2SJohannes Berg 	if (sta->uploaded) {
1440f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1441f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1442f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1443f09603a2SJohannes Berg 	}
144434e89507SJohannes Berg 
1445bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1446bdcbd8e0SJohannes Berg 
14470ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
14480ef049dcSArnd Bergmann 	if (sinfo)
14490fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
14500ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
14510ef049dcSArnd Bergmann 	kfree(sinfo);
1452ec15e68bSJouni Malinen 
145334e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
145434e89507SJohannes Berg 
14553a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
14563a11ce08SJohannes Berg 
1457d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1458d778207bSJohannes Berg }
1459d778207bSJohannes Berg 
1460d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1461d778207bSJohannes Berg {
1462d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1463d778207bSJohannes Berg 
1464d778207bSJohannes Berg 	if (err)
1465d778207bSJohannes Berg 		return err;
1466d778207bSJohannes Berg 
1467d778207bSJohannes Berg 	synchronize_net();
1468d778207bSJohannes Berg 
146992747f17SJohannes Berg 	__sta_info_destroy_part2(sta, true);
147034e89507SJohannes Berg 
147134e89507SJohannes Berg 	return 0;
147234e89507SJohannes Berg }
147334e89507SJohannes Berg 
147434e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
147534e89507SJohannes Berg {
147634e89507SJohannes Berg 	struct sta_info *sta;
147734e89507SJohannes Berg 	int ret;
147834e89507SJohannes Berg 
147934e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
14807852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
148134e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
148234e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
148334e89507SJohannes Berg 
148434e89507SJohannes Berg 	return ret;
148534e89507SJohannes Berg }
148634e89507SJohannes Berg 
148734e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
148834e89507SJohannes Berg 			      const u8 *addr)
148934e89507SJohannes Berg {
149034e89507SJohannes Berg 	struct sta_info *sta;
149134e89507SJohannes Berg 	int ret;
149234e89507SJohannes Berg 
149334e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
14947852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
149534e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
149634e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
149734e89507SJohannes Berg 
149834e89507SJohannes Berg 	return ret;
149934e89507SJohannes Berg }
1500f0706e82SJiri Benc 
150134f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1502f0706e82SJiri Benc {
150334f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1504f0706e82SJiri Benc 	struct sta_info *sta;
15053393a608SJuuso Oikarinen 	bool timer_needed = false;
1506f0706e82SJiri Benc 
1507d0709a65SJohannes Berg 	rcu_read_lock();
1508d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
15093393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
15103393a608SJuuso Oikarinen 			timer_needed = true;
1511d0709a65SJohannes Berg 	rcu_read_unlock();
1512f0706e82SJiri Benc 
15135bb644a0SJohannes Berg 	if (local->quiescing)
15145bb644a0SJohannes Berg 		return;
15155bb644a0SJohannes Berg 
15163393a608SJuuso Oikarinen 	if (!timer_needed)
15173393a608SJuuso Oikarinen 		return;
15183393a608SJuuso Oikarinen 
151926d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
152026d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1521f0706e82SJiri Benc }
1522f0706e82SJiri Benc 
15237bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
15247bedd0cfSJohannes Berg {
15257bedd0cfSJohannes Berg 	int err;
15267bedd0cfSJohannes Berg 
152783e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
15287bedd0cfSJohannes Berg 	if (err)
15297bedd0cfSJohannes Berg 		return err;
15307bedd0cfSJohannes Berg 
1531ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1532ba6ddab9SJohannes Berg 	if (err) {
1533ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1534ba6ddab9SJohannes Berg 		return err;
1535ba6ddab9SJohannes Berg 	}
1536ba6ddab9SJohannes Berg 
15374d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
153834e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1539f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1540f0706e82SJiri Benc 
154134f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
15427bedd0cfSJohannes Berg 	return 0;
1543f0706e82SJiri Benc }
1544f0706e82SJiri Benc 
1545f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1546f0706e82SJiri Benc {
1547a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
154883e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1549ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1550f0706e82SJiri Benc }
1551f0706e82SJiri Benc 
1552051007d9SJohannes Berg 
1553e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1554f0706e82SJiri Benc {
1555b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1556f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1557d778207bSJohannes Berg 	LIST_HEAD(free_list);
155844213b5eSJohannes Berg 	int ret = 0;
1559f0706e82SJiri Benc 
1560d0709a65SJohannes Berg 	might_sleep();
1561d0709a65SJohannes Berg 
1562e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1563e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1564e716251dSJohannes Berg 
156534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
156634e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1567e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1568e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1569d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1570d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
157134316837SJohannes Berg 			ret++;
157234316837SJohannes Berg 		}
157334e89507SJohannes Berg 	}
1574d778207bSJohannes Berg 
1575d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
157692747f17SJohannes Berg 		bool support_p2p_ps = true;
157792747f17SJohannes Berg 
1578d778207bSJohannes Berg 		synchronize_net();
157992747f17SJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list) {
158092747f17SJohannes Berg 			if (!sta->sta.support_p2p_ps)
158192747f17SJohannes Berg 				support_p2p_ps = false;
158292747f17SJohannes Berg 			__sta_info_destroy_part2(sta, false);
158392747f17SJohannes Berg 		}
158492747f17SJohannes Berg 
158592747f17SJohannes Berg 		ieee80211_recalc_min_chandef(sdata, -1);
158692747f17SJohannes Berg 		if (!support_p2p_ps)
158792747f17SJohannes Berg 			ieee80211_recalc_p2p_go_ps_allowed(sdata);
1588d778207bSJohannes Berg 	}
158934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
159044213b5eSJohannes Berg 
1591051007d9SJohannes Berg 	return ret;
1592051007d9SJohannes Berg }
1593051007d9SJohannes Berg 
159424723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
159524723d1bSJohannes Berg 			  unsigned long exp_time)
159624723d1bSJohannes Berg {
159724723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
159824723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
159924723d1bSJohannes Berg 
160034e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1601e46a2cf9SMohammed Shafi Shajakhan 
1602e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1603b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1604b8da6b6aSJohannes Berg 
1605ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1606ec2b774eSMarek Lindner 			continue;
1607ec2b774eSMarek Lindner 
1608b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1609eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1610bdcbd8e0SJohannes Berg 				sta->sta.addr);
16113f52b7e3SMarco Porsch 
16123f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
16133f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
16143f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
16153f52b7e3SMarco Porsch 
161634e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
161724723d1bSJohannes Berg 		}
1618e46a2cf9SMohammed Shafi Shajakhan 	}
1619e46a2cf9SMohammed Shafi Shajakhan 
162034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
162124723d1bSJohannes Berg }
162217741cdcSJohannes Berg 
1623686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1624686b9cb9SBen Greear 						   const u8 *addr,
1625686b9cb9SBen Greear 						   const u8 *localaddr)
162617741cdcSJohannes Berg {
16277bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
162883e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
16297bedd0cfSJohannes Berg 	struct sta_info *sta;
163017741cdcSJohannes Berg 
1631686b9cb9SBen Greear 	/*
1632686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1633686b9cb9SBen Greear 	 * ... first in list.
1634686b9cb9SBen Greear 	 */
163583e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1636686b9cb9SBen Greear 		if (localaddr &&
1637b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1638686b9cb9SBen Greear 			continue;
1639f7c65594SJohannes Berg 		if (!sta->uploaded)
1640f7c65594SJohannes Berg 			return NULL;
164117741cdcSJohannes Berg 		return &sta->sta;
1642f7c65594SJohannes Berg 	}
1643f7c65594SJohannes Berg 
1644abe60632SJohannes Berg 	return NULL;
164517741cdcSJohannes Berg }
1646686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
16475ed176e1SJohannes Berg 
16485ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
16495ed176e1SJohannes Berg 					 const u8 *addr)
16505ed176e1SJohannes Berg {
1651f7c65594SJohannes Berg 	struct sta_info *sta;
16525ed176e1SJohannes Berg 
16535ed176e1SJohannes Berg 	if (!vif)
16545ed176e1SJohannes Berg 		return NULL;
16555ed176e1SJohannes Berg 
1656f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1657f7c65594SJohannes Berg 	if (!sta)
1658f7c65594SJohannes Berg 		return NULL;
16595ed176e1SJohannes Berg 
1660f7c65594SJohannes Berg 	if (!sta->uploaded)
1661f7c65594SJohannes Berg 		return NULL;
1662f7c65594SJohannes Berg 
1663f7c65594SJohannes Berg 	return &sta->sta;
16645ed176e1SJohannes Berg }
166517741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1666af818581SJohannes Berg 
1667e3685e03SJohannes Berg /* powersave support code */
1668e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
166950a9432dSJohannes Berg {
1670608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1671e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1672e3685e03SJohannes Berg 	struct sk_buff_head pending;
1673ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1674e3685e03SJohannes Berg 	unsigned long flags;
1675d012a605SMarco Porsch 	struct ps_data *ps;
1676d012a605SMarco Porsch 
16773918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
16783918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
16793918edb0SFelix Fietkau 				     u.ap);
16803918edb0SFelix Fietkau 
16813918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1682d012a605SMarco Porsch 		ps = &sdata->bss->ps;
16833f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
16843f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1685d012a605SMarco Porsch 	else
1686d012a605SMarco Porsch 		return;
168750a9432dSJohannes Berg 
1688c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
168947086fc5SJohannes Berg 
16905a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1691948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1692ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1693948d887dSJohannes Berg 
169430686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
169512375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1696af818581SJohannes Berg 
1697ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1698adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1699ba8c3d6fSFelix Fietkau 			continue;
1700ba8c3d6fSFelix Fietkau 
170118667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1702ba8c3d6fSFelix Fietkau 	}
1703ba8c3d6fSFelix Fietkau 
1704948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1705af818581SJohannes Berg 
17061d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
17071d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1708af818581SJohannes Berg 	/* Send all buffered frames to the station */
1709948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1710948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1711948d887dSJohannes Berg 
1712987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1713948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1714987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1715948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1716948d887dSJohannes Berg 		filtered += tmp - count;
1717948d887dSJohannes Berg 		count = tmp;
1718948d887dSJohannes Berg 
1719987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1720948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1721987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1722948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1723948d887dSJohannes Berg 		buffered += tmp - count;
1724948d887dSJohannes Berg 	}
1725948d887dSJohannes Berg 
1726e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
17275ac2e350SJohannes Berg 
17285ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
17295ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
17305ac2e350SJohannes Berg 
17315ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
17325ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
17335ac2e350SJohannes Berg 	 */
17345ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
17355ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
17361d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1737948d887dSJohannes Berg 
1738e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1739e3685e03SJohannes Berg 
1740af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1741af818581SJohannes Berg 
1742c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1743c868cb35SJohannes Berg 
1744bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
17452595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1746bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
174717c18bf8SJohannes Berg 
174817c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1749af818581SJohannes Berg }
1750af818581SJohannes Berg 
17510ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1752b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
17530ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1754ce662b44SJohannes Berg {
17550ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1756ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1757ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1758ce662b44SJohannes Berg 	struct sk_buff *skb;
1759ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1760ce662b44SJohannes Berg 	__le16 fc;
1761a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1762ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
176355de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1764ce662b44SJohannes Berg 
1765ce662b44SJohannes Berg 	if (qos) {
1766ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1767ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1768ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1769ce662b44SJohannes Berg 	} else {
1770ce662b44SJohannes Berg 		size -= 2;
1771ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1772ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1773ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1774ce662b44SJohannes Berg 	}
1775ce662b44SJohannes Berg 
1776ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1777ce662b44SJohannes Berg 	if (!skb)
1778ce662b44SJohannes Berg 		return;
1779ce662b44SJohannes Berg 
1780ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1781ce662b44SJohannes Berg 
17824df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1783ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1784ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1785ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1786ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1787ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1788864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1789ce662b44SJohannes Berg 
1790ce662b44SJohannes Berg 	skb->priority = tid;
1791ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
179259b66255SJohannes Berg 	if (qos) {
1793ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1794ce662b44SJohannes Berg 
17950ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1796ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1797ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
17980ead2510SEmmanuel Grumbach 			if (more_data)
17990ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
18000ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
18010ead2510SEmmanuel Grumbach 		}
1802ce662b44SJohannes Berg 	}
1803ce662b44SJohannes Berg 
1804ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1805ce662b44SJohannes Berg 
1806ce662b44SJohannes Berg 	/*
1807ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1808ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1809deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1810deeaee19SJohannes Berg 	 * ends the poll/service period.
1811ce662b44SJohannes Berg 	 */
181202f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1813deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1814ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1815ce662b44SJohannes Berg 
18166b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
18176b127c71SSujith Manoharan 
1818b77cf4f8SJohannes Berg 	if (call_driver)
1819b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1820b77cf4f8SJohannes Berg 					  reason, false);
182140b96408SJohannes Berg 
182289afe614SJohannes Berg 	skb->dev = sdata->dev;
182389afe614SJohannes Berg 
182455de908aSJohannes Berg 	rcu_read_lock();
1825d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
182655de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
182755de908aSJohannes Berg 		rcu_read_unlock();
182855de908aSJohannes Berg 		kfree_skb(skb);
182955de908aSJohannes Berg 		return;
183055de908aSJohannes Berg 	}
183155de908aSJohannes Berg 
183273c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
183308aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
183455de908aSJohannes Berg 	rcu_read_unlock();
1835ce662b44SJohannes Berg }
1836ce662b44SJohannes Berg 
18370a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
18380a1cb809SJohannes Berg {
18390a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
18400a1cb809SJohannes Berg 	if (tids & 0xF8)
18410a1cb809SJohannes Berg 		return fls(tids) - 1;
18420a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
18430a1cb809SJohannes Berg 	if (tids & BIT(0))
18440a1cb809SJohannes Berg 		return 0;
18450a1cb809SJohannes Berg 	return fls(tids) - 1;
18460a1cb809SJohannes Berg }
18470a1cb809SJohannes Berg 
18480ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
18490ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
18500ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
18510ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
18520ead2510SEmmanuel Grumbach  */
18530ead2510SEmmanuel Grumbach static bool
18540ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
18550ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
18560ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
18570ead2510SEmmanuel Grumbach {
18580ead2510SEmmanuel Grumbach 	int ac;
18590ead2510SEmmanuel Grumbach 
18600ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
18610ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
18620ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
18630ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
18640ead2510SEmmanuel Grumbach 	 */
18650ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
18660ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
18670ead2510SEmmanuel Grumbach 		return true;
18680ead2510SEmmanuel Grumbach 
18690ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1870f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
18710ead2510SEmmanuel Grumbach 			continue;
18720ead2510SEmmanuel Grumbach 
18730ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
18740ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
18750ead2510SEmmanuel Grumbach 			return true;
18760ead2510SEmmanuel Grumbach 	}
18770ead2510SEmmanuel Grumbach 
18780ead2510SEmmanuel Grumbach 	return false;
18790ead2510SEmmanuel Grumbach }
18800ead2510SEmmanuel Grumbach 
188147086fc5SJohannes Berg static void
18820ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
18830ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
18840ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
18850ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1886af818581SJohannes Berg {
1887af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1888af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1889948d887dSJohannes Berg 	int ac;
1890af818581SJohannes Berg 
1891f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1892948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
18934049e09aSJohannes Berg 		unsigned long tids;
18944049e09aSJohannes Berg 
1895f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1896948d887dSJohannes Berg 			continue;
1897948d887dSJohannes Berg 
18984049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
18994049e09aSJohannes Berg 
1900f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1901f9f760b4SJohannes Berg 		 * release from hardware queues
1902f9f760b4SJohannes Berg 		 */
19030ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
19040ead2510SEmmanuel Grumbach 			*driver_release_tids |=
19050ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
19060ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1907ba8c3d6fSFelix Fietkau 		}
1908f9f760b4SJohannes Berg 
19090ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
191047086fc5SJohannes Berg 			struct sk_buff *skb;
191147086fc5SJohannes Berg 
191247086fc5SJohannes Berg 			while (n_frames > 0) {
1913948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1914948d887dSJohannes Berg 				if (!skb) {
191547086fc5SJohannes Berg 					skb = skb_dequeue(
191647086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1917af818581SJohannes Berg 					if (skb)
1918af818581SJohannes Berg 						local->total_ps_buffered--;
1919af818581SJohannes Berg 				}
192047086fc5SJohannes Berg 				if (!skb)
192147086fc5SJohannes Berg 					break;
192247086fc5SJohannes Berg 				n_frames--;
19230ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
192447086fc5SJohannes Berg 			}
1925948d887dSJohannes Berg 		}
1926948d887dSJohannes Berg 
19270ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
19280ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
19290ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
19304049e09aSJohannes Berg 		 */
1931948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
19320ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1933948d887dSJohannes Berg 			break;
1934948d887dSJohannes Berg 	}
1935948d887dSJohannes Berg }
1936af818581SJohannes Berg 
19370ead2510SEmmanuel Grumbach static void
19380ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
19390ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
19400ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
19410ead2510SEmmanuel Grumbach {
19420ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
19430ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
19440ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
19450ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
19460ead2510SEmmanuel Grumbach 	bool more_data;
19470ead2510SEmmanuel Grumbach 
19480ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
19490ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
19500ead2510SEmmanuel Grumbach 
19510ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
19520ead2510SEmmanuel Grumbach 
19530ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
19540ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
19550ead2510SEmmanuel Grumbach 
19560ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
19570ead2510SEmmanuel Grumbach 
19581a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
19590ead2510SEmmanuel Grumbach 		driver_release_tids =
19600ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
19610ead2510SEmmanuel Grumbach 
1962f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1963f438ceb8SEmmanuel Grumbach 		int tid, ac;
19644049e09aSJohannes Berg 
1965ce662b44SJohannes Berg 		/*
1966ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1967ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1968ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1969ce662b44SJohannes Berg 		 *
1970ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1971ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1972ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1973ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1974ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1975ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1976ce662b44SJohannes Berg 		 *
1977ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1978ce662b44SJohannes Berg 		 */
1979ce662b44SJohannes Berg 
1980ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1981f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1982d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1983d7f84244SEmmanuel Grumbach 				break;
1984f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1985ce662b44SJohannes Berg 
19860ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1987f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
198847086fc5SJohannes Berg 		struct sk_buff_head pending;
198947086fc5SJohannes Berg 		struct sk_buff *skb;
199040b96408SJohannes Berg 		int num = 0;
199140b96408SJohannes Berg 		u16 tids = 0;
1992b77cf4f8SJohannes Berg 		bool need_null = false;
199347086fc5SJohannes Berg 
199447086fc5SJohannes Berg 		skb_queue_head_init(&pending);
199547086fc5SJohannes Berg 
199647086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1997af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
199847086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
199940b96408SJohannes Berg 			u8 *qoshdr = NULL;
200040b96408SJohannes Berg 
200140b96408SJohannes Berg 			num++;
2002af818581SJohannes Berg 
2003af818581SJohannes Berg 			/*
200447086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
200547086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
200647086fc5SJohannes Berg 			 * exchange.
2007af818581SJohannes Berg 			 */
20086b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
20096b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
2010af818581SJohannes Berg 
201147086fc5SJohannes Berg 			/*
201247086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
201347086fc5SJohannes Berg 			 * more buffered frames for this STA
201447086fc5SJohannes Berg 			 */
201524b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
201647086fc5SJohannes Berg 				hdr->frame_control |=
201747086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
201824b9c373SJanusz.Dziedzic@tieto.com 			else
201924b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
202024b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
2021af818581SJohannes Berg 
202240b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
202340b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
202440b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
202540b96408SJohannes Berg 
2026b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
2027b77cf4f8SJohannes Berg 
2028b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
2029b77cf4f8SJohannes Berg 
2030b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
2031b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
2032b77cf4f8SJohannes Berg 				continue;
2033b77cf4f8SJohannes Berg 
2034b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
2035b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
2036b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
2037b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2038b77cf4f8SJohannes Berg 				break;
2039b77cf4f8SJohannes Berg 			}
2040b77cf4f8SJohannes Berg 
2041b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
2042b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
2043b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
2044b77cf4f8SJohannes Berg 			 * and be done.
2045b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
2046b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
2047b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
2048b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
2049b77cf4f8SJohannes Berg 			 *
2050b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
2051b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
2052b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
2053b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
2054b77cf4f8SJohannes Berg 			 *
2055b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
2056b77cf4f8SJohannes Berg 			 */
2057b77cf4f8SJohannes Berg 			if (qoshdr) {
205840b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
2059deeaee19SJohannes Berg 
206047086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
206147086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2062b77cf4f8SJohannes Berg 			} else {
2063b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
2064b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
2065b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
2066b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
2067b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
2068b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
2069b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
2070b77cf4f8SJohannes Berg 				 */
2071b77cf4f8SJohannes Berg 				hdr->frame_control |=
2072b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2073b77cf4f8SJohannes Berg 				need_null = true;
2074b77cf4f8SJohannes Berg 				num++;
207552a3f20cSMarco Porsch 			}
2076b77cf4f8SJohannes Berg 			break;
207747086fc5SJohannes Berg 		}
207847086fc5SJohannes Berg 
207940b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
208040b96408SJohannes Berg 					  reason, more_data);
208140b96408SJohannes Berg 
208247086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
2083af818581SJohannes Berg 
2084b77cf4f8SJohannes Berg 		if (need_null)
2085b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
20860ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
20870ead2510SEmmanuel Grumbach 				reason, false, false);
2088b77cf4f8SJohannes Berg 
2089c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
2090af818581SJohannes Berg 	} else {
2091ba8c3d6fSFelix Fietkau 		int tid;
2092ba8c3d6fSFelix Fietkau 
2093af818581SJohannes Berg 		/*
20944049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
20954049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
2096f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
2097f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
2098f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
2099f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
2100f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
2101f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
2102af818581SJohannes Berg 		 */
21034049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
210447086fc5SJohannes Berg 					    n_frames, reason, more_data);
21054049e09aSJohannes Berg 
21064049e09aSJohannes Berg 		/*
21074049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
21084049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
2109f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
21104049e09aSJohannes Berg 		 * release function.
2111f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
2112ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
2113ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
21144049e09aSJohannes Berg 		 */
2115ba8c3d6fSFelix Fietkau 
2116ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
2117adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
2118adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
21191e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
2120ba8c3d6fSFelix Fietkau 				continue;
2121ba8c3d6fSFelix Fietkau 
2122ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
2123ba8c3d6fSFelix Fietkau 			break;
2124ba8c3d6fSFelix Fietkau 		}
2125af818581SJohannes Berg 	}
2126af818581SJohannes Berg }
2127af818581SJohannes Berg 
2128af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
2129af818581SJohannes Berg {
213047086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
2131af818581SJohannes Berg 
2132af818581SJohannes Berg 	/*
213347086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
213447086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
213547086fc5SJohannes Berg 	 * only from the non-enabled ones.
2136af818581SJohannes Berg 	 */
213747086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
213847086fc5SJohannes Berg 		ignore_for_response = 0;
2139af818581SJohannes Berg 
214047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
214147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
2142af818581SJohannes Berg }
214347086fc5SJohannes Berg 
214447086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
214547086fc5SJohannes Berg {
214647086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
214747086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
214847086fc5SJohannes Berg 
214947086fc5SJohannes Berg 	/*
215047086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
215147086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
215247086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
215347086fc5SJohannes Berg 	 * actually getting called.
215447086fc5SJohannes Berg 	 */
215547086fc5SJohannes Berg 	if (!delivery_enabled)
215647086fc5SJohannes Berg 		return;
215747086fc5SJohannes Berg 
215847086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
215947086fc5SJohannes Berg 	case 1:
216047086fc5SJohannes Berg 		n_frames = 2;
216147086fc5SJohannes Berg 		break;
216247086fc5SJohannes Berg 	case 2:
216347086fc5SJohannes Berg 		n_frames = 4;
216447086fc5SJohannes Berg 		break;
216547086fc5SJohannes Berg 	case 3:
216647086fc5SJohannes Berg 		n_frames = 6;
216747086fc5SJohannes Berg 		break;
216847086fc5SJohannes Berg 	case 0:
216947086fc5SJohannes Berg 		/* XXX: what is a good value? */
217013a8098aSAndrei Otcheretianski 		n_frames = 128;
217147086fc5SJohannes Berg 		break;
217247086fc5SJohannes Berg 	}
217347086fc5SJohannes Berg 
217447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
217547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
2176af818581SJohannes Berg }
2177af818581SJohannes Berg 
2178af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2179af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
2180af818581SJohannes Berg {
2181af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2182af818581SJohannes Berg 
2183b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
2184b5878a2dSJohannes Berg 
21855ac2e350SJohannes Berg 	if (block) {
2186c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
218717c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
21885ac2e350SJohannes Berg 		return;
21895ac2e350SJohannes Berg 	}
21905ac2e350SJohannes Berg 
21915ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
21925ac2e350SJohannes Berg 		return;
21935ac2e350SJohannes Berg 
21945ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
21955ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
21965ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
21975ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
21985ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
21995ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
22005ac2e350SJohannes Berg 		/* must be asleep in this case */
22015ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
22025ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
22035ac2e350SJohannes Berg 	} else {
22045ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
220517c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
22065ac2e350SJohannes Berg 	}
2207af818581SJohannes Berg }
2208af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
2209dcf55fb5SFelix Fietkau 
2210e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
221137fbd908SJohannes Berg {
221237fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
221337fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
221437fbd908SJohannes Berg 
221537fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
221637fbd908SJohannes Berg 
221737fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
221837fbd908SJohannes Berg }
2219e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
222037fbd908SJohannes Berg 
22210ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
22220ead2510SEmmanuel Grumbach {
22230ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
22240ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
22250ead2510SEmmanuel Grumbach 	bool more_data;
22260ead2510SEmmanuel Grumbach 
22270ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
22280ead2510SEmmanuel Grumbach 
22290ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
22300ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
22310ead2510SEmmanuel Grumbach 					       reason, 0);
22320ead2510SEmmanuel Grumbach 
22330ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
22340ead2510SEmmanuel Grumbach }
22350ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
22360ead2510SEmmanuel Grumbach 
2237042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2238042ec453SJohannes Berg 				u8 tid, bool buffered)
2239dcf55fb5SFelix Fietkau {
2240dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2241dcf55fb5SFelix Fietkau 
22425a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2243042ec453SJohannes Berg 		return;
2244042ec453SJohannes Berg 
22451b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
22461b000789SJohannes Berg 
2247948d887dSJohannes Berg 	if (buffered)
2248948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2249948d887dSJohannes Berg 	else
2250948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2251948d887dSJohannes Berg 
2252c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2253dcf55fb5SFelix Fietkau }
2254042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2255d9a7ddb0SJohannes Berg 
2256b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2257b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2258b4809e94SToke Høiland-Jørgensen {
2259942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2260942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2261942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2262942741daSFelix Fietkau 	u32 airtime = 0;
2263c77bfab9SFelix Fietkau 	u32 diff;
2264b4809e94SToke Høiland-Jørgensen 
2265942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2266942741daSFelix Fietkau 		airtime += tx_airtime;
2267942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2268942741daSFelix Fietkau 		airtime += rx_airtime;
2269b4809e94SToke Høiland-Jørgensen 
2270942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2271942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2272942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2273c77bfab9SFelix Fietkau 
2274c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2275c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2276942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2277c77bfab9SFelix Fietkau 
2278942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2279b4809e94SToke Høiland-Jørgensen }
2280b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2281b4809e94SToke Høiland-Jørgensen 
22829b41a9d7SJohannes Berg void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
22834c51541dSBenjamin Berg {
22844c51541dSBenjamin Berg 	bool first = true;
22859b41a9d7SJohannes Berg 	int link_id;
22864c51541dSBenjamin Berg 
22879b41a9d7SJohannes Berg 	if (!sta->sta.valid_links || !sta->sta.mlo) {
22889b41a9d7SJohannes Berg 		sta->sta.cur = &sta->sta.deflink.agg;
22894c51541dSBenjamin Berg 		return;
22904c51541dSBenjamin Berg 	}
22914c51541dSBenjamin Berg 
22924c51541dSBenjamin Berg 	rcu_read_lock();
22939b41a9d7SJohannes Berg 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
22949b41a9d7SJohannes Berg 		struct ieee80211_link_sta *link_sta;
22959b41a9d7SJohannes Berg 		int i;
22969b41a9d7SJohannes Berg 
22979b41a9d7SJohannes Berg 		if (!(active_links & BIT(link_id)))
22989b41a9d7SJohannes Berg 			continue;
22999b41a9d7SJohannes Berg 
23009b41a9d7SJohannes Berg 		link_sta = rcu_dereference(sta->sta.link[link_id]);
23019b41a9d7SJohannes Berg 		if (!link_sta)
23029b41a9d7SJohannes Berg 			continue;
23039b41a9d7SJohannes Berg 
23044c51541dSBenjamin Berg 		if (first) {
23059b41a9d7SJohannes Berg 			sta->cur = sta->sta.deflink.agg;
23064c51541dSBenjamin Berg 			first = false;
23074c51541dSBenjamin Berg 			continue;
23084c51541dSBenjamin Berg 		}
23094c51541dSBenjamin Berg 
23104c51541dSBenjamin Berg 		sta->cur.max_amsdu_len =
23114c51541dSBenjamin Berg 			min(sta->cur.max_amsdu_len,
23124c51541dSBenjamin Berg 			    link_sta->agg.max_amsdu_len);
23134c51541dSBenjamin Berg 		sta->cur.max_rc_amsdu_len =
23144c51541dSBenjamin Berg 			min(sta->cur.max_rc_amsdu_len,
23154c51541dSBenjamin Berg 			    link_sta->agg.max_rc_amsdu_len);
23164c51541dSBenjamin Berg 
23174c51541dSBenjamin Berg 		for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
23184c51541dSBenjamin Berg 			sta->cur.max_tid_amsdu_len[i] =
23194c51541dSBenjamin Berg 				min(sta->cur.max_tid_amsdu_len[i],
23204c51541dSBenjamin Berg 				    link_sta->agg.max_tid_amsdu_len[i]);
23214c51541dSBenjamin Berg 	}
23224c51541dSBenjamin Berg 	rcu_read_unlock();
23234c51541dSBenjamin Berg 
23249b41a9d7SJohannes Berg 	sta->sta.cur = &sta->cur;
23259b41a9d7SJohannes Berg }
23269b41a9d7SJohannes Berg 
23279b41a9d7SJohannes Berg void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
23289b41a9d7SJohannes Berg {
23299b41a9d7SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
23309b41a9d7SJohannes Berg 
23319b41a9d7SJohannes Berg 	__ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
23324c51541dSBenjamin Berg }
23334c51541dSBenjamin Berg EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
23344c51541dSBenjamin Berg 
23353ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
23363ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
23373ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
23383ace10f5SKan Yan {
23393ace10f5SKan Yan 	int tx_pending;
23403ace10f5SKan Yan 
2341911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2342911bde0fSToke Høiland-Jørgensen 		return;
2343911bde0fSToke Høiland-Jørgensen 
23443ace10f5SKan Yan 	if (!tx_completed) {
23453ace10f5SKan Yan 		if (sta)
23463ace10f5SKan Yan 			atomic_add(tx_airtime,
23473ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
23483ace10f5SKan Yan 
23493ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
23508e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
23513ace10f5SKan Yan 		return;
23523ace10f5SKan Yan 	}
23533ace10f5SKan Yan 
23543ace10f5SKan Yan 	if (sta) {
23553ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
23563ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
235704e35caaSFelix Fietkau 		if (tx_pending < 0)
23583ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
23593ace10f5SKan Yan 				       tx_pending, 0);
23603ace10f5SKan Yan 	}
23613ace10f5SKan Yan 
23628e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
23633ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
23648e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
23653ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
23663ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
23673ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
23688e4bac06SFelix Fietkau 		      tx_airtime)) {
23698e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
23703ace10f5SKan Yan 			       tx_pending, 0);
23718e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
23728e4bac06SFelix Fietkau 	}
23733ace10f5SKan Yan }
23743ace10f5SKan Yan 
2375c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2376c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2377c9c5962bSJohannes Berg {
2378046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2379c9c5962bSJohannes Berg 	int cpu;
2380c9c5962bSJohannes Berg 
2381046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2382c9c5962bSJohannes Berg 		return stats;
2383c9c5962bSJohannes Berg 
2384c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2385c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2386c9c5962bSJohannes Berg 
2387046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2388c9c5962bSJohannes Berg 
2389c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2390c9c5962bSJohannes Berg 			stats = cpustats;
2391c9c5962bSJohannes Berg 	}
2392c9c5962bSJohannes Berg 
2393c9c5962bSJohannes Berg 	return stats;
2394c9c5962bSJohannes Berg }
2395c9c5962bSJohannes Berg 
239641cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
23974f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2398fbd6ff5cSJohannes Berg {
2399dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2400fbd6ff5cSJohannes Berg 
2401dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
24027f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
24034f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2404dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2405dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2406dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2407dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
24087f406cd1SJohannes Berg 		break;
24097f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
24104f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2411dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2412dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2413dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
24147f406cd1SJohannes Berg 		break;
24157f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2416fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2417fbd6ff5cSJohannes Berg 		u16 brate;
24184f6b1b3dSJohannes Berg 		unsigned int shift;
2419dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2420dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2421fbd6ff5cSJohannes Berg 
2422dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
24238b783d10SThomas Pedersen 
24248b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
24258b783d10SThomas Pedersen 			break;
24268b783d10SThomas Pedersen 
2427dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
24284f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
24294f6b1b3dSJohannes Berg 			shift = 2;
24304f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
24314f6b1b3dSJohannes Berg 			shift = 1;
24324f6b1b3dSJohannes Berg 		else
24334f6b1b3dSJohannes Berg 			shift = 0;
2434fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
24357f406cd1SJohannes Berg 		break;
24367f406cd1SJohannes Berg 		}
243741cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
243841cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
243941cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
244041cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
244141cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
244241cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
244341cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
244441cbb0f5SLuca Coelho 		break;
2445f66c48afSJohannes Berg 	case STA_STATS_RATE_TYPE_EHT:
2446f66c48afSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_EHT_MCS;
2447f66c48afSJohannes Berg 		rinfo->mcs = STA_STATS_GET(EHT_MCS, rate);
2448f66c48afSJohannes Berg 		rinfo->nss = STA_STATS_GET(EHT_NSS, rate);
2449f66c48afSJohannes Berg 		rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate);
2450f66c48afSJohannes Berg 		rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate);
2451f66c48afSJohannes Berg 		break;
2452fbd6ff5cSJohannes Berg 	}
24534f6b1b3dSJohannes Berg }
2454fbd6ff5cSJohannes Berg 
2455a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
24564f6b1b3dSJohannes Berg {
245759336e07SShayne Chen 	u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
24584f6b1b3dSJohannes Berg 
24594f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2460a17d93ffSBen Greear 		return -EINVAL;
2461a17d93ffSBen Greear 
24624f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2463a17d93ffSBen Greear 	return 0;
2464fbd6ff5cSJohannes Berg }
2465fbd6ff5cSJohannes Berg 
2466b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2467b255b72bSSeevalamuthu Mariappan 					int tid)
2468b255b72bSSeevalamuthu Mariappan {
2469b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2470b255b72bSSeevalamuthu Mariappan 	u64 value;
2471b255b72bSSeevalamuthu Mariappan 
2472b255b72bSSeevalamuthu Mariappan 	do {
2473d120d1a6SThomas Gleixner 		start = u64_stats_fetch_begin(&rxstats->syncp);
2474b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2475d120d1a6SThomas Gleixner 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2476b255b72bSSeevalamuthu Mariappan 
2477b255b72bSSeevalamuthu Mariappan 	return value;
2478b255b72bSSeevalamuthu Mariappan }
2479b255b72bSSeevalamuthu Mariappan 
24800f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
24810f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
24820f9c5a61SJohannes Berg 			     int tid)
24830f9c5a61SJohannes Berg {
24840f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2485b255b72bSSeevalamuthu Mariappan 	int cpu;
24860f9c5a61SJohannes Berg 
24870f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2488046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2489046d2e7cSSriram R 							   tid);
24900f9c5a61SJohannes Berg 
2491046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2492b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2493b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2494b255b72bSSeevalamuthu Mariappan 
2495046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2496046d2e7cSSriram R 						     cpu);
2497b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2498b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2499b255b72bSSeevalamuthu Mariappan 			}
2500b255b72bSSeevalamuthu Mariappan 		}
25010f9c5a61SJohannes Berg 
25020f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
25030f9c5a61SJohannes Berg 	}
25040f9c5a61SJohannes Berg 
25050f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
25060f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2507046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
25080f9c5a61SJohannes Berg 	}
25090f9c5a61SJohannes Berg 
25100f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
25110f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
25120f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2513046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
25140f9c5a61SJohannes Berg 	}
25150f9c5a61SJohannes Berg 
25160f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
25170f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
25180f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2519046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
25200f9c5a61SJohannes Berg 	}
25212fe4a29aSToke Høiland-Jørgensen 
2522107395f9SAlexander Wetzel 	if (tid < IEEE80211_NUM_TIDS) {
25232fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
25242fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
25252fe4a29aSToke Høiland-Jørgensen 
25262fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
25272fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
25282fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
25292fe4a29aSToke Høiland-Jørgensen 
25302fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
25312fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
25322fe4a29aSToke Høiland-Jørgensen 	}
25330f9c5a61SJohannes Berg }
25340f9c5a61SJohannes Berg 
2535c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2536c9c5962bSJohannes Berg {
2537c9c5962bSJohannes Berg 	unsigned int start;
2538c9c5962bSJohannes Berg 	u64 value;
2539c9c5962bSJohannes Berg 
2540c9c5962bSJohannes Berg 	do {
2541d120d1a6SThomas Gleixner 		start = u64_stats_fetch_begin(&rxstats->syncp);
2542c9c5962bSJohannes Berg 		value = rxstats->bytes;
2543d120d1a6SThomas Gleixner 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2544c9c5962bSJohannes Berg 
2545c9c5962bSJohannes Berg 	return value;
2546c9c5962bSJohannes Berg }
2547c9c5962bSJohannes Berg 
25480fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
25490fdf1493SJohannes Berg 		   bool tidstats)
2550b7ffbd7eSJohannes Berg {
2551b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2552b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2553b7ffbd7eSJohannes Berg 	u32 thr = 0;
2554c9c5962bSJohannes Berg 	int i, ac, cpu;
2555c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2556c9c5962bSJohannes Berg 
2557c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2558b7ffbd7eSJohannes Berg 
2559b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2560b7ffbd7eSJohannes Berg 
2561225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2562225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2563225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2564225b8189SJohannes Berg 	 */
2565225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2566bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2567225b8189SJohannes Berg 
25682b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2569a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2570a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2571a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2572a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
25739cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2574a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2575976bd9efSJohannes Berg 
2576976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2577bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2578bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2579a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2580976bd9efSJohannes Berg 	}
2581b7ffbd7eSJohannes Berg 
258284b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
25839cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2584e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2585b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
25862b9a7e1bSJohannes Berg 
2587a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2588a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2589b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
25902b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2591046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2592a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2593b7ffbd7eSJohannes Berg 	}
25942b9a7e1bSJohannes Berg 
2595a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
25962b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
25972b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2598046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2599a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
26002b9a7e1bSJohannes Berg 	}
26012b9a7e1bSJohannes Berg 
2602a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2603a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2604046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
26050f9c5a61SJohannes Berg 
2606046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2607c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2608c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2609c9c5962bSJohannes Berg 
2610046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2611046d2e7cSSriram R 						     cpu);
2612c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2613c9c5962bSJohannes Berg 			}
2614c9c5962bSJohannes Berg 		}
2615c9c5962bSJohannes Berg 
2616a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
26172b9a7e1bSJohannes Berg 	}
26182b9a7e1bSJohannes Berg 
2619a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2620046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2621046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2622c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2623c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2624c9c5962bSJohannes Berg 
2625046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2626046d2e7cSSriram R 						     cpu);
2627c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2628c9c5962bSJohannes Berg 			}
2629c9c5962bSJohannes Berg 		}
2630a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
26312b9a7e1bSJohannes Berg 	}
26322b9a7e1bSJohannes Berg 
2633a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2634046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2635a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
26362b9a7e1bSJohannes Berg 	}
26372b9a7e1bSJohannes Berg 
2638a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2639046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2640a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
26412b9a7e1bSJohannes Berg 	}
26422b9a7e1bSJohannes Berg 
2643b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2644b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2645b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2646b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2647b4809e94SToke Høiland-Jørgensen 	}
2648b4809e94SToke Høiland-Jørgensen 
2649b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2650b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2651b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2652b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2653b4809e94SToke Høiland-Jørgensen 	}
2654b4809e94SToke Høiland-Jørgensen 
2655b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2656942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2657b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2658b4809e94SToke Høiland-Jørgensen 	}
2659b4809e94SToke Høiland-Jørgensen 
2660046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2661046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2662c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2663c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2664c9c5962bSJohannes Berg 
2665046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2666e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2667c9c5962bSJohannes Berg 		}
2668c9c5962bSJohannes Berg 	}
2669b7ffbd7eSJohannes Berg 
2670225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2671225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2672a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2673a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2674225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2675225b8189SJohannes Berg 	}
2676225b8189SJohannes Berg 
267730686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
267830686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2679a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2680c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2681a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2682b7ffbd7eSJohannes Berg 		}
26832b9a7e1bSJohannes Berg 
2684046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2685a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
268640d9a38aSJohannes Berg 			sinfo->signal_avg =
2687046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2688a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
26892b9a7e1bSJohannes Berg 		}
26902b9a7e1bSJohannes Berg 	}
26912b9a7e1bSJohannes Berg 
2692c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2693c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2694c9c5962bSJohannes Berg 	 * pcpu statistics
2695c9c5962bSJohannes Berg 	 */
2696c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2697a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2698a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2699a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2700046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2701a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2702b7ffbd7eSJohannes Berg 
2703c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2704c9c5962bSJohannes Berg 
2705b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2706e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2707c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2708b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2709046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2710b7ffbd7eSJohannes Berg 		}
2711b7ffbd7eSJohannes Berg 	}
2712b7ffbd7eSJohannes Berg 
27138a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
27148a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2715046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2716e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2717a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
27182b9a7e1bSJohannes Berg 	}
27192b9a7e1bSJohannes Berg 
27208a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
27218a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2722a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2723a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
27242b9a7e1bSJohannes Berg 	}
2725b7ffbd7eSJohannes Berg 
27260fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
27276af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
27286af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
27298689c051SArend van Spriel 	}
273079c892b8SJohannes Berg 
2731b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2732b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2733a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2734a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2735a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2736a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2737a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2738dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
27391303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
27401303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2741b7ffbd7eSJohannes Berg 
2742433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2743433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2744433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2745b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2746a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2747433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2748b7ffbd7eSJohannes Berg 		}
2749433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2750433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2751433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2752dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
27531303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2754b7ffbd7eSJohannes Berg #endif
2755b7ffbd7eSJohannes Berg 	}
2756b7ffbd7eSJohannes Berg 
2757b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2758b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2759b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2760b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2761b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2762b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2763b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2764785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2765b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2766b7ffbd7eSJohannes Berg 
2767b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2768b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2769b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2770b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2771b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2772b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2773b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2774b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2775b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2776b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2777b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2778b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2779a74a8c84SJohannes Berg 	if (sta->sta.wme)
2780b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2781b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2782b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2783b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2784b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2785b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2786b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2787b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2788b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2789b7ffbd7eSJohannes Berg 
27903b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
27913b17fbf8SMaxim Altshul 
27923b17fbf8SMaxim Altshul 	if (thr != 0) {
2793a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
27943b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
27953b17fbf8SMaxim Altshul 	}
2796a78b26ffSVenkateswara Naralasetty 
2797a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2798046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2799046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2800a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2801a78b26ffSVenkateswara Naralasetty 	}
2802cc60dbbfSBalaji Pothunoori 
28039c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2804046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2805cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2806cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2807046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2808cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
28099c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2810cc60dbbfSBalaji Pothunoori 	}
2811ab60633cSNarayanraddi Masti 
2812ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2813ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2814ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2815ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2816ab60633cSNarayanraddi Masti 	}
28173b17fbf8SMaxim Altshul }
28183b17fbf8SMaxim Altshul 
28193b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
28203b17fbf8SMaxim Altshul {
28213b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
28223b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
28233b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
28243b17fbf8SMaxim Altshul 	u32 thr = 0;
28253b17fbf8SMaxim Altshul 
28263b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
28273b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
28283b17fbf8SMaxim Altshul 
2829b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2830b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2831b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2832b7ffbd7eSJohannes Berg 	else
28334fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2834b7ffbd7eSJohannes Berg 
28353b17fbf8SMaxim Altshul 	return thr;
2836b7ffbd7eSJohannes Berg }
2837b8da6b6aSJohannes Berg 
2838b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2839b8da6b6aSJohannes Berg {
2840c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2841c9c5962bSJohannes Berg 
2842046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2843046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2844c9c5962bSJohannes Berg 		return stats->last_rx;
2845046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2846b8da6b6aSJohannes Berg }
2847484a54c2SToke Høiland-Jørgensen 
2848484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2849484a54c2SToke Høiland-Jørgensen {
2850484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2851484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2852484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2853484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2854484a54c2SToke Høiland-Jørgensen 	} else {
2855484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2856484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2857484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2858484a54c2SToke Høiland-Jørgensen 	}
2859484a54c2SToke Høiland-Jørgensen }
2860484a54c2SToke Høiland-Jørgensen 
2861484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2862484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2863484a54c2SToke Høiland-Jørgensen {
2864484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2865484a54c2SToke Høiland-Jørgensen 
2866484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2867484a54c2SToke Høiland-Jørgensen }
2868cb71f1d1SJohannes Berg 
2869cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2870cb71f1d1SJohannes Berg {
2871cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2872cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2873cb71f1d1SJohannes Berg 	int ret;
2874cb71f1d1SJohannes Berg 
2875cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2876cb71f1d1SJohannes Berg 
2877cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2878cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2879cb71f1d1SJohannes Berg 		return -EINVAL;
2880cb71f1d1SJohannes Berg 
2881cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2882cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2883cb71f1d1SJohannes Berg 		return -EBUSY;
2884cb71f1d1SJohannes Berg 
2885cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2886cb71f1d1SJohannes Berg 	if (!alloc)
2887cb71f1d1SJohannes Berg 		return -ENOMEM;
2888cb71f1d1SJohannes Berg 
2889cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2890cb71f1d1SJohannes Berg 	if (ret) {
2891cb71f1d1SJohannes Berg 		kfree(alloc);
2892cb71f1d1SJohannes Berg 		return ret;
2893cb71f1d1SJohannes Berg 	}
2894cb71f1d1SJohannes Berg 
2895cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2896cb71f1d1SJohannes Berg 
2897d2caad52SBenjamin Berg 	ieee80211_link_sta_debugfs_add(&alloc->info);
2898d2caad52SBenjamin Berg 
2899cb71f1d1SJohannes Berg 	return 0;
2900cb71f1d1SJohannes Berg }
2901cb71f1d1SJohannes Berg 
290221476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
290321476ad1SShaul Triebitz {
290421476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
290521476ad1SShaul Triebitz 
290621476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
290721476ad1SShaul Triebitz }
290821476ad1SShaul Triebitz 
2909cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2910cb71f1d1SJohannes Berg {
2911cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2912c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2913cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2914cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2915cb71f1d1SJohannes Berg 	int ret;
2916cb71f1d1SJohannes Berg 
2917c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2918c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2919cb71f1d1SJohannes Berg 
2920c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2921cb71f1d1SJohannes Berg 		return -EINVAL;
2922cb71f1d1SJohannes Berg 
2923177577dbSJohannes Berg 	rcu_read_lock();
2924177577dbSJohannes Berg 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2925177577dbSJohannes Berg 		rcu_read_unlock();
2926177577dbSJohannes Berg 		return -EALREADY;
2927177577dbSJohannes Berg 	}
2928177577dbSJohannes Berg 	/* we only modify under the mutex so this is fine */
2929177577dbSJohannes Berg 	rcu_read_unlock();
2930177577dbSJohannes Berg 
2931cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2932cb71f1d1SJohannes Berg 
293380e2b1faSLukas Bulwahn 	if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2934ba6ddab9SJohannes Berg 		goto hash;
2935cb71f1d1SJohannes Berg 
2936ba7af265SJohannes Berg 	ieee80211_recalc_min_chandef(sdata, link_id);
2937ba7af265SJohannes Berg 
29384c51541dSBenjamin Berg 	/* Ensure the values are updated for the driver,
29394c51541dSBenjamin Berg 	 * redone by sta_remove_link on failure.
29404c51541dSBenjamin Berg 	 */
29414c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
29424c51541dSBenjamin Berg 
2943cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2944cb71f1d1SJohannes Berg 				   old_links, new_links);
2945cb71f1d1SJohannes Berg 	if (ret) {
2946cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2947ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2948177577dbSJohannes Berg 		return ret;
2949cb71f1d1SJohannes Berg 	}
2950cb71f1d1SJohannes Berg 
2951ba6ddab9SJohannes Berg hash:
2952177577dbSJohannes Berg 	ret = link_sta_info_hash_add(sdata->local, link_sta);
2953177577dbSJohannes Berg 	WARN_ON(ret);
2954177577dbSJohannes Berg 	return 0;
2955cb71f1d1SJohannes Berg }
2956cb71f1d1SJohannes Berg 
2957cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2958cb71f1d1SJohannes Berg {
2959cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2960a8f62399SShaul Triebitz 	u16 old_links = sta->sta.valid_links;
2961cb71f1d1SJohannes Berg 
2962cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2963cb71f1d1SJohannes Berg 
2964cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2965cb71f1d1SJohannes Berg 
2966cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2967cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2968a8f62399SShaul Triebitz 				     old_links, sta->sta.valid_links);
2969cb71f1d1SJohannes Berg 
2970ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2971cb71f1d1SJohannes Berg }
2972175ad2ecSJohannes Berg 
2973175ad2ecSJohannes Berg void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2974175ad2ecSJohannes Berg 					   const u8 *ext_capab,
2975175ad2ecSJohannes Berg 					   unsigned int ext_capab_len)
2976175ad2ecSJohannes Berg {
2977175ad2ecSJohannes Berg 	u8 val;
2978175ad2ecSJohannes Berg 
2979175ad2ecSJohannes Berg 	sta->sta.max_amsdu_subframes = 0;
2980175ad2ecSJohannes Berg 
2981175ad2ecSJohannes Berg 	if (ext_capab_len < 8)
2982175ad2ecSJohannes Berg 		return;
2983175ad2ecSJohannes Berg 
2984175ad2ecSJohannes Berg 	/* The sender might not have sent the last bit, consider it to be 0 */
2985175ad2ecSJohannes Berg 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2986175ad2ecSJohannes Berg 
2987175ad2ecSJohannes Berg 	/* we did get all the bits, take the MSB as well */
2988175ad2ecSJohannes Berg 	if (ext_capab_len >= 9)
2989175ad2ecSJohannes Berg 		val |= u8_get_bits(ext_capab[8],
2990175ad2ecSJohannes Berg 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2991175ad2ecSJohannes Berg 
2992175ad2ecSJohannes Berg 	if (val)
2993175ad2ecSJohannes Berg 		sta->sta.max_amsdu_subframes = 4 << val;
2994175ad2ecSJohannes Berg }
299565fd846cSJohannes Berg 
299665fd846cSJohannes Berg #ifdef CONFIG_LOCKDEP
299765fd846cSJohannes Berg bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
299865fd846cSJohannes Berg {
299965fd846cSJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
300065fd846cSJohannes Berg 
300165fd846cSJohannes Berg 	return lockdep_is_held(&sta->local->sta_mtx);
300265fd846cSJohannes Berg }
300365fd846cSJohannes Berg EXPORT_SYMBOL(lockdep_sta_mutex_held);
300465fd846cSJohannes Berg #endif
3005