xref: /openbmc/linux/net/mac80211/sta_info.c (revision 4c51541ddb78cef2da9c4c30006c0d9d06ea9a77)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f0706e82SJiri Benc /*
3f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
4f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
6dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
73a11ce08SJohannes Berg  * Copyright (C) 2018-2021 Intel Corporation
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
67cb71f1d1SJohannes Berg struct sta_link_alloc {
68cb71f1d1SJohannes Berg 	struct link_sta_info info;
69cb71f1d1SJohannes Berg 	struct ieee80211_link_sta sta;
70c71420dbSJohannes Berg 	struct rcu_head rcu_head;
71cb71f1d1SJohannes Berg };
72cb71f1d1SJohannes Berg 
737bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
747bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
75caf22d31SJohannes Berg 	.automatic_shrinking = true,
767bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
77ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
787bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
79ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
807bedd0cfSJohannes Berg };
817bedd0cfSJohannes Berg 
82ba6ddab9SJohannes Berg static const struct rhashtable_params link_sta_rht_params = {
83ba6ddab9SJohannes Berg 	.nelem_hint = 3, /* start small */
84ba6ddab9SJohannes Berg 	.automatic_shrinking = true,
85ba6ddab9SJohannes Berg 	.head_offset = offsetof(struct link_sta_info, link_hash_node),
86ba6ddab9SJohannes Berg 	.key_offset = offsetof(struct link_sta_info, addr),
87ba6ddab9SJohannes Berg 	.key_len = ETH_ALEN,
88ba6ddab9SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89ba6ddab9SJohannes Berg };
90ba6ddab9SJohannes Berg 
914d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
92be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
93f0706e82SJiri Benc 			     struct sta_info *sta)
94f0706e82SJiri Benc {
9583e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
967bedd0cfSJohannes Berg 			       sta_rht_params);
97f0706e82SJiri Benc }
98f0706e82SJiri Benc 
99f36fe0a2SJohannes Berg static int link_sta_info_hash_add(struct ieee80211_local *local,
100f36fe0a2SJohannes Berg 				  struct link_sta_info *link_sta)
101f36fe0a2SJohannes Berg {
102177577dbSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
103f36fe0a2SJohannes Berg 	return rhltable_insert(&local->link_sta_hash,
104f36fe0a2SJohannes Berg 			       &link_sta->link_hash_node,
105f36fe0a2SJohannes Berg 			       link_sta_rht_params);
106f36fe0a2SJohannes Berg }
107f36fe0a2SJohannes Berg 
108ba6ddab9SJohannes Berg static int link_sta_info_hash_del(struct ieee80211_local *local,
109ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
110ba6ddab9SJohannes Berg {
111177577dbSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
112ba6ddab9SJohannes Berg 	return rhltable_remove(&local->link_sta_hash,
113ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
114ba6ddab9SJohannes Berg 			       link_sta_rht_params);
115ba6ddab9SJohannes Berg }
116ba6ddab9SJohannes Berg 
1175108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
118b22cfcfcSEliad Peller {
119b22cfcfcSEliad Peller 	int ac, i;
120b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
121b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
122b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
123d012a605SMarco Porsch 	struct ps_data *ps;
124b22cfcfcSEliad Peller 
125e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1265ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1275ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
128d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
129d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
130d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1313f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1323f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
133d012a605SMarco Porsch 		else
134d012a605SMarco Porsch 			return;
135b22cfcfcSEliad Peller 
136b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
137e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1385ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
139b22cfcfcSEliad Peller 
140d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
141b22cfcfcSEliad Peller 	}
142b22cfcfcSEliad Peller 
143ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
144ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
145adf8ed01SJohannes Berg 			struct txq_info *txqi;
146adf8ed01SJohannes Berg 
147adf8ed01SJohannes Berg 			if (!sta->sta.txq[i])
148adf8ed01SJohannes Berg 				continue;
149adf8ed01SJohannes Berg 
150adf8ed01SJohannes Berg 			txqi = to_txq_info(sta->sta.txq[i]);
151ba8c3d6fSFelix Fietkau 
152fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
153ba8c3d6fSFelix Fietkau 		}
154ba8c3d6fSFelix Fietkau 	}
155ba8c3d6fSFelix Fietkau 
156b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
157b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1581f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1591f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
160b22cfcfcSEliad Peller 	}
161b22cfcfcSEliad Peller 
16245b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
16345b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
164b22cfcfcSEliad Peller 
1655ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
166b22cfcfcSEliad Peller 
167b22cfcfcSEliad Peller 	/*
168b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
169b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
170b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
171b22cfcfcSEliad Peller 	 * directly by station destruction.
172b22cfcfcSEliad Peller 	 */
1735a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
174661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
175b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
176b22cfcfcSEliad Peller 		if (!tid_tx)
177b22cfcfcSEliad Peller 			continue;
1781f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
179b22cfcfcSEliad Peller 		kfree(tid_tx);
180b22cfcfcSEliad Peller 	}
1815108ca82SJohannes Berg }
182b22cfcfcSEliad Peller 
1835108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1845108ca82SJohannes Berg {
1855108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1865108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1875108ca82SJohannes Berg 
1885108ca82SJohannes Berg 	__cleanup_single_sta(sta);
189b22cfcfcSEliad Peller 	sta_info_free(local, sta);
190b22cfcfcSEliad Peller }
191b22cfcfcSEliad Peller 
19283e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
19383e7e4ceSHerbert Xu 					 const u8 *addr)
19483e7e4ceSHerbert Xu {
19583e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
19683e7e4ceSHerbert Xu }
19783e7e4ceSHerbert Xu 
198d0709a65SJohannes Berg /* protected by RCU */
199abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
200abe60632SJohannes Berg 			      const u8 *addr)
20143ba7e95SJohannes Berg {
202abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
20383e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
20460f4b626SJohannes Berg 	struct sta_info *sta;
20543ba7e95SJohannes Berg 
20660f4b626SJohannes Berg 	rcu_read_lock();
20783e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
20860f4b626SJohannes Berg 		if (sta->sdata == sdata) {
20960f4b626SJohannes Berg 			rcu_read_unlock();
21060f4b626SJohannes Berg 			/* this is safe as the caller must already hold
21160f4b626SJohannes Berg 			 * another rcu read section or the mutex
21260f4b626SJohannes Berg 			 */
21360f4b626SJohannes Berg 			return sta;
21460f4b626SJohannes Berg 		}
21560f4b626SJohannes Berg 	}
21660f4b626SJohannes Berg 	rcu_read_unlock();
21760f4b626SJohannes Berg 	return NULL;
21843ba7e95SJohannes Berg }
21943ba7e95SJohannes Berg 
2200e5ded5aSFelix Fietkau /*
2210e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
2220e5ded5aSFelix Fietkau  * or from one of its vlans
2230e5ded5aSFelix Fietkau  */
2240e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
2250e5ded5aSFelix Fietkau 				  const u8 *addr)
2260e5ded5aSFelix Fietkau {
2270e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
22883e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
2290e5ded5aSFelix Fietkau 	struct sta_info *sta;
2300e5ded5aSFelix Fietkau 
2317bedd0cfSJohannes Berg 	rcu_read_lock();
23283e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2337bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2347bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2357bedd0cfSJohannes Berg 			rcu_read_unlock();
2367bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2377bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2387bedd0cfSJohannes Berg 			 */
2390e5ded5aSFelix Fietkau 			return sta;
2400e5ded5aSFelix Fietkau 		}
2417bedd0cfSJohannes Berg 	}
2427bedd0cfSJohannes Berg 	rcu_read_unlock();
2437bedd0cfSJohannes Berg 	return NULL;
2447bedd0cfSJohannes Berg }
2450e5ded5aSFelix Fietkau 
246ba6ddab9SJohannes Berg struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
247ba6ddab9SJohannes Berg 					      const u8 *addr)
248ba6ddab9SJohannes Berg {
249ba6ddab9SJohannes Berg 	return rhltable_lookup(&local->link_sta_hash, addr,
250ba6ddab9SJohannes Berg 			       link_sta_rht_params);
251ba6ddab9SJohannes Berg }
252ba6ddab9SJohannes Berg 
253ba6ddab9SJohannes Berg struct link_sta_info *
254ba6ddab9SJohannes Berg link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
255ba6ddab9SJohannes Berg {
256ba6ddab9SJohannes Berg 	struct ieee80211_local *local = sdata->local;
257ba6ddab9SJohannes Berg 	struct rhlist_head *tmp;
258ba6ddab9SJohannes Berg 	struct link_sta_info *link_sta;
259ba6ddab9SJohannes Berg 
260ba6ddab9SJohannes Berg 	rcu_read_lock();
261ba6ddab9SJohannes Berg 	for_each_link_sta_info(local, addr, link_sta, tmp) {
262ba6ddab9SJohannes Berg 		struct sta_info *sta = link_sta->sta;
263ba6ddab9SJohannes Berg 
264ba6ddab9SJohannes Berg 		if (sta->sdata == sdata ||
265ba6ddab9SJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
266ba6ddab9SJohannes Berg 			rcu_read_unlock();
267ba6ddab9SJohannes Berg 			/* this is safe as the caller must already hold
268ba6ddab9SJohannes Berg 			 * another rcu read section or the mutex
269ba6ddab9SJohannes Berg 			 */
270ba6ddab9SJohannes Berg 			return link_sta;
271ba6ddab9SJohannes Berg 		}
272ba6ddab9SJohannes Berg 	}
273ba6ddab9SJohannes Berg 	rcu_read_unlock();
274ba6ddab9SJohannes Berg 	return NULL;
275ba6ddab9SJohannes Berg }
276ba6ddab9SJohannes Berg 
277ffa9598eSJohannes Berg struct ieee80211_sta *
278ffa9598eSJohannes Berg ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
279ffa9598eSJohannes Berg 				 const u8 *addr,
280ffa9598eSJohannes Berg 				 const u8 *localaddr,
281ffa9598eSJohannes Berg 				 unsigned int *link_id)
282ffa9598eSJohannes Berg {
283ffa9598eSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
284ffa9598eSJohannes Berg 	struct link_sta_info *link_sta;
285ffa9598eSJohannes Berg 	struct rhlist_head *tmp;
286ffa9598eSJohannes Berg 
287ffa9598eSJohannes Berg 	for_each_link_sta_info(local, addr, link_sta, tmp) {
288ffa9598eSJohannes Berg 		struct sta_info *sta = link_sta->sta;
289ffa9598eSJohannes Berg 		struct ieee80211_link_data *link;
290ffa9598eSJohannes Berg 		u8 _link_id = link_sta->link_id;
291ffa9598eSJohannes Berg 
292ffa9598eSJohannes Berg 		if (!localaddr) {
293ffa9598eSJohannes Berg 			if (link_id)
294ffa9598eSJohannes Berg 				*link_id = _link_id;
295ffa9598eSJohannes Berg 			return &sta->sta;
296ffa9598eSJohannes Berg 		}
297ffa9598eSJohannes Berg 
298ffa9598eSJohannes Berg 		link = rcu_dereference(sta->sdata->link[_link_id]);
299ffa9598eSJohannes Berg 		if (!link)
300ffa9598eSJohannes Berg 			continue;
301ffa9598eSJohannes Berg 
302ffa9598eSJohannes Berg 		if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
303ffa9598eSJohannes Berg 			continue;
304ffa9598eSJohannes Berg 
305ffa9598eSJohannes Berg 		if (link_id)
306ffa9598eSJohannes Berg 			*link_id = _link_id;
307ffa9598eSJohannes Berg 		return &sta->sta;
308ffa9598eSJohannes Berg 	}
309ffa9598eSJohannes Berg 
310ffa9598eSJohannes Berg 	return NULL;
311ffa9598eSJohannes Berg }
312ffa9598eSJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
313ffa9598eSJohannes Berg 
3145072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
3155072f73cSToke Høiland-Jørgensen 				       const u8 *sta_addr, const u8 *vif_addr)
3165072f73cSToke Høiland-Jørgensen {
3175072f73cSToke Høiland-Jørgensen 	struct rhlist_head *tmp;
3185072f73cSToke Høiland-Jørgensen 	struct sta_info *sta;
3195072f73cSToke Høiland-Jørgensen 
3205072f73cSToke Høiland-Jørgensen 	for_each_sta_info(local, sta_addr, sta, tmp) {
3215072f73cSToke Høiland-Jørgensen 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
3225072f73cSToke Høiland-Jørgensen 			return sta;
3235072f73cSToke Høiland-Jørgensen 	}
3245072f73cSToke Høiland-Jørgensen 
3255072f73cSToke Høiland-Jørgensen 	return NULL;
3265072f73cSToke Høiland-Jørgensen }
3275072f73cSToke Høiland-Jørgensen 
3283b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
3293b53fde8SJohannes Berg 				     int idx)
330ee385855SLuis Carlos Cobo {
3313b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
332ee385855SLuis Carlos Cobo 	struct sta_info *sta;
333ee385855SLuis Carlos Cobo 	int i = 0;
334ee385855SLuis Carlos Cobo 
3358ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
3368ca47eb9SMadhuparna Bhowmik 				lockdep_is_held(&local->sta_mtx)) {
3373b53fde8SJohannes Berg 		if (sdata != sta->sdata)
3382a8ca29aSLuis Carlos Cobo 			continue;
339ee385855SLuis Carlos Cobo 		if (i < idx) {
340ee385855SLuis Carlos Cobo 			++i;
341ee385855SLuis Carlos Cobo 			continue;
342ee385855SLuis Carlos Cobo 		}
3432a8ca29aSLuis Carlos Cobo 		return sta;
344ee385855SLuis Carlos Cobo 	}
345ee385855SLuis Carlos Cobo 
346ee385855SLuis Carlos Cobo 	return NULL;
347ee385855SLuis Carlos Cobo }
348f0706e82SJiri Benc 
349cb71f1d1SJohannes Berg static void sta_info_free_link(struct link_sta_info *link_sta)
350246b39e4SJohannes Berg {
351cb71f1d1SJohannes Berg 	free_percpu(link_sta->pcpu_rx_stats);
352cb71f1d1SJohannes Berg }
353246b39e4SJohannes Berg 
354ba6ddab9SJohannes Berg static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
355ba6ddab9SJohannes Berg 			    bool unhash)
356cb71f1d1SJohannes Berg {
357cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc = NULL;
358c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
359cb71f1d1SJohannes Berg 
360c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
361c71420dbSJohannes Berg 					     lockdep_is_held(&sta->local->sta_mtx));
362c71420dbSJohannes Berg 
363c71420dbSJohannes Berg 	if (WARN_ON(!link_sta))
364cb71f1d1SJohannes Berg 		return;
365246b39e4SJohannes Berg 
366ba6ddab9SJohannes Berg 	if (unhash)
367ba6ddab9SJohannes Berg 		link_sta_info_hash_del(sta->local, link_sta);
368ba6ddab9SJohannes Berg 
369c71420dbSJohannes Berg 	if (link_sta != &sta->deflink)
370c71420dbSJohannes Berg 		alloc = container_of(link_sta, typeof(*alloc), info);
371cb71f1d1SJohannes Berg 
372cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
373c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->link[link_id], NULL);
374c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
375cb71f1d1SJohannes Berg 	if (alloc) {
376cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
377c71420dbSJohannes Berg 		kfree_rcu(alloc, rcu_head);
378246b39e4SJohannes Berg 	}
379*4c51541dSBenjamin Berg 
380*4c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
381246b39e4SJohannes Berg }
382246b39e4SJohannes Berg 
38393e5deb1SJohannes Berg /**
384d9a7ddb0SJohannes Berg  * sta_info_free - free STA
38593e5deb1SJohannes Berg  *
3866ef307bcSRandy Dunlap  * @local: pointer to the global information
38793e5deb1SJohannes Berg  * @sta: STA info to free
38893e5deb1SJohannes Berg  *
38993e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
390d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
391d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
392d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
39393e5deb1SJohannes Berg  */
394d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
39593e5deb1SJohannes Berg {
396cb71f1d1SJohannes Berg 	int i;
397cb71f1d1SJohannes Berg 
398cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
399cb71f1d1SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
400cb71f1d1SJohannes Berg 			continue;
401cb71f1d1SJohannes Berg 
4020ad49045SJohannes Berg 		sta_remove_link(sta, i, false);
403cb71f1d1SJohannes Berg 	}
404cb71f1d1SJohannes Berg 
405dcd479e1SJohannes Berg 	/*
406dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
407dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
408dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
409dcd479e1SJohannes Berg 	 *
410dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
411dcd479e1SJohannes Berg 	 * allocated during state transitions.
412dcd479e1SJohannes Berg 	 */
413dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
414dcd479e1SJohannes Berg 		int ret;
415dcd479e1SJohannes Berg 
416dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
417dcd479e1SJohannes Berg 
418dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
419dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
420dcd479e1SJohannes Berg 			break;
421dcd479e1SJohannes Berg 	}
422dcd479e1SJohannes Berg 
423889cbb91SJohannes Berg 	if (sta->rate_ctrl)
4244b7679a5SJohannes Berg 		rate_control_free_sta(sta);
42593e5deb1SJohannes Berg 
426bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
42793e5deb1SJohannes Berg 
428ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
429ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
43053d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
431433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
432433f5bc1SJohannes Berg 	kfree(sta->mesh);
433433f5bc1SJohannes Berg #endif
434246b39e4SJohannes Berg 
435cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
43693e5deb1SJohannes Berg 	kfree(sta);
43793e5deb1SJohannes Berg }
43893e5deb1SJohannes Berg 
4394d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
44062b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
441d0709a65SJohannes Berg 			     struct sta_info *sta)
442f0706e82SJiri Benc {
44383e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
4447bedd0cfSJohannes Berg 			       sta_rht_params);
445f0706e82SJiri Benc }
446f0706e82SJiri Benc 
4475ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
448af818581SJohannes Berg {
449af818581SJohannes Berg 	struct sta_info *sta;
450af818581SJohannes Berg 
4515ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
452af818581SJohannes Berg 
453af818581SJohannes Berg 	if (sta->dead)
454af818581SJohannes Berg 		return;
455af818581SJohannes Berg 
45654420473SHelmut Schaa 	local_bh_disable();
4575ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
458af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
4595ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
460af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
4615ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
46247086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
463ce662b44SJohannes Berg 	local_bh_enable();
464af818581SJohannes Berg }
465af818581SJohannes Berg 
466af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
467af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
468af65cd96SJohannes Berg {
46930686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
470af65cd96SJohannes Berg 		return 0;
471af65cd96SJohannes Berg 
472889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
473af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
47435c347acSJohannes Berg 						     sta, gfp);
475889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
476af65cd96SJohannes Berg 		return -ENOMEM;
477af65cd96SJohannes Berg 
478af65cd96SJohannes Berg 	return 0;
479af65cd96SJohannes Berg }
480af65cd96SJohannes Berg 
481cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
482246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
483246b39e4SJohannes Berg 			       gfp_t gfp)
484246b39e4SJohannes Berg {
485246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
486246b39e4SJohannes Berg 	int i;
487246b39e4SJohannes Berg 
488246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
489246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
490246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
491246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
492246b39e4SJohannes Berg 			return -ENOMEM;
493246b39e4SJohannes Berg 	}
494246b39e4SJohannes Berg 
495246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
496246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
497246b39e4SJohannes Berg 
498246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
499246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
500246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
501246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
502246b39e4SJohannes Berg 
503246b39e4SJohannes Berg 	return 0;
504246b39e4SJohannes Berg }
505246b39e4SJohannes Berg 
506cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
507cb71f1d1SJohannes Berg 			      unsigned int link_id,
508cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
509cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
510cb71f1d1SJohannes Berg {
511cb71f1d1SJohannes Berg 	link_info->sta = sta;
512cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
513c71420dbSJohannes Berg 	link_info->pub = link_sta;
514c73993b8SJohannes Berg 	link_sta->link_id = link_id;
515c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
516c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
517261ce887SBenjamin Berg 
518261ce887SBenjamin Berg 	link_sta->smps_mode = IEEE80211_SMPS_OFF;
519*4c51541dSBenjamin Berg 	link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
520cb71f1d1SJohannes Berg }
521cb71f1d1SJohannes Berg 
522f36fe0a2SJohannes Berg static struct sta_info *
523f36fe0a2SJohannes Berg __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
524f36fe0a2SJohannes Berg 		 const u8 *addr, int link_id, const u8 *link_addr,
525f36fe0a2SJohannes Berg 		 gfp_t gfp)
526f0706e82SJiri Benc {
527d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
528ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
529f0706e82SJiri Benc 	struct sta_info *sta;
53016c5f15cSRon Rindjunsky 	int i;
531f0706e82SJiri Benc 
532ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
533f0706e82SJiri Benc 	if (!sta)
53473651ee6SJohannes Berg 		return NULL;
535f0706e82SJiri Benc 
536246b39e4SJohannes Berg 	sta->local = local;
537246b39e4SJohannes Berg 	sta->sdata = sdata;
538246b39e4SJohannes Berg 
539cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
54036fe8e4eSLorenzo Bianconi 		goto free;
541c9c5962bSJohannes Berg 
542cb71f1d1SJohannes Berg 	if (link_id >= 0) {
543cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
544cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
545cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
546cb71f1d1SJohannes Berg 	} else {
547cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
548cb71f1d1SJohannes Berg 	}
549cb71f1d1SJohannes Berg 
550*4c51541dSBenjamin Berg 	sta->sta.cur = &sta->sta.deflink.agg;
551*4c51541dSBenjamin Berg 
55207346f81SJohannes Berg 	spin_lock_init(&sta->lock);
5531d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
5545ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
55567c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
556a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
55787f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
558433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
559433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
560433f5bc1SJohannes Berg 		if (!sta->mesh)
561433f5bc1SJohannes Berg 			goto free;
5624c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
563433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
56445d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5654c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5664c02d62fSKees Cook 				    0);
567433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
568433f5bc1SJohannes Berg 	}
56987f59c70SThomas Pedersen #endif
57007346f81SJohannes Berg 
571ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
57217741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
573f36fe0a2SJohannes Berg 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
574f36fe0a2SJohannes Berg 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
575480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
576480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
577480dd46bSMaxim Altshul 
578046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
579046d2e7cSSriram R 
58096fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
58196fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
58296fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
58396fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
58496fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
58596fc6efbSAlexander Wetzel 	 */
58696fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
58796fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
58896fc6efbSAlexander Wetzel 
5890f9c5a61SJohannes Berg 
5903a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5913a11ce08SJohannes Berg 
59271ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
59371ec375cSJohannes Berg 
594b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
595b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
596b6da911bSLiad Kaufman 
59784b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
598541a45a1SBruno Randolf 
599ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
600ba8c3d6fSFelix Fietkau 		void *txq_data;
601ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
602ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
603ba8c3d6fSFelix Fietkau 
604ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
605ba8c3d6fSFelix Fietkau 		if (!txq_data)
606ba8c3d6fSFelix Fietkau 			goto free;
607ba8c3d6fSFelix Fietkau 
608ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
609ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
610ba8c3d6fSFelix Fietkau 
611adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
612fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
613abfbc3afSJohannes Berg 		}
614ba8c3d6fSFelix Fietkau 	}
615ba8c3d6fSFelix Fietkau 
616ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
617ba8c3d6fSFelix Fietkau 		goto free_txq;
618f0706e82SJiri Benc 
619942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
620b4809e94SToke Høiland-Jørgensen 
621948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
622948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
623948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
624942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
625942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
626942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
627942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
628948d887dSJohannes Berg 	}
62973651ee6SJohannes Berg 
6305a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
6314be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
632cccaec98SSenthil Balasubramanian 
633bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
634bd718fc1SJohannes Berg 		u32 mandatory = 0;
635bd718fc1SJohannes Berg 		int r;
636bd718fc1SJohannes Berg 
637bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
638bd718fc1SJohannes Berg 			continue;
639bd718fc1SJohannes Berg 
640bd718fc1SJohannes Berg 		switch (i) {
641bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
64263fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
643bd718fc1SJohannes Berg 			/*
644bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
645bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
646bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
647bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
648bd718fc1SJohannes Berg 			 * possible rate anyway.
649bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
650bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
651bd718fc1SJohannes Berg 			 */
652bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
653bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
654bd718fc1SJohannes Berg 			break;
655bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
656bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
657bd718fc1SJohannes Berg 			break;
658bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
659bd718fc1SJohannes Berg 			WARN_ON(1);
660bd718fc1SJohannes Berg 			mandatory = 0;
661bd718fc1SJohannes Berg 			break;
662bd718fc1SJohannes Berg 		}
663bd718fc1SJohannes Berg 
664bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
665bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
666bd718fc1SJohannes Berg 
667bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
668bd718fc1SJohannes Berg 
669bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
670bd718fc1SJohannes Berg 				continue;
671046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
672bd718fc1SJohannes Berg 		}
673bd718fc1SJohannes Berg 	}
674bd718fc1SJohannes Berg 
675484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
676484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
677484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
678484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
679dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
680dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
681484a54c2SToke Høiland-Jørgensen 
682bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
683ef04a297SJohannes Berg 
684abfbc3afSJohannes Berg 	return sta;
685ba8c3d6fSFelix Fietkau 
686ba8c3d6fSFelix Fietkau free_txq:
687ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
688ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
689ba8c3d6fSFelix Fietkau free:
690cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
691433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
692433f5bc1SJohannes Berg 	kfree(sta->mesh);
693433f5bc1SJohannes Berg #endif
694ba8c3d6fSFelix Fietkau 	kfree(sta);
695ba8c3d6fSFelix Fietkau 	return NULL;
69673651ee6SJohannes Berg }
69773651ee6SJohannes Berg 
698f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
699f36fe0a2SJohannes Berg 				const u8 *addr, gfp_t gfp)
700f36fe0a2SJohannes Berg {
701f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
702f36fe0a2SJohannes Berg }
703f36fe0a2SJohannes Berg 
704f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
705f36fe0a2SJohannes Berg 					  const u8 *mld_addr,
706f36fe0a2SJohannes Berg 					  unsigned int link_id,
707f36fe0a2SJohannes Berg 					  const u8 *link_addr,
708f36fe0a2SJohannes Berg 					  gfp_t gfp)
709f36fe0a2SJohannes Berg {
710f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
711f36fe0a2SJohannes Berg }
712f36fe0a2SJohannes Berg 
7138c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
71434e89507SJohannes Berg {
71534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
71634e89507SJohannes Berg 
71703e4497eSJohannes Berg 	/*
71803e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
71903e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
72003e4497eSJohannes Berg 	 * and another CPU turns off the net device.
72103e4497eSJohannes Berg 	 */
7228c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
7238c71df7aSGuy Eilam 		return -ENETDOWN;
72403e4497eSJohannes Berg 
725b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
726deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
7278c71df7aSGuy Eilam 		return -EINVAL;
7288c71df7aSGuy Eilam 
72983e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
73083e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
73183e7e4ceSHerbert Xu 	 * for correctness.
73231104891SJohannes Berg 	 */
73331104891SJohannes Berg 	rcu_read_lock();
73431104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
73531104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
73631104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
73731104891SJohannes Berg 		rcu_read_unlock();
73831104891SJohannes Berg 		return -ENOTUNIQ;
73931104891SJohannes Berg 	}
74031104891SJohannes Berg 	rcu_read_unlock();
74131104891SJohannes Berg 
7428c71df7aSGuy Eilam 	return 0;
74393e5deb1SJohannes Berg }
74444213b5eSJohannes Berg 
745f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
746f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
747f09603a2SJohannes Berg 				     struct sta_info *sta)
748f09603a2SJohannes Berg {
749f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
750f09603a2SJohannes Berg 	int err = 0;
751f09603a2SJohannes Berg 
752f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
753f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
754f09603a2SJohannes Berg 		if (err)
755f09603a2SJohannes Berg 			break;
756f09603a2SJohannes Berg 	}
757f09603a2SJohannes Berg 
758f09603a2SJohannes Berg 	if (!err) {
759a4ec45a4SJohannes Berg 		/*
760a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
761a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
762a4ec45a4SJohannes Berg 		 */
763a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
764f09603a2SJohannes Berg 			sta->uploaded = true;
765f09603a2SJohannes Berg 		return 0;
766f09603a2SJohannes Berg 	}
767f09603a2SJohannes Berg 
768f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
769bdcbd8e0SJohannes Berg 		sdata_info(sdata,
770bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
771bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
772f09603a2SJohannes Berg 		err = 0;
773f09603a2SJohannes Berg 	}
774f09603a2SJohannes Berg 
775f09603a2SJohannes Berg 	/* unwind on error */
776f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
777f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
778f09603a2SJohannes Berg 
779f09603a2SJohannes Berg 	return err;
780f09603a2SJohannes Berg }
781f09603a2SJohannes Berg 
782d405fd8cSGregory Greenman static void
783d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
784d405fd8cSGregory Greenman {
785d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
786d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
787d405fd8cSGregory Greenman 	struct sta_info *sta;
788d405fd8cSGregory Greenman 
789d405fd8cSGregory Greenman 	rcu_read_lock();
790d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
791d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
792d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
793d405fd8cSGregory Greenman 			continue;
794d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
795d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
796d405fd8cSGregory Greenman 			break;
797d405fd8cSGregory Greenman 		}
798d405fd8cSGregory Greenman 	}
799d405fd8cSGregory Greenman 	rcu_read_unlock();
800d405fd8cSGregory Greenman 
801d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
802d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
803d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
804d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
805d405fd8cSGregory Greenman 	}
806d405fd8cSGregory Greenman }
807d405fd8cSGregory Greenman 
80834e89507SJohannes Berg /*
8098c71df7aSGuy Eilam  * should be called with sta_mtx locked
8108c71df7aSGuy Eilam  * this function replaces the mutex lock
8118c71df7aSGuy Eilam  * with a RCU lock
8128c71df7aSGuy Eilam  */
8134d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
8148c71df7aSGuy Eilam {
8158c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
8168c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8170c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
8188c71df7aSGuy Eilam 	int err = 0;
8198c71df7aSGuy Eilam 
8208c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
8218c71df7aSGuy Eilam 
8227852e361SJohannes Berg 	/* check if STA exists already */
8237852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
8244d33960bSJohannes Berg 		err = -EEXIST;
8258f9dcc29SAhmed Zaki 		goto out_cleanup;
82634e89507SJohannes Berg 	}
82734e89507SJohannes Berg 
8280c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
8290c2e3842SKoen Vandeputte 	if (!sinfo) {
8300c2e3842SKoen Vandeputte 		err = -ENOMEM;
8318f9dcc29SAhmed Zaki 		goto out_cleanup;
8320c2e3842SKoen Vandeputte 	}
8330c2e3842SKoen Vandeputte 
8344d33960bSJohannes Berg 	local->num_sta++;
8354d33960bSJohannes Berg 	local->sta_generation++;
8364d33960bSJohannes Berg 	smp_mb();
8374d33960bSJohannes Berg 
8385108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
8395108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
8405108ca82SJohannes Berg 
8414d33960bSJohannes Berg 	/* make the station visible */
84262b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
84362b14b24SJohannes Berg 	if (err)
84462b14b24SJohannes Berg 		goto out_drop_sta;
8454d33960bSJohannes Berg 
846f36fe0a2SJohannes Berg 	if (sta->sta.valid_links) {
847f36fe0a2SJohannes Berg 		err = link_sta_info_hash_add(local, &sta->deflink);
848f36fe0a2SJohannes Berg 		if (err) {
849f36fe0a2SJohannes Berg 			sta_info_hash_del(local, sta);
850f36fe0a2SJohannes Berg 			goto out_drop_sta;
851f36fe0a2SJohannes Berg 		}
852f36fe0a2SJohannes Berg 	}
853f36fe0a2SJohannes Berg 
8542bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
85583d5cc01SJohannes Berg 
8564dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8574dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8584dde3c36SMordechay Goodstein 	 */
8594dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8600cbf348aSAndrei Otcheretianski 		ieee80211_recalc_min_chandef(sta->sdata, -1);
8614dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8624dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8634dde3c36SMordechay Goodstein 	}
8644dde3c36SMordechay Goodstein 
8655108ca82SJohannes Berg 	/* notify driver */
8665108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8675108ca82SJohannes Berg 	if (err)
8685108ca82SJohannes Berg 		goto out_remove;
8695108ca82SJohannes Berg 
87083d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
871d405fd8cSGregory Greenman 
8725108ca82SJohannes Berg 	/* accept BA sessions now */
8735108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8744d33960bSJohannes Berg 
8754d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8764d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
8774d33960bSJohannes Berg 
8780ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
8790ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
8800ef049dcSArnd Bergmann 	kfree(sinfo);
881d0709a65SJohannes Berg 
882bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
883f0706e82SJiri Benc 
88434e89507SJohannes Berg 	/* move reference to rcu-protected */
88534e89507SJohannes Berg 	rcu_read_lock();
88634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
887e9f207f0SJiri Benc 
88873651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
88973651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
89073651ee6SJohannes Berg 
89173651ee6SJohannes Berg 	return 0;
8925108ca82SJohannes Berg  out_remove:
8930ad49045SJohannes Berg 	if (sta->sta.valid_links)
8940ad49045SJohannes Berg 		link_sta_info_hash_del(local, &sta->deflink);
8955108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
8965108ca82SJohannes Berg 	list_del_rcu(&sta->list);
89762b14b24SJohannes Berg  out_drop_sta:
8985108ca82SJohannes Berg 	local->num_sta--;
8995108ca82SJohannes Berg 	synchronize_net();
9008f9dcc29SAhmed Zaki  out_cleanup:
9017bc40aedSJohannes Berg 	cleanup_single_sta(sta);
9024d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
903ea32f065SSudip Mukherjee 	kfree(sinfo);
9044d33960bSJohannes Berg 	rcu_read_lock();
9054d33960bSJohannes Berg 	return err;
9068c71df7aSGuy Eilam }
9078c71df7aSGuy Eilam 
9088c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
9098c71df7aSGuy Eilam {
9108c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
911308f7fcfSZhao, Gang 	int err;
9128c71df7aSGuy Eilam 
9134d33960bSJohannes Berg 	might_sleep();
9144d33960bSJohannes Berg 
91531104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
91631104891SJohannes Berg 
9178c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
9188c71df7aSGuy Eilam 	if (err) {
9197bc40aedSJohannes Berg 		sta_info_free(local, sta);
92031104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
9218c71df7aSGuy Eilam 		rcu_read_lock();
9227bc40aedSJohannes Berg 		return err;
9238c71df7aSGuy Eilam 	}
9248c71df7aSGuy Eilam 
9257bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
926f0706e82SJiri Benc }
927f0706e82SJiri Benc 
92834e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
92934e89507SJohannes Berg {
93034e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
93134e89507SJohannes Berg 
93234e89507SJohannes Berg 	rcu_read_unlock();
93334e89507SJohannes Berg 
93434e89507SJohannes Berg 	return err;
93534e89507SJohannes Berg }
93634e89507SJohannes Berg 
937d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
938004c872eSJohannes Berg {
939004c872eSJohannes Berg 	/*
940004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
941004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
942004c872eSJohannes Berg 	 */
943d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
944004c872eSJohannes Berg }
945004c872eSJohannes Berg 
946d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
947004c872eSJohannes Berg {
948004c872eSJohannes Berg 	/*
949004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
950004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
951004c872eSJohannes Berg 	 */
952d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
953004c872eSJohannes Berg }
954004c872eSJohannes Berg 
9553d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9563d5839b6SIlan Peer {
9573d5839b6SIlan Peer 	/*
9583d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9593d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9603d5839b6SIlan Peer 	 */
9613d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9623d5839b6SIlan Peer }
9633d5839b6SIlan Peer 
964948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
965004c872eSJohannes Berg {
966948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
967948d887dSJohannes Berg 	switch (ac) {
968948d887dSJohannes Berg 	case IEEE80211_AC_VO:
969948d887dSJohannes Berg 		return BIT(6) | BIT(7);
970948d887dSJohannes Berg 	case IEEE80211_AC_VI:
971948d887dSJohannes Berg 		return BIT(4) | BIT(5);
972948d887dSJohannes Berg 	case IEEE80211_AC_BE:
973948d887dSJohannes Berg 		return BIT(0) | BIT(3);
974948d887dSJohannes Berg 	case IEEE80211_AC_BK:
975948d887dSJohannes Berg 		return BIT(1) | BIT(2);
976948d887dSJohannes Berg 	default:
977948d887dSJohannes Berg 		WARN_ON(1);
978948d887dSJohannes Berg 		return 0;
979d0709a65SJohannes Berg 	}
980004c872eSJohannes Berg }
981004c872eSJohannes Berg 
9829b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
983004c872eSJohannes Berg {
984c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
985d012a605SMarco Porsch 	struct ps_data *ps;
986948d887dSJohannes Berg 	bool indicate_tim = false;
987948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
988948d887dSJohannes Berg 	int ac;
989a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
990004c872eSJohannes Berg 
991d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
992d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
993c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
994c868cb35SJohannes Berg 			return;
9953e122be0SJohannes Berg 
996d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
9973f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
9983f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
9993f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
10003f52b7e3SMarco Porsch #endif
1001d012a605SMarco Porsch 	} else {
1002d012a605SMarco Porsch 		return;
1003d012a605SMarco Porsch 	}
1004d012a605SMarco Porsch 
1005c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
1006d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1007c868cb35SJohannes Berg 		return;
1008004c872eSJohannes Berg 
1009c868cb35SJohannes Berg 	if (sta->dead)
1010c868cb35SJohannes Berg 		goto done;
10113e122be0SJohannes Berg 
1012948d887dSJohannes Berg 	/*
1013948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
1014948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
1015948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
1016948d887dSJohannes Berg 	 * non-enabled ones.
1017948d887dSJohannes Berg 	 */
1018948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1019948d887dSJohannes Berg 		ignore_for_tim = 0;
1020948d887dSJohannes Berg 
10219b7a86f3SJohannes Berg 	if (ignore_pending)
10229b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
10239b7a86f3SJohannes Berg 
1024948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1025948d887dSJohannes Berg 		unsigned long tids;
1026948d887dSJohannes Berg 
1027f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1028948d887dSJohannes Berg 			continue;
1029948d887dSJohannes Berg 
1030948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1031948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
1032948d887dSJohannes Berg 		if (indicate_tim)
1033948d887dSJohannes Berg 			break;
1034948d887dSJohannes Berg 
1035948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
1036948d887dSJohannes Berg 
1037948d887dSJohannes Berg 		indicate_tim |=
1038948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
1039ba8c3d6fSFelix Fietkau 		indicate_tim |=
1040ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
1041004c872eSJohannes Berg 	}
1042004c872eSJohannes Berg 
1043c868cb35SJohannes Berg  done:
104465f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
1045004c872eSJohannes Berg 
10463d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
10473d5839b6SIlan Peer 		goto out_unlock;
10483d5839b6SIlan Peer 
1049948d887dSJohannes Berg 	if (indicate_tim)
1050d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1051c868cb35SJohannes Berg 	else
1052d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10533e122be0SJohannes Berg 
10549b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1055c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1056948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1057c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1058004c872eSJohannes Berg 	}
1059004c872eSJohannes Berg 
10603d5839b6SIlan Peer out_unlock:
106165f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1062004c872eSJohannes Berg }
1063004c872eSJohannes Berg 
10649b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10659b7a86f3SJohannes Berg {
10669b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10679b7a86f3SJohannes Berg }
10689b7a86f3SJohannes Berg 
1069cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1070f0706e82SJiri Benc {
1071e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1072f0706e82SJiri Benc 	int timeout;
1073f0706e82SJiri Benc 
1074f0706e82SJiri Benc 	if (!skb)
1075cd0b8d89SJohannes Berg 		return false;
1076f0706e82SJiri Benc 
1077e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1078f0706e82SJiri Benc 
1079f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
108057c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
108157c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
108257c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1083f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1084f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1085e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1086f0706e82SJiri Benc }
1087f0706e82SJiri Benc 
1088f0706e82SJiri Benc 
1089948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1090948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1091f0706e82SJiri Benc {
1092f0706e82SJiri Benc 	unsigned long flags;
1093f0706e82SJiri Benc 	struct sk_buff *skb;
1094f0706e82SJiri Benc 
109560750397SJohannes Berg 	/*
109660750397SJohannes Berg 	 * First check for frames that should expire on the filtered
109760750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
109860750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
109960750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
110060750397SJohannes Berg 	 * total_ps_buffered counter.
110160750397SJohannes Berg 	 */
1102f0706e82SJiri Benc 	for (;;) {
1103948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1104948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
110557c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1106948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1107836341a7SJohannes Berg 		else
1108f0706e82SJiri Benc 			skb = NULL;
1109948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1110f0706e82SJiri Benc 
111160750397SJohannes Berg 		/*
111260750397SJohannes Berg 		 * Frames are queued in order, so if this one
111360750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
111460750397SJohannes Berg 		 * we actually reached the end of the queue we
111560750397SJohannes Berg 		 * also need to stop, of course.
111660750397SJohannes Berg 		 */
111760750397SJohannes Berg 		if (!skb)
111860750397SJohannes Berg 			break;
1119d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
112060750397SJohannes Berg 	}
112160750397SJohannes Berg 
112260750397SJohannes Berg 	/*
112360750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
112460750397SJohannes Berg 	 * only find something if the filtered queue was emptied
112560750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
112660750397SJohannes Berg 	 * buffered frames.
112760750397SJohannes Berg 	 */
1128f0706e82SJiri Benc 	for (;;) {
1129948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1130948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1131f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1132948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1133f0706e82SJiri Benc 		else
1134f0706e82SJiri Benc 			skb = NULL;
1135948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1136f0706e82SJiri Benc 
113760750397SJohannes Berg 		/*
113860750397SJohannes Berg 		 * frames are queued in order, so if this one
113960750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
114060750397SJohannes Berg 		 * the queue) we can stop testing
114160750397SJohannes Berg 		 */
1142836341a7SJohannes Berg 		if (!skb)
1143836341a7SJohannes Berg 			break;
1144836341a7SJohannes Berg 
1145f0706e82SJiri Benc 		local->total_ps_buffered--;
1146bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1147bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1148d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1149f0706e82SJiri Benc 	}
11503393a608SJuuso Oikarinen 
115160750397SJohannes Berg 	/*
115260750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
115360750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
115460750397SJohannes Berg 	 * frames.
115560750397SJohannes Berg 	 */
115660750397SJohannes Berg 	sta_info_recalc_tim(sta);
115760750397SJohannes Berg 
115860750397SJohannes Berg 	/*
115960750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
116060750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
116160750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
116260750397SJohannes Berg 	 */
1163948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1164948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1165948d887dSJohannes Berg }
1166948d887dSJohannes Berg 
1167948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1168948d887dSJohannes Berg 					     struct sta_info *sta)
1169948d887dSJohannes Berg {
1170948d887dSJohannes Berg 	bool have_buffered = false;
1171948d887dSJohannes Berg 	int ac;
1172948d887dSJohannes Berg 
11733f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11743f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11753f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1176948d887dSJohannes Berg 		return false;
1177948d887dSJohannes Berg 
1178948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1179948d887dSJohannes Berg 		have_buffered |=
1180948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1181948d887dSJohannes Berg 
1182948d887dSJohannes Berg 	return have_buffered;
1183f0706e82SJiri Benc }
1184f0706e82SJiri Benc 
1185d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
118634e89507SJohannes Berg {
118734e89507SJohannes Berg 	struct ieee80211_local *local;
118834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
11890ad49045SJohannes Berg 	int ret, i;
119034e89507SJohannes Berg 
119134e89507SJohannes Berg 	might_sleep();
119234e89507SJohannes Berg 
119334e89507SJohannes Berg 	if (!sta)
119434e89507SJohannes Berg 		return -ENOENT;
119534e89507SJohannes Berg 
119634e89507SJohannes Berg 	local = sta->local;
119734e89507SJohannes Berg 	sdata = sta->sdata;
119834e89507SJohannes Berg 
119983d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
120083d5cc01SJohannes Berg 
1201098a6070SJohannes Berg 	/*
1202098a6070SJohannes Berg 	 * Before removing the station from the driver and
1203098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1204098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1205098a6070SJohannes Berg 	 * will be sufficient.
1206098a6070SJohannes Berg 	 */
1207c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1208c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1209098a6070SJohannes Berg 
1210f59374ebSSara Sharon 	/*
1211f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1212f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1213f59374ebSSara Sharon 	 * all such frames to be processed.
1214f59374ebSSara Sharon 	 */
1215f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1216f59374ebSSara Sharon 
12170ad49045SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
12180ad49045SJohannes Berg 		struct link_sta_info *link_sta;
12190ad49045SJohannes Berg 
12200ad49045SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
12210ad49045SJohannes Berg 			continue;
12220ad49045SJohannes Berg 
12230ad49045SJohannes Berg 		link_sta = rcu_dereference_protected(sta->link[i],
12240ad49045SJohannes Berg 						     lockdep_is_held(&local->sta_mtx));
12250ad49045SJohannes Berg 
12260ad49045SJohannes Berg 		link_sta_info_hash_del(local, link_sta);
12270ad49045SJohannes Berg 	}
12280ad49045SJohannes Berg 
122934e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1230b01711beSJohannes Berg 	if (WARN_ON(ret))
123134e89507SJohannes Berg 		return ret;
123234e89507SJohannes Berg 
1233a7a6bdd0SArik Nemtsov 	/*
1234a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1235a7a6bdd0SArik Nemtsov 	 * removal.
1236a7a6bdd0SArik Nemtsov 	 */
1237a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1238a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1239a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1240a7a6bdd0SArik Nemtsov 	}
1241a7a6bdd0SArik Nemtsov 
1242794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1243ef044763SEliad Peller 	sta->removed = true;
12444d33960bSJohannes Berg 
12456a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
12466a9d1b91SJohannes Berg 
1247a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1248a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1249a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1250a710c816SJohannes Berg 
1251d778207bSJohannes Berg 	return 0;
1252d778207bSJohannes Berg }
1253d778207bSJohannes Berg 
1254d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1255d778207bSJohannes Berg {
1256d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1257d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
12580ef049dcSArnd Bergmann 	struct station_info *sinfo;
1259d778207bSJohannes Berg 	int ret;
1260d778207bSJohannes Berg 
1261d778207bSJohannes Berg 	/*
1262d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1263d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1264d778207bSJohannes Berg 	 */
1265d778207bSJohannes Berg 
1266d778207bSJohannes Berg 	might_sleep();
1267d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1268d778207bSJohannes Berg 
12695981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1270b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1271b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1272b16798f5SJohannes Berg 	}
1273b16798f5SJohannes Berg 
1274c8782078SJohannes Berg 	/* now keys can no longer be reached */
12756d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
127634e89507SJohannes Berg 
12779b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
12789b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
12799b7a86f3SJohannes Berg 
128034e89507SJohannes Berg 	sta->dead = true;
128134e89507SJohannes Berg 
128234e89507SJohannes Berg 	local->num_sta--;
128334e89507SJohannes Berg 	local->sta_generation++;
128434e89507SJohannes Berg 
128583d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1286f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1287f09603a2SJohannes Berg 		if (ret) {
128883d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
128983d5cc01SJohannes Berg 			break;
129083d5cc01SJohannes Berg 		}
129183d5cc01SJohannes Berg 	}
1292d9a7ddb0SJohannes Berg 
1293f09603a2SJohannes Berg 	if (sta->uploaded) {
1294f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1295f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1296f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1297f09603a2SJohannes Berg 	}
129834e89507SJohannes Berg 
1299bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1300bdcbd8e0SJohannes Berg 
13010ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
13020ef049dcSArnd Bergmann 	if (sinfo)
13030fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
13040ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
13050ef049dcSArnd Bergmann 	kfree(sinfo);
1306ec15e68bSJouni Malinen 
130734e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
130834e89507SJohannes Berg 
13093a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
13103a11ce08SJohannes Berg 
1311d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1312d778207bSJohannes Berg }
1313d778207bSJohannes Berg 
1314d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1315d778207bSJohannes Berg {
1316d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1317d778207bSJohannes Berg 
1318d778207bSJohannes Berg 	if (err)
1319d778207bSJohannes Berg 		return err;
1320d778207bSJohannes Berg 
1321d778207bSJohannes Berg 	synchronize_net();
1322d778207bSJohannes Berg 
1323d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
132434e89507SJohannes Berg 
132534e89507SJohannes Berg 	return 0;
132634e89507SJohannes Berg }
132734e89507SJohannes Berg 
132834e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
132934e89507SJohannes Berg {
133034e89507SJohannes Berg 	struct sta_info *sta;
133134e89507SJohannes Berg 	int ret;
133234e89507SJohannes Berg 
133334e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
13347852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
133534e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
133634e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
133734e89507SJohannes Berg 
133834e89507SJohannes Berg 	return ret;
133934e89507SJohannes Berg }
134034e89507SJohannes Berg 
134134e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
134234e89507SJohannes Berg 			      const u8 *addr)
134334e89507SJohannes Berg {
134434e89507SJohannes Berg 	struct sta_info *sta;
134534e89507SJohannes Berg 	int ret;
134634e89507SJohannes Berg 
134734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
13487852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
134934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
135034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
135134e89507SJohannes Berg 
135234e89507SJohannes Berg 	return ret;
135334e89507SJohannes Berg }
1354f0706e82SJiri Benc 
135534f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1356f0706e82SJiri Benc {
135734f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1358f0706e82SJiri Benc 	struct sta_info *sta;
13593393a608SJuuso Oikarinen 	bool timer_needed = false;
1360f0706e82SJiri Benc 
1361d0709a65SJohannes Berg 	rcu_read_lock();
1362d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
13633393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
13643393a608SJuuso Oikarinen 			timer_needed = true;
1365d0709a65SJohannes Berg 	rcu_read_unlock();
1366f0706e82SJiri Benc 
13675bb644a0SJohannes Berg 	if (local->quiescing)
13685bb644a0SJohannes Berg 		return;
13695bb644a0SJohannes Berg 
13703393a608SJuuso Oikarinen 	if (!timer_needed)
13713393a608SJuuso Oikarinen 		return;
13723393a608SJuuso Oikarinen 
137326d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
137426d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1375f0706e82SJiri Benc }
1376f0706e82SJiri Benc 
13777bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
13787bedd0cfSJohannes Berg {
13797bedd0cfSJohannes Berg 	int err;
13807bedd0cfSJohannes Berg 
138183e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
13827bedd0cfSJohannes Berg 	if (err)
13837bedd0cfSJohannes Berg 		return err;
13847bedd0cfSJohannes Berg 
1385ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1386ba6ddab9SJohannes Berg 	if (err) {
1387ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1388ba6ddab9SJohannes Berg 		return err;
1389ba6ddab9SJohannes Berg 	}
1390ba6ddab9SJohannes Berg 
13914d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
139234e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1393f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1394f0706e82SJiri Benc 
139534f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
13967bedd0cfSJohannes Berg 	return 0;
1397f0706e82SJiri Benc }
1398f0706e82SJiri Benc 
1399f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1400f0706e82SJiri Benc {
1401a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
140283e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1403ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1404f0706e82SJiri Benc }
1405f0706e82SJiri Benc 
1406051007d9SJohannes Berg 
1407e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1408f0706e82SJiri Benc {
1409b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1410f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1411d778207bSJohannes Berg 	LIST_HEAD(free_list);
141244213b5eSJohannes Berg 	int ret = 0;
1413f0706e82SJiri Benc 
1414d0709a65SJohannes Berg 	might_sleep();
1415d0709a65SJohannes Berg 
1416e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1417e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1418e716251dSJohannes Berg 
141934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
142034e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1421e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1422e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1423d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1424d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
142534316837SJohannes Berg 			ret++;
142634316837SJohannes Berg 		}
142734e89507SJohannes Berg 	}
1428d778207bSJohannes Berg 
1429d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1430d778207bSJohannes Berg 		synchronize_net();
1431d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1432d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1433d778207bSJohannes Berg 	}
143434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
143544213b5eSJohannes Berg 
1436051007d9SJohannes Berg 	return ret;
1437051007d9SJohannes Berg }
1438051007d9SJohannes Berg 
143924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
144024723d1bSJohannes Berg 			  unsigned long exp_time)
144124723d1bSJohannes Berg {
144224723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
144324723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
144424723d1bSJohannes Berg 
144534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1446e46a2cf9SMohammed Shafi Shajakhan 
1447e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1448b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1449b8da6b6aSJohannes Berg 
1450ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1451ec2b774eSMarek Lindner 			continue;
1452ec2b774eSMarek Lindner 
1453b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1454eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1455bdcbd8e0SJohannes Berg 				sta->sta.addr);
14563f52b7e3SMarco Porsch 
14573f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
14583f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
14593f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
14603f52b7e3SMarco Porsch 
146134e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
146224723d1bSJohannes Berg 		}
1463e46a2cf9SMohammed Shafi Shajakhan 	}
1464e46a2cf9SMohammed Shafi Shajakhan 
146534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
146624723d1bSJohannes Berg }
146717741cdcSJohannes Berg 
1468686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1469686b9cb9SBen Greear 						   const u8 *addr,
1470686b9cb9SBen Greear 						   const u8 *localaddr)
147117741cdcSJohannes Berg {
14727bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
147383e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
14747bedd0cfSJohannes Berg 	struct sta_info *sta;
147517741cdcSJohannes Berg 
1476686b9cb9SBen Greear 	/*
1477686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1478686b9cb9SBen Greear 	 * ... first in list.
1479686b9cb9SBen Greear 	 */
148083e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1481686b9cb9SBen Greear 		if (localaddr &&
1482b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1483686b9cb9SBen Greear 			continue;
1484f7c65594SJohannes Berg 		if (!sta->uploaded)
1485f7c65594SJohannes Berg 			return NULL;
148617741cdcSJohannes Berg 		return &sta->sta;
1487f7c65594SJohannes Berg 	}
1488f7c65594SJohannes Berg 
1489abe60632SJohannes Berg 	return NULL;
149017741cdcSJohannes Berg }
1491686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
14925ed176e1SJohannes Berg 
14935ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
14945ed176e1SJohannes Berg 					 const u8 *addr)
14955ed176e1SJohannes Berg {
1496f7c65594SJohannes Berg 	struct sta_info *sta;
14975ed176e1SJohannes Berg 
14985ed176e1SJohannes Berg 	if (!vif)
14995ed176e1SJohannes Berg 		return NULL;
15005ed176e1SJohannes Berg 
1501f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1502f7c65594SJohannes Berg 	if (!sta)
1503f7c65594SJohannes Berg 		return NULL;
15045ed176e1SJohannes Berg 
1505f7c65594SJohannes Berg 	if (!sta->uploaded)
1506f7c65594SJohannes Berg 		return NULL;
1507f7c65594SJohannes Berg 
1508f7c65594SJohannes Berg 	return &sta->sta;
15095ed176e1SJohannes Berg }
151017741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1511af818581SJohannes Berg 
1512e3685e03SJohannes Berg /* powersave support code */
1513e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
151450a9432dSJohannes Berg {
1515608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1516e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1517e3685e03SJohannes Berg 	struct sk_buff_head pending;
1518ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1519e3685e03SJohannes Berg 	unsigned long flags;
1520d012a605SMarco Porsch 	struct ps_data *ps;
1521d012a605SMarco Porsch 
15223918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
15233918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
15243918edb0SFelix Fietkau 				     u.ap);
15253918edb0SFelix Fietkau 
15263918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1527d012a605SMarco Porsch 		ps = &sdata->bss->ps;
15283f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
15293f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1530d012a605SMarco Porsch 	else
1531d012a605SMarco Porsch 		return;
153250a9432dSJohannes Berg 
1533c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
153447086fc5SJohannes Berg 
15355a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1536948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1537ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1538948d887dSJohannes Berg 
153930686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
154012375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1541af818581SJohannes Berg 
1542ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1543adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1544ba8c3d6fSFelix Fietkau 			continue;
1545ba8c3d6fSFelix Fietkau 
154618667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1547ba8c3d6fSFelix Fietkau 	}
1548ba8c3d6fSFelix Fietkau 
1549948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1550af818581SJohannes Berg 
15511d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
15521d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1553af818581SJohannes Berg 	/* Send all buffered frames to the station */
1554948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1555948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1556948d887dSJohannes Berg 
1557987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1558948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1559987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1560948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1561948d887dSJohannes Berg 		filtered += tmp - count;
1562948d887dSJohannes Berg 		count = tmp;
1563948d887dSJohannes Berg 
1564987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1565948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1566987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1567948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1568948d887dSJohannes Berg 		buffered += tmp - count;
1569948d887dSJohannes Berg 	}
1570948d887dSJohannes Berg 
1571e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
15725ac2e350SJohannes Berg 
15735ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
15745ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
15755ac2e350SJohannes Berg 
15765ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
15775ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
15785ac2e350SJohannes Berg 	 */
15795ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
15805ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
15811d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1582948d887dSJohannes Berg 
1583e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1584e3685e03SJohannes Berg 
1585af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1586af818581SJohannes Berg 
1587c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1588c868cb35SJohannes Berg 
1589bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
15902595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1591bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
159217c18bf8SJohannes Berg 
159317c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1594af818581SJohannes Berg }
1595af818581SJohannes Berg 
15960ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1597b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
15980ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1599ce662b44SJohannes Berg {
16000ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1601ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1602ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1603ce662b44SJohannes Berg 	struct sk_buff *skb;
1604ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1605ce662b44SJohannes Berg 	__le16 fc;
1606a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1607ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
160855de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1609ce662b44SJohannes Berg 
1610ce662b44SJohannes Berg 	if (qos) {
1611ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1612ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1613ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1614ce662b44SJohannes Berg 	} else {
1615ce662b44SJohannes Berg 		size -= 2;
1616ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1617ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1618ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1619ce662b44SJohannes Berg 	}
1620ce662b44SJohannes Berg 
1621ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1622ce662b44SJohannes Berg 	if (!skb)
1623ce662b44SJohannes Berg 		return;
1624ce662b44SJohannes Berg 
1625ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1626ce662b44SJohannes Berg 
16274df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1628ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1629ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1630ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1631ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1632ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1633864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1634ce662b44SJohannes Berg 
1635ce662b44SJohannes Berg 	skb->priority = tid;
1636ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
163759b66255SJohannes Berg 	if (qos) {
1638ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1639ce662b44SJohannes Berg 
16400ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1641ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1642ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
16430ead2510SEmmanuel Grumbach 			if (more_data)
16440ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
16450ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
16460ead2510SEmmanuel Grumbach 		}
1647ce662b44SJohannes Berg 	}
1648ce662b44SJohannes Berg 
1649ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1650ce662b44SJohannes Berg 
1651ce662b44SJohannes Berg 	/*
1652ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1653ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1654deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1655deeaee19SJohannes Berg 	 * ends the poll/service period.
1656ce662b44SJohannes Berg 	 */
165702f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1658deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1659ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1660ce662b44SJohannes Berg 
16616b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
16626b127c71SSujith Manoharan 
1663b77cf4f8SJohannes Berg 	if (call_driver)
1664b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1665b77cf4f8SJohannes Berg 					  reason, false);
166640b96408SJohannes Berg 
166789afe614SJohannes Berg 	skb->dev = sdata->dev;
166889afe614SJohannes Berg 
166955de908aSJohannes Berg 	rcu_read_lock();
1670d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
167155de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
167255de908aSJohannes Berg 		rcu_read_unlock();
167355de908aSJohannes Berg 		kfree_skb(skb);
167455de908aSJohannes Berg 		return;
167555de908aSJohannes Berg 	}
167655de908aSJohannes Berg 
167773c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
167808aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
167955de908aSJohannes Berg 	rcu_read_unlock();
1680ce662b44SJohannes Berg }
1681ce662b44SJohannes Berg 
16820a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
16830a1cb809SJohannes Berg {
16840a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
16850a1cb809SJohannes Berg 	if (tids & 0xF8)
16860a1cb809SJohannes Berg 		return fls(tids) - 1;
16870a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
16880a1cb809SJohannes Berg 	if (tids & BIT(0))
16890a1cb809SJohannes Berg 		return 0;
16900a1cb809SJohannes Berg 	return fls(tids) - 1;
16910a1cb809SJohannes Berg }
16920a1cb809SJohannes Berg 
16930ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
16940ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
16950ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
16960ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
16970ead2510SEmmanuel Grumbach  */
16980ead2510SEmmanuel Grumbach static bool
16990ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
17000ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
17010ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
17020ead2510SEmmanuel Grumbach {
17030ead2510SEmmanuel Grumbach 	int ac;
17040ead2510SEmmanuel Grumbach 
17050ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
17060ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
17070ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
17080ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
17090ead2510SEmmanuel Grumbach 	 */
17100ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
17110ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
17120ead2510SEmmanuel Grumbach 		return true;
17130ead2510SEmmanuel Grumbach 
17140ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1715f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
17160ead2510SEmmanuel Grumbach 			continue;
17170ead2510SEmmanuel Grumbach 
17180ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17190ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
17200ead2510SEmmanuel Grumbach 			return true;
17210ead2510SEmmanuel Grumbach 	}
17220ead2510SEmmanuel Grumbach 
17230ead2510SEmmanuel Grumbach 	return false;
17240ead2510SEmmanuel Grumbach }
17250ead2510SEmmanuel Grumbach 
172647086fc5SJohannes Berg static void
17270ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
17280ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
17290ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
17300ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1731af818581SJohannes Berg {
1732af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1733af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1734948d887dSJohannes Berg 	int ac;
1735af818581SJohannes Berg 
1736f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1737948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
17384049e09aSJohannes Berg 		unsigned long tids;
17394049e09aSJohannes Berg 
1740f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1741948d887dSJohannes Berg 			continue;
1742948d887dSJohannes Berg 
17434049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
17444049e09aSJohannes Berg 
1745f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1746f9f760b4SJohannes Berg 		 * release from hardware queues
1747f9f760b4SJohannes Berg 		 */
17480ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
17490ead2510SEmmanuel Grumbach 			*driver_release_tids |=
17500ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
17510ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1752ba8c3d6fSFelix Fietkau 		}
1753f9f760b4SJohannes Berg 
17540ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
175547086fc5SJohannes Berg 			struct sk_buff *skb;
175647086fc5SJohannes Berg 
175747086fc5SJohannes Berg 			while (n_frames > 0) {
1758948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1759948d887dSJohannes Berg 				if (!skb) {
176047086fc5SJohannes Berg 					skb = skb_dequeue(
176147086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1762af818581SJohannes Berg 					if (skb)
1763af818581SJohannes Berg 						local->total_ps_buffered--;
1764af818581SJohannes Berg 				}
176547086fc5SJohannes Berg 				if (!skb)
176647086fc5SJohannes Berg 					break;
176747086fc5SJohannes Berg 				n_frames--;
17680ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
176947086fc5SJohannes Berg 			}
1770948d887dSJohannes Berg 		}
1771948d887dSJohannes Berg 
17720ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
17730ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
17740ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
17754049e09aSJohannes Berg 		 */
1776948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17770ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1778948d887dSJohannes Berg 			break;
1779948d887dSJohannes Berg 	}
1780948d887dSJohannes Berg }
1781af818581SJohannes Berg 
17820ead2510SEmmanuel Grumbach static void
17830ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
17840ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
17850ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
17860ead2510SEmmanuel Grumbach {
17870ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
17880ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
17890ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
17900ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
17910ead2510SEmmanuel Grumbach 	bool more_data;
17920ead2510SEmmanuel Grumbach 
17930ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
17940ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
17950ead2510SEmmanuel Grumbach 
17960ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
17970ead2510SEmmanuel Grumbach 
17980ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
17990ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
18000ead2510SEmmanuel Grumbach 
18010ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
18020ead2510SEmmanuel Grumbach 
18031a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
18040ead2510SEmmanuel Grumbach 		driver_release_tids =
18050ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
18060ead2510SEmmanuel Grumbach 
1807f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1808f438ceb8SEmmanuel Grumbach 		int tid, ac;
18094049e09aSJohannes Berg 
1810ce662b44SJohannes Berg 		/*
1811ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1812ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1813ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1814ce662b44SJohannes Berg 		 *
1815ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1816ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1817ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1818ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1819ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1820ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1821ce662b44SJohannes Berg 		 *
1822ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1823ce662b44SJohannes Berg 		 */
1824ce662b44SJohannes Berg 
1825ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1826f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1827d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1828d7f84244SEmmanuel Grumbach 				break;
1829f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1830ce662b44SJohannes Berg 
18310ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1832f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
183347086fc5SJohannes Berg 		struct sk_buff_head pending;
183447086fc5SJohannes Berg 		struct sk_buff *skb;
183540b96408SJohannes Berg 		int num = 0;
183640b96408SJohannes Berg 		u16 tids = 0;
1837b77cf4f8SJohannes Berg 		bool need_null = false;
183847086fc5SJohannes Berg 
183947086fc5SJohannes Berg 		skb_queue_head_init(&pending);
184047086fc5SJohannes Berg 
184147086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1842af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
184347086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
184440b96408SJohannes Berg 			u8 *qoshdr = NULL;
184540b96408SJohannes Berg 
184640b96408SJohannes Berg 			num++;
1847af818581SJohannes Berg 
1848af818581SJohannes Berg 			/*
184947086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
185047086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
185147086fc5SJohannes Berg 			 * exchange.
1852af818581SJohannes Berg 			 */
18536b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
18546b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1855af818581SJohannes Berg 
185647086fc5SJohannes Berg 			/*
185747086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
185847086fc5SJohannes Berg 			 * more buffered frames for this STA
185947086fc5SJohannes Berg 			 */
186024b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
186147086fc5SJohannes Berg 				hdr->frame_control |=
186247086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
186324b9c373SJanusz.Dziedzic@tieto.com 			else
186424b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
186524b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1866af818581SJohannes Berg 
186740b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
186840b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
186940b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
187040b96408SJohannes Berg 
1871b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1872b77cf4f8SJohannes Berg 
1873b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1874b77cf4f8SJohannes Berg 
1875b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1876b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1877b77cf4f8SJohannes Berg 				continue;
1878b77cf4f8SJohannes Berg 
1879b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1880b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1881b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1882b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1883b77cf4f8SJohannes Berg 				break;
1884b77cf4f8SJohannes Berg 			}
1885b77cf4f8SJohannes Berg 
1886b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1887b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1888b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1889b77cf4f8SJohannes Berg 			 * and be done.
1890b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1891b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1892b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1893b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1894b77cf4f8SJohannes Berg 			 *
1895b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1896b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1897b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1898b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1899b77cf4f8SJohannes Berg 			 *
1900b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1901b77cf4f8SJohannes Berg 			 */
1902b77cf4f8SJohannes Berg 			if (qoshdr) {
190340b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1904deeaee19SJohannes Berg 
190547086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
190647086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1907b77cf4f8SJohannes Berg 			} else {
1908b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1909b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1910b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1911b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1912b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1913b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1914b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1915b77cf4f8SJohannes Berg 				 */
1916b77cf4f8SJohannes Berg 				hdr->frame_control |=
1917b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1918b77cf4f8SJohannes Berg 				need_null = true;
1919b77cf4f8SJohannes Berg 				num++;
192052a3f20cSMarco Porsch 			}
1921b77cf4f8SJohannes Berg 			break;
192247086fc5SJohannes Berg 		}
192347086fc5SJohannes Berg 
192440b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
192540b96408SJohannes Berg 					  reason, more_data);
192640b96408SJohannes Berg 
192747086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1928af818581SJohannes Berg 
1929b77cf4f8SJohannes Berg 		if (need_null)
1930b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
19310ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
19320ead2510SEmmanuel Grumbach 				reason, false, false);
1933b77cf4f8SJohannes Berg 
1934c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1935af818581SJohannes Berg 	} else {
1936ba8c3d6fSFelix Fietkau 		int tid;
1937ba8c3d6fSFelix Fietkau 
1938af818581SJohannes Berg 		/*
19394049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
19404049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1941f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1942f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1943f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1944f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1945f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1946f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1947af818581SJohannes Berg 		 */
19484049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
194947086fc5SJohannes Berg 					    n_frames, reason, more_data);
19504049e09aSJohannes Berg 
19514049e09aSJohannes Berg 		/*
19524049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
19534049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1954f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
19554049e09aSJohannes Berg 		 * release function.
1956f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1957ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1958ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
19594049e09aSJohannes Berg 		 */
1960ba8c3d6fSFelix Fietkau 
1961ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1962ba8c3d6fSFelix Fietkau 			return;
1963ba8c3d6fSFelix Fietkau 
1964ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1965adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1966adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
19671e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1968ba8c3d6fSFelix Fietkau 				continue;
1969ba8c3d6fSFelix Fietkau 
1970ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1971ba8c3d6fSFelix Fietkau 			break;
1972ba8c3d6fSFelix Fietkau 		}
1973af818581SJohannes Berg 	}
1974af818581SJohannes Berg }
1975af818581SJohannes Berg 
1976af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1977af818581SJohannes Berg {
197847086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1979af818581SJohannes Berg 
1980af818581SJohannes Berg 	/*
198147086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
198247086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
198347086fc5SJohannes Berg 	 * only from the non-enabled ones.
1984af818581SJohannes Berg 	 */
198547086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
198647086fc5SJohannes Berg 		ignore_for_response = 0;
1987af818581SJohannes Berg 
198847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
198947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1990af818581SJohannes Berg }
199147086fc5SJohannes Berg 
199247086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
199347086fc5SJohannes Berg {
199447086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
199547086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
199647086fc5SJohannes Berg 
199747086fc5SJohannes Berg 	/*
199847086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
199947086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
200047086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
200147086fc5SJohannes Berg 	 * actually getting called.
200247086fc5SJohannes Berg 	 */
200347086fc5SJohannes Berg 	if (!delivery_enabled)
200447086fc5SJohannes Berg 		return;
200547086fc5SJohannes Berg 
200647086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
200747086fc5SJohannes Berg 	case 1:
200847086fc5SJohannes Berg 		n_frames = 2;
200947086fc5SJohannes Berg 		break;
201047086fc5SJohannes Berg 	case 2:
201147086fc5SJohannes Berg 		n_frames = 4;
201247086fc5SJohannes Berg 		break;
201347086fc5SJohannes Berg 	case 3:
201447086fc5SJohannes Berg 		n_frames = 6;
201547086fc5SJohannes Berg 		break;
201647086fc5SJohannes Berg 	case 0:
201747086fc5SJohannes Berg 		/* XXX: what is a good value? */
201813a8098aSAndrei Otcheretianski 		n_frames = 128;
201947086fc5SJohannes Berg 		break;
202047086fc5SJohannes Berg 	}
202147086fc5SJohannes Berg 
202247086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
202347086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
2024af818581SJohannes Berg }
2025af818581SJohannes Berg 
2026af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2027af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
2028af818581SJohannes Berg {
2029af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2030af818581SJohannes Berg 
2031b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
2032b5878a2dSJohannes Berg 
20335ac2e350SJohannes Berg 	if (block) {
2034c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
203517c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
20365ac2e350SJohannes Berg 		return;
20375ac2e350SJohannes Berg 	}
20385ac2e350SJohannes Berg 
20395ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
20405ac2e350SJohannes Berg 		return;
20415ac2e350SJohannes Berg 
20425ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
20435ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
20445ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20455ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20465ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
20475ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
20485ac2e350SJohannes Berg 		/* must be asleep in this case */
20495ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20505ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20515ac2e350SJohannes Berg 	} else {
20525ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
205317c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
20545ac2e350SJohannes Berg 	}
2055af818581SJohannes Berg }
2056af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
2057dcf55fb5SFelix Fietkau 
2058e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
205937fbd908SJohannes Berg {
206037fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
206137fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
206237fbd908SJohannes Berg 
206337fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
206437fbd908SJohannes Berg 
206537fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
206637fbd908SJohannes Berg }
2067e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
206837fbd908SJohannes Berg 
20690ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
20700ead2510SEmmanuel Grumbach {
20710ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
20720ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
20730ead2510SEmmanuel Grumbach 	bool more_data;
20740ead2510SEmmanuel Grumbach 
20750ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
20760ead2510SEmmanuel Grumbach 
20770ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
20780ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
20790ead2510SEmmanuel Grumbach 					       reason, 0);
20800ead2510SEmmanuel Grumbach 
20810ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
20820ead2510SEmmanuel Grumbach }
20830ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
20840ead2510SEmmanuel Grumbach 
2085042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2086042ec453SJohannes Berg 				u8 tid, bool buffered)
2087dcf55fb5SFelix Fietkau {
2088dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2089dcf55fb5SFelix Fietkau 
20905a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2091042ec453SJohannes Berg 		return;
2092042ec453SJohannes Berg 
20931b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
20941b000789SJohannes Berg 
2095948d887dSJohannes Berg 	if (buffered)
2096948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2097948d887dSJohannes Berg 	else
2098948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2099948d887dSJohannes Berg 
2100c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2101dcf55fb5SFelix Fietkau }
2102042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2103d9a7ddb0SJohannes Berg 
2104b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2105b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2106b4809e94SToke Høiland-Jørgensen {
2107942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2108942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2109942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2110942741daSFelix Fietkau 	u32 airtime = 0;
2111c77bfab9SFelix Fietkau 	u32 diff;
2112b4809e94SToke Høiland-Jørgensen 
2113942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2114942741daSFelix Fietkau 		airtime += tx_airtime;
2115942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2116942741daSFelix Fietkau 		airtime += rx_airtime;
2117b4809e94SToke Høiland-Jørgensen 
2118942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2119942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2120942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2121c77bfab9SFelix Fietkau 
2122c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2123c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2124942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2125c77bfab9SFelix Fietkau 
2126942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2127b4809e94SToke Høiland-Jørgensen }
2128b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2129b4809e94SToke Høiland-Jørgensen 
2130*4c51541dSBenjamin Berg void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2131*4c51541dSBenjamin Berg {
2132*4c51541dSBenjamin Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2133*4c51541dSBenjamin Berg 	struct ieee80211_link_sta *link_sta;
2134*4c51541dSBenjamin Berg 	int link_id, i;
2135*4c51541dSBenjamin Berg 	bool first = true;
2136*4c51541dSBenjamin Berg 
2137*4c51541dSBenjamin Berg 	if (!pubsta->valid_links || !pubsta->mlo) {
2138*4c51541dSBenjamin Berg 		pubsta->cur = &pubsta->deflink.agg;
2139*4c51541dSBenjamin Berg 		return;
2140*4c51541dSBenjamin Berg 	}
2141*4c51541dSBenjamin Berg 
2142*4c51541dSBenjamin Berg 	rcu_read_lock();
2143*4c51541dSBenjamin Berg 	for_each_sta_active_link(&sta->sdata->vif, pubsta, link_sta, link_id) {
2144*4c51541dSBenjamin Berg 		if (first) {
2145*4c51541dSBenjamin Berg 			sta->cur = pubsta->deflink.agg;
2146*4c51541dSBenjamin Berg 			first = false;
2147*4c51541dSBenjamin Berg 			continue;
2148*4c51541dSBenjamin Berg 		}
2149*4c51541dSBenjamin Berg 
2150*4c51541dSBenjamin Berg 		sta->cur.max_amsdu_len =
2151*4c51541dSBenjamin Berg 			min(sta->cur.max_amsdu_len,
2152*4c51541dSBenjamin Berg 			    link_sta->agg.max_amsdu_len);
2153*4c51541dSBenjamin Berg 		sta->cur.max_rc_amsdu_len =
2154*4c51541dSBenjamin Berg 			min(sta->cur.max_rc_amsdu_len,
2155*4c51541dSBenjamin Berg 			    link_sta->agg.max_rc_amsdu_len);
2156*4c51541dSBenjamin Berg 
2157*4c51541dSBenjamin Berg 		for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2158*4c51541dSBenjamin Berg 			sta->cur.max_tid_amsdu_len[i] =
2159*4c51541dSBenjamin Berg 				min(sta->cur.max_tid_amsdu_len[i],
2160*4c51541dSBenjamin Berg 				    link_sta->agg.max_tid_amsdu_len[i]);
2161*4c51541dSBenjamin Berg 	}
2162*4c51541dSBenjamin Berg 	rcu_read_unlock();
2163*4c51541dSBenjamin Berg 
2164*4c51541dSBenjamin Berg 	pubsta->cur = &sta->cur;
2165*4c51541dSBenjamin Berg }
2166*4c51541dSBenjamin Berg EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2167*4c51541dSBenjamin Berg 
21683ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
21693ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
21703ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
21713ace10f5SKan Yan {
21723ace10f5SKan Yan 	int tx_pending;
21733ace10f5SKan Yan 
2174911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2175911bde0fSToke Høiland-Jørgensen 		return;
2176911bde0fSToke Høiland-Jørgensen 
21773ace10f5SKan Yan 	if (!tx_completed) {
21783ace10f5SKan Yan 		if (sta)
21793ace10f5SKan Yan 			atomic_add(tx_airtime,
21803ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
21813ace10f5SKan Yan 
21823ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
21838e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
21843ace10f5SKan Yan 		return;
21853ace10f5SKan Yan 	}
21863ace10f5SKan Yan 
21873ace10f5SKan Yan 	if (sta) {
21883ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
21893ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
219004e35caaSFelix Fietkau 		if (tx_pending < 0)
21913ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
21923ace10f5SKan Yan 				       tx_pending, 0);
21933ace10f5SKan Yan 	}
21943ace10f5SKan Yan 
21958e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
21963ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
21978e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
21983ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
21993ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
22003ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
22018e4bac06SFelix Fietkau 		      tx_airtime)) {
22028e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
22033ace10f5SKan Yan 			       tx_pending, 0);
22048e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
22058e4bac06SFelix Fietkau 	}
22063ace10f5SKan Yan }
22073ace10f5SKan Yan 
220883d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2209d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2210d9a7ddb0SJohannes Berg {
22118bf11d8dSJohannes Berg 	might_sleep();
2212d9a7ddb0SJohannes Berg 
2213d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2214d9a7ddb0SJohannes Berg 		return 0;
2215d9a7ddb0SJohannes Berg 
2216f09603a2SJohannes Berg 	/* check allowed transitions first */
2217f09603a2SJohannes Berg 
2218d9a7ddb0SJohannes Berg 	switch (new_state) {
2219d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2220f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2221d9a7ddb0SJohannes Berg 			return -EINVAL;
2222d9a7ddb0SJohannes Berg 		break;
2223d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2224f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2225f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2226d9a7ddb0SJohannes Berg 			return -EINVAL;
2227d9a7ddb0SJohannes Berg 		break;
2228d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2229f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2230f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2231d9a7ddb0SJohannes Berg 			return -EINVAL;
2232d9a7ddb0SJohannes Berg 		break;
2233d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2234f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2235d9a7ddb0SJohannes Berg 			return -EINVAL;
2236d9a7ddb0SJohannes Berg 		break;
2237d9a7ddb0SJohannes Berg 	default:
2238d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2239d9a7ddb0SJohannes Berg 		return -EINVAL;
2240d9a7ddb0SJohannes Berg 	}
2241d9a7ddb0SJohannes Berg 
2242bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2243bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2244f09603a2SJohannes Berg 
2245f09603a2SJohannes Berg 	/*
2246f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2247f09603a2SJohannes Berg 	 * fail the transition
2248f09603a2SJohannes Berg 	 */
2249f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2250f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2251f09603a2SJohannes Berg 					sta->sta_state, new_state);
2252f09603a2SJohannes Berg 		if (err)
2253f09603a2SJohannes Berg 			return err;
2254f09603a2SJohannes Berg 	}
2255f09603a2SJohannes Berg 
2256f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2257f09603a2SJohannes Berg 
2258f09603a2SJohannes Berg 	switch (new_state) {
2259f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2260f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2261f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2262f09603a2SJohannes Berg 		break;
2263f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2264a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2265f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2266a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2267f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
22680cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
226952cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
227052cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2271a7201a6cSIlan Peer 		}
2272f09603a2SJohannes Berg 		break;
2273f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2274f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2275f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
22769cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
22770cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
227852cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
227952cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2280f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
228172f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2282f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
228317c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
228449ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2285f09603a2SJohannes Berg 		}
2286f09603a2SJohannes Berg 		break;
2287f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2288f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
228972f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2290f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
229117c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
229249ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2293f09603a2SJohannes Berg 		}
22943e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
22953e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
22963e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
22973e493173SJouni Malinen 						    sta->sta.addr);
2298f09603a2SJohannes Berg 		break;
2299f09603a2SJohannes Berg 	default:
2300f09603a2SJohannes Berg 		break;
2301f09603a2SJohannes Berg 	}
2302f09603a2SJohannes Berg 
2303d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2304d9a7ddb0SJohannes Berg 
2305d9a7ddb0SJohannes Berg 	return 0;
2306d9a7ddb0SJohannes Berg }
2307687da132SEmmanuel Grumbach 
2308c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2309c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2310c9c5962bSJohannes Berg {
2311046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2312c9c5962bSJohannes Berg 	int cpu;
2313c9c5962bSJohannes Berg 
2314046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2315c9c5962bSJohannes Berg 		return stats;
2316c9c5962bSJohannes Berg 
2317c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2318c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2319c9c5962bSJohannes Berg 
2320046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2321c9c5962bSJohannes Berg 
2322c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2323c9c5962bSJohannes Berg 			stats = cpustats;
2324c9c5962bSJohannes Berg 	}
2325c9c5962bSJohannes Berg 
2326c9c5962bSJohannes Berg 	return stats;
2327c9c5962bSJohannes Berg }
2328c9c5962bSJohannes Berg 
232941cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
23304f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2331fbd6ff5cSJohannes Berg {
2332dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2333fbd6ff5cSJohannes Berg 
2334dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
23357f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
23364f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2337dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2338dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2339dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2340dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
23417f406cd1SJohannes Berg 		break;
23427f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
23434f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2344dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2345dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2346dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
23477f406cd1SJohannes Berg 		break;
23487f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2349fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2350fbd6ff5cSJohannes Berg 		u16 brate;
23514f6b1b3dSJohannes Berg 		unsigned int shift;
2352dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2353dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2354fbd6ff5cSJohannes Berg 
2355dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
23568b783d10SThomas Pedersen 
23578b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
23588b783d10SThomas Pedersen 			break;
23598b783d10SThomas Pedersen 
2360dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
23614f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
23624f6b1b3dSJohannes Berg 			shift = 2;
23634f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
23644f6b1b3dSJohannes Berg 			shift = 1;
23654f6b1b3dSJohannes Berg 		else
23664f6b1b3dSJohannes Berg 			shift = 0;
2367fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
23687f406cd1SJohannes Berg 		break;
23697f406cd1SJohannes Berg 		}
237041cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
237141cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
237241cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
237341cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
237441cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
237541cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
237641cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
237741cbb0f5SLuca Coelho 		break;
2378fbd6ff5cSJohannes Berg 	}
23794f6b1b3dSJohannes Berg }
2380fbd6ff5cSJohannes Berg 
2381a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
23824f6b1b3dSJohannes Berg {
23836aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
23844f6b1b3dSJohannes Berg 
23854f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2386a17d93ffSBen Greear 		return -EINVAL;
2387a17d93ffSBen Greear 
23884f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2389a17d93ffSBen Greear 	return 0;
2390fbd6ff5cSJohannes Berg }
2391fbd6ff5cSJohannes Berg 
2392b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2393b255b72bSSeevalamuthu Mariappan 					int tid)
2394b255b72bSSeevalamuthu Mariappan {
2395b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2396b255b72bSSeevalamuthu Mariappan 	u64 value;
2397b255b72bSSeevalamuthu Mariappan 
2398b255b72bSSeevalamuthu Mariappan 	do {
2399278d3ba6SSebastian Andrzej Siewior 		start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2400b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2401278d3ba6SSebastian Andrzej Siewior 	} while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2402b255b72bSSeevalamuthu Mariappan 
2403b255b72bSSeevalamuthu Mariappan 	return value;
2404b255b72bSSeevalamuthu Mariappan }
2405b255b72bSSeevalamuthu Mariappan 
24060f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
24070f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
24080f9c5a61SJohannes Berg 			     int tid)
24090f9c5a61SJohannes Berg {
24100f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2411b255b72bSSeevalamuthu Mariappan 	int cpu;
24120f9c5a61SJohannes Berg 
24130f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2414046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2415046d2e7cSSriram R 							   tid);
24160f9c5a61SJohannes Berg 
2417046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2418b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2419b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2420b255b72bSSeevalamuthu Mariappan 
2421046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2422046d2e7cSSriram R 						     cpu);
2423b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2424b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2425b255b72bSSeevalamuthu Mariappan 			}
2426b255b72bSSeevalamuthu Mariappan 		}
24270f9c5a61SJohannes Berg 
24280f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
24290f9c5a61SJohannes Berg 	}
24300f9c5a61SJohannes Berg 
24310f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
24320f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2433046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
24340f9c5a61SJohannes Berg 	}
24350f9c5a61SJohannes Berg 
24360f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
24370f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
24380f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2439046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
24400f9c5a61SJohannes Berg 	}
24410f9c5a61SJohannes Berg 
24420f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
24430f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
24440f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2445046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
24460f9c5a61SJohannes Berg 	}
24472fe4a29aSToke Høiland-Jørgensen 
24482fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
24492fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
24502fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
24512fe4a29aSToke Høiland-Jørgensen 
24522fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
24532fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
24542fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
24552fe4a29aSToke Høiland-Jørgensen 
24562fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
24572fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
24582fe4a29aSToke Høiland-Jørgensen 	}
24590f9c5a61SJohannes Berg }
24600f9c5a61SJohannes Berg 
2461c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2462c9c5962bSJohannes Berg {
2463c9c5962bSJohannes Berg 	unsigned int start;
2464c9c5962bSJohannes Berg 	u64 value;
2465c9c5962bSJohannes Berg 
2466c9c5962bSJohannes Berg 	do {
2467278d3ba6SSebastian Andrzej Siewior 		start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2468c9c5962bSJohannes Berg 		value = rxstats->bytes;
2469278d3ba6SSebastian Andrzej Siewior 	} while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2470c9c5962bSJohannes Berg 
2471c9c5962bSJohannes Berg 	return value;
2472c9c5962bSJohannes Berg }
2473c9c5962bSJohannes Berg 
24740fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
24750fdf1493SJohannes Berg 		   bool tidstats)
2476b7ffbd7eSJohannes Berg {
2477b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2478b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2479b7ffbd7eSJohannes Berg 	u32 thr = 0;
2480c9c5962bSJohannes Berg 	int i, ac, cpu;
2481c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2482c9c5962bSJohannes Berg 
2483c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2484b7ffbd7eSJohannes Berg 
2485b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2486b7ffbd7eSJohannes Berg 
2487225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2488225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2489225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2490225b8189SJohannes Berg 	 */
2491225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2492bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2493225b8189SJohannes Berg 
24942b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2495a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2496a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2497a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2498a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
24999cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2500a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2501976bd9efSJohannes Berg 
2502976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2503bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2504bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2505a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2506976bd9efSJohannes Berg 	}
2507b7ffbd7eSJohannes Berg 
250884b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
25099cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2510e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2511b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
25122b9a7e1bSJohannes Berg 
2513a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2514a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2515b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
25162b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2517046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2518a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2519b7ffbd7eSJohannes Berg 	}
25202b9a7e1bSJohannes Berg 
2521a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
25222b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
25232b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2524046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2525a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
25262b9a7e1bSJohannes Berg 	}
25272b9a7e1bSJohannes Berg 
2528a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2529a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2530046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
25310f9c5a61SJohannes Berg 
2532046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2533c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2534c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2535c9c5962bSJohannes Berg 
2536046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2537046d2e7cSSriram R 						     cpu);
2538c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2539c9c5962bSJohannes Berg 			}
2540c9c5962bSJohannes Berg 		}
2541c9c5962bSJohannes Berg 
2542a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
25432b9a7e1bSJohannes Berg 	}
25442b9a7e1bSJohannes Berg 
2545a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2546046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2547046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2548c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2549c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2550c9c5962bSJohannes Berg 
2551046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2552046d2e7cSSriram R 						     cpu);
2553c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2554c9c5962bSJohannes Berg 			}
2555c9c5962bSJohannes Berg 		}
2556a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
25572b9a7e1bSJohannes Berg 	}
25582b9a7e1bSJohannes Berg 
2559a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2560046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2561a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
25622b9a7e1bSJohannes Berg 	}
25632b9a7e1bSJohannes Berg 
2564a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2565046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2566a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
25672b9a7e1bSJohannes Berg 	}
25682b9a7e1bSJohannes Berg 
2569b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2570b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2571b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2572b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2573b4809e94SToke Høiland-Jørgensen 	}
2574b4809e94SToke Høiland-Jørgensen 
2575b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2576b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2577b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2578b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2579b4809e94SToke Høiland-Jørgensen 	}
2580b4809e94SToke Høiland-Jørgensen 
2581b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2582942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2583b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2584b4809e94SToke Høiland-Jørgensen 	}
2585b4809e94SToke Høiland-Jørgensen 
2586046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2587046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2588c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2589c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2590c9c5962bSJohannes Berg 
2591046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2592e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2593c9c5962bSJohannes Berg 		}
2594c9c5962bSJohannes Berg 	}
2595b7ffbd7eSJohannes Berg 
2596225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2597225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2598a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2599a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2600225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2601225b8189SJohannes Berg 	}
2602225b8189SJohannes Berg 
260330686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
260430686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2605a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2606c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2607a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2608b7ffbd7eSJohannes Berg 		}
26092b9a7e1bSJohannes Berg 
2610046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2611a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
261240d9a38aSJohannes Berg 			sinfo->signal_avg =
2613046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2614a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
26152b9a7e1bSJohannes Berg 		}
26162b9a7e1bSJohannes Berg 	}
26172b9a7e1bSJohannes Berg 
2618c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2619c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2620c9c5962bSJohannes Berg 	 * pcpu statistics
2621c9c5962bSJohannes Berg 	 */
2622c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2623a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2624a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2625a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2626046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2627a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2628b7ffbd7eSJohannes Berg 
2629c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2630c9c5962bSJohannes Berg 
2631b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2632e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2633c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2634b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2635046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2636b7ffbd7eSJohannes Berg 		}
2637b7ffbd7eSJohannes Berg 	}
2638b7ffbd7eSJohannes Berg 
26398a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
26408a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2641046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2642e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2643a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
26442b9a7e1bSJohannes Berg 	}
26452b9a7e1bSJohannes Berg 
26468a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
26478a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2648a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2649a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
26502b9a7e1bSJohannes Berg 	}
2651b7ffbd7eSJohannes Berg 
26520fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
26536af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
26546af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
26558689c051SArend van Spriel 	}
265679c892b8SJohannes Berg 
2657b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2658b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2659a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2660a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2661a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2662a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2663a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2664dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
26651303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
26661303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2667b7ffbd7eSJohannes Berg 
2668433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2669433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2670433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2671b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2672a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2673433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2674b7ffbd7eSJohannes Berg 		}
2675433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2676433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2677433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2678dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
26791303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2680b7ffbd7eSJohannes Berg #endif
2681b7ffbd7eSJohannes Berg 	}
2682b7ffbd7eSJohannes Berg 
2683b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2684b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2685b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2686b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2687b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2688b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2689b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2690785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2691b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2692b7ffbd7eSJohannes Berg 
2693b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2694b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2695b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2696b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2697b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2698b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2699b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2700b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2701b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2702b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2703b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2704b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2705a74a8c84SJohannes Berg 	if (sta->sta.wme)
2706b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2707b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2708b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2709b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2710b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2711b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2712b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2713b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2714b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2715b7ffbd7eSJohannes Berg 
27163b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
27173b17fbf8SMaxim Altshul 
27183b17fbf8SMaxim Altshul 	if (thr != 0) {
2719a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
27203b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
27213b17fbf8SMaxim Altshul 	}
2722a78b26ffSVenkateswara Naralasetty 
2723a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2724046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2725046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2726a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2727a78b26ffSVenkateswara Naralasetty 	}
2728cc60dbbfSBalaji Pothunoori 
27299c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2730046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2731cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2732cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2733046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2734cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
27359c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2736cc60dbbfSBalaji Pothunoori 	}
2737ab60633cSNarayanraddi Masti 
2738ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2739ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2740ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2741ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2742ab60633cSNarayanraddi Masti 	}
27433b17fbf8SMaxim Altshul }
27443b17fbf8SMaxim Altshul 
27453b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
27463b17fbf8SMaxim Altshul {
27473b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
27483b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
27493b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
27503b17fbf8SMaxim Altshul 	u32 thr = 0;
27513b17fbf8SMaxim Altshul 
27523b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
27533b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
27543b17fbf8SMaxim Altshul 
2755b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2756b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2757b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2758b7ffbd7eSJohannes Berg 	else
27594fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2760b7ffbd7eSJohannes Berg 
27613b17fbf8SMaxim Altshul 	return thr;
2762b7ffbd7eSJohannes Berg }
2763b8da6b6aSJohannes Berg 
2764b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2765b8da6b6aSJohannes Berg {
2766c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2767c9c5962bSJohannes Berg 
2768046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2769046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2770c9c5962bSJohannes Berg 		return stats->last_rx;
2771046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2772b8da6b6aSJohannes Berg }
2773484a54c2SToke Høiland-Jørgensen 
2774484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2775484a54c2SToke Høiland-Jørgensen {
2776484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2777484a54c2SToke Høiland-Jørgensen 		return;
2778484a54c2SToke Høiland-Jørgensen 
2779484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2780484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2781484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2782484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2783484a54c2SToke Høiland-Jørgensen 	} else {
2784484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2785484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2786484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2787484a54c2SToke Høiland-Jørgensen 	}
2788484a54c2SToke Høiland-Jørgensen }
2789484a54c2SToke Høiland-Jørgensen 
2790484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2791484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2792484a54c2SToke Høiland-Jørgensen {
2793484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2794484a54c2SToke Høiland-Jørgensen 
2795484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2796484a54c2SToke Høiland-Jørgensen }
2797cb71f1d1SJohannes Berg 
2798cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2799cb71f1d1SJohannes Berg {
2800cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2801cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2802cb71f1d1SJohannes Berg 	int ret;
2803cb71f1d1SJohannes Berg 
2804cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2805cb71f1d1SJohannes Berg 
2806cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2807cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2808cb71f1d1SJohannes Berg 		return -EINVAL;
2809cb71f1d1SJohannes Berg 
2810cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2811cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2812cb71f1d1SJohannes Berg 		return -EBUSY;
2813cb71f1d1SJohannes Berg 
2814cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2815cb71f1d1SJohannes Berg 	if (!alloc)
2816cb71f1d1SJohannes Berg 		return -ENOMEM;
2817cb71f1d1SJohannes Berg 
2818cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2819cb71f1d1SJohannes Berg 	if (ret) {
2820cb71f1d1SJohannes Berg 		kfree(alloc);
2821cb71f1d1SJohannes Berg 		return ret;
2822cb71f1d1SJohannes Berg 	}
2823cb71f1d1SJohannes Berg 
2824cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2825cb71f1d1SJohannes Berg 
2826cb71f1d1SJohannes Berg 	return 0;
2827cb71f1d1SJohannes Berg }
2828cb71f1d1SJohannes Berg 
282921476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
283021476ad1SShaul Triebitz {
283121476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
283221476ad1SShaul Triebitz 
283321476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
283421476ad1SShaul Triebitz }
283521476ad1SShaul Triebitz 
2836cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2837cb71f1d1SJohannes Berg {
2838cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2839c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2840cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2841cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2842cb71f1d1SJohannes Berg 	int ret;
2843cb71f1d1SJohannes Berg 
2844c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2845c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2846cb71f1d1SJohannes Berg 
2847c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2848cb71f1d1SJohannes Berg 		return -EINVAL;
2849cb71f1d1SJohannes Berg 
2850177577dbSJohannes Berg 	rcu_read_lock();
2851177577dbSJohannes Berg 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2852177577dbSJohannes Berg 		rcu_read_unlock();
2853177577dbSJohannes Berg 		return -EALREADY;
2854177577dbSJohannes Berg 	}
2855177577dbSJohannes Berg 	/* we only modify under the mutex so this is fine */
2856177577dbSJohannes Berg 	rcu_read_unlock();
2857177577dbSJohannes Berg 
2858cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2859cb71f1d1SJohannes Berg 
286080e2b1faSLukas Bulwahn 	if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2861ba6ddab9SJohannes Berg 		goto hash;
2862cb71f1d1SJohannes Berg 
2863*4c51541dSBenjamin Berg 	/* Ensure the values are updated for the driver,
2864*4c51541dSBenjamin Berg 	 * redone by sta_remove_link on failure.
2865*4c51541dSBenjamin Berg 	 */
2866*4c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
2867*4c51541dSBenjamin Berg 
2868cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2869cb71f1d1SJohannes Berg 				   old_links, new_links);
2870cb71f1d1SJohannes Berg 	if (ret) {
2871cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2872ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2873177577dbSJohannes Berg 		return ret;
2874cb71f1d1SJohannes Berg 	}
2875cb71f1d1SJohannes Berg 
2876ba6ddab9SJohannes Berg hash:
2877177577dbSJohannes Berg 	ret = link_sta_info_hash_add(sdata->local, link_sta);
2878177577dbSJohannes Berg 	WARN_ON(ret);
2879177577dbSJohannes Berg 	return 0;
2880cb71f1d1SJohannes Berg }
2881cb71f1d1SJohannes Berg 
2882cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2883cb71f1d1SJohannes Berg {
2884cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2885a8f62399SShaul Triebitz 	u16 old_links = sta->sta.valid_links;
2886cb71f1d1SJohannes Berg 
2887cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2888cb71f1d1SJohannes Berg 
2889cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2890cb71f1d1SJohannes Berg 
2891cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2892cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2893a8f62399SShaul Triebitz 				     old_links, sta->sta.valid_links);
2894cb71f1d1SJohannes Berg 
2895ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2896cb71f1d1SJohannes Berg }
2897175ad2ecSJohannes Berg 
2898175ad2ecSJohannes Berg void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2899175ad2ecSJohannes Berg 					   const u8 *ext_capab,
2900175ad2ecSJohannes Berg 					   unsigned int ext_capab_len)
2901175ad2ecSJohannes Berg {
2902175ad2ecSJohannes Berg 	u8 val;
2903175ad2ecSJohannes Berg 
2904175ad2ecSJohannes Berg 	sta->sta.max_amsdu_subframes = 0;
2905175ad2ecSJohannes Berg 
2906175ad2ecSJohannes Berg 	if (ext_capab_len < 8)
2907175ad2ecSJohannes Berg 		return;
2908175ad2ecSJohannes Berg 
2909175ad2ecSJohannes Berg 	/* The sender might not have sent the last bit, consider it to be 0 */
2910175ad2ecSJohannes Berg 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2911175ad2ecSJohannes Berg 
2912175ad2ecSJohannes Berg 	/* we did get all the bits, take the MSB as well */
2913175ad2ecSJohannes Berg 	if (ext_capab_len >= 9)
2914175ad2ecSJohannes Berg 		val |= u8_get_bits(ext_capab[8],
2915175ad2ecSJohannes Berg 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2916175ad2ecSJohannes Berg 
2917175ad2ecSJohannes Berg 	if (val)
2918175ad2ecSJohannes Berg 		sta->sta.max_amsdu_subframes = 4 << val;
2919175ad2ecSJohannes Berg }
292065fd846cSJohannes Berg 
292165fd846cSJohannes Berg #ifdef CONFIG_LOCKDEP
292265fd846cSJohannes Berg bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
292365fd846cSJohannes Berg {
292465fd846cSJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
292565fd846cSJohannes Berg 
292665fd846cSJohannes Berg 	return lockdep_is_held(&sta->local->sta_mtx);
292765fd846cSJohannes Berg }
292865fd846cSJohannes Berg EXPORT_SYMBOL(lockdep_sta_mutex_held);
292965fd846cSJohannes Berg #endif
2930