xref: /openbmc/linux/net/mac80211/sta_info.c (revision 0ad49045f28474b3cbc1f27ec7a4238970734764)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f0706e82SJiri Benc /*
3f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
4f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
6dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
73a11ce08SJohannes Berg  * Copyright (C) 2018-2021 Intel Corporation
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
67cb71f1d1SJohannes Berg struct sta_link_alloc {
68cb71f1d1SJohannes Berg 	struct link_sta_info info;
69cb71f1d1SJohannes Berg 	struct ieee80211_link_sta sta;
70c71420dbSJohannes Berg 	struct rcu_head rcu_head;
71cb71f1d1SJohannes Berg };
72cb71f1d1SJohannes Berg 
737bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
747bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
75caf22d31SJohannes Berg 	.automatic_shrinking = true,
767bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
77ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
787bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
79ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
807bedd0cfSJohannes Berg };
817bedd0cfSJohannes Berg 
82ba6ddab9SJohannes Berg static const struct rhashtable_params link_sta_rht_params = {
83ba6ddab9SJohannes Berg 	.nelem_hint = 3, /* start small */
84ba6ddab9SJohannes Berg 	.automatic_shrinking = true,
85ba6ddab9SJohannes Berg 	.head_offset = offsetof(struct link_sta_info, link_hash_node),
86ba6ddab9SJohannes Berg 	.key_offset = offsetof(struct link_sta_info, addr),
87ba6ddab9SJohannes Berg 	.key_len = ETH_ALEN,
88ba6ddab9SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89ba6ddab9SJohannes Berg };
90ba6ddab9SJohannes Berg 
914d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
92be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
93f0706e82SJiri Benc 			     struct sta_info *sta)
94f0706e82SJiri Benc {
9583e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
967bedd0cfSJohannes Berg 			       sta_rht_params);
97f0706e82SJiri Benc }
98f0706e82SJiri Benc 
99f36fe0a2SJohannes Berg static int link_sta_info_hash_add(struct ieee80211_local *local,
100f36fe0a2SJohannes Berg 				  struct link_sta_info *link_sta)
101f36fe0a2SJohannes Berg {
102f36fe0a2SJohannes Berg 	return rhltable_insert(&local->link_sta_hash,
103f36fe0a2SJohannes Berg 			       &link_sta->link_hash_node,
104f36fe0a2SJohannes Berg 			       link_sta_rht_params);
105f36fe0a2SJohannes Berg }
106f36fe0a2SJohannes Berg 
107ba6ddab9SJohannes Berg static int link_sta_info_hash_del(struct ieee80211_local *local,
108ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
109ba6ddab9SJohannes Berg {
110ba6ddab9SJohannes Berg 	return rhltable_remove(&local->link_sta_hash,
111ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
112ba6ddab9SJohannes Berg 			       link_sta_rht_params);
113ba6ddab9SJohannes Berg }
114ba6ddab9SJohannes Berg 
1155108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
116b22cfcfcSEliad Peller {
117b22cfcfcSEliad Peller 	int ac, i;
118b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
119b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
120b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
121d012a605SMarco Porsch 	struct ps_data *ps;
122b22cfcfcSEliad Peller 
123e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1245ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1255ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
126d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
127d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
128d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1293f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1303f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
131d012a605SMarco Porsch 		else
132d012a605SMarco Porsch 			return;
133b22cfcfcSEliad Peller 
134b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
135e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1365ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
137b22cfcfcSEliad Peller 
138d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
139b22cfcfcSEliad Peller 	}
140b22cfcfcSEliad Peller 
141ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
142ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
143adf8ed01SJohannes Berg 			struct txq_info *txqi;
144adf8ed01SJohannes Berg 
145adf8ed01SJohannes Berg 			if (!sta->sta.txq[i])
146adf8ed01SJohannes Berg 				continue;
147adf8ed01SJohannes Berg 
148adf8ed01SJohannes Berg 			txqi = to_txq_info(sta->sta.txq[i]);
149ba8c3d6fSFelix Fietkau 
150fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
151ba8c3d6fSFelix Fietkau 		}
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 
2755072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
2765072f73cSToke Høiland-Jørgensen 				       const u8 *sta_addr, const u8 *vif_addr)
2775072f73cSToke Høiland-Jørgensen {
2785072f73cSToke Høiland-Jørgensen 	struct rhlist_head *tmp;
2795072f73cSToke Høiland-Jørgensen 	struct sta_info *sta;
2805072f73cSToke Høiland-Jørgensen 
2815072f73cSToke Høiland-Jørgensen 	for_each_sta_info(local, sta_addr, sta, tmp) {
2825072f73cSToke Høiland-Jørgensen 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
2835072f73cSToke Høiland-Jørgensen 			return sta;
2845072f73cSToke Høiland-Jørgensen 	}
2855072f73cSToke Høiland-Jørgensen 
2865072f73cSToke Høiland-Jørgensen 	return NULL;
2875072f73cSToke Høiland-Jørgensen }
2885072f73cSToke Høiland-Jørgensen 
2893b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2903b53fde8SJohannes Berg 				     int idx)
291ee385855SLuis Carlos Cobo {
2923b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
293ee385855SLuis Carlos Cobo 	struct sta_info *sta;
294ee385855SLuis Carlos Cobo 	int i = 0;
295ee385855SLuis Carlos Cobo 
2968ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
2978ca47eb9SMadhuparna Bhowmik 				lockdep_is_held(&local->sta_mtx)) {
2983b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2992a8ca29aSLuis Carlos Cobo 			continue;
300ee385855SLuis Carlos Cobo 		if (i < idx) {
301ee385855SLuis Carlos Cobo 			++i;
302ee385855SLuis Carlos Cobo 			continue;
303ee385855SLuis Carlos Cobo 		}
3042a8ca29aSLuis Carlos Cobo 		return sta;
305ee385855SLuis Carlos Cobo 	}
306ee385855SLuis Carlos Cobo 
307ee385855SLuis Carlos Cobo 	return NULL;
308ee385855SLuis Carlos Cobo }
309f0706e82SJiri Benc 
310cb71f1d1SJohannes Berg static void sta_info_free_link(struct link_sta_info *link_sta)
311246b39e4SJohannes Berg {
312cb71f1d1SJohannes Berg 	free_percpu(link_sta->pcpu_rx_stats);
313cb71f1d1SJohannes Berg }
314246b39e4SJohannes Berg 
315ba6ddab9SJohannes Berg static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
316ba6ddab9SJohannes Berg 			    bool unhash)
317cb71f1d1SJohannes Berg {
318cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc = NULL;
319c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
320cb71f1d1SJohannes Berg 
321c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
322c71420dbSJohannes Berg 					     lockdep_is_held(&sta->local->sta_mtx));
323c71420dbSJohannes Berg 
324c71420dbSJohannes Berg 	if (WARN_ON(!link_sta))
325cb71f1d1SJohannes Berg 		return;
326246b39e4SJohannes Berg 
327ba6ddab9SJohannes Berg 	if (unhash)
328ba6ddab9SJohannes Berg 		link_sta_info_hash_del(sta->local, link_sta);
329ba6ddab9SJohannes Berg 
330c71420dbSJohannes Berg 	if (link_sta != &sta->deflink)
331c71420dbSJohannes Berg 		alloc = container_of(link_sta, typeof(*alloc), info);
332cb71f1d1SJohannes Berg 
333cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
334c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->link[link_id], NULL);
335c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
336cb71f1d1SJohannes Berg 	if (alloc) {
337cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
338c71420dbSJohannes Berg 		kfree_rcu(alloc, rcu_head);
339246b39e4SJohannes Berg 	}
340246b39e4SJohannes Berg }
341246b39e4SJohannes Berg 
34293e5deb1SJohannes Berg /**
343d9a7ddb0SJohannes Berg  * sta_info_free - free STA
34493e5deb1SJohannes Berg  *
3456ef307bcSRandy Dunlap  * @local: pointer to the global information
34693e5deb1SJohannes Berg  * @sta: STA info to free
34793e5deb1SJohannes Berg  *
34893e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
349d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
350d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
351d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
35293e5deb1SJohannes Berg  */
353d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
35493e5deb1SJohannes Berg {
355cb71f1d1SJohannes Berg 	int i;
356cb71f1d1SJohannes Berg 
357cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
358cb71f1d1SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
359cb71f1d1SJohannes Berg 			continue;
360cb71f1d1SJohannes Berg 
361*0ad49045SJohannes Berg 		sta_remove_link(sta, i, false);
362cb71f1d1SJohannes Berg 	}
363cb71f1d1SJohannes Berg 
364dcd479e1SJohannes Berg 	/*
365dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
366dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
367dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
368dcd479e1SJohannes Berg 	 *
369dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
370dcd479e1SJohannes Berg 	 * allocated during state transitions.
371dcd479e1SJohannes Berg 	 */
372dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
373dcd479e1SJohannes Berg 		int ret;
374dcd479e1SJohannes Berg 
375dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
376dcd479e1SJohannes Berg 
377dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
378dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
379dcd479e1SJohannes Berg 			break;
380dcd479e1SJohannes Berg 	}
381dcd479e1SJohannes Berg 
382889cbb91SJohannes Berg 	if (sta->rate_ctrl)
3834b7679a5SJohannes Berg 		rate_control_free_sta(sta);
38493e5deb1SJohannes Berg 
385bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
38693e5deb1SJohannes Berg 
387ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
388ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
38953d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
390433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
391433f5bc1SJohannes Berg 	kfree(sta->mesh);
392433f5bc1SJohannes Berg #endif
393246b39e4SJohannes Berg 
394cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
39593e5deb1SJohannes Berg 	kfree(sta);
39693e5deb1SJohannes Berg }
39793e5deb1SJohannes Berg 
3984d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
39962b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
400d0709a65SJohannes Berg 			     struct sta_info *sta)
401f0706e82SJiri Benc {
40283e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
4037bedd0cfSJohannes Berg 			       sta_rht_params);
404f0706e82SJiri Benc }
405f0706e82SJiri Benc 
4065ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
407af818581SJohannes Berg {
408af818581SJohannes Berg 	struct sta_info *sta;
409af818581SJohannes Berg 
4105ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
411af818581SJohannes Berg 
412af818581SJohannes Berg 	if (sta->dead)
413af818581SJohannes Berg 		return;
414af818581SJohannes Berg 
41554420473SHelmut Schaa 	local_bh_disable();
4165ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
417af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
4185ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
419af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
4205ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
42147086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
422ce662b44SJohannes Berg 	local_bh_enable();
423af818581SJohannes Berg }
424af818581SJohannes Berg 
425af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
426af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
427af65cd96SJohannes Berg {
42830686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
429af65cd96SJohannes Berg 		return 0;
430af65cd96SJohannes Berg 
431889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
432af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
43335c347acSJohannes Berg 						     sta, gfp);
434889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
435af65cd96SJohannes Berg 		return -ENOMEM;
436af65cd96SJohannes Berg 
437af65cd96SJohannes Berg 	return 0;
438af65cd96SJohannes Berg }
439af65cd96SJohannes Berg 
440cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
441246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
442246b39e4SJohannes Berg 			       gfp_t gfp)
443246b39e4SJohannes Berg {
444246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
445246b39e4SJohannes Berg 	int i;
446246b39e4SJohannes Berg 
447246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
448246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
449246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
450246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
451246b39e4SJohannes Berg 			return -ENOMEM;
452246b39e4SJohannes Berg 	}
453246b39e4SJohannes Berg 
454246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
455246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
456246b39e4SJohannes Berg 
457246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
458246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
459246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
460246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
461246b39e4SJohannes Berg 
462246b39e4SJohannes Berg 	return 0;
463246b39e4SJohannes Berg }
464246b39e4SJohannes Berg 
465cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
466cb71f1d1SJohannes Berg 			      unsigned int link_id,
467cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
468cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
469cb71f1d1SJohannes Berg {
470cb71f1d1SJohannes Berg 	link_info->sta = sta;
471cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
472c71420dbSJohannes Berg 	link_info->pub = link_sta;
473c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
474c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
475cb71f1d1SJohannes Berg }
476cb71f1d1SJohannes Berg 
477f36fe0a2SJohannes Berg static struct sta_info *
478f36fe0a2SJohannes Berg __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
479f36fe0a2SJohannes Berg 		 const u8 *addr, int link_id, const u8 *link_addr,
480f36fe0a2SJohannes Berg 		 gfp_t gfp)
481f0706e82SJiri Benc {
482d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
483ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
484f0706e82SJiri Benc 	struct sta_info *sta;
48516c5f15cSRon Rindjunsky 	int i;
486f0706e82SJiri Benc 
487ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
488f0706e82SJiri Benc 	if (!sta)
48973651ee6SJohannes Berg 		return NULL;
490f0706e82SJiri Benc 
491246b39e4SJohannes Berg 	sta->local = local;
492246b39e4SJohannes Berg 	sta->sdata = sdata;
493246b39e4SJohannes Berg 
494cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
495246b39e4SJohannes Berg 		return NULL;
496c9c5962bSJohannes Berg 
497cb71f1d1SJohannes Berg 	if (link_id >= 0) {
498cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
499cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
500cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
501cb71f1d1SJohannes Berg 	} else {
502cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
503cb71f1d1SJohannes Berg 	}
504cb71f1d1SJohannes Berg 
50507346f81SJohannes Berg 	spin_lock_init(&sta->lock);
5061d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
5075ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
50867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
509a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
51087f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
511433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
512433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
513433f5bc1SJohannes Berg 		if (!sta->mesh)
514433f5bc1SJohannes Berg 			goto free;
5154c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
516433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
51745d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5184c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5194c02d62fSKees Cook 				    0);
520433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
521433f5bc1SJohannes Berg 	}
52287f59c70SThomas Pedersen #endif
52307346f81SJohannes Berg 
524ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
52517741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
526f36fe0a2SJohannes Berg 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
527f36fe0a2SJohannes Berg 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
528480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
529480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
530480dd46bSMaxim Altshul 
531046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
532046d2e7cSSriram R 
53396fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
53496fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
53596fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
53696fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
53796fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
53896fc6efbSAlexander Wetzel 	 */
53996fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
54096fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
54196fc6efbSAlexander Wetzel 
5420f9c5a61SJohannes Berg 
5433a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5443a11ce08SJohannes Berg 
54571ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
54671ec375cSJohannes Berg 
547b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
548b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
549b6da911bSLiad Kaufman 
55084b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
551541a45a1SBruno Randolf 
552ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
553ba8c3d6fSFelix Fietkau 		void *txq_data;
554ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
555ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
556ba8c3d6fSFelix Fietkau 
557ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
558ba8c3d6fSFelix Fietkau 		if (!txq_data)
559ba8c3d6fSFelix Fietkau 			goto free;
560ba8c3d6fSFelix Fietkau 
561ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
562ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
563ba8c3d6fSFelix Fietkau 
564adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
565fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
566abfbc3afSJohannes Berg 		}
567ba8c3d6fSFelix Fietkau 	}
568ba8c3d6fSFelix Fietkau 
569ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
570ba8c3d6fSFelix Fietkau 		goto free_txq;
571f0706e82SJiri Benc 
572942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
573b4809e94SToke Høiland-Jørgensen 
574948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
575948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
576948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
577942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
578942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
579942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
580942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
581948d887dSJohannes Berg 	}
58273651ee6SJohannes Berg 
5835a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
5844be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
585cccaec98SSenthil Balasubramanian 
586bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
587bd718fc1SJohannes Berg 		u32 mandatory = 0;
588bd718fc1SJohannes Berg 		int r;
589bd718fc1SJohannes Berg 
590bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
591bd718fc1SJohannes Berg 			continue;
592bd718fc1SJohannes Berg 
593bd718fc1SJohannes Berg 		switch (i) {
594bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
59563fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
596bd718fc1SJohannes Berg 			/*
597bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
598bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
599bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
600bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
601bd718fc1SJohannes Berg 			 * possible rate anyway.
602bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
603bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
604bd718fc1SJohannes Berg 			 */
605bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
606bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
607bd718fc1SJohannes Berg 			break;
608bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
609bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
610bd718fc1SJohannes Berg 			break;
611bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
612bd718fc1SJohannes Berg 			WARN_ON(1);
613bd718fc1SJohannes Berg 			mandatory = 0;
614bd718fc1SJohannes Berg 			break;
615bd718fc1SJohannes Berg 		}
616bd718fc1SJohannes Berg 
617bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
618bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
619bd718fc1SJohannes Berg 
620bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
621bd718fc1SJohannes Berg 
622bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
623bd718fc1SJohannes Berg 				continue;
624046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
625bd718fc1SJohannes Berg 		}
626bd718fc1SJohannes Berg 	}
627bd718fc1SJohannes Berg 
628af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
6296e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
6306e0456b5SFelix Fietkau 
631484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
632484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
633484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
634484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
635dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
636dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
637484a54c2SToke Høiland-Jørgensen 
638bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
639ef04a297SJohannes Berg 
640abfbc3afSJohannes Berg 	return sta;
641ba8c3d6fSFelix Fietkau 
642ba8c3d6fSFelix Fietkau free_txq:
643ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
644ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
645ba8c3d6fSFelix Fietkau free:
646cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
647433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
648433f5bc1SJohannes Berg 	kfree(sta->mesh);
649433f5bc1SJohannes Berg #endif
650ba8c3d6fSFelix Fietkau 	kfree(sta);
651ba8c3d6fSFelix Fietkau 	return NULL;
65273651ee6SJohannes Berg }
65373651ee6SJohannes Berg 
654f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
655f36fe0a2SJohannes Berg 				const u8 *addr, gfp_t gfp)
656f36fe0a2SJohannes Berg {
657f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
658f36fe0a2SJohannes Berg }
659f36fe0a2SJohannes Berg 
660f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
661f36fe0a2SJohannes Berg 					  const u8 *mld_addr,
662f36fe0a2SJohannes Berg 					  unsigned int link_id,
663f36fe0a2SJohannes Berg 					  const u8 *link_addr,
664f36fe0a2SJohannes Berg 					  gfp_t gfp)
665f36fe0a2SJohannes Berg {
666f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
667f36fe0a2SJohannes Berg }
668f36fe0a2SJohannes Berg 
6698c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
67034e89507SJohannes Berg {
67134e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
67234e89507SJohannes Berg 
67303e4497eSJohannes Berg 	/*
67403e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
67503e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
67603e4497eSJohannes Berg 	 * and another CPU turns off the net device.
67703e4497eSJohannes Berg 	 */
6788c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
6798c71df7aSGuy Eilam 		return -ENETDOWN;
68003e4497eSJohannes Berg 
681b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
682deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
6838c71df7aSGuy Eilam 		return -EINVAL;
6848c71df7aSGuy Eilam 
68583e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
68683e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
68783e7e4ceSHerbert Xu 	 * for correctness.
68831104891SJohannes Berg 	 */
68931104891SJohannes Berg 	rcu_read_lock();
69031104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
69131104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
69231104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
69331104891SJohannes Berg 		rcu_read_unlock();
69431104891SJohannes Berg 		return -ENOTUNIQ;
69531104891SJohannes Berg 	}
69631104891SJohannes Berg 	rcu_read_unlock();
69731104891SJohannes Berg 
6988c71df7aSGuy Eilam 	return 0;
69993e5deb1SJohannes Berg }
70044213b5eSJohannes Berg 
701f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
702f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
703f09603a2SJohannes Berg 				     struct sta_info *sta)
704f09603a2SJohannes Berg {
705f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
706f09603a2SJohannes Berg 	int err = 0;
707f09603a2SJohannes Berg 
708f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
709f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
710f09603a2SJohannes Berg 		if (err)
711f09603a2SJohannes Berg 			break;
712f09603a2SJohannes Berg 	}
713f09603a2SJohannes Berg 
714f09603a2SJohannes Berg 	if (!err) {
715a4ec45a4SJohannes Berg 		/*
716a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
717a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
718a4ec45a4SJohannes Berg 		 */
719a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
720f09603a2SJohannes Berg 			sta->uploaded = true;
721f09603a2SJohannes Berg 		return 0;
722f09603a2SJohannes Berg 	}
723f09603a2SJohannes Berg 
724f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
725bdcbd8e0SJohannes Berg 		sdata_info(sdata,
726bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
727bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
728f09603a2SJohannes Berg 		err = 0;
729f09603a2SJohannes Berg 	}
730f09603a2SJohannes Berg 
731f09603a2SJohannes Berg 	/* unwind on error */
732f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
733f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
734f09603a2SJohannes Berg 
735f09603a2SJohannes Berg 	return err;
736f09603a2SJohannes Berg }
737f09603a2SJohannes Berg 
738d405fd8cSGregory Greenman static void
739d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
740d405fd8cSGregory Greenman {
741d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
742d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
743d405fd8cSGregory Greenman 	struct sta_info *sta;
744d405fd8cSGregory Greenman 
745d405fd8cSGregory Greenman 	rcu_read_lock();
746d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
747d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
748d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
749d405fd8cSGregory Greenman 			continue;
750d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
751d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
752d405fd8cSGregory Greenman 			break;
753d405fd8cSGregory Greenman 		}
754d405fd8cSGregory Greenman 	}
755d405fd8cSGregory Greenman 	rcu_read_unlock();
756d405fd8cSGregory Greenman 
757d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
758d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
759d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
760d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
761d405fd8cSGregory Greenman 	}
762d405fd8cSGregory Greenman }
763d405fd8cSGregory Greenman 
76434e89507SJohannes Berg /*
7658c71df7aSGuy Eilam  * should be called with sta_mtx locked
7668c71df7aSGuy Eilam  * this function replaces the mutex lock
7678c71df7aSGuy Eilam  * with a RCU lock
7688c71df7aSGuy Eilam  */
7694d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
7708c71df7aSGuy Eilam {
7718c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
7728c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
7730c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
7748c71df7aSGuy Eilam 	int err = 0;
7758c71df7aSGuy Eilam 
7768c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
7778c71df7aSGuy Eilam 
7787852e361SJohannes Berg 	/* check if STA exists already */
7797852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
7804d33960bSJohannes Berg 		err = -EEXIST;
7818f9dcc29SAhmed Zaki 		goto out_cleanup;
78234e89507SJohannes Berg 	}
78334e89507SJohannes Berg 
7840c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
7850c2e3842SKoen Vandeputte 	if (!sinfo) {
7860c2e3842SKoen Vandeputte 		err = -ENOMEM;
7878f9dcc29SAhmed Zaki 		goto out_cleanup;
7880c2e3842SKoen Vandeputte 	}
7890c2e3842SKoen Vandeputte 
7904d33960bSJohannes Berg 	local->num_sta++;
7914d33960bSJohannes Berg 	local->sta_generation++;
7924d33960bSJohannes Berg 	smp_mb();
7934d33960bSJohannes Berg 
7945108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
7955108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
7965108ca82SJohannes Berg 
7974d33960bSJohannes Berg 	/* make the station visible */
79862b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
79962b14b24SJohannes Berg 	if (err)
80062b14b24SJohannes Berg 		goto out_drop_sta;
8014d33960bSJohannes Berg 
802f36fe0a2SJohannes Berg 	if (sta->sta.valid_links) {
803f36fe0a2SJohannes Berg 		err = link_sta_info_hash_add(local, &sta->deflink);
804f36fe0a2SJohannes Berg 		if (err) {
805f36fe0a2SJohannes Berg 			sta_info_hash_del(local, sta);
806f36fe0a2SJohannes Berg 			goto out_drop_sta;
807f36fe0a2SJohannes Berg 		}
808f36fe0a2SJohannes Berg 	}
809f36fe0a2SJohannes Berg 
8102bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
81183d5cc01SJohannes Berg 
8124dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8134dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8144dde3c36SMordechay Goodstein 	 */
8154dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8160cbf348aSAndrei Otcheretianski 		ieee80211_recalc_min_chandef(sta->sdata, -1);
8174dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8184dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8194dde3c36SMordechay Goodstein 	}
8204dde3c36SMordechay Goodstein 
8215108ca82SJohannes Berg 	/* notify driver */
8225108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8235108ca82SJohannes Berg 	if (err)
8245108ca82SJohannes Berg 		goto out_remove;
8255108ca82SJohannes Berg 
82683d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
827d405fd8cSGregory Greenman 
8285108ca82SJohannes Berg 	/* accept BA sessions now */
8295108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8304d33960bSJohannes Berg 
8314d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8324d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
8334d33960bSJohannes Berg 
8340ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
8350ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
8360ef049dcSArnd Bergmann 	kfree(sinfo);
837d0709a65SJohannes Berg 
838bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
839f0706e82SJiri Benc 
84034e89507SJohannes Berg 	/* move reference to rcu-protected */
84134e89507SJohannes Berg 	rcu_read_lock();
84234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
843e9f207f0SJiri Benc 
84473651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
84573651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
84673651ee6SJohannes Berg 
84773651ee6SJohannes Berg 	return 0;
8485108ca82SJohannes Berg  out_remove:
849*0ad49045SJohannes Berg 	if (sta->sta.valid_links)
850*0ad49045SJohannes Berg 		link_sta_info_hash_del(local, &sta->deflink);
8515108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
8525108ca82SJohannes Berg 	list_del_rcu(&sta->list);
85362b14b24SJohannes Berg  out_drop_sta:
8545108ca82SJohannes Berg 	local->num_sta--;
8555108ca82SJohannes Berg 	synchronize_net();
8568f9dcc29SAhmed Zaki  out_cleanup:
8577bc40aedSJohannes Berg 	cleanup_single_sta(sta);
8584d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
859ea32f065SSudip Mukherjee 	kfree(sinfo);
8604d33960bSJohannes Berg 	rcu_read_lock();
8614d33960bSJohannes Berg 	return err;
8628c71df7aSGuy Eilam }
8638c71df7aSGuy Eilam 
8648c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
8658c71df7aSGuy Eilam {
8668c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
867308f7fcfSZhao, Gang 	int err;
8688c71df7aSGuy Eilam 
8694d33960bSJohannes Berg 	might_sleep();
8704d33960bSJohannes Berg 
87131104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
87231104891SJohannes Berg 
8738c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
8748c71df7aSGuy Eilam 	if (err) {
8757bc40aedSJohannes Berg 		sta_info_free(local, sta);
87631104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
8778c71df7aSGuy Eilam 		rcu_read_lock();
8787bc40aedSJohannes Berg 		return err;
8798c71df7aSGuy Eilam 	}
8808c71df7aSGuy Eilam 
8817bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
882f0706e82SJiri Benc }
883f0706e82SJiri Benc 
88434e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
88534e89507SJohannes Berg {
88634e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
88734e89507SJohannes Berg 
88834e89507SJohannes Berg 	rcu_read_unlock();
88934e89507SJohannes Berg 
89034e89507SJohannes Berg 	return err;
89134e89507SJohannes Berg }
89234e89507SJohannes Berg 
893d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
894004c872eSJohannes Berg {
895004c872eSJohannes Berg 	/*
896004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
897004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
898004c872eSJohannes Berg 	 */
899d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
900004c872eSJohannes Berg }
901004c872eSJohannes Berg 
902d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
903004c872eSJohannes Berg {
904004c872eSJohannes Berg 	/*
905004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
906004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
907004c872eSJohannes Berg 	 */
908d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
909004c872eSJohannes Berg }
910004c872eSJohannes Berg 
9113d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9123d5839b6SIlan Peer {
9133d5839b6SIlan Peer 	/*
9143d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9153d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9163d5839b6SIlan Peer 	 */
9173d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9183d5839b6SIlan Peer }
9193d5839b6SIlan Peer 
920948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
921004c872eSJohannes Berg {
922948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
923948d887dSJohannes Berg 	switch (ac) {
924948d887dSJohannes Berg 	case IEEE80211_AC_VO:
925948d887dSJohannes Berg 		return BIT(6) | BIT(7);
926948d887dSJohannes Berg 	case IEEE80211_AC_VI:
927948d887dSJohannes Berg 		return BIT(4) | BIT(5);
928948d887dSJohannes Berg 	case IEEE80211_AC_BE:
929948d887dSJohannes Berg 		return BIT(0) | BIT(3);
930948d887dSJohannes Berg 	case IEEE80211_AC_BK:
931948d887dSJohannes Berg 		return BIT(1) | BIT(2);
932948d887dSJohannes Berg 	default:
933948d887dSJohannes Berg 		WARN_ON(1);
934948d887dSJohannes Berg 		return 0;
935d0709a65SJohannes Berg 	}
936004c872eSJohannes Berg }
937004c872eSJohannes Berg 
9389b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
939004c872eSJohannes Berg {
940c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
941d012a605SMarco Porsch 	struct ps_data *ps;
942948d887dSJohannes Berg 	bool indicate_tim = false;
943948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
944948d887dSJohannes Berg 	int ac;
945a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
946004c872eSJohannes Berg 
947d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
948d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
949c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
950c868cb35SJohannes Berg 			return;
9513e122be0SJohannes Berg 
952d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
9533f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
9543f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
9553f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
9563f52b7e3SMarco Porsch #endif
957d012a605SMarco Porsch 	} else {
958d012a605SMarco Porsch 		return;
959d012a605SMarco Porsch 	}
960d012a605SMarco Porsch 
961c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
962d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
963c868cb35SJohannes Berg 		return;
964004c872eSJohannes Berg 
965c868cb35SJohannes Berg 	if (sta->dead)
966c868cb35SJohannes Berg 		goto done;
9673e122be0SJohannes Berg 
968948d887dSJohannes Berg 	/*
969948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
970948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
971948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
972948d887dSJohannes Berg 	 * non-enabled ones.
973948d887dSJohannes Berg 	 */
974948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
975948d887dSJohannes Berg 		ignore_for_tim = 0;
976948d887dSJohannes Berg 
9779b7a86f3SJohannes Berg 	if (ignore_pending)
9789b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
9799b7a86f3SJohannes Berg 
980948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
981948d887dSJohannes Berg 		unsigned long tids;
982948d887dSJohannes Berg 
983f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
984948d887dSJohannes Berg 			continue;
985948d887dSJohannes Berg 
986948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
987948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
988948d887dSJohannes Berg 		if (indicate_tim)
989948d887dSJohannes Berg 			break;
990948d887dSJohannes Berg 
991948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
992948d887dSJohannes Berg 
993948d887dSJohannes Berg 		indicate_tim |=
994948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
995ba8c3d6fSFelix Fietkau 		indicate_tim |=
996ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
997004c872eSJohannes Berg 	}
998004c872eSJohannes Berg 
999c868cb35SJohannes Berg  done:
100065f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
1001004c872eSJohannes Berg 
10023d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
10033d5839b6SIlan Peer 		goto out_unlock;
10043d5839b6SIlan Peer 
1005948d887dSJohannes Berg 	if (indicate_tim)
1006d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1007c868cb35SJohannes Berg 	else
1008d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10093e122be0SJohannes Berg 
10109b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1011c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1012948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1013c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1014004c872eSJohannes Berg 	}
1015004c872eSJohannes Berg 
10163d5839b6SIlan Peer out_unlock:
101765f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1018004c872eSJohannes Berg }
1019004c872eSJohannes Berg 
10209b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10219b7a86f3SJohannes Berg {
10229b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10239b7a86f3SJohannes Berg }
10249b7a86f3SJohannes Berg 
1025cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1026f0706e82SJiri Benc {
1027e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1028f0706e82SJiri Benc 	int timeout;
1029f0706e82SJiri Benc 
1030f0706e82SJiri Benc 	if (!skb)
1031cd0b8d89SJohannes Berg 		return false;
1032f0706e82SJiri Benc 
1033e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1034f0706e82SJiri Benc 
1035f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
103657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
103757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
103857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1039f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1040f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1041e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1042f0706e82SJiri Benc }
1043f0706e82SJiri Benc 
1044f0706e82SJiri Benc 
1045948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1046948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1047f0706e82SJiri Benc {
1048f0706e82SJiri Benc 	unsigned long flags;
1049f0706e82SJiri Benc 	struct sk_buff *skb;
1050f0706e82SJiri Benc 
105160750397SJohannes Berg 	/*
105260750397SJohannes Berg 	 * First check for frames that should expire on the filtered
105360750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
105460750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
105560750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
105660750397SJohannes Berg 	 * total_ps_buffered counter.
105760750397SJohannes Berg 	 */
1058f0706e82SJiri Benc 	for (;;) {
1059948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1060948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
106157c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1062948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1063836341a7SJohannes Berg 		else
1064f0706e82SJiri Benc 			skb = NULL;
1065948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1066f0706e82SJiri Benc 
106760750397SJohannes Berg 		/*
106860750397SJohannes Berg 		 * Frames are queued in order, so if this one
106960750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
107060750397SJohannes Berg 		 * we actually reached the end of the queue we
107160750397SJohannes Berg 		 * also need to stop, of course.
107260750397SJohannes Berg 		 */
107360750397SJohannes Berg 		if (!skb)
107460750397SJohannes Berg 			break;
1075d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
107660750397SJohannes Berg 	}
107760750397SJohannes Berg 
107860750397SJohannes Berg 	/*
107960750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
108060750397SJohannes Berg 	 * only find something if the filtered queue was emptied
108160750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
108260750397SJohannes Berg 	 * buffered frames.
108360750397SJohannes Berg 	 */
1084f0706e82SJiri Benc 	for (;;) {
1085948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1086948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1087f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1088948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1089f0706e82SJiri Benc 		else
1090f0706e82SJiri Benc 			skb = NULL;
1091948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1092f0706e82SJiri Benc 
109360750397SJohannes Berg 		/*
109460750397SJohannes Berg 		 * frames are queued in order, so if this one
109560750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
109660750397SJohannes Berg 		 * the queue) we can stop testing
109760750397SJohannes Berg 		 */
1098836341a7SJohannes Berg 		if (!skb)
1099836341a7SJohannes Berg 			break;
1100836341a7SJohannes Berg 
1101f0706e82SJiri Benc 		local->total_ps_buffered--;
1102bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1103bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1104d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1105f0706e82SJiri Benc 	}
11063393a608SJuuso Oikarinen 
110760750397SJohannes Berg 	/*
110860750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
110960750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
111060750397SJohannes Berg 	 * frames.
111160750397SJohannes Berg 	 */
111260750397SJohannes Berg 	sta_info_recalc_tim(sta);
111360750397SJohannes Berg 
111460750397SJohannes Berg 	/*
111560750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
111660750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
111760750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
111860750397SJohannes Berg 	 */
1119948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1120948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1121948d887dSJohannes Berg }
1122948d887dSJohannes Berg 
1123948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1124948d887dSJohannes Berg 					     struct sta_info *sta)
1125948d887dSJohannes Berg {
1126948d887dSJohannes Berg 	bool have_buffered = false;
1127948d887dSJohannes Berg 	int ac;
1128948d887dSJohannes Berg 
11293f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11303f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11313f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1132948d887dSJohannes Berg 		return false;
1133948d887dSJohannes Berg 
1134948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1135948d887dSJohannes Berg 		have_buffered |=
1136948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1137948d887dSJohannes Berg 
1138948d887dSJohannes Berg 	return have_buffered;
1139f0706e82SJiri Benc }
1140f0706e82SJiri Benc 
1141d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
114234e89507SJohannes Berg {
114334e89507SJohannes Berg 	struct ieee80211_local *local;
114434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
1145*0ad49045SJohannes Berg 	int ret, i;
114634e89507SJohannes Berg 
114734e89507SJohannes Berg 	might_sleep();
114834e89507SJohannes Berg 
114934e89507SJohannes Berg 	if (!sta)
115034e89507SJohannes Berg 		return -ENOENT;
115134e89507SJohannes Berg 
115234e89507SJohannes Berg 	local = sta->local;
115334e89507SJohannes Berg 	sdata = sta->sdata;
115434e89507SJohannes Berg 
115583d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
115683d5cc01SJohannes Berg 
1157098a6070SJohannes Berg 	/*
1158098a6070SJohannes Berg 	 * Before removing the station from the driver and
1159098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1160098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1161098a6070SJohannes Berg 	 * will be sufficient.
1162098a6070SJohannes Berg 	 */
1163c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1164c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1165098a6070SJohannes Berg 
1166f59374ebSSara Sharon 	/*
1167f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1168f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1169f59374ebSSara Sharon 	 * all such frames to be processed.
1170f59374ebSSara Sharon 	 */
1171f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1172f59374ebSSara Sharon 
1173*0ad49045SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1174*0ad49045SJohannes Berg 		struct link_sta_info *link_sta;
1175*0ad49045SJohannes Berg 
1176*0ad49045SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
1177*0ad49045SJohannes Berg 			continue;
1178*0ad49045SJohannes Berg 
1179*0ad49045SJohannes Berg 		link_sta = rcu_dereference_protected(sta->link[i],
1180*0ad49045SJohannes Berg 						     lockdep_is_held(&local->sta_mtx));
1181*0ad49045SJohannes Berg 
1182*0ad49045SJohannes Berg 		link_sta_info_hash_del(local, link_sta);
1183*0ad49045SJohannes Berg 	}
1184*0ad49045SJohannes Berg 
118534e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1186b01711beSJohannes Berg 	if (WARN_ON(ret))
118734e89507SJohannes Berg 		return ret;
118834e89507SJohannes Berg 
1189a7a6bdd0SArik Nemtsov 	/*
1190a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1191a7a6bdd0SArik Nemtsov 	 * removal.
1192a7a6bdd0SArik Nemtsov 	 */
1193a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1194a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1195a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1196a7a6bdd0SArik Nemtsov 	}
1197a7a6bdd0SArik Nemtsov 
1198794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1199ef044763SEliad Peller 	sta->removed = true;
12004d33960bSJohannes Berg 
12016a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
12026a9d1b91SJohannes Berg 
1203a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1204a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1205a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1206a710c816SJohannes Berg 
1207d778207bSJohannes Berg 	return 0;
1208d778207bSJohannes Berg }
1209d778207bSJohannes Berg 
1210d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1211d778207bSJohannes Berg {
1212d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1213d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
12140ef049dcSArnd Bergmann 	struct station_info *sinfo;
1215d778207bSJohannes Berg 	int ret;
1216d778207bSJohannes Berg 
1217d778207bSJohannes Berg 	/*
1218d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1219d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1220d778207bSJohannes Berg 	 */
1221d778207bSJohannes Berg 
1222d778207bSJohannes Berg 	might_sleep();
1223d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1224d778207bSJohannes Berg 
12255981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1226b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1227b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1228b16798f5SJohannes Berg 	}
1229b16798f5SJohannes Berg 
1230c8782078SJohannes Berg 	/* now keys can no longer be reached */
12316d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
123234e89507SJohannes Berg 
12339b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
12349b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
12359b7a86f3SJohannes Berg 
123634e89507SJohannes Berg 	sta->dead = true;
123734e89507SJohannes Berg 
123834e89507SJohannes Berg 	local->num_sta--;
123934e89507SJohannes Berg 	local->sta_generation++;
124034e89507SJohannes Berg 
124183d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1242f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1243f09603a2SJohannes Berg 		if (ret) {
124483d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
124583d5cc01SJohannes Berg 			break;
124683d5cc01SJohannes Berg 		}
124783d5cc01SJohannes Berg 	}
1248d9a7ddb0SJohannes Berg 
1249f09603a2SJohannes Berg 	if (sta->uploaded) {
1250f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1251f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1252f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1253f09603a2SJohannes Berg 	}
125434e89507SJohannes Berg 
1255bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1256bdcbd8e0SJohannes Berg 
12570ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
12580ef049dcSArnd Bergmann 	if (sinfo)
12590fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
12600ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
12610ef049dcSArnd Bergmann 	kfree(sinfo);
1262ec15e68bSJouni Malinen 
126334e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
126434e89507SJohannes Berg 
12653a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
12663a11ce08SJohannes Berg 
1267d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1268d778207bSJohannes Berg }
1269d778207bSJohannes Berg 
1270d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1271d778207bSJohannes Berg {
1272d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1273d778207bSJohannes Berg 
1274d778207bSJohannes Berg 	if (err)
1275d778207bSJohannes Berg 		return err;
1276d778207bSJohannes Berg 
1277d778207bSJohannes Berg 	synchronize_net();
1278d778207bSJohannes Berg 
1279d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
128034e89507SJohannes Berg 
128134e89507SJohannes Berg 	return 0;
128234e89507SJohannes Berg }
128334e89507SJohannes Berg 
128434e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
128534e89507SJohannes Berg {
128634e89507SJohannes Berg 	struct sta_info *sta;
128734e89507SJohannes Berg 	int ret;
128834e89507SJohannes Berg 
128934e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12907852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
129134e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
129234e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
129334e89507SJohannes Berg 
129434e89507SJohannes Berg 	return ret;
129534e89507SJohannes Berg }
129634e89507SJohannes Berg 
129734e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
129834e89507SJohannes Berg 			      const u8 *addr)
129934e89507SJohannes Berg {
130034e89507SJohannes Berg 	struct sta_info *sta;
130134e89507SJohannes Berg 	int ret;
130234e89507SJohannes Berg 
130334e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
13047852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
130534e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
130634e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
130734e89507SJohannes Berg 
130834e89507SJohannes Berg 	return ret;
130934e89507SJohannes Berg }
1310f0706e82SJiri Benc 
131134f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1312f0706e82SJiri Benc {
131334f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1314f0706e82SJiri Benc 	struct sta_info *sta;
13153393a608SJuuso Oikarinen 	bool timer_needed = false;
1316f0706e82SJiri Benc 
1317d0709a65SJohannes Berg 	rcu_read_lock();
1318d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
13193393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
13203393a608SJuuso Oikarinen 			timer_needed = true;
1321d0709a65SJohannes Berg 	rcu_read_unlock();
1322f0706e82SJiri Benc 
13235bb644a0SJohannes Berg 	if (local->quiescing)
13245bb644a0SJohannes Berg 		return;
13255bb644a0SJohannes Berg 
13263393a608SJuuso Oikarinen 	if (!timer_needed)
13273393a608SJuuso Oikarinen 		return;
13283393a608SJuuso Oikarinen 
132926d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
133026d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1331f0706e82SJiri Benc }
1332f0706e82SJiri Benc 
13337bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
13347bedd0cfSJohannes Berg {
13357bedd0cfSJohannes Berg 	int err;
13367bedd0cfSJohannes Berg 
133783e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
13387bedd0cfSJohannes Berg 	if (err)
13397bedd0cfSJohannes Berg 		return err;
13407bedd0cfSJohannes Berg 
1341ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1342ba6ddab9SJohannes Berg 	if (err) {
1343ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1344ba6ddab9SJohannes Berg 		return err;
1345ba6ddab9SJohannes Berg 	}
1346ba6ddab9SJohannes Berg 
13474d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
134834e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1349f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1350f0706e82SJiri Benc 
135134f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
13527bedd0cfSJohannes Berg 	return 0;
1353f0706e82SJiri Benc }
1354f0706e82SJiri Benc 
1355f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1356f0706e82SJiri Benc {
1357a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
135883e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1359ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1360f0706e82SJiri Benc }
1361f0706e82SJiri Benc 
1362051007d9SJohannes Berg 
1363e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1364f0706e82SJiri Benc {
1365b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1366f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1367d778207bSJohannes Berg 	LIST_HEAD(free_list);
136844213b5eSJohannes Berg 	int ret = 0;
1369f0706e82SJiri Benc 
1370d0709a65SJohannes Berg 	might_sleep();
1371d0709a65SJohannes Berg 
1372e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1373e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1374e716251dSJohannes Berg 
137534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
137634e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1377e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1378e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1379d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1380d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
138134316837SJohannes Berg 			ret++;
138234316837SJohannes Berg 		}
138334e89507SJohannes Berg 	}
1384d778207bSJohannes Berg 
1385d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1386d778207bSJohannes Berg 		synchronize_net();
1387d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1388d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1389d778207bSJohannes Berg 	}
139034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
139144213b5eSJohannes Berg 
1392051007d9SJohannes Berg 	return ret;
1393051007d9SJohannes Berg }
1394051007d9SJohannes Berg 
139524723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
139624723d1bSJohannes Berg 			  unsigned long exp_time)
139724723d1bSJohannes Berg {
139824723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
139924723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
140024723d1bSJohannes Berg 
140134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1402e46a2cf9SMohammed Shafi Shajakhan 
1403e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1404b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1405b8da6b6aSJohannes Berg 
1406ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1407ec2b774eSMarek Lindner 			continue;
1408ec2b774eSMarek Lindner 
1409b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1410eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1411bdcbd8e0SJohannes Berg 				sta->sta.addr);
14123f52b7e3SMarco Porsch 
14133f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
14143f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
14153f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
14163f52b7e3SMarco Porsch 
141734e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
141824723d1bSJohannes Berg 		}
1419e46a2cf9SMohammed Shafi Shajakhan 	}
1420e46a2cf9SMohammed Shafi Shajakhan 
142134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
142224723d1bSJohannes Berg }
142317741cdcSJohannes Berg 
1424686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1425686b9cb9SBen Greear 						   const u8 *addr,
1426686b9cb9SBen Greear 						   const u8 *localaddr)
142717741cdcSJohannes Berg {
14287bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
142983e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
14307bedd0cfSJohannes Berg 	struct sta_info *sta;
143117741cdcSJohannes Berg 
1432686b9cb9SBen Greear 	/*
1433686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1434686b9cb9SBen Greear 	 * ... first in list.
1435686b9cb9SBen Greear 	 */
143683e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1437686b9cb9SBen Greear 		if (localaddr &&
1438b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1439686b9cb9SBen Greear 			continue;
1440f7c65594SJohannes Berg 		if (!sta->uploaded)
1441f7c65594SJohannes Berg 			return NULL;
144217741cdcSJohannes Berg 		return &sta->sta;
1443f7c65594SJohannes Berg 	}
1444f7c65594SJohannes Berg 
1445abe60632SJohannes Berg 	return NULL;
144617741cdcSJohannes Berg }
1447686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
14485ed176e1SJohannes Berg 
14495ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
14505ed176e1SJohannes Berg 					 const u8 *addr)
14515ed176e1SJohannes Berg {
1452f7c65594SJohannes Berg 	struct sta_info *sta;
14535ed176e1SJohannes Berg 
14545ed176e1SJohannes Berg 	if (!vif)
14555ed176e1SJohannes Berg 		return NULL;
14565ed176e1SJohannes Berg 
1457f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1458f7c65594SJohannes Berg 	if (!sta)
1459f7c65594SJohannes Berg 		return NULL;
14605ed176e1SJohannes Berg 
1461f7c65594SJohannes Berg 	if (!sta->uploaded)
1462f7c65594SJohannes Berg 		return NULL;
1463f7c65594SJohannes Berg 
1464f7c65594SJohannes Berg 	return &sta->sta;
14655ed176e1SJohannes Berg }
146617741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1467af818581SJohannes Berg 
1468e3685e03SJohannes Berg /* powersave support code */
1469e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
147050a9432dSJohannes Berg {
1471608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1472e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1473e3685e03SJohannes Berg 	struct sk_buff_head pending;
1474ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1475e3685e03SJohannes Berg 	unsigned long flags;
1476d012a605SMarco Porsch 	struct ps_data *ps;
1477d012a605SMarco Porsch 
14783918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
14793918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
14803918edb0SFelix Fietkau 				     u.ap);
14813918edb0SFelix Fietkau 
14823918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1483d012a605SMarco Porsch 		ps = &sdata->bss->ps;
14843f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
14853f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1486d012a605SMarco Porsch 	else
1487d012a605SMarco Porsch 		return;
148850a9432dSJohannes Berg 
1489c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
149047086fc5SJohannes Berg 
14915a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1492948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1493ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1494948d887dSJohannes Berg 
149530686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
149612375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1497af818581SJohannes Berg 
1498ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1499adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1500ba8c3d6fSFelix Fietkau 			continue;
1501ba8c3d6fSFelix Fietkau 
150218667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1503ba8c3d6fSFelix Fietkau 	}
1504ba8c3d6fSFelix Fietkau 
1505948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1506af818581SJohannes Berg 
15071d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
15081d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1509af818581SJohannes Berg 	/* Send all buffered frames to the station */
1510948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1511948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1512948d887dSJohannes Berg 
1513987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1514948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1515987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1516948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1517948d887dSJohannes Berg 		filtered += tmp - count;
1518948d887dSJohannes Berg 		count = tmp;
1519948d887dSJohannes Berg 
1520987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1521948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1522987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1523948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1524948d887dSJohannes Berg 		buffered += tmp - count;
1525948d887dSJohannes Berg 	}
1526948d887dSJohannes Berg 
1527e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
15285ac2e350SJohannes Berg 
15295ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
15305ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
15315ac2e350SJohannes Berg 
15325ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
15335ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
15345ac2e350SJohannes Berg 	 */
15355ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
15365ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
15371d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1538948d887dSJohannes Berg 
1539e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1540e3685e03SJohannes Berg 
1541af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1542af818581SJohannes Berg 
1543c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1544c868cb35SJohannes Berg 
1545bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
15462595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1547bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
154817c18bf8SJohannes Berg 
154917c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1550af818581SJohannes Berg }
1551af818581SJohannes Berg 
15520ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1553b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
15540ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1555ce662b44SJohannes Berg {
15560ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1557ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1558ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1559ce662b44SJohannes Berg 	struct sk_buff *skb;
1560ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1561ce662b44SJohannes Berg 	__le16 fc;
1562a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1563ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
156455de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1565ce662b44SJohannes Berg 
1566ce662b44SJohannes Berg 	if (qos) {
1567ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1568ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1569ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1570ce662b44SJohannes Berg 	} else {
1571ce662b44SJohannes Berg 		size -= 2;
1572ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1573ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1574ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1575ce662b44SJohannes Berg 	}
1576ce662b44SJohannes Berg 
1577ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1578ce662b44SJohannes Berg 	if (!skb)
1579ce662b44SJohannes Berg 		return;
1580ce662b44SJohannes Berg 
1581ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1582ce662b44SJohannes Berg 
15834df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1584ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1585ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1586ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1587ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1588ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1589864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1590ce662b44SJohannes Berg 
1591ce662b44SJohannes Berg 	skb->priority = tid;
1592ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
159359b66255SJohannes Berg 	if (qos) {
1594ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1595ce662b44SJohannes Berg 
15960ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1597ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1598ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
15990ead2510SEmmanuel Grumbach 			if (more_data)
16000ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
16010ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
16020ead2510SEmmanuel Grumbach 		}
1603ce662b44SJohannes Berg 	}
1604ce662b44SJohannes Berg 
1605ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1606ce662b44SJohannes Berg 
1607ce662b44SJohannes Berg 	/*
1608ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1609ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1610deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1611deeaee19SJohannes Berg 	 * ends the poll/service period.
1612ce662b44SJohannes Berg 	 */
161302f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1614deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1615ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1616ce662b44SJohannes Berg 
16176b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
16186b127c71SSujith Manoharan 
1619b77cf4f8SJohannes Berg 	if (call_driver)
1620b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1621b77cf4f8SJohannes Berg 					  reason, false);
162240b96408SJohannes Berg 
162389afe614SJohannes Berg 	skb->dev = sdata->dev;
162489afe614SJohannes Berg 
162555de908aSJohannes Berg 	rcu_read_lock();
1626d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
162755de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
162855de908aSJohannes Berg 		rcu_read_unlock();
162955de908aSJohannes Berg 		kfree_skb(skb);
163055de908aSJohannes Berg 		return;
163155de908aSJohannes Berg 	}
163255de908aSJohannes Berg 
163373c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
163408aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
163555de908aSJohannes Berg 	rcu_read_unlock();
1636ce662b44SJohannes Berg }
1637ce662b44SJohannes Berg 
16380a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
16390a1cb809SJohannes Berg {
16400a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
16410a1cb809SJohannes Berg 	if (tids & 0xF8)
16420a1cb809SJohannes Berg 		return fls(tids) - 1;
16430a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
16440a1cb809SJohannes Berg 	if (tids & BIT(0))
16450a1cb809SJohannes Berg 		return 0;
16460a1cb809SJohannes Berg 	return fls(tids) - 1;
16470a1cb809SJohannes Berg }
16480a1cb809SJohannes Berg 
16490ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
16500ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
16510ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
16520ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
16530ead2510SEmmanuel Grumbach  */
16540ead2510SEmmanuel Grumbach static bool
16550ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
16560ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
16570ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
16580ead2510SEmmanuel Grumbach {
16590ead2510SEmmanuel Grumbach 	int ac;
16600ead2510SEmmanuel Grumbach 
16610ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
16620ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
16630ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
16640ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
16650ead2510SEmmanuel Grumbach 	 */
16660ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
16670ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
16680ead2510SEmmanuel Grumbach 		return true;
16690ead2510SEmmanuel Grumbach 
16700ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1671f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
16720ead2510SEmmanuel Grumbach 			continue;
16730ead2510SEmmanuel Grumbach 
16740ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
16750ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
16760ead2510SEmmanuel Grumbach 			return true;
16770ead2510SEmmanuel Grumbach 	}
16780ead2510SEmmanuel Grumbach 
16790ead2510SEmmanuel Grumbach 	return false;
16800ead2510SEmmanuel Grumbach }
16810ead2510SEmmanuel Grumbach 
168247086fc5SJohannes Berg static void
16830ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
16840ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
16850ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
16860ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1687af818581SJohannes Berg {
1688af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1689af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1690948d887dSJohannes Berg 	int ac;
1691af818581SJohannes Berg 
1692f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1693948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
16944049e09aSJohannes Berg 		unsigned long tids;
16954049e09aSJohannes Berg 
1696f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1697948d887dSJohannes Berg 			continue;
1698948d887dSJohannes Berg 
16994049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
17004049e09aSJohannes Berg 
1701f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1702f9f760b4SJohannes Berg 		 * release from hardware queues
1703f9f760b4SJohannes Berg 		 */
17040ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
17050ead2510SEmmanuel Grumbach 			*driver_release_tids |=
17060ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
17070ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1708ba8c3d6fSFelix Fietkau 		}
1709f9f760b4SJohannes Berg 
17100ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
171147086fc5SJohannes Berg 			struct sk_buff *skb;
171247086fc5SJohannes Berg 
171347086fc5SJohannes Berg 			while (n_frames > 0) {
1714948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1715948d887dSJohannes Berg 				if (!skb) {
171647086fc5SJohannes Berg 					skb = skb_dequeue(
171747086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1718af818581SJohannes Berg 					if (skb)
1719af818581SJohannes Berg 						local->total_ps_buffered--;
1720af818581SJohannes Berg 				}
172147086fc5SJohannes Berg 				if (!skb)
172247086fc5SJohannes Berg 					break;
172347086fc5SJohannes Berg 				n_frames--;
17240ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
172547086fc5SJohannes Berg 			}
1726948d887dSJohannes Berg 		}
1727948d887dSJohannes Berg 
17280ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
17290ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
17300ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
17314049e09aSJohannes Berg 		 */
1732948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17330ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1734948d887dSJohannes Berg 			break;
1735948d887dSJohannes Berg 	}
1736948d887dSJohannes Berg }
1737af818581SJohannes Berg 
17380ead2510SEmmanuel Grumbach static void
17390ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
17400ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
17410ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
17420ead2510SEmmanuel Grumbach {
17430ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
17440ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
17450ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
17460ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
17470ead2510SEmmanuel Grumbach 	bool more_data;
17480ead2510SEmmanuel Grumbach 
17490ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
17500ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
17510ead2510SEmmanuel Grumbach 
17520ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
17530ead2510SEmmanuel Grumbach 
17540ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
17550ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
17560ead2510SEmmanuel Grumbach 
17570ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
17580ead2510SEmmanuel Grumbach 
17591a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
17600ead2510SEmmanuel Grumbach 		driver_release_tids =
17610ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
17620ead2510SEmmanuel Grumbach 
1763f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1764f438ceb8SEmmanuel Grumbach 		int tid, ac;
17654049e09aSJohannes Berg 
1766ce662b44SJohannes Berg 		/*
1767ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1768ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1769ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1770ce662b44SJohannes Berg 		 *
1771ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1772ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1773ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1774ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1775ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1776ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1777ce662b44SJohannes Berg 		 *
1778ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1779ce662b44SJohannes Berg 		 */
1780ce662b44SJohannes Berg 
1781ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1782f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1783d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1784d7f84244SEmmanuel Grumbach 				break;
1785f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1786ce662b44SJohannes Berg 
17870ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1788f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
178947086fc5SJohannes Berg 		struct sk_buff_head pending;
179047086fc5SJohannes Berg 		struct sk_buff *skb;
179140b96408SJohannes Berg 		int num = 0;
179240b96408SJohannes Berg 		u16 tids = 0;
1793b77cf4f8SJohannes Berg 		bool need_null = false;
179447086fc5SJohannes Berg 
179547086fc5SJohannes Berg 		skb_queue_head_init(&pending);
179647086fc5SJohannes Berg 
179747086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1798af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
179947086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
180040b96408SJohannes Berg 			u8 *qoshdr = NULL;
180140b96408SJohannes Berg 
180240b96408SJohannes Berg 			num++;
1803af818581SJohannes Berg 
1804af818581SJohannes Berg 			/*
180547086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
180647086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
180747086fc5SJohannes Berg 			 * exchange.
1808af818581SJohannes Berg 			 */
18096b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
18106b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1811af818581SJohannes Berg 
181247086fc5SJohannes Berg 			/*
181347086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
181447086fc5SJohannes Berg 			 * more buffered frames for this STA
181547086fc5SJohannes Berg 			 */
181624b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
181747086fc5SJohannes Berg 				hdr->frame_control |=
181847086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
181924b9c373SJanusz.Dziedzic@tieto.com 			else
182024b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
182124b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1822af818581SJohannes Berg 
182340b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
182440b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
182540b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
182640b96408SJohannes Berg 
1827b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1828b77cf4f8SJohannes Berg 
1829b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1830b77cf4f8SJohannes Berg 
1831b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1832b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1833b77cf4f8SJohannes Berg 				continue;
1834b77cf4f8SJohannes Berg 
1835b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1836b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1837b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1838b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1839b77cf4f8SJohannes Berg 				break;
1840b77cf4f8SJohannes Berg 			}
1841b77cf4f8SJohannes Berg 
1842b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1843b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1844b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1845b77cf4f8SJohannes Berg 			 * and be done.
1846b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1847b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1848b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1849b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1850b77cf4f8SJohannes Berg 			 *
1851b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1852b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1853b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1854b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1855b77cf4f8SJohannes Berg 			 *
1856b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1857b77cf4f8SJohannes Berg 			 */
1858b77cf4f8SJohannes Berg 			if (qoshdr) {
185940b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1860deeaee19SJohannes Berg 
186147086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
186247086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1863b77cf4f8SJohannes Berg 			} else {
1864b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1865b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1866b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1867b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1868b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1869b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1870b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1871b77cf4f8SJohannes Berg 				 */
1872b77cf4f8SJohannes Berg 				hdr->frame_control |=
1873b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1874b77cf4f8SJohannes Berg 				need_null = true;
1875b77cf4f8SJohannes Berg 				num++;
187652a3f20cSMarco Porsch 			}
1877b77cf4f8SJohannes Berg 			break;
187847086fc5SJohannes Berg 		}
187947086fc5SJohannes Berg 
188040b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
188140b96408SJohannes Berg 					  reason, more_data);
188240b96408SJohannes Berg 
188347086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1884af818581SJohannes Berg 
1885b77cf4f8SJohannes Berg 		if (need_null)
1886b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
18870ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
18880ead2510SEmmanuel Grumbach 				reason, false, false);
1889b77cf4f8SJohannes Berg 
1890c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1891af818581SJohannes Berg 	} else {
1892ba8c3d6fSFelix Fietkau 		int tid;
1893ba8c3d6fSFelix Fietkau 
1894af818581SJohannes Berg 		/*
18954049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
18964049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1897f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1898f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1899f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1900f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1901f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1902f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1903af818581SJohannes Berg 		 */
19044049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
190547086fc5SJohannes Berg 					    n_frames, reason, more_data);
19064049e09aSJohannes Berg 
19074049e09aSJohannes Berg 		/*
19084049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
19094049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1910f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
19114049e09aSJohannes Berg 		 * release function.
1912f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1913ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1914ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
19154049e09aSJohannes Berg 		 */
1916ba8c3d6fSFelix Fietkau 
1917ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1918ba8c3d6fSFelix Fietkau 			return;
1919ba8c3d6fSFelix Fietkau 
1920ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1921adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1922adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
19231e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1924ba8c3d6fSFelix Fietkau 				continue;
1925ba8c3d6fSFelix Fietkau 
1926ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1927ba8c3d6fSFelix Fietkau 			break;
1928ba8c3d6fSFelix Fietkau 		}
1929af818581SJohannes Berg 	}
1930af818581SJohannes Berg }
1931af818581SJohannes Berg 
1932af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1933af818581SJohannes Berg {
193447086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1935af818581SJohannes Berg 
1936af818581SJohannes Berg 	/*
193747086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
193847086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
193947086fc5SJohannes Berg 	 * only from the non-enabled ones.
1940af818581SJohannes Berg 	 */
194147086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
194247086fc5SJohannes Berg 		ignore_for_response = 0;
1943af818581SJohannes Berg 
194447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
194547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1946af818581SJohannes Berg }
194747086fc5SJohannes Berg 
194847086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
194947086fc5SJohannes Berg {
195047086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
195147086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
195247086fc5SJohannes Berg 
195347086fc5SJohannes Berg 	/*
195447086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
195547086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
195647086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
195747086fc5SJohannes Berg 	 * actually getting called.
195847086fc5SJohannes Berg 	 */
195947086fc5SJohannes Berg 	if (!delivery_enabled)
196047086fc5SJohannes Berg 		return;
196147086fc5SJohannes Berg 
196247086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
196347086fc5SJohannes Berg 	case 1:
196447086fc5SJohannes Berg 		n_frames = 2;
196547086fc5SJohannes Berg 		break;
196647086fc5SJohannes Berg 	case 2:
196747086fc5SJohannes Berg 		n_frames = 4;
196847086fc5SJohannes Berg 		break;
196947086fc5SJohannes Berg 	case 3:
197047086fc5SJohannes Berg 		n_frames = 6;
197147086fc5SJohannes Berg 		break;
197247086fc5SJohannes Berg 	case 0:
197347086fc5SJohannes Berg 		/* XXX: what is a good value? */
197413a8098aSAndrei Otcheretianski 		n_frames = 128;
197547086fc5SJohannes Berg 		break;
197647086fc5SJohannes Berg 	}
197747086fc5SJohannes Berg 
197847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
197947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1980af818581SJohannes Berg }
1981af818581SJohannes Berg 
1982af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1983af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1984af818581SJohannes Berg {
1985af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1986af818581SJohannes Berg 
1987b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1988b5878a2dSJohannes Berg 
19895ac2e350SJohannes Berg 	if (block) {
1990c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
199117c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
19925ac2e350SJohannes Berg 		return;
19935ac2e350SJohannes Berg 	}
19945ac2e350SJohannes Berg 
19955ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
19965ac2e350SJohannes Berg 		return;
19975ac2e350SJohannes Berg 
19985ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
19995ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
20005ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20015ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20025ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
20035ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
20045ac2e350SJohannes Berg 		/* must be asleep in this case */
20055ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20065ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20075ac2e350SJohannes Berg 	} else {
20085ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
200917c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
20105ac2e350SJohannes Berg 	}
2011af818581SJohannes Berg }
2012af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
2013dcf55fb5SFelix Fietkau 
2014e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
201537fbd908SJohannes Berg {
201637fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
201737fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
201837fbd908SJohannes Berg 
201937fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
202037fbd908SJohannes Berg 
202137fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
202237fbd908SJohannes Berg }
2023e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
202437fbd908SJohannes Berg 
20250ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
20260ead2510SEmmanuel Grumbach {
20270ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
20280ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
20290ead2510SEmmanuel Grumbach 	bool more_data;
20300ead2510SEmmanuel Grumbach 
20310ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
20320ead2510SEmmanuel Grumbach 
20330ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
20340ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
20350ead2510SEmmanuel Grumbach 					       reason, 0);
20360ead2510SEmmanuel Grumbach 
20370ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
20380ead2510SEmmanuel Grumbach }
20390ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
20400ead2510SEmmanuel Grumbach 
2041042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2042042ec453SJohannes Berg 				u8 tid, bool buffered)
2043dcf55fb5SFelix Fietkau {
2044dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2045dcf55fb5SFelix Fietkau 
20465a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2047042ec453SJohannes Berg 		return;
2048042ec453SJohannes Berg 
20491b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
20501b000789SJohannes Berg 
2051948d887dSJohannes Berg 	if (buffered)
2052948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2053948d887dSJohannes Berg 	else
2054948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2055948d887dSJohannes Berg 
2056c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2057dcf55fb5SFelix Fietkau }
2058042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2059d9a7ddb0SJohannes Berg 
2060b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2061b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2062b4809e94SToke Høiland-Jørgensen {
2063942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2064942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2065942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2066942741daSFelix Fietkau 	u32 airtime = 0;
2067c77bfab9SFelix Fietkau 	u32 diff;
2068b4809e94SToke Høiland-Jørgensen 
2069942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2070942741daSFelix Fietkau 		airtime += tx_airtime;
2071942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2072942741daSFelix Fietkau 		airtime += rx_airtime;
2073b4809e94SToke Høiland-Jørgensen 
2074942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2075942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2076942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2077c77bfab9SFelix Fietkau 
2078c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2079c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2080942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2081c77bfab9SFelix Fietkau 
2082942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2083b4809e94SToke Høiland-Jørgensen }
2084b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2085b4809e94SToke Høiland-Jørgensen 
20863ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
20873ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
20883ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
20893ace10f5SKan Yan {
20903ace10f5SKan Yan 	int tx_pending;
20913ace10f5SKan Yan 
2092911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2093911bde0fSToke Høiland-Jørgensen 		return;
2094911bde0fSToke Høiland-Jørgensen 
20953ace10f5SKan Yan 	if (!tx_completed) {
20963ace10f5SKan Yan 		if (sta)
20973ace10f5SKan Yan 			atomic_add(tx_airtime,
20983ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
20993ace10f5SKan Yan 
21003ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
21018e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
21023ace10f5SKan Yan 		return;
21033ace10f5SKan Yan 	}
21043ace10f5SKan Yan 
21053ace10f5SKan Yan 	if (sta) {
21063ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
21073ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
210804e35caaSFelix Fietkau 		if (tx_pending < 0)
21093ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
21103ace10f5SKan Yan 				       tx_pending, 0);
21113ace10f5SKan Yan 	}
21123ace10f5SKan Yan 
21138e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
21143ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
21158e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
21163ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
21173ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
21183ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
21198e4bac06SFelix Fietkau 		      tx_airtime)) {
21208e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
21213ace10f5SKan Yan 			       tx_pending, 0);
21228e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
21238e4bac06SFelix Fietkau 	}
21243ace10f5SKan Yan }
21253ace10f5SKan Yan 
212683d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2127d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2128d9a7ddb0SJohannes Berg {
21298bf11d8dSJohannes Berg 	might_sleep();
2130d9a7ddb0SJohannes Berg 
2131d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2132d9a7ddb0SJohannes Berg 		return 0;
2133d9a7ddb0SJohannes Berg 
2134f09603a2SJohannes Berg 	/* check allowed transitions first */
2135f09603a2SJohannes Berg 
2136d9a7ddb0SJohannes Berg 	switch (new_state) {
2137d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2138f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2139d9a7ddb0SJohannes Berg 			return -EINVAL;
2140d9a7ddb0SJohannes Berg 		break;
2141d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2142f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2143f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2144d9a7ddb0SJohannes Berg 			return -EINVAL;
2145d9a7ddb0SJohannes Berg 		break;
2146d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2147f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2148f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2149d9a7ddb0SJohannes Berg 			return -EINVAL;
2150d9a7ddb0SJohannes Berg 		break;
2151d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2152f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2153d9a7ddb0SJohannes Berg 			return -EINVAL;
2154d9a7ddb0SJohannes Berg 		break;
2155d9a7ddb0SJohannes Berg 	default:
2156d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2157d9a7ddb0SJohannes Berg 		return -EINVAL;
2158d9a7ddb0SJohannes Berg 	}
2159d9a7ddb0SJohannes Berg 
2160bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2161bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2162f09603a2SJohannes Berg 
2163f09603a2SJohannes Berg 	/*
2164f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2165f09603a2SJohannes Berg 	 * fail the transition
2166f09603a2SJohannes Berg 	 */
2167f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2168f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2169f09603a2SJohannes Berg 					sta->sta_state, new_state);
2170f09603a2SJohannes Berg 		if (err)
2171f09603a2SJohannes Berg 			return err;
2172f09603a2SJohannes Berg 	}
2173f09603a2SJohannes Berg 
2174f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2175f09603a2SJohannes Berg 
2176f09603a2SJohannes Berg 	switch (new_state) {
2177f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2178f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2179f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2180f09603a2SJohannes Berg 		break;
2181f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2182a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2183f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2184a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2185f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
21860cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
218752cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
218852cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2189a7201a6cSIlan Peer 		}
2190f09603a2SJohannes Berg 		break;
2191f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2192f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2193f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
21949cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
21950cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
219652cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
219752cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2198f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
219972f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2200f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
220117c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
220249ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2203f09603a2SJohannes Berg 		}
2204f09603a2SJohannes Berg 		break;
2205f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2206f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
220772f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2208f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
220917c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
221049ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2211f09603a2SJohannes Berg 		}
22123e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
22133e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
22143e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
22153e493173SJouni Malinen 						    sta->sta.addr);
2216f09603a2SJohannes Berg 		break;
2217f09603a2SJohannes Berg 	default:
2218f09603a2SJohannes Berg 		break;
2219f09603a2SJohannes Berg 	}
2220f09603a2SJohannes Berg 
2221d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2222d9a7ddb0SJohannes Berg 
2223d9a7ddb0SJohannes Berg 	return 0;
2224d9a7ddb0SJohannes Berg }
2225687da132SEmmanuel Grumbach 
2226c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2227c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2228c9c5962bSJohannes Berg {
2229046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2230c9c5962bSJohannes Berg 	int cpu;
2231c9c5962bSJohannes Berg 
2232046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2233c9c5962bSJohannes Berg 		return stats;
2234c9c5962bSJohannes Berg 
2235c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2236c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2237c9c5962bSJohannes Berg 
2238046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2239c9c5962bSJohannes Berg 
2240c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2241c9c5962bSJohannes Berg 			stats = cpustats;
2242c9c5962bSJohannes Berg 	}
2243c9c5962bSJohannes Berg 
2244c9c5962bSJohannes Berg 	return stats;
2245c9c5962bSJohannes Berg }
2246c9c5962bSJohannes Berg 
224741cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
22484f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2249fbd6ff5cSJohannes Berg {
2250dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2251fbd6ff5cSJohannes Berg 
2252dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
22537f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
22544f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2255dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2256dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2257dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2258dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22597f406cd1SJohannes Berg 		break;
22607f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
22614f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2262dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2263dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2264dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22657f406cd1SJohannes Berg 		break;
22667f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2267fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2268fbd6ff5cSJohannes Berg 		u16 brate;
22694f6b1b3dSJohannes Berg 		unsigned int shift;
2270dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2271dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2272fbd6ff5cSJohannes Berg 
2273dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
22748b783d10SThomas Pedersen 
22758b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
22768b783d10SThomas Pedersen 			break;
22778b783d10SThomas Pedersen 
2278dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
22794f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
22804f6b1b3dSJohannes Berg 			shift = 2;
22814f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
22824f6b1b3dSJohannes Berg 			shift = 1;
22834f6b1b3dSJohannes Berg 		else
22844f6b1b3dSJohannes Berg 			shift = 0;
2285fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
22867f406cd1SJohannes Berg 		break;
22877f406cd1SJohannes Berg 		}
228841cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
228941cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
229041cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
229141cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
229241cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
229341cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
229441cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
229541cbb0f5SLuca Coelho 		break;
2296fbd6ff5cSJohannes Berg 	}
22974f6b1b3dSJohannes Berg }
2298fbd6ff5cSJohannes Berg 
2299a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
23004f6b1b3dSJohannes Berg {
23016aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
23024f6b1b3dSJohannes Berg 
23034f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2304a17d93ffSBen Greear 		return -EINVAL;
2305a17d93ffSBen Greear 
23064f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2307a17d93ffSBen Greear 	return 0;
2308fbd6ff5cSJohannes Berg }
2309fbd6ff5cSJohannes Berg 
2310b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2311b255b72bSSeevalamuthu Mariappan 					int tid)
2312b255b72bSSeevalamuthu Mariappan {
2313b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2314b255b72bSSeevalamuthu Mariappan 	u64 value;
2315b255b72bSSeevalamuthu Mariappan 
2316b255b72bSSeevalamuthu Mariappan 	do {
2317b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2318b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2319b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2320b255b72bSSeevalamuthu Mariappan 
2321b255b72bSSeevalamuthu Mariappan 	return value;
2322b255b72bSSeevalamuthu Mariappan }
2323b255b72bSSeevalamuthu Mariappan 
23240f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
23250f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
23260f9c5a61SJohannes Berg 			     int tid)
23270f9c5a61SJohannes Berg {
23280f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2329b255b72bSSeevalamuthu Mariappan 	int cpu;
23300f9c5a61SJohannes Berg 
23310f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2332046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2333046d2e7cSSriram R 							   tid);
23340f9c5a61SJohannes Berg 
2335046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2336b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2337b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2338b255b72bSSeevalamuthu Mariappan 
2339046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2340046d2e7cSSriram R 						     cpu);
2341b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2342b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2343b255b72bSSeevalamuthu Mariappan 			}
2344b255b72bSSeevalamuthu Mariappan 		}
23450f9c5a61SJohannes Berg 
23460f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
23470f9c5a61SJohannes Berg 	}
23480f9c5a61SJohannes Berg 
23490f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
23500f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2351046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
23520f9c5a61SJohannes Berg 	}
23530f9c5a61SJohannes Berg 
23540f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
23550f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23560f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2357046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
23580f9c5a61SJohannes Berg 	}
23590f9c5a61SJohannes Berg 
23600f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
23610f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23620f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2363046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
23640f9c5a61SJohannes Berg 	}
23652fe4a29aSToke Høiland-Jørgensen 
23662fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
23672fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
23682fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
23692fe4a29aSToke Høiland-Jørgensen 
23702fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
23712fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
23722fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
23732fe4a29aSToke Høiland-Jørgensen 
23742fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
23752fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
23762fe4a29aSToke Høiland-Jørgensen 	}
23770f9c5a61SJohannes Berg }
23780f9c5a61SJohannes Berg 
2379c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2380c9c5962bSJohannes Berg {
2381c9c5962bSJohannes Berg 	unsigned int start;
2382c9c5962bSJohannes Berg 	u64 value;
2383c9c5962bSJohannes Berg 
2384c9c5962bSJohannes Berg 	do {
2385c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2386c9c5962bSJohannes Berg 		value = rxstats->bytes;
2387c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2388c9c5962bSJohannes Berg 
2389c9c5962bSJohannes Berg 	return value;
2390c9c5962bSJohannes Berg }
2391c9c5962bSJohannes Berg 
23920fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
23930fdf1493SJohannes Berg 		   bool tidstats)
2394b7ffbd7eSJohannes Berg {
2395b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2396b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2397b7ffbd7eSJohannes Berg 	u32 thr = 0;
2398c9c5962bSJohannes Berg 	int i, ac, cpu;
2399c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2400c9c5962bSJohannes Berg 
2401c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2402b7ffbd7eSJohannes Berg 
2403b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2404b7ffbd7eSJohannes Berg 
2405225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2406225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2407225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2408225b8189SJohannes Berg 	 */
2409225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2410bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2411225b8189SJohannes Berg 
24122b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2413a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2414a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2415a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2416a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
24179cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2418a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2419976bd9efSJohannes Berg 
2420976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2421bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2422bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2423a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2424976bd9efSJohannes Berg 	}
2425b7ffbd7eSJohannes Berg 
242684b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
24279cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2428e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2429b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
24302b9a7e1bSJohannes Berg 
2431a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2432a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2433b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
24342b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2435046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2436a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2437b7ffbd7eSJohannes Berg 	}
24382b9a7e1bSJohannes Berg 
2439a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
24402b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
24412b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2442046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2443a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
24442b9a7e1bSJohannes Berg 	}
24452b9a7e1bSJohannes Berg 
2446a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2447a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2448046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
24490f9c5a61SJohannes Berg 
2450046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2451c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2452c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2453c9c5962bSJohannes Berg 
2454046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2455046d2e7cSSriram R 						     cpu);
2456c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2457c9c5962bSJohannes Berg 			}
2458c9c5962bSJohannes Berg 		}
2459c9c5962bSJohannes Berg 
2460a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
24612b9a7e1bSJohannes Berg 	}
24622b9a7e1bSJohannes Berg 
2463a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2464046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2465046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2466c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2467c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2468c9c5962bSJohannes Berg 
2469046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2470046d2e7cSSriram R 						     cpu);
2471c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2472c9c5962bSJohannes Berg 			}
2473c9c5962bSJohannes Berg 		}
2474a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
24752b9a7e1bSJohannes Berg 	}
24762b9a7e1bSJohannes Berg 
2477a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2478046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2479a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
24802b9a7e1bSJohannes Berg 	}
24812b9a7e1bSJohannes Berg 
2482a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2483046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2484a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
24852b9a7e1bSJohannes Berg 	}
24862b9a7e1bSJohannes Berg 
2487b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2488b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2489b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2490b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2491b4809e94SToke Høiland-Jørgensen 	}
2492b4809e94SToke Høiland-Jørgensen 
2493b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2494b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2495b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2496b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2497b4809e94SToke Høiland-Jørgensen 	}
2498b4809e94SToke Høiland-Jørgensen 
2499b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2500942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2501b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2502b4809e94SToke Høiland-Jørgensen 	}
2503b4809e94SToke Høiland-Jørgensen 
2504046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2505046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2506c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2507c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2508c9c5962bSJohannes Berg 
2509046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2510e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2511c9c5962bSJohannes Berg 		}
2512c9c5962bSJohannes Berg 	}
2513b7ffbd7eSJohannes Berg 
2514225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2515225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2516a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2517a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2518225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2519225b8189SJohannes Berg 	}
2520225b8189SJohannes Berg 
252130686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
252230686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2523a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2524c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2525a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2526b7ffbd7eSJohannes Berg 		}
25272b9a7e1bSJohannes Berg 
2528046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2529a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
253040d9a38aSJohannes Berg 			sinfo->signal_avg =
2531046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2532a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
25332b9a7e1bSJohannes Berg 		}
25342b9a7e1bSJohannes Berg 	}
25352b9a7e1bSJohannes Berg 
2536c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2537c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2538c9c5962bSJohannes Berg 	 * pcpu statistics
2539c9c5962bSJohannes Berg 	 */
2540c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2541a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2542a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2543a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2544046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2545a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2546b7ffbd7eSJohannes Berg 
2547c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2548c9c5962bSJohannes Berg 
2549b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2550e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2551c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2552b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2553046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2554b7ffbd7eSJohannes Berg 		}
2555b7ffbd7eSJohannes Berg 	}
2556b7ffbd7eSJohannes Berg 
25578a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
25588a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2559046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2560e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2561a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
25622b9a7e1bSJohannes Berg 	}
25632b9a7e1bSJohannes Berg 
25648a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
25658a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2566a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2567a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
25682b9a7e1bSJohannes Berg 	}
2569b7ffbd7eSJohannes Berg 
25700fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
25716af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
25726af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
25738689c051SArend van Spriel 	}
257479c892b8SJohannes Berg 
2575b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2576b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2577a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2578a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2579a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2580a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2581a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2582dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
25831303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
25841303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2585b7ffbd7eSJohannes Berg 
2586433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2587433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2588433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2589b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2590a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2591433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2592b7ffbd7eSJohannes Berg 		}
2593433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2594433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2595433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2596dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
25971303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2598b7ffbd7eSJohannes Berg #endif
2599b7ffbd7eSJohannes Berg 	}
2600b7ffbd7eSJohannes Berg 
2601b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2602b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2603b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2604b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2605b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2606b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2607b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2608785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2609b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2610b7ffbd7eSJohannes Berg 
2611b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2612b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2613b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2614b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2615b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2616b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2617b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2618b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2619b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2620b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2621b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2622b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2623a74a8c84SJohannes Berg 	if (sta->sta.wme)
2624b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2625b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2626b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2627b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2628b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2629b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2630b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2631b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2632b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2633b7ffbd7eSJohannes Berg 
26343b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
26353b17fbf8SMaxim Altshul 
26363b17fbf8SMaxim Altshul 	if (thr != 0) {
2637a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
26383b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
26393b17fbf8SMaxim Altshul 	}
2640a78b26ffSVenkateswara Naralasetty 
2641a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2642046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2643046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2644a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2645a78b26ffSVenkateswara Naralasetty 	}
2646cc60dbbfSBalaji Pothunoori 
26479c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2648046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2649cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2650cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2651046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2652cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
26539c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2654cc60dbbfSBalaji Pothunoori 	}
2655ab60633cSNarayanraddi Masti 
2656ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2657ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2658ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2659ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2660ab60633cSNarayanraddi Masti 	}
26613b17fbf8SMaxim Altshul }
26623b17fbf8SMaxim Altshul 
26633b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
26643b17fbf8SMaxim Altshul {
26653b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
26663b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
26673b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
26683b17fbf8SMaxim Altshul 	u32 thr = 0;
26693b17fbf8SMaxim Altshul 
26703b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
26713b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
26723b17fbf8SMaxim Altshul 
2673b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2674b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2675b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2676b7ffbd7eSJohannes Berg 	else
26774fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2678b7ffbd7eSJohannes Berg 
26793b17fbf8SMaxim Altshul 	return thr;
2680b7ffbd7eSJohannes Berg }
2681b8da6b6aSJohannes Berg 
2682b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2683b8da6b6aSJohannes Berg {
2684c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2685c9c5962bSJohannes Berg 
2686046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2687046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2688c9c5962bSJohannes Berg 		return stats->last_rx;
2689046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2690b8da6b6aSJohannes Berg }
2691484a54c2SToke Høiland-Jørgensen 
2692484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2693484a54c2SToke Høiland-Jørgensen {
2694484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2695484a54c2SToke Høiland-Jørgensen 		return;
2696484a54c2SToke Høiland-Jørgensen 
2697484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2698484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2699484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2700484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2701484a54c2SToke Høiland-Jørgensen 	} else {
2702484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2703484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2704484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2705484a54c2SToke Høiland-Jørgensen 	}
2706484a54c2SToke Høiland-Jørgensen }
2707484a54c2SToke Høiland-Jørgensen 
2708484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2709484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2710484a54c2SToke Høiland-Jørgensen {
2711484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2712484a54c2SToke Høiland-Jørgensen 
2713484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2714484a54c2SToke Høiland-Jørgensen }
2715cb71f1d1SJohannes Berg 
2716cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2717cb71f1d1SJohannes Berg {
2718cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2719cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2720cb71f1d1SJohannes Berg 	int ret;
2721cb71f1d1SJohannes Berg 
2722cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2723cb71f1d1SJohannes Berg 
2724cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2725cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2726cb71f1d1SJohannes Berg 		return -EINVAL;
2727cb71f1d1SJohannes Berg 
2728cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2729cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2730cb71f1d1SJohannes Berg 		return -EBUSY;
2731cb71f1d1SJohannes Berg 
2732cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2733cb71f1d1SJohannes Berg 	if (!alloc)
2734cb71f1d1SJohannes Berg 		return -ENOMEM;
2735cb71f1d1SJohannes Berg 
2736cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2737cb71f1d1SJohannes Berg 	if (ret) {
2738cb71f1d1SJohannes Berg 		kfree(alloc);
2739cb71f1d1SJohannes Berg 		return ret;
2740cb71f1d1SJohannes Berg 	}
2741cb71f1d1SJohannes Berg 
2742cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2743cb71f1d1SJohannes Berg 
2744cb71f1d1SJohannes Berg 	return 0;
2745cb71f1d1SJohannes Berg }
2746cb71f1d1SJohannes Berg 
274721476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
274821476ad1SShaul Triebitz {
274921476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
275021476ad1SShaul Triebitz 
275121476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
275221476ad1SShaul Triebitz }
275321476ad1SShaul Triebitz 
2754cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2755cb71f1d1SJohannes Berg {
2756cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2757c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2758cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2759cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2760cb71f1d1SJohannes Berg 	int ret;
2761cb71f1d1SJohannes Berg 
2762c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2763c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2764cb71f1d1SJohannes Berg 
2765c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2766cb71f1d1SJohannes Berg 		return -EINVAL;
2767cb71f1d1SJohannes Berg 
2768cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2769cb71f1d1SJohannes Berg 
2770ba6ddab9SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_INSERTED)) {
2771ba6ddab9SJohannes Berg 		ret = 0;
2772ba6ddab9SJohannes Berg 		goto hash;
2773ba6ddab9SJohannes Berg 	}
2774cb71f1d1SJohannes Berg 
2775cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2776cb71f1d1SJohannes Berg 				   old_links, new_links);
2777cb71f1d1SJohannes Berg 	if (ret) {
2778cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2779ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2780cb71f1d1SJohannes Berg 	}
2781cb71f1d1SJohannes Berg 
2782ba6ddab9SJohannes Berg hash:
2783ba6ddab9SJohannes Berg 	link_sta_info_hash_add(sdata->local, link_sta);
2784ba6ddab9SJohannes Berg 
2785cb71f1d1SJohannes Berg 	return ret;
2786cb71f1d1SJohannes Berg }
2787cb71f1d1SJohannes Berg 
2788cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2789cb71f1d1SJohannes Berg {
2790cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2791cb71f1d1SJohannes Berg 
2792cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2793cb71f1d1SJohannes Berg 
2794cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2795cb71f1d1SJohannes Berg 
2796cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2797cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2798cb71f1d1SJohannes Berg 				     sta->sta.valid_links,
2799cb71f1d1SJohannes Berg 				     sta->sta.valid_links & ~BIT(link_id));
2800cb71f1d1SJohannes Berg 
2801ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2802cb71f1d1SJohannes Berg }
2803175ad2ecSJohannes Berg 
2804175ad2ecSJohannes Berg void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2805175ad2ecSJohannes Berg 					   const u8 *ext_capab,
2806175ad2ecSJohannes Berg 					   unsigned int ext_capab_len)
2807175ad2ecSJohannes Berg {
2808175ad2ecSJohannes Berg 	u8 val;
2809175ad2ecSJohannes Berg 
2810175ad2ecSJohannes Berg 	sta->sta.max_amsdu_subframes = 0;
2811175ad2ecSJohannes Berg 
2812175ad2ecSJohannes Berg 	if (ext_capab_len < 8)
2813175ad2ecSJohannes Berg 		return;
2814175ad2ecSJohannes Berg 
2815175ad2ecSJohannes Berg 	/* The sender might not have sent the last bit, consider it to be 0 */
2816175ad2ecSJohannes Berg 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2817175ad2ecSJohannes Berg 
2818175ad2ecSJohannes Berg 	/* we did get all the bits, take the MSB as well */
2819175ad2ecSJohannes Berg 	if (ext_capab_len >= 9)
2820175ad2ecSJohannes Berg 		val |= u8_get_bits(ext_capab[8],
2821175ad2ecSJohannes Berg 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2822175ad2ecSJohannes Berg 
2823175ad2ecSJohannes Berg 	if (val)
2824175ad2ecSJohannes Berg 		sta->sta.max_amsdu_subframes = 4 << val;
2825175ad2ecSJohannes Berg }
2826