xref: /openbmc/linux/net/mac80211/sta_info.c (revision 92747f17c431a75967b461bedbb36ff259acead1)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f0706e82SJiri Benc /*
3f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
4f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
6dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
710a7ba92SJohannes Berg  * Copyright (C) 2018-2023 Intel Corporation
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
67cb71f1d1SJohannes Berg struct sta_link_alloc {
68cb71f1d1SJohannes Berg 	struct link_sta_info info;
69cb71f1d1SJohannes Berg 	struct ieee80211_link_sta sta;
70c71420dbSJohannes Berg 	struct rcu_head rcu_head;
71cb71f1d1SJohannes Berg };
72cb71f1d1SJohannes Berg 
737bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
747bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
75caf22d31SJohannes Berg 	.automatic_shrinking = true,
767bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
77ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
787bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
79ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
807bedd0cfSJohannes Berg };
817bedd0cfSJohannes Berg 
82ba6ddab9SJohannes Berg static const struct rhashtable_params link_sta_rht_params = {
83ba6ddab9SJohannes Berg 	.nelem_hint = 3, /* start small */
84ba6ddab9SJohannes Berg 	.automatic_shrinking = true,
85ba6ddab9SJohannes Berg 	.head_offset = offsetof(struct link_sta_info, link_hash_node),
86ba6ddab9SJohannes Berg 	.key_offset = offsetof(struct link_sta_info, addr),
87ba6ddab9SJohannes Berg 	.key_len = ETH_ALEN,
88ba6ddab9SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89ba6ddab9SJohannes Berg };
90ba6ddab9SJohannes Berg 
914d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
92be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
93f0706e82SJiri Benc 			     struct sta_info *sta)
94f0706e82SJiri Benc {
9583e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
967bedd0cfSJohannes Berg 			       sta_rht_params);
97f0706e82SJiri Benc }
98f0706e82SJiri Benc 
99f36fe0a2SJohannes Berg static int link_sta_info_hash_add(struct ieee80211_local *local,
100f36fe0a2SJohannes Berg 				  struct link_sta_info *link_sta)
101f36fe0a2SJohannes Berg {
102177577dbSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
103f36fe0a2SJohannes Berg 	return rhltable_insert(&local->link_sta_hash,
104f36fe0a2SJohannes Berg 			       &link_sta->link_hash_node,
105f36fe0a2SJohannes Berg 			       link_sta_rht_params);
106f36fe0a2SJohannes Berg }
107f36fe0a2SJohannes Berg 
108ba6ddab9SJohannes Berg static int link_sta_info_hash_del(struct ieee80211_local *local,
109ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
110ba6ddab9SJohannes Berg {
111177577dbSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
112ba6ddab9SJohannes Berg 	return rhltable_remove(&local->link_sta_hash,
113ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
114ba6ddab9SJohannes Berg 			       link_sta_rht_params);
115ba6ddab9SJohannes Berg }
116ba6ddab9SJohannes Berg 
1175108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
118b22cfcfcSEliad Peller {
119b22cfcfcSEliad Peller 	int ac, i;
120b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
121b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
122b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
123d012a605SMarco Porsch 	struct ps_data *ps;
124b22cfcfcSEliad Peller 
125e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1265ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1275ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
128d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
129d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
130d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1313f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1323f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
133d012a605SMarco Porsch 		else
134d012a605SMarco Porsch 			return;
135b22cfcfcSEliad Peller 
136b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
137e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1385ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
139b22cfcfcSEliad Peller 
140d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
141b22cfcfcSEliad Peller 	}
142b22cfcfcSEliad Peller 
143ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
144adf8ed01SJohannes Berg 		struct txq_info *txqi;
145adf8ed01SJohannes Berg 
146adf8ed01SJohannes Berg 		if (!sta->sta.txq[i])
147adf8ed01SJohannes Berg 			continue;
148adf8ed01SJohannes Berg 
149adf8ed01SJohannes Berg 		txqi = to_txq_info(sta->sta.txq[i]);
150ba8c3d6fSFelix Fietkau 
151fa962b92SMichal Kazior 		ieee80211_txq_purge(local, txqi);
152ba8c3d6fSFelix Fietkau 	}
153ba8c3d6fSFelix Fietkau 
154b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
155b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1561f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1571f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
158b22cfcfcSEliad Peller 	}
159b22cfcfcSEliad Peller 
16045b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
16145b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
162b22cfcfcSEliad Peller 
1635ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
164b22cfcfcSEliad Peller 
165b22cfcfcSEliad Peller 	/*
166b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
167b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
168b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
169b22cfcfcSEliad Peller 	 * directly by station destruction.
170b22cfcfcSEliad Peller 	 */
1715a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
172661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
173b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
174b22cfcfcSEliad Peller 		if (!tid_tx)
175b22cfcfcSEliad Peller 			continue;
1761f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
177b22cfcfcSEliad Peller 		kfree(tid_tx);
178b22cfcfcSEliad Peller 	}
1795108ca82SJohannes Berg }
180b22cfcfcSEliad Peller 
1815108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1825108ca82SJohannes Berg {
1835108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1845108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1855108ca82SJohannes Berg 
1865108ca82SJohannes Berg 	__cleanup_single_sta(sta);
187b22cfcfcSEliad Peller 	sta_info_free(local, sta);
188b22cfcfcSEliad Peller }
189b22cfcfcSEliad Peller 
19083e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
19183e7e4ceSHerbert Xu 					 const u8 *addr)
19283e7e4ceSHerbert Xu {
19383e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
19483e7e4ceSHerbert Xu }
19583e7e4ceSHerbert Xu 
196d0709a65SJohannes Berg /* protected by RCU */
197abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
198abe60632SJohannes Berg 			      const u8 *addr)
19943ba7e95SJohannes Berg {
200abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
20183e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
20260f4b626SJohannes Berg 	struct sta_info *sta;
20343ba7e95SJohannes Berg 
20460f4b626SJohannes Berg 	rcu_read_lock();
20583e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
20660f4b626SJohannes Berg 		if (sta->sdata == sdata) {
20760f4b626SJohannes Berg 			rcu_read_unlock();
20860f4b626SJohannes Berg 			/* this is safe as the caller must already hold
20960f4b626SJohannes Berg 			 * another rcu read section or the mutex
21060f4b626SJohannes Berg 			 */
21160f4b626SJohannes Berg 			return sta;
21260f4b626SJohannes Berg 		}
21360f4b626SJohannes Berg 	}
21460f4b626SJohannes Berg 	rcu_read_unlock();
21560f4b626SJohannes Berg 	return NULL;
21643ba7e95SJohannes Berg }
21743ba7e95SJohannes Berg 
2180e5ded5aSFelix Fietkau /*
2190e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
2200e5ded5aSFelix Fietkau  * or from one of its vlans
2210e5ded5aSFelix Fietkau  */
2220e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
2230e5ded5aSFelix Fietkau 				  const u8 *addr)
2240e5ded5aSFelix Fietkau {
2250e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
22683e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
2270e5ded5aSFelix Fietkau 	struct sta_info *sta;
2280e5ded5aSFelix Fietkau 
2297bedd0cfSJohannes Berg 	rcu_read_lock();
23083e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2317bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2327bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2337bedd0cfSJohannes Berg 			rcu_read_unlock();
2347bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2357bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2367bedd0cfSJohannes Berg 			 */
2370e5ded5aSFelix Fietkau 			return sta;
2380e5ded5aSFelix Fietkau 		}
2397bedd0cfSJohannes Berg 	}
2407bedd0cfSJohannes Berg 	rcu_read_unlock();
2417bedd0cfSJohannes Berg 	return NULL;
2427bedd0cfSJohannes Berg }
2430e5ded5aSFelix Fietkau 
244ba6ddab9SJohannes Berg struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
245ba6ddab9SJohannes Berg 					      const u8 *addr)
246ba6ddab9SJohannes Berg {
247ba6ddab9SJohannes Berg 	return rhltable_lookup(&local->link_sta_hash, addr,
248ba6ddab9SJohannes Berg 			       link_sta_rht_params);
249ba6ddab9SJohannes Berg }
250ba6ddab9SJohannes Berg 
251ba6ddab9SJohannes Berg struct link_sta_info *
252ba6ddab9SJohannes Berg link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
253ba6ddab9SJohannes Berg {
254ba6ddab9SJohannes Berg 	struct ieee80211_local *local = sdata->local;
255ba6ddab9SJohannes Berg 	struct rhlist_head *tmp;
256ba6ddab9SJohannes Berg 	struct link_sta_info *link_sta;
257ba6ddab9SJohannes Berg 
258ba6ddab9SJohannes Berg 	rcu_read_lock();
259ba6ddab9SJohannes Berg 	for_each_link_sta_info(local, addr, link_sta, tmp) {
260ba6ddab9SJohannes Berg 		struct sta_info *sta = link_sta->sta;
261ba6ddab9SJohannes Berg 
262ba6ddab9SJohannes Berg 		if (sta->sdata == sdata ||
263ba6ddab9SJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
264ba6ddab9SJohannes Berg 			rcu_read_unlock();
265ba6ddab9SJohannes Berg 			/* this is safe as the caller must already hold
266ba6ddab9SJohannes Berg 			 * another rcu read section or the mutex
267ba6ddab9SJohannes Berg 			 */
268ba6ddab9SJohannes Berg 			return link_sta;
269ba6ddab9SJohannes Berg 		}
270ba6ddab9SJohannes Berg 	}
271ba6ddab9SJohannes Berg 	rcu_read_unlock();
272ba6ddab9SJohannes Berg 	return NULL;
273ba6ddab9SJohannes Berg }
274ba6ddab9SJohannes Berg 
275ffa9598eSJohannes Berg struct ieee80211_sta *
276ffa9598eSJohannes Berg ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
277ffa9598eSJohannes Berg 				 const u8 *addr,
278ffa9598eSJohannes Berg 				 const u8 *localaddr,
279ffa9598eSJohannes Berg 				 unsigned int *link_id)
280ffa9598eSJohannes Berg {
281ffa9598eSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
282ffa9598eSJohannes Berg 	struct link_sta_info *link_sta;
283ffa9598eSJohannes Berg 	struct rhlist_head *tmp;
284ffa9598eSJohannes Berg 
285ffa9598eSJohannes Berg 	for_each_link_sta_info(local, addr, link_sta, tmp) {
286ffa9598eSJohannes Berg 		struct sta_info *sta = link_sta->sta;
287ffa9598eSJohannes Berg 		struct ieee80211_link_data *link;
288ffa9598eSJohannes Berg 		u8 _link_id = link_sta->link_id;
289ffa9598eSJohannes Berg 
290ffa9598eSJohannes Berg 		if (!localaddr) {
291ffa9598eSJohannes Berg 			if (link_id)
292ffa9598eSJohannes Berg 				*link_id = _link_id;
293ffa9598eSJohannes Berg 			return &sta->sta;
294ffa9598eSJohannes Berg 		}
295ffa9598eSJohannes Berg 
296ffa9598eSJohannes Berg 		link = rcu_dereference(sta->sdata->link[_link_id]);
297ffa9598eSJohannes Berg 		if (!link)
298ffa9598eSJohannes Berg 			continue;
299ffa9598eSJohannes Berg 
300ffa9598eSJohannes Berg 		if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
301ffa9598eSJohannes Berg 			continue;
302ffa9598eSJohannes Berg 
303ffa9598eSJohannes Berg 		if (link_id)
304ffa9598eSJohannes Berg 			*link_id = _link_id;
305ffa9598eSJohannes Berg 		return &sta->sta;
306ffa9598eSJohannes Berg 	}
307ffa9598eSJohannes Berg 
308ffa9598eSJohannes Berg 	return NULL;
309ffa9598eSJohannes Berg }
310ffa9598eSJohannes Berg EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
311ffa9598eSJohannes Berg 
3125072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
3135072f73cSToke Høiland-Jørgensen 				       const u8 *sta_addr, const u8 *vif_addr)
3145072f73cSToke Høiland-Jørgensen {
3155072f73cSToke Høiland-Jørgensen 	struct rhlist_head *tmp;
3165072f73cSToke Høiland-Jørgensen 	struct sta_info *sta;
3175072f73cSToke Høiland-Jørgensen 
3185072f73cSToke Høiland-Jørgensen 	for_each_sta_info(local, sta_addr, sta, tmp) {
3195072f73cSToke Høiland-Jørgensen 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
3205072f73cSToke Høiland-Jørgensen 			return sta;
3215072f73cSToke Høiland-Jørgensen 	}
3225072f73cSToke Høiland-Jørgensen 
3235072f73cSToke Høiland-Jørgensen 	return NULL;
3245072f73cSToke Høiland-Jørgensen }
3255072f73cSToke Høiland-Jørgensen 
3263b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
3273b53fde8SJohannes Berg 				     int idx)
328ee385855SLuis Carlos Cobo {
3293b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
330ee385855SLuis Carlos Cobo 	struct sta_info *sta;
331ee385855SLuis Carlos Cobo 	int i = 0;
332ee385855SLuis Carlos Cobo 
3338ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
3348ca47eb9SMadhuparna Bhowmik 				lockdep_is_held(&local->sta_mtx)) {
3353b53fde8SJohannes Berg 		if (sdata != sta->sdata)
3362a8ca29aSLuis Carlos Cobo 			continue;
337ee385855SLuis Carlos Cobo 		if (i < idx) {
338ee385855SLuis Carlos Cobo 			++i;
339ee385855SLuis Carlos Cobo 			continue;
340ee385855SLuis Carlos Cobo 		}
3412a8ca29aSLuis Carlos Cobo 		return sta;
342ee385855SLuis Carlos Cobo 	}
343ee385855SLuis Carlos Cobo 
344ee385855SLuis Carlos Cobo 	return NULL;
345ee385855SLuis Carlos Cobo }
346f0706e82SJiri Benc 
347cb71f1d1SJohannes Berg static void sta_info_free_link(struct link_sta_info *link_sta)
348246b39e4SJohannes Berg {
349cb71f1d1SJohannes Berg 	free_percpu(link_sta->pcpu_rx_stats);
350cb71f1d1SJohannes Berg }
351246b39e4SJohannes Berg 
352ba6ddab9SJohannes Berg static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
353ba6ddab9SJohannes Berg 			    bool unhash)
354cb71f1d1SJohannes Berg {
355cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc = NULL;
356c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
357cb71f1d1SJohannes Berg 
358c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
359c71420dbSJohannes Berg 					     lockdep_is_held(&sta->local->sta_mtx));
360c71420dbSJohannes Berg 
361c71420dbSJohannes Berg 	if (WARN_ON(!link_sta))
362cb71f1d1SJohannes Berg 		return;
363246b39e4SJohannes Berg 
364ba6ddab9SJohannes Berg 	if (unhash)
365ba6ddab9SJohannes Berg 		link_sta_info_hash_del(sta->local, link_sta);
366ba6ddab9SJohannes Berg 
367d2caad52SBenjamin Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
368d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_remove(link_sta);
369d2caad52SBenjamin Berg 
370c71420dbSJohannes Berg 	if (link_sta != &sta->deflink)
371c71420dbSJohannes Berg 		alloc = container_of(link_sta, typeof(*alloc), info);
372cb71f1d1SJohannes Berg 
373cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
374c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->link[link_id], NULL);
375c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
376cb71f1d1SJohannes Berg 	if (alloc) {
377cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
378c71420dbSJohannes Berg 		kfree_rcu(alloc, rcu_head);
379246b39e4SJohannes Berg 	}
3804c51541dSBenjamin Berg 
3814c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
382246b39e4SJohannes Berg }
383246b39e4SJohannes Berg 
38493e5deb1SJohannes Berg /**
385d9a7ddb0SJohannes Berg  * sta_info_free - free STA
38693e5deb1SJohannes Berg  *
3876ef307bcSRandy Dunlap  * @local: pointer to the global information
38893e5deb1SJohannes Berg  * @sta: STA info to free
38993e5deb1SJohannes Berg  *
39093e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
391d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
392d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
393d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
39493e5deb1SJohannes Berg  */
395d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
39693e5deb1SJohannes Berg {
397cb71f1d1SJohannes Berg 	int i;
398cb71f1d1SJohannes Berg 
399cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
400cb71f1d1SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
401cb71f1d1SJohannes Berg 			continue;
402cb71f1d1SJohannes Berg 
4030ad49045SJohannes Berg 		sta_remove_link(sta, i, false);
404cb71f1d1SJohannes Berg 	}
405cb71f1d1SJohannes Berg 
406dcd479e1SJohannes Berg 	/*
407dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
408dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
409dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
410dcd479e1SJohannes Berg 	 *
411dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
412dcd479e1SJohannes Berg 	 * allocated during state transitions.
413dcd479e1SJohannes Berg 	 */
414dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
415dcd479e1SJohannes Berg 		int ret;
416dcd479e1SJohannes Berg 
417dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
418dcd479e1SJohannes Berg 
419dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
420dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
421dcd479e1SJohannes Berg 			break;
422dcd479e1SJohannes Berg 	}
423dcd479e1SJohannes Berg 
424889cbb91SJohannes Berg 	if (sta->rate_ctrl)
4254b7679a5SJohannes Berg 		rate_control_free_sta(sta);
42693e5deb1SJohannes Berg 
427bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
42893e5deb1SJohannes Berg 
429ba8c3d6fSFelix Fietkau 	kfree(to_txq_info(sta->sta.txq[0]));
43053d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
431433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
432433f5bc1SJohannes Berg 	kfree(sta->mesh);
433433f5bc1SJohannes Berg #endif
434246b39e4SJohannes Berg 
435cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
43693e5deb1SJohannes Berg 	kfree(sta);
43793e5deb1SJohannes Berg }
43893e5deb1SJohannes Berg 
4394d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
44062b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
441d0709a65SJohannes Berg 			     struct sta_info *sta)
442f0706e82SJiri Benc {
44383e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
4447bedd0cfSJohannes Berg 			       sta_rht_params);
445f0706e82SJiri Benc }
446f0706e82SJiri Benc 
4475ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
448af818581SJohannes Berg {
449af818581SJohannes Berg 	struct sta_info *sta;
450af818581SJohannes Berg 
4515ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
452af818581SJohannes Berg 
453af818581SJohannes Berg 	if (sta->dead)
454af818581SJohannes Berg 		return;
455af818581SJohannes Berg 
45654420473SHelmut Schaa 	local_bh_disable();
4575ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
458af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
4595ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
460af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
4615ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
46247086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
463ce662b44SJohannes Berg 	local_bh_enable();
464af818581SJohannes Berg }
465af818581SJohannes Berg 
466af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
467af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
468af65cd96SJohannes Berg {
46930686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
470af65cd96SJohannes Berg 		return 0;
471af65cd96SJohannes Berg 
472889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
473af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
47435c347acSJohannes Berg 						     sta, gfp);
475889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
476af65cd96SJohannes Berg 		return -ENOMEM;
477af65cd96SJohannes Berg 
478af65cd96SJohannes Berg 	return 0;
479af65cd96SJohannes Berg }
480af65cd96SJohannes Berg 
481cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
482246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
483246b39e4SJohannes Berg 			       gfp_t gfp)
484246b39e4SJohannes Berg {
485246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
486246b39e4SJohannes Berg 	int i;
487246b39e4SJohannes Berg 
488246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
489246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
490246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
491246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
492246b39e4SJohannes Berg 			return -ENOMEM;
493246b39e4SJohannes Berg 	}
494246b39e4SJohannes Berg 
495246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
496246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
497246b39e4SJohannes Berg 
498246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
499246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
500246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
501246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
502246b39e4SJohannes Berg 
503246b39e4SJohannes Berg 	return 0;
504246b39e4SJohannes Berg }
505246b39e4SJohannes Berg 
506cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
507cb71f1d1SJohannes Berg 			      unsigned int link_id,
508cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
509cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
510cb71f1d1SJohannes Berg {
511cb71f1d1SJohannes Berg 	link_info->sta = sta;
512cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
513c71420dbSJohannes Berg 	link_info->pub = link_sta;
5141d9e4c91SBenjamin Berg 	link_info->pub->sta = &sta->sta;
515c73993b8SJohannes Berg 	link_sta->link_id = link_id;
516c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
517c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
518261ce887SBenjamin Berg 
519261ce887SBenjamin Berg 	link_sta->smps_mode = IEEE80211_SMPS_OFF;
5204c51541dSBenjamin Berg 	link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
521cb71f1d1SJohannes Berg }
522cb71f1d1SJohannes Berg 
523f36fe0a2SJohannes Berg static struct sta_info *
524f36fe0a2SJohannes Berg __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
525f36fe0a2SJohannes Berg 		 const u8 *addr, int link_id, const u8 *link_addr,
526f36fe0a2SJohannes Berg 		 gfp_t gfp)
527f0706e82SJiri Benc {
528d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
529ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
530f0706e82SJiri Benc 	struct sta_info *sta;
531107395f9SAlexander Wetzel 	void *txq_data;
532107395f9SAlexander Wetzel 	int size;
53316c5f15cSRon Rindjunsky 	int i;
534f0706e82SJiri Benc 
535ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
536f0706e82SJiri Benc 	if (!sta)
53773651ee6SJohannes Berg 		return NULL;
538f0706e82SJiri Benc 
539246b39e4SJohannes Berg 	sta->local = local;
540246b39e4SJohannes Berg 	sta->sdata = sdata;
541246b39e4SJohannes Berg 
542cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
54336fe8e4eSLorenzo Bianconi 		goto free;
544c9c5962bSJohannes Berg 
545cb71f1d1SJohannes Berg 	if (link_id >= 0) {
546cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
547cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
548cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
549cb71f1d1SJohannes Berg 	} else {
550cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
551cb71f1d1SJohannes Berg 	}
552cb71f1d1SJohannes Berg 
5534c51541dSBenjamin Berg 	sta->sta.cur = &sta->sta.deflink.agg;
5544c51541dSBenjamin Berg 
55507346f81SJohannes Berg 	spin_lock_init(&sta->lock);
5561d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
5575ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
55867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
559a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
56087f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
561433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
562433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
563433f5bc1SJohannes Berg 		if (!sta->mesh)
564433f5bc1SJohannes Berg 			goto free;
5654c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
566433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
56745d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5684c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5694c02d62fSKees Cook 				    0);
570433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
571433f5bc1SJohannes Berg 	}
57287f59c70SThomas Pedersen #endif
57307346f81SJohannes Berg 
574ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
57517741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
576f36fe0a2SJohannes Berg 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
577f36fe0a2SJohannes Berg 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
578480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
579480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
580480dd46bSMaxim Altshul 
581046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
582046d2e7cSSriram R 
58396fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
58496fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
58596fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
58696fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
58796fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
58896fc6efbSAlexander Wetzel 	 */
58996fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
59096fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
59196fc6efbSAlexander Wetzel 
5920f9c5a61SJohannes Berg 
5933a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5943a11ce08SJohannes Berg 
59571ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
59671ec375cSJohannes Berg 
5976e4c0d04SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
5986e4c0d04SFelix Fietkau 		sta->amsdu_mesh_control = -1;
5996e4c0d04SFelix Fietkau 
600b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
601b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
602b6da911bSLiad Kaufman 
60384b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
604541a45a1SBruno Randolf 
605107395f9SAlexander Wetzel 	size = sizeof(struct txq_info) +
606ba8c3d6fSFelix Fietkau 	       ALIGN(hw->txq_data_size, sizeof(void *));
607ba8c3d6fSFelix Fietkau 
608ba8c3d6fSFelix Fietkau 	txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
609ba8c3d6fSFelix Fietkau 	if (!txq_data)
610ba8c3d6fSFelix Fietkau 		goto free;
611ba8c3d6fSFelix Fietkau 
612ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
613ba8c3d6fSFelix Fietkau 		struct txq_info *txq = txq_data + i * size;
614ba8c3d6fSFelix Fietkau 
615107395f9SAlexander Wetzel 		/* might not do anything for the (bufferable) MMPDU TXQ */
616fa962b92SMichal Kazior 		ieee80211_txq_init(sdata, sta, txq, i);
617abfbc3afSJohannes Berg 	}
618ba8c3d6fSFelix Fietkau 
619ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
620ba8c3d6fSFelix Fietkau 		goto free_txq;
621f0706e82SJiri Benc 
622942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
623b4809e94SToke Høiland-Jørgensen 
624948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
625948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
626948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
627942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
628942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
629942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
630942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
631948d887dSJohannes Berg 	}
63273651ee6SJohannes Berg 
6335a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
6344be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
635cccaec98SSenthil Balasubramanian 
636bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
637bd718fc1SJohannes Berg 		u32 mandatory = 0;
638bd718fc1SJohannes Berg 		int r;
639bd718fc1SJohannes Berg 
640bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
641bd718fc1SJohannes Berg 			continue;
642bd718fc1SJohannes Berg 
643bd718fc1SJohannes Berg 		switch (i) {
644bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
64563fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
646bd718fc1SJohannes Berg 			/*
647bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
648bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
649bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
650bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
651bd718fc1SJohannes Berg 			 * possible rate anyway.
652bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
653bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
654bd718fc1SJohannes Berg 			 */
655bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
656bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
657bd718fc1SJohannes Berg 			break;
658bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
659bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
660bd718fc1SJohannes Berg 			break;
661bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
662bd718fc1SJohannes Berg 			WARN_ON(1);
663bd718fc1SJohannes Berg 			mandatory = 0;
664bd718fc1SJohannes Berg 			break;
665bd718fc1SJohannes Berg 		}
666bd718fc1SJohannes Berg 
667bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
668bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
669bd718fc1SJohannes Berg 
670bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
671bd718fc1SJohannes Berg 
672bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
673bd718fc1SJohannes Berg 				continue;
674046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
675bd718fc1SJohannes Berg 		}
676bd718fc1SJohannes Berg 	}
677bd718fc1SJohannes Berg 
678484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
679484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
680484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
681484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
682dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
683dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
684484a54c2SToke Høiland-Jørgensen 
685bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
686ef04a297SJohannes Berg 
687abfbc3afSJohannes Berg 	return sta;
688ba8c3d6fSFelix Fietkau 
689ba8c3d6fSFelix Fietkau free_txq:
690ba8c3d6fSFelix Fietkau 	kfree(to_txq_info(sta->sta.txq[0]));
691ba8c3d6fSFelix Fietkau free:
692cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
693433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
694433f5bc1SJohannes Berg 	kfree(sta->mesh);
695433f5bc1SJohannes Berg #endif
696ba8c3d6fSFelix Fietkau 	kfree(sta);
697ba8c3d6fSFelix Fietkau 	return NULL;
69873651ee6SJohannes Berg }
69973651ee6SJohannes Berg 
700f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
701f36fe0a2SJohannes Berg 				const u8 *addr, gfp_t gfp)
702f36fe0a2SJohannes Berg {
703f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
704f36fe0a2SJohannes Berg }
705f36fe0a2SJohannes Berg 
706f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
707f36fe0a2SJohannes Berg 					  const u8 *mld_addr,
708f36fe0a2SJohannes Berg 					  unsigned int link_id,
709f36fe0a2SJohannes Berg 					  const u8 *link_addr,
710f36fe0a2SJohannes Berg 					  gfp_t gfp)
711f36fe0a2SJohannes Berg {
712f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
713f36fe0a2SJohannes Berg }
714f36fe0a2SJohannes Berg 
7158c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
71634e89507SJohannes Berg {
71734e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
71834e89507SJohannes Berg 
71903e4497eSJohannes Berg 	/*
72003e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
72103e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
72203e4497eSJohannes Berg 	 * and another CPU turns off the net device.
72303e4497eSJohannes Berg 	 */
7248c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
7258c71df7aSGuy Eilam 		return -ENETDOWN;
72603e4497eSJohannes Berg 
727b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
728deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
7298c71df7aSGuy Eilam 		return -EINVAL;
7308c71df7aSGuy Eilam 
73183e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
73283e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
73383e7e4ceSHerbert Xu 	 * for correctness.
73431104891SJohannes Berg 	 */
73531104891SJohannes Berg 	rcu_read_lock();
73631104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
73731104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
73831104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
73931104891SJohannes Berg 		rcu_read_unlock();
74031104891SJohannes Berg 		return -ENOTUNIQ;
74131104891SJohannes Berg 	}
74231104891SJohannes Berg 	rcu_read_unlock();
74331104891SJohannes Berg 
7448c71df7aSGuy Eilam 	return 0;
74593e5deb1SJohannes Berg }
74644213b5eSJohannes Berg 
747f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
748f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
749f09603a2SJohannes Berg 				     struct sta_info *sta)
750f09603a2SJohannes Berg {
751f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
752f09603a2SJohannes Berg 	int err = 0;
753f09603a2SJohannes Berg 
754f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
755f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
756f09603a2SJohannes Berg 		if (err)
757f09603a2SJohannes Berg 			break;
758f09603a2SJohannes Berg 	}
759f09603a2SJohannes Berg 
760f09603a2SJohannes Berg 	if (!err) {
761a4ec45a4SJohannes Berg 		/*
762a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
763a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
764a4ec45a4SJohannes Berg 		 */
765a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
766f09603a2SJohannes Berg 			sta->uploaded = true;
767f09603a2SJohannes Berg 		return 0;
768f09603a2SJohannes Berg 	}
769f09603a2SJohannes Berg 
770f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
771bdcbd8e0SJohannes Berg 		sdata_info(sdata,
772bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
773bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
774f09603a2SJohannes Berg 		err = 0;
775f09603a2SJohannes Berg 	}
776f09603a2SJohannes Berg 
777f09603a2SJohannes Berg 	/* unwind on error */
778f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
779f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
780f09603a2SJohannes Berg 
781f09603a2SJohannes Berg 	return err;
782f09603a2SJohannes Berg }
783f09603a2SJohannes Berg 
784d405fd8cSGregory Greenman static void
785d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
786d405fd8cSGregory Greenman {
787d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
788d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
789d405fd8cSGregory Greenman 	struct sta_info *sta;
790d405fd8cSGregory Greenman 
791d405fd8cSGregory Greenman 	rcu_read_lock();
792d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
793d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
794d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
795d405fd8cSGregory Greenman 			continue;
796d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
797d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
798d405fd8cSGregory Greenman 			break;
799d405fd8cSGregory Greenman 		}
800d405fd8cSGregory Greenman 	}
801d405fd8cSGregory Greenman 	rcu_read_unlock();
802d405fd8cSGregory Greenman 
803d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
804d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
805d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
806d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
807d405fd8cSGregory Greenman 	}
808d405fd8cSGregory Greenman }
809d405fd8cSGregory Greenman 
81034e89507SJohannes Berg /*
8118c71df7aSGuy Eilam  * should be called with sta_mtx locked
8128c71df7aSGuy Eilam  * this function replaces the mutex lock
8138c71df7aSGuy Eilam  * with a RCU lock
8148c71df7aSGuy Eilam  */
8154d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
8168c71df7aSGuy Eilam {
8178c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
8188c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8190c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
8208c71df7aSGuy Eilam 	int err = 0;
8218c71df7aSGuy Eilam 
8228c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
8238c71df7aSGuy Eilam 
8247852e361SJohannes Berg 	/* check if STA exists already */
8257852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
8264d33960bSJohannes Berg 		err = -EEXIST;
8278f9dcc29SAhmed Zaki 		goto out_cleanup;
82834e89507SJohannes Berg 	}
82934e89507SJohannes Berg 
8300c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
8310c2e3842SKoen Vandeputte 	if (!sinfo) {
8320c2e3842SKoen Vandeputte 		err = -ENOMEM;
8338f9dcc29SAhmed Zaki 		goto out_cleanup;
8340c2e3842SKoen Vandeputte 	}
8350c2e3842SKoen Vandeputte 
8364d33960bSJohannes Berg 	local->num_sta++;
8374d33960bSJohannes Berg 	local->sta_generation++;
8384d33960bSJohannes Berg 	smp_mb();
8394d33960bSJohannes Berg 
8405108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
8415108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
8425108ca82SJohannes Berg 
8434d33960bSJohannes Berg 	/* make the station visible */
84462b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
84562b14b24SJohannes Berg 	if (err)
84662b14b24SJohannes Berg 		goto out_drop_sta;
8474d33960bSJohannes Berg 
848f36fe0a2SJohannes Berg 	if (sta->sta.valid_links) {
849f36fe0a2SJohannes Berg 		err = link_sta_info_hash_add(local, &sta->deflink);
850f36fe0a2SJohannes Berg 		if (err) {
851f36fe0a2SJohannes Berg 			sta_info_hash_del(local, sta);
852f36fe0a2SJohannes Berg 			goto out_drop_sta;
853f36fe0a2SJohannes Berg 		}
854f36fe0a2SJohannes Berg 	}
855f36fe0a2SJohannes Berg 
8562bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
85783d5cc01SJohannes Berg 
8584dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8594dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8604dde3c36SMordechay Goodstein 	 */
8614dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8620cbf348aSAndrei Otcheretianski 		ieee80211_recalc_min_chandef(sta->sdata, -1);
8634dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8644dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8654dde3c36SMordechay Goodstein 	}
8664dde3c36SMordechay Goodstein 
8675108ca82SJohannes Berg 	/* notify driver */
8685108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8695108ca82SJohannes Berg 	if (err)
8705108ca82SJohannes Berg 		goto out_remove;
8715108ca82SJohannes Berg 
87283d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
873d405fd8cSGregory Greenman 
8745108ca82SJohannes Berg 	/* accept BA sessions now */
8755108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8764d33960bSJohannes Berg 
8774d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8784d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
879d2caad52SBenjamin Berg 	if (sta->sta.valid_links) {
880d2caad52SBenjamin Berg 		int i;
881d2caad52SBenjamin Berg 
882d2caad52SBenjamin Berg 		for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
883d2caad52SBenjamin Berg 			struct link_sta_info *link_sta;
884d2caad52SBenjamin Berg 
885d2caad52SBenjamin Berg 			link_sta = rcu_dereference_protected(sta->link[i],
886d2caad52SBenjamin Berg 							     lockdep_is_held(&local->sta_mtx));
887d2caad52SBenjamin Berg 
888d2caad52SBenjamin Berg 			if (!link_sta)
889d2caad52SBenjamin Berg 				continue;
890d2caad52SBenjamin Berg 
891d2caad52SBenjamin Berg 			ieee80211_link_sta_debugfs_add(link_sta);
892d2caad52SBenjamin Berg 			if (sdata->vif.active_links & BIT(i))
893d2caad52SBenjamin Berg 				ieee80211_link_sta_debugfs_drv_add(link_sta);
894d2caad52SBenjamin Berg 		}
895d2caad52SBenjamin Berg 	} else {
896d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_add(&sta->deflink);
897d2caad52SBenjamin Berg 		ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
898d2caad52SBenjamin Berg 	}
8994d33960bSJohannes Berg 
9000ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
9010ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
9020ef049dcSArnd Bergmann 	kfree(sinfo);
903d0709a65SJohannes Berg 
904bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
905f0706e82SJiri Benc 
90634e89507SJohannes Berg 	/* move reference to rcu-protected */
90734e89507SJohannes Berg 	rcu_read_lock();
90834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
909e9f207f0SJiri Benc 
91073651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
91173651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
91273651ee6SJohannes Berg 
91373651ee6SJohannes Berg 	return 0;
9145108ca82SJohannes Berg  out_remove:
9150ad49045SJohannes Berg 	if (sta->sta.valid_links)
9160ad49045SJohannes Berg 		link_sta_info_hash_del(local, &sta->deflink);
9175108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
9185108ca82SJohannes Berg 	list_del_rcu(&sta->list);
91962b14b24SJohannes Berg  out_drop_sta:
9205108ca82SJohannes Berg 	local->num_sta--;
9215108ca82SJohannes Berg 	synchronize_net();
9228f9dcc29SAhmed Zaki  out_cleanup:
9237bc40aedSJohannes Berg 	cleanup_single_sta(sta);
9244d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
925ea32f065SSudip Mukherjee 	kfree(sinfo);
9264d33960bSJohannes Berg 	rcu_read_lock();
9274d33960bSJohannes Berg 	return err;
9288c71df7aSGuy Eilam }
9298c71df7aSGuy Eilam 
9308c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
9318c71df7aSGuy Eilam {
9328c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
933308f7fcfSZhao, Gang 	int err;
9348c71df7aSGuy Eilam 
9354d33960bSJohannes Berg 	might_sleep();
9364d33960bSJohannes Berg 
93731104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
93831104891SJohannes Berg 
9398c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
9408c71df7aSGuy Eilam 	if (err) {
9417bc40aedSJohannes Berg 		sta_info_free(local, sta);
94231104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
9438c71df7aSGuy Eilam 		rcu_read_lock();
9447bc40aedSJohannes Berg 		return err;
9458c71df7aSGuy Eilam 	}
9468c71df7aSGuy Eilam 
9477bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
948f0706e82SJiri Benc }
949f0706e82SJiri Benc 
95034e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
95134e89507SJohannes Berg {
95234e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
95334e89507SJohannes Berg 
95434e89507SJohannes Berg 	rcu_read_unlock();
95534e89507SJohannes Berg 
95634e89507SJohannes Berg 	return err;
95734e89507SJohannes Berg }
95834e89507SJohannes Berg 
959d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
960004c872eSJohannes Berg {
961004c872eSJohannes Berg 	/*
962004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
963004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
964004c872eSJohannes Berg 	 */
965d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
966004c872eSJohannes Berg }
967004c872eSJohannes Berg 
968d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
969004c872eSJohannes Berg {
970004c872eSJohannes Berg 	/*
971004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
972004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
973004c872eSJohannes Berg 	 */
974d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
975004c872eSJohannes Berg }
976004c872eSJohannes Berg 
9773d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9783d5839b6SIlan Peer {
9793d5839b6SIlan Peer 	/*
9803d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9813d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9823d5839b6SIlan Peer 	 */
9833d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9843d5839b6SIlan Peer }
9853d5839b6SIlan Peer 
986948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
987004c872eSJohannes Berg {
988948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
989948d887dSJohannes Berg 	switch (ac) {
990948d887dSJohannes Berg 	case IEEE80211_AC_VO:
991948d887dSJohannes Berg 		return BIT(6) | BIT(7);
992948d887dSJohannes Berg 	case IEEE80211_AC_VI:
993948d887dSJohannes Berg 		return BIT(4) | BIT(5);
994948d887dSJohannes Berg 	case IEEE80211_AC_BE:
995948d887dSJohannes Berg 		return BIT(0) | BIT(3);
996948d887dSJohannes Berg 	case IEEE80211_AC_BK:
997948d887dSJohannes Berg 		return BIT(1) | BIT(2);
998948d887dSJohannes Berg 	default:
999948d887dSJohannes Berg 		WARN_ON(1);
1000948d887dSJohannes Berg 		return 0;
1001d0709a65SJohannes Berg 	}
1002004c872eSJohannes Berg }
1003004c872eSJohannes Berg 
10049b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
1005004c872eSJohannes Berg {
1006c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
1007d012a605SMarco Porsch 	struct ps_data *ps;
1008948d887dSJohannes Berg 	bool indicate_tim = false;
1009948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
1010948d887dSJohannes Berg 	int ac;
1011a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
1012004c872eSJohannes Berg 
1013d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1014d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1015c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
1016c868cb35SJohannes Berg 			return;
10173e122be0SJohannes Berg 
1018d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
10193f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
10203f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
10213f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
10223f52b7e3SMarco Porsch #endif
1023d012a605SMarco Porsch 	} else {
1024d012a605SMarco Porsch 		return;
1025d012a605SMarco Porsch 	}
1026d012a605SMarco Porsch 
1027c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
1028d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1029c868cb35SJohannes Berg 		return;
1030004c872eSJohannes Berg 
1031c868cb35SJohannes Berg 	if (sta->dead)
1032c868cb35SJohannes Berg 		goto done;
10333e122be0SJohannes Berg 
1034948d887dSJohannes Berg 	/*
1035948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
1036948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
1037948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
1038948d887dSJohannes Berg 	 * non-enabled ones.
1039948d887dSJohannes Berg 	 */
1040948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1041948d887dSJohannes Berg 		ignore_for_tim = 0;
1042948d887dSJohannes Berg 
10439b7a86f3SJohannes Berg 	if (ignore_pending)
10449b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
10459b7a86f3SJohannes Berg 
1046948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1047948d887dSJohannes Berg 		unsigned long tids;
1048948d887dSJohannes Berg 
1049f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1050948d887dSJohannes Berg 			continue;
1051948d887dSJohannes Berg 
1052948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1053948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
1054948d887dSJohannes Berg 		if (indicate_tim)
1055948d887dSJohannes Berg 			break;
1056948d887dSJohannes Berg 
1057948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
1058948d887dSJohannes Berg 
1059948d887dSJohannes Berg 		indicate_tim |=
1060948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
1061ba8c3d6fSFelix Fietkau 		indicate_tim |=
1062ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
1063004c872eSJohannes Berg 	}
1064004c872eSJohannes Berg 
1065c868cb35SJohannes Berg  done:
106665f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
1067004c872eSJohannes Berg 
10683d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
10693d5839b6SIlan Peer 		goto out_unlock;
10703d5839b6SIlan Peer 
1071948d887dSJohannes Berg 	if (indicate_tim)
1072d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1073c868cb35SJohannes Berg 	else
1074d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10753e122be0SJohannes Berg 
10769b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1077c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1078948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1079c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1080004c872eSJohannes Berg 	}
1081004c872eSJohannes Berg 
10823d5839b6SIlan Peer out_unlock:
108365f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1084004c872eSJohannes Berg }
1085004c872eSJohannes Berg 
10869b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10879b7a86f3SJohannes Berg {
10889b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10899b7a86f3SJohannes Berg }
10909b7a86f3SJohannes Berg 
1091cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1092f0706e82SJiri Benc {
1093e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1094f0706e82SJiri Benc 	int timeout;
1095f0706e82SJiri Benc 
1096f0706e82SJiri Benc 	if (!skb)
1097cd0b8d89SJohannes Berg 		return false;
1098f0706e82SJiri Benc 
1099e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1100f0706e82SJiri Benc 
1101f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
110257c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
110357c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
110457c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1105f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1106f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1107e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1108f0706e82SJiri Benc }
1109f0706e82SJiri Benc 
1110f0706e82SJiri Benc 
1111948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1112948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1113f0706e82SJiri Benc {
1114f0706e82SJiri Benc 	unsigned long flags;
1115f0706e82SJiri Benc 	struct sk_buff *skb;
1116f0706e82SJiri Benc 
111760750397SJohannes Berg 	/*
111860750397SJohannes Berg 	 * First check for frames that should expire on the filtered
111960750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
112060750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
112160750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
112260750397SJohannes Berg 	 * total_ps_buffered counter.
112360750397SJohannes Berg 	 */
1124f0706e82SJiri Benc 	for (;;) {
1125948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1126948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
112757c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1128948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1129836341a7SJohannes Berg 		else
1130f0706e82SJiri Benc 			skb = NULL;
1131948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1132f0706e82SJiri Benc 
113360750397SJohannes Berg 		/*
113460750397SJohannes Berg 		 * Frames are queued in order, so if this one
113560750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
113660750397SJohannes Berg 		 * we actually reached the end of the queue we
113760750397SJohannes Berg 		 * also need to stop, of course.
113860750397SJohannes Berg 		 */
113960750397SJohannes Berg 		if (!skb)
114060750397SJohannes Berg 			break;
1141d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
114260750397SJohannes Berg 	}
114360750397SJohannes Berg 
114460750397SJohannes Berg 	/*
114560750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
114660750397SJohannes Berg 	 * only find something if the filtered queue was emptied
114760750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
114860750397SJohannes Berg 	 * buffered frames.
114960750397SJohannes Berg 	 */
1150f0706e82SJiri Benc 	for (;;) {
1151948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1152948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1153f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1154948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1155f0706e82SJiri Benc 		else
1156f0706e82SJiri Benc 			skb = NULL;
1157948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1158f0706e82SJiri Benc 
115960750397SJohannes Berg 		/*
116060750397SJohannes Berg 		 * frames are queued in order, so if this one
116160750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
116260750397SJohannes Berg 		 * the queue) we can stop testing
116360750397SJohannes Berg 		 */
1164836341a7SJohannes Berg 		if (!skb)
1165836341a7SJohannes Berg 			break;
1166836341a7SJohannes Berg 
1167f0706e82SJiri Benc 		local->total_ps_buffered--;
1168bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1169bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1170d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1171f0706e82SJiri Benc 	}
11723393a608SJuuso Oikarinen 
117360750397SJohannes Berg 	/*
117460750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
117560750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
117660750397SJohannes Berg 	 * frames.
117760750397SJohannes Berg 	 */
117860750397SJohannes Berg 	sta_info_recalc_tim(sta);
117960750397SJohannes Berg 
118060750397SJohannes Berg 	/*
118160750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
118260750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
118360750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
118460750397SJohannes Berg 	 */
1185948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1186948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1187948d887dSJohannes Berg }
1188948d887dSJohannes Berg 
1189948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1190948d887dSJohannes Berg 					     struct sta_info *sta)
1191948d887dSJohannes Berg {
1192948d887dSJohannes Berg 	bool have_buffered = false;
1193948d887dSJohannes Berg 	int ac;
1194948d887dSJohannes Berg 
11953f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11963f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11973f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1198948d887dSJohannes Berg 		return false;
1199948d887dSJohannes Berg 
1200948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1201948d887dSJohannes Berg 		have_buffered |=
1202948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1203948d887dSJohannes Berg 
1204948d887dSJohannes Berg 	return have_buffered;
1205f0706e82SJiri Benc }
1206f0706e82SJiri Benc 
1207d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
120834e89507SJohannes Berg {
120934e89507SJohannes Berg 	struct ieee80211_local *local;
121034e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
12110ad49045SJohannes Berg 	int ret, i;
121234e89507SJohannes Berg 
121334e89507SJohannes Berg 	might_sleep();
121434e89507SJohannes Berg 
121534e89507SJohannes Berg 	if (!sta)
121634e89507SJohannes Berg 		return -ENOENT;
121734e89507SJohannes Berg 
121834e89507SJohannes Berg 	local = sta->local;
121934e89507SJohannes Berg 	sdata = sta->sdata;
122034e89507SJohannes Berg 
122183d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
122283d5cc01SJohannes Berg 
1223098a6070SJohannes Berg 	/*
1224098a6070SJohannes Berg 	 * Before removing the station from the driver and
1225098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1226098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1227098a6070SJohannes Berg 	 * will be sufficient.
1228098a6070SJohannes Berg 	 */
1229c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1230c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1231098a6070SJohannes Berg 
1232f59374ebSSara Sharon 	/*
1233f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1234f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1235f59374ebSSara Sharon 	 * all such frames to be processed.
1236f59374ebSSara Sharon 	 */
1237f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1238f59374ebSSara Sharon 
12390ad49045SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
12400ad49045SJohannes Berg 		struct link_sta_info *link_sta;
12410ad49045SJohannes Berg 
12420ad49045SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
12430ad49045SJohannes Berg 			continue;
12440ad49045SJohannes Berg 
12450ad49045SJohannes Berg 		link_sta = rcu_dereference_protected(sta->link[i],
12460ad49045SJohannes Berg 						     lockdep_is_held(&local->sta_mtx));
12470ad49045SJohannes Berg 
12480ad49045SJohannes Berg 		link_sta_info_hash_del(local, link_sta);
12490ad49045SJohannes Berg 	}
12500ad49045SJohannes Berg 
125134e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1252b01711beSJohannes Berg 	if (WARN_ON(ret))
125334e89507SJohannes Berg 		return ret;
125434e89507SJohannes Berg 
1255a7a6bdd0SArik Nemtsov 	/*
1256a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1257a7a6bdd0SArik Nemtsov 	 * removal.
1258a7a6bdd0SArik Nemtsov 	 */
1259a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1260a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1261a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1262a7a6bdd0SArik Nemtsov 	}
1263a7a6bdd0SArik Nemtsov 
1264794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1265ef044763SEliad Peller 	sta->removed = true;
12664d33960bSJohannes Berg 
126712b220a6SFelix Fietkau 	if (sta->uploaded)
12686a9d1b91SJohannes Berg 		drv_sta_pre_rcu_remove(local, sta->sdata, sta);
12696a9d1b91SJohannes Berg 
1270a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1271a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1272a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1273a710c816SJohannes Berg 
1274d778207bSJohannes Berg 	return 0;
1275d778207bSJohannes Berg }
1276d778207bSJohannes Berg 
1277*92747f17SJohannes Berg static int _sta_info_move_state(struct sta_info *sta,
1278*92747f17SJohannes Berg 				enum ieee80211_sta_state new_state,
1279*92747f17SJohannes Berg 				bool recalc)
128010a7ba92SJohannes Berg {
128110a7ba92SJohannes Berg 	might_sleep();
128210a7ba92SJohannes Berg 
128310a7ba92SJohannes Berg 	if (sta->sta_state == new_state)
128410a7ba92SJohannes Berg 		return 0;
128510a7ba92SJohannes Berg 
128610a7ba92SJohannes Berg 	/* check allowed transitions first */
128710a7ba92SJohannes Berg 
128810a7ba92SJohannes Berg 	switch (new_state) {
128910a7ba92SJohannes Berg 	case IEEE80211_STA_NONE:
129010a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
129110a7ba92SJohannes Berg 			return -EINVAL;
129210a7ba92SJohannes Berg 		break;
129310a7ba92SJohannes Berg 	case IEEE80211_STA_AUTH:
129410a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
129510a7ba92SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
129610a7ba92SJohannes Berg 			return -EINVAL;
129710a7ba92SJohannes Berg 		break;
129810a7ba92SJohannes Berg 	case IEEE80211_STA_ASSOC:
129910a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
130010a7ba92SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
130110a7ba92SJohannes Berg 			return -EINVAL;
130210a7ba92SJohannes Berg 		break;
130310a7ba92SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
130410a7ba92SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
130510a7ba92SJohannes Berg 			return -EINVAL;
130610a7ba92SJohannes Berg 		break;
130710a7ba92SJohannes Berg 	default:
130810a7ba92SJohannes Berg 		WARN(1, "invalid state %d", new_state);
130910a7ba92SJohannes Berg 		return -EINVAL;
131010a7ba92SJohannes Berg 	}
131110a7ba92SJohannes Berg 
131210a7ba92SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
131310a7ba92SJohannes Berg 		sta->sta.addr, new_state);
131410a7ba92SJohannes Berg 
131510a7ba92SJohannes Berg 	/* notify the driver before the actual changes so it can
131610a7ba92SJohannes Berg 	 * fail the transition
131710a7ba92SJohannes Berg 	 */
131810a7ba92SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
131910a7ba92SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
132010a7ba92SJohannes Berg 					sta->sta_state, new_state);
132110a7ba92SJohannes Berg 		if (err)
132210a7ba92SJohannes Berg 			return err;
132310a7ba92SJohannes Berg 	}
132410a7ba92SJohannes Berg 
132510a7ba92SJohannes Berg 	/* reflect the change in all state variables */
132610a7ba92SJohannes Berg 
132710a7ba92SJohannes Berg 	switch (new_state) {
132810a7ba92SJohannes Berg 	case IEEE80211_STA_NONE:
132910a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
133010a7ba92SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
133110a7ba92SJohannes Berg 		break;
133210a7ba92SJohannes Berg 	case IEEE80211_STA_AUTH:
133310a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE) {
133410a7ba92SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
133510a7ba92SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
133610a7ba92SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1337*92747f17SJohannes Berg 			if (recalc) {
133810a7ba92SJohannes Berg 				ieee80211_recalc_min_chandef(sta->sdata, -1);
133910a7ba92SJohannes Berg 				if (!sta->sta.support_p2p_ps)
134010a7ba92SJohannes Berg 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
134110a7ba92SJohannes Berg 			}
1342*92747f17SJohannes Berg 		}
134310a7ba92SJohannes Berg 		break;
134410a7ba92SJohannes Berg 	case IEEE80211_STA_ASSOC:
134510a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
134610a7ba92SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
134710a7ba92SJohannes Berg 			sta->assoc_at = ktime_get_boottime_ns();
1348*92747f17SJohannes Berg 			if (recalc) {
134910a7ba92SJohannes Berg 				ieee80211_recalc_min_chandef(sta->sdata, -1);
135010a7ba92SJohannes Berg 				if (!sta->sta.support_p2p_ps)
135110a7ba92SJohannes Berg 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1352*92747f17SJohannes Berg 			}
135310a7ba92SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
135410a7ba92SJohannes Berg 			ieee80211_vif_dec_num_mcast(sta->sdata);
135510a7ba92SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
135610a7ba92SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
135710a7ba92SJohannes Berg 			ieee80211_clear_fast_rx(sta);
135810a7ba92SJohannes Berg 		}
135910a7ba92SJohannes Berg 		break;
136010a7ba92SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
136110a7ba92SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
136210a7ba92SJohannes Berg 			ieee80211_vif_inc_num_mcast(sta->sdata);
136310a7ba92SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
136410a7ba92SJohannes Berg 			ieee80211_check_fast_xmit(sta);
136510a7ba92SJohannes Berg 			ieee80211_check_fast_rx(sta);
136610a7ba92SJohannes Berg 		}
136710a7ba92SJohannes Berg 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
136810a7ba92SJohannes Berg 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
136910a7ba92SJohannes Berg 			cfg80211_send_layer2_update(sta->sdata->dev,
137010a7ba92SJohannes Berg 						    sta->sta.addr);
137110a7ba92SJohannes Berg 		break;
137210a7ba92SJohannes Berg 	default:
137310a7ba92SJohannes Berg 		break;
137410a7ba92SJohannes Berg 	}
137510a7ba92SJohannes Berg 
137610a7ba92SJohannes Berg 	sta->sta_state = new_state;
137710a7ba92SJohannes Berg 
137810a7ba92SJohannes Berg 	return 0;
137910a7ba92SJohannes Berg }
138010a7ba92SJohannes Berg 
1381*92747f17SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1382*92747f17SJohannes Berg 			enum ieee80211_sta_state new_state)
1383*92747f17SJohannes Berg {
1384*92747f17SJohannes Berg 	return _sta_info_move_state(sta, new_state, true);
1385*92747f17SJohannes Berg }
1386*92747f17SJohannes Berg 
1387*92747f17SJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc)
1388d778207bSJohannes Berg {
1389d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1390d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
13910ef049dcSArnd Bergmann 	struct station_info *sinfo;
1392d778207bSJohannes Berg 	int ret;
1393d778207bSJohannes Berg 
1394d778207bSJohannes Berg 	/*
1395d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1396d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1397d778207bSJohannes Berg 	 */
1398d778207bSJohannes Berg 
1399d778207bSJohannes Berg 	might_sleep();
1400d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1401d778207bSJohannes Berg 
14025981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1403*92747f17SJohannes Berg 		ret = _sta_info_move_state(sta, IEEE80211_STA_ASSOC, recalc);
1404b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1405b16798f5SJohannes Berg 	}
1406b16798f5SJohannes Berg 
14070b75a1b1SJohannes Berg 	/* Flush queues before removing keys, as that might remove them
14080b75a1b1SJohannes Berg 	 * from hardware, and then depending on the offload method, any
14090b75a1b1SJohannes Berg 	 * frames sitting on hardware queues might be sent out without
14100b75a1b1SJohannes Berg 	 * any encryption at all.
14110b75a1b1SJohannes Berg 	 */
1412d00800a2SJohannes Berg 	if (local->ops->set_key) {
1413d00800a2SJohannes Berg 		if (local->ops->flush_sta)
1414d00800a2SJohannes Berg 			drv_flush_sta(local, sta->sdata, sta);
1415d00800a2SJohannes Berg 		else
14160b75a1b1SJohannes Berg 			ieee80211_flush_queues(local, sta->sdata, false);
1417d00800a2SJohannes Berg 	}
14180b75a1b1SJohannes Berg 
1419c8782078SJohannes Berg 	/* now keys can no longer be reached */
14206d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
142134e89507SJohannes Berg 
14229b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
14239b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
14249b7a86f3SJohannes Berg 
142534e89507SJohannes Berg 	sta->dead = true;
142634e89507SJohannes Berg 
142734e89507SJohannes Berg 	local->num_sta--;
142834e89507SJohannes Berg 	local->sta_generation++;
142934e89507SJohannes Berg 
143083d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1431*92747f17SJohannes Berg 		ret = _sta_info_move_state(sta, sta->sta_state - 1, recalc);
1432f09603a2SJohannes Berg 		if (ret) {
143383d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
143483d5cc01SJohannes Berg 			break;
143583d5cc01SJohannes Berg 		}
143683d5cc01SJohannes Berg 	}
1437d9a7ddb0SJohannes Berg 
1438f09603a2SJohannes Berg 	if (sta->uploaded) {
1439f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1440f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1441f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1442f09603a2SJohannes Berg 	}
144334e89507SJohannes Berg 
1444bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1445bdcbd8e0SJohannes Berg 
14460ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
14470ef049dcSArnd Bergmann 	if (sinfo)
14480fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
14490ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
14500ef049dcSArnd Bergmann 	kfree(sinfo);
1451ec15e68bSJouni Malinen 
145234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
145334e89507SJohannes Berg 
14543a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
14553a11ce08SJohannes Berg 
1456d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1457d778207bSJohannes Berg }
1458d778207bSJohannes Berg 
1459d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1460d778207bSJohannes Berg {
1461d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1462d778207bSJohannes Berg 
1463d778207bSJohannes Berg 	if (err)
1464d778207bSJohannes Berg 		return err;
1465d778207bSJohannes Berg 
1466d778207bSJohannes Berg 	synchronize_net();
1467d778207bSJohannes Berg 
1468*92747f17SJohannes Berg 	__sta_info_destroy_part2(sta, true);
146934e89507SJohannes Berg 
147034e89507SJohannes Berg 	return 0;
147134e89507SJohannes Berg }
147234e89507SJohannes Berg 
147334e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
147434e89507SJohannes Berg {
147534e89507SJohannes Berg 	struct sta_info *sta;
147634e89507SJohannes Berg 	int ret;
147734e89507SJohannes Berg 
147834e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
14797852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
148034e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
148134e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
148234e89507SJohannes Berg 
148334e89507SJohannes Berg 	return ret;
148434e89507SJohannes Berg }
148534e89507SJohannes Berg 
148634e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
148734e89507SJohannes Berg 			      const u8 *addr)
148834e89507SJohannes Berg {
148934e89507SJohannes Berg 	struct sta_info *sta;
149034e89507SJohannes Berg 	int ret;
149134e89507SJohannes Berg 
149234e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
14937852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
149434e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
149534e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
149634e89507SJohannes Berg 
149734e89507SJohannes Berg 	return ret;
149834e89507SJohannes Berg }
1499f0706e82SJiri Benc 
150034f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1501f0706e82SJiri Benc {
150234f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1503f0706e82SJiri Benc 	struct sta_info *sta;
15043393a608SJuuso Oikarinen 	bool timer_needed = false;
1505f0706e82SJiri Benc 
1506d0709a65SJohannes Berg 	rcu_read_lock();
1507d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
15083393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
15093393a608SJuuso Oikarinen 			timer_needed = true;
1510d0709a65SJohannes Berg 	rcu_read_unlock();
1511f0706e82SJiri Benc 
15125bb644a0SJohannes Berg 	if (local->quiescing)
15135bb644a0SJohannes Berg 		return;
15145bb644a0SJohannes Berg 
15153393a608SJuuso Oikarinen 	if (!timer_needed)
15163393a608SJuuso Oikarinen 		return;
15173393a608SJuuso Oikarinen 
151826d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
151926d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1520f0706e82SJiri Benc }
1521f0706e82SJiri Benc 
15227bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
15237bedd0cfSJohannes Berg {
15247bedd0cfSJohannes Berg 	int err;
15257bedd0cfSJohannes Berg 
152683e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
15277bedd0cfSJohannes Berg 	if (err)
15287bedd0cfSJohannes Berg 		return err;
15297bedd0cfSJohannes Berg 
1530ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1531ba6ddab9SJohannes Berg 	if (err) {
1532ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1533ba6ddab9SJohannes Berg 		return err;
1534ba6ddab9SJohannes Berg 	}
1535ba6ddab9SJohannes Berg 
15364d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
153734e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1538f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1539f0706e82SJiri Benc 
154034f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
15417bedd0cfSJohannes Berg 	return 0;
1542f0706e82SJiri Benc }
1543f0706e82SJiri Benc 
1544f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1545f0706e82SJiri Benc {
1546a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
154783e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1548ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1549f0706e82SJiri Benc }
1550f0706e82SJiri Benc 
1551051007d9SJohannes Berg 
1552e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1553f0706e82SJiri Benc {
1554b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1555f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1556d778207bSJohannes Berg 	LIST_HEAD(free_list);
155744213b5eSJohannes Berg 	int ret = 0;
1558f0706e82SJiri Benc 
1559d0709a65SJohannes Berg 	might_sleep();
1560d0709a65SJohannes Berg 
1561e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1562e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1563e716251dSJohannes Berg 
156434e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
156534e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1566e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1567e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1568d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1569d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
157034316837SJohannes Berg 			ret++;
157134316837SJohannes Berg 		}
157234e89507SJohannes Berg 	}
1573d778207bSJohannes Berg 
1574d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1575*92747f17SJohannes Berg 		bool support_p2p_ps = true;
1576*92747f17SJohannes Berg 
1577d778207bSJohannes Berg 		synchronize_net();
1578*92747f17SJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list) {
1579*92747f17SJohannes Berg 			if (!sta->sta.support_p2p_ps)
1580*92747f17SJohannes Berg 				support_p2p_ps = false;
1581*92747f17SJohannes Berg 			__sta_info_destroy_part2(sta, false);
1582*92747f17SJohannes Berg 		}
1583*92747f17SJohannes Berg 
1584*92747f17SJohannes Berg 		ieee80211_recalc_min_chandef(sdata, -1);
1585*92747f17SJohannes Berg 		if (!support_p2p_ps)
1586*92747f17SJohannes Berg 			ieee80211_recalc_p2p_go_ps_allowed(sdata);
1587d778207bSJohannes Berg 	}
158834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
158944213b5eSJohannes Berg 
1590051007d9SJohannes Berg 	return ret;
1591051007d9SJohannes Berg }
1592051007d9SJohannes Berg 
159324723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
159424723d1bSJohannes Berg 			  unsigned long exp_time)
159524723d1bSJohannes Berg {
159624723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
159724723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
159824723d1bSJohannes Berg 
159934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1600e46a2cf9SMohammed Shafi Shajakhan 
1601e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1602b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1603b8da6b6aSJohannes Berg 
1604ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1605ec2b774eSMarek Lindner 			continue;
1606ec2b774eSMarek Lindner 
1607b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1608eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1609bdcbd8e0SJohannes Berg 				sta->sta.addr);
16103f52b7e3SMarco Porsch 
16113f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
16123f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
16133f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
16143f52b7e3SMarco Porsch 
161534e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
161624723d1bSJohannes Berg 		}
1617e46a2cf9SMohammed Shafi Shajakhan 	}
1618e46a2cf9SMohammed Shafi Shajakhan 
161934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
162024723d1bSJohannes Berg }
162117741cdcSJohannes Berg 
1622686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1623686b9cb9SBen Greear 						   const u8 *addr,
1624686b9cb9SBen Greear 						   const u8 *localaddr)
162517741cdcSJohannes Berg {
16267bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
162783e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
16287bedd0cfSJohannes Berg 	struct sta_info *sta;
162917741cdcSJohannes Berg 
1630686b9cb9SBen Greear 	/*
1631686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1632686b9cb9SBen Greear 	 * ... first in list.
1633686b9cb9SBen Greear 	 */
163483e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1635686b9cb9SBen Greear 		if (localaddr &&
1636b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1637686b9cb9SBen Greear 			continue;
1638f7c65594SJohannes Berg 		if (!sta->uploaded)
1639f7c65594SJohannes Berg 			return NULL;
164017741cdcSJohannes Berg 		return &sta->sta;
1641f7c65594SJohannes Berg 	}
1642f7c65594SJohannes Berg 
1643abe60632SJohannes Berg 	return NULL;
164417741cdcSJohannes Berg }
1645686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
16465ed176e1SJohannes Berg 
16475ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
16485ed176e1SJohannes Berg 					 const u8 *addr)
16495ed176e1SJohannes Berg {
1650f7c65594SJohannes Berg 	struct sta_info *sta;
16515ed176e1SJohannes Berg 
16525ed176e1SJohannes Berg 	if (!vif)
16535ed176e1SJohannes Berg 		return NULL;
16545ed176e1SJohannes Berg 
1655f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1656f7c65594SJohannes Berg 	if (!sta)
1657f7c65594SJohannes Berg 		return NULL;
16585ed176e1SJohannes Berg 
1659f7c65594SJohannes Berg 	if (!sta->uploaded)
1660f7c65594SJohannes Berg 		return NULL;
1661f7c65594SJohannes Berg 
1662f7c65594SJohannes Berg 	return &sta->sta;
16635ed176e1SJohannes Berg }
166417741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1665af818581SJohannes Berg 
1666e3685e03SJohannes Berg /* powersave support code */
1667e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
166850a9432dSJohannes Berg {
1669608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1670e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1671e3685e03SJohannes Berg 	struct sk_buff_head pending;
1672ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1673e3685e03SJohannes Berg 	unsigned long flags;
1674d012a605SMarco Porsch 	struct ps_data *ps;
1675d012a605SMarco Porsch 
16763918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
16773918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
16783918edb0SFelix Fietkau 				     u.ap);
16793918edb0SFelix Fietkau 
16803918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1681d012a605SMarco Porsch 		ps = &sdata->bss->ps;
16823f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
16833f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1684d012a605SMarco Porsch 	else
1685d012a605SMarco Porsch 		return;
168650a9432dSJohannes Berg 
1687c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
168847086fc5SJohannes Berg 
16895a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1690948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1691ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1692948d887dSJohannes Berg 
169330686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
169412375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1695af818581SJohannes Berg 
1696ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1697adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1698ba8c3d6fSFelix Fietkau 			continue;
1699ba8c3d6fSFelix Fietkau 
170018667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1701ba8c3d6fSFelix Fietkau 	}
1702ba8c3d6fSFelix Fietkau 
1703948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1704af818581SJohannes Berg 
17051d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
17061d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1707af818581SJohannes Berg 	/* Send all buffered frames to the station */
1708948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1709948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1710948d887dSJohannes Berg 
1711987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1712948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1713987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1714948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1715948d887dSJohannes Berg 		filtered += tmp - count;
1716948d887dSJohannes Berg 		count = tmp;
1717948d887dSJohannes Berg 
1718987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1719948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1720987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1721948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1722948d887dSJohannes Berg 		buffered += tmp - count;
1723948d887dSJohannes Berg 	}
1724948d887dSJohannes Berg 
1725e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
17265ac2e350SJohannes Berg 
17275ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
17285ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
17295ac2e350SJohannes Berg 
17305ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
17315ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
17325ac2e350SJohannes Berg 	 */
17335ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
17345ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
17351d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1736948d887dSJohannes Berg 
1737e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1738e3685e03SJohannes Berg 
1739af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1740af818581SJohannes Berg 
1741c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1742c868cb35SJohannes Berg 
1743bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
17442595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1745bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
174617c18bf8SJohannes Berg 
174717c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1748af818581SJohannes Berg }
1749af818581SJohannes Berg 
17500ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1751b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
17520ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1753ce662b44SJohannes Berg {
17540ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1755ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1756ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1757ce662b44SJohannes Berg 	struct sk_buff *skb;
1758ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1759ce662b44SJohannes Berg 	__le16 fc;
1760a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1761ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
176255de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1763ce662b44SJohannes Berg 
1764ce662b44SJohannes Berg 	if (qos) {
1765ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1766ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1767ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1768ce662b44SJohannes Berg 	} else {
1769ce662b44SJohannes Berg 		size -= 2;
1770ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1771ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1772ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1773ce662b44SJohannes Berg 	}
1774ce662b44SJohannes Berg 
1775ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1776ce662b44SJohannes Berg 	if (!skb)
1777ce662b44SJohannes Berg 		return;
1778ce662b44SJohannes Berg 
1779ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1780ce662b44SJohannes Berg 
17814df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1782ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1783ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1784ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1785ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1786ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1787864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1788ce662b44SJohannes Berg 
1789ce662b44SJohannes Berg 	skb->priority = tid;
1790ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
179159b66255SJohannes Berg 	if (qos) {
1792ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1793ce662b44SJohannes Berg 
17940ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1795ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1796ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
17970ead2510SEmmanuel Grumbach 			if (more_data)
17980ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
17990ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
18000ead2510SEmmanuel Grumbach 		}
1801ce662b44SJohannes Berg 	}
1802ce662b44SJohannes Berg 
1803ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1804ce662b44SJohannes Berg 
1805ce662b44SJohannes Berg 	/*
1806ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1807ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1808deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1809deeaee19SJohannes Berg 	 * ends the poll/service period.
1810ce662b44SJohannes Berg 	 */
181102f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1812deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1813ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1814ce662b44SJohannes Berg 
18156b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
18166b127c71SSujith Manoharan 
1817b77cf4f8SJohannes Berg 	if (call_driver)
1818b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1819b77cf4f8SJohannes Berg 					  reason, false);
182040b96408SJohannes Berg 
182189afe614SJohannes Berg 	skb->dev = sdata->dev;
182289afe614SJohannes Berg 
182355de908aSJohannes Berg 	rcu_read_lock();
1824d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
182555de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
182655de908aSJohannes Berg 		rcu_read_unlock();
182755de908aSJohannes Berg 		kfree_skb(skb);
182855de908aSJohannes Berg 		return;
182955de908aSJohannes Berg 	}
183055de908aSJohannes Berg 
183173c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
183208aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
183355de908aSJohannes Berg 	rcu_read_unlock();
1834ce662b44SJohannes Berg }
1835ce662b44SJohannes Berg 
18360a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
18370a1cb809SJohannes Berg {
18380a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
18390a1cb809SJohannes Berg 	if (tids & 0xF8)
18400a1cb809SJohannes Berg 		return fls(tids) - 1;
18410a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
18420a1cb809SJohannes Berg 	if (tids & BIT(0))
18430a1cb809SJohannes Berg 		return 0;
18440a1cb809SJohannes Berg 	return fls(tids) - 1;
18450a1cb809SJohannes Berg }
18460a1cb809SJohannes Berg 
18470ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
18480ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
18490ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
18500ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
18510ead2510SEmmanuel Grumbach  */
18520ead2510SEmmanuel Grumbach static bool
18530ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
18540ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
18550ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
18560ead2510SEmmanuel Grumbach {
18570ead2510SEmmanuel Grumbach 	int ac;
18580ead2510SEmmanuel Grumbach 
18590ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
18600ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
18610ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
18620ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
18630ead2510SEmmanuel Grumbach 	 */
18640ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
18650ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
18660ead2510SEmmanuel Grumbach 		return true;
18670ead2510SEmmanuel Grumbach 
18680ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1869f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
18700ead2510SEmmanuel Grumbach 			continue;
18710ead2510SEmmanuel Grumbach 
18720ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
18730ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
18740ead2510SEmmanuel Grumbach 			return true;
18750ead2510SEmmanuel Grumbach 	}
18760ead2510SEmmanuel Grumbach 
18770ead2510SEmmanuel Grumbach 	return false;
18780ead2510SEmmanuel Grumbach }
18790ead2510SEmmanuel Grumbach 
188047086fc5SJohannes Berg static void
18810ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
18820ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
18830ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
18840ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1885af818581SJohannes Berg {
1886af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1887af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1888948d887dSJohannes Berg 	int ac;
1889af818581SJohannes Berg 
1890f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1891948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
18924049e09aSJohannes Berg 		unsigned long tids;
18934049e09aSJohannes Berg 
1894f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1895948d887dSJohannes Berg 			continue;
1896948d887dSJohannes Berg 
18974049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
18984049e09aSJohannes Berg 
1899f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1900f9f760b4SJohannes Berg 		 * release from hardware queues
1901f9f760b4SJohannes Berg 		 */
19020ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
19030ead2510SEmmanuel Grumbach 			*driver_release_tids |=
19040ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
19050ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1906ba8c3d6fSFelix Fietkau 		}
1907f9f760b4SJohannes Berg 
19080ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
190947086fc5SJohannes Berg 			struct sk_buff *skb;
191047086fc5SJohannes Berg 
191147086fc5SJohannes Berg 			while (n_frames > 0) {
1912948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1913948d887dSJohannes Berg 				if (!skb) {
191447086fc5SJohannes Berg 					skb = skb_dequeue(
191547086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1916af818581SJohannes Berg 					if (skb)
1917af818581SJohannes Berg 						local->total_ps_buffered--;
1918af818581SJohannes Berg 				}
191947086fc5SJohannes Berg 				if (!skb)
192047086fc5SJohannes Berg 					break;
192147086fc5SJohannes Berg 				n_frames--;
19220ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
192347086fc5SJohannes Berg 			}
1924948d887dSJohannes Berg 		}
1925948d887dSJohannes Berg 
19260ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
19270ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
19280ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
19294049e09aSJohannes Berg 		 */
1930948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
19310ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1932948d887dSJohannes Berg 			break;
1933948d887dSJohannes Berg 	}
1934948d887dSJohannes Berg }
1935af818581SJohannes Berg 
19360ead2510SEmmanuel Grumbach static void
19370ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
19380ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
19390ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
19400ead2510SEmmanuel Grumbach {
19410ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
19420ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
19430ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
19440ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
19450ead2510SEmmanuel Grumbach 	bool more_data;
19460ead2510SEmmanuel Grumbach 
19470ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
19480ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
19490ead2510SEmmanuel Grumbach 
19500ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
19510ead2510SEmmanuel Grumbach 
19520ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
19530ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
19540ead2510SEmmanuel Grumbach 
19550ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
19560ead2510SEmmanuel Grumbach 
19571a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
19580ead2510SEmmanuel Grumbach 		driver_release_tids =
19590ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
19600ead2510SEmmanuel Grumbach 
1961f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1962f438ceb8SEmmanuel Grumbach 		int tid, ac;
19634049e09aSJohannes Berg 
1964ce662b44SJohannes Berg 		/*
1965ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1966ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1967ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1968ce662b44SJohannes Berg 		 *
1969ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1970ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1971ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1972ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1973ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1974ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1975ce662b44SJohannes Berg 		 *
1976ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1977ce662b44SJohannes Berg 		 */
1978ce662b44SJohannes Berg 
1979ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1980f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1981d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1982d7f84244SEmmanuel Grumbach 				break;
1983f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1984ce662b44SJohannes Berg 
19850ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1986f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
198747086fc5SJohannes Berg 		struct sk_buff_head pending;
198847086fc5SJohannes Berg 		struct sk_buff *skb;
198940b96408SJohannes Berg 		int num = 0;
199040b96408SJohannes Berg 		u16 tids = 0;
1991b77cf4f8SJohannes Berg 		bool need_null = false;
199247086fc5SJohannes Berg 
199347086fc5SJohannes Berg 		skb_queue_head_init(&pending);
199447086fc5SJohannes Berg 
199547086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1996af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
199747086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
199840b96408SJohannes Berg 			u8 *qoshdr = NULL;
199940b96408SJohannes Berg 
200040b96408SJohannes Berg 			num++;
2001af818581SJohannes Berg 
2002af818581SJohannes Berg 			/*
200347086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
200447086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
200547086fc5SJohannes Berg 			 * exchange.
2006af818581SJohannes Berg 			 */
20076b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
20086b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
2009af818581SJohannes Berg 
201047086fc5SJohannes Berg 			/*
201147086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
201247086fc5SJohannes Berg 			 * more buffered frames for this STA
201347086fc5SJohannes Berg 			 */
201424b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
201547086fc5SJohannes Berg 				hdr->frame_control |=
201647086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
201724b9c373SJanusz.Dziedzic@tieto.com 			else
201824b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
201924b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
2020af818581SJohannes Berg 
202140b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
202240b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
202340b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
202440b96408SJohannes Berg 
2025b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
2026b77cf4f8SJohannes Berg 
2027b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
2028b77cf4f8SJohannes Berg 
2029b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
2030b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
2031b77cf4f8SJohannes Berg 				continue;
2032b77cf4f8SJohannes Berg 
2033b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
2034b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
2035b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
2036b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2037b77cf4f8SJohannes Berg 				break;
2038b77cf4f8SJohannes Berg 			}
2039b77cf4f8SJohannes Berg 
2040b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
2041b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
2042b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
2043b77cf4f8SJohannes Berg 			 * and be done.
2044b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
2045b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
2046b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
2047b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
2048b77cf4f8SJohannes Berg 			 *
2049b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
2050b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
2051b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
2052b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
2053b77cf4f8SJohannes Berg 			 *
2054b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
2055b77cf4f8SJohannes Berg 			 */
2056b77cf4f8SJohannes Berg 			if (qoshdr) {
205740b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
2058deeaee19SJohannes Berg 
205947086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
206047086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2061b77cf4f8SJohannes Berg 			} else {
2062b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
2063b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
2064b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
2065b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
2066b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
2067b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
2068b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
2069b77cf4f8SJohannes Berg 				 */
2070b77cf4f8SJohannes Berg 				hdr->frame_control |=
2071b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2072b77cf4f8SJohannes Berg 				need_null = true;
2073b77cf4f8SJohannes Berg 				num++;
207452a3f20cSMarco Porsch 			}
2075b77cf4f8SJohannes Berg 			break;
207647086fc5SJohannes Berg 		}
207747086fc5SJohannes Berg 
207840b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
207940b96408SJohannes Berg 					  reason, more_data);
208040b96408SJohannes Berg 
208147086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
2082af818581SJohannes Berg 
2083b77cf4f8SJohannes Berg 		if (need_null)
2084b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
20850ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
20860ead2510SEmmanuel Grumbach 				reason, false, false);
2087b77cf4f8SJohannes Berg 
2088c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
2089af818581SJohannes Berg 	} else {
2090ba8c3d6fSFelix Fietkau 		int tid;
2091ba8c3d6fSFelix Fietkau 
2092af818581SJohannes Berg 		/*
20934049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
20944049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
2095f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
2096f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
2097f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
2098f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
2099f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
2100f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
2101af818581SJohannes Berg 		 */
21024049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
210347086fc5SJohannes Berg 					    n_frames, reason, more_data);
21044049e09aSJohannes Berg 
21054049e09aSJohannes Berg 		/*
21064049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
21074049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
2108f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
21094049e09aSJohannes Berg 		 * release function.
2110f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
2111ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
2112ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
21134049e09aSJohannes Berg 		 */
2114ba8c3d6fSFelix Fietkau 
2115ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
2116adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
2117adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
21181e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
2119ba8c3d6fSFelix Fietkau 				continue;
2120ba8c3d6fSFelix Fietkau 
2121ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
2122ba8c3d6fSFelix Fietkau 			break;
2123ba8c3d6fSFelix Fietkau 		}
2124af818581SJohannes Berg 	}
2125af818581SJohannes Berg }
2126af818581SJohannes Berg 
2127af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
2128af818581SJohannes Berg {
212947086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
2130af818581SJohannes Berg 
2131af818581SJohannes Berg 	/*
213247086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
213347086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
213447086fc5SJohannes Berg 	 * only from the non-enabled ones.
2135af818581SJohannes Berg 	 */
213647086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
213747086fc5SJohannes Berg 		ignore_for_response = 0;
2138af818581SJohannes Berg 
213947086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
214047086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
2141af818581SJohannes Berg }
214247086fc5SJohannes Berg 
214347086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
214447086fc5SJohannes Berg {
214547086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
214647086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
214747086fc5SJohannes Berg 
214847086fc5SJohannes Berg 	/*
214947086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
215047086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
215147086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
215247086fc5SJohannes Berg 	 * actually getting called.
215347086fc5SJohannes Berg 	 */
215447086fc5SJohannes Berg 	if (!delivery_enabled)
215547086fc5SJohannes Berg 		return;
215647086fc5SJohannes Berg 
215747086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
215847086fc5SJohannes Berg 	case 1:
215947086fc5SJohannes Berg 		n_frames = 2;
216047086fc5SJohannes Berg 		break;
216147086fc5SJohannes Berg 	case 2:
216247086fc5SJohannes Berg 		n_frames = 4;
216347086fc5SJohannes Berg 		break;
216447086fc5SJohannes Berg 	case 3:
216547086fc5SJohannes Berg 		n_frames = 6;
216647086fc5SJohannes Berg 		break;
216747086fc5SJohannes Berg 	case 0:
216847086fc5SJohannes Berg 		/* XXX: what is a good value? */
216913a8098aSAndrei Otcheretianski 		n_frames = 128;
217047086fc5SJohannes Berg 		break;
217147086fc5SJohannes Berg 	}
217247086fc5SJohannes Berg 
217347086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
217447086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
2175af818581SJohannes Berg }
2176af818581SJohannes Berg 
2177af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2178af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
2179af818581SJohannes Berg {
2180af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2181af818581SJohannes Berg 
2182b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
2183b5878a2dSJohannes Berg 
21845ac2e350SJohannes Berg 	if (block) {
2185c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
218617c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
21875ac2e350SJohannes Berg 		return;
21885ac2e350SJohannes Berg 	}
21895ac2e350SJohannes Berg 
21905ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
21915ac2e350SJohannes Berg 		return;
21925ac2e350SJohannes Berg 
21935ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
21945ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
21955ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
21965ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
21975ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
21985ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
21995ac2e350SJohannes Berg 		/* must be asleep in this case */
22005ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
22015ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
22025ac2e350SJohannes Berg 	} else {
22035ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
220417c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
22055ac2e350SJohannes Berg 	}
2206af818581SJohannes Berg }
2207af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
2208dcf55fb5SFelix Fietkau 
2209e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
221037fbd908SJohannes Berg {
221137fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
221237fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
221337fbd908SJohannes Berg 
221437fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
221537fbd908SJohannes Berg 
221637fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
221737fbd908SJohannes Berg }
2218e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
221937fbd908SJohannes Berg 
22200ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
22210ead2510SEmmanuel Grumbach {
22220ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
22230ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
22240ead2510SEmmanuel Grumbach 	bool more_data;
22250ead2510SEmmanuel Grumbach 
22260ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
22270ead2510SEmmanuel Grumbach 
22280ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
22290ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
22300ead2510SEmmanuel Grumbach 					       reason, 0);
22310ead2510SEmmanuel Grumbach 
22320ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
22330ead2510SEmmanuel Grumbach }
22340ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
22350ead2510SEmmanuel Grumbach 
2236042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2237042ec453SJohannes Berg 				u8 tid, bool buffered)
2238dcf55fb5SFelix Fietkau {
2239dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2240dcf55fb5SFelix Fietkau 
22415a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2242042ec453SJohannes Berg 		return;
2243042ec453SJohannes Berg 
22441b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
22451b000789SJohannes Berg 
2246948d887dSJohannes Berg 	if (buffered)
2247948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2248948d887dSJohannes Berg 	else
2249948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2250948d887dSJohannes Berg 
2251c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2252dcf55fb5SFelix Fietkau }
2253042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2254d9a7ddb0SJohannes Berg 
2255b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2256b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2257b4809e94SToke Høiland-Jørgensen {
2258942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2259942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2260942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2261942741daSFelix Fietkau 	u32 airtime = 0;
2262c77bfab9SFelix Fietkau 	u32 diff;
2263b4809e94SToke Høiland-Jørgensen 
2264942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2265942741daSFelix Fietkau 		airtime += tx_airtime;
2266942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2267942741daSFelix Fietkau 		airtime += rx_airtime;
2268b4809e94SToke Høiland-Jørgensen 
2269942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2270942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2271942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2272c77bfab9SFelix Fietkau 
2273c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2274c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2275942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2276c77bfab9SFelix Fietkau 
2277942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2278b4809e94SToke Høiland-Jørgensen }
2279b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2280b4809e94SToke Høiland-Jørgensen 
22819b41a9d7SJohannes Berg void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
22824c51541dSBenjamin Berg {
22834c51541dSBenjamin Berg 	bool first = true;
22849b41a9d7SJohannes Berg 	int link_id;
22854c51541dSBenjamin Berg 
22869b41a9d7SJohannes Berg 	if (!sta->sta.valid_links || !sta->sta.mlo) {
22879b41a9d7SJohannes Berg 		sta->sta.cur = &sta->sta.deflink.agg;
22884c51541dSBenjamin Berg 		return;
22894c51541dSBenjamin Berg 	}
22904c51541dSBenjamin Berg 
22914c51541dSBenjamin Berg 	rcu_read_lock();
22929b41a9d7SJohannes Berg 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
22939b41a9d7SJohannes Berg 		struct ieee80211_link_sta *link_sta;
22949b41a9d7SJohannes Berg 		int i;
22959b41a9d7SJohannes Berg 
22969b41a9d7SJohannes Berg 		if (!(active_links & BIT(link_id)))
22979b41a9d7SJohannes Berg 			continue;
22989b41a9d7SJohannes Berg 
22999b41a9d7SJohannes Berg 		link_sta = rcu_dereference(sta->sta.link[link_id]);
23009b41a9d7SJohannes Berg 		if (!link_sta)
23019b41a9d7SJohannes Berg 			continue;
23029b41a9d7SJohannes Berg 
23034c51541dSBenjamin Berg 		if (first) {
23049b41a9d7SJohannes Berg 			sta->cur = sta->sta.deflink.agg;
23054c51541dSBenjamin Berg 			first = false;
23064c51541dSBenjamin Berg 			continue;
23074c51541dSBenjamin Berg 		}
23084c51541dSBenjamin Berg 
23094c51541dSBenjamin Berg 		sta->cur.max_amsdu_len =
23104c51541dSBenjamin Berg 			min(sta->cur.max_amsdu_len,
23114c51541dSBenjamin Berg 			    link_sta->agg.max_amsdu_len);
23124c51541dSBenjamin Berg 		sta->cur.max_rc_amsdu_len =
23134c51541dSBenjamin Berg 			min(sta->cur.max_rc_amsdu_len,
23144c51541dSBenjamin Berg 			    link_sta->agg.max_rc_amsdu_len);
23154c51541dSBenjamin Berg 
23164c51541dSBenjamin Berg 		for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
23174c51541dSBenjamin Berg 			sta->cur.max_tid_amsdu_len[i] =
23184c51541dSBenjamin Berg 				min(sta->cur.max_tid_amsdu_len[i],
23194c51541dSBenjamin Berg 				    link_sta->agg.max_tid_amsdu_len[i]);
23204c51541dSBenjamin Berg 	}
23214c51541dSBenjamin Berg 	rcu_read_unlock();
23224c51541dSBenjamin Berg 
23239b41a9d7SJohannes Berg 	sta->sta.cur = &sta->cur;
23249b41a9d7SJohannes Berg }
23259b41a9d7SJohannes Berg 
23269b41a9d7SJohannes Berg void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
23279b41a9d7SJohannes Berg {
23289b41a9d7SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
23299b41a9d7SJohannes Berg 
23309b41a9d7SJohannes Berg 	__ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
23314c51541dSBenjamin Berg }
23324c51541dSBenjamin Berg EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
23334c51541dSBenjamin Berg 
23343ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
23353ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
23363ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
23373ace10f5SKan Yan {
23383ace10f5SKan Yan 	int tx_pending;
23393ace10f5SKan Yan 
2340911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2341911bde0fSToke Høiland-Jørgensen 		return;
2342911bde0fSToke Høiland-Jørgensen 
23433ace10f5SKan Yan 	if (!tx_completed) {
23443ace10f5SKan Yan 		if (sta)
23453ace10f5SKan Yan 			atomic_add(tx_airtime,
23463ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
23473ace10f5SKan Yan 
23483ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
23498e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
23503ace10f5SKan Yan 		return;
23513ace10f5SKan Yan 	}
23523ace10f5SKan Yan 
23533ace10f5SKan Yan 	if (sta) {
23543ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
23553ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
235604e35caaSFelix Fietkau 		if (tx_pending < 0)
23573ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
23583ace10f5SKan Yan 				       tx_pending, 0);
23593ace10f5SKan Yan 	}
23603ace10f5SKan Yan 
23618e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
23623ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
23638e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
23643ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
23653ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
23663ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
23678e4bac06SFelix Fietkau 		      tx_airtime)) {
23688e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
23693ace10f5SKan Yan 			       tx_pending, 0);
23708e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
23718e4bac06SFelix Fietkau 	}
23723ace10f5SKan Yan }
23733ace10f5SKan Yan 
2374c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2375c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2376c9c5962bSJohannes Berg {
2377046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2378c9c5962bSJohannes Berg 	int cpu;
2379c9c5962bSJohannes Berg 
2380046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2381c9c5962bSJohannes Berg 		return stats;
2382c9c5962bSJohannes Berg 
2383c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2384c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2385c9c5962bSJohannes Berg 
2386046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2387c9c5962bSJohannes Berg 
2388c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2389c9c5962bSJohannes Berg 			stats = cpustats;
2390c9c5962bSJohannes Berg 	}
2391c9c5962bSJohannes Berg 
2392c9c5962bSJohannes Berg 	return stats;
2393c9c5962bSJohannes Berg }
2394c9c5962bSJohannes Berg 
239541cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
23964f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2397fbd6ff5cSJohannes Berg {
2398dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2399fbd6ff5cSJohannes Berg 
2400dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
24017f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
24024f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2403dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2404dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2405dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2406dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
24077f406cd1SJohannes Berg 		break;
24087f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
24094f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2410dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2411dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2412dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
24137f406cd1SJohannes Berg 		break;
24147f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2415fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2416fbd6ff5cSJohannes Berg 		u16 brate;
24174f6b1b3dSJohannes Berg 		unsigned int shift;
2418dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2419dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2420fbd6ff5cSJohannes Berg 
2421dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
24228b783d10SThomas Pedersen 
24238b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
24248b783d10SThomas Pedersen 			break;
24258b783d10SThomas Pedersen 
2426dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
24274f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
24284f6b1b3dSJohannes Berg 			shift = 2;
24294f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
24304f6b1b3dSJohannes Berg 			shift = 1;
24314f6b1b3dSJohannes Berg 		else
24324f6b1b3dSJohannes Berg 			shift = 0;
2433fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
24347f406cd1SJohannes Berg 		break;
24357f406cd1SJohannes Berg 		}
243641cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
243741cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
243841cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
243941cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
244041cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
244141cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
244241cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
244341cbb0f5SLuca Coelho 		break;
2444f66c48afSJohannes Berg 	case STA_STATS_RATE_TYPE_EHT:
2445f66c48afSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_EHT_MCS;
2446f66c48afSJohannes Berg 		rinfo->mcs = STA_STATS_GET(EHT_MCS, rate);
2447f66c48afSJohannes Berg 		rinfo->nss = STA_STATS_GET(EHT_NSS, rate);
2448f66c48afSJohannes Berg 		rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate);
2449f66c48afSJohannes Berg 		rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate);
2450f66c48afSJohannes Berg 		break;
2451fbd6ff5cSJohannes Berg 	}
24524f6b1b3dSJohannes Berg }
2453fbd6ff5cSJohannes Berg 
2454a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
24554f6b1b3dSJohannes Berg {
245659336e07SShayne Chen 	u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
24574f6b1b3dSJohannes Berg 
24584f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2459a17d93ffSBen Greear 		return -EINVAL;
2460a17d93ffSBen Greear 
24614f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2462a17d93ffSBen Greear 	return 0;
2463fbd6ff5cSJohannes Berg }
2464fbd6ff5cSJohannes Berg 
2465b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2466b255b72bSSeevalamuthu Mariappan 					int tid)
2467b255b72bSSeevalamuthu Mariappan {
2468b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2469b255b72bSSeevalamuthu Mariappan 	u64 value;
2470b255b72bSSeevalamuthu Mariappan 
2471b255b72bSSeevalamuthu Mariappan 	do {
2472d120d1a6SThomas Gleixner 		start = u64_stats_fetch_begin(&rxstats->syncp);
2473b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2474d120d1a6SThomas Gleixner 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2475b255b72bSSeevalamuthu Mariappan 
2476b255b72bSSeevalamuthu Mariappan 	return value;
2477b255b72bSSeevalamuthu Mariappan }
2478b255b72bSSeevalamuthu Mariappan 
24790f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
24800f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
24810f9c5a61SJohannes Berg 			     int tid)
24820f9c5a61SJohannes Berg {
24830f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2484b255b72bSSeevalamuthu Mariappan 	int cpu;
24850f9c5a61SJohannes Berg 
24860f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2487046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2488046d2e7cSSriram R 							   tid);
24890f9c5a61SJohannes Berg 
2490046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2491b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2492b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2493b255b72bSSeevalamuthu Mariappan 
2494046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2495046d2e7cSSriram R 						     cpu);
2496b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2497b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2498b255b72bSSeevalamuthu Mariappan 			}
2499b255b72bSSeevalamuthu Mariappan 		}
25000f9c5a61SJohannes Berg 
25010f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
25020f9c5a61SJohannes Berg 	}
25030f9c5a61SJohannes Berg 
25040f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
25050f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2506046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
25070f9c5a61SJohannes Berg 	}
25080f9c5a61SJohannes Berg 
25090f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
25100f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
25110f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2512046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
25130f9c5a61SJohannes Berg 	}
25140f9c5a61SJohannes Berg 
25150f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
25160f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
25170f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2518046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
25190f9c5a61SJohannes Berg 	}
25202fe4a29aSToke Høiland-Jørgensen 
2521107395f9SAlexander Wetzel 	if (tid < IEEE80211_NUM_TIDS) {
25222fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
25232fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
25242fe4a29aSToke Høiland-Jørgensen 
25252fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
25262fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
25272fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
25282fe4a29aSToke Høiland-Jørgensen 
25292fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
25302fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
25312fe4a29aSToke Høiland-Jørgensen 	}
25320f9c5a61SJohannes Berg }
25330f9c5a61SJohannes Berg 
2534c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2535c9c5962bSJohannes Berg {
2536c9c5962bSJohannes Berg 	unsigned int start;
2537c9c5962bSJohannes Berg 	u64 value;
2538c9c5962bSJohannes Berg 
2539c9c5962bSJohannes Berg 	do {
2540d120d1a6SThomas Gleixner 		start = u64_stats_fetch_begin(&rxstats->syncp);
2541c9c5962bSJohannes Berg 		value = rxstats->bytes;
2542d120d1a6SThomas Gleixner 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2543c9c5962bSJohannes Berg 
2544c9c5962bSJohannes Berg 	return value;
2545c9c5962bSJohannes Berg }
2546c9c5962bSJohannes Berg 
25470fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
25480fdf1493SJohannes Berg 		   bool tidstats)
2549b7ffbd7eSJohannes Berg {
2550b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2551b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2552b7ffbd7eSJohannes Berg 	u32 thr = 0;
2553c9c5962bSJohannes Berg 	int i, ac, cpu;
2554c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2555c9c5962bSJohannes Berg 
2556c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2557b7ffbd7eSJohannes Berg 
2558b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2559b7ffbd7eSJohannes Berg 
2560225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2561225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2562225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2563225b8189SJohannes Berg 	 */
2564225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2565bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2566225b8189SJohannes Berg 
25672b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2568a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2569a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2570a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2571a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
25729cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2573a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2574976bd9efSJohannes Berg 
2575976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2576bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2577bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2578a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2579976bd9efSJohannes Berg 	}
2580b7ffbd7eSJohannes Berg 
258184b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
25829cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2583e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2584b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
25852b9a7e1bSJohannes Berg 
2586a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2587a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2588b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
25892b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2590046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2591a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2592b7ffbd7eSJohannes Berg 	}
25932b9a7e1bSJohannes Berg 
2594a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
25952b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
25962b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2597046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2598a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
25992b9a7e1bSJohannes Berg 	}
26002b9a7e1bSJohannes Berg 
2601a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2602a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2603046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
26040f9c5a61SJohannes Berg 
2605046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2606c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2607c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2608c9c5962bSJohannes Berg 
2609046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2610046d2e7cSSriram R 						     cpu);
2611c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2612c9c5962bSJohannes Berg 			}
2613c9c5962bSJohannes Berg 		}
2614c9c5962bSJohannes Berg 
2615a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
26162b9a7e1bSJohannes Berg 	}
26172b9a7e1bSJohannes Berg 
2618a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2619046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2620046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2621c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2622c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2623c9c5962bSJohannes Berg 
2624046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2625046d2e7cSSriram R 						     cpu);
2626c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2627c9c5962bSJohannes Berg 			}
2628c9c5962bSJohannes Berg 		}
2629a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
26302b9a7e1bSJohannes Berg 	}
26312b9a7e1bSJohannes Berg 
2632a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2633046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2634a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
26352b9a7e1bSJohannes Berg 	}
26362b9a7e1bSJohannes Berg 
2637a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2638046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2639a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
26402b9a7e1bSJohannes Berg 	}
26412b9a7e1bSJohannes Berg 
2642b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2643b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2644b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2645b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2646b4809e94SToke Høiland-Jørgensen 	}
2647b4809e94SToke Høiland-Jørgensen 
2648b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2649b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2650b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2651b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2652b4809e94SToke Høiland-Jørgensen 	}
2653b4809e94SToke Høiland-Jørgensen 
2654b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2655942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2656b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2657b4809e94SToke Høiland-Jørgensen 	}
2658b4809e94SToke Høiland-Jørgensen 
2659046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2660046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2661c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2662c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2663c9c5962bSJohannes Berg 
2664046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2665e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2666c9c5962bSJohannes Berg 		}
2667c9c5962bSJohannes Berg 	}
2668b7ffbd7eSJohannes Berg 
2669225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2670225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2671a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2672a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2673225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2674225b8189SJohannes Berg 	}
2675225b8189SJohannes Berg 
267630686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
267730686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2678a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2679c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2680a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2681b7ffbd7eSJohannes Berg 		}
26822b9a7e1bSJohannes Berg 
2683046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2684a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
268540d9a38aSJohannes Berg 			sinfo->signal_avg =
2686046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2687a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
26882b9a7e1bSJohannes Berg 		}
26892b9a7e1bSJohannes Berg 	}
26902b9a7e1bSJohannes Berg 
2691c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2692c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2693c9c5962bSJohannes Berg 	 * pcpu statistics
2694c9c5962bSJohannes Berg 	 */
2695c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2696a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2697a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2698a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2699046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2700a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2701b7ffbd7eSJohannes Berg 
2702c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2703c9c5962bSJohannes Berg 
2704b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2705e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2706c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2707b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2708046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2709b7ffbd7eSJohannes Berg 		}
2710b7ffbd7eSJohannes Berg 	}
2711b7ffbd7eSJohannes Berg 
27128a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
27138a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2714046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2715e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2716a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
27172b9a7e1bSJohannes Berg 	}
27182b9a7e1bSJohannes Berg 
27198a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
27208a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2721a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2722a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
27232b9a7e1bSJohannes Berg 	}
2724b7ffbd7eSJohannes Berg 
27250fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
27266af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
27276af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
27288689c051SArend van Spriel 	}
272979c892b8SJohannes Berg 
2730b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2731b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2732a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2733a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2734a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2735a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2736a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2737dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
27381303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
27391303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2740b7ffbd7eSJohannes Berg 
2741433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2742433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2743433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2744b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2745a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2746433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2747b7ffbd7eSJohannes Berg 		}
2748433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2749433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2750433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2751dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
27521303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2753b7ffbd7eSJohannes Berg #endif
2754b7ffbd7eSJohannes Berg 	}
2755b7ffbd7eSJohannes Berg 
2756b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2757b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2758b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2759b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2760b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2761b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2762b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2763785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2764b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2765b7ffbd7eSJohannes Berg 
2766b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2767b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2768b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2769b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2770b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2771b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2772b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2773b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2774b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2775b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2776b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2777b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2778a74a8c84SJohannes Berg 	if (sta->sta.wme)
2779b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2780b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2781b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2782b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2783b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2784b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2785b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2786b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2787b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2788b7ffbd7eSJohannes Berg 
27893b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
27903b17fbf8SMaxim Altshul 
27913b17fbf8SMaxim Altshul 	if (thr != 0) {
2792a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
27933b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
27943b17fbf8SMaxim Altshul 	}
2795a78b26ffSVenkateswara Naralasetty 
2796a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2797046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2798046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2799a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2800a78b26ffSVenkateswara Naralasetty 	}
2801cc60dbbfSBalaji Pothunoori 
28029c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2803046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2804cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2805cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2806046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2807cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
28089c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2809cc60dbbfSBalaji Pothunoori 	}
2810ab60633cSNarayanraddi Masti 
2811ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2812ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2813ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2814ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2815ab60633cSNarayanraddi Masti 	}
28163b17fbf8SMaxim Altshul }
28173b17fbf8SMaxim Altshul 
28183b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
28193b17fbf8SMaxim Altshul {
28203b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
28213b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
28223b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
28233b17fbf8SMaxim Altshul 	u32 thr = 0;
28243b17fbf8SMaxim Altshul 
28253b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
28263b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
28273b17fbf8SMaxim Altshul 
2828b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2829b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2830b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2831b7ffbd7eSJohannes Berg 	else
28324fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2833b7ffbd7eSJohannes Berg 
28343b17fbf8SMaxim Altshul 	return thr;
2835b7ffbd7eSJohannes Berg }
2836b8da6b6aSJohannes Berg 
2837b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2838b8da6b6aSJohannes Berg {
2839c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2840c9c5962bSJohannes Berg 
2841046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2842046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2843c9c5962bSJohannes Berg 		return stats->last_rx;
2844046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2845b8da6b6aSJohannes Berg }
2846484a54c2SToke Høiland-Jørgensen 
2847484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2848484a54c2SToke Høiland-Jørgensen {
2849484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2850484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2851484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2852484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2853484a54c2SToke Høiland-Jørgensen 	} else {
2854484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2855484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2856484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2857484a54c2SToke Høiland-Jørgensen 	}
2858484a54c2SToke Høiland-Jørgensen }
2859484a54c2SToke Høiland-Jørgensen 
2860484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2861484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2862484a54c2SToke Høiland-Jørgensen {
2863484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2864484a54c2SToke Høiland-Jørgensen 
2865484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2866484a54c2SToke Høiland-Jørgensen }
2867cb71f1d1SJohannes Berg 
2868cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2869cb71f1d1SJohannes Berg {
2870cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2871cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2872cb71f1d1SJohannes Berg 	int ret;
2873cb71f1d1SJohannes Berg 
2874cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2875cb71f1d1SJohannes Berg 
2876cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2877cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2878cb71f1d1SJohannes Berg 		return -EINVAL;
2879cb71f1d1SJohannes Berg 
2880cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2881cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2882cb71f1d1SJohannes Berg 		return -EBUSY;
2883cb71f1d1SJohannes Berg 
2884cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2885cb71f1d1SJohannes Berg 	if (!alloc)
2886cb71f1d1SJohannes Berg 		return -ENOMEM;
2887cb71f1d1SJohannes Berg 
2888cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2889cb71f1d1SJohannes Berg 	if (ret) {
2890cb71f1d1SJohannes Berg 		kfree(alloc);
2891cb71f1d1SJohannes Berg 		return ret;
2892cb71f1d1SJohannes Berg 	}
2893cb71f1d1SJohannes Berg 
2894cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2895cb71f1d1SJohannes Berg 
2896d2caad52SBenjamin Berg 	ieee80211_link_sta_debugfs_add(&alloc->info);
2897d2caad52SBenjamin Berg 
2898cb71f1d1SJohannes Berg 	return 0;
2899cb71f1d1SJohannes Berg }
2900cb71f1d1SJohannes Berg 
290121476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
290221476ad1SShaul Triebitz {
290321476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
290421476ad1SShaul Triebitz 
290521476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
290621476ad1SShaul Triebitz }
290721476ad1SShaul Triebitz 
2908cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2909cb71f1d1SJohannes Berg {
2910cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2911c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2912cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2913cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2914cb71f1d1SJohannes Berg 	int ret;
2915cb71f1d1SJohannes Berg 
2916c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2917c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2918cb71f1d1SJohannes Berg 
2919c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2920cb71f1d1SJohannes Berg 		return -EINVAL;
2921cb71f1d1SJohannes Berg 
2922177577dbSJohannes Berg 	rcu_read_lock();
2923177577dbSJohannes Berg 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2924177577dbSJohannes Berg 		rcu_read_unlock();
2925177577dbSJohannes Berg 		return -EALREADY;
2926177577dbSJohannes Berg 	}
2927177577dbSJohannes Berg 	/* we only modify under the mutex so this is fine */
2928177577dbSJohannes Berg 	rcu_read_unlock();
2929177577dbSJohannes Berg 
2930cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2931cb71f1d1SJohannes Berg 
293280e2b1faSLukas Bulwahn 	if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2933ba6ddab9SJohannes Berg 		goto hash;
2934cb71f1d1SJohannes Berg 
2935ba7af265SJohannes Berg 	ieee80211_recalc_min_chandef(sdata, link_id);
2936ba7af265SJohannes Berg 
29374c51541dSBenjamin Berg 	/* Ensure the values are updated for the driver,
29384c51541dSBenjamin Berg 	 * redone by sta_remove_link on failure.
29394c51541dSBenjamin Berg 	 */
29404c51541dSBenjamin Berg 	ieee80211_sta_recalc_aggregates(&sta->sta);
29414c51541dSBenjamin Berg 
2942cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2943cb71f1d1SJohannes Berg 				   old_links, new_links);
2944cb71f1d1SJohannes Berg 	if (ret) {
2945cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2946ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2947177577dbSJohannes Berg 		return ret;
2948cb71f1d1SJohannes Berg 	}
2949cb71f1d1SJohannes Berg 
2950ba6ddab9SJohannes Berg hash:
2951177577dbSJohannes Berg 	ret = link_sta_info_hash_add(sdata->local, link_sta);
2952177577dbSJohannes Berg 	WARN_ON(ret);
2953177577dbSJohannes Berg 	return 0;
2954cb71f1d1SJohannes Berg }
2955cb71f1d1SJohannes Berg 
2956cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2957cb71f1d1SJohannes Berg {
2958cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2959a8f62399SShaul Triebitz 	u16 old_links = sta->sta.valid_links;
2960cb71f1d1SJohannes Berg 
2961cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2962cb71f1d1SJohannes Berg 
2963cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2964cb71f1d1SJohannes Berg 
2965cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2966cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2967a8f62399SShaul Triebitz 				     old_links, sta->sta.valid_links);
2968cb71f1d1SJohannes Berg 
2969ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2970cb71f1d1SJohannes Berg }
2971175ad2ecSJohannes Berg 
2972175ad2ecSJohannes Berg void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2973175ad2ecSJohannes Berg 					   const u8 *ext_capab,
2974175ad2ecSJohannes Berg 					   unsigned int ext_capab_len)
2975175ad2ecSJohannes Berg {
2976175ad2ecSJohannes Berg 	u8 val;
2977175ad2ecSJohannes Berg 
2978175ad2ecSJohannes Berg 	sta->sta.max_amsdu_subframes = 0;
2979175ad2ecSJohannes Berg 
2980175ad2ecSJohannes Berg 	if (ext_capab_len < 8)
2981175ad2ecSJohannes Berg 		return;
2982175ad2ecSJohannes Berg 
2983175ad2ecSJohannes Berg 	/* The sender might not have sent the last bit, consider it to be 0 */
2984175ad2ecSJohannes Berg 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2985175ad2ecSJohannes Berg 
2986175ad2ecSJohannes Berg 	/* we did get all the bits, take the MSB as well */
2987175ad2ecSJohannes Berg 	if (ext_capab_len >= 9)
2988175ad2ecSJohannes Berg 		val |= u8_get_bits(ext_capab[8],
2989175ad2ecSJohannes Berg 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2990175ad2ecSJohannes Berg 
2991175ad2ecSJohannes Berg 	if (val)
2992175ad2ecSJohannes Berg 		sta->sta.max_amsdu_subframes = 4 << val;
2993175ad2ecSJohannes Berg }
299465fd846cSJohannes Berg 
299565fd846cSJohannes Berg #ifdef CONFIG_LOCKDEP
299665fd846cSJohannes Berg bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
299765fd846cSJohannes Berg {
299865fd846cSJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
299965fd846cSJohannes Berg 
300065fd846cSJohannes Berg 	return lockdep_is_held(&sta->local->sta_mtx);
300165fd846cSJohannes Berg }
300265fd846cSJohannes Berg EXPORT_SYMBOL(lockdep_sta_mutex_held);
300365fd846cSJohannes Berg #endif
3004