xref: /openbmc/linux/net/mac80211/sta_info.c (revision f66c48af7a110c0d694c4ac4a1257affb272a2ea)
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
7*f66c48afSJohannes Berg  * Copyright (C) 2018-2022 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 
358c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
359c71420dbSJohannes Berg 					     lockdep_is_held(&sta->local->sta_mtx));
360c71420dbSJohannes Berg 
361c71420dbSJohannes Berg 	if (WARN_ON(!link_sta))
362cb71f1d1SJohannes Berg 		return;
363246b39e4SJohannes Berg 
364ba6ddab9SJohannes Berg 	if (unhash)
365ba6ddab9SJohannes Berg 		link_sta_info_hash_del(sta->local, link_sta);
366ba6ddab9SJohannes Berg 
367d2caad52SBenjamin Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
368d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_remove(link_sta);
369d2caad52SBenjamin Berg 
370c71420dbSJohannes Berg 	if (link_sta != &sta->deflink)
371c71420dbSJohannes Berg 		alloc = container_of(link_sta, typeof(*alloc), info);
372cb71f1d1SJohannes Berg 
373cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
374c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->link[link_id], NULL);
375c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
376cb71f1d1SJohannes Berg 	if (alloc) {
377cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
378c71420dbSJohannes Berg 		kfree_rcu(alloc, rcu_head);
379246b39e4SJohannes Berg 	}
3804c51541dSBenjamin Berg 
3814c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
382246b39e4SJohannes Berg }
383246b39e4SJohannes Berg 
38493e5deb1SJohannes Berg /**
385d9a7ddb0SJohannes Berg  * sta_info_free - free STA
38693e5deb1SJohannes Berg  *
3876ef307bcSRandy Dunlap  * @local: pointer to the global information
38893e5deb1SJohannes Berg  * @sta: STA info to free
38993e5deb1SJohannes Berg  *
39093e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
391d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
392d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
393d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
39493e5deb1SJohannes Berg  */
395d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
39693e5deb1SJohannes Berg {
397cb71f1d1SJohannes Berg 	int i;
398cb71f1d1SJohannes Berg 
399cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
400cb71f1d1SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
401cb71f1d1SJohannes Berg 			continue;
402cb71f1d1SJohannes Berg 
4030ad49045SJohannes Berg 		sta_remove_link(sta, i, false);
404cb71f1d1SJohannes Berg 	}
405cb71f1d1SJohannes Berg 
406dcd479e1SJohannes Berg 	/*
407dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
408dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
409dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
410dcd479e1SJohannes Berg 	 *
411dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
412dcd479e1SJohannes Berg 	 * allocated during state transitions.
413dcd479e1SJohannes Berg 	 */
414dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
415dcd479e1SJohannes Berg 		int ret;
416dcd479e1SJohannes Berg 
417dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
418dcd479e1SJohannes Berg 
419dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
420dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
421dcd479e1SJohannes Berg 			break;
422dcd479e1SJohannes Berg 	}
423dcd479e1SJohannes Berg 
424889cbb91SJohannes Berg 	if (sta->rate_ctrl)
4254b7679a5SJohannes Berg 		rate_control_free_sta(sta);
42693e5deb1SJohannes Berg 
427bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
42893e5deb1SJohannes Berg 
429ba8c3d6fSFelix Fietkau 	kfree(to_txq_info(sta->sta.txq[0]));
43053d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
431433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
432433f5bc1SJohannes Berg 	kfree(sta->mesh);
433433f5bc1SJohannes Berg #endif
434246b39e4SJohannes Berg 
435cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
43693e5deb1SJohannes Berg 	kfree(sta);
43793e5deb1SJohannes Berg }
43893e5deb1SJohannes Berg 
4394d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
44062b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
441d0709a65SJohannes Berg 			     struct sta_info *sta)
442f0706e82SJiri Benc {
44383e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
4447bedd0cfSJohannes Berg 			       sta_rht_params);
445f0706e82SJiri Benc }
446f0706e82SJiri Benc 
4475ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
448af818581SJohannes Berg {
449af818581SJohannes Berg 	struct sta_info *sta;
450af818581SJohannes Berg 
4515ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
452af818581SJohannes Berg 
453af818581SJohannes Berg 	if (sta->dead)
454af818581SJohannes Berg 		return;
455af818581SJohannes Berg 
45654420473SHelmut Schaa 	local_bh_disable();
4575ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
458af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
4595ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
460af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
4615ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
46247086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
463ce662b44SJohannes Berg 	local_bh_enable();
464af818581SJohannes Berg }
465af818581SJohannes Berg 
466af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
467af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
468af65cd96SJohannes Berg {
46930686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
470af65cd96SJohannes Berg 		return 0;
471af65cd96SJohannes Berg 
472889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
473af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
47435c347acSJohannes Berg 						     sta, gfp);
475889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
476af65cd96SJohannes Berg 		return -ENOMEM;
477af65cd96SJohannes Berg 
478af65cd96SJohannes Berg 	return 0;
479af65cd96SJohannes Berg }
480af65cd96SJohannes Berg 
481cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
482246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
483246b39e4SJohannes Berg 			       gfp_t gfp)
484246b39e4SJohannes Berg {
485246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
486246b39e4SJohannes Berg 	int i;
487246b39e4SJohannes Berg 
488246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
489246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
490246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
491246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
492246b39e4SJohannes Berg 			return -ENOMEM;
493246b39e4SJohannes Berg 	}
494246b39e4SJohannes Berg 
495246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
496246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
497246b39e4SJohannes Berg 
498246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
499246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
500246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
501246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
502246b39e4SJohannes Berg 
503246b39e4SJohannes Berg 	return 0;
504246b39e4SJohannes Berg }
505246b39e4SJohannes Berg 
506cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
507cb71f1d1SJohannes Berg 			      unsigned int link_id,
508cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
509cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
510cb71f1d1SJohannes Berg {
511cb71f1d1SJohannes Berg 	link_info->sta = sta;
512cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
513c71420dbSJohannes Berg 	link_info->pub = link_sta;
5141d9e4c91SBenjamin Berg 	link_info->pub->sta = &sta->sta;
515c73993b8SJohannes Berg 	link_sta->link_id = link_id;
516c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
517c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
518261ce887SBenjamin Berg 
519261ce887SBenjamin Berg 	link_sta->smps_mode = IEEE80211_SMPS_OFF;
5204c51541dSBenjamin Berg 	link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
521cb71f1d1SJohannes Berg }
522cb71f1d1SJohannes Berg 
523f36fe0a2SJohannes Berg static struct sta_info *
524f36fe0a2SJohannes Berg __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
525f36fe0a2SJohannes Berg 		 const u8 *addr, int link_id, const u8 *link_addr,
526f36fe0a2SJohannes Berg 		 gfp_t gfp)
527f0706e82SJiri Benc {
528d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
529ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
530f0706e82SJiri Benc 	struct sta_info *sta;
531107395f9SAlexander Wetzel 	void *txq_data;
532107395f9SAlexander Wetzel 	int size;
53316c5f15cSRon Rindjunsky 	int i;
534f0706e82SJiri Benc 
535ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
536f0706e82SJiri Benc 	if (!sta)
53773651ee6SJohannes Berg 		return NULL;
538f0706e82SJiri Benc 
539246b39e4SJohannes Berg 	sta->local = local;
540246b39e4SJohannes Berg 	sta->sdata = sdata;
541246b39e4SJohannes Berg 
542cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
54336fe8e4eSLorenzo Bianconi 		goto free;
544c9c5962bSJohannes Berg 
545cb71f1d1SJohannes Berg 	if (link_id >= 0) {
546cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
547cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
548cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
549cb71f1d1SJohannes Berg 	} else {
550cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
551cb71f1d1SJohannes Berg 	}
552cb71f1d1SJohannes Berg 
5534c51541dSBenjamin Berg 	sta->sta.cur = &sta->sta.deflink.agg;
5544c51541dSBenjamin Berg 
55507346f81SJohannes Berg 	spin_lock_init(&sta->lock);
5561d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
5575ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
55867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
559a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
56087f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
561433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
562433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
563433f5bc1SJohannes Berg 		if (!sta->mesh)
564433f5bc1SJohannes Berg 			goto free;
5654c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
566433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
56745d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5684c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5694c02d62fSKees Cook 				    0);
570433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
571433f5bc1SJohannes Berg 	}
57287f59c70SThomas Pedersen #endif
57307346f81SJohannes Berg 
574ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
57517741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
576f36fe0a2SJohannes Berg 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
577f36fe0a2SJohannes Berg 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
578480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
579480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
580480dd46bSMaxim Altshul 
581046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
582046d2e7cSSriram R 
58396fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
58496fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
58596fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
58696fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
58796fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
58896fc6efbSAlexander Wetzel 	 */
58996fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
59096fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
59196fc6efbSAlexander Wetzel 
5920f9c5a61SJohannes Berg 
5933a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5943a11ce08SJohannes Berg 
59571ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
59671ec375cSJohannes Berg 
597b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
598b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
599b6da911bSLiad Kaufman 
60084b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
601541a45a1SBruno Randolf 
602107395f9SAlexander Wetzel 	size = sizeof(struct txq_info) +
603ba8c3d6fSFelix Fietkau 	       ALIGN(hw->txq_data_size, sizeof(void *));
604ba8c3d6fSFelix Fietkau 
605ba8c3d6fSFelix Fietkau 	txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
606ba8c3d6fSFelix Fietkau 	if (!txq_data)
607ba8c3d6fSFelix Fietkau 		goto free;
608ba8c3d6fSFelix Fietkau 
609ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
610ba8c3d6fSFelix Fietkau 		struct txq_info *txq = txq_data + i * size;
611ba8c3d6fSFelix Fietkau 
612107395f9SAlexander Wetzel 		/* might not do anything for the (bufferable) MMPDU TXQ */
613fa962b92SMichal Kazior 		ieee80211_txq_init(sdata, sta, txq, i);
614abfbc3afSJohannes Berg 	}
615ba8c3d6fSFelix Fietkau 
616ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
617ba8c3d6fSFelix Fietkau 		goto free_txq;
618f0706e82SJiri Benc 
619942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
620b4809e94SToke Høiland-Jørgensen 
621948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
622948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
623948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
624942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
625942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
626942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
627942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
628948d887dSJohannes Berg 	}
62973651ee6SJohannes Berg 
6305a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
6314be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
632cccaec98SSenthil Balasubramanian 
633bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
634bd718fc1SJohannes Berg 		u32 mandatory = 0;
635bd718fc1SJohannes Berg 		int r;
636bd718fc1SJohannes Berg 
637bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
638bd718fc1SJohannes Berg 			continue;
639bd718fc1SJohannes Berg 
640bd718fc1SJohannes Berg 		switch (i) {
641bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
64263fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
643bd718fc1SJohannes Berg 			/*
644bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
645bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
646bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
647bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
648bd718fc1SJohannes Berg 			 * possible rate anyway.
649bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
650bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
651bd718fc1SJohannes Berg 			 */
652bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
653bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
654bd718fc1SJohannes Berg 			break;
655bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
656bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
657bd718fc1SJohannes Berg 			break;
658bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
659bd718fc1SJohannes Berg 			WARN_ON(1);
660bd718fc1SJohannes Berg 			mandatory = 0;
661bd718fc1SJohannes Berg 			break;
662bd718fc1SJohannes Berg 		}
663bd718fc1SJohannes Berg 
664bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
665bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
666bd718fc1SJohannes Berg 
667bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
668bd718fc1SJohannes Berg 
669bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
670bd718fc1SJohannes Berg 				continue;
671046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
672bd718fc1SJohannes Berg 		}
673bd718fc1SJohannes Berg 	}
674bd718fc1SJohannes Berg 
675484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
676484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
677484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
678484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
679dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
680dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
681484a54c2SToke Høiland-Jørgensen 
682bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
683ef04a297SJohannes Berg 
684abfbc3afSJohannes Berg 	return sta;
685ba8c3d6fSFelix Fietkau 
686ba8c3d6fSFelix Fietkau free_txq:
687ba8c3d6fSFelix Fietkau 	kfree(to_txq_info(sta->sta.txq[0]));
688ba8c3d6fSFelix Fietkau free:
689cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
690433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
691433f5bc1SJohannes Berg 	kfree(sta->mesh);
692433f5bc1SJohannes Berg #endif
693ba8c3d6fSFelix Fietkau 	kfree(sta);
694ba8c3d6fSFelix Fietkau 	return NULL;
69573651ee6SJohannes Berg }
69673651ee6SJohannes Berg 
697f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
698f36fe0a2SJohannes Berg 				const u8 *addr, gfp_t gfp)
699f36fe0a2SJohannes Berg {
700f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
701f36fe0a2SJohannes Berg }
702f36fe0a2SJohannes Berg 
703f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
704f36fe0a2SJohannes Berg 					  const u8 *mld_addr,
705f36fe0a2SJohannes Berg 					  unsigned int link_id,
706f36fe0a2SJohannes Berg 					  const u8 *link_addr,
707f36fe0a2SJohannes Berg 					  gfp_t gfp)
708f36fe0a2SJohannes Berg {
709f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
710f36fe0a2SJohannes Berg }
711f36fe0a2SJohannes Berg 
7128c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
71334e89507SJohannes Berg {
71434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
71534e89507SJohannes Berg 
71603e4497eSJohannes Berg 	/*
71703e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
71803e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
71903e4497eSJohannes Berg 	 * and another CPU turns off the net device.
72003e4497eSJohannes Berg 	 */
7218c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
7228c71df7aSGuy Eilam 		return -ENETDOWN;
72303e4497eSJohannes Berg 
724b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
725deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
7268c71df7aSGuy Eilam 		return -EINVAL;
7278c71df7aSGuy Eilam 
72883e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
72983e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
73083e7e4ceSHerbert Xu 	 * for correctness.
73131104891SJohannes Berg 	 */
73231104891SJohannes Berg 	rcu_read_lock();
73331104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
73431104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
73531104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
73631104891SJohannes Berg 		rcu_read_unlock();
73731104891SJohannes Berg 		return -ENOTUNIQ;
73831104891SJohannes Berg 	}
73931104891SJohannes Berg 	rcu_read_unlock();
74031104891SJohannes Berg 
7418c71df7aSGuy Eilam 	return 0;
74293e5deb1SJohannes Berg }
74344213b5eSJohannes Berg 
744f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
745f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
746f09603a2SJohannes Berg 				     struct sta_info *sta)
747f09603a2SJohannes Berg {
748f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
749f09603a2SJohannes Berg 	int err = 0;
750f09603a2SJohannes Berg 
751f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
752f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
753f09603a2SJohannes Berg 		if (err)
754f09603a2SJohannes Berg 			break;
755f09603a2SJohannes Berg 	}
756f09603a2SJohannes Berg 
757f09603a2SJohannes Berg 	if (!err) {
758a4ec45a4SJohannes Berg 		/*
759a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
760a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
761a4ec45a4SJohannes Berg 		 */
762a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
763f09603a2SJohannes Berg 			sta->uploaded = true;
764f09603a2SJohannes Berg 		return 0;
765f09603a2SJohannes Berg 	}
766f09603a2SJohannes Berg 
767f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
768bdcbd8e0SJohannes Berg 		sdata_info(sdata,
769bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
770bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
771f09603a2SJohannes Berg 		err = 0;
772f09603a2SJohannes Berg 	}
773f09603a2SJohannes Berg 
774f09603a2SJohannes Berg 	/* unwind on error */
775f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
776f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
777f09603a2SJohannes Berg 
778f09603a2SJohannes Berg 	return err;
779f09603a2SJohannes Berg }
780f09603a2SJohannes Berg 
781d405fd8cSGregory Greenman static void
782d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
783d405fd8cSGregory Greenman {
784d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
785d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
786d405fd8cSGregory Greenman 	struct sta_info *sta;
787d405fd8cSGregory Greenman 
788d405fd8cSGregory Greenman 	rcu_read_lock();
789d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
790d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
791d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
792d405fd8cSGregory Greenman 			continue;
793d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
794d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
795d405fd8cSGregory Greenman 			break;
796d405fd8cSGregory Greenman 		}
797d405fd8cSGregory Greenman 	}
798d405fd8cSGregory Greenman 	rcu_read_unlock();
799d405fd8cSGregory Greenman 
800d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
801d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
802d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
803d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
804d405fd8cSGregory Greenman 	}
805d405fd8cSGregory Greenman }
806d405fd8cSGregory Greenman 
80734e89507SJohannes Berg /*
8088c71df7aSGuy Eilam  * should be called with sta_mtx locked
8098c71df7aSGuy Eilam  * this function replaces the mutex lock
8108c71df7aSGuy Eilam  * with a RCU lock
8118c71df7aSGuy Eilam  */
8124d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
8138c71df7aSGuy Eilam {
8148c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
8158c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8160c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
8178c71df7aSGuy Eilam 	int err = 0;
8188c71df7aSGuy Eilam 
8198c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
8208c71df7aSGuy Eilam 
8217852e361SJohannes Berg 	/* check if STA exists already */
8227852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
8234d33960bSJohannes Berg 		err = -EEXIST;
8248f9dcc29SAhmed Zaki 		goto out_cleanup;
82534e89507SJohannes Berg 	}
82634e89507SJohannes Berg 
8270c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
8280c2e3842SKoen Vandeputte 	if (!sinfo) {
8290c2e3842SKoen Vandeputte 		err = -ENOMEM;
8308f9dcc29SAhmed Zaki 		goto out_cleanup;
8310c2e3842SKoen Vandeputte 	}
8320c2e3842SKoen Vandeputte 
8334d33960bSJohannes Berg 	local->num_sta++;
8344d33960bSJohannes Berg 	local->sta_generation++;
8354d33960bSJohannes Berg 	smp_mb();
8364d33960bSJohannes Berg 
8375108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
8385108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
8395108ca82SJohannes Berg 
8404d33960bSJohannes Berg 	/* make the station visible */
84162b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
84262b14b24SJohannes Berg 	if (err)
84362b14b24SJohannes Berg 		goto out_drop_sta;
8444d33960bSJohannes Berg 
845f36fe0a2SJohannes Berg 	if (sta->sta.valid_links) {
846f36fe0a2SJohannes Berg 		err = link_sta_info_hash_add(local, &sta->deflink);
847f36fe0a2SJohannes Berg 		if (err) {
848f36fe0a2SJohannes Berg 			sta_info_hash_del(local, sta);
849f36fe0a2SJohannes Berg 			goto out_drop_sta;
850f36fe0a2SJohannes Berg 		}
851f36fe0a2SJohannes Berg 	}
852f36fe0a2SJohannes Berg 
8532bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
85483d5cc01SJohannes Berg 
8554dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8564dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8574dde3c36SMordechay Goodstein 	 */
8584dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8590cbf348aSAndrei Otcheretianski 		ieee80211_recalc_min_chandef(sta->sdata, -1);
8604dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8614dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8624dde3c36SMordechay Goodstein 	}
8634dde3c36SMordechay Goodstein 
8645108ca82SJohannes Berg 	/* notify driver */
8655108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8665108ca82SJohannes Berg 	if (err)
8675108ca82SJohannes Berg 		goto out_remove;
8685108ca82SJohannes Berg 
86983d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
870d405fd8cSGregory Greenman 
8715108ca82SJohannes Berg 	/* accept BA sessions now */
8725108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8734d33960bSJohannes Berg 
8744d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8754d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
876d2caad52SBenjamin Berg 	if (sta->sta.valid_links) {
877d2caad52SBenjamin Berg 		int i;
878d2caad52SBenjamin Berg 
879d2caad52SBenjamin Berg 		for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
880d2caad52SBenjamin Berg 			struct link_sta_info *link_sta;
881d2caad52SBenjamin Berg 
882d2caad52SBenjamin Berg 			link_sta = rcu_dereference_protected(sta->link[i],
883d2caad52SBenjamin Berg 							     lockdep_is_held(&local->sta_mtx));
884d2caad52SBenjamin Berg 
885d2caad52SBenjamin Berg 			if (!link_sta)
886d2caad52SBenjamin Berg 				continue;
887d2caad52SBenjamin Berg 
888d2caad52SBenjamin Berg 			ieee80211_link_sta_debugfs_add(link_sta);
889d2caad52SBenjamin Berg 			if (sdata->vif.active_links & BIT(i))
890d2caad52SBenjamin Berg 				ieee80211_link_sta_debugfs_drv_add(link_sta);
891d2caad52SBenjamin Berg 		}
892d2caad52SBenjamin Berg 	} else {
893d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_add(&sta->deflink);
894d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
895d2caad52SBenjamin Berg 	}
8964d33960bSJohannes Berg 
8970ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
8980ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
8990ef049dcSArnd Bergmann 	kfree(sinfo);
900d0709a65SJohannes Berg 
901bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
902f0706e82SJiri Benc 
90334e89507SJohannes Berg 	/* move reference to rcu-protected */
90434e89507SJohannes Berg 	rcu_read_lock();
90534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
906e9f207f0SJiri Benc 
90773651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
90873651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
90973651ee6SJohannes Berg 
91073651ee6SJohannes Berg 	return 0;
9115108ca82SJohannes Berg  out_remove:
9120ad49045SJohannes Berg 	if (sta->sta.valid_links)
9130ad49045SJohannes Berg 		link_sta_info_hash_del(local, &sta->deflink);
9145108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
9155108ca82SJohannes Berg 	list_del_rcu(&sta->list);
91662b14b24SJohannes Berg  out_drop_sta:
9175108ca82SJohannes Berg 	local->num_sta--;
9185108ca82SJohannes Berg 	synchronize_net();
9198f9dcc29SAhmed Zaki  out_cleanup:
9207bc40aedSJohannes Berg 	cleanup_single_sta(sta);
9214d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
922ea32f065SSudip Mukherjee 	kfree(sinfo);
9234d33960bSJohannes Berg 	rcu_read_lock();
9244d33960bSJohannes Berg 	return err;
9258c71df7aSGuy Eilam }
9268c71df7aSGuy Eilam 
9278c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
9288c71df7aSGuy Eilam {
9298c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
930308f7fcfSZhao, Gang 	int err;
9318c71df7aSGuy Eilam 
9324d33960bSJohannes Berg 	might_sleep();
9334d33960bSJohannes Berg 
93431104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
93531104891SJohannes Berg 
9368c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
9378c71df7aSGuy Eilam 	if (err) {
9387bc40aedSJohannes Berg 		sta_info_free(local, sta);
93931104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
9408c71df7aSGuy Eilam 		rcu_read_lock();
9417bc40aedSJohannes Berg 		return err;
9428c71df7aSGuy Eilam 	}
9438c71df7aSGuy Eilam 
9447bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
945f0706e82SJiri Benc }
946f0706e82SJiri Benc 
94734e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
94834e89507SJohannes Berg {
94934e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
95034e89507SJohannes Berg 
95134e89507SJohannes Berg 	rcu_read_unlock();
95234e89507SJohannes Berg 
95334e89507SJohannes Berg 	return err;
95434e89507SJohannes Berg }
95534e89507SJohannes Berg 
956d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
957004c872eSJohannes Berg {
958004c872eSJohannes Berg 	/*
959004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
960004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
961004c872eSJohannes Berg 	 */
962d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
963004c872eSJohannes Berg }
964004c872eSJohannes Berg 
965d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
966004c872eSJohannes Berg {
967004c872eSJohannes Berg 	/*
968004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
969004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
970004c872eSJohannes Berg 	 */
971d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
972004c872eSJohannes Berg }
973004c872eSJohannes Berg 
9743d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9753d5839b6SIlan Peer {
9763d5839b6SIlan Peer 	/*
9773d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9783d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9793d5839b6SIlan Peer 	 */
9803d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9813d5839b6SIlan Peer }
9823d5839b6SIlan Peer 
983948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
984004c872eSJohannes Berg {
985948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
986948d887dSJohannes Berg 	switch (ac) {
987948d887dSJohannes Berg 	case IEEE80211_AC_VO:
988948d887dSJohannes Berg 		return BIT(6) | BIT(7);
989948d887dSJohannes Berg 	case IEEE80211_AC_VI:
990948d887dSJohannes Berg 		return BIT(4) | BIT(5);
991948d887dSJohannes Berg 	case IEEE80211_AC_BE:
992948d887dSJohannes Berg 		return BIT(0) | BIT(3);
993948d887dSJohannes Berg 	case IEEE80211_AC_BK:
994948d887dSJohannes Berg 		return BIT(1) | BIT(2);
995948d887dSJohannes Berg 	default:
996948d887dSJohannes Berg 		WARN_ON(1);
997948d887dSJohannes Berg 		return 0;
998d0709a65SJohannes Berg 	}
999004c872eSJohannes Berg }
1000004c872eSJohannes Berg 
10019b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
1002004c872eSJohannes Berg {
1003c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
1004d012a605SMarco Porsch 	struct ps_data *ps;
1005948d887dSJohannes Berg 	bool indicate_tim = false;
1006948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
1007948d887dSJohannes Berg 	int ac;
1008a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
1009004c872eSJohannes Berg 
1010d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1011d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1012c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
1013c868cb35SJohannes Berg 			return;
10143e122be0SJohannes Berg 
1015d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
10163f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
10173f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
10183f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
10193f52b7e3SMarco Porsch #endif
1020d012a605SMarco Porsch 	} else {
1021d012a605SMarco Porsch 		return;
1022d012a605SMarco Porsch 	}
1023d012a605SMarco Porsch 
1024c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
1025d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1026c868cb35SJohannes Berg 		return;
1027004c872eSJohannes Berg 
1028c868cb35SJohannes Berg 	if (sta->dead)
1029c868cb35SJohannes Berg 		goto done;
10303e122be0SJohannes Berg 
1031948d887dSJohannes Berg 	/*
1032948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
1033948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
1034948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
1035948d887dSJohannes Berg 	 * non-enabled ones.
1036948d887dSJohannes Berg 	 */
1037948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1038948d887dSJohannes Berg 		ignore_for_tim = 0;
1039948d887dSJohannes Berg 
10409b7a86f3SJohannes Berg 	if (ignore_pending)
10419b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
10429b7a86f3SJohannes Berg 
1043948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1044948d887dSJohannes Berg 		unsigned long tids;
1045948d887dSJohannes Berg 
1046f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1047948d887dSJohannes Berg 			continue;
1048948d887dSJohannes Berg 
1049948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1050948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
1051948d887dSJohannes Berg 		if (indicate_tim)
1052948d887dSJohannes Berg 			break;
1053948d887dSJohannes Berg 
1054948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
1055948d887dSJohannes Berg 
1056948d887dSJohannes Berg 		indicate_tim |=
1057948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
1058ba8c3d6fSFelix Fietkau 		indicate_tim |=
1059ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
1060004c872eSJohannes Berg 	}
1061004c872eSJohannes Berg 
1062c868cb35SJohannes Berg  done:
106365f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
1064004c872eSJohannes Berg 
10653d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
10663d5839b6SIlan Peer 		goto out_unlock;
10673d5839b6SIlan Peer 
1068948d887dSJohannes Berg 	if (indicate_tim)
1069d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1070c868cb35SJohannes Berg 	else
1071d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10723e122be0SJohannes Berg 
10739b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1074c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1075948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1076c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1077004c872eSJohannes Berg 	}
1078004c872eSJohannes Berg 
10793d5839b6SIlan Peer out_unlock:
108065f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1081004c872eSJohannes Berg }
1082004c872eSJohannes Berg 
10839b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10849b7a86f3SJohannes Berg {
10859b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10869b7a86f3SJohannes Berg }
10879b7a86f3SJohannes Berg 
1088cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1089f0706e82SJiri Benc {
1090e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1091f0706e82SJiri Benc 	int timeout;
1092f0706e82SJiri Benc 
1093f0706e82SJiri Benc 	if (!skb)
1094cd0b8d89SJohannes Berg 		return false;
1095f0706e82SJiri Benc 
1096e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1097f0706e82SJiri Benc 
1098f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
109957c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
110057c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
110157c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1102f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1103f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1104e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1105f0706e82SJiri Benc }
1106f0706e82SJiri Benc 
1107f0706e82SJiri Benc 
1108948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1109948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1110f0706e82SJiri Benc {
1111f0706e82SJiri Benc 	unsigned long flags;
1112f0706e82SJiri Benc 	struct sk_buff *skb;
1113f0706e82SJiri Benc 
111460750397SJohannes Berg 	/*
111560750397SJohannes Berg 	 * First check for frames that should expire on the filtered
111660750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
111760750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
111860750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
111960750397SJohannes Berg 	 * total_ps_buffered counter.
112060750397SJohannes Berg 	 */
1121f0706e82SJiri Benc 	for (;;) {
1122948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1123948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
112457c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1125948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1126836341a7SJohannes Berg 		else
1127f0706e82SJiri Benc 			skb = NULL;
1128948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1129f0706e82SJiri Benc 
113060750397SJohannes Berg 		/*
113160750397SJohannes Berg 		 * Frames are queued in order, so if this one
113260750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
113360750397SJohannes Berg 		 * we actually reached the end of the queue we
113460750397SJohannes Berg 		 * also need to stop, of course.
113560750397SJohannes Berg 		 */
113660750397SJohannes Berg 		if (!skb)
113760750397SJohannes Berg 			break;
1138d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
113960750397SJohannes Berg 	}
114060750397SJohannes Berg 
114160750397SJohannes Berg 	/*
114260750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
114360750397SJohannes Berg 	 * only find something if the filtered queue was emptied
114460750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
114560750397SJohannes Berg 	 * buffered frames.
114660750397SJohannes Berg 	 */
1147f0706e82SJiri Benc 	for (;;) {
1148948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1149948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1150f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1151948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1152f0706e82SJiri Benc 		else
1153f0706e82SJiri Benc 			skb = NULL;
1154948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1155f0706e82SJiri Benc 
115660750397SJohannes Berg 		/*
115760750397SJohannes Berg 		 * frames are queued in order, so if this one
115860750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
115960750397SJohannes Berg 		 * the queue) we can stop testing
116060750397SJohannes Berg 		 */
1161836341a7SJohannes Berg 		if (!skb)
1162836341a7SJohannes Berg 			break;
1163836341a7SJohannes Berg 
1164f0706e82SJiri Benc 		local->total_ps_buffered--;
1165bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1166bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1167d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1168f0706e82SJiri Benc 	}
11693393a608SJuuso Oikarinen 
117060750397SJohannes Berg 	/*
117160750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
117260750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
117360750397SJohannes Berg 	 * frames.
117460750397SJohannes Berg 	 */
117560750397SJohannes Berg 	sta_info_recalc_tim(sta);
117660750397SJohannes Berg 
117760750397SJohannes Berg 	/*
117860750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
117960750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
118060750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
118160750397SJohannes Berg 	 */
1182948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1183948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1184948d887dSJohannes Berg }
1185948d887dSJohannes Berg 
1186948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1187948d887dSJohannes Berg 					     struct sta_info *sta)
1188948d887dSJohannes Berg {
1189948d887dSJohannes Berg 	bool have_buffered = false;
1190948d887dSJohannes Berg 	int ac;
1191948d887dSJohannes Berg 
11923f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11933f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11943f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1195948d887dSJohannes Berg 		return false;
1196948d887dSJohannes Berg 
1197948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1198948d887dSJohannes Berg 		have_buffered |=
1199948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1200948d887dSJohannes Berg 
1201948d887dSJohannes Berg 	return have_buffered;
1202f0706e82SJiri Benc }
1203f0706e82SJiri Benc 
1204d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
120534e89507SJohannes Berg {
120634e89507SJohannes Berg 	struct ieee80211_local *local;
120734e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
12080ad49045SJohannes Berg 	int ret, i;
120934e89507SJohannes Berg 
121034e89507SJohannes Berg 	might_sleep();
121134e89507SJohannes Berg 
121234e89507SJohannes Berg 	if (!sta)
121334e89507SJohannes Berg 		return -ENOENT;
121434e89507SJohannes Berg 
121534e89507SJohannes Berg 	local = sta->local;
121634e89507SJohannes Berg 	sdata = sta->sdata;
121734e89507SJohannes Berg 
121883d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
121983d5cc01SJohannes Berg 
1220098a6070SJohannes Berg 	/*
1221098a6070SJohannes Berg 	 * Before removing the station from the driver and
1222098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1223098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1224098a6070SJohannes Berg 	 * will be sufficient.
1225098a6070SJohannes Berg 	 */
1226c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1227c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1228098a6070SJohannes Berg 
1229f59374ebSSara Sharon 	/*
1230f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1231f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1232f59374ebSSara Sharon 	 * all such frames to be processed.
1233f59374ebSSara Sharon 	 */
1234f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1235f59374ebSSara Sharon 
12360ad49045SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
12370ad49045SJohannes Berg 		struct link_sta_info *link_sta;
12380ad49045SJohannes Berg 
12390ad49045SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
12400ad49045SJohannes Berg 			continue;
12410ad49045SJohannes Berg 
12420ad49045SJohannes Berg 		link_sta = rcu_dereference_protected(sta->link[i],
12430ad49045SJohannes Berg 						     lockdep_is_held(&local->sta_mtx));
12440ad49045SJohannes Berg 
12450ad49045SJohannes Berg 		link_sta_info_hash_del(local, link_sta);
12460ad49045SJohannes Berg 	}
12470ad49045SJohannes Berg 
124834e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1249b01711beSJohannes Berg 	if (WARN_ON(ret))
125034e89507SJohannes Berg 		return ret;
125134e89507SJohannes Berg 
1252a7a6bdd0SArik Nemtsov 	/*
1253a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1254a7a6bdd0SArik Nemtsov 	 * removal.
1255a7a6bdd0SArik Nemtsov 	 */
1256a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1257a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1258a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1259a7a6bdd0SArik Nemtsov 	}
1260a7a6bdd0SArik Nemtsov 
1261794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1262ef044763SEliad Peller 	sta->removed = true;
12634d33960bSJohannes Berg 
12646a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
12656a9d1b91SJohannes Berg 
1266a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1267a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1268a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1269a710c816SJohannes Berg 
1270d778207bSJohannes Berg 	return 0;
1271d778207bSJohannes Berg }
1272d778207bSJohannes Berg 
1273d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1274d778207bSJohannes Berg {
1275d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1276d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
12770ef049dcSArnd Bergmann 	struct station_info *sinfo;
1278d778207bSJohannes Berg 	int ret;
1279d778207bSJohannes Berg 
1280d778207bSJohannes Berg 	/*
1281d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1282d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1283d778207bSJohannes Berg 	 */
1284d778207bSJohannes Berg 
1285d778207bSJohannes Berg 	might_sleep();
1286d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1287d778207bSJohannes Berg 
12885981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1289b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1290b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1291b16798f5SJohannes Berg 	}
1292b16798f5SJohannes Berg 
1293c8782078SJohannes Berg 	/* now keys can no longer be reached */
12946d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
129534e89507SJohannes Berg 
12969b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
12979b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
12989b7a86f3SJohannes Berg 
129934e89507SJohannes Berg 	sta->dead = true;
130034e89507SJohannes Berg 
130134e89507SJohannes Berg 	local->num_sta--;
130234e89507SJohannes Berg 	local->sta_generation++;
130334e89507SJohannes Berg 
130483d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1305f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1306f09603a2SJohannes Berg 		if (ret) {
130783d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
130883d5cc01SJohannes Berg 			break;
130983d5cc01SJohannes Berg 		}
131083d5cc01SJohannes Berg 	}
1311d9a7ddb0SJohannes Berg 
1312f09603a2SJohannes Berg 	if (sta->uploaded) {
1313f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1314f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1315f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1316f09603a2SJohannes Berg 	}
131734e89507SJohannes Berg 
1318bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1319bdcbd8e0SJohannes Berg 
13200ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
13210ef049dcSArnd Bergmann 	if (sinfo)
13220fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
13230ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
13240ef049dcSArnd Bergmann 	kfree(sinfo);
1325ec15e68bSJouni Malinen 
132634e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
132734e89507SJohannes Berg 
13283a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
13293a11ce08SJohannes Berg 
1330d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1331d778207bSJohannes Berg }
1332d778207bSJohannes Berg 
1333d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1334d778207bSJohannes Berg {
1335d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1336d778207bSJohannes Berg 
1337d778207bSJohannes Berg 	if (err)
1338d778207bSJohannes Berg 		return err;
1339d778207bSJohannes Berg 
1340d778207bSJohannes Berg 	synchronize_net();
1341d778207bSJohannes Berg 
1342d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
134334e89507SJohannes Berg 
134434e89507SJohannes Berg 	return 0;
134534e89507SJohannes Berg }
134634e89507SJohannes Berg 
134734e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
134834e89507SJohannes Berg {
134934e89507SJohannes Berg 	struct sta_info *sta;
135034e89507SJohannes Berg 	int ret;
135134e89507SJohannes Berg 
135234e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
13537852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
135434e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
135534e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
135634e89507SJohannes Berg 
135734e89507SJohannes Berg 	return ret;
135834e89507SJohannes Berg }
135934e89507SJohannes Berg 
136034e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
136134e89507SJohannes Berg 			      const u8 *addr)
136234e89507SJohannes Berg {
136334e89507SJohannes Berg 	struct sta_info *sta;
136434e89507SJohannes Berg 	int ret;
136534e89507SJohannes Berg 
136634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
13677852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
136834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
136934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
137034e89507SJohannes Berg 
137134e89507SJohannes Berg 	return ret;
137234e89507SJohannes Berg }
1373f0706e82SJiri Benc 
137434f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1375f0706e82SJiri Benc {
137634f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1377f0706e82SJiri Benc 	struct sta_info *sta;
13783393a608SJuuso Oikarinen 	bool timer_needed = false;
1379f0706e82SJiri Benc 
1380d0709a65SJohannes Berg 	rcu_read_lock();
1381d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
13823393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
13833393a608SJuuso Oikarinen 			timer_needed = true;
1384d0709a65SJohannes Berg 	rcu_read_unlock();
1385f0706e82SJiri Benc 
13865bb644a0SJohannes Berg 	if (local->quiescing)
13875bb644a0SJohannes Berg 		return;
13885bb644a0SJohannes Berg 
13893393a608SJuuso Oikarinen 	if (!timer_needed)
13903393a608SJuuso Oikarinen 		return;
13913393a608SJuuso Oikarinen 
139226d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
139326d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1394f0706e82SJiri Benc }
1395f0706e82SJiri Benc 
13967bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
13977bedd0cfSJohannes Berg {
13987bedd0cfSJohannes Berg 	int err;
13997bedd0cfSJohannes Berg 
140083e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
14017bedd0cfSJohannes Berg 	if (err)
14027bedd0cfSJohannes Berg 		return err;
14037bedd0cfSJohannes Berg 
1404ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1405ba6ddab9SJohannes Berg 	if (err) {
1406ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1407ba6ddab9SJohannes Berg 		return err;
1408ba6ddab9SJohannes Berg 	}
1409ba6ddab9SJohannes Berg 
14104d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
141134e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1412f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1413f0706e82SJiri Benc 
141434f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
14157bedd0cfSJohannes Berg 	return 0;
1416f0706e82SJiri Benc }
1417f0706e82SJiri Benc 
1418f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1419f0706e82SJiri Benc {
1420a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
142183e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1422ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1423f0706e82SJiri Benc }
1424f0706e82SJiri Benc 
1425051007d9SJohannes Berg 
1426e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1427f0706e82SJiri Benc {
1428b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1429f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1430d778207bSJohannes Berg 	LIST_HEAD(free_list);
143144213b5eSJohannes Berg 	int ret = 0;
1432f0706e82SJiri Benc 
1433d0709a65SJohannes Berg 	might_sleep();
1434d0709a65SJohannes Berg 
1435e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1436e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1437e716251dSJohannes Berg 
143834e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
143934e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1440e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1441e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1442d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1443d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
144434316837SJohannes Berg 			ret++;
144534316837SJohannes Berg 		}
144634e89507SJohannes Berg 	}
1447d778207bSJohannes Berg 
1448d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1449d778207bSJohannes Berg 		synchronize_net();
1450d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1451d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1452d778207bSJohannes Berg 	}
145334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
145444213b5eSJohannes Berg 
1455051007d9SJohannes Berg 	return ret;
1456051007d9SJohannes Berg }
1457051007d9SJohannes Berg 
145824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
145924723d1bSJohannes Berg 			  unsigned long exp_time)
146024723d1bSJohannes Berg {
146124723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
146224723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
146324723d1bSJohannes Berg 
146434e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1465e46a2cf9SMohammed Shafi Shajakhan 
1466e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1467b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1468b8da6b6aSJohannes Berg 
1469ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1470ec2b774eSMarek Lindner 			continue;
1471ec2b774eSMarek Lindner 
1472b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1473eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1474bdcbd8e0SJohannes Berg 				sta->sta.addr);
14753f52b7e3SMarco Porsch 
14763f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
14773f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
14783f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
14793f52b7e3SMarco Porsch 
148034e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
148124723d1bSJohannes Berg 		}
1482e46a2cf9SMohammed Shafi Shajakhan 	}
1483e46a2cf9SMohammed Shafi Shajakhan 
148434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
148524723d1bSJohannes Berg }
148617741cdcSJohannes Berg 
1487686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1488686b9cb9SBen Greear 						   const u8 *addr,
1489686b9cb9SBen Greear 						   const u8 *localaddr)
149017741cdcSJohannes Berg {
14917bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
149283e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
14937bedd0cfSJohannes Berg 	struct sta_info *sta;
149417741cdcSJohannes Berg 
1495686b9cb9SBen Greear 	/*
1496686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1497686b9cb9SBen Greear 	 * ... first in list.
1498686b9cb9SBen Greear 	 */
149983e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1500686b9cb9SBen Greear 		if (localaddr &&
1501b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1502686b9cb9SBen Greear 			continue;
1503f7c65594SJohannes Berg 		if (!sta->uploaded)
1504f7c65594SJohannes Berg 			return NULL;
150517741cdcSJohannes Berg 		return &sta->sta;
1506f7c65594SJohannes Berg 	}
1507f7c65594SJohannes Berg 
1508abe60632SJohannes Berg 	return NULL;
150917741cdcSJohannes Berg }
1510686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
15115ed176e1SJohannes Berg 
15125ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
15135ed176e1SJohannes Berg 					 const u8 *addr)
15145ed176e1SJohannes Berg {
1515f7c65594SJohannes Berg 	struct sta_info *sta;
15165ed176e1SJohannes Berg 
15175ed176e1SJohannes Berg 	if (!vif)
15185ed176e1SJohannes Berg 		return NULL;
15195ed176e1SJohannes Berg 
1520f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1521f7c65594SJohannes Berg 	if (!sta)
1522f7c65594SJohannes Berg 		return NULL;
15235ed176e1SJohannes Berg 
1524f7c65594SJohannes Berg 	if (!sta->uploaded)
1525f7c65594SJohannes Berg 		return NULL;
1526f7c65594SJohannes Berg 
1527f7c65594SJohannes Berg 	return &sta->sta;
15285ed176e1SJohannes Berg }
152917741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1530af818581SJohannes Berg 
1531e3685e03SJohannes Berg /* powersave support code */
1532e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
153350a9432dSJohannes Berg {
1534608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1535e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1536e3685e03SJohannes Berg 	struct sk_buff_head pending;
1537ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1538e3685e03SJohannes Berg 	unsigned long flags;
1539d012a605SMarco Porsch 	struct ps_data *ps;
1540d012a605SMarco Porsch 
15413918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
15423918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
15433918edb0SFelix Fietkau 				     u.ap);
15443918edb0SFelix Fietkau 
15453918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1546d012a605SMarco Porsch 		ps = &sdata->bss->ps;
15473f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
15483f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1549d012a605SMarco Porsch 	else
1550d012a605SMarco Porsch 		return;
155150a9432dSJohannes Berg 
1552c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
155347086fc5SJohannes Berg 
15545a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1555948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1556ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1557948d887dSJohannes Berg 
155830686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
155912375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1560af818581SJohannes Berg 
1561ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1562adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1563ba8c3d6fSFelix Fietkau 			continue;
1564ba8c3d6fSFelix Fietkau 
156518667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1566ba8c3d6fSFelix Fietkau 	}
1567ba8c3d6fSFelix Fietkau 
1568948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1569af818581SJohannes Berg 
15701d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
15711d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1572af818581SJohannes Berg 	/* Send all buffered frames to the station */
1573948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1574948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1575948d887dSJohannes Berg 
1576987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1577948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1578987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1579948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1580948d887dSJohannes Berg 		filtered += tmp - count;
1581948d887dSJohannes Berg 		count = tmp;
1582948d887dSJohannes Berg 
1583987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1584948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1585987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1586948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1587948d887dSJohannes Berg 		buffered += tmp - count;
1588948d887dSJohannes Berg 	}
1589948d887dSJohannes Berg 
1590e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
15915ac2e350SJohannes Berg 
15925ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
15935ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
15945ac2e350SJohannes Berg 
15955ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
15965ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
15975ac2e350SJohannes Berg 	 */
15985ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
15995ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
16001d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1601948d887dSJohannes Berg 
1602e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1603e3685e03SJohannes Berg 
1604af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1605af818581SJohannes Berg 
1606c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1607c868cb35SJohannes Berg 
1608bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
16092595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1610bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
161117c18bf8SJohannes Berg 
161217c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1613af818581SJohannes Berg }
1614af818581SJohannes Berg 
16150ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1616b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
16170ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1618ce662b44SJohannes Berg {
16190ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1620ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1621ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1622ce662b44SJohannes Berg 	struct sk_buff *skb;
1623ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1624ce662b44SJohannes Berg 	__le16 fc;
1625a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1626ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
162755de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1628ce662b44SJohannes Berg 
1629ce662b44SJohannes Berg 	if (qos) {
1630ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1631ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1632ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1633ce662b44SJohannes Berg 	} else {
1634ce662b44SJohannes Berg 		size -= 2;
1635ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1636ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1637ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1638ce662b44SJohannes Berg 	}
1639ce662b44SJohannes Berg 
1640ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1641ce662b44SJohannes Berg 	if (!skb)
1642ce662b44SJohannes Berg 		return;
1643ce662b44SJohannes Berg 
1644ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1645ce662b44SJohannes Berg 
16464df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1647ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1648ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1649ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1650ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1651ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1652864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1653ce662b44SJohannes Berg 
1654ce662b44SJohannes Berg 	skb->priority = tid;
1655ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
165659b66255SJohannes Berg 	if (qos) {
1657ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1658ce662b44SJohannes Berg 
16590ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1660ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1661ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
16620ead2510SEmmanuel Grumbach 			if (more_data)
16630ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
16640ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
16650ead2510SEmmanuel Grumbach 		}
1666ce662b44SJohannes Berg 	}
1667ce662b44SJohannes Berg 
1668ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1669ce662b44SJohannes Berg 
1670ce662b44SJohannes Berg 	/*
1671ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1672ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1673deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1674deeaee19SJohannes Berg 	 * ends the poll/service period.
1675ce662b44SJohannes Berg 	 */
167602f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1677deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1678ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1679ce662b44SJohannes Berg 
16806b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
16816b127c71SSujith Manoharan 
1682b77cf4f8SJohannes Berg 	if (call_driver)
1683b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1684b77cf4f8SJohannes Berg 					  reason, false);
168540b96408SJohannes Berg 
168689afe614SJohannes Berg 	skb->dev = sdata->dev;
168789afe614SJohannes Berg 
168855de908aSJohannes Berg 	rcu_read_lock();
1689d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
169055de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
169155de908aSJohannes Berg 		rcu_read_unlock();
169255de908aSJohannes Berg 		kfree_skb(skb);
169355de908aSJohannes Berg 		return;
169455de908aSJohannes Berg 	}
169555de908aSJohannes Berg 
169673c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
169708aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
169855de908aSJohannes Berg 	rcu_read_unlock();
1699ce662b44SJohannes Berg }
1700ce662b44SJohannes Berg 
17010a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
17020a1cb809SJohannes Berg {
17030a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
17040a1cb809SJohannes Berg 	if (tids & 0xF8)
17050a1cb809SJohannes Berg 		return fls(tids) - 1;
17060a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
17070a1cb809SJohannes Berg 	if (tids & BIT(0))
17080a1cb809SJohannes Berg 		return 0;
17090a1cb809SJohannes Berg 	return fls(tids) - 1;
17100a1cb809SJohannes Berg }
17110a1cb809SJohannes Berg 
17120ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
17130ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
17140ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
17150ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
17160ead2510SEmmanuel Grumbach  */
17170ead2510SEmmanuel Grumbach static bool
17180ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
17190ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
17200ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
17210ead2510SEmmanuel Grumbach {
17220ead2510SEmmanuel Grumbach 	int ac;
17230ead2510SEmmanuel Grumbach 
17240ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
17250ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
17260ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
17270ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
17280ead2510SEmmanuel Grumbach 	 */
17290ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
17300ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
17310ead2510SEmmanuel Grumbach 		return true;
17320ead2510SEmmanuel Grumbach 
17330ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1734f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
17350ead2510SEmmanuel Grumbach 			continue;
17360ead2510SEmmanuel Grumbach 
17370ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17380ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
17390ead2510SEmmanuel Grumbach 			return true;
17400ead2510SEmmanuel Grumbach 	}
17410ead2510SEmmanuel Grumbach 
17420ead2510SEmmanuel Grumbach 	return false;
17430ead2510SEmmanuel Grumbach }
17440ead2510SEmmanuel Grumbach 
174547086fc5SJohannes Berg static void
17460ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
17470ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
17480ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
17490ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1750af818581SJohannes Berg {
1751af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1752af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1753948d887dSJohannes Berg 	int ac;
1754af818581SJohannes Berg 
1755f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1756948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
17574049e09aSJohannes Berg 		unsigned long tids;
17584049e09aSJohannes Berg 
1759f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1760948d887dSJohannes Berg 			continue;
1761948d887dSJohannes Berg 
17624049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
17634049e09aSJohannes Berg 
1764f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1765f9f760b4SJohannes Berg 		 * release from hardware queues
1766f9f760b4SJohannes Berg 		 */
17670ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
17680ead2510SEmmanuel Grumbach 			*driver_release_tids |=
17690ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
17700ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1771ba8c3d6fSFelix Fietkau 		}
1772f9f760b4SJohannes Berg 
17730ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
177447086fc5SJohannes Berg 			struct sk_buff *skb;
177547086fc5SJohannes Berg 
177647086fc5SJohannes Berg 			while (n_frames > 0) {
1777948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1778948d887dSJohannes Berg 				if (!skb) {
177947086fc5SJohannes Berg 					skb = skb_dequeue(
178047086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1781af818581SJohannes Berg 					if (skb)
1782af818581SJohannes Berg 						local->total_ps_buffered--;
1783af818581SJohannes Berg 				}
178447086fc5SJohannes Berg 				if (!skb)
178547086fc5SJohannes Berg 					break;
178647086fc5SJohannes Berg 				n_frames--;
17870ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
178847086fc5SJohannes Berg 			}
1789948d887dSJohannes Berg 		}
1790948d887dSJohannes Berg 
17910ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
17920ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
17930ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
17944049e09aSJohannes Berg 		 */
1795948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17960ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1797948d887dSJohannes Berg 			break;
1798948d887dSJohannes Berg 	}
1799948d887dSJohannes Berg }
1800af818581SJohannes Berg 
18010ead2510SEmmanuel Grumbach static void
18020ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
18030ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
18040ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
18050ead2510SEmmanuel Grumbach {
18060ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
18070ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
18080ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
18090ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
18100ead2510SEmmanuel Grumbach 	bool more_data;
18110ead2510SEmmanuel Grumbach 
18120ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
18130ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
18140ead2510SEmmanuel Grumbach 
18150ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
18160ead2510SEmmanuel Grumbach 
18170ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
18180ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
18190ead2510SEmmanuel Grumbach 
18200ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
18210ead2510SEmmanuel Grumbach 
18221a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
18230ead2510SEmmanuel Grumbach 		driver_release_tids =
18240ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
18250ead2510SEmmanuel Grumbach 
1826f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1827f438ceb8SEmmanuel Grumbach 		int tid, ac;
18284049e09aSJohannes Berg 
1829ce662b44SJohannes Berg 		/*
1830ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1831ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1832ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1833ce662b44SJohannes Berg 		 *
1834ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1835ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1836ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1837ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1838ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1839ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1840ce662b44SJohannes Berg 		 *
1841ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1842ce662b44SJohannes Berg 		 */
1843ce662b44SJohannes Berg 
1844ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1845f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1846d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1847d7f84244SEmmanuel Grumbach 				break;
1848f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1849ce662b44SJohannes Berg 
18500ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1851f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
185247086fc5SJohannes Berg 		struct sk_buff_head pending;
185347086fc5SJohannes Berg 		struct sk_buff *skb;
185440b96408SJohannes Berg 		int num = 0;
185540b96408SJohannes Berg 		u16 tids = 0;
1856b77cf4f8SJohannes Berg 		bool need_null = false;
185747086fc5SJohannes Berg 
185847086fc5SJohannes Berg 		skb_queue_head_init(&pending);
185947086fc5SJohannes Berg 
186047086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1861af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
186247086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
186340b96408SJohannes Berg 			u8 *qoshdr = NULL;
186440b96408SJohannes Berg 
186540b96408SJohannes Berg 			num++;
1866af818581SJohannes Berg 
1867af818581SJohannes Berg 			/*
186847086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
186947086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
187047086fc5SJohannes Berg 			 * exchange.
1871af818581SJohannes Berg 			 */
18726b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
18736b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1874af818581SJohannes Berg 
187547086fc5SJohannes Berg 			/*
187647086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
187747086fc5SJohannes Berg 			 * more buffered frames for this STA
187847086fc5SJohannes Berg 			 */
187924b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
188047086fc5SJohannes Berg 				hdr->frame_control |=
188147086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
188224b9c373SJanusz.Dziedzic@tieto.com 			else
188324b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
188424b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1885af818581SJohannes Berg 
188640b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
188740b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
188840b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
188940b96408SJohannes Berg 
1890b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1891b77cf4f8SJohannes Berg 
1892b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1893b77cf4f8SJohannes Berg 
1894b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1895b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1896b77cf4f8SJohannes Berg 				continue;
1897b77cf4f8SJohannes Berg 
1898b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1899b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1900b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1901b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1902b77cf4f8SJohannes Berg 				break;
1903b77cf4f8SJohannes Berg 			}
1904b77cf4f8SJohannes Berg 
1905b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1906b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1907b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1908b77cf4f8SJohannes Berg 			 * and be done.
1909b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1910b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1911b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1912b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1913b77cf4f8SJohannes Berg 			 *
1914b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1915b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1916b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1917b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1918b77cf4f8SJohannes Berg 			 *
1919b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1920b77cf4f8SJohannes Berg 			 */
1921b77cf4f8SJohannes Berg 			if (qoshdr) {
192240b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1923deeaee19SJohannes Berg 
192447086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
192547086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1926b77cf4f8SJohannes Berg 			} else {
1927b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1928b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1929b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1930b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1931b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1932b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1933b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1934b77cf4f8SJohannes Berg 				 */
1935b77cf4f8SJohannes Berg 				hdr->frame_control |=
1936b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1937b77cf4f8SJohannes Berg 				need_null = true;
1938b77cf4f8SJohannes Berg 				num++;
193952a3f20cSMarco Porsch 			}
1940b77cf4f8SJohannes Berg 			break;
194147086fc5SJohannes Berg 		}
194247086fc5SJohannes Berg 
194340b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
194440b96408SJohannes Berg 					  reason, more_data);
194540b96408SJohannes Berg 
194647086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1947af818581SJohannes Berg 
1948b77cf4f8SJohannes Berg 		if (need_null)
1949b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
19500ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
19510ead2510SEmmanuel Grumbach 				reason, false, false);
1952b77cf4f8SJohannes Berg 
1953c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1954af818581SJohannes Berg 	} else {
1955ba8c3d6fSFelix Fietkau 		int tid;
1956ba8c3d6fSFelix Fietkau 
1957af818581SJohannes Berg 		/*
19584049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
19594049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1960f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1961f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1962f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1963f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1964f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1965f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1966af818581SJohannes Berg 		 */
19674049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
196847086fc5SJohannes Berg 					    n_frames, reason, more_data);
19694049e09aSJohannes Berg 
19704049e09aSJohannes Berg 		/*
19714049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
19724049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1973f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
19744049e09aSJohannes Berg 		 * release function.
1975f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1976ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1977ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
19784049e09aSJohannes Berg 		 */
1979ba8c3d6fSFelix Fietkau 
1980ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1981adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1982adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
19831e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1984ba8c3d6fSFelix Fietkau 				continue;
1985ba8c3d6fSFelix Fietkau 
1986ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1987ba8c3d6fSFelix Fietkau 			break;
1988ba8c3d6fSFelix Fietkau 		}
1989af818581SJohannes Berg 	}
1990af818581SJohannes Berg }
1991af818581SJohannes Berg 
1992af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1993af818581SJohannes Berg {
199447086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1995af818581SJohannes Berg 
1996af818581SJohannes Berg 	/*
199747086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
199847086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
199947086fc5SJohannes Berg 	 * only from the non-enabled ones.
2000af818581SJohannes Berg 	 */
200147086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
200247086fc5SJohannes Berg 		ignore_for_response = 0;
2003af818581SJohannes Berg 
200447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
200547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
2006af818581SJohannes Berg }
200747086fc5SJohannes Berg 
200847086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
200947086fc5SJohannes Berg {
201047086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
201147086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
201247086fc5SJohannes Berg 
201347086fc5SJohannes Berg 	/*
201447086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
201547086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
201647086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
201747086fc5SJohannes Berg 	 * actually getting called.
201847086fc5SJohannes Berg 	 */
201947086fc5SJohannes Berg 	if (!delivery_enabled)
202047086fc5SJohannes Berg 		return;
202147086fc5SJohannes Berg 
202247086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
202347086fc5SJohannes Berg 	case 1:
202447086fc5SJohannes Berg 		n_frames = 2;
202547086fc5SJohannes Berg 		break;
202647086fc5SJohannes Berg 	case 2:
202747086fc5SJohannes Berg 		n_frames = 4;
202847086fc5SJohannes Berg 		break;
202947086fc5SJohannes Berg 	case 3:
203047086fc5SJohannes Berg 		n_frames = 6;
203147086fc5SJohannes Berg 		break;
203247086fc5SJohannes Berg 	case 0:
203347086fc5SJohannes Berg 		/* XXX: what is a good value? */
203413a8098aSAndrei Otcheretianski 		n_frames = 128;
203547086fc5SJohannes Berg 		break;
203647086fc5SJohannes Berg 	}
203747086fc5SJohannes Berg 
203847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
203947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
2040af818581SJohannes Berg }
2041af818581SJohannes Berg 
2042af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2043af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
2044af818581SJohannes Berg {
2045af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2046af818581SJohannes Berg 
2047b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
2048b5878a2dSJohannes Berg 
20495ac2e350SJohannes Berg 	if (block) {
2050c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
205117c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
20525ac2e350SJohannes Berg 		return;
20535ac2e350SJohannes Berg 	}
20545ac2e350SJohannes Berg 
20555ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
20565ac2e350SJohannes Berg 		return;
20575ac2e350SJohannes Berg 
20585ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
20595ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
20605ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20615ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20625ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
20635ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
20645ac2e350SJohannes Berg 		/* must be asleep in this case */
20655ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20665ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20675ac2e350SJohannes Berg 	} else {
20685ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
206917c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
20705ac2e350SJohannes Berg 	}
2071af818581SJohannes Berg }
2072af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
2073dcf55fb5SFelix Fietkau 
2074e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
207537fbd908SJohannes Berg {
207637fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
207737fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
207837fbd908SJohannes Berg 
207937fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
208037fbd908SJohannes Berg 
208137fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
208237fbd908SJohannes Berg }
2083e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
208437fbd908SJohannes Berg 
20850ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
20860ead2510SEmmanuel Grumbach {
20870ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
20880ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
20890ead2510SEmmanuel Grumbach 	bool more_data;
20900ead2510SEmmanuel Grumbach 
20910ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
20920ead2510SEmmanuel Grumbach 
20930ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
20940ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
20950ead2510SEmmanuel Grumbach 					       reason, 0);
20960ead2510SEmmanuel Grumbach 
20970ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
20980ead2510SEmmanuel Grumbach }
20990ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
21000ead2510SEmmanuel Grumbach 
2101042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2102042ec453SJohannes Berg 				u8 tid, bool buffered)
2103dcf55fb5SFelix Fietkau {
2104dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2105dcf55fb5SFelix Fietkau 
21065a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2107042ec453SJohannes Berg 		return;
2108042ec453SJohannes Berg 
21091b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
21101b000789SJohannes Berg 
2111948d887dSJohannes Berg 	if (buffered)
2112948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2113948d887dSJohannes Berg 	else
2114948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2115948d887dSJohannes Berg 
2116c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2117dcf55fb5SFelix Fietkau }
2118042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2119d9a7ddb0SJohannes Berg 
2120b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2121b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2122b4809e94SToke Høiland-Jørgensen {
2123942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2124942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2125942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2126942741daSFelix Fietkau 	u32 airtime = 0;
2127c77bfab9SFelix Fietkau 	u32 diff;
2128b4809e94SToke Høiland-Jørgensen 
2129942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2130942741daSFelix Fietkau 		airtime += tx_airtime;
2131942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2132942741daSFelix Fietkau 		airtime += rx_airtime;
2133b4809e94SToke Høiland-Jørgensen 
2134942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2135942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2136942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2137c77bfab9SFelix Fietkau 
2138c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2139c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2140942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2141c77bfab9SFelix Fietkau 
2142942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2143b4809e94SToke Høiland-Jørgensen }
2144b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2145b4809e94SToke Høiland-Jørgensen 
21469b41a9d7SJohannes Berg void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
21474c51541dSBenjamin Berg {
21484c51541dSBenjamin Berg 	bool first = true;
21499b41a9d7SJohannes Berg 	int link_id;
21504c51541dSBenjamin Berg 
21519b41a9d7SJohannes Berg 	if (!sta->sta.valid_links || !sta->sta.mlo) {
21529b41a9d7SJohannes Berg 		sta->sta.cur = &sta->sta.deflink.agg;
21534c51541dSBenjamin Berg 		return;
21544c51541dSBenjamin Berg 	}
21554c51541dSBenjamin Berg 
21564c51541dSBenjamin Berg 	rcu_read_lock();
21579b41a9d7SJohannes Berg 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
21589b41a9d7SJohannes Berg 		struct ieee80211_link_sta *link_sta;
21599b41a9d7SJohannes Berg 		int i;
21609b41a9d7SJohannes Berg 
21619b41a9d7SJohannes Berg 		if (!(active_links & BIT(link_id)))
21629b41a9d7SJohannes Berg 			continue;
21639b41a9d7SJohannes Berg 
21649b41a9d7SJohannes Berg 		link_sta = rcu_dereference(sta->sta.link[link_id]);
21659b41a9d7SJohannes Berg 		if (!link_sta)
21669b41a9d7SJohannes Berg 			continue;
21679b41a9d7SJohannes Berg 
21684c51541dSBenjamin Berg 		if (first) {
21699b41a9d7SJohannes Berg 			sta->cur = sta->sta.deflink.agg;
21704c51541dSBenjamin Berg 			first = false;
21714c51541dSBenjamin Berg 			continue;
21724c51541dSBenjamin Berg 		}
21734c51541dSBenjamin Berg 
21744c51541dSBenjamin Berg 		sta->cur.max_amsdu_len =
21754c51541dSBenjamin Berg 			min(sta->cur.max_amsdu_len,
21764c51541dSBenjamin Berg 			    link_sta->agg.max_amsdu_len);
21774c51541dSBenjamin Berg 		sta->cur.max_rc_amsdu_len =
21784c51541dSBenjamin Berg 			min(sta->cur.max_rc_amsdu_len,
21794c51541dSBenjamin Berg 			    link_sta->agg.max_rc_amsdu_len);
21804c51541dSBenjamin Berg 
21814c51541dSBenjamin Berg 		for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
21824c51541dSBenjamin Berg 			sta->cur.max_tid_amsdu_len[i] =
21834c51541dSBenjamin Berg 				min(sta->cur.max_tid_amsdu_len[i],
21844c51541dSBenjamin Berg 				    link_sta->agg.max_tid_amsdu_len[i]);
21854c51541dSBenjamin Berg 	}
21864c51541dSBenjamin Berg 	rcu_read_unlock();
21874c51541dSBenjamin Berg 
21889b41a9d7SJohannes Berg 	sta->sta.cur = &sta->cur;
21899b41a9d7SJohannes Berg }
21909b41a9d7SJohannes Berg 
21919b41a9d7SJohannes Berg void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
21929b41a9d7SJohannes Berg {
21939b41a9d7SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
21949b41a9d7SJohannes Berg 
21959b41a9d7SJohannes Berg 	__ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
21964c51541dSBenjamin Berg }
21974c51541dSBenjamin Berg EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
21984c51541dSBenjamin Berg 
21993ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
22003ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
22013ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
22023ace10f5SKan Yan {
22033ace10f5SKan Yan 	int tx_pending;
22043ace10f5SKan Yan 
2205911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2206911bde0fSToke Høiland-Jørgensen 		return;
2207911bde0fSToke Høiland-Jørgensen 
22083ace10f5SKan Yan 	if (!tx_completed) {
22093ace10f5SKan Yan 		if (sta)
22103ace10f5SKan Yan 			atomic_add(tx_airtime,
22113ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
22123ace10f5SKan Yan 
22133ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
22148e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
22153ace10f5SKan Yan 		return;
22163ace10f5SKan Yan 	}
22173ace10f5SKan Yan 
22183ace10f5SKan Yan 	if (sta) {
22193ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
22203ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
222104e35caaSFelix Fietkau 		if (tx_pending < 0)
22223ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
22233ace10f5SKan Yan 				       tx_pending, 0);
22243ace10f5SKan Yan 	}
22253ace10f5SKan Yan 
22268e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
22273ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
22288e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
22293ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
22303ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
22313ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
22328e4bac06SFelix Fietkau 		      tx_airtime)) {
22338e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
22343ace10f5SKan Yan 			       tx_pending, 0);
22358e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
22368e4bac06SFelix Fietkau 	}
22373ace10f5SKan Yan }
22383ace10f5SKan Yan 
223983d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2240d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2241d9a7ddb0SJohannes Berg {
22428bf11d8dSJohannes Berg 	might_sleep();
2243d9a7ddb0SJohannes Berg 
2244d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2245d9a7ddb0SJohannes Berg 		return 0;
2246d9a7ddb0SJohannes Berg 
2247f09603a2SJohannes Berg 	/* check allowed transitions first */
2248f09603a2SJohannes Berg 
2249d9a7ddb0SJohannes Berg 	switch (new_state) {
2250d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2251f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2252d9a7ddb0SJohannes Berg 			return -EINVAL;
2253d9a7ddb0SJohannes Berg 		break;
2254d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2255f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2256f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2257d9a7ddb0SJohannes Berg 			return -EINVAL;
2258d9a7ddb0SJohannes Berg 		break;
2259d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2260f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2261f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2262d9a7ddb0SJohannes Berg 			return -EINVAL;
2263d9a7ddb0SJohannes Berg 		break;
2264d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2265f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2266d9a7ddb0SJohannes Berg 			return -EINVAL;
2267d9a7ddb0SJohannes Berg 		break;
2268d9a7ddb0SJohannes Berg 	default:
2269d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2270d9a7ddb0SJohannes Berg 		return -EINVAL;
2271d9a7ddb0SJohannes Berg 	}
2272d9a7ddb0SJohannes Berg 
2273bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2274bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2275f09603a2SJohannes Berg 
2276f09603a2SJohannes Berg 	/*
2277f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2278f09603a2SJohannes Berg 	 * fail the transition
2279f09603a2SJohannes Berg 	 */
2280f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2281f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2282f09603a2SJohannes Berg 					sta->sta_state, new_state);
2283f09603a2SJohannes Berg 		if (err)
2284f09603a2SJohannes Berg 			return err;
2285f09603a2SJohannes Berg 	}
2286f09603a2SJohannes Berg 
2287f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2288f09603a2SJohannes Berg 
2289f09603a2SJohannes Berg 	switch (new_state) {
2290f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2291f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2292f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2293f09603a2SJohannes Berg 		break;
2294f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2295a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2296f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2297a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2298f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
22990cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
230052cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
230152cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2302a7201a6cSIlan Peer 		}
2303f09603a2SJohannes Berg 		break;
2304f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2305f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2306f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
23079cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
23080cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
230952cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
231052cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2311f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
231272f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2313f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
231417c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
231549ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2316f09603a2SJohannes Berg 		}
2317f09603a2SJohannes Berg 		break;
2318f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2319f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
232072f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2321f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
232217c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
232349ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2324f09603a2SJohannes Berg 		}
23253e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
23263e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
23273e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
23283e493173SJouni Malinen 						    sta->sta.addr);
2329f09603a2SJohannes Berg 		break;
2330f09603a2SJohannes Berg 	default:
2331f09603a2SJohannes Berg 		break;
2332f09603a2SJohannes Berg 	}
2333f09603a2SJohannes Berg 
2334d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2335d9a7ddb0SJohannes Berg 
2336d9a7ddb0SJohannes Berg 	return 0;
2337d9a7ddb0SJohannes Berg }
2338687da132SEmmanuel Grumbach 
2339c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2340c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2341c9c5962bSJohannes Berg {
2342046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2343c9c5962bSJohannes Berg 	int cpu;
2344c9c5962bSJohannes Berg 
2345046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2346c9c5962bSJohannes Berg 		return stats;
2347c9c5962bSJohannes Berg 
2348c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2349c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2350c9c5962bSJohannes Berg 
2351046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2352c9c5962bSJohannes Berg 
2353c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2354c9c5962bSJohannes Berg 			stats = cpustats;
2355c9c5962bSJohannes Berg 	}
2356c9c5962bSJohannes Berg 
2357c9c5962bSJohannes Berg 	return stats;
2358c9c5962bSJohannes Berg }
2359c9c5962bSJohannes Berg 
236041cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
23614f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2362fbd6ff5cSJohannes Berg {
2363dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2364fbd6ff5cSJohannes Berg 
2365dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
23667f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
23674f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2368dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2369dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2370dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2371dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
23727f406cd1SJohannes Berg 		break;
23737f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
23744f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2375dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2376dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2377dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
23787f406cd1SJohannes Berg 		break;
23797f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2380fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2381fbd6ff5cSJohannes Berg 		u16 brate;
23824f6b1b3dSJohannes Berg 		unsigned int shift;
2383dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2384dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2385fbd6ff5cSJohannes Berg 
2386dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
23878b783d10SThomas Pedersen 
23888b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
23898b783d10SThomas Pedersen 			break;
23908b783d10SThomas Pedersen 
2391dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
23924f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
23934f6b1b3dSJohannes Berg 			shift = 2;
23944f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
23954f6b1b3dSJohannes Berg 			shift = 1;
23964f6b1b3dSJohannes Berg 		else
23974f6b1b3dSJohannes Berg 			shift = 0;
2398fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
23997f406cd1SJohannes Berg 		break;
24007f406cd1SJohannes Berg 		}
240141cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
240241cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
240341cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
240441cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
240541cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
240641cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
240741cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
240841cbb0f5SLuca Coelho 		break;
2409*f66c48afSJohannes Berg 	case STA_STATS_RATE_TYPE_EHT:
2410*f66c48afSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_EHT_MCS;
2411*f66c48afSJohannes Berg 		rinfo->mcs = STA_STATS_GET(EHT_MCS, rate);
2412*f66c48afSJohannes Berg 		rinfo->nss = STA_STATS_GET(EHT_NSS, rate);
2413*f66c48afSJohannes Berg 		rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate);
2414*f66c48afSJohannes Berg 		rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate);
2415*f66c48afSJohannes Berg 		break;
2416fbd6ff5cSJohannes Berg 	}
24174f6b1b3dSJohannes Berg }
2418fbd6ff5cSJohannes Berg 
2419a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
24204f6b1b3dSJohannes Berg {
24216aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
24224f6b1b3dSJohannes Berg 
24234f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2424a17d93ffSBen Greear 		return -EINVAL;
2425a17d93ffSBen Greear 
24264f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2427a17d93ffSBen Greear 	return 0;
2428fbd6ff5cSJohannes Berg }
2429fbd6ff5cSJohannes Berg 
2430b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2431b255b72bSSeevalamuthu Mariappan 					int tid)
2432b255b72bSSeevalamuthu Mariappan {
2433b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2434b255b72bSSeevalamuthu Mariappan 	u64 value;
2435b255b72bSSeevalamuthu Mariappan 
2436b255b72bSSeevalamuthu Mariappan 	do {
2437d120d1a6SThomas Gleixner 		start = u64_stats_fetch_begin(&rxstats->syncp);
2438b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2439d120d1a6SThomas Gleixner 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2440b255b72bSSeevalamuthu Mariappan 
2441b255b72bSSeevalamuthu Mariappan 	return value;
2442b255b72bSSeevalamuthu Mariappan }
2443b255b72bSSeevalamuthu Mariappan 
24440f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
24450f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
24460f9c5a61SJohannes Berg 			     int tid)
24470f9c5a61SJohannes Berg {
24480f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2449b255b72bSSeevalamuthu Mariappan 	int cpu;
24500f9c5a61SJohannes Berg 
24510f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2452046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2453046d2e7cSSriram R 							   tid);
24540f9c5a61SJohannes Berg 
2455046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2456b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2457b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2458b255b72bSSeevalamuthu Mariappan 
2459046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2460046d2e7cSSriram R 						     cpu);
2461b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2462b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2463b255b72bSSeevalamuthu Mariappan 			}
2464b255b72bSSeevalamuthu Mariappan 		}
24650f9c5a61SJohannes Berg 
24660f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
24670f9c5a61SJohannes Berg 	}
24680f9c5a61SJohannes Berg 
24690f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
24700f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2471046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
24720f9c5a61SJohannes Berg 	}
24730f9c5a61SJohannes Berg 
24740f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
24750f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
24760f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2477046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
24780f9c5a61SJohannes Berg 	}
24790f9c5a61SJohannes Berg 
24800f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
24810f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
24820f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2483046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
24840f9c5a61SJohannes Berg 	}
24852fe4a29aSToke Høiland-Jørgensen 
2486107395f9SAlexander Wetzel 	if (tid < IEEE80211_NUM_TIDS) {
24872fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
24882fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
24892fe4a29aSToke Høiland-Jørgensen 
24902fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
24912fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
24922fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
24932fe4a29aSToke Høiland-Jørgensen 
24942fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
24952fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
24962fe4a29aSToke Høiland-Jørgensen 	}
24970f9c5a61SJohannes Berg }
24980f9c5a61SJohannes Berg 
2499c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2500c9c5962bSJohannes Berg {
2501c9c5962bSJohannes Berg 	unsigned int start;
2502c9c5962bSJohannes Berg 	u64 value;
2503c9c5962bSJohannes Berg 
2504c9c5962bSJohannes Berg 	do {
2505d120d1a6SThomas Gleixner 		start = u64_stats_fetch_begin(&rxstats->syncp);
2506c9c5962bSJohannes Berg 		value = rxstats->bytes;
2507d120d1a6SThomas Gleixner 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2508c9c5962bSJohannes Berg 
2509c9c5962bSJohannes Berg 	return value;
2510c9c5962bSJohannes Berg }
2511c9c5962bSJohannes Berg 
25120fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
25130fdf1493SJohannes Berg 		   bool tidstats)
2514b7ffbd7eSJohannes Berg {
2515b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2516b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2517b7ffbd7eSJohannes Berg 	u32 thr = 0;
2518c9c5962bSJohannes Berg 	int i, ac, cpu;
2519c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2520c9c5962bSJohannes Berg 
2521c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2522b7ffbd7eSJohannes Berg 
2523b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2524b7ffbd7eSJohannes Berg 
2525225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2526225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2527225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2528225b8189SJohannes Berg 	 */
2529225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2530bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2531225b8189SJohannes Berg 
25322b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2533a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2534a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2535a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2536a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
25379cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2538a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2539976bd9efSJohannes Berg 
2540976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2541bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2542bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2543a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2544976bd9efSJohannes Berg 	}
2545b7ffbd7eSJohannes Berg 
254684b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
25479cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2548e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2549b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
25502b9a7e1bSJohannes Berg 
2551a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2552a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2553b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
25542b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2555046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2556a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2557b7ffbd7eSJohannes Berg 	}
25582b9a7e1bSJohannes Berg 
2559a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
25602b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
25612b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2562046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2563a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
25642b9a7e1bSJohannes Berg 	}
25652b9a7e1bSJohannes Berg 
2566a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2567a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2568046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
25690f9c5a61SJohannes Berg 
2570046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2571c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2572c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2573c9c5962bSJohannes Berg 
2574046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2575046d2e7cSSriram R 						     cpu);
2576c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2577c9c5962bSJohannes Berg 			}
2578c9c5962bSJohannes Berg 		}
2579c9c5962bSJohannes Berg 
2580a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
25812b9a7e1bSJohannes Berg 	}
25822b9a7e1bSJohannes Berg 
2583a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2584046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2585046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2586c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2587c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2588c9c5962bSJohannes Berg 
2589046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2590046d2e7cSSriram R 						     cpu);
2591c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2592c9c5962bSJohannes Berg 			}
2593c9c5962bSJohannes Berg 		}
2594a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
25952b9a7e1bSJohannes Berg 	}
25962b9a7e1bSJohannes Berg 
2597a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2598046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2599a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
26002b9a7e1bSJohannes Berg 	}
26012b9a7e1bSJohannes Berg 
2602a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2603046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2604a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
26052b9a7e1bSJohannes Berg 	}
26062b9a7e1bSJohannes Berg 
2607b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2608b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2609b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2610b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2611b4809e94SToke Høiland-Jørgensen 	}
2612b4809e94SToke Høiland-Jørgensen 
2613b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2614b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2615b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2616b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2617b4809e94SToke Høiland-Jørgensen 	}
2618b4809e94SToke Høiland-Jørgensen 
2619b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2620942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2621b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2622b4809e94SToke Høiland-Jørgensen 	}
2623b4809e94SToke Høiland-Jørgensen 
2624046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2625046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2626c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2627c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2628c9c5962bSJohannes Berg 
2629046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2630e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2631c9c5962bSJohannes Berg 		}
2632c9c5962bSJohannes Berg 	}
2633b7ffbd7eSJohannes Berg 
2634225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2635225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2636a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2637a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2638225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2639225b8189SJohannes Berg 	}
2640225b8189SJohannes Berg 
264130686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
264230686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2643a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2644c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2645a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2646b7ffbd7eSJohannes Berg 		}
26472b9a7e1bSJohannes Berg 
2648046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2649a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
265040d9a38aSJohannes Berg 			sinfo->signal_avg =
2651046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2652a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
26532b9a7e1bSJohannes Berg 		}
26542b9a7e1bSJohannes Berg 	}
26552b9a7e1bSJohannes Berg 
2656c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2657c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2658c9c5962bSJohannes Berg 	 * pcpu statistics
2659c9c5962bSJohannes Berg 	 */
2660c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2661a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2662a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2663a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2664046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2665a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2666b7ffbd7eSJohannes Berg 
2667c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2668c9c5962bSJohannes Berg 
2669b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2670e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2671c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2672b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2673046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2674b7ffbd7eSJohannes Berg 		}
2675b7ffbd7eSJohannes Berg 	}
2676b7ffbd7eSJohannes Berg 
26778a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
26788a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2679046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2680e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2681a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
26822b9a7e1bSJohannes Berg 	}
26832b9a7e1bSJohannes Berg 
26848a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
26858a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2686a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2687a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
26882b9a7e1bSJohannes Berg 	}
2689b7ffbd7eSJohannes Berg 
26900fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
26916af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
26926af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
26938689c051SArend van Spriel 	}
269479c892b8SJohannes Berg 
2695b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2696b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2697a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2698a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2699a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2700a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2701a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2702dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
27031303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
27041303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2705b7ffbd7eSJohannes Berg 
2706433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2707433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2708433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2709b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2710a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2711433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2712b7ffbd7eSJohannes Berg 		}
2713433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2714433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2715433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2716dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
27171303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2718b7ffbd7eSJohannes Berg #endif
2719b7ffbd7eSJohannes Berg 	}
2720b7ffbd7eSJohannes Berg 
2721b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2722b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2723b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2724b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2725b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2726b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2727b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2728785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2729b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2730b7ffbd7eSJohannes Berg 
2731b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2732b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2733b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2734b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2735b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2736b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2737b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2738b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2739b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2740b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2741b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2742b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2743a74a8c84SJohannes Berg 	if (sta->sta.wme)
2744b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2745b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2746b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2747b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2748b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2749b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2750b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2751b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2752b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2753b7ffbd7eSJohannes Berg 
27543b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
27553b17fbf8SMaxim Altshul 
27563b17fbf8SMaxim Altshul 	if (thr != 0) {
2757a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
27583b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
27593b17fbf8SMaxim Altshul 	}
2760a78b26ffSVenkateswara Naralasetty 
2761a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2762046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2763046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2764a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2765a78b26ffSVenkateswara Naralasetty 	}
2766cc60dbbfSBalaji Pothunoori 
27679c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2768046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2769cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2770cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2771046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2772cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
27739c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2774cc60dbbfSBalaji Pothunoori 	}
2775ab60633cSNarayanraddi Masti 
2776ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2777ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2778ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2779ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2780ab60633cSNarayanraddi Masti 	}
27813b17fbf8SMaxim Altshul }
27823b17fbf8SMaxim Altshul 
27833b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
27843b17fbf8SMaxim Altshul {
27853b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
27863b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
27873b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
27883b17fbf8SMaxim Altshul 	u32 thr = 0;
27893b17fbf8SMaxim Altshul 
27903b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
27913b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
27923b17fbf8SMaxim Altshul 
2793b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2794b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2795b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2796b7ffbd7eSJohannes Berg 	else
27974fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2798b7ffbd7eSJohannes Berg 
27993b17fbf8SMaxim Altshul 	return thr;
2800b7ffbd7eSJohannes Berg }
2801b8da6b6aSJohannes Berg 
2802b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2803b8da6b6aSJohannes Berg {
2804c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2805c9c5962bSJohannes Berg 
2806046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2807046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2808c9c5962bSJohannes Berg 		return stats->last_rx;
2809046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2810b8da6b6aSJohannes Berg }
2811484a54c2SToke Høiland-Jørgensen 
2812484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2813484a54c2SToke Høiland-Jørgensen {
2814484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2815484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2816484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2817484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2818484a54c2SToke Høiland-Jørgensen 	} else {
2819484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2820484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2821484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2822484a54c2SToke Høiland-Jørgensen 	}
2823484a54c2SToke Høiland-Jørgensen }
2824484a54c2SToke Høiland-Jørgensen 
2825484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2826484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2827484a54c2SToke Høiland-Jørgensen {
2828484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2829484a54c2SToke Høiland-Jørgensen 
2830484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2831484a54c2SToke Høiland-Jørgensen }
2832cb71f1d1SJohannes Berg 
2833cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2834cb71f1d1SJohannes Berg {
2835cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2836cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2837cb71f1d1SJohannes Berg 	int ret;
2838cb71f1d1SJohannes Berg 
2839cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2840cb71f1d1SJohannes Berg 
2841cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2842cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2843cb71f1d1SJohannes Berg 		return -EINVAL;
2844cb71f1d1SJohannes Berg 
2845cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2846cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2847cb71f1d1SJohannes Berg 		return -EBUSY;
2848cb71f1d1SJohannes Berg 
2849cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2850cb71f1d1SJohannes Berg 	if (!alloc)
2851cb71f1d1SJohannes Berg 		return -ENOMEM;
2852cb71f1d1SJohannes Berg 
2853cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2854cb71f1d1SJohannes Berg 	if (ret) {
2855cb71f1d1SJohannes Berg 		kfree(alloc);
2856cb71f1d1SJohannes Berg 		return ret;
2857cb71f1d1SJohannes Berg 	}
2858cb71f1d1SJohannes Berg 
2859cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2860cb71f1d1SJohannes Berg 
2861d2caad52SBenjamin Berg 	ieee80211_link_sta_debugfs_add(&alloc->info);
2862d2caad52SBenjamin Berg 
2863cb71f1d1SJohannes Berg 	return 0;
2864cb71f1d1SJohannes Berg }
2865cb71f1d1SJohannes Berg 
286621476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
286721476ad1SShaul Triebitz {
286821476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
286921476ad1SShaul Triebitz 
287021476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
287121476ad1SShaul Triebitz }
287221476ad1SShaul Triebitz 
2873cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2874cb71f1d1SJohannes Berg {
2875cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2876c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2877cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2878cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2879cb71f1d1SJohannes Berg 	int ret;
2880cb71f1d1SJohannes Berg 
2881c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2882c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2883cb71f1d1SJohannes Berg 
2884c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2885cb71f1d1SJohannes Berg 		return -EINVAL;
2886cb71f1d1SJohannes Berg 
2887177577dbSJohannes Berg 	rcu_read_lock();
2888177577dbSJohannes Berg 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2889177577dbSJohannes Berg 		rcu_read_unlock();
2890177577dbSJohannes Berg 		return -EALREADY;
2891177577dbSJohannes Berg 	}
2892177577dbSJohannes Berg 	/* we only modify under the mutex so this is fine */
2893177577dbSJohannes Berg 	rcu_read_unlock();
2894177577dbSJohannes Berg 
2895cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2896cb71f1d1SJohannes Berg 
289780e2b1faSLukas Bulwahn 	if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2898ba6ddab9SJohannes Berg 		goto hash;
2899cb71f1d1SJohannes Berg 
29004c51541dSBenjamin Berg 	/* Ensure the values are updated for the driver,
29014c51541dSBenjamin Berg 	 * redone by sta_remove_link on failure.
29024c51541dSBenjamin Berg 	 */
29034c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
29044c51541dSBenjamin Berg 
2905cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2906cb71f1d1SJohannes Berg 				   old_links, new_links);
2907cb71f1d1SJohannes Berg 	if (ret) {
2908cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2909ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2910177577dbSJohannes Berg 		return ret;
2911cb71f1d1SJohannes Berg 	}
2912cb71f1d1SJohannes Berg 
2913ba6ddab9SJohannes Berg hash:
2914177577dbSJohannes Berg 	ret = link_sta_info_hash_add(sdata->local, link_sta);
2915177577dbSJohannes Berg 	WARN_ON(ret);
2916177577dbSJohannes Berg 	return 0;
2917cb71f1d1SJohannes Berg }
2918cb71f1d1SJohannes Berg 
2919cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2920cb71f1d1SJohannes Berg {
2921cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2922a8f62399SShaul Triebitz 	u16 old_links = sta->sta.valid_links;
2923cb71f1d1SJohannes Berg 
2924cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2925cb71f1d1SJohannes Berg 
2926cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2927cb71f1d1SJohannes Berg 
2928cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2929cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2930a8f62399SShaul Triebitz 				     old_links, sta->sta.valid_links);
2931cb71f1d1SJohannes Berg 
2932ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2933cb71f1d1SJohannes Berg }
2934175ad2ecSJohannes Berg 
2935175ad2ecSJohannes Berg void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2936175ad2ecSJohannes Berg 					   const u8 *ext_capab,
2937175ad2ecSJohannes Berg 					   unsigned int ext_capab_len)
2938175ad2ecSJohannes Berg {
2939175ad2ecSJohannes Berg 	u8 val;
2940175ad2ecSJohannes Berg 
2941175ad2ecSJohannes Berg 	sta->sta.max_amsdu_subframes = 0;
2942175ad2ecSJohannes Berg 
2943175ad2ecSJohannes Berg 	if (ext_capab_len < 8)
2944175ad2ecSJohannes Berg 		return;
2945175ad2ecSJohannes Berg 
2946175ad2ecSJohannes Berg 	/* The sender might not have sent the last bit, consider it to be 0 */
2947175ad2ecSJohannes Berg 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2948175ad2ecSJohannes Berg 
2949175ad2ecSJohannes Berg 	/* we did get all the bits, take the MSB as well */
2950175ad2ecSJohannes Berg 	if (ext_capab_len >= 9)
2951175ad2ecSJohannes Berg 		val |= u8_get_bits(ext_capab[8],
2952175ad2ecSJohannes Berg 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2953175ad2ecSJohannes Berg 
2954175ad2ecSJohannes Berg 	if (val)
2955175ad2ecSJohannes Berg 		sta->sta.max_amsdu_subframes = 4 << val;
2956175ad2ecSJohannes Berg }
295765fd846cSJohannes Berg 
295865fd846cSJohannes Berg #ifdef CONFIG_LOCKDEP
295965fd846cSJohannes Berg bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
296065fd846cSJohannes Berg {
296165fd846cSJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
296265fd846cSJohannes Berg 
296365fd846cSJohannes Berg 	return lockdep_is_held(&sta->local->sta_mtx);
296465fd846cSJohannes Berg }
296565fd846cSJohannes Berg EXPORT_SYMBOL(lockdep_sta_mutex_held);
296665fd846cSJohannes Berg #endif
2967