xref: /openbmc/linux/net/mac80211/sta_info.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f0706e82SJiri Benc /*
3f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
4f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
6dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
710a7ba92SJohannes Berg  * Copyright (C) 2018-2023 Intel Corporation
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
67cb71f1d1SJohannes Berg struct sta_link_alloc {
68cb71f1d1SJohannes Berg 	struct link_sta_info info;
69cb71f1d1SJohannes Berg 	struct ieee80211_link_sta sta;
70c71420dbSJohannes Berg 	struct rcu_head rcu_head;
71cb71f1d1SJohannes Berg };
72cb71f1d1SJohannes Berg 
737bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
747bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
75caf22d31SJohannes Berg 	.automatic_shrinking = true,
767bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
77ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
787bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
79ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
807bedd0cfSJohannes Berg };
817bedd0cfSJohannes Berg 
82ba6ddab9SJohannes Berg static const struct rhashtable_params link_sta_rht_params = {
83ba6ddab9SJohannes Berg 	.nelem_hint = 3, /* start small */
84ba6ddab9SJohannes Berg 	.automatic_shrinking = true,
85ba6ddab9SJohannes Berg 	.head_offset = offsetof(struct link_sta_info, link_hash_node),
86ba6ddab9SJohannes Berg 	.key_offset = offsetof(struct link_sta_info, addr),
87ba6ddab9SJohannes Berg 	.key_len = ETH_ALEN,
88ba6ddab9SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89ba6ddab9SJohannes Berg };
90ba6ddab9SJohannes Berg 
914d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
sta_info_hash_del(struct ieee80211_local * local,struct sta_info * sta)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 
link_sta_info_hash_add(struct ieee80211_local * local,struct link_sta_info * link_sta)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 
link_sta_info_hash_del(struct ieee80211_local * local,struct link_sta_info * link_sta)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 
__cleanup_single_sta(struct sta_info * sta)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 
cleanup_single_sta(struct sta_info * sta)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 
sta_info_hash_lookup(struct ieee80211_local * local,const u8 * addr)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 */
sta_info_get(struct ieee80211_sub_if_data * sdata,const u8 * addr)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  */
sta_info_get_bss(struct ieee80211_sub_if_data * sdata,const u8 * addr)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 
link_sta_info_hash_lookup(struct ieee80211_local * local,const u8 * addr)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 *
link_sta_info_get_bss(struct ieee80211_sub_if_data * sdata,const u8 * addr)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 *
ieee80211_find_sta_by_link_addrs(struct ieee80211_hw * hw,const u8 * addr,const u8 * localaddr,unsigned int * link_id)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 
sta_info_get_by_addrs(struct ieee80211_local * local,const u8 * sta_addr,const u8 * vif_addr)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 
sta_info_get_by_idx(struct ieee80211_sub_if_data * sdata,int idx)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 
sta_info_free_link(struct link_sta_info * link_sta)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 
sta_remove_link(struct sta_info * sta,unsigned int link_id,bool unhash)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 
358b8b80770SBenjamin Berg 	link_sta = rcu_access_pointer(sta->link[link_id]);
359b8b80770SBenjamin Berg 	if (link_sta != &sta->deflink)
360b8b80770SBenjamin Berg 		lockdep_assert_held(&sta->local->sta_mtx);
361c71420dbSJohannes Berg 
362c71420dbSJohannes Berg 	if (WARN_ON(!link_sta))
363cb71f1d1SJohannes Berg 		return;
364246b39e4SJohannes Berg 
365ba6ddab9SJohannes Berg 	if (unhash)
366ba6ddab9SJohannes Berg 		link_sta_info_hash_del(sta->local, link_sta);
367ba6ddab9SJohannes Berg 
368d2caad52SBenjamin Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
369d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_remove(link_sta);
370d2caad52SBenjamin Berg 
371c71420dbSJohannes Berg 	if (link_sta != &sta->deflink)
372c71420dbSJohannes Berg 		alloc = container_of(link_sta, typeof(*alloc), info);
373cb71f1d1SJohannes Berg 
374cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
375c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->link[link_id], NULL);
376c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
377cb71f1d1SJohannes Berg 	if (alloc) {
378cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
379c71420dbSJohannes Berg 		kfree_rcu(alloc, rcu_head);
380246b39e4SJohannes Berg 	}
3814c51541dSBenjamin Berg 
3824c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
383246b39e4SJohannes Berg }
384246b39e4SJohannes Berg 
38593e5deb1SJohannes Berg /**
386d9a7ddb0SJohannes Berg  * sta_info_free - free STA
38793e5deb1SJohannes Berg  *
3886ef307bcSRandy Dunlap  * @local: pointer to the global information
38993e5deb1SJohannes Berg  * @sta: STA info to free
39093e5deb1SJohannes Berg  *
39193e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
392d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
393d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
394d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
39593e5deb1SJohannes Berg  */
sta_info_free(struct ieee80211_local * local,struct sta_info * sta)396d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
39793e5deb1SJohannes Berg {
398cb71f1d1SJohannes Berg 	int i;
399cb71f1d1SJohannes Berg 
400cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
401587c5892SJohannes Berg 		struct link_sta_info *link_sta;
402587c5892SJohannes Berg 
403587c5892SJohannes Berg 		link_sta = rcu_access_pointer(sta->link[i]);
404587c5892SJohannes Berg 		if (!link_sta)
405cb71f1d1SJohannes Berg 			continue;
406cb71f1d1SJohannes Berg 
4070ad49045SJohannes Berg 		sta_remove_link(sta, i, false);
408cb71f1d1SJohannes Berg 	}
409cb71f1d1SJohannes Berg 
410dcd479e1SJohannes Berg 	/*
411dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
412dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
413dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
414dcd479e1SJohannes Berg 	 *
415dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
416dcd479e1SJohannes Berg 	 * allocated during state transitions.
417dcd479e1SJohannes Berg 	 */
418dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
419dcd479e1SJohannes Berg 		int ret;
420dcd479e1SJohannes Berg 
421dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
422dcd479e1SJohannes Berg 
423dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
424dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
425dcd479e1SJohannes Berg 			break;
426dcd479e1SJohannes Berg 	}
427dcd479e1SJohannes Berg 
428889cbb91SJohannes Berg 	if (sta->rate_ctrl)
4294b7679a5SJohannes Berg 		rate_control_free_sta(sta);
43093e5deb1SJohannes Berg 
431bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
43293e5deb1SJohannes Berg 
433ba8c3d6fSFelix Fietkau 	kfree(to_txq_info(sta->sta.txq[0]));
43453d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
435433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
436433f5bc1SJohannes Berg 	kfree(sta->mesh);
437433f5bc1SJohannes Berg #endif
438246b39e4SJohannes Berg 
439cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
44093e5deb1SJohannes Berg 	kfree(sta);
44193e5deb1SJohannes Berg }
44293e5deb1SJohannes Berg 
4434d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
sta_info_hash_add(struct ieee80211_local * local,struct sta_info * sta)44462b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
445d0709a65SJohannes Berg 			     struct sta_info *sta)
446f0706e82SJiri Benc {
44783e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
4487bedd0cfSJohannes Berg 			       sta_rht_params);
449f0706e82SJiri Benc }
450f0706e82SJiri Benc 
sta_deliver_ps_frames(struct work_struct * wk)4515ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
452af818581SJohannes Berg {
453af818581SJohannes Berg 	struct sta_info *sta;
454af818581SJohannes Berg 
4555ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
456af818581SJohannes Berg 
457af818581SJohannes Berg 	if (sta->dead)
458af818581SJohannes Berg 		return;
459af818581SJohannes Berg 
46054420473SHelmut Schaa 	local_bh_disable();
4615ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
462af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
4635ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
464af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
4655ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
46647086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
467ce662b44SJohannes Berg 	local_bh_enable();
468af818581SJohannes Berg }
469af818581SJohannes Berg 
sta_prepare_rate_control(struct ieee80211_local * local,struct sta_info * sta,gfp_t gfp)470af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
471af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
472af65cd96SJohannes Berg {
47330686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
474af65cd96SJohannes Berg 		return 0;
475af65cd96SJohannes Berg 
476889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
477af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
47835c347acSJohannes Berg 						     sta, gfp);
479889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
480af65cd96SJohannes Berg 		return -ENOMEM;
481af65cd96SJohannes Berg 
482af65cd96SJohannes Berg 	return 0;
483af65cd96SJohannes Berg }
484af65cd96SJohannes Berg 
sta_info_alloc_link(struct ieee80211_local * local,struct link_sta_info * link_info,gfp_t gfp)485cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
486246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
487246b39e4SJohannes Berg 			       gfp_t gfp)
488246b39e4SJohannes Berg {
489246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
490246b39e4SJohannes Berg 	int i;
491246b39e4SJohannes Berg 
492246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
493246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
494246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
495246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
496246b39e4SJohannes Berg 			return -ENOMEM;
497246b39e4SJohannes Berg 	}
498246b39e4SJohannes Berg 
499246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
500246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
501246b39e4SJohannes Berg 
502246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
503246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
504246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
505246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
506246b39e4SJohannes Berg 
507246b39e4SJohannes Berg 	return 0;
508246b39e4SJohannes Berg }
509246b39e4SJohannes Berg 
sta_info_add_link(struct sta_info * sta,unsigned int link_id,struct link_sta_info * link_info,struct ieee80211_link_sta * link_sta)510cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
511cb71f1d1SJohannes Berg 			      unsigned int link_id,
512cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
513cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
514cb71f1d1SJohannes Berg {
515cb71f1d1SJohannes Berg 	link_info->sta = sta;
516cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
517c71420dbSJohannes Berg 	link_info->pub = link_sta;
5181d9e4c91SBenjamin Berg 	link_info->pub->sta = &sta->sta;
519c73993b8SJohannes Berg 	link_sta->link_id = link_id;
520c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
521c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
522261ce887SBenjamin Berg 
523261ce887SBenjamin Berg 	link_sta->smps_mode = IEEE80211_SMPS_OFF;
5244c51541dSBenjamin Berg 	link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
525cb71f1d1SJohannes Berg }
526cb71f1d1SJohannes Berg 
527f36fe0a2SJohannes Berg static struct sta_info *
__sta_info_alloc(struct ieee80211_sub_if_data * sdata,const u8 * addr,int link_id,const u8 * link_addr,gfp_t gfp)528f36fe0a2SJohannes Berg __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
529f36fe0a2SJohannes Berg 		 const u8 *addr, int link_id, const u8 *link_addr,
530f36fe0a2SJohannes Berg 		 gfp_t gfp)
531f0706e82SJiri Benc {
532d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
533ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
534f0706e82SJiri Benc 	struct sta_info *sta;
535107395f9SAlexander Wetzel 	void *txq_data;
536107395f9SAlexander Wetzel 	int size;
53716c5f15cSRon Rindjunsky 	int i;
538f0706e82SJiri Benc 
539ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
540f0706e82SJiri Benc 	if (!sta)
54173651ee6SJohannes Berg 		return NULL;
542f0706e82SJiri Benc 
543246b39e4SJohannes Berg 	sta->local = local;
544246b39e4SJohannes Berg 	sta->sdata = sdata;
545246b39e4SJohannes Berg 
546cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
54736fe8e4eSLorenzo Bianconi 		goto free;
548c9c5962bSJohannes Berg 
549cb71f1d1SJohannes Berg 	if (link_id >= 0) {
550cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
551cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
552cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
553cb71f1d1SJohannes Berg 	} else {
554cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
555cb71f1d1SJohannes Berg 	}
556cb71f1d1SJohannes Berg 
5574c51541dSBenjamin Berg 	sta->sta.cur = &sta->sta.deflink.agg;
5584c51541dSBenjamin Berg 
55907346f81SJohannes Berg 	spin_lock_init(&sta->lock);
5601d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
5615ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
56267c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
563a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
56487f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
565433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
566433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
567433f5bc1SJohannes Berg 		if (!sta->mesh)
568433f5bc1SJohannes Berg 			goto free;
5694c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
570433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
57145d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5724c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5734c02d62fSKees Cook 				    0);
574433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
575433f5bc1SJohannes Berg 	}
57687f59c70SThomas Pedersen #endif
57707346f81SJohannes Berg 
578ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
57917741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
580f36fe0a2SJohannes Berg 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
581f36fe0a2SJohannes Berg 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
582480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
583480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
584480dd46bSMaxim Altshul 
585046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
586046d2e7cSSriram R 
58796fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
58896fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
58996fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
59096fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
59196fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
59296fc6efbSAlexander Wetzel 	 */
59396fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
59496fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
59596fc6efbSAlexander Wetzel 
5960f9c5a61SJohannes Berg 
5973a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5983a11ce08SJohannes Berg 
59971ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
60071ec375cSJohannes Berg 
6016e4c0d04SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
6026e4c0d04SFelix Fietkau 		sta->amsdu_mesh_control = -1;
6036e4c0d04SFelix Fietkau 
604b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
605b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
606b6da911bSLiad Kaufman 
60784b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
608541a45a1SBruno Randolf 
609107395f9SAlexander Wetzel 	size = sizeof(struct txq_info) +
610ba8c3d6fSFelix Fietkau 	       ALIGN(hw->txq_data_size, sizeof(void *));
611ba8c3d6fSFelix Fietkau 
612ba8c3d6fSFelix Fietkau 	txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
613ba8c3d6fSFelix Fietkau 	if (!txq_data)
614ba8c3d6fSFelix Fietkau 		goto free;
615ba8c3d6fSFelix Fietkau 
616ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
617ba8c3d6fSFelix Fietkau 		struct txq_info *txq = txq_data + i * size;
618ba8c3d6fSFelix Fietkau 
619107395f9SAlexander Wetzel 		/* might not do anything for the (bufferable) MMPDU TXQ */
620fa962b92SMichal Kazior 		ieee80211_txq_init(sdata, sta, txq, i);
621abfbc3afSJohannes Berg 	}
622ba8c3d6fSFelix Fietkau 
623ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
624ba8c3d6fSFelix Fietkau 		goto free_txq;
625f0706e82SJiri Benc 
626942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
627b4809e94SToke Høiland-Jørgensen 
628948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
629948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
630948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
631942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
632942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
633942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
634942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
635948d887dSJohannes Berg 	}
63673651ee6SJohannes Berg 
6375a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
6384be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
639cccaec98SSenthil Balasubramanian 
640bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
641bd718fc1SJohannes Berg 		u32 mandatory = 0;
642bd718fc1SJohannes Berg 		int r;
643bd718fc1SJohannes Berg 
644bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
645bd718fc1SJohannes Berg 			continue;
646bd718fc1SJohannes Berg 
647bd718fc1SJohannes Berg 		switch (i) {
648bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
64963fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
650bd718fc1SJohannes Berg 			/*
651bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
652bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
653bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
654bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
655bd718fc1SJohannes Berg 			 * possible rate anyway.
656bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
657bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
658bd718fc1SJohannes Berg 			 */
659bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
660bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
661bd718fc1SJohannes Berg 			break;
662bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
663bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
664bd718fc1SJohannes Berg 			break;
665bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
666bd718fc1SJohannes Berg 			WARN_ON(1);
667bd718fc1SJohannes Berg 			mandatory = 0;
668bd718fc1SJohannes Berg 			break;
669bd718fc1SJohannes Berg 		}
670bd718fc1SJohannes Berg 
671bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
672bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
673bd718fc1SJohannes Berg 
674bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
675bd718fc1SJohannes Berg 
676bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
677bd718fc1SJohannes Berg 				continue;
678046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
679bd718fc1SJohannes Berg 		}
680bd718fc1SJohannes Berg 	}
681bd718fc1SJohannes Berg 
682484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
683484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
684484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
685484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
686dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
687dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
688484a54c2SToke Høiland-Jørgensen 
689bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
690ef04a297SJohannes Berg 
691abfbc3afSJohannes Berg 	return sta;
692ba8c3d6fSFelix Fietkau 
693ba8c3d6fSFelix Fietkau free_txq:
694ba8c3d6fSFelix Fietkau 	kfree(to_txq_info(sta->sta.txq[0]));
695ba8c3d6fSFelix Fietkau free:
696cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
697433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
698433f5bc1SJohannes Berg 	kfree(sta->mesh);
699433f5bc1SJohannes Berg #endif
700ba8c3d6fSFelix Fietkau 	kfree(sta);
701ba8c3d6fSFelix Fietkau 	return NULL;
70273651ee6SJohannes Berg }
70373651ee6SJohannes Berg 
sta_info_alloc(struct ieee80211_sub_if_data * sdata,const u8 * addr,gfp_t gfp)704f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
705f36fe0a2SJohannes Berg 				const u8 *addr, gfp_t gfp)
706f36fe0a2SJohannes Berg {
707f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
708f36fe0a2SJohannes Berg }
709f36fe0a2SJohannes Berg 
sta_info_alloc_with_link(struct ieee80211_sub_if_data * sdata,const u8 * mld_addr,unsigned int link_id,const u8 * link_addr,gfp_t gfp)710f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
711f36fe0a2SJohannes Berg 					  const u8 *mld_addr,
712f36fe0a2SJohannes Berg 					  unsigned int link_id,
713f36fe0a2SJohannes Berg 					  const u8 *link_addr,
714f36fe0a2SJohannes Berg 					  gfp_t gfp)
715f36fe0a2SJohannes Berg {
716f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
717f36fe0a2SJohannes Berg }
718f36fe0a2SJohannes Berg 
sta_info_insert_check(struct sta_info * sta)7198c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
72034e89507SJohannes Berg {
72134e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
72234e89507SJohannes Berg 
72303e4497eSJohannes Berg 	/*
72403e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
72503e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
72603e4497eSJohannes Berg 	 * and another CPU turns off the net device.
72703e4497eSJohannes Berg 	 */
7288c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
7298c71df7aSGuy Eilam 		return -ENETDOWN;
73003e4497eSJohannes Berg 
731b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
732deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
7338c71df7aSGuy Eilam 		return -EINVAL;
7348c71df7aSGuy Eilam 
73583e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
73683e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
73783e7e4ceSHerbert Xu 	 * for correctness.
73831104891SJohannes Berg 	 */
73931104891SJohannes Berg 	rcu_read_lock();
74031104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
74131104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
74231104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
74331104891SJohannes Berg 		rcu_read_unlock();
74431104891SJohannes Berg 		return -ENOTUNIQ;
74531104891SJohannes Berg 	}
74631104891SJohannes Berg 	rcu_read_unlock();
74731104891SJohannes Berg 
7488c71df7aSGuy Eilam 	return 0;
74993e5deb1SJohannes Berg }
75044213b5eSJohannes Berg 
sta_info_insert_drv_state(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,struct sta_info * sta)751f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
752f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
753f09603a2SJohannes Berg 				     struct sta_info *sta)
754f09603a2SJohannes Berg {
755f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
756f09603a2SJohannes Berg 	int err = 0;
757f09603a2SJohannes Berg 
758f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
759f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
760f09603a2SJohannes Berg 		if (err)
761f09603a2SJohannes Berg 			break;
762f09603a2SJohannes Berg 	}
763f09603a2SJohannes Berg 
764f09603a2SJohannes Berg 	if (!err) {
765a4ec45a4SJohannes Berg 		/*
766a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
767a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
768a4ec45a4SJohannes Berg 		 */
769a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
770f09603a2SJohannes Berg 			sta->uploaded = true;
771f09603a2SJohannes Berg 		return 0;
772f09603a2SJohannes Berg 	}
773f09603a2SJohannes Berg 
774f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
775bdcbd8e0SJohannes Berg 		sdata_info(sdata,
776bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
777bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
778f09603a2SJohannes Berg 		err = 0;
779f09603a2SJohannes Berg 	}
780f09603a2SJohannes Berg 
781f09603a2SJohannes Berg 	/* unwind on error */
782f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
783f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
784f09603a2SJohannes Berg 
785f09603a2SJohannes Berg 	return err;
786f09603a2SJohannes Berg }
787f09603a2SJohannes Berg 
788d405fd8cSGregory Greenman static void
ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data * sdata)789d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
790d405fd8cSGregory Greenman {
791d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
792d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
793d405fd8cSGregory Greenman 	struct sta_info *sta;
794d405fd8cSGregory Greenman 
795d405fd8cSGregory Greenman 	rcu_read_lock();
796d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
797d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
798d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
799d405fd8cSGregory Greenman 			continue;
800d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
801d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
802d405fd8cSGregory Greenman 			break;
803d405fd8cSGregory Greenman 		}
804d405fd8cSGregory Greenman 	}
805d405fd8cSGregory Greenman 	rcu_read_unlock();
806d405fd8cSGregory Greenman 
807d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
808d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
809d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
810d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
811d405fd8cSGregory Greenman 	}
812d405fd8cSGregory Greenman }
813d405fd8cSGregory Greenman 
81434e89507SJohannes Berg /*
8158c71df7aSGuy Eilam  * should be called with sta_mtx locked
8168c71df7aSGuy Eilam  * this function replaces the mutex lock
8178c71df7aSGuy Eilam  * with a RCU lock
8188c71df7aSGuy Eilam  */
sta_info_insert_finish(struct sta_info * sta)8194d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
8208c71df7aSGuy Eilam {
8218c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
8228c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8230c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
8248c71df7aSGuy Eilam 	int err = 0;
8258c71df7aSGuy Eilam 
8268c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
8278c71df7aSGuy Eilam 
8287852e361SJohannes Berg 	/* check if STA exists already */
8297852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
8304d33960bSJohannes Berg 		err = -EEXIST;
8318f9dcc29SAhmed Zaki 		goto out_cleanup;
83234e89507SJohannes Berg 	}
83334e89507SJohannes Berg 
8340c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
8350c2e3842SKoen Vandeputte 	if (!sinfo) {
8360c2e3842SKoen Vandeputte 		err = -ENOMEM;
8378f9dcc29SAhmed Zaki 		goto out_cleanup;
8380c2e3842SKoen Vandeputte 	}
8390c2e3842SKoen Vandeputte 
8404d33960bSJohannes Berg 	local->num_sta++;
8414d33960bSJohannes Berg 	local->sta_generation++;
8424d33960bSJohannes Berg 	smp_mb();
8434d33960bSJohannes Berg 
8445108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
8455108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
8465108ca82SJohannes Berg 
8474d33960bSJohannes Berg 	/* make the station visible */
84862b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
84962b14b24SJohannes Berg 	if (err)
85062b14b24SJohannes Berg 		goto out_drop_sta;
8514d33960bSJohannes Berg 
852f36fe0a2SJohannes Berg 	if (sta->sta.valid_links) {
853f36fe0a2SJohannes Berg 		err = link_sta_info_hash_add(local, &sta->deflink);
854f36fe0a2SJohannes Berg 		if (err) {
855f36fe0a2SJohannes Berg 			sta_info_hash_del(local, sta);
856f36fe0a2SJohannes Berg 			goto out_drop_sta;
857f36fe0a2SJohannes Berg 		}
858f36fe0a2SJohannes Berg 	}
859f36fe0a2SJohannes Berg 
8602bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
86183d5cc01SJohannes Berg 
8624dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8634dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8644dde3c36SMordechay Goodstein 	 */
8654dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8660cbf348aSAndrei Otcheretianski 		ieee80211_recalc_min_chandef(sta->sdata, -1);
8674dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8684dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8694dde3c36SMordechay Goodstein 	}
8704dde3c36SMordechay Goodstein 
8715108ca82SJohannes Berg 	/* notify driver */
8725108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8735108ca82SJohannes Berg 	if (err)
8745108ca82SJohannes Berg 		goto out_remove;
8755108ca82SJohannes Berg 
87683d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
877d405fd8cSGregory Greenman 
8785108ca82SJohannes Berg 	/* accept BA sessions now */
8795108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8804d33960bSJohannes Berg 
8814d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8824d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
883d2caad52SBenjamin Berg 	if (sta->sta.valid_links) {
884d2caad52SBenjamin Berg 		int i;
885d2caad52SBenjamin Berg 
886d2caad52SBenjamin Berg 		for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
887d2caad52SBenjamin Berg 			struct link_sta_info *link_sta;
888d2caad52SBenjamin Berg 
889d2caad52SBenjamin Berg 			link_sta = rcu_dereference_protected(sta->link[i],
890d2caad52SBenjamin Berg 							     lockdep_is_held(&local->sta_mtx));
891d2caad52SBenjamin Berg 
892d2caad52SBenjamin Berg 			if (!link_sta)
893d2caad52SBenjamin Berg 				continue;
894d2caad52SBenjamin Berg 
895d2caad52SBenjamin Berg 			ieee80211_link_sta_debugfs_add(link_sta);
896d2caad52SBenjamin Berg 			if (sdata->vif.active_links & BIT(i))
897d2caad52SBenjamin Berg 				ieee80211_link_sta_debugfs_drv_add(link_sta);
898d2caad52SBenjamin Berg 		}
899d2caad52SBenjamin Berg 	} else {
900d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_add(&sta->deflink);
901d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
902d2caad52SBenjamin Berg 	}
9034d33960bSJohannes Berg 
9040ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
9050ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
9060ef049dcSArnd Bergmann 	kfree(sinfo);
907d0709a65SJohannes Berg 
908bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
909f0706e82SJiri Benc 
91034e89507SJohannes Berg 	/* move reference to rcu-protected */
91134e89507SJohannes Berg 	rcu_read_lock();
91234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
913e9f207f0SJiri Benc 
91473651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
91573651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
91673651ee6SJohannes Berg 
91754b79d87SFelix Fietkau 	ieee80211_check_fast_xmit(sta);
91854b79d87SFelix Fietkau 
91973651ee6SJohannes Berg 	return 0;
9205108ca82SJohannes Berg  out_remove:
9210ad49045SJohannes Berg 	if (sta->sta.valid_links)
9220ad49045SJohannes Berg 		link_sta_info_hash_del(local, &sta->deflink);
9235108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
9245108ca82SJohannes Berg 	list_del_rcu(&sta->list);
92562b14b24SJohannes Berg  out_drop_sta:
9265108ca82SJohannes Berg 	local->num_sta--;
9275108ca82SJohannes Berg 	synchronize_net();
9288f9dcc29SAhmed Zaki  out_cleanup:
9297bc40aedSJohannes Berg 	cleanup_single_sta(sta);
9304d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
931ea32f065SSudip Mukherjee 	kfree(sinfo);
9324d33960bSJohannes Berg 	rcu_read_lock();
9334d33960bSJohannes Berg 	return err;
9348c71df7aSGuy Eilam }
9358c71df7aSGuy Eilam 
sta_info_insert_rcu(struct sta_info * sta)9368c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
9378c71df7aSGuy Eilam {
9388c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
939308f7fcfSZhao, Gang 	int err;
9408c71df7aSGuy Eilam 
9414d33960bSJohannes Berg 	might_sleep();
9424d33960bSJohannes Berg 
94331104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
94431104891SJohannes Berg 
9458c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
9468c71df7aSGuy Eilam 	if (err) {
9477bc40aedSJohannes Berg 		sta_info_free(local, sta);
94831104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
9498c71df7aSGuy Eilam 		rcu_read_lock();
9507bc40aedSJohannes Berg 		return err;
9518c71df7aSGuy Eilam 	}
9528c71df7aSGuy Eilam 
9537bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
954f0706e82SJiri Benc }
955f0706e82SJiri Benc 
sta_info_insert(struct sta_info * sta)95634e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
95734e89507SJohannes Berg {
95834e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
95934e89507SJohannes Berg 
96034e89507SJohannes Berg 	rcu_read_unlock();
96134e89507SJohannes Berg 
96234e89507SJohannes Berg 	return err;
96334e89507SJohannes Berg }
96434e89507SJohannes Berg 
__bss_tim_set(u8 * tim,u16 id)965d012a605SMarco Porsch static inline void __bss_tim_set(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 __set_bit() format.
970004c872eSJohannes Berg 	 */
971d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
972004c872eSJohannes Berg }
973004c872eSJohannes Berg 
__bss_tim_clear(u8 * tim,u16 id)974d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
975004c872eSJohannes Berg {
976004c872eSJohannes Berg 	/*
977004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
978004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
979004c872eSJohannes Berg 	 */
980d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
981004c872eSJohannes Berg }
982004c872eSJohannes Berg 
__bss_tim_get(u8 * tim,u16 id)9833d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9843d5839b6SIlan Peer {
9853d5839b6SIlan Peer 	/*
9863d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9873d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9883d5839b6SIlan Peer 	 */
9893d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9903d5839b6SIlan Peer }
9913d5839b6SIlan Peer 
ieee80211_tids_for_ac(int ac)992948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
993004c872eSJohannes Berg {
994948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
995948d887dSJohannes Berg 	switch (ac) {
996948d887dSJohannes Berg 	case IEEE80211_AC_VO:
997948d887dSJohannes Berg 		return BIT(6) | BIT(7);
998948d887dSJohannes Berg 	case IEEE80211_AC_VI:
999948d887dSJohannes Berg 		return BIT(4) | BIT(5);
1000948d887dSJohannes Berg 	case IEEE80211_AC_BE:
1001948d887dSJohannes Berg 		return BIT(0) | BIT(3);
1002948d887dSJohannes Berg 	case IEEE80211_AC_BK:
1003948d887dSJohannes Berg 		return BIT(1) | BIT(2);
1004948d887dSJohannes Berg 	default:
1005948d887dSJohannes Berg 		WARN_ON(1);
1006948d887dSJohannes Berg 		return 0;
1007d0709a65SJohannes Berg 	}
1008004c872eSJohannes Berg }
1009004c872eSJohannes Berg 
__sta_info_recalc_tim(struct sta_info * sta,bool ignore_pending)10109b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
1011004c872eSJohannes Berg {
1012c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
1013d012a605SMarco Porsch 	struct ps_data *ps;
1014948d887dSJohannes Berg 	bool indicate_tim = false;
1015948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
1016948d887dSJohannes Berg 	int ac;
1017a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
1018004c872eSJohannes Berg 
1019d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1020d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1021c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
1022c868cb35SJohannes Berg 			return;
10233e122be0SJohannes Berg 
1024d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
10253f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
10263f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
10273f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
10283f52b7e3SMarco Porsch #endif
1029d012a605SMarco Porsch 	} else {
1030d012a605SMarco Porsch 		return;
1031d012a605SMarco Porsch 	}
1032d012a605SMarco Porsch 
1033c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
1034d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1035c868cb35SJohannes Berg 		return;
1036004c872eSJohannes Berg 
1037c868cb35SJohannes Berg 	if (sta->dead)
1038c868cb35SJohannes Berg 		goto done;
10393e122be0SJohannes Berg 
1040948d887dSJohannes Berg 	/*
1041948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
1042948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
1043948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
1044948d887dSJohannes Berg 	 * non-enabled ones.
1045948d887dSJohannes Berg 	 */
1046948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1047948d887dSJohannes Berg 		ignore_for_tim = 0;
1048948d887dSJohannes Berg 
10499b7a86f3SJohannes Berg 	if (ignore_pending)
10509b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
10519b7a86f3SJohannes Berg 
1052948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1053948d887dSJohannes Berg 		unsigned long tids;
1054948d887dSJohannes Berg 
1055f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1056948d887dSJohannes Berg 			continue;
1057948d887dSJohannes Berg 
1058948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1059948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
1060948d887dSJohannes Berg 		if (indicate_tim)
1061948d887dSJohannes Berg 			break;
1062948d887dSJohannes Berg 
1063948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
1064948d887dSJohannes Berg 
1065948d887dSJohannes Berg 		indicate_tim |=
1066948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
1067ba8c3d6fSFelix Fietkau 		indicate_tim |=
1068ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
1069004c872eSJohannes Berg 	}
1070004c872eSJohannes Berg 
1071c868cb35SJohannes Berg  done:
107265f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
1073004c872eSJohannes Berg 
10743d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
10753d5839b6SIlan Peer 		goto out_unlock;
10763d5839b6SIlan Peer 
1077948d887dSJohannes Berg 	if (indicate_tim)
1078d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1079c868cb35SJohannes Berg 	else
1080d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10813e122be0SJohannes Berg 
10829b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1083c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1084948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1085c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1086004c872eSJohannes Berg 	}
1087004c872eSJohannes Berg 
10883d5839b6SIlan Peer out_unlock:
108965f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1090004c872eSJohannes Berg }
1091004c872eSJohannes Berg 
sta_info_recalc_tim(struct sta_info * sta)10929b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10939b7a86f3SJohannes Berg {
10949b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10959b7a86f3SJohannes Berg }
10969b7a86f3SJohannes Berg 
sta_info_buffer_expired(struct sta_info * sta,struct sk_buff * skb)1097cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1098f0706e82SJiri Benc {
1099e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1100f0706e82SJiri Benc 	int timeout;
1101f0706e82SJiri Benc 
1102f0706e82SJiri Benc 	if (!skb)
1103cd0b8d89SJohannes Berg 		return false;
1104f0706e82SJiri Benc 
1105e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1106f0706e82SJiri Benc 
1107f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
110857c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
110957c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
111057c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1111f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1112f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1113e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1114f0706e82SJiri Benc }
1115f0706e82SJiri Benc 
1116f0706e82SJiri Benc 
sta_info_cleanup_expire_buffered_ac(struct ieee80211_local * local,struct sta_info * sta,int ac)1117948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1118948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1119f0706e82SJiri Benc {
1120f0706e82SJiri Benc 	unsigned long flags;
1121f0706e82SJiri Benc 	struct sk_buff *skb;
1122f0706e82SJiri Benc 
112360750397SJohannes Berg 	/*
112460750397SJohannes Berg 	 * First check for frames that should expire on the filtered
112560750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
112660750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
112760750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
112860750397SJohannes Berg 	 * total_ps_buffered counter.
112960750397SJohannes Berg 	 */
1130f0706e82SJiri Benc 	for (;;) {
1131948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1132948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
113357c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1134948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1135836341a7SJohannes Berg 		else
1136f0706e82SJiri Benc 			skb = NULL;
1137948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1138f0706e82SJiri Benc 
113960750397SJohannes Berg 		/*
114060750397SJohannes Berg 		 * Frames are queued in order, so if this one
114160750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
114260750397SJohannes Berg 		 * we actually reached the end of the queue we
114360750397SJohannes Berg 		 * also need to stop, of course.
114460750397SJohannes Berg 		 */
114560750397SJohannes Berg 		if (!skb)
114660750397SJohannes Berg 			break;
1147d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
114860750397SJohannes Berg 	}
114960750397SJohannes Berg 
115060750397SJohannes Berg 	/*
115160750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
115260750397SJohannes Berg 	 * only find something if the filtered queue was emptied
115360750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
115460750397SJohannes Berg 	 * buffered frames.
115560750397SJohannes Berg 	 */
1156f0706e82SJiri Benc 	for (;;) {
1157948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1158948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1159f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1160948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1161f0706e82SJiri Benc 		else
1162f0706e82SJiri Benc 			skb = NULL;
1163948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1164f0706e82SJiri Benc 
116560750397SJohannes Berg 		/*
116660750397SJohannes Berg 		 * frames are queued in order, so if this one
116760750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
116860750397SJohannes Berg 		 * the queue) we can stop testing
116960750397SJohannes Berg 		 */
1170836341a7SJohannes Berg 		if (!skb)
1171836341a7SJohannes Berg 			break;
1172836341a7SJohannes Berg 
1173f0706e82SJiri Benc 		local->total_ps_buffered--;
1174bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1175bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1176d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1177f0706e82SJiri Benc 	}
11783393a608SJuuso Oikarinen 
117960750397SJohannes Berg 	/*
118060750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
118160750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
118260750397SJohannes Berg 	 * frames.
118360750397SJohannes Berg 	 */
118460750397SJohannes Berg 	sta_info_recalc_tim(sta);
118560750397SJohannes Berg 
118660750397SJohannes Berg 	/*
118760750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
118860750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
118960750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
119060750397SJohannes Berg 	 */
1191948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1192948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1193948d887dSJohannes Berg }
1194948d887dSJohannes Berg 
sta_info_cleanup_expire_buffered(struct ieee80211_local * local,struct sta_info * sta)1195948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1196948d887dSJohannes Berg 					     struct sta_info *sta)
1197948d887dSJohannes Berg {
1198948d887dSJohannes Berg 	bool have_buffered = false;
1199948d887dSJohannes Berg 	int ac;
1200948d887dSJohannes Berg 
12013f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
12023f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
12033f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1204948d887dSJohannes Berg 		return false;
1205948d887dSJohannes Berg 
1206948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1207948d887dSJohannes Berg 		have_buffered |=
1208948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1209948d887dSJohannes Berg 
1210948d887dSJohannes Berg 	return have_buffered;
1211f0706e82SJiri Benc }
1212f0706e82SJiri Benc 
__sta_info_destroy_part1(struct sta_info * sta)1213d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
121434e89507SJohannes Berg {
121534e89507SJohannes Berg 	struct ieee80211_local *local;
121634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
12170ad49045SJohannes Berg 	int ret, i;
121834e89507SJohannes Berg 
121934e89507SJohannes Berg 	might_sleep();
122034e89507SJohannes Berg 
122134e89507SJohannes Berg 	if (!sta)
122234e89507SJohannes Berg 		return -ENOENT;
122334e89507SJohannes Berg 
122434e89507SJohannes Berg 	local = sta->local;
122534e89507SJohannes Berg 	sdata = sta->sdata;
122634e89507SJohannes Berg 
122783d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
122883d5cc01SJohannes Berg 
1229098a6070SJohannes Berg 	/*
1230098a6070SJohannes Berg 	 * Before removing the station from the driver and
1231098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1232098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1233098a6070SJohannes Berg 	 * will be sufficient.
1234098a6070SJohannes Berg 	 */
1235c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1236c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1237098a6070SJohannes Berg 
1238f59374ebSSara Sharon 	/*
1239f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1240f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1241f59374ebSSara Sharon 	 * all such frames to be processed.
1242f59374ebSSara Sharon 	 */
1243f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1244f59374ebSSara Sharon 
12450ad49045SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
12460ad49045SJohannes Berg 		struct link_sta_info *link_sta;
12470ad49045SJohannes Berg 
12480ad49045SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
12490ad49045SJohannes Berg 			continue;
12500ad49045SJohannes Berg 
12510ad49045SJohannes Berg 		link_sta = rcu_dereference_protected(sta->link[i],
12520ad49045SJohannes Berg 						     lockdep_is_held(&local->sta_mtx));
12530ad49045SJohannes Berg 
12540ad49045SJohannes Berg 		link_sta_info_hash_del(local, link_sta);
12550ad49045SJohannes Berg 	}
12560ad49045SJohannes Berg 
125734e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1258b01711beSJohannes Berg 	if (WARN_ON(ret))
125934e89507SJohannes Berg 		return ret;
126034e89507SJohannes Berg 
1261a7a6bdd0SArik Nemtsov 	/*
1262a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1263a7a6bdd0SArik Nemtsov 	 * removal.
1264a7a6bdd0SArik Nemtsov 	 */
1265a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1266a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1267a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1268a7a6bdd0SArik Nemtsov 	}
1269a7a6bdd0SArik Nemtsov 
1270794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1271ef044763SEliad Peller 	sta->removed = true;
12724d33960bSJohannes Berg 
127312b220a6SFelix Fietkau 	if (sta->uploaded)
12746a9d1b91SJohannes Berg 		drv_sta_pre_rcu_remove(local, sta->sdata, sta);
12756a9d1b91SJohannes Berg 
1276a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1277a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1278a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1279a710c816SJohannes Berg 
1280d778207bSJohannes Berg 	return 0;
1281d778207bSJohannes Berg }
1282d778207bSJohannes Berg 
_sta_info_move_state(struct sta_info * sta,enum ieee80211_sta_state new_state,bool recalc)128392747f17SJohannes Berg static int _sta_info_move_state(struct sta_info *sta,
128492747f17SJohannes Berg 				enum ieee80211_sta_state new_state,
128592747f17SJohannes Berg 				bool recalc)
128610a7ba92SJohannes Berg {
1287*1a327862SJohannes Berg 	struct ieee80211_local *local = sta->local;
1288*1a327862SJohannes Berg 
128910a7ba92SJohannes Berg 	might_sleep();
129010a7ba92SJohannes Berg 
129110a7ba92SJohannes Berg 	if (sta->sta_state == new_state)
129210a7ba92SJohannes Berg 		return 0;
129310a7ba92SJohannes Berg 
129410a7ba92SJohannes Berg 	/* check allowed transitions first */
129510a7ba92SJohannes Berg 
129610a7ba92SJohannes Berg 	switch (new_state) {
129710a7ba92SJohannes Berg 	case IEEE80211_STA_NONE:
129810a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
129910a7ba92SJohannes Berg 			return -EINVAL;
130010a7ba92SJohannes Berg 		break;
130110a7ba92SJohannes Berg 	case IEEE80211_STA_AUTH:
130210a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
130310a7ba92SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
130410a7ba92SJohannes Berg 			return -EINVAL;
130510a7ba92SJohannes Berg 		break;
130610a7ba92SJohannes Berg 	case IEEE80211_STA_ASSOC:
130710a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
130810a7ba92SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
130910a7ba92SJohannes Berg 			return -EINVAL;
131010a7ba92SJohannes Berg 		break;
131110a7ba92SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
131210a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
131310a7ba92SJohannes Berg 			return -EINVAL;
131410a7ba92SJohannes Berg 		break;
131510a7ba92SJohannes Berg 	default:
131610a7ba92SJohannes Berg 		WARN(1, "invalid state %d", new_state);
131710a7ba92SJohannes Berg 		return -EINVAL;
131810a7ba92SJohannes Berg 	}
131910a7ba92SJohannes Berg 
132010a7ba92SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
132110a7ba92SJohannes Berg 		sta->sta.addr, new_state);
132210a7ba92SJohannes Berg 
132310a7ba92SJohannes Berg 	/* notify the driver before the actual changes so it can
132410a7ba92SJohannes Berg 	 * fail the transition
132510a7ba92SJohannes Berg 	 */
132610a7ba92SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
132710a7ba92SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
132810a7ba92SJohannes Berg 					sta->sta_state, new_state);
132910a7ba92SJohannes Berg 		if (err)
133010a7ba92SJohannes Berg 			return err;
133110a7ba92SJohannes Berg 	}
133210a7ba92SJohannes Berg 
133310a7ba92SJohannes Berg 	/* reflect the change in all state variables */
133410a7ba92SJohannes Berg 
133510a7ba92SJohannes Berg 	switch (new_state) {
133610a7ba92SJohannes Berg 	case IEEE80211_STA_NONE:
133710a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
133810a7ba92SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
133910a7ba92SJohannes Berg 		break;
134010a7ba92SJohannes Berg 	case IEEE80211_STA_AUTH:
134110a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE) {
134210a7ba92SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
134310a7ba92SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
134410a7ba92SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
134592747f17SJohannes Berg 			if (recalc) {
134610a7ba92SJohannes Berg 				ieee80211_recalc_min_chandef(sta->sdata, -1);
134710a7ba92SJohannes Berg 				if (!sta->sta.support_p2p_ps)
134810a7ba92SJohannes Berg 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
134910a7ba92SJohannes Berg 			}
135092747f17SJohannes Berg 		}
135110a7ba92SJohannes Berg 		break;
135210a7ba92SJohannes Berg 	case IEEE80211_STA_ASSOC:
135310a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
135410a7ba92SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
135510a7ba92SJohannes Berg 			sta->assoc_at = ktime_get_boottime_ns();
135692747f17SJohannes Berg 			if (recalc) {
135710a7ba92SJohannes Berg 				ieee80211_recalc_min_chandef(sta->sdata, -1);
135810a7ba92SJohannes Berg 				if (!sta->sta.support_p2p_ps)
135910a7ba92SJohannes Berg 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
136092747f17SJohannes Berg 			}
136110a7ba92SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
136210a7ba92SJohannes Berg 			ieee80211_vif_dec_num_mcast(sta->sdata);
136310a7ba92SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1364*1a327862SJohannes Berg 
1365*1a327862SJohannes Berg 			/*
1366*1a327862SJohannes Berg 			 * If we have encryption offload, flush (station) queues
1367*1a327862SJohannes Berg 			 * (after ensuring concurrent TX completed) so we won't
1368*1a327862SJohannes Berg 			 * transmit anything later unencrypted if/when keys are
1369*1a327862SJohannes Berg 			 * also removed, which might otherwise happen depending
1370*1a327862SJohannes Berg 			 * on how the hardware offload works.
1371*1a327862SJohannes Berg 			 */
1372*1a327862SJohannes Berg 			if (local->ops->set_key) {
1373*1a327862SJohannes Berg 				synchronize_net();
1374*1a327862SJohannes Berg 				if (local->ops->flush_sta)
1375*1a327862SJohannes Berg 					drv_flush_sta(local, sta->sdata, sta);
1376*1a327862SJohannes Berg 				else
1377*1a327862SJohannes Berg 					ieee80211_flush_queues(local,
1378*1a327862SJohannes Berg 							       sta->sdata,
1379*1a327862SJohannes Berg 							       false);
1380*1a327862SJohannes Berg 			}
1381*1a327862SJohannes Berg 
138210a7ba92SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
138310a7ba92SJohannes Berg 			ieee80211_clear_fast_rx(sta);
138410a7ba92SJohannes Berg 		}
138510a7ba92SJohannes Berg 		break;
138610a7ba92SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
138710a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
138810a7ba92SJohannes Berg 			ieee80211_vif_inc_num_mcast(sta->sdata);
138910a7ba92SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
139010a7ba92SJohannes Berg 			ieee80211_check_fast_xmit(sta);
139110a7ba92SJohannes Berg 			ieee80211_check_fast_rx(sta);
139210a7ba92SJohannes Berg 		}
139310a7ba92SJohannes Berg 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
139410a7ba92SJohannes Berg 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
139510a7ba92SJohannes Berg 			cfg80211_send_layer2_update(sta->sdata->dev,
139610a7ba92SJohannes Berg 						    sta->sta.addr);
139710a7ba92SJohannes Berg 		break;
139810a7ba92SJohannes Berg 	default:
139910a7ba92SJohannes Berg 		break;
140010a7ba92SJohannes Berg 	}
140110a7ba92SJohannes Berg 
140210a7ba92SJohannes Berg 	sta->sta_state = new_state;
140310a7ba92SJohannes Berg 
140410a7ba92SJohannes Berg 	return 0;
140510a7ba92SJohannes Berg }
140610a7ba92SJohannes Berg 
sta_info_move_state(struct sta_info * sta,enum ieee80211_sta_state new_state)140792747f17SJohannes Berg int sta_info_move_state(struct sta_info *sta,
140892747f17SJohannes Berg 			enum ieee80211_sta_state new_state)
140992747f17SJohannes Berg {
141092747f17SJohannes Berg 	return _sta_info_move_state(sta, new_state, true);
141192747f17SJohannes Berg }
141292747f17SJohannes Berg 
__sta_info_destroy_part2(struct sta_info * sta,bool recalc)141392747f17SJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc)
1414d778207bSJohannes Berg {
1415d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1416d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
14170ef049dcSArnd Bergmann 	struct station_info *sinfo;
1418d778207bSJohannes Berg 	int ret;
1419d778207bSJohannes Berg 
1420d778207bSJohannes Berg 	/*
1421d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1422d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1423d778207bSJohannes Berg 	 */
1424d778207bSJohannes Berg 
142586f22e7cSJohannes Berg 	/*
142686f22e7cSJohannes Berg 	 * There's a potential race in _part1 where we set WLAN_STA_BLOCK_BA
142786f22e7cSJohannes Berg 	 * but someone might have just gotten past a check, and not yet into
142886f22e7cSJohannes Berg 	 * queuing the work/creating the data/etc.
142986f22e7cSJohannes Berg 	 *
143086f22e7cSJohannes Berg 	 * Do another round of destruction so that the worker is certainly
143186f22e7cSJohannes Berg 	 * canceled before we later free the station.
143286f22e7cSJohannes Berg 	 *
143386f22e7cSJohannes Berg 	 * Since this is after synchronize_rcu()/synchronize_net() we're now
143486f22e7cSJohannes Berg 	 * certain that nobody can actually hold a reference to the STA and
143586f22e7cSJohannes Berg 	 * be calling e.g. ieee80211_start_tx_ba_session().
143686f22e7cSJohannes Berg 	 */
143786f22e7cSJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
143886f22e7cSJohannes Berg 
1439d778207bSJohannes Berg 	might_sleep();
1440d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1441d778207bSJohannes Berg 
14425981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
144392747f17SJohannes Berg 		ret = _sta_info_move_state(sta, IEEE80211_STA_ASSOC, recalc);
1444b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1445b16798f5SJohannes Berg 	}
1446b16798f5SJohannes Berg 
1447c8782078SJohannes Berg 	/* now keys can no longer be reached */
14486d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
144934e89507SJohannes Berg 
14509b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
14519b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
14529b7a86f3SJohannes Berg 
145334e89507SJohannes Berg 	sta->dead = true;
145434e89507SJohannes Berg 
145534e89507SJohannes Berg 	local->num_sta--;
145634e89507SJohannes Berg 	local->sta_generation++;
145734e89507SJohannes Berg 
145883d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
145992747f17SJohannes Berg 		ret = _sta_info_move_state(sta, sta->sta_state - 1, recalc);
1460f09603a2SJohannes Berg 		if (ret) {
146183d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
146283d5cc01SJohannes Berg 			break;
146383d5cc01SJohannes Berg 		}
146483d5cc01SJohannes Berg 	}
1465d9a7ddb0SJohannes Berg 
1466f09603a2SJohannes Berg 	if (sta->uploaded) {
1467f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1468f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1469f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1470f09603a2SJohannes Berg 	}
147134e89507SJohannes Berg 
1472bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1473bdcbd8e0SJohannes Berg 
14740ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
14750ef049dcSArnd Bergmann 	if (sinfo)
14760fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
14770ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
14780ef049dcSArnd Bergmann 	kfree(sinfo);
1479ec15e68bSJouni Malinen 
148034e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
148134e89507SJohannes Berg 
14823a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
14833a11ce08SJohannes Berg 
1484d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1485d778207bSJohannes Berg }
1486d778207bSJohannes Berg 
__sta_info_destroy(struct sta_info * sta)1487d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1488d778207bSJohannes Berg {
1489d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1490d778207bSJohannes Berg 
1491d778207bSJohannes Berg 	if (err)
1492d778207bSJohannes Berg 		return err;
1493d778207bSJohannes Berg 
1494d778207bSJohannes Berg 	synchronize_net();
1495d778207bSJohannes Berg 
149692747f17SJohannes Berg 	__sta_info_destroy_part2(sta, true);
149734e89507SJohannes Berg 
149834e89507SJohannes Berg 	return 0;
149934e89507SJohannes Berg }
150034e89507SJohannes Berg 
sta_info_destroy_addr(struct ieee80211_sub_if_data * sdata,const u8 * addr)150134e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
150234e89507SJohannes Berg {
150334e89507SJohannes Berg 	struct sta_info *sta;
150434e89507SJohannes Berg 	int ret;
150534e89507SJohannes Berg 
150634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
15077852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
150834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
150934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
151034e89507SJohannes Berg 
151134e89507SJohannes Berg 	return ret;
151234e89507SJohannes Berg }
151334e89507SJohannes Berg 
sta_info_destroy_addr_bss(struct ieee80211_sub_if_data * sdata,const u8 * addr)151434e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
151534e89507SJohannes Berg 			      const u8 *addr)
151634e89507SJohannes Berg {
151734e89507SJohannes Berg 	struct sta_info *sta;
151834e89507SJohannes Berg 	int ret;
151934e89507SJohannes Berg 
152034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
15217852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
152234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
152334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
152434e89507SJohannes Berg 
152534e89507SJohannes Berg 	return ret;
152634e89507SJohannes Berg }
1527f0706e82SJiri Benc 
sta_info_cleanup(struct timer_list * t)152834f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1529f0706e82SJiri Benc {
153034f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1531f0706e82SJiri Benc 	struct sta_info *sta;
15323393a608SJuuso Oikarinen 	bool timer_needed = false;
1533f0706e82SJiri Benc 
1534d0709a65SJohannes Berg 	rcu_read_lock();
1535d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
15363393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
15373393a608SJuuso Oikarinen 			timer_needed = true;
1538d0709a65SJohannes Berg 	rcu_read_unlock();
1539f0706e82SJiri Benc 
15405bb644a0SJohannes Berg 	if (local->quiescing)
15415bb644a0SJohannes Berg 		return;
15425bb644a0SJohannes Berg 
15433393a608SJuuso Oikarinen 	if (!timer_needed)
15443393a608SJuuso Oikarinen 		return;
15453393a608SJuuso Oikarinen 
154626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
154726d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1548f0706e82SJiri Benc }
1549f0706e82SJiri Benc 
sta_info_init(struct ieee80211_local * local)15507bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
15517bedd0cfSJohannes Berg {
15527bedd0cfSJohannes Berg 	int err;
15537bedd0cfSJohannes Berg 
155483e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
15557bedd0cfSJohannes Berg 	if (err)
15567bedd0cfSJohannes Berg 		return err;
15577bedd0cfSJohannes Berg 
1558ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1559ba6ddab9SJohannes Berg 	if (err) {
1560ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1561ba6ddab9SJohannes Berg 		return err;
1562ba6ddab9SJohannes Berg 	}
1563ba6ddab9SJohannes Berg 
15644d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
156534e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1566f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1567f0706e82SJiri Benc 
156834f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
15697bedd0cfSJohannes Berg 	return 0;
1570f0706e82SJiri Benc }
1571f0706e82SJiri Benc 
sta_info_stop(struct ieee80211_local * local)1572f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1573f0706e82SJiri Benc {
1574a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
157583e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1576ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1577f0706e82SJiri Benc }
1578f0706e82SJiri Benc 
1579051007d9SJohannes Berg 
__sta_info_flush(struct ieee80211_sub_if_data * sdata,bool vlans)1580e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1581f0706e82SJiri Benc {
1582b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1583f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1584d778207bSJohannes Berg 	LIST_HEAD(free_list);
158544213b5eSJohannes Berg 	int ret = 0;
1586f0706e82SJiri Benc 
1587d0709a65SJohannes Berg 	might_sleep();
1588d0709a65SJohannes Berg 
1589e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1590e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1591e716251dSJohannes Berg 
159234e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
159334e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1594e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1595e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1596d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1597d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
159834316837SJohannes Berg 			ret++;
159934316837SJohannes Berg 		}
160034e89507SJohannes Berg 	}
1601d778207bSJohannes Berg 
1602d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
160392747f17SJohannes Berg 		bool support_p2p_ps = true;
160492747f17SJohannes Berg 
1605d778207bSJohannes Berg 		synchronize_net();
160692747f17SJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list) {
160792747f17SJohannes Berg 			if (!sta->sta.support_p2p_ps)
160892747f17SJohannes Berg 				support_p2p_ps = false;
160992747f17SJohannes Berg 			__sta_info_destroy_part2(sta, false);
161092747f17SJohannes Berg 		}
161192747f17SJohannes Berg 
161292747f17SJohannes Berg 		ieee80211_recalc_min_chandef(sdata, -1);
161392747f17SJohannes Berg 		if (!support_p2p_ps)
161492747f17SJohannes Berg 			ieee80211_recalc_p2p_go_ps_allowed(sdata);
1615d778207bSJohannes Berg 	}
161634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
161744213b5eSJohannes Berg 
1618051007d9SJohannes Berg 	return ret;
1619051007d9SJohannes Berg }
1620051007d9SJohannes Berg 
ieee80211_sta_expire(struct ieee80211_sub_if_data * sdata,unsigned long exp_time)162124723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
162224723d1bSJohannes Berg 			  unsigned long exp_time)
162324723d1bSJohannes Berg {
162424723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
162524723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
162624723d1bSJohannes Berg 
162734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1628e46a2cf9SMohammed Shafi Shajakhan 
1629e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1630b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1631b8da6b6aSJohannes Berg 
1632ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1633ec2b774eSMarek Lindner 			continue;
1634ec2b774eSMarek Lindner 
1635b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1636eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1637bdcbd8e0SJohannes Berg 				sta->sta.addr);
16383f52b7e3SMarco Porsch 
16393f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
16403f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
16413f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
16423f52b7e3SMarco Porsch 
164334e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
164424723d1bSJohannes Berg 		}
1645e46a2cf9SMohammed Shafi Shajakhan 	}
1646e46a2cf9SMohammed Shafi Shajakhan 
164734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
164824723d1bSJohannes Berg }
164917741cdcSJohannes Berg 
ieee80211_find_sta_by_ifaddr(struct ieee80211_hw * hw,const u8 * addr,const u8 * localaddr)1650686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1651686b9cb9SBen Greear 						   const u8 *addr,
1652686b9cb9SBen Greear 						   const u8 *localaddr)
165317741cdcSJohannes Berg {
16547bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
165583e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
16567bedd0cfSJohannes Berg 	struct sta_info *sta;
165717741cdcSJohannes Berg 
1658686b9cb9SBen Greear 	/*
1659686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1660686b9cb9SBen Greear 	 * ... first in list.
1661686b9cb9SBen Greear 	 */
166283e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1663686b9cb9SBen Greear 		if (localaddr &&
1664b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1665686b9cb9SBen Greear 			continue;
1666f7c65594SJohannes Berg 		if (!sta->uploaded)
1667f7c65594SJohannes Berg 			return NULL;
166817741cdcSJohannes Berg 		return &sta->sta;
1669f7c65594SJohannes Berg 	}
1670f7c65594SJohannes Berg 
1671abe60632SJohannes Berg 	return NULL;
167217741cdcSJohannes Berg }
1673686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
16745ed176e1SJohannes Berg 
ieee80211_find_sta(struct ieee80211_vif * vif,const u8 * addr)16755ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
16765ed176e1SJohannes Berg 					 const u8 *addr)
16775ed176e1SJohannes Berg {
1678f7c65594SJohannes Berg 	struct sta_info *sta;
16795ed176e1SJohannes Berg 
16805ed176e1SJohannes Berg 	if (!vif)
16815ed176e1SJohannes Berg 		return NULL;
16825ed176e1SJohannes Berg 
1683f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1684f7c65594SJohannes Berg 	if (!sta)
1685f7c65594SJohannes Berg 		return NULL;
16865ed176e1SJohannes Berg 
1687f7c65594SJohannes Berg 	if (!sta->uploaded)
1688f7c65594SJohannes Berg 		return NULL;
1689f7c65594SJohannes Berg 
1690f7c65594SJohannes Berg 	return &sta->sta;
16915ed176e1SJohannes Berg }
169217741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1693af818581SJohannes Berg 
1694e3685e03SJohannes Berg /* powersave support code */
ieee80211_sta_ps_deliver_wakeup(struct sta_info * sta)1695e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
169650a9432dSJohannes Berg {
1697608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1698e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1699e3685e03SJohannes Berg 	struct sk_buff_head pending;
1700ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1701e3685e03SJohannes Berg 	unsigned long flags;
1702d012a605SMarco Porsch 	struct ps_data *ps;
1703d012a605SMarco Porsch 
17043918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
17053918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
17063918edb0SFelix Fietkau 				     u.ap);
17073918edb0SFelix Fietkau 
17083918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1709d012a605SMarco Porsch 		ps = &sdata->bss->ps;
17103f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
17113f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1712d012a605SMarco Porsch 	else
1713d012a605SMarco Porsch 		return;
171450a9432dSJohannes Berg 
1715c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
171647086fc5SJohannes Berg 
17175a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1718948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1719ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1720948d887dSJohannes Berg 
172130686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
172212375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1723af818581SJohannes Berg 
1724ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1725adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1726ba8c3d6fSFelix Fietkau 			continue;
1727ba8c3d6fSFelix Fietkau 
172818667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1729ba8c3d6fSFelix Fietkau 	}
1730ba8c3d6fSFelix Fietkau 
1731948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1732af818581SJohannes Berg 
17331d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
1734456bbb8aSRemi Pommarel 	spin_lock_bh(&sta->ps_lock);
1735af818581SJohannes Berg 	/* Send all buffered frames to the station */
1736948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1737948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1738948d887dSJohannes Berg 
1739987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1740948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1741987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1742948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1743948d887dSJohannes Berg 		filtered += tmp - count;
1744948d887dSJohannes Berg 		count = tmp;
1745948d887dSJohannes Berg 
1746987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1747948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1748987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1749948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1750948d887dSJohannes Berg 		buffered += tmp - count;
1751948d887dSJohannes Berg 	}
1752948d887dSJohannes Berg 
1753e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
17545ac2e350SJohannes Berg 
17555ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
17565ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
17575ac2e350SJohannes Berg 
17585ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
17595ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
17605ac2e350SJohannes Berg 	 */
17615ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
17625ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
1763456bbb8aSRemi Pommarel 	spin_unlock_bh(&sta->ps_lock);
1764948d887dSJohannes Berg 
1765e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1766e3685e03SJohannes Berg 
1767af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1768af818581SJohannes Berg 
1769c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1770c868cb35SJohannes Berg 
1771bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
17722595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1773bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
177417c18bf8SJohannes Berg 
177517c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1776af818581SJohannes Berg }
1777af818581SJohannes Berg 
ieee80211_send_null_response(struct sta_info * sta,int tid,enum ieee80211_frame_release_type reason,bool call_driver,bool more_data)17780ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1779b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
17800ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1781ce662b44SJohannes Berg {
17820ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1783ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1784ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1785ce662b44SJohannes Berg 	struct sk_buff *skb;
1786ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1787ce662b44SJohannes Berg 	__le16 fc;
1788a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1789ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
179055de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1791ce662b44SJohannes Berg 
1792ce662b44SJohannes Berg 	if (qos) {
1793ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1794ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1795ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1796ce662b44SJohannes Berg 	} else {
1797ce662b44SJohannes Berg 		size -= 2;
1798ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1799ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1800ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1801ce662b44SJohannes Berg 	}
1802ce662b44SJohannes Berg 
1803ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1804ce662b44SJohannes Berg 	if (!skb)
1805ce662b44SJohannes Berg 		return;
1806ce662b44SJohannes Berg 
1807ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1808ce662b44SJohannes Berg 
18094df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1810ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1811ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1812ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1813ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1814ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1815864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1816ce662b44SJohannes Berg 
1817ce662b44SJohannes Berg 	skb->priority = tid;
1818ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
181959b66255SJohannes Berg 	if (qos) {
1820ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1821ce662b44SJohannes Berg 
18220ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1823ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1824ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
18250ead2510SEmmanuel Grumbach 			if (more_data)
18260ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
18270ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
18280ead2510SEmmanuel Grumbach 		}
1829ce662b44SJohannes Berg 	}
1830ce662b44SJohannes Berg 
1831ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1832ce662b44SJohannes Berg 
1833ce662b44SJohannes Berg 	/*
1834ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1835ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1836deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1837deeaee19SJohannes Berg 	 * ends the poll/service period.
1838ce662b44SJohannes Berg 	 */
183902f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1840deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1841ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1842ce662b44SJohannes Berg 
18436b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
18446b127c71SSujith Manoharan 
1845b77cf4f8SJohannes Berg 	if (call_driver)
1846b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1847b77cf4f8SJohannes Berg 					  reason, false);
184840b96408SJohannes Berg 
184989afe614SJohannes Berg 	skb->dev = sdata->dev;
185089afe614SJohannes Berg 
185155de908aSJohannes Berg 	rcu_read_lock();
1852d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
185355de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
185455de908aSJohannes Berg 		rcu_read_unlock();
185555de908aSJohannes Berg 		kfree_skb(skb);
185655de908aSJohannes Berg 		return;
185755de908aSJohannes Berg 	}
185855de908aSJohannes Berg 
185973c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
186008aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
186155de908aSJohannes Berg 	rcu_read_unlock();
1862ce662b44SJohannes Berg }
1863ce662b44SJohannes Berg 
find_highest_prio_tid(unsigned long tids)18640a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
18650a1cb809SJohannes Berg {
18660a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
18670a1cb809SJohannes Berg 	if (tids & 0xF8)
18680a1cb809SJohannes Berg 		return fls(tids) - 1;
18690a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
18700a1cb809SJohannes Berg 	if (tids & BIT(0))
18710a1cb809SJohannes Berg 		return 0;
18720a1cb809SJohannes Berg 	return fls(tids) - 1;
18730a1cb809SJohannes Berg }
18740a1cb809SJohannes Berg 
18750ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
18760ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
18770ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
18780ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
18790ead2510SEmmanuel Grumbach  */
18800ead2510SEmmanuel Grumbach static bool
ieee80211_sta_ps_more_data(struct sta_info * sta,u8 ignored_acs,enum ieee80211_frame_release_type reason,unsigned long driver_release_tids)18810ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
18820ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
18830ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
18840ead2510SEmmanuel Grumbach {
18850ead2510SEmmanuel Grumbach 	int ac;
18860ead2510SEmmanuel Grumbach 
18870ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
18880ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
18890ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
18900ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
18910ead2510SEmmanuel Grumbach 	 */
18920ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
18930ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
18940ead2510SEmmanuel Grumbach 		return true;
18950ead2510SEmmanuel Grumbach 
18960ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1897f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
18980ead2510SEmmanuel Grumbach 			continue;
18990ead2510SEmmanuel Grumbach 
19000ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
19010ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
19020ead2510SEmmanuel Grumbach 			return true;
19030ead2510SEmmanuel Grumbach 	}
19040ead2510SEmmanuel Grumbach 
19050ead2510SEmmanuel Grumbach 	return false;
19060ead2510SEmmanuel Grumbach }
19070ead2510SEmmanuel Grumbach 
190847086fc5SJohannes Berg static void
ieee80211_sta_ps_get_frames(struct sta_info * sta,int n_frames,u8 ignored_acs,enum ieee80211_frame_release_type reason,struct sk_buff_head * frames,unsigned long * driver_release_tids)19090ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
19100ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
19110ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
19120ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1913af818581SJohannes Berg {
1914af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1915af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1916948d887dSJohannes Berg 	int ac;
1917af818581SJohannes Berg 
1918f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1919948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
19204049e09aSJohannes Berg 		unsigned long tids;
19214049e09aSJohannes Berg 
1922f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1923948d887dSJohannes Berg 			continue;
1924948d887dSJohannes Berg 
19254049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
19264049e09aSJohannes Berg 
1927f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1928f9f760b4SJohannes Berg 		 * release from hardware queues
1929f9f760b4SJohannes Berg 		 */
19300ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
19310ead2510SEmmanuel Grumbach 			*driver_release_tids |=
19320ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
19330ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1934ba8c3d6fSFelix Fietkau 		}
1935f9f760b4SJohannes Berg 
19360ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
193747086fc5SJohannes Berg 			struct sk_buff *skb;
193847086fc5SJohannes Berg 
193947086fc5SJohannes Berg 			while (n_frames > 0) {
1940948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1941948d887dSJohannes Berg 				if (!skb) {
194247086fc5SJohannes Berg 					skb = skb_dequeue(
194347086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1944af818581SJohannes Berg 					if (skb)
1945af818581SJohannes Berg 						local->total_ps_buffered--;
1946af818581SJohannes Berg 				}
194747086fc5SJohannes Berg 				if (!skb)
194847086fc5SJohannes Berg 					break;
194947086fc5SJohannes Berg 				n_frames--;
19500ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
195147086fc5SJohannes Berg 			}
1952948d887dSJohannes Berg 		}
1953948d887dSJohannes Berg 
19540ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
19550ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
19560ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
19574049e09aSJohannes Berg 		 */
1958948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
19590ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1960948d887dSJohannes Berg 			break;
1961948d887dSJohannes Berg 	}
1962948d887dSJohannes Berg }
1963af818581SJohannes Berg 
19640ead2510SEmmanuel Grumbach static void
ieee80211_sta_ps_deliver_response(struct sta_info * sta,int n_frames,u8 ignored_acs,enum ieee80211_frame_release_type reason)19650ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
19660ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
19670ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
19680ead2510SEmmanuel Grumbach {
19690ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
19700ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
19710ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
19720ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
19730ead2510SEmmanuel Grumbach 	bool more_data;
19740ead2510SEmmanuel Grumbach 
19750ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
19760ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
19770ead2510SEmmanuel Grumbach 
19780ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
19790ead2510SEmmanuel Grumbach 
19800ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
19810ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
19820ead2510SEmmanuel Grumbach 
19830ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
19840ead2510SEmmanuel Grumbach 
19851a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
19860ead2510SEmmanuel Grumbach 		driver_release_tids =
19870ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
19880ead2510SEmmanuel Grumbach 
1989f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1990f438ceb8SEmmanuel Grumbach 		int tid, ac;
19914049e09aSJohannes Berg 
1992ce662b44SJohannes Berg 		/*
1993ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1994ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1995ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1996ce662b44SJohannes Berg 		 *
1997ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1998ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1999ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
2000ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
2001ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
2002ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
2003ce662b44SJohannes Berg 		 *
2004ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
2005ce662b44SJohannes Berg 		 */
2006ce662b44SJohannes Berg 
2007ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
2008f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
2009d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
2010d7f84244SEmmanuel Grumbach 				break;
2011f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
2012ce662b44SJohannes Berg 
20130ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
2014f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
201547086fc5SJohannes Berg 		struct sk_buff_head pending;
201647086fc5SJohannes Berg 		struct sk_buff *skb;
201740b96408SJohannes Berg 		int num = 0;
201840b96408SJohannes Berg 		u16 tids = 0;
2019b77cf4f8SJohannes Berg 		bool need_null = false;
202047086fc5SJohannes Berg 
202147086fc5SJohannes Berg 		skb_queue_head_init(&pending);
202247086fc5SJohannes Berg 
202347086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
2024af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
202547086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
202640b96408SJohannes Berg 			u8 *qoshdr = NULL;
202740b96408SJohannes Berg 
202840b96408SJohannes Berg 			num++;
2029af818581SJohannes Berg 
2030af818581SJohannes Berg 			/*
203147086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
203247086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
203347086fc5SJohannes Berg 			 * exchange.
2034af818581SJohannes Berg 			 */
20356b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
20366b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
2037af818581SJohannes Berg 
203847086fc5SJohannes Berg 			/*
203947086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
204047086fc5SJohannes Berg 			 * more buffered frames for this STA
204147086fc5SJohannes Berg 			 */
204224b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
204347086fc5SJohannes Berg 				hdr->frame_control |=
204447086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
204524b9c373SJanusz.Dziedzic@tieto.com 			else
204624b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
204724b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
2048af818581SJohannes Berg 
204940b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
205040b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
205140b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
205240b96408SJohannes Berg 
2053b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
2054b77cf4f8SJohannes Berg 
2055b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
2056b77cf4f8SJohannes Berg 
2057b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
2058b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
2059b77cf4f8SJohannes Berg 				continue;
2060b77cf4f8SJohannes Berg 
2061b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
2062b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
2063b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
2064b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2065b77cf4f8SJohannes Berg 				break;
2066b77cf4f8SJohannes Berg 			}
2067b77cf4f8SJohannes Berg 
2068b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
2069b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
2070b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
2071b77cf4f8SJohannes Berg 			 * and be done.
2072b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
2073b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
2074b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
2075b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
2076b77cf4f8SJohannes Berg 			 *
2077b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
2078b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
2079b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
2080b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
2081b77cf4f8SJohannes Berg 			 *
2082b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
2083b77cf4f8SJohannes Berg 			 */
2084b77cf4f8SJohannes Berg 			if (qoshdr) {
208540b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
2086deeaee19SJohannes Berg 
208747086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
208847086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2089b77cf4f8SJohannes Berg 			} else {
2090b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
2091b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
2092b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
2093b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
2094b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
2095b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
2096b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
2097b77cf4f8SJohannes Berg 				 */
2098b77cf4f8SJohannes Berg 				hdr->frame_control |=
2099b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2100b77cf4f8SJohannes Berg 				need_null = true;
2101b77cf4f8SJohannes Berg 				num++;
210252a3f20cSMarco Porsch 			}
2103b77cf4f8SJohannes Berg 			break;
210447086fc5SJohannes Berg 		}
210547086fc5SJohannes Berg 
210640b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
210740b96408SJohannes Berg 					  reason, more_data);
210840b96408SJohannes Berg 
210947086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
2110af818581SJohannes Berg 
2111b77cf4f8SJohannes Berg 		if (need_null)
2112b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
21130ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
21140ead2510SEmmanuel Grumbach 				reason, false, false);
2115b77cf4f8SJohannes Berg 
2116c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
2117af818581SJohannes Berg 	} else {
2118ba8c3d6fSFelix Fietkau 		int tid;
2119ba8c3d6fSFelix Fietkau 
2120af818581SJohannes Berg 		/*
21214049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
21224049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
2123f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
2124f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
2125f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
2126f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
2127f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
2128f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
2129af818581SJohannes Berg 		 */
21304049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
213147086fc5SJohannes Berg 					    n_frames, reason, more_data);
21324049e09aSJohannes Berg 
21334049e09aSJohannes Berg 		/*
21344049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
21354049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
2136f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
21374049e09aSJohannes Berg 		 * release function.
2138f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
2139ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
2140ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
21414049e09aSJohannes Berg 		 */
2142ba8c3d6fSFelix Fietkau 
2143ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
2144adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
2145adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
21461e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
2147ba8c3d6fSFelix Fietkau 				continue;
2148ba8c3d6fSFelix Fietkau 
2149ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
2150ba8c3d6fSFelix Fietkau 			break;
2151ba8c3d6fSFelix Fietkau 		}
2152af818581SJohannes Berg 	}
2153af818581SJohannes Berg }
2154af818581SJohannes Berg 
ieee80211_sta_ps_deliver_poll_response(struct sta_info * sta)2155af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
2156af818581SJohannes Berg {
215747086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
2158af818581SJohannes Berg 
2159af818581SJohannes Berg 	/*
216047086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
216147086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
216247086fc5SJohannes Berg 	 * only from the non-enabled ones.
2163af818581SJohannes Berg 	 */
216447086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
216547086fc5SJohannes Berg 		ignore_for_response = 0;
2166af818581SJohannes Berg 
216747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
216847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
2169af818581SJohannes Berg }
217047086fc5SJohannes Berg 
ieee80211_sta_ps_deliver_uapsd(struct sta_info * sta)217147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
217247086fc5SJohannes Berg {
217347086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
217447086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
217547086fc5SJohannes Berg 
217647086fc5SJohannes Berg 	/*
217747086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
217847086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
217947086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
218047086fc5SJohannes Berg 	 * actually getting called.
218147086fc5SJohannes Berg 	 */
218247086fc5SJohannes Berg 	if (!delivery_enabled)
218347086fc5SJohannes Berg 		return;
218447086fc5SJohannes Berg 
218547086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
218647086fc5SJohannes Berg 	case 1:
218747086fc5SJohannes Berg 		n_frames = 2;
218847086fc5SJohannes Berg 		break;
218947086fc5SJohannes Berg 	case 2:
219047086fc5SJohannes Berg 		n_frames = 4;
219147086fc5SJohannes Berg 		break;
219247086fc5SJohannes Berg 	case 3:
219347086fc5SJohannes Berg 		n_frames = 6;
219447086fc5SJohannes Berg 		break;
219547086fc5SJohannes Berg 	case 0:
219647086fc5SJohannes Berg 		/* XXX: what is a good value? */
219713a8098aSAndrei Otcheretianski 		n_frames = 128;
219847086fc5SJohannes Berg 		break;
219947086fc5SJohannes Berg 	}
220047086fc5SJohannes Berg 
220147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
220247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
2203af818581SJohannes Berg }
2204af818581SJohannes Berg 
ieee80211_sta_block_awake(struct ieee80211_hw * hw,struct ieee80211_sta * pubsta,bool block)2205af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2206af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
2207af818581SJohannes Berg {
2208af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2209af818581SJohannes Berg 
2210b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
2211b5878a2dSJohannes Berg 
22125ac2e350SJohannes Berg 	if (block) {
2213c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
221417c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
22155ac2e350SJohannes Berg 		return;
22165ac2e350SJohannes Berg 	}
22175ac2e350SJohannes Berg 
22185ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
22195ac2e350SJohannes Berg 		return;
22205ac2e350SJohannes Berg 
22215ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
22225ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
22235ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
22245ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
22255ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
22265ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
22275ac2e350SJohannes Berg 		/* must be asleep in this case */
22285ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
22295ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
22305ac2e350SJohannes Berg 	} else {
22315ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
223217c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
22335ac2e350SJohannes Berg 	}
2234af818581SJohannes Berg }
2235af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
2236dcf55fb5SFelix Fietkau 
ieee80211_sta_eosp(struct ieee80211_sta * pubsta)2237e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
223837fbd908SJohannes Berg {
223937fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
224037fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
224137fbd908SJohannes Berg 
224237fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
224337fbd908SJohannes Berg 
224437fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
224537fbd908SJohannes Berg }
2246e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
224737fbd908SJohannes Berg 
ieee80211_send_eosp_nullfunc(struct ieee80211_sta * pubsta,int tid)22480ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
22490ead2510SEmmanuel Grumbach {
22500ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
22510ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
22520ead2510SEmmanuel Grumbach 	bool more_data;
22530ead2510SEmmanuel Grumbach 
22540ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
22550ead2510SEmmanuel Grumbach 
22560ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
22570ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
22580ead2510SEmmanuel Grumbach 					       reason, 0);
22590ead2510SEmmanuel Grumbach 
22600ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
22610ead2510SEmmanuel Grumbach }
22620ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
22630ead2510SEmmanuel Grumbach 
ieee80211_sta_set_buffered(struct ieee80211_sta * pubsta,u8 tid,bool buffered)2264042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2265042ec453SJohannes Berg 				u8 tid, bool buffered)
2266dcf55fb5SFelix Fietkau {
2267dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2268dcf55fb5SFelix Fietkau 
22695a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2270042ec453SJohannes Berg 		return;
2271042ec453SJohannes Berg 
22721b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
22731b000789SJohannes Berg 
2274948d887dSJohannes Berg 	if (buffered)
2275948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2276948d887dSJohannes Berg 	else
2277948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2278948d887dSJohannes Berg 
2279c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2280dcf55fb5SFelix Fietkau }
2281042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2282d9a7ddb0SJohannes Berg 
ieee80211_sta_register_airtime(struct ieee80211_sta * pubsta,u8 tid,u32 tx_airtime,u32 rx_airtime)2283b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2284b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2285b4809e94SToke Høiland-Jørgensen {
2286942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2287942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2288942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2289942741daSFelix Fietkau 	u32 airtime = 0;
2290c77bfab9SFelix Fietkau 	u32 diff;
2291b4809e94SToke Høiland-Jørgensen 
2292942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2293942741daSFelix Fietkau 		airtime += tx_airtime;
2294942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2295942741daSFelix Fietkau 		airtime += rx_airtime;
2296b4809e94SToke Høiland-Jørgensen 
2297942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2298942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2299942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2300c77bfab9SFelix Fietkau 
2301c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2302c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2303942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2304c77bfab9SFelix Fietkau 
2305942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2306b4809e94SToke Høiland-Jørgensen }
2307b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2308b4809e94SToke Høiland-Jørgensen 
__ieee80211_sta_recalc_aggregates(struct sta_info * sta,u16 active_links)23099b41a9d7SJohannes Berg void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
23104c51541dSBenjamin Berg {
23114c51541dSBenjamin Berg 	bool first = true;
23129b41a9d7SJohannes Berg 	int link_id;
23134c51541dSBenjamin Berg 
23149b41a9d7SJohannes Berg 	if (!sta->sta.valid_links || !sta->sta.mlo) {
23159b41a9d7SJohannes Berg 		sta->sta.cur = &sta->sta.deflink.agg;
23164c51541dSBenjamin Berg 		return;
23174c51541dSBenjamin Berg 	}
23184c51541dSBenjamin Berg 
23194c51541dSBenjamin Berg 	rcu_read_lock();
23209b41a9d7SJohannes Berg 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
23219b41a9d7SJohannes Berg 		struct ieee80211_link_sta *link_sta;
23229b41a9d7SJohannes Berg 		int i;
23239b41a9d7SJohannes Berg 
23249b41a9d7SJohannes Berg 		if (!(active_links & BIT(link_id)))
23259b41a9d7SJohannes Berg 			continue;
23269b41a9d7SJohannes Berg 
23279b41a9d7SJohannes Berg 		link_sta = rcu_dereference(sta->sta.link[link_id]);
23289b41a9d7SJohannes Berg 		if (!link_sta)
23299b41a9d7SJohannes Berg 			continue;
23309b41a9d7SJohannes Berg 
23314c51541dSBenjamin Berg 		if (first) {
23329b41a9d7SJohannes Berg 			sta->cur = sta->sta.deflink.agg;
23334c51541dSBenjamin Berg 			first = false;
23344c51541dSBenjamin Berg 			continue;
23354c51541dSBenjamin Berg 		}
23364c51541dSBenjamin Berg 
23374c51541dSBenjamin Berg 		sta->cur.max_amsdu_len =
23384c51541dSBenjamin Berg 			min(sta->cur.max_amsdu_len,
23394c51541dSBenjamin Berg 			    link_sta->agg.max_amsdu_len);
23404c51541dSBenjamin Berg 		sta->cur.max_rc_amsdu_len =
23414c51541dSBenjamin Berg 			min(sta->cur.max_rc_amsdu_len,
23424c51541dSBenjamin Berg 			    link_sta->agg.max_rc_amsdu_len);
23434c51541dSBenjamin Berg 
23444c51541dSBenjamin Berg 		for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
23454c51541dSBenjamin Berg 			sta->cur.max_tid_amsdu_len[i] =
23464c51541dSBenjamin Berg 				min(sta->cur.max_tid_amsdu_len[i],
23474c51541dSBenjamin Berg 				    link_sta->agg.max_tid_amsdu_len[i]);
23484c51541dSBenjamin Berg 	}
23494c51541dSBenjamin Berg 	rcu_read_unlock();
23504c51541dSBenjamin Berg 
23519b41a9d7SJohannes Berg 	sta->sta.cur = &sta->cur;
23529b41a9d7SJohannes Berg }
23539b41a9d7SJohannes Berg 
ieee80211_sta_recalc_aggregates(struct ieee80211_sta * pubsta)23549b41a9d7SJohannes Berg void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
23559b41a9d7SJohannes Berg {
23569b41a9d7SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
23579b41a9d7SJohannes Berg 
23589b41a9d7SJohannes Berg 	__ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
23594c51541dSBenjamin Berg }
23604c51541dSBenjamin Berg EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
23614c51541dSBenjamin Berg 
ieee80211_sta_update_pending_airtime(struct ieee80211_local * local,struct sta_info * sta,u8 ac,u16 tx_airtime,bool tx_completed)23623ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
23633ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
23643ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
23653ace10f5SKan Yan {
23663ace10f5SKan Yan 	int tx_pending;
23673ace10f5SKan Yan 
2368911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2369911bde0fSToke Høiland-Jørgensen 		return;
2370911bde0fSToke Høiland-Jørgensen 
23713ace10f5SKan Yan 	if (!tx_completed) {
23723ace10f5SKan Yan 		if (sta)
23733ace10f5SKan Yan 			atomic_add(tx_airtime,
23743ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
23753ace10f5SKan Yan 
23763ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
23778e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
23783ace10f5SKan Yan 		return;
23793ace10f5SKan Yan 	}
23803ace10f5SKan Yan 
23813ace10f5SKan Yan 	if (sta) {
23823ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
23833ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
238404e35caaSFelix Fietkau 		if (tx_pending < 0)
23853ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
23863ace10f5SKan Yan 				       tx_pending, 0);
23873ace10f5SKan Yan 	}
23883ace10f5SKan Yan 
23898e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
23903ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
23918e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
23923ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
23933ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
23943ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
23958e4bac06SFelix Fietkau 		      tx_airtime)) {
23968e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
23973ace10f5SKan Yan 			       tx_pending, 0);
23988e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
23998e4bac06SFelix Fietkau 	}
24003ace10f5SKan Yan }
24013ace10f5SKan Yan 
2402c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
sta_get_last_rx_stats(struct sta_info * sta)2403c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2404c9c5962bSJohannes Berg {
2405046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2406c9c5962bSJohannes Berg 	int cpu;
2407c9c5962bSJohannes Berg 
2408046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2409c9c5962bSJohannes Berg 		return stats;
2410c9c5962bSJohannes Berg 
2411c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2412c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2413c9c5962bSJohannes Berg 
2414046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2415c9c5962bSJohannes Berg 
2416c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2417c9c5962bSJohannes Berg 			stats = cpustats;
2418c9c5962bSJohannes Berg 	}
2419c9c5962bSJohannes Berg 
2420c9c5962bSJohannes Berg 	return stats;
2421c9c5962bSJohannes Berg }
2422c9c5962bSJohannes Berg 
sta_stats_decode_rate(struct ieee80211_local * local,u32 rate,struct rate_info * rinfo)242341cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
24244f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2425fbd6ff5cSJohannes Berg {
2426dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2427fbd6ff5cSJohannes Berg 
2428dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
24297f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
24304f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2431dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2432dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2433dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2434dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
24357f406cd1SJohannes Berg 		break;
24367f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
24374f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2438dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2439dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2440dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
24417f406cd1SJohannes Berg 		break;
24427f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2443fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2444fbd6ff5cSJohannes Berg 		u16 brate;
24454f6b1b3dSJohannes Berg 		unsigned int shift;
2446dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2447dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2448fbd6ff5cSJohannes Berg 
2449dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
24508b783d10SThomas Pedersen 
24518b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
24528b783d10SThomas Pedersen 			break;
24538b783d10SThomas Pedersen 
2454dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
24554f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
24564f6b1b3dSJohannes Berg 			shift = 2;
24574f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
24584f6b1b3dSJohannes Berg 			shift = 1;
24594f6b1b3dSJohannes Berg 		else
24604f6b1b3dSJohannes Berg 			shift = 0;
2461fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
24627f406cd1SJohannes Berg 		break;
24637f406cd1SJohannes Berg 		}
246441cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
246541cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
246641cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
246741cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
246841cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
246941cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
247041cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
247141cbb0f5SLuca Coelho 		break;
2472f66c48afSJohannes Berg 	case STA_STATS_RATE_TYPE_EHT:
2473f66c48afSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_EHT_MCS;
2474f66c48afSJohannes Berg 		rinfo->mcs = STA_STATS_GET(EHT_MCS, rate);
2475f66c48afSJohannes Berg 		rinfo->nss = STA_STATS_GET(EHT_NSS, rate);
2476f66c48afSJohannes Berg 		rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate);
2477f66c48afSJohannes Berg 		rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate);
2478f66c48afSJohannes Berg 		break;
2479fbd6ff5cSJohannes Berg 	}
24804f6b1b3dSJohannes Berg }
2481fbd6ff5cSJohannes Berg 
sta_set_rate_info_rx(struct sta_info * sta,struct rate_info * rinfo)2482a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
24834f6b1b3dSJohannes Berg {
248459336e07SShayne Chen 	u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
24854f6b1b3dSJohannes Berg 
24864f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2487a17d93ffSBen Greear 		return -EINVAL;
2488a17d93ffSBen Greear 
24894f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2490a17d93ffSBen Greear 	return 0;
2491fbd6ff5cSJohannes Berg }
2492fbd6ff5cSJohannes Berg 
sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats * rxstats,int tid)2493b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2494b255b72bSSeevalamuthu Mariappan 					int tid)
2495b255b72bSSeevalamuthu Mariappan {
2496b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2497b255b72bSSeevalamuthu Mariappan 	u64 value;
2498b255b72bSSeevalamuthu Mariappan 
2499b255b72bSSeevalamuthu Mariappan 	do {
2500d120d1a6SThomas Gleixner 		start = u64_stats_fetch_begin(&rxstats->syncp);
2501b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2502d120d1a6SThomas Gleixner 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2503b255b72bSSeevalamuthu Mariappan 
2504b255b72bSSeevalamuthu Mariappan 	return value;
2505b255b72bSSeevalamuthu Mariappan }
2506b255b72bSSeevalamuthu Mariappan 
sta_set_tidstats(struct sta_info * sta,struct cfg80211_tid_stats * tidstats,int tid)25070f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
25080f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
25090f9c5a61SJohannes Berg 			     int tid)
25100f9c5a61SJohannes Berg {
25110f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2512b255b72bSSeevalamuthu Mariappan 	int cpu;
25130f9c5a61SJohannes Berg 
25140f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2515046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2516046d2e7cSSriram R 							   tid);
25170f9c5a61SJohannes Berg 
2518046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2519b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2520b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2521b255b72bSSeevalamuthu Mariappan 
2522046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2523046d2e7cSSriram R 						     cpu);
2524b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2525b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2526b255b72bSSeevalamuthu Mariappan 			}
2527b255b72bSSeevalamuthu Mariappan 		}
25280f9c5a61SJohannes Berg 
25290f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
25300f9c5a61SJohannes Berg 	}
25310f9c5a61SJohannes Berg 
25320f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
25330f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2534046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
25350f9c5a61SJohannes Berg 	}
25360f9c5a61SJohannes Berg 
25370f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
25380f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
25390f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2540046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
25410f9c5a61SJohannes Berg 	}
25420f9c5a61SJohannes Berg 
25430f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
25440f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
25450f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2546046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
25470f9c5a61SJohannes Berg 	}
25482fe4a29aSToke Høiland-Jørgensen 
2549107395f9SAlexander Wetzel 	if (tid < IEEE80211_NUM_TIDS) {
25502fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
25512fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
25522fe4a29aSToke Høiland-Jørgensen 
25532fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
25542fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
25552fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
25562fe4a29aSToke Høiland-Jørgensen 
25572fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
25582fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
25592fe4a29aSToke Høiland-Jørgensen 	}
25600f9c5a61SJohannes Berg }
25610f9c5a61SJohannes Berg 
sta_get_stats_bytes(struct ieee80211_sta_rx_stats * rxstats)2562c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2563c9c5962bSJohannes Berg {
2564c9c5962bSJohannes Berg 	unsigned int start;
2565c9c5962bSJohannes Berg 	u64 value;
2566c9c5962bSJohannes Berg 
2567c9c5962bSJohannes Berg 	do {
2568d120d1a6SThomas Gleixner 		start = u64_stats_fetch_begin(&rxstats->syncp);
2569c9c5962bSJohannes Berg 		value = rxstats->bytes;
2570d120d1a6SThomas Gleixner 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2571c9c5962bSJohannes Berg 
2572c9c5962bSJohannes Berg 	return value;
2573c9c5962bSJohannes Berg }
2574c9c5962bSJohannes Berg 
sta_set_sinfo(struct sta_info * sta,struct station_info * sinfo,bool tidstats)25750fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
25760fdf1493SJohannes Berg 		   bool tidstats)
2577b7ffbd7eSJohannes Berg {
2578b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2579b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2580b7ffbd7eSJohannes Berg 	u32 thr = 0;
2581c9c5962bSJohannes Berg 	int i, ac, cpu;
2582c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2583c9c5962bSJohannes Berg 
2584c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2585b7ffbd7eSJohannes Berg 
2586b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2587b7ffbd7eSJohannes Berg 
2588225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2589225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2590225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2591225b8189SJohannes Berg 	 */
2592225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2593bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2594225b8189SJohannes Berg 
25952b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2596a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2597a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2598a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2599a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
26009cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2601a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2602976bd9efSJohannes Berg 
2603976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2604bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2605bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2606a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2607976bd9efSJohannes Berg 	}
2608b7ffbd7eSJohannes Berg 
260984b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
26109cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2611e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2612b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
26132b9a7e1bSJohannes Berg 
2614a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2615a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2616b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
26172b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2618046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2619a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2620b7ffbd7eSJohannes Berg 	}
26212b9a7e1bSJohannes Berg 
2622a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
26232b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
26242b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2625046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2626a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
26272b9a7e1bSJohannes Berg 	}
26282b9a7e1bSJohannes Berg 
2629a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2630a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2631046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
26320f9c5a61SJohannes Berg 
2633046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2634c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2635c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2636c9c5962bSJohannes Berg 
2637046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2638046d2e7cSSriram R 						     cpu);
2639c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2640c9c5962bSJohannes Berg 			}
2641c9c5962bSJohannes Berg 		}
2642c9c5962bSJohannes Berg 
2643a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
26442b9a7e1bSJohannes Berg 	}
26452b9a7e1bSJohannes Berg 
2646a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2647046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2648046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2649c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2650c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2651c9c5962bSJohannes Berg 
2652046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2653046d2e7cSSriram R 						     cpu);
2654c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2655c9c5962bSJohannes Berg 			}
2656c9c5962bSJohannes Berg 		}
2657a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
26582b9a7e1bSJohannes Berg 	}
26592b9a7e1bSJohannes Berg 
2660a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2661046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2662a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
26632b9a7e1bSJohannes Berg 	}
26642b9a7e1bSJohannes Berg 
2665a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2666046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2667a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
26682b9a7e1bSJohannes Berg 	}
26692b9a7e1bSJohannes Berg 
2670b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2671b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2672b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2673b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2674b4809e94SToke Høiland-Jørgensen 	}
2675b4809e94SToke Høiland-Jørgensen 
2676b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2677b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2678b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2679b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2680b4809e94SToke Høiland-Jørgensen 	}
2681b4809e94SToke Høiland-Jørgensen 
2682b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2683942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2684b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2685b4809e94SToke Høiland-Jørgensen 	}
2686b4809e94SToke Høiland-Jørgensen 
2687046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2688046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2689c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2690c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2691c9c5962bSJohannes Berg 
2692046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2693e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2694c9c5962bSJohannes Berg 		}
2695c9c5962bSJohannes Berg 	}
2696b7ffbd7eSJohannes Berg 
2697225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2698225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2699a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2700a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2701225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2702225b8189SJohannes Berg 	}
2703225b8189SJohannes Berg 
270430686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
270530686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2706a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2707c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2708a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2709b7ffbd7eSJohannes Berg 		}
27102b9a7e1bSJohannes Berg 
2711046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2712a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
271340d9a38aSJohannes Berg 			sinfo->signal_avg =
2714046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2715a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
27162b9a7e1bSJohannes Berg 		}
27172b9a7e1bSJohannes Berg 	}
27182b9a7e1bSJohannes Berg 
2719c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2720c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2721c9c5962bSJohannes Berg 	 * pcpu statistics
2722c9c5962bSJohannes Berg 	 */
2723c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2724a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2725a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2726a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2727046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2728a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2729b7ffbd7eSJohannes Berg 
2730c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2731c9c5962bSJohannes Berg 
2732b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2733e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2734c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2735b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2736046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2737b7ffbd7eSJohannes Berg 		}
2738b7ffbd7eSJohannes Berg 	}
2739b7ffbd7eSJohannes Berg 
27408a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
27418a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2742046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2743e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2744a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
27452b9a7e1bSJohannes Berg 	}
27462b9a7e1bSJohannes Berg 
27478a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
27488a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2749a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2750a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
27512b9a7e1bSJohannes Berg 	}
2752b7ffbd7eSJohannes Berg 
27530fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
27546af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
27556af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
27568689c051SArend van Spriel 	}
275779c892b8SJohannes Berg 
2758b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2759b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2760a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2761a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2762a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2763a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2764a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2765dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
27661303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
27671303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2768b7ffbd7eSJohannes Berg 
2769433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2770433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2771433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2772b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2773a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2774433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2775b7ffbd7eSJohannes Berg 		}
2776433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2777433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2778433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2779dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
27801303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2781b7ffbd7eSJohannes Berg #endif
2782b7ffbd7eSJohannes Berg 	}
2783b7ffbd7eSJohannes Berg 
2784b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2785b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2786b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2787b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2788b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2789b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2790b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2791785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2792b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2793b7ffbd7eSJohannes Berg 
2794b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2795b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2796b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2797b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2798b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2799b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2800b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2801b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2802b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2803b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2804b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2805b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2806a74a8c84SJohannes Berg 	if (sta->sta.wme)
2807b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2808b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2809b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2810b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2811b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2812b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2813b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2814b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2815b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2816b7ffbd7eSJohannes Berg 
28173b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
28183b17fbf8SMaxim Altshul 
28193b17fbf8SMaxim Altshul 	if (thr != 0) {
2820a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
28213b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
28223b17fbf8SMaxim Altshul 	}
2823a78b26ffSVenkateswara Naralasetty 
2824a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2825046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2826046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2827a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2828a78b26ffSVenkateswara Naralasetty 	}
2829cc60dbbfSBalaji Pothunoori 
28309c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2831046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2832cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2833cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2834046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2835cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
28369c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2837cc60dbbfSBalaji Pothunoori 	}
2838ab60633cSNarayanraddi Masti 
2839ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2840ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2841ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2842ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2843ab60633cSNarayanraddi Masti 	}
28443b17fbf8SMaxim Altshul }
28453b17fbf8SMaxim Altshul 
sta_get_expected_throughput(struct sta_info * sta)28463b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
28473b17fbf8SMaxim Altshul {
28483b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
28493b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
28503b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
28513b17fbf8SMaxim Altshul 	u32 thr = 0;
28523b17fbf8SMaxim Altshul 
28533b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
28543b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
28553b17fbf8SMaxim Altshul 
2856b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2857b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2858b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2859b7ffbd7eSJohannes Berg 	else
28604fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2861b7ffbd7eSJohannes Berg 
28623b17fbf8SMaxim Altshul 	return thr;
2863b7ffbd7eSJohannes Berg }
2864b8da6b6aSJohannes Berg 
ieee80211_sta_last_active(struct sta_info * sta)2865b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2866b8da6b6aSJohannes Berg {
2867c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2868c9c5962bSJohannes Berg 
2869046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2870046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2871c9c5962bSJohannes Berg 		return stats->last_rx;
2872046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2873b8da6b6aSJohannes Berg }
2874484a54c2SToke Høiland-Jørgensen 
sta_update_codel_params(struct sta_info * sta,u32 thr)2875484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2876484a54c2SToke Høiland-Jørgensen {
2877484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2878484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2879484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2880484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2881484a54c2SToke Høiland-Jørgensen 	} else {
2882484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2883484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2884484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2885484a54c2SToke Høiland-Jørgensen 	}
2886484a54c2SToke Høiland-Jørgensen }
2887484a54c2SToke Høiland-Jørgensen 
ieee80211_sta_set_expected_throughput(struct ieee80211_sta * pubsta,u32 thr)2888484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2889484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2890484a54c2SToke Høiland-Jørgensen {
2891484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2892484a54c2SToke Høiland-Jørgensen 
2893484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2894484a54c2SToke Høiland-Jørgensen }
2895cb71f1d1SJohannes Berg 
ieee80211_sta_allocate_link(struct sta_info * sta,unsigned int link_id)2896cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2897cb71f1d1SJohannes Berg {
2898cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2899cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2900cb71f1d1SJohannes Berg 	int ret;
2901cb71f1d1SJohannes Berg 
2902cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2903cb71f1d1SJohannes Berg 
2904cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2905cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2906cb71f1d1SJohannes Berg 		return -EINVAL;
2907cb71f1d1SJohannes Berg 
2908cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2909cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2910cb71f1d1SJohannes Berg 		return -EBUSY;
2911cb71f1d1SJohannes Berg 
2912cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2913cb71f1d1SJohannes Berg 	if (!alloc)
2914cb71f1d1SJohannes Berg 		return -ENOMEM;
2915cb71f1d1SJohannes Berg 
2916cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2917cb71f1d1SJohannes Berg 	if (ret) {
2918cb71f1d1SJohannes Berg 		kfree(alloc);
2919cb71f1d1SJohannes Berg 		return ret;
2920cb71f1d1SJohannes Berg 	}
2921cb71f1d1SJohannes Berg 
2922cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2923cb71f1d1SJohannes Berg 
2924d2caad52SBenjamin Berg 	ieee80211_link_sta_debugfs_add(&alloc->info);
2925d2caad52SBenjamin Berg 
2926cb71f1d1SJohannes Berg 	return 0;
2927cb71f1d1SJohannes Berg }
2928cb71f1d1SJohannes Berg 
ieee80211_sta_free_link(struct sta_info * sta,unsigned int link_id)292921476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
293021476ad1SShaul Triebitz {
293121476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
293221476ad1SShaul Triebitz 
293321476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
293421476ad1SShaul Triebitz }
293521476ad1SShaul Triebitz 
ieee80211_sta_activate_link(struct sta_info * sta,unsigned int link_id)2936cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2937cb71f1d1SJohannes Berg {
2938cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2939c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2940cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2941cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2942cb71f1d1SJohannes Berg 	int ret;
2943cb71f1d1SJohannes Berg 
2944c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2945c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2946cb71f1d1SJohannes Berg 
2947c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2948cb71f1d1SJohannes Berg 		return -EINVAL;
2949cb71f1d1SJohannes Berg 
2950177577dbSJohannes Berg 	rcu_read_lock();
2951177577dbSJohannes Berg 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2952177577dbSJohannes Berg 		rcu_read_unlock();
2953177577dbSJohannes Berg 		return -EALREADY;
2954177577dbSJohannes Berg 	}
2955177577dbSJohannes Berg 	/* we only modify under the mutex so this is fine */
2956177577dbSJohannes Berg 	rcu_read_unlock();
2957177577dbSJohannes Berg 
2958cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2959cb71f1d1SJohannes Berg 
296080e2b1faSLukas Bulwahn 	if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2961ba6ddab9SJohannes Berg 		goto hash;
2962cb71f1d1SJohannes Berg 
2963ba7af265SJohannes Berg 	ieee80211_recalc_min_chandef(sdata, link_id);
2964ba7af265SJohannes Berg 
29654c51541dSBenjamin Berg 	/* Ensure the values are updated for the driver,
29664c51541dSBenjamin Berg 	 * redone by sta_remove_link on failure.
29674c51541dSBenjamin Berg 	 */
29684c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
29694c51541dSBenjamin Berg 
2970cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2971cb71f1d1SJohannes Berg 				   old_links, new_links);
2972cb71f1d1SJohannes Berg 	if (ret) {
2973cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2974ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2975177577dbSJohannes Berg 		return ret;
2976cb71f1d1SJohannes Berg 	}
2977cb71f1d1SJohannes Berg 
2978ba6ddab9SJohannes Berg hash:
2979177577dbSJohannes Berg 	ret = link_sta_info_hash_add(sdata->local, link_sta);
2980177577dbSJohannes Berg 	WARN_ON(ret);
2981177577dbSJohannes Berg 	return 0;
2982cb71f1d1SJohannes Berg }
2983cb71f1d1SJohannes Berg 
ieee80211_sta_remove_link(struct sta_info * sta,unsigned int link_id)2984cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2985cb71f1d1SJohannes Berg {
2986cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2987a8f62399SShaul Triebitz 	u16 old_links = sta->sta.valid_links;
2988cb71f1d1SJohannes Berg 
2989cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2990cb71f1d1SJohannes Berg 
2991cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2992cb71f1d1SJohannes Berg 
2993cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2994cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2995a8f62399SShaul Triebitz 				     old_links, sta->sta.valid_links);
2996cb71f1d1SJohannes Berg 
2997ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2998cb71f1d1SJohannes Berg }
2999175ad2ecSJohannes Berg 
ieee80211_sta_set_max_amsdu_subframes(struct sta_info * sta,const u8 * ext_capab,unsigned int ext_capab_len)3000175ad2ecSJohannes Berg void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
3001175ad2ecSJohannes Berg 					   const u8 *ext_capab,
3002175ad2ecSJohannes Berg 					   unsigned int ext_capab_len)
3003175ad2ecSJohannes Berg {
3004175ad2ecSJohannes Berg 	u8 val;
3005175ad2ecSJohannes Berg 
3006175ad2ecSJohannes Berg 	sta->sta.max_amsdu_subframes = 0;
3007175ad2ecSJohannes Berg 
3008175ad2ecSJohannes Berg 	if (ext_capab_len < 8)
3009175ad2ecSJohannes Berg 		return;
3010175ad2ecSJohannes Berg 
3011175ad2ecSJohannes Berg 	/* The sender might not have sent the last bit, consider it to be 0 */
3012175ad2ecSJohannes Berg 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
3013175ad2ecSJohannes Berg 
3014175ad2ecSJohannes Berg 	/* we did get all the bits, take the MSB as well */
3015175ad2ecSJohannes Berg 	if (ext_capab_len >= 9)
3016175ad2ecSJohannes Berg 		val |= u8_get_bits(ext_capab[8],
3017175ad2ecSJohannes Berg 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
3018175ad2ecSJohannes Berg 
3019175ad2ecSJohannes Berg 	if (val)
30202085a3d1SJohannes Berg 		sta->sta.max_amsdu_subframes = 4 << (4 - val);
3021175ad2ecSJohannes Berg }
302265fd846cSJohannes Berg 
302365fd846cSJohannes Berg #ifdef CONFIG_LOCKDEP
lockdep_sta_mutex_held(struct ieee80211_sta * pubsta)302465fd846cSJohannes Berg bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
302565fd846cSJohannes Berg {
302665fd846cSJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
302765fd846cSJohannes Berg 
302865fd846cSJohannes Berg 	return lockdep_is_held(&sta->local->sta_mtx);
302965fd846cSJohannes Berg }
303065fd846cSJohannes Berg EXPORT_SYMBOL(lockdep_sta_mutex_held);
303165fd846cSJohannes Berg #endif
3032