xref: /openbmc/linux/net/mac80211/sta_info.c (revision 8e4bac0671054ba1ad2e3d41aa568ac55f51affd)
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 
99ba6ddab9SJohannes Berg static int link_sta_info_hash_del(struct ieee80211_local *local,
100ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
101ba6ddab9SJohannes Berg {
102ba6ddab9SJohannes Berg 	return rhltable_remove(&local->link_sta_hash,
103ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
104ba6ddab9SJohannes Berg 			       link_sta_rht_params);
105ba6ddab9SJohannes Berg }
106ba6ddab9SJohannes Berg 
1075108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
108b22cfcfcSEliad Peller {
109b22cfcfcSEliad Peller 	int ac, i;
110b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
111b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
112b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
113d012a605SMarco Porsch 	struct ps_data *ps;
114b22cfcfcSEliad Peller 
115e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1165ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1175ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
118d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
119d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
120d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1213f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1223f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
123d012a605SMarco Porsch 		else
124d012a605SMarco Porsch 			return;
125b22cfcfcSEliad Peller 
126b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
127e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1285ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
129b22cfcfcSEliad Peller 
130d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
131b22cfcfcSEliad Peller 	}
132b22cfcfcSEliad Peller 
133ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
134ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
135adf8ed01SJohannes Berg 			struct txq_info *txqi;
136adf8ed01SJohannes Berg 
137adf8ed01SJohannes Berg 			if (!sta->sta.txq[i])
138adf8ed01SJohannes Berg 				continue;
139adf8ed01SJohannes Berg 
140adf8ed01SJohannes Berg 			txqi = to_txq_info(sta->sta.txq[i]);
141ba8c3d6fSFelix Fietkau 
142fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
143ba8c3d6fSFelix Fietkau 		}
144ba8c3d6fSFelix Fietkau 	}
145ba8c3d6fSFelix Fietkau 
146b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
147b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1481f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1491f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
150b22cfcfcSEliad Peller 	}
151b22cfcfcSEliad Peller 
15245b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
15345b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
154b22cfcfcSEliad Peller 
1555ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
156b22cfcfcSEliad Peller 
157b22cfcfcSEliad Peller 	/*
158b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
159b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
160b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
161b22cfcfcSEliad Peller 	 * directly by station destruction.
162b22cfcfcSEliad Peller 	 */
1635a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
164661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
165b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
166b22cfcfcSEliad Peller 		if (!tid_tx)
167b22cfcfcSEliad Peller 			continue;
1681f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
169b22cfcfcSEliad Peller 		kfree(tid_tx);
170b22cfcfcSEliad Peller 	}
1715108ca82SJohannes Berg }
172b22cfcfcSEliad Peller 
1735108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1745108ca82SJohannes Berg {
1755108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1765108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1775108ca82SJohannes Berg 
1785108ca82SJohannes Berg 	__cleanup_single_sta(sta);
179b22cfcfcSEliad Peller 	sta_info_free(local, sta);
180b22cfcfcSEliad Peller }
181b22cfcfcSEliad Peller 
18283e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
18383e7e4ceSHerbert Xu 					 const u8 *addr)
18483e7e4ceSHerbert Xu {
18583e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
18683e7e4ceSHerbert Xu }
18783e7e4ceSHerbert Xu 
188d0709a65SJohannes Berg /* protected by RCU */
189abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
190abe60632SJohannes Berg 			      const u8 *addr)
19143ba7e95SJohannes Berg {
192abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
19383e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
19460f4b626SJohannes Berg 	struct sta_info *sta;
19543ba7e95SJohannes Berg 
19660f4b626SJohannes Berg 	rcu_read_lock();
19783e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
19860f4b626SJohannes Berg 		if (sta->sdata == sdata) {
19960f4b626SJohannes Berg 			rcu_read_unlock();
20060f4b626SJohannes Berg 			/* this is safe as the caller must already hold
20160f4b626SJohannes Berg 			 * another rcu read section or the mutex
20260f4b626SJohannes Berg 			 */
20360f4b626SJohannes Berg 			return sta;
20460f4b626SJohannes Berg 		}
20560f4b626SJohannes Berg 	}
20660f4b626SJohannes Berg 	rcu_read_unlock();
20760f4b626SJohannes Berg 	return NULL;
20843ba7e95SJohannes Berg }
20943ba7e95SJohannes Berg 
2100e5ded5aSFelix Fietkau /*
2110e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
2120e5ded5aSFelix Fietkau  * or from one of its vlans
2130e5ded5aSFelix Fietkau  */
2140e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
2150e5ded5aSFelix Fietkau 				  const u8 *addr)
2160e5ded5aSFelix Fietkau {
2170e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
21883e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
2190e5ded5aSFelix Fietkau 	struct sta_info *sta;
2200e5ded5aSFelix Fietkau 
2217bedd0cfSJohannes Berg 	rcu_read_lock();
22283e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2237bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2247bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2257bedd0cfSJohannes Berg 			rcu_read_unlock();
2267bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2277bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2287bedd0cfSJohannes Berg 			 */
2290e5ded5aSFelix Fietkau 			return sta;
2300e5ded5aSFelix Fietkau 		}
2317bedd0cfSJohannes Berg 	}
2327bedd0cfSJohannes Berg 	rcu_read_unlock();
2337bedd0cfSJohannes Berg 	return NULL;
2347bedd0cfSJohannes Berg }
2350e5ded5aSFelix Fietkau 
236ba6ddab9SJohannes Berg struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
237ba6ddab9SJohannes Berg 					      const u8 *addr)
238ba6ddab9SJohannes Berg {
239ba6ddab9SJohannes Berg 	return rhltable_lookup(&local->link_sta_hash, addr,
240ba6ddab9SJohannes Berg 			       link_sta_rht_params);
241ba6ddab9SJohannes Berg }
242ba6ddab9SJohannes Berg 
243ba6ddab9SJohannes Berg struct link_sta_info *
244ba6ddab9SJohannes Berg link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
245ba6ddab9SJohannes Berg {
246ba6ddab9SJohannes Berg 	struct ieee80211_local *local = sdata->local;
247ba6ddab9SJohannes Berg 	struct rhlist_head *tmp;
248ba6ddab9SJohannes Berg 	struct link_sta_info *link_sta;
249ba6ddab9SJohannes Berg 
250ba6ddab9SJohannes Berg 	rcu_read_lock();
251ba6ddab9SJohannes Berg 	for_each_link_sta_info(local, addr, link_sta, tmp) {
252ba6ddab9SJohannes Berg 		struct sta_info *sta = link_sta->sta;
253ba6ddab9SJohannes Berg 
254ba6ddab9SJohannes Berg 		if (sta->sdata == sdata ||
255ba6ddab9SJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
256ba6ddab9SJohannes Berg 			rcu_read_unlock();
257ba6ddab9SJohannes Berg 			/* this is safe as the caller must already hold
258ba6ddab9SJohannes Berg 			 * another rcu read section or the mutex
259ba6ddab9SJohannes Berg 			 */
260ba6ddab9SJohannes Berg 			return link_sta;
261ba6ddab9SJohannes Berg 		}
262ba6ddab9SJohannes Berg 	}
263ba6ddab9SJohannes Berg 	rcu_read_unlock();
264ba6ddab9SJohannes Berg 	return NULL;
265ba6ddab9SJohannes Berg }
266ba6ddab9SJohannes Berg 
2675072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
2685072f73cSToke Høiland-Jørgensen 				       const u8 *sta_addr, const u8 *vif_addr)
2695072f73cSToke Høiland-Jørgensen {
2705072f73cSToke Høiland-Jørgensen 	struct rhlist_head *tmp;
2715072f73cSToke Høiland-Jørgensen 	struct sta_info *sta;
2725072f73cSToke Høiland-Jørgensen 
2735072f73cSToke Høiland-Jørgensen 	for_each_sta_info(local, sta_addr, sta, tmp) {
2745072f73cSToke Høiland-Jørgensen 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
2755072f73cSToke Høiland-Jørgensen 			return sta;
2765072f73cSToke Høiland-Jørgensen 	}
2775072f73cSToke Høiland-Jørgensen 
2785072f73cSToke Høiland-Jørgensen 	return NULL;
2795072f73cSToke Høiland-Jørgensen }
2805072f73cSToke Høiland-Jørgensen 
2813b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2823b53fde8SJohannes Berg 				     int idx)
283ee385855SLuis Carlos Cobo {
2843b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
285ee385855SLuis Carlos Cobo 	struct sta_info *sta;
286ee385855SLuis Carlos Cobo 	int i = 0;
287ee385855SLuis Carlos Cobo 
2888ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
2898ca47eb9SMadhuparna Bhowmik 				lockdep_is_held(&local->sta_mtx)) {
2903b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2912a8ca29aSLuis Carlos Cobo 			continue;
292ee385855SLuis Carlos Cobo 		if (i < idx) {
293ee385855SLuis Carlos Cobo 			++i;
294ee385855SLuis Carlos Cobo 			continue;
295ee385855SLuis Carlos Cobo 		}
2962a8ca29aSLuis Carlos Cobo 		return sta;
297ee385855SLuis Carlos Cobo 	}
298ee385855SLuis Carlos Cobo 
299ee385855SLuis Carlos Cobo 	return NULL;
300ee385855SLuis Carlos Cobo }
301f0706e82SJiri Benc 
302cb71f1d1SJohannes Berg static void sta_info_free_link(struct link_sta_info *link_sta)
303246b39e4SJohannes Berg {
304cb71f1d1SJohannes Berg 	free_percpu(link_sta->pcpu_rx_stats);
305cb71f1d1SJohannes Berg }
306246b39e4SJohannes Berg 
307ba6ddab9SJohannes Berg static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
308ba6ddab9SJohannes Berg 			    bool unhash)
309cb71f1d1SJohannes Berg {
310cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc = NULL;
311c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
312cb71f1d1SJohannes Berg 
313c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
314c71420dbSJohannes Berg 					     lockdep_is_held(&sta->local->sta_mtx));
315c71420dbSJohannes Berg 
316c71420dbSJohannes Berg 	if (WARN_ON(!link_sta))
317cb71f1d1SJohannes Berg 		return;
318246b39e4SJohannes Berg 
319ba6ddab9SJohannes Berg 	if (unhash)
320ba6ddab9SJohannes Berg 		link_sta_info_hash_del(sta->local, link_sta);
321ba6ddab9SJohannes Berg 
322c71420dbSJohannes Berg 	if (link_sta != &sta->deflink)
323c71420dbSJohannes Berg 		alloc = container_of(link_sta, typeof(*alloc), info);
324cb71f1d1SJohannes Berg 
325cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
326c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->link[link_id], NULL);
327c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
328cb71f1d1SJohannes Berg 	if (alloc) {
329cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
330c71420dbSJohannes Berg 		kfree_rcu(alloc, rcu_head);
331246b39e4SJohannes Berg 	}
332246b39e4SJohannes Berg }
333246b39e4SJohannes Berg 
33493e5deb1SJohannes Berg /**
335d9a7ddb0SJohannes Berg  * sta_info_free - free STA
33693e5deb1SJohannes Berg  *
3376ef307bcSRandy Dunlap  * @local: pointer to the global information
33893e5deb1SJohannes Berg  * @sta: STA info to free
33993e5deb1SJohannes Berg  *
34093e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
341d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
342d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
343d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
34493e5deb1SJohannes Berg  */
345d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
34693e5deb1SJohannes Berg {
347cb71f1d1SJohannes Berg 	int i;
348cb71f1d1SJohannes Berg 
349cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
350cb71f1d1SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
351cb71f1d1SJohannes Berg 			continue;
352cb71f1d1SJohannes Berg 
353ba6ddab9SJohannes Berg 		sta_remove_link(sta, i, true);
354cb71f1d1SJohannes Berg 	}
355cb71f1d1SJohannes Berg 
356dcd479e1SJohannes Berg 	/*
357dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
358dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
359dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
360dcd479e1SJohannes Berg 	 *
361dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
362dcd479e1SJohannes Berg 	 * allocated during state transitions.
363dcd479e1SJohannes Berg 	 */
364dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
365dcd479e1SJohannes Berg 		int ret;
366dcd479e1SJohannes Berg 
367dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
368dcd479e1SJohannes Berg 
369dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
370dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
371dcd479e1SJohannes Berg 			break;
372dcd479e1SJohannes Berg 	}
373dcd479e1SJohannes Berg 
374889cbb91SJohannes Berg 	if (sta->rate_ctrl)
3754b7679a5SJohannes Berg 		rate_control_free_sta(sta);
37693e5deb1SJohannes Berg 
377bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
37893e5deb1SJohannes Berg 
379ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
380ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
38153d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
382433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
383433f5bc1SJohannes Berg 	kfree(sta->mesh);
384433f5bc1SJohannes Berg #endif
385246b39e4SJohannes Berg 
386cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
38793e5deb1SJohannes Berg 	kfree(sta);
38893e5deb1SJohannes Berg }
38993e5deb1SJohannes Berg 
3904d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
39162b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
392d0709a65SJohannes Berg 			     struct sta_info *sta)
393f0706e82SJiri Benc {
39483e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
3957bedd0cfSJohannes Berg 			       sta_rht_params);
396f0706e82SJiri Benc }
397f0706e82SJiri Benc 
3985ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
399af818581SJohannes Berg {
400af818581SJohannes Berg 	struct sta_info *sta;
401af818581SJohannes Berg 
4025ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
403af818581SJohannes Berg 
404af818581SJohannes Berg 	if (sta->dead)
405af818581SJohannes Berg 		return;
406af818581SJohannes Berg 
40754420473SHelmut Schaa 	local_bh_disable();
4085ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
409af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
4105ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
411af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
4125ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
41347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
414ce662b44SJohannes Berg 	local_bh_enable();
415af818581SJohannes Berg }
416af818581SJohannes Berg 
417af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
418af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
419af65cd96SJohannes Berg {
42030686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
421af65cd96SJohannes Berg 		return 0;
422af65cd96SJohannes Berg 
423889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
424af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
42535c347acSJohannes Berg 						     sta, gfp);
426889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
427af65cd96SJohannes Berg 		return -ENOMEM;
428af65cd96SJohannes Berg 
429af65cd96SJohannes Berg 	return 0;
430af65cd96SJohannes Berg }
431af65cd96SJohannes Berg 
432cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
433246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
434246b39e4SJohannes Berg 			       gfp_t gfp)
435246b39e4SJohannes Berg {
436246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
437246b39e4SJohannes Berg 	int i;
438246b39e4SJohannes Berg 
439246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
440246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
441246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
442246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
443246b39e4SJohannes Berg 			return -ENOMEM;
444246b39e4SJohannes Berg 	}
445246b39e4SJohannes Berg 
446246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
447246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
448246b39e4SJohannes Berg 
449246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
450246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
451246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
452246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
453246b39e4SJohannes Berg 
454246b39e4SJohannes Berg 	return 0;
455246b39e4SJohannes Berg }
456246b39e4SJohannes Berg 
457cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
458cb71f1d1SJohannes Berg 			      unsigned int link_id,
459cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
460cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
461cb71f1d1SJohannes Berg {
462cb71f1d1SJohannes Berg 	link_info->sta = sta;
463cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
464c71420dbSJohannes Berg 	link_info->pub = link_sta;
465c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
466c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
467cb71f1d1SJohannes Berg }
468cb71f1d1SJohannes Berg 
46973651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
470cb71f1d1SJohannes Berg 				const u8 *addr, int link_id, gfp_t gfp)
471f0706e82SJiri Benc {
472d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
473ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
474f0706e82SJiri Benc 	struct sta_info *sta;
47516c5f15cSRon Rindjunsky 	int i;
476f0706e82SJiri Benc 
477ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
478f0706e82SJiri Benc 	if (!sta)
47973651ee6SJohannes Berg 		return NULL;
480f0706e82SJiri Benc 
481246b39e4SJohannes Berg 	sta->local = local;
482246b39e4SJohannes Berg 	sta->sdata = sdata;
483246b39e4SJohannes Berg 
484cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
485246b39e4SJohannes Berg 		return NULL;
486c9c5962bSJohannes Berg 
487cb71f1d1SJohannes Berg 	if (link_id >= 0) {
488cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
489cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
490cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
491cb71f1d1SJohannes Berg 	} else {
492cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
493cb71f1d1SJohannes Berg 	}
494cb71f1d1SJohannes Berg 
49507346f81SJohannes Berg 	spin_lock_init(&sta->lock);
4961d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
4975ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
49867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
499a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
50087f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
501433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
502433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
503433f5bc1SJohannes Berg 		if (!sta->mesh)
504433f5bc1SJohannes Berg 			goto free;
5054c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
506433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
50745d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5084c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5094c02d62fSKees Cook 				    0);
510433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
511433f5bc1SJohannes Berg 	}
51287f59c70SThomas Pedersen #endif
51307346f81SJohannes Berg 
514ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
51517741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
516630c7e46SJohannes Berg 	memcpy(sta->deflink.addr, addr, ETH_ALEN);
517630c7e46SJohannes Berg 	memcpy(sta->sta.deflink.addr, addr, ETH_ALEN);
518480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
519480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
520480dd46bSMaxim Altshul 
521046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
522046d2e7cSSriram R 
52396fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
52496fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
52596fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
52696fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
52796fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
52896fc6efbSAlexander Wetzel 	 */
52996fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
53096fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
53196fc6efbSAlexander Wetzel 
5320f9c5a61SJohannes Berg 
5333a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5343a11ce08SJohannes Berg 
53571ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
53671ec375cSJohannes Berg 
537b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
538b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
539b6da911bSLiad Kaufman 
54084b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
541541a45a1SBruno Randolf 
542ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
543ba8c3d6fSFelix Fietkau 		void *txq_data;
544ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
545ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
546ba8c3d6fSFelix Fietkau 
547ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
548ba8c3d6fSFelix Fietkau 		if (!txq_data)
549ba8c3d6fSFelix Fietkau 			goto free;
550ba8c3d6fSFelix Fietkau 
551ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
552ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
553ba8c3d6fSFelix Fietkau 
554adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
555fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
556abfbc3afSJohannes Berg 		}
557ba8c3d6fSFelix Fietkau 	}
558ba8c3d6fSFelix Fietkau 
559ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
560ba8c3d6fSFelix Fietkau 		goto free_txq;
561f0706e82SJiri Benc 
562942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
563b4809e94SToke Høiland-Jørgensen 
564948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
565948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
566948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
567942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
568942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
569942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
570942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
571948d887dSJohannes Berg 	}
57273651ee6SJohannes Berg 
5735a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
5744be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
575cccaec98SSenthil Balasubramanian 
576bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
577bd718fc1SJohannes Berg 		u32 mandatory = 0;
578bd718fc1SJohannes Berg 		int r;
579bd718fc1SJohannes Berg 
580bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
581bd718fc1SJohannes Berg 			continue;
582bd718fc1SJohannes Berg 
583bd718fc1SJohannes Berg 		switch (i) {
584bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
58563fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
586bd718fc1SJohannes Berg 			/*
587bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
588bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
589bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
590bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
591bd718fc1SJohannes Berg 			 * possible rate anyway.
592bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
593bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
594bd718fc1SJohannes Berg 			 */
595bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
596bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
597bd718fc1SJohannes Berg 			break;
598bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
599bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
600bd718fc1SJohannes Berg 			break;
601bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
602bd718fc1SJohannes Berg 			WARN_ON(1);
603bd718fc1SJohannes Berg 			mandatory = 0;
604bd718fc1SJohannes Berg 			break;
605bd718fc1SJohannes Berg 		}
606bd718fc1SJohannes Berg 
607bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
608bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
609bd718fc1SJohannes Berg 
610bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
611bd718fc1SJohannes Berg 
612bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
613bd718fc1SJohannes Berg 				continue;
614046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
615bd718fc1SJohannes Berg 		}
616bd718fc1SJohannes Berg 	}
617bd718fc1SJohannes Berg 
618af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
619687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
620687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
62121a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
62221a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
62321a8e9ddSMohammed Shafi Shajakhan 
62421a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
62521a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
62621a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
62721a8e9ddSMohammed Shafi Shajakhan 
62821a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
629687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
630687da132SEmmanuel Grumbach 		/*
631687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
632687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
633687da132SEmmanuel Grumbach 		 */
634687da132SEmmanuel Grumbach 		switch (smps) {
635687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
636687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
637687da132SEmmanuel Grumbach 			break;
638687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
639687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
640687da132SEmmanuel Grumbach 			break;
641687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
642687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
643687da132SEmmanuel Grumbach 			break;
644687da132SEmmanuel Grumbach 		default:
645687da132SEmmanuel Grumbach 			WARN_ON(1);
646687da132SEmmanuel Grumbach 		}
647687da132SEmmanuel Grumbach 	}
648af0ed69bSJohannes Berg 
6496e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
6506e0456b5SFelix Fietkau 
651484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
652484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
653484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
654484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
655dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
656dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
657484a54c2SToke Høiland-Jørgensen 
658bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
659ef04a297SJohannes Berg 
660abfbc3afSJohannes Berg 	return sta;
661ba8c3d6fSFelix Fietkau 
662ba8c3d6fSFelix Fietkau free_txq:
663ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
664ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
665ba8c3d6fSFelix Fietkau free:
666cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
667433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
668433f5bc1SJohannes Berg 	kfree(sta->mesh);
669433f5bc1SJohannes Berg #endif
670ba8c3d6fSFelix Fietkau 	kfree(sta);
671ba8c3d6fSFelix Fietkau 	return NULL;
67273651ee6SJohannes Berg }
67373651ee6SJohannes Berg 
6748c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
67534e89507SJohannes Berg {
67634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
67734e89507SJohannes Berg 
67803e4497eSJohannes Berg 	/*
67903e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
68003e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
68103e4497eSJohannes Berg 	 * and another CPU turns off the net device.
68203e4497eSJohannes Berg 	 */
6838c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
6848c71df7aSGuy Eilam 		return -ENETDOWN;
68503e4497eSJohannes Berg 
686b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
687deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
6888c71df7aSGuy Eilam 		return -EINVAL;
6898c71df7aSGuy Eilam 
69083e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
69183e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
69283e7e4ceSHerbert Xu 	 * for correctness.
69331104891SJohannes Berg 	 */
69431104891SJohannes Berg 	rcu_read_lock();
69531104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
69631104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
69731104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
69831104891SJohannes Berg 		rcu_read_unlock();
69931104891SJohannes Berg 		return -ENOTUNIQ;
70031104891SJohannes Berg 	}
70131104891SJohannes Berg 	rcu_read_unlock();
70231104891SJohannes Berg 
7038c71df7aSGuy Eilam 	return 0;
70493e5deb1SJohannes Berg }
70544213b5eSJohannes Berg 
706f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
707f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
708f09603a2SJohannes Berg 				     struct sta_info *sta)
709f09603a2SJohannes Berg {
710f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
711f09603a2SJohannes Berg 	int err = 0;
712f09603a2SJohannes Berg 
713f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
714f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
715f09603a2SJohannes Berg 		if (err)
716f09603a2SJohannes Berg 			break;
717f09603a2SJohannes Berg 	}
718f09603a2SJohannes Berg 
719f09603a2SJohannes Berg 	if (!err) {
720a4ec45a4SJohannes Berg 		/*
721a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
722a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
723a4ec45a4SJohannes Berg 		 */
724a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
725f09603a2SJohannes Berg 			sta->uploaded = true;
726f09603a2SJohannes Berg 		return 0;
727f09603a2SJohannes Berg 	}
728f09603a2SJohannes Berg 
729f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
730bdcbd8e0SJohannes Berg 		sdata_info(sdata,
731bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
732bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
733f09603a2SJohannes Berg 		err = 0;
734f09603a2SJohannes Berg 	}
735f09603a2SJohannes Berg 
736f09603a2SJohannes Berg 	/* unwind on error */
737f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
738f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
739f09603a2SJohannes Berg 
740f09603a2SJohannes Berg 	return err;
741f09603a2SJohannes Berg }
742f09603a2SJohannes Berg 
743d405fd8cSGregory Greenman static void
744d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
745d405fd8cSGregory Greenman {
746d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
747d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
748d405fd8cSGregory Greenman 	struct sta_info *sta;
749d405fd8cSGregory Greenman 
750d405fd8cSGregory Greenman 	rcu_read_lock();
751d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
752d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
753d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
754d405fd8cSGregory Greenman 			continue;
755d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
756d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
757d405fd8cSGregory Greenman 			break;
758d405fd8cSGregory Greenman 		}
759d405fd8cSGregory Greenman 	}
760d405fd8cSGregory Greenman 	rcu_read_unlock();
761d405fd8cSGregory Greenman 
762d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
763d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
7647b7090b4SJohannes Berg 		ieee80211_link_info_change_notify(sdata, 0, BSS_CHANGED_P2P_PS);
765d405fd8cSGregory Greenman 	}
766d405fd8cSGregory Greenman }
767d405fd8cSGregory Greenman 
76834e89507SJohannes Berg /*
7698c71df7aSGuy Eilam  * should be called with sta_mtx locked
7708c71df7aSGuy Eilam  * this function replaces the mutex lock
7718c71df7aSGuy Eilam  * with a RCU lock
7728c71df7aSGuy Eilam  */
7734d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
7748c71df7aSGuy Eilam {
7758c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
7768c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
7770c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
7788c71df7aSGuy Eilam 	int err = 0;
7798c71df7aSGuy Eilam 
7808c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
7818c71df7aSGuy Eilam 
7827852e361SJohannes Berg 	/* check if STA exists already */
7837852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
7844d33960bSJohannes Berg 		err = -EEXIST;
7858f9dcc29SAhmed Zaki 		goto out_cleanup;
78634e89507SJohannes Berg 	}
78734e89507SJohannes Berg 
7880c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
7890c2e3842SKoen Vandeputte 	if (!sinfo) {
7900c2e3842SKoen Vandeputte 		err = -ENOMEM;
7918f9dcc29SAhmed Zaki 		goto out_cleanup;
7920c2e3842SKoen Vandeputte 	}
7930c2e3842SKoen Vandeputte 
7944d33960bSJohannes Berg 	local->num_sta++;
7954d33960bSJohannes Berg 	local->sta_generation++;
7964d33960bSJohannes Berg 	smp_mb();
7974d33960bSJohannes Berg 
7985108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
7995108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
8005108ca82SJohannes Berg 
8014d33960bSJohannes Berg 	/* make the station visible */
80262b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
80362b14b24SJohannes Berg 	if (err)
80462b14b24SJohannes Berg 		goto out_drop_sta;
8054d33960bSJohannes Berg 
8062bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
80783d5cc01SJohannes Berg 
8084dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8094dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8104dde3c36SMordechay Goodstein 	 */
8114dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8124dde3c36SMordechay Goodstein 		ieee80211_recalc_min_chandef(sta->sdata);
8134dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8144dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8154dde3c36SMordechay Goodstein 	}
8164dde3c36SMordechay Goodstein 
8175108ca82SJohannes Berg 	/* notify driver */
8185108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8195108ca82SJohannes Berg 	if (err)
8205108ca82SJohannes Berg 		goto out_remove;
8215108ca82SJohannes Berg 
82283d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
823d405fd8cSGregory Greenman 
8245108ca82SJohannes Berg 	/* accept BA sessions now */
8255108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8264d33960bSJohannes Berg 
8274d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8284d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
8294d33960bSJohannes Berg 
8300ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
8310ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
8320ef049dcSArnd Bergmann 	kfree(sinfo);
833d0709a65SJohannes Berg 
834bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
835f0706e82SJiri Benc 
83634e89507SJohannes Berg 	/* move reference to rcu-protected */
83734e89507SJohannes Berg 	rcu_read_lock();
83834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
839e9f207f0SJiri Benc 
84073651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
84173651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
84273651ee6SJohannes Berg 
84373651ee6SJohannes Berg 	return 0;
8445108ca82SJohannes Berg  out_remove:
8455108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
8465108ca82SJohannes Berg 	list_del_rcu(&sta->list);
84762b14b24SJohannes Berg  out_drop_sta:
8485108ca82SJohannes Berg 	local->num_sta--;
8495108ca82SJohannes Berg 	synchronize_net();
8508f9dcc29SAhmed Zaki  out_cleanup:
8517bc40aedSJohannes Berg 	cleanup_single_sta(sta);
8524d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
853ea32f065SSudip Mukherjee 	kfree(sinfo);
8544d33960bSJohannes Berg 	rcu_read_lock();
8554d33960bSJohannes Berg 	return err;
8568c71df7aSGuy Eilam }
8578c71df7aSGuy Eilam 
8588c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
8598c71df7aSGuy Eilam {
8608c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
861308f7fcfSZhao, Gang 	int err;
8628c71df7aSGuy Eilam 
8634d33960bSJohannes Berg 	might_sleep();
8644d33960bSJohannes Berg 
86531104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
86631104891SJohannes Berg 
8678c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
8688c71df7aSGuy Eilam 	if (err) {
8697bc40aedSJohannes Berg 		sta_info_free(local, sta);
87031104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
8718c71df7aSGuy Eilam 		rcu_read_lock();
8727bc40aedSJohannes Berg 		return err;
8738c71df7aSGuy Eilam 	}
8748c71df7aSGuy Eilam 
8757bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
876f0706e82SJiri Benc }
877f0706e82SJiri Benc 
87834e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
87934e89507SJohannes Berg {
88034e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
88134e89507SJohannes Berg 
88234e89507SJohannes Berg 	rcu_read_unlock();
88334e89507SJohannes Berg 
88434e89507SJohannes Berg 	return err;
88534e89507SJohannes Berg }
88634e89507SJohannes Berg 
887d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
888004c872eSJohannes Berg {
889004c872eSJohannes Berg 	/*
890004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
891004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
892004c872eSJohannes Berg 	 */
893d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
894004c872eSJohannes Berg }
895004c872eSJohannes Berg 
896d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
897004c872eSJohannes Berg {
898004c872eSJohannes Berg 	/*
899004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
900004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
901004c872eSJohannes Berg 	 */
902d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
903004c872eSJohannes Berg }
904004c872eSJohannes Berg 
9053d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9063d5839b6SIlan Peer {
9073d5839b6SIlan Peer 	/*
9083d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9093d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9103d5839b6SIlan Peer 	 */
9113d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9123d5839b6SIlan Peer }
9133d5839b6SIlan Peer 
914948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
915004c872eSJohannes Berg {
916948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
917948d887dSJohannes Berg 	switch (ac) {
918948d887dSJohannes Berg 	case IEEE80211_AC_VO:
919948d887dSJohannes Berg 		return BIT(6) | BIT(7);
920948d887dSJohannes Berg 	case IEEE80211_AC_VI:
921948d887dSJohannes Berg 		return BIT(4) | BIT(5);
922948d887dSJohannes Berg 	case IEEE80211_AC_BE:
923948d887dSJohannes Berg 		return BIT(0) | BIT(3);
924948d887dSJohannes Berg 	case IEEE80211_AC_BK:
925948d887dSJohannes Berg 		return BIT(1) | BIT(2);
926948d887dSJohannes Berg 	default:
927948d887dSJohannes Berg 		WARN_ON(1);
928948d887dSJohannes Berg 		return 0;
929d0709a65SJohannes Berg 	}
930004c872eSJohannes Berg }
931004c872eSJohannes Berg 
9329b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
933004c872eSJohannes Berg {
934c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
935d012a605SMarco Porsch 	struct ps_data *ps;
936948d887dSJohannes Berg 	bool indicate_tim = false;
937948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
938948d887dSJohannes Berg 	int ac;
939a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
940004c872eSJohannes Berg 
941d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
942d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
943c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
944c868cb35SJohannes Berg 			return;
9453e122be0SJohannes Berg 
946d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
9473f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
9483f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
9493f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
9503f52b7e3SMarco Porsch #endif
951d012a605SMarco Porsch 	} else {
952d012a605SMarco Porsch 		return;
953d012a605SMarco Porsch 	}
954d012a605SMarco Porsch 
955c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
956d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
957c868cb35SJohannes Berg 		return;
958004c872eSJohannes Berg 
959c868cb35SJohannes Berg 	if (sta->dead)
960c868cb35SJohannes Berg 		goto done;
9613e122be0SJohannes Berg 
962948d887dSJohannes Berg 	/*
963948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
964948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
965948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
966948d887dSJohannes Berg 	 * non-enabled ones.
967948d887dSJohannes Berg 	 */
968948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
969948d887dSJohannes Berg 		ignore_for_tim = 0;
970948d887dSJohannes Berg 
9719b7a86f3SJohannes Berg 	if (ignore_pending)
9729b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
9739b7a86f3SJohannes Berg 
974948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
975948d887dSJohannes Berg 		unsigned long tids;
976948d887dSJohannes Berg 
977f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
978948d887dSJohannes Berg 			continue;
979948d887dSJohannes Berg 
980948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
981948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
982948d887dSJohannes Berg 		if (indicate_tim)
983948d887dSJohannes Berg 			break;
984948d887dSJohannes Berg 
985948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
986948d887dSJohannes Berg 
987948d887dSJohannes Berg 		indicate_tim |=
988948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
989ba8c3d6fSFelix Fietkau 		indicate_tim |=
990ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
991004c872eSJohannes Berg 	}
992004c872eSJohannes Berg 
993c868cb35SJohannes Berg  done:
99465f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
995004c872eSJohannes Berg 
9963d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
9973d5839b6SIlan Peer 		goto out_unlock;
9983d5839b6SIlan Peer 
999948d887dSJohannes Berg 	if (indicate_tim)
1000d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1001c868cb35SJohannes Berg 	else
1002d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10033e122be0SJohannes Berg 
10049b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1005c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1006948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1007c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1008004c872eSJohannes Berg 	}
1009004c872eSJohannes Berg 
10103d5839b6SIlan Peer out_unlock:
101165f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1012004c872eSJohannes Berg }
1013004c872eSJohannes Berg 
10149b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10159b7a86f3SJohannes Berg {
10169b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10179b7a86f3SJohannes Berg }
10189b7a86f3SJohannes Berg 
1019cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1020f0706e82SJiri Benc {
1021e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1022f0706e82SJiri Benc 	int timeout;
1023f0706e82SJiri Benc 
1024f0706e82SJiri Benc 	if (!skb)
1025cd0b8d89SJohannes Berg 		return false;
1026f0706e82SJiri Benc 
1027e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1028f0706e82SJiri Benc 
1029f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
103057c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
103157c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
103257c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1033f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1034f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1035e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1036f0706e82SJiri Benc }
1037f0706e82SJiri Benc 
1038f0706e82SJiri Benc 
1039948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1040948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1041f0706e82SJiri Benc {
1042f0706e82SJiri Benc 	unsigned long flags;
1043f0706e82SJiri Benc 	struct sk_buff *skb;
1044f0706e82SJiri Benc 
104560750397SJohannes Berg 	/*
104660750397SJohannes Berg 	 * First check for frames that should expire on the filtered
104760750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
104860750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
104960750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
105060750397SJohannes Berg 	 * total_ps_buffered counter.
105160750397SJohannes Berg 	 */
1052f0706e82SJiri Benc 	for (;;) {
1053948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1054948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
105557c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1056948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1057836341a7SJohannes Berg 		else
1058f0706e82SJiri Benc 			skb = NULL;
1059948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1060f0706e82SJiri Benc 
106160750397SJohannes Berg 		/*
106260750397SJohannes Berg 		 * Frames are queued in order, so if this one
106360750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
106460750397SJohannes Berg 		 * we actually reached the end of the queue we
106560750397SJohannes Berg 		 * also need to stop, of course.
106660750397SJohannes Berg 		 */
106760750397SJohannes Berg 		if (!skb)
106860750397SJohannes Berg 			break;
1069d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
107060750397SJohannes Berg 	}
107160750397SJohannes Berg 
107260750397SJohannes Berg 	/*
107360750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
107460750397SJohannes Berg 	 * only find something if the filtered queue was emptied
107560750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
107660750397SJohannes Berg 	 * buffered frames.
107760750397SJohannes Berg 	 */
1078f0706e82SJiri Benc 	for (;;) {
1079948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1080948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1081f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1082948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1083f0706e82SJiri Benc 		else
1084f0706e82SJiri Benc 			skb = NULL;
1085948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1086f0706e82SJiri Benc 
108760750397SJohannes Berg 		/*
108860750397SJohannes Berg 		 * frames are queued in order, so if this one
108960750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
109060750397SJohannes Berg 		 * the queue) we can stop testing
109160750397SJohannes Berg 		 */
1092836341a7SJohannes Berg 		if (!skb)
1093836341a7SJohannes Berg 			break;
1094836341a7SJohannes Berg 
1095f0706e82SJiri Benc 		local->total_ps_buffered--;
1096bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1097bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1098d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1099f0706e82SJiri Benc 	}
11003393a608SJuuso Oikarinen 
110160750397SJohannes Berg 	/*
110260750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
110360750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
110460750397SJohannes Berg 	 * frames.
110560750397SJohannes Berg 	 */
110660750397SJohannes Berg 	sta_info_recalc_tim(sta);
110760750397SJohannes Berg 
110860750397SJohannes Berg 	/*
110960750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
111060750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
111160750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
111260750397SJohannes Berg 	 */
1113948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1114948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1115948d887dSJohannes Berg }
1116948d887dSJohannes Berg 
1117948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1118948d887dSJohannes Berg 					     struct sta_info *sta)
1119948d887dSJohannes Berg {
1120948d887dSJohannes Berg 	bool have_buffered = false;
1121948d887dSJohannes Berg 	int ac;
1122948d887dSJohannes Berg 
11233f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11243f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11253f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1126948d887dSJohannes Berg 		return false;
1127948d887dSJohannes Berg 
1128948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1129948d887dSJohannes Berg 		have_buffered |=
1130948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1131948d887dSJohannes Berg 
1132948d887dSJohannes Berg 	return have_buffered;
1133f0706e82SJiri Benc }
1134f0706e82SJiri Benc 
1135d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
113634e89507SJohannes Berg {
113734e89507SJohannes Berg 	struct ieee80211_local *local;
113834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
11396d10e46bSJohannes Berg 	int ret;
114034e89507SJohannes Berg 
114134e89507SJohannes Berg 	might_sleep();
114234e89507SJohannes Berg 
114334e89507SJohannes Berg 	if (!sta)
114434e89507SJohannes Berg 		return -ENOENT;
114534e89507SJohannes Berg 
114634e89507SJohannes Berg 	local = sta->local;
114734e89507SJohannes Berg 	sdata = sta->sdata;
114834e89507SJohannes Berg 
114983d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
115083d5cc01SJohannes Berg 
1151098a6070SJohannes Berg 	/*
1152098a6070SJohannes Berg 	 * Before removing the station from the driver and
1153098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1154098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1155098a6070SJohannes Berg 	 * will be sufficient.
1156098a6070SJohannes Berg 	 */
1157c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1158c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1159098a6070SJohannes Berg 
1160f59374ebSSara Sharon 	/*
1161f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1162f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1163f59374ebSSara Sharon 	 * all such frames to be processed.
1164f59374ebSSara Sharon 	 */
1165f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1166f59374ebSSara Sharon 
116734e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1168b01711beSJohannes Berg 	if (WARN_ON(ret))
116934e89507SJohannes Berg 		return ret;
117034e89507SJohannes Berg 
1171a7a6bdd0SArik Nemtsov 	/*
1172a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1173a7a6bdd0SArik Nemtsov 	 * removal.
1174a7a6bdd0SArik Nemtsov 	 */
1175a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1176a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1177a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1178a7a6bdd0SArik Nemtsov 	}
1179a7a6bdd0SArik Nemtsov 
1180794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1181ef044763SEliad Peller 	sta->removed = true;
11824d33960bSJohannes Berg 
11836a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
11846a9d1b91SJohannes Berg 
1185a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1186a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1187a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1188a710c816SJohannes Berg 
1189d778207bSJohannes Berg 	return 0;
1190d778207bSJohannes Berg }
1191d778207bSJohannes Berg 
1192d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1193d778207bSJohannes Berg {
1194d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1195d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
11960ef049dcSArnd Bergmann 	struct station_info *sinfo;
1197d778207bSJohannes Berg 	int ret;
1198d778207bSJohannes Berg 
1199d778207bSJohannes Berg 	/*
1200d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1201d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1202d778207bSJohannes Berg 	 */
1203d778207bSJohannes Berg 
1204d778207bSJohannes Berg 	might_sleep();
1205d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1206d778207bSJohannes Berg 
12075981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1208b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1209b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1210b16798f5SJohannes Berg 	}
1211b16798f5SJohannes Berg 
1212c8782078SJohannes Berg 	/* now keys can no longer be reached */
12136d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
121434e89507SJohannes Berg 
12159b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
12169b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
12179b7a86f3SJohannes Berg 
121834e89507SJohannes Berg 	sta->dead = true;
121934e89507SJohannes Berg 
122034e89507SJohannes Berg 	local->num_sta--;
122134e89507SJohannes Berg 	local->sta_generation++;
122234e89507SJohannes Berg 
122383d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1224f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1225f09603a2SJohannes Berg 		if (ret) {
122683d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
122783d5cc01SJohannes Berg 			break;
122883d5cc01SJohannes Berg 		}
122983d5cc01SJohannes Berg 	}
1230d9a7ddb0SJohannes Berg 
1231f09603a2SJohannes Berg 	if (sta->uploaded) {
1232f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1233f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1234f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1235f09603a2SJohannes Berg 	}
123634e89507SJohannes Berg 
1237bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1238bdcbd8e0SJohannes Berg 
12390ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
12400ef049dcSArnd Bergmann 	if (sinfo)
12410fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
12420ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
12430ef049dcSArnd Bergmann 	kfree(sinfo);
1244ec15e68bSJouni Malinen 
124534e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
124634e89507SJohannes Berg 
12473a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
12483a11ce08SJohannes Berg 
1249d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1250d778207bSJohannes Berg }
1251d778207bSJohannes Berg 
1252d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1253d778207bSJohannes Berg {
1254d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1255d778207bSJohannes Berg 
1256d778207bSJohannes Berg 	if (err)
1257d778207bSJohannes Berg 		return err;
1258d778207bSJohannes Berg 
1259d778207bSJohannes Berg 	synchronize_net();
1260d778207bSJohannes Berg 
1261d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
126234e89507SJohannes Berg 
126334e89507SJohannes Berg 	return 0;
126434e89507SJohannes Berg }
126534e89507SJohannes Berg 
126634e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
126734e89507SJohannes Berg {
126834e89507SJohannes Berg 	struct sta_info *sta;
126934e89507SJohannes Berg 	int ret;
127034e89507SJohannes Berg 
127134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12727852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
127334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
127434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
127534e89507SJohannes Berg 
127634e89507SJohannes Berg 	return ret;
127734e89507SJohannes Berg }
127834e89507SJohannes Berg 
127934e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
128034e89507SJohannes Berg 			      const u8 *addr)
128134e89507SJohannes Berg {
128234e89507SJohannes Berg 	struct sta_info *sta;
128334e89507SJohannes Berg 	int ret;
128434e89507SJohannes Berg 
128534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12867852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
128734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
128834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
128934e89507SJohannes Berg 
129034e89507SJohannes Berg 	return ret;
129134e89507SJohannes Berg }
1292f0706e82SJiri Benc 
129334f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1294f0706e82SJiri Benc {
129534f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1296f0706e82SJiri Benc 	struct sta_info *sta;
12973393a608SJuuso Oikarinen 	bool timer_needed = false;
1298f0706e82SJiri Benc 
1299d0709a65SJohannes Berg 	rcu_read_lock();
1300d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
13013393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
13023393a608SJuuso Oikarinen 			timer_needed = true;
1303d0709a65SJohannes Berg 	rcu_read_unlock();
1304f0706e82SJiri Benc 
13055bb644a0SJohannes Berg 	if (local->quiescing)
13065bb644a0SJohannes Berg 		return;
13075bb644a0SJohannes Berg 
13083393a608SJuuso Oikarinen 	if (!timer_needed)
13093393a608SJuuso Oikarinen 		return;
13103393a608SJuuso Oikarinen 
131126d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
131226d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1313f0706e82SJiri Benc }
1314f0706e82SJiri Benc 
13157bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
13167bedd0cfSJohannes Berg {
13177bedd0cfSJohannes Berg 	int err;
13187bedd0cfSJohannes Berg 
131983e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
13207bedd0cfSJohannes Berg 	if (err)
13217bedd0cfSJohannes Berg 		return err;
13227bedd0cfSJohannes Berg 
1323ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1324ba6ddab9SJohannes Berg 	if (err) {
1325ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1326ba6ddab9SJohannes Berg 		return err;
1327ba6ddab9SJohannes Berg 	}
1328ba6ddab9SJohannes Berg 
13294d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
133034e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1331f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1332f0706e82SJiri Benc 
133334f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
13347bedd0cfSJohannes Berg 	return 0;
1335f0706e82SJiri Benc }
1336f0706e82SJiri Benc 
1337f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1338f0706e82SJiri Benc {
1339a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
134083e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1341ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1342f0706e82SJiri Benc }
1343f0706e82SJiri Benc 
1344051007d9SJohannes Berg 
1345e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1346f0706e82SJiri Benc {
1347b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1348f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1349d778207bSJohannes Berg 	LIST_HEAD(free_list);
135044213b5eSJohannes Berg 	int ret = 0;
1351f0706e82SJiri Benc 
1352d0709a65SJohannes Berg 	might_sleep();
1353d0709a65SJohannes Berg 
1354e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1355e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1356e716251dSJohannes Berg 
135734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
135834e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1359e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1360e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1361d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1362d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
136334316837SJohannes Berg 			ret++;
136434316837SJohannes Berg 		}
136534e89507SJohannes Berg 	}
1366d778207bSJohannes Berg 
1367d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1368d778207bSJohannes Berg 		synchronize_net();
1369d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1370d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1371d778207bSJohannes Berg 	}
137234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
137344213b5eSJohannes Berg 
1374051007d9SJohannes Berg 	return ret;
1375051007d9SJohannes Berg }
1376051007d9SJohannes Berg 
137724723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
137824723d1bSJohannes Berg 			  unsigned long exp_time)
137924723d1bSJohannes Berg {
138024723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
138124723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
138224723d1bSJohannes Berg 
138334e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1384e46a2cf9SMohammed Shafi Shajakhan 
1385e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1386b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1387b8da6b6aSJohannes Berg 
1388ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1389ec2b774eSMarek Lindner 			continue;
1390ec2b774eSMarek Lindner 
1391b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1392eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1393bdcbd8e0SJohannes Berg 				sta->sta.addr);
13943f52b7e3SMarco Porsch 
13953f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
13963f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
13973f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
13983f52b7e3SMarco Porsch 
139934e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
140024723d1bSJohannes Berg 		}
1401e46a2cf9SMohammed Shafi Shajakhan 	}
1402e46a2cf9SMohammed Shafi Shajakhan 
140334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
140424723d1bSJohannes Berg }
140517741cdcSJohannes Berg 
1406686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1407686b9cb9SBen Greear 						   const u8 *addr,
1408686b9cb9SBen Greear 						   const u8 *localaddr)
140917741cdcSJohannes Berg {
14107bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
141183e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
14127bedd0cfSJohannes Berg 	struct sta_info *sta;
141317741cdcSJohannes Berg 
1414686b9cb9SBen Greear 	/*
1415686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1416686b9cb9SBen Greear 	 * ... first in list.
1417686b9cb9SBen Greear 	 */
141883e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1419686b9cb9SBen Greear 		if (localaddr &&
1420b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1421686b9cb9SBen Greear 			continue;
1422f7c65594SJohannes Berg 		if (!sta->uploaded)
1423f7c65594SJohannes Berg 			return NULL;
142417741cdcSJohannes Berg 		return &sta->sta;
1425f7c65594SJohannes Berg 	}
1426f7c65594SJohannes Berg 
1427abe60632SJohannes Berg 	return NULL;
142817741cdcSJohannes Berg }
1429686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
14305ed176e1SJohannes Berg 
14315ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
14325ed176e1SJohannes Berg 					 const u8 *addr)
14335ed176e1SJohannes Berg {
1434f7c65594SJohannes Berg 	struct sta_info *sta;
14355ed176e1SJohannes Berg 
14365ed176e1SJohannes Berg 	if (!vif)
14375ed176e1SJohannes Berg 		return NULL;
14385ed176e1SJohannes Berg 
1439f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1440f7c65594SJohannes Berg 	if (!sta)
1441f7c65594SJohannes Berg 		return NULL;
14425ed176e1SJohannes Berg 
1443f7c65594SJohannes Berg 	if (!sta->uploaded)
1444f7c65594SJohannes Berg 		return NULL;
1445f7c65594SJohannes Berg 
1446f7c65594SJohannes Berg 	return &sta->sta;
14475ed176e1SJohannes Berg }
144817741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1449af818581SJohannes Berg 
1450e3685e03SJohannes Berg /* powersave support code */
1451e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
145250a9432dSJohannes Berg {
1453608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1454e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1455e3685e03SJohannes Berg 	struct sk_buff_head pending;
1456ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1457e3685e03SJohannes Berg 	unsigned long flags;
1458d012a605SMarco Porsch 	struct ps_data *ps;
1459d012a605SMarco Porsch 
14603918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
14613918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
14623918edb0SFelix Fietkau 				     u.ap);
14633918edb0SFelix Fietkau 
14643918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1465d012a605SMarco Porsch 		ps = &sdata->bss->ps;
14663f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
14673f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1468d012a605SMarco Porsch 	else
1469d012a605SMarco Porsch 		return;
147050a9432dSJohannes Berg 
1471c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
147247086fc5SJohannes Berg 
14735a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1474948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1475ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1476948d887dSJohannes Berg 
147730686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
147812375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1479af818581SJohannes Berg 
1480ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1481adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1482ba8c3d6fSFelix Fietkau 			continue;
1483ba8c3d6fSFelix Fietkau 
148418667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1485ba8c3d6fSFelix Fietkau 	}
1486ba8c3d6fSFelix Fietkau 
1487948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1488af818581SJohannes Berg 
14891d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
14901d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1491af818581SJohannes Berg 	/* Send all buffered frames to the station */
1492948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1493948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1494948d887dSJohannes Berg 
1495987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1496948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1497987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1498948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1499948d887dSJohannes Berg 		filtered += tmp - count;
1500948d887dSJohannes Berg 		count = tmp;
1501948d887dSJohannes Berg 
1502987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1503948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1504987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1505948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1506948d887dSJohannes Berg 		buffered += tmp - count;
1507948d887dSJohannes Berg 	}
1508948d887dSJohannes Berg 
1509e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
15105ac2e350SJohannes Berg 
15115ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
15125ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
15135ac2e350SJohannes Berg 
15145ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
15155ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
15165ac2e350SJohannes Berg 	 */
15175ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
15185ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
15191d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1520948d887dSJohannes Berg 
1521e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1522e3685e03SJohannes Berg 
1523af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1524af818581SJohannes Berg 
1525c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1526c868cb35SJohannes Berg 
1527bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
15282595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1529bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
153017c18bf8SJohannes Berg 
153117c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1532af818581SJohannes Berg }
1533af818581SJohannes Berg 
15340ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1535b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
15360ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1537ce662b44SJohannes Berg {
15380ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1539ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1540ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1541ce662b44SJohannes Berg 	struct sk_buff *skb;
1542ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1543ce662b44SJohannes Berg 	__le16 fc;
1544a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1545ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
154655de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1547ce662b44SJohannes Berg 
1548ce662b44SJohannes Berg 	if (qos) {
1549ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1550ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1551ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1552ce662b44SJohannes Berg 	} else {
1553ce662b44SJohannes Berg 		size -= 2;
1554ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1555ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1556ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1557ce662b44SJohannes Berg 	}
1558ce662b44SJohannes Berg 
1559ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1560ce662b44SJohannes Berg 	if (!skb)
1561ce662b44SJohannes Berg 		return;
1562ce662b44SJohannes Berg 
1563ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1564ce662b44SJohannes Berg 
15654df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1566ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1567ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1568ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1569ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1570ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1571864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1572ce662b44SJohannes Berg 
1573ce662b44SJohannes Berg 	skb->priority = tid;
1574ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
157559b66255SJohannes Berg 	if (qos) {
1576ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1577ce662b44SJohannes Berg 
15780ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1579ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1580ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
15810ead2510SEmmanuel Grumbach 			if (more_data)
15820ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
15830ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
15840ead2510SEmmanuel Grumbach 		}
1585ce662b44SJohannes Berg 	}
1586ce662b44SJohannes Berg 
1587ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1588ce662b44SJohannes Berg 
1589ce662b44SJohannes Berg 	/*
1590ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1591ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1592deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1593deeaee19SJohannes Berg 	 * ends the poll/service period.
1594ce662b44SJohannes Berg 	 */
159502f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1596deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1597ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1598ce662b44SJohannes Berg 
15996b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
16006b127c71SSujith Manoharan 
1601b77cf4f8SJohannes Berg 	if (call_driver)
1602b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1603b77cf4f8SJohannes Berg 					  reason, false);
160440b96408SJohannes Berg 
160589afe614SJohannes Berg 	skb->dev = sdata->dev;
160689afe614SJohannes Berg 
160755de908aSJohannes Berg 	rcu_read_lock();
1608d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
160955de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
161055de908aSJohannes Berg 		rcu_read_unlock();
161155de908aSJohannes Berg 		kfree_skb(skb);
161255de908aSJohannes Berg 		return;
161355de908aSJohannes Berg 	}
161455de908aSJohannes Berg 
161573c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
161608aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
161755de908aSJohannes Berg 	rcu_read_unlock();
1618ce662b44SJohannes Berg }
1619ce662b44SJohannes Berg 
16200a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
16210a1cb809SJohannes Berg {
16220a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
16230a1cb809SJohannes Berg 	if (tids & 0xF8)
16240a1cb809SJohannes Berg 		return fls(tids) - 1;
16250a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
16260a1cb809SJohannes Berg 	if (tids & BIT(0))
16270a1cb809SJohannes Berg 		return 0;
16280a1cb809SJohannes Berg 	return fls(tids) - 1;
16290a1cb809SJohannes Berg }
16300a1cb809SJohannes Berg 
16310ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
16320ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
16330ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
16340ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
16350ead2510SEmmanuel Grumbach  */
16360ead2510SEmmanuel Grumbach static bool
16370ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
16380ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
16390ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
16400ead2510SEmmanuel Grumbach {
16410ead2510SEmmanuel Grumbach 	int ac;
16420ead2510SEmmanuel Grumbach 
16430ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
16440ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
16450ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
16460ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
16470ead2510SEmmanuel Grumbach 	 */
16480ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
16490ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
16500ead2510SEmmanuel Grumbach 		return true;
16510ead2510SEmmanuel Grumbach 
16520ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1653f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
16540ead2510SEmmanuel Grumbach 			continue;
16550ead2510SEmmanuel Grumbach 
16560ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
16570ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
16580ead2510SEmmanuel Grumbach 			return true;
16590ead2510SEmmanuel Grumbach 	}
16600ead2510SEmmanuel Grumbach 
16610ead2510SEmmanuel Grumbach 	return false;
16620ead2510SEmmanuel Grumbach }
16630ead2510SEmmanuel Grumbach 
166447086fc5SJohannes Berg static void
16650ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
16660ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
16670ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
16680ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1669af818581SJohannes Berg {
1670af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1671af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1672948d887dSJohannes Berg 	int ac;
1673af818581SJohannes Berg 
1674f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1675948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
16764049e09aSJohannes Berg 		unsigned long tids;
16774049e09aSJohannes Berg 
1678f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1679948d887dSJohannes Berg 			continue;
1680948d887dSJohannes Berg 
16814049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
16824049e09aSJohannes Berg 
1683f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1684f9f760b4SJohannes Berg 		 * release from hardware queues
1685f9f760b4SJohannes Berg 		 */
16860ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
16870ead2510SEmmanuel Grumbach 			*driver_release_tids |=
16880ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
16890ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1690ba8c3d6fSFelix Fietkau 		}
1691f9f760b4SJohannes Berg 
16920ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
169347086fc5SJohannes Berg 			struct sk_buff *skb;
169447086fc5SJohannes Berg 
169547086fc5SJohannes Berg 			while (n_frames > 0) {
1696948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1697948d887dSJohannes Berg 				if (!skb) {
169847086fc5SJohannes Berg 					skb = skb_dequeue(
169947086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1700af818581SJohannes Berg 					if (skb)
1701af818581SJohannes Berg 						local->total_ps_buffered--;
1702af818581SJohannes Berg 				}
170347086fc5SJohannes Berg 				if (!skb)
170447086fc5SJohannes Berg 					break;
170547086fc5SJohannes Berg 				n_frames--;
17060ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
170747086fc5SJohannes Berg 			}
1708948d887dSJohannes Berg 		}
1709948d887dSJohannes Berg 
17100ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
17110ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
17120ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
17134049e09aSJohannes Berg 		 */
1714948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17150ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1716948d887dSJohannes Berg 			break;
1717948d887dSJohannes Berg 	}
1718948d887dSJohannes Berg }
1719af818581SJohannes Berg 
17200ead2510SEmmanuel Grumbach static void
17210ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
17220ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
17230ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
17240ead2510SEmmanuel Grumbach {
17250ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
17260ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
17270ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
17280ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
17290ead2510SEmmanuel Grumbach 	bool more_data;
17300ead2510SEmmanuel Grumbach 
17310ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
17320ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
17330ead2510SEmmanuel Grumbach 
17340ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
17350ead2510SEmmanuel Grumbach 
17360ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
17370ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
17380ead2510SEmmanuel Grumbach 
17390ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
17400ead2510SEmmanuel Grumbach 
17411a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
17420ead2510SEmmanuel Grumbach 		driver_release_tids =
17430ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
17440ead2510SEmmanuel Grumbach 
1745f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1746f438ceb8SEmmanuel Grumbach 		int tid, ac;
17474049e09aSJohannes Berg 
1748ce662b44SJohannes Berg 		/*
1749ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1750ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1751ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1752ce662b44SJohannes Berg 		 *
1753ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1754ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1755ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1756ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1757ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1758ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1759ce662b44SJohannes Berg 		 *
1760ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1761ce662b44SJohannes Berg 		 */
1762ce662b44SJohannes Berg 
1763ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1764f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1765d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1766d7f84244SEmmanuel Grumbach 				break;
1767f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1768ce662b44SJohannes Berg 
17690ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1770f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
177147086fc5SJohannes Berg 		struct sk_buff_head pending;
177247086fc5SJohannes Berg 		struct sk_buff *skb;
177340b96408SJohannes Berg 		int num = 0;
177440b96408SJohannes Berg 		u16 tids = 0;
1775b77cf4f8SJohannes Berg 		bool need_null = false;
177647086fc5SJohannes Berg 
177747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
177847086fc5SJohannes Berg 
177947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1780af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
178147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
178240b96408SJohannes Berg 			u8 *qoshdr = NULL;
178340b96408SJohannes Berg 
178440b96408SJohannes Berg 			num++;
1785af818581SJohannes Berg 
1786af818581SJohannes Berg 			/*
178747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
178847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
178947086fc5SJohannes Berg 			 * exchange.
1790af818581SJohannes Berg 			 */
17916b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
17926b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1793af818581SJohannes Berg 
179447086fc5SJohannes Berg 			/*
179547086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
179647086fc5SJohannes Berg 			 * more buffered frames for this STA
179747086fc5SJohannes Berg 			 */
179824b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
179947086fc5SJohannes Berg 				hdr->frame_control |=
180047086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
180124b9c373SJanusz.Dziedzic@tieto.com 			else
180224b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
180324b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1804af818581SJohannes Berg 
180540b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
180640b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
180740b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
180840b96408SJohannes Berg 
1809b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1810b77cf4f8SJohannes Berg 
1811b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1812b77cf4f8SJohannes Berg 
1813b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1814b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1815b77cf4f8SJohannes Berg 				continue;
1816b77cf4f8SJohannes Berg 
1817b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1818b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1819b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1820b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1821b77cf4f8SJohannes Berg 				break;
1822b77cf4f8SJohannes Berg 			}
1823b77cf4f8SJohannes Berg 
1824b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1825b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1826b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1827b77cf4f8SJohannes Berg 			 * and be done.
1828b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1829b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1830b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1831b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1832b77cf4f8SJohannes Berg 			 *
1833b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1834b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1835b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1836b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1837b77cf4f8SJohannes Berg 			 *
1838b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1839b77cf4f8SJohannes Berg 			 */
1840b77cf4f8SJohannes Berg 			if (qoshdr) {
184140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1842deeaee19SJohannes Berg 
184347086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
184447086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1845b77cf4f8SJohannes Berg 			} else {
1846b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1847b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1848b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1849b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1850b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1851b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1852b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1853b77cf4f8SJohannes Berg 				 */
1854b77cf4f8SJohannes Berg 				hdr->frame_control |=
1855b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1856b77cf4f8SJohannes Berg 				need_null = true;
1857b77cf4f8SJohannes Berg 				num++;
185852a3f20cSMarco Porsch 			}
1859b77cf4f8SJohannes Berg 			break;
186047086fc5SJohannes Berg 		}
186147086fc5SJohannes Berg 
186240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
186340b96408SJohannes Berg 					  reason, more_data);
186440b96408SJohannes Berg 
186547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1866af818581SJohannes Berg 
1867b77cf4f8SJohannes Berg 		if (need_null)
1868b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
18690ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
18700ead2510SEmmanuel Grumbach 				reason, false, false);
1871b77cf4f8SJohannes Berg 
1872c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1873af818581SJohannes Berg 	} else {
1874ba8c3d6fSFelix Fietkau 		int tid;
1875ba8c3d6fSFelix Fietkau 
1876af818581SJohannes Berg 		/*
18774049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
18784049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1879f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1880f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1881f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1882f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1883f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1884f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1885af818581SJohannes Berg 		 */
18864049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
188747086fc5SJohannes Berg 					    n_frames, reason, more_data);
18884049e09aSJohannes Berg 
18894049e09aSJohannes Berg 		/*
18904049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
18914049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1892f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
18934049e09aSJohannes Berg 		 * release function.
1894f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1895ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1896ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
18974049e09aSJohannes Berg 		 */
1898ba8c3d6fSFelix Fietkau 
1899ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1900ba8c3d6fSFelix Fietkau 			return;
1901ba8c3d6fSFelix Fietkau 
1902ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1903adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1904adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
19051e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1906ba8c3d6fSFelix Fietkau 				continue;
1907ba8c3d6fSFelix Fietkau 
1908ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1909ba8c3d6fSFelix Fietkau 			break;
1910ba8c3d6fSFelix Fietkau 		}
1911af818581SJohannes Berg 	}
1912af818581SJohannes Berg }
1913af818581SJohannes Berg 
1914af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1915af818581SJohannes Berg {
191647086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1917af818581SJohannes Berg 
1918af818581SJohannes Berg 	/*
191947086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
192047086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
192147086fc5SJohannes Berg 	 * only from the non-enabled ones.
1922af818581SJohannes Berg 	 */
192347086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
192447086fc5SJohannes Berg 		ignore_for_response = 0;
1925af818581SJohannes Berg 
192647086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
192747086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1928af818581SJohannes Berg }
192947086fc5SJohannes Berg 
193047086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
193147086fc5SJohannes Berg {
193247086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
193347086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
193447086fc5SJohannes Berg 
193547086fc5SJohannes Berg 	/*
193647086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
193747086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
193847086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
193947086fc5SJohannes Berg 	 * actually getting called.
194047086fc5SJohannes Berg 	 */
194147086fc5SJohannes Berg 	if (!delivery_enabled)
194247086fc5SJohannes Berg 		return;
194347086fc5SJohannes Berg 
194447086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
194547086fc5SJohannes Berg 	case 1:
194647086fc5SJohannes Berg 		n_frames = 2;
194747086fc5SJohannes Berg 		break;
194847086fc5SJohannes Berg 	case 2:
194947086fc5SJohannes Berg 		n_frames = 4;
195047086fc5SJohannes Berg 		break;
195147086fc5SJohannes Berg 	case 3:
195247086fc5SJohannes Berg 		n_frames = 6;
195347086fc5SJohannes Berg 		break;
195447086fc5SJohannes Berg 	case 0:
195547086fc5SJohannes Berg 		/* XXX: what is a good value? */
195613a8098aSAndrei Otcheretianski 		n_frames = 128;
195747086fc5SJohannes Berg 		break;
195847086fc5SJohannes Berg 	}
195947086fc5SJohannes Berg 
196047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
196147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1962af818581SJohannes Berg }
1963af818581SJohannes Berg 
1964af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1965af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1966af818581SJohannes Berg {
1967af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1968af818581SJohannes Berg 
1969b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1970b5878a2dSJohannes Berg 
19715ac2e350SJohannes Berg 	if (block) {
1972c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
197317c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
19745ac2e350SJohannes Berg 		return;
19755ac2e350SJohannes Berg 	}
19765ac2e350SJohannes Berg 
19775ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
19785ac2e350SJohannes Berg 		return;
19795ac2e350SJohannes Berg 
19805ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
19815ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
19825ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
19835ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
19845ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
19855ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
19865ac2e350SJohannes Berg 		/* must be asleep in this case */
19875ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
19885ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
19895ac2e350SJohannes Berg 	} else {
19905ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
199117c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
19925ac2e350SJohannes Berg 	}
1993af818581SJohannes Berg }
1994af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1995dcf55fb5SFelix Fietkau 
1996e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
199737fbd908SJohannes Berg {
199837fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
199937fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
200037fbd908SJohannes Berg 
200137fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
200237fbd908SJohannes Berg 
200337fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
200437fbd908SJohannes Berg }
2005e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
200637fbd908SJohannes Berg 
20070ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
20080ead2510SEmmanuel Grumbach {
20090ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
20100ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
20110ead2510SEmmanuel Grumbach 	bool more_data;
20120ead2510SEmmanuel Grumbach 
20130ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
20140ead2510SEmmanuel Grumbach 
20150ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
20160ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
20170ead2510SEmmanuel Grumbach 					       reason, 0);
20180ead2510SEmmanuel Grumbach 
20190ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
20200ead2510SEmmanuel Grumbach }
20210ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
20220ead2510SEmmanuel Grumbach 
2023042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2024042ec453SJohannes Berg 				u8 tid, bool buffered)
2025dcf55fb5SFelix Fietkau {
2026dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2027dcf55fb5SFelix Fietkau 
20285a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2029042ec453SJohannes Berg 		return;
2030042ec453SJohannes Berg 
20311b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
20321b000789SJohannes Berg 
2033948d887dSJohannes Berg 	if (buffered)
2034948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2035948d887dSJohannes Berg 	else
2036948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2037948d887dSJohannes Berg 
2038c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2039dcf55fb5SFelix Fietkau }
2040042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2041d9a7ddb0SJohannes Berg 
2042b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2043b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2044b4809e94SToke Høiland-Jørgensen {
2045942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2046942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2047942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2048942741daSFelix Fietkau 	u32 airtime = 0;
2049b4809e94SToke Høiland-Jørgensen 
2050942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2051942741daSFelix Fietkau 		airtime += tx_airtime;
2052942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2053942741daSFelix Fietkau 		airtime += rx_airtime;
2054b4809e94SToke Høiland-Jørgensen 
2055942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2056942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2057942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2058942741daSFelix Fietkau 	sta->airtime[ac].deficit -= airtime;
2059942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2060b4809e94SToke Høiland-Jørgensen }
2061b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2062b4809e94SToke Høiland-Jørgensen 
20633ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
20643ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
20653ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
20663ace10f5SKan Yan {
20673ace10f5SKan Yan 	int tx_pending;
20683ace10f5SKan Yan 
2069911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2070911bde0fSToke Høiland-Jørgensen 		return;
2071911bde0fSToke Høiland-Jørgensen 
20723ace10f5SKan Yan 	if (!tx_completed) {
20733ace10f5SKan Yan 		if (sta)
20743ace10f5SKan Yan 			atomic_add(tx_airtime,
20753ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
20763ace10f5SKan Yan 
20773ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2078*8e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
20793ace10f5SKan Yan 		return;
20803ace10f5SKan Yan 	}
20813ace10f5SKan Yan 
20823ace10f5SKan Yan 	if (sta) {
20833ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
20843ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
208504e35caaSFelix Fietkau 		if (tx_pending < 0)
20863ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
20873ace10f5SKan Yan 				       tx_pending, 0);
20883ace10f5SKan Yan 	}
20893ace10f5SKan Yan 
2090*8e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
20913ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
2092*8e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
20933ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
20943ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
20953ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
2096*8e4bac06SFelix Fietkau 		      tx_airtime)) {
2097*8e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
20983ace10f5SKan Yan 			       tx_pending, 0);
2099*8e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2100*8e4bac06SFelix Fietkau 	}
21013ace10f5SKan Yan }
21023ace10f5SKan Yan 
210383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2104d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2105d9a7ddb0SJohannes Berg {
21068bf11d8dSJohannes Berg 	might_sleep();
2107d9a7ddb0SJohannes Berg 
2108d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2109d9a7ddb0SJohannes Berg 		return 0;
2110d9a7ddb0SJohannes Berg 
2111f09603a2SJohannes Berg 	/* check allowed transitions first */
2112f09603a2SJohannes Berg 
2113d9a7ddb0SJohannes Berg 	switch (new_state) {
2114d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2115f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2116d9a7ddb0SJohannes Berg 			return -EINVAL;
2117d9a7ddb0SJohannes Berg 		break;
2118d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2119f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2120f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2121d9a7ddb0SJohannes Berg 			return -EINVAL;
2122d9a7ddb0SJohannes Berg 		break;
2123d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2124f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2125f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2126d9a7ddb0SJohannes Berg 			return -EINVAL;
2127d9a7ddb0SJohannes Berg 		break;
2128d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2129f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2130d9a7ddb0SJohannes Berg 			return -EINVAL;
2131d9a7ddb0SJohannes Berg 		break;
2132d9a7ddb0SJohannes Berg 	default:
2133d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2134d9a7ddb0SJohannes Berg 		return -EINVAL;
2135d9a7ddb0SJohannes Berg 	}
2136d9a7ddb0SJohannes Berg 
2137bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2138bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2139f09603a2SJohannes Berg 
2140f09603a2SJohannes Berg 	/*
2141f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2142f09603a2SJohannes Berg 	 * fail the transition
2143f09603a2SJohannes Berg 	 */
2144f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2145f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2146f09603a2SJohannes Berg 					sta->sta_state, new_state);
2147f09603a2SJohannes Berg 		if (err)
2148f09603a2SJohannes Berg 			return err;
2149f09603a2SJohannes Berg 	}
2150f09603a2SJohannes Berg 
2151f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2152f09603a2SJohannes Berg 
2153f09603a2SJohannes Berg 	switch (new_state) {
2154f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2155f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2156f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2157f09603a2SJohannes Berg 		break;
2158f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2159a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2160f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2161a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2162f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2163a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
216452cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
216552cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2166a7201a6cSIlan Peer 		}
2167f09603a2SJohannes Berg 		break;
2168f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2169f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2170f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
21719cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
2172a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
217352cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
217452cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2175f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
217672f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2177f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
217817c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
217949ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2180f09603a2SJohannes Berg 		}
2181f09603a2SJohannes Berg 		break;
2182f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2183f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
218472f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2185f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
218617c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
218749ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2188f09603a2SJohannes Berg 		}
21893e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
21903e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
21913e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
21923e493173SJouni Malinen 						    sta->sta.addr);
2193f09603a2SJohannes Berg 		break;
2194f09603a2SJohannes Berg 	default:
2195f09603a2SJohannes Berg 		break;
2196f09603a2SJohannes Berg 	}
2197f09603a2SJohannes Berg 
2198d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2199d9a7ddb0SJohannes Berg 
2200d9a7ddb0SJohannes Berg 	return 0;
2201d9a7ddb0SJohannes Berg }
2202687da132SEmmanuel Grumbach 
2203c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2204c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2205c9c5962bSJohannes Berg {
2206046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2207c9c5962bSJohannes Berg 	int cpu;
2208c9c5962bSJohannes Berg 
2209046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2210c9c5962bSJohannes Berg 		return stats;
2211c9c5962bSJohannes Berg 
2212c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2213c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2214c9c5962bSJohannes Berg 
2215046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2216c9c5962bSJohannes Berg 
2217c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2218c9c5962bSJohannes Berg 			stats = cpustats;
2219c9c5962bSJohannes Berg 	}
2220c9c5962bSJohannes Berg 
2221c9c5962bSJohannes Berg 	return stats;
2222c9c5962bSJohannes Berg }
2223c9c5962bSJohannes Berg 
222441cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
22254f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2226fbd6ff5cSJohannes Berg {
2227dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2228fbd6ff5cSJohannes Berg 
2229dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
22307f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
22314f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2232dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2233dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2234dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2235dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22367f406cd1SJohannes Berg 		break;
22377f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
22384f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2239dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2240dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2241dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22427f406cd1SJohannes Berg 		break;
22437f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2244fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2245fbd6ff5cSJohannes Berg 		u16 brate;
22464f6b1b3dSJohannes Berg 		unsigned int shift;
2247dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2248dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2249fbd6ff5cSJohannes Berg 
2250dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
22518b783d10SThomas Pedersen 
22528b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
22538b783d10SThomas Pedersen 			break;
22548b783d10SThomas Pedersen 
2255dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
22564f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
22574f6b1b3dSJohannes Berg 			shift = 2;
22584f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
22594f6b1b3dSJohannes Berg 			shift = 1;
22604f6b1b3dSJohannes Berg 		else
22614f6b1b3dSJohannes Berg 			shift = 0;
2262fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
22637f406cd1SJohannes Berg 		break;
22647f406cd1SJohannes Berg 		}
226541cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
226641cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
226741cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
226841cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
226941cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
227041cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
227141cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
227241cbb0f5SLuca Coelho 		break;
2273fbd6ff5cSJohannes Berg 	}
22744f6b1b3dSJohannes Berg }
2275fbd6ff5cSJohannes Berg 
2276a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
22774f6b1b3dSJohannes Berg {
22786aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
22794f6b1b3dSJohannes Berg 
22804f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2281a17d93ffSBen Greear 		return -EINVAL;
2282a17d93ffSBen Greear 
22834f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2284a17d93ffSBen Greear 	return 0;
2285fbd6ff5cSJohannes Berg }
2286fbd6ff5cSJohannes Berg 
2287b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2288b255b72bSSeevalamuthu Mariappan 					int tid)
2289b255b72bSSeevalamuthu Mariappan {
2290b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2291b255b72bSSeevalamuthu Mariappan 	u64 value;
2292b255b72bSSeevalamuthu Mariappan 
2293b255b72bSSeevalamuthu Mariappan 	do {
2294b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2295b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2296b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2297b255b72bSSeevalamuthu Mariappan 
2298b255b72bSSeevalamuthu Mariappan 	return value;
2299b255b72bSSeevalamuthu Mariappan }
2300b255b72bSSeevalamuthu Mariappan 
23010f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
23020f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
23030f9c5a61SJohannes Berg 			     int tid)
23040f9c5a61SJohannes Berg {
23050f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2306b255b72bSSeevalamuthu Mariappan 	int cpu;
23070f9c5a61SJohannes Berg 
23080f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2309046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2310046d2e7cSSriram R 							   tid);
23110f9c5a61SJohannes Berg 
2312046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2313b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2314b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2315b255b72bSSeevalamuthu Mariappan 
2316046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2317046d2e7cSSriram R 						     cpu);
2318b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2319b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2320b255b72bSSeevalamuthu Mariappan 			}
2321b255b72bSSeevalamuthu Mariappan 		}
23220f9c5a61SJohannes Berg 
23230f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
23240f9c5a61SJohannes Berg 	}
23250f9c5a61SJohannes Berg 
23260f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
23270f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2328046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
23290f9c5a61SJohannes Berg 	}
23300f9c5a61SJohannes Berg 
23310f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
23320f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23330f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2334046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
23350f9c5a61SJohannes Berg 	}
23360f9c5a61SJohannes Berg 
23370f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
23380f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23390f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2340046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
23410f9c5a61SJohannes Berg 	}
23422fe4a29aSToke Høiland-Jørgensen 
23432fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
23442fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
23452fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
23462fe4a29aSToke Høiland-Jørgensen 
23472fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
23482fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
23492fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
23502fe4a29aSToke Høiland-Jørgensen 
23512fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
23522fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
23532fe4a29aSToke Høiland-Jørgensen 	}
23540f9c5a61SJohannes Berg }
23550f9c5a61SJohannes Berg 
2356c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2357c9c5962bSJohannes Berg {
2358c9c5962bSJohannes Berg 	unsigned int start;
2359c9c5962bSJohannes Berg 	u64 value;
2360c9c5962bSJohannes Berg 
2361c9c5962bSJohannes Berg 	do {
2362c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2363c9c5962bSJohannes Berg 		value = rxstats->bytes;
2364c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2365c9c5962bSJohannes Berg 
2366c9c5962bSJohannes Berg 	return value;
2367c9c5962bSJohannes Berg }
2368c9c5962bSJohannes Berg 
23690fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
23700fdf1493SJohannes Berg 		   bool tidstats)
2371b7ffbd7eSJohannes Berg {
2372b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2373b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2374b7ffbd7eSJohannes Berg 	u32 thr = 0;
2375c9c5962bSJohannes Berg 	int i, ac, cpu;
2376c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2377c9c5962bSJohannes Berg 
2378c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2379b7ffbd7eSJohannes Berg 
2380b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2381b7ffbd7eSJohannes Berg 
2382225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2383225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2384225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2385225b8189SJohannes Berg 	 */
2386225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2387bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2388225b8189SJohannes Berg 
23892b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2390a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2391a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2392a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2393a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
23949cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2395a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2396976bd9efSJohannes Berg 
2397976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2398bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2399bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2400a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2401976bd9efSJohannes Berg 	}
2402b7ffbd7eSJohannes Berg 
240384b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
24049cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2405e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2406b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
24072b9a7e1bSJohannes Berg 
2408a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2409a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2410b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
24112b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2412046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2413a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2414b7ffbd7eSJohannes Berg 	}
24152b9a7e1bSJohannes Berg 
2416a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
24172b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
24182b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2419046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2420a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
24212b9a7e1bSJohannes Berg 	}
24222b9a7e1bSJohannes Berg 
2423a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2424a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2425046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
24260f9c5a61SJohannes Berg 
2427046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2428c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2429c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2430c9c5962bSJohannes Berg 
2431046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2432046d2e7cSSriram R 						     cpu);
2433c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2434c9c5962bSJohannes Berg 			}
2435c9c5962bSJohannes Berg 		}
2436c9c5962bSJohannes Berg 
2437a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
24382b9a7e1bSJohannes Berg 	}
24392b9a7e1bSJohannes Berg 
2440a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2441046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2442046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2443c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2444c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2445c9c5962bSJohannes Berg 
2446046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2447046d2e7cSSriram R 						     cpu);
2448c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2449c9c5962bSJohannes Berg 			}
2450c9c5962bSJohannes Berg 		}
2451a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
24522b9a7e1bSJohannes Berg 	}
24532b9a7e1bSJohannes Berg 
2454a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2455046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2456a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
24572b9a7e1bSJohannes Berg 	}
24582b9a7e1bSJohannes Berg 
2459a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2460046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2461a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
24622b9a7e1bSJohannes Berg 	}
24632b9a7e1bSJohannes Berg 
2464b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2465b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2466b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2467b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2468b4809e94SToke Høiland-Jørgensen 	}
2469b4809e94SToke Høiland-Jørgensen 
2470b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2471b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2472b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2473b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2474b4809e94SToke Høiland-Jørgensen 	}
2475b4809e94SToke Høiland-Jørgensen 
2476b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2477942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2478b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2479b4809e94SToke Høiland-Jørgensen 	}
2480b4809e94SToke Høiland-Jørgensen 
2481046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2482046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2483c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2484c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2485c9c5962bSJohannes Berg 
2486046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2487e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2488c9c5962bSJohannes Berg 		}
2489c9c5962bSJohannes Berg 	}
2490b7ffbd7eSJohannes Berg 
2491225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2492225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2493a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2494a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2495225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2496225b8189SJohannes Berg 	}
2497225b8189SJohannes Berg 
249830686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
249930686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2500a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2501c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2502a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2503b7ffbd7eSJohannes Berg 		}
25042b9a7e1bSJohannes Berg 
2505046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2506a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
250740d9a38aSJohannes Berg 			sinfo->signal_avg =
2508046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2509a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
25102b9a7e1bSJohannes Berg 		}
25112b9a7e1bSJohannes Berg 	}
25122b9a7e1bSJohannes Berg 
2513c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2514c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2515c9c5962bSJohannes Berg 	 * pcpu statistics
2516c9c5962bSJohannes Berg 	 */
2517c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2518a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2519a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2520a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2521046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2522a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2523b7ffbd7eSJohannes Berg 
2524c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2525c9c5962bSJohannes Berg 
2526b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2527e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2528c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2529b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2530046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2531b7ffbd7eSJohannes Berg 		}
2532b7ffbd7eSJohannes Berg 	}
2533b7ffbd7eSJohannes Berg 
2534a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2535046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2536e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2537a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
25382b9a7e1bSJohannes Berg 	}
25392b9a7e1bSJohannes Berg 
2540a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2541a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2542a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
25432b9a7e1bSJohannes Berg 	}
2544b7ffbd7eSJohannes Berg 
25450fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
25466af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
25476af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
25488689c051SArend van Spriel 	}
254979c892b8SJohannes Berg 
2550b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2551b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2552a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2553a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2554a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2555a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2556a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2557dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
25581303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
25591303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2560b7ffbd7eSJohannes Berg 
2561433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2562433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2563433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2564b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2565a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2566433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2567b7ffbd7eSJohannes Berg 		}
2568433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2569433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2570433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2571dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
25721303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2573b7ffbd7eSJohannes Berg #endif
2574b7ffbd7eSJohannes Berg 	}
2575b7ffbd7eSJohannes Berg 
2576b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2577b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2578b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2579b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2580b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2581b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2582b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2583785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2584b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2585b7ffbd7eSJohannes Berg 
2586b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2587b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2588b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2589b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2590b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2591b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2592b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2593b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2594b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2595b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2596b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2597b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2598a74a8c84SJohannes Berg 	if (sta->sta.wme)
2599b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2600b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2601b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2602b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2603b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2604b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2605b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2606b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2607b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2608b7ffbd7eSJohannes Berg 
26093b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
26103b17fbf8SMaxim Altshul 
26113b17fbf8SMaxim Altshul 	if (thr != 0) {
2612a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
26133b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
26143b17fbf8SMaxim Altshul 	}
2615a78b26ffSVenkateswara Naralasetty 
2616a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2617046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2618046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2619a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2620a78b26ffSVenkateswara Naralasetty 	}
2621cc60dbbfSBalaji Pothunoori 
26229c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2623046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2624cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2625cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2626046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2627cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
26289c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2629cc60dbbfSBalaji Pothunoori 	}
2630ab60633cSNarayanraddi Masti 
2631ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2632ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2633ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2634ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2635ab60633cSNarayanraddi Masti 	}
26363b17fbf8SMaxim Altshul }
26373b17fbf8SMaxim Altshul 
26383b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
26393b17fbf8SMaxim Altshul {
26403b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
26413b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
26423b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
26433b17fbf8SMaxim Altshul 	u32 thr = 0;
26443b17fbf8SMaxim Altshul 
26453b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
26463b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
26473b17fbf8SMaxim Altshul 
2648b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2649b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2650b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2651b7ffbd7eSJohannes Berg 	else
26524fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2653b7ffbd7eSJohannes Berg 
26543b17fbf8SMaxim Altshul 	return thr;
2655b7ffbd7eSJohannes Berg }
2656b8da6b6aSJohannes Berg 
2657b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2658b8da6b6aSJohannes Berg {
2659c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2660c9c5962bSJohannes Berg 
2661046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2662046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2663c9c5962bSJohannes Berg 		return stats->last_rx;
2664046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2665b8da6b6aSJohannes Berg }
2666484a54c2SToke Høiland-Jørgensen 
2667484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2668484a54c2SToke Høiland-Jørgensen {
2669484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2670484a54c2SToke Høiland-Jørgensen 		return;
2671484a54c2SToke Høiland-Jørgensen 
2672484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2673484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2674484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2675484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2676484a54c2SToke Høiland-Jørgensen 	} else {
2677484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2678484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2679484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2680484a54c2SToke Høiland-Jørgensen 	}
2681484a54c2SToke Høiland-Jørgensen }
2682484a54c2SToke Høiland-Jørgensen 
2683484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2684484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2685484a54c2SToke Høiland-Jørgensen {
2686484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2687484a54c2SToke Høiland-Jørgensen 
2688484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2689484a54c2SToke Høiland-Jørgensen }
2690cb71f1d1SJohannes Berg 
2691cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2692cb71f1d1SJohannes Berg {
2693cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2694cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2695cb71f1d1SJohannes Berg 	int ret;
2696cb71f1d1SJohannes Berg 
2697cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2698cb71f1d1SJohannes Berg 
2699cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2700cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2701cb71f1d1SJohannes Berg 		return -EINVAL;
2702cb71f1d1SJohannes Berg 
2703cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2704cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2705cb71f1d1SJohannes Berg 		return -EBUSY;
2706cb71f1d1SJohannes Berg 
2707cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2708cb71f1d1SJohannes Berg 	if (!alloc)
2709cb71f1d1SJohannes Berg 		return -ENOMEM;
2710cb71f1d1SJohannes Berg 
2711cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2712cb71f1d1SJohannes Berg 	if (ret) {
2713cb71f1d1SJohannes Berg 		kfree(alloc);
2714cb71f1d1SJohannes Berg 		return ret;
2715cb71f1d1SJohannes Berg 	}
2716cb71f1d1SJohannes Berg 
2717cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2718cb71f1d1SJohannes Berg 
2719cb71f1d1SJohannes Berg 	return 0;
2720cb71f1d1SJohannes Berg }
2721cb71f1d1SJohannes Berg 
2722ba6ddab9SJohannes Berg static int link_sta_info_hash_add(struct ieee80211_local *local,
2723ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
2724ba6ddab9SJohannes Berg {
2725ba6ddab9SJohannes Berg 	return rhltable_insert(&local->link_sta_hash,
2726ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
2727ba6ddab9SJohannes Berg 			       link_sta_rht_params);
2728ba6ddab9SJohannes Berg }
2729ba6ddab9SJohannes Berg 
2730cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2731cb71f1d1SJohannes Berg {
2732cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2733c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2734cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2735cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2736cb71f1d1SJohannes Berg 	int ret;
2737cb71f1d1SJohannes Berg 
2738c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2739c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2740cb71f1d1SJohannes Berg 
2741c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2742cb71f1d1SJohannes Berg 		return -EINVAL;
2743cb71f1d1SJohannes Berg 
2744cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2745cb71f1d1SJohannes Berg 
2746ba6ddab9SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_INSERTED)) {
2747ba6ddab9SJohannes Berg 		ret = 0;
2748ba6ddab9SJohannes Berg 		goto hash;
2749ba6ddab9SJohannes Berg 	}
2750cb71f1d1SJohannes Berg 
2751cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2752cb71f1d1SJohannes Berg 				   old_links, new_links);
2753cb71f1d1SJohannes Berg 	if (ret) {
2754cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2755ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2756cb71f1d1SJohannes Berg 	}
2757cb71f1d1SJohannes Berg 
2758ba6ddab9SJohannes Berg hash:
2759ba6ddab9SJohannes Berg 	link_sta_info_hash_add(sdata->local, link_sta);
2760ba6ddab9SJohannes Berg 
2761cb71f1d1SJohannes Berg 	return ret;
2762cb71f1d1SJohannes Berg }
2763cb71f1d1SJohannes Berg 
2764cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2765cb71f1d1SJohannes Berg {
2766cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2767cb71f1d1SJohannes Berg 
2768cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2769cb71f1d1SJohannes Berg 
2770cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2771cb71f1d1SJohannes Berg 
2772cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2773cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2774cb71f1d1SJohannes Berg 				     sta->sta.valid_links,
2775cb71f1d1SJohannes Berg 				     sta->sta.valid_links & ~BIT(link_id));
2776cb71f1d1SJohannes Berg 
2777ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2778cb71f1d1SJohannes Berg }
2779