xref: /openbmc/linux/net/mac80211/sta_info.c (revision d8675a63518c6148827838058feb7f18403faed1)
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;
6196e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
6206e0456b5SFelix Fietkau 
621484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
622484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
623484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
624484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
625dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
626dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
627484a54c2SToke Høiland-Jørgensen 
628bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
629ef04a297SJohannes Berg 
630abfbc3afSJohannes Berg 	return sta;
631ba8c3d6fSFelix Fietkau 
632ba8c3d6fSFelix Fietkau free_txq:
633ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
634ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
635ba8c3d6fSFelix Fietkau free:
636cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
637433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
638433f5bc1SJohannes Berg 	kfree(sta->mesh);
639433f5bc1SJohannes Berg #endif
640ba8c3d6fSFelix Fietkau 	kfree(sta);
641ba8c3d6fSFelix Fietkau 	return NULL;
64273651ee6SJohannes Berg }
64373651ee6SJohannes Berg 
6448c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
64534e89507SJohannes Berg {
64634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
64734e89507SJohannes Berg 
64803e4497eSJohannes Berg 	/*
64903e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
65003e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
65103e4497eSJohannes Berg 	 * and another CPU turns off the net device.
65203e4497eSJohannes Berg 	 */
6538c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
6548c71df7aSGuy Eilam 		return -ENETDOWN;
65503e4497eSJohannes Berg 
656b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
657deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
6588c71df7aSGuy Eilam 		return -EINVAL;
6598c71df7aSGuy Eilam 
66083e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
66183e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
66283e7e4ceSHerbert Xu 	 * for correctness.
66331104891SJohannes Berg 	 */
66431104891SJohannes Berg 	rcu_read_lock();
66531104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
66631104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
66731104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
66831104891SJohannes Berg 		rcu_read_unlock();
66931104891SJohannes Berg 		return -ENOTUNIQ;
67031104891SJohannes Berg 	}
67131104891SJohannes Berg 	rcu_read_unlock();
67231104891SJohannes Berg 
6738c71df7aSGuy Eilam 	return 0;
67493e5deb1SJohannes Berg }
67544213b5eSJohannes Berg 
676f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
677f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
678f09603a2SJohannes Berg 				     struct sta_info *sta)
679f09603a2SJohannes Berg {
680f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
681f09603a2SJohannes Berg 	int err = 0;
682f09603a2SJohannes Berg 
683f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
684f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
685f09603a2SJohannes Berg 		if (err)
686f09603a2SJohannes Berg 			break;
687f09603a2SJohannes Berg 	}
688f09603a2SJohannes Berg 
689f09603a2SJohannes Berg 	if (!err) {
690a4ec45a4SJohannes Berg 		/*
691a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
692a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
693a4ec45a4SJohannes Berg 		 */
694a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
695f09603a2SJohannes Berg 			sta->uploaded = true;
696f09603a2SJohannes Berg 		return 0;
697f09603a2SJohannes Berg 	}
698f09603a2SJohannes Berg 
699f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
700bdcbd8e0SJohannes Berg 		sdata_info(sdata,
701bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
702bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
703f09603a2SJohannes Berg 		err = 0;
704f09603a2SJohannes Berg 	}
705f09603a2SJohannes Berg 
706f09603a2SJohannes Berg 	/* unwind on error */
707f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
708f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
709f09603a2SJohannes Berg 
710f09603a2SJohannes Berg 	return err;
711f09603a2SJohannes Berg }
712f09603a2SJohannes Berg 
713d405fd8cSGregory Greenman static void
714d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
715d405fd8cSGregory Greenman {
716d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
717d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
718d405fd8cSGregory Greenman 	struct sta_info *sta;
719d405fd8cSGregory Greenman 
720d405fd8cSGregory Greenman 	rcu_read_lock();
721d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
722d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
723d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
724d405fd8cSGregory Greenman 			continue;
725d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
726d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
727d405fd8cSGregory Greenman 			break;
728d405fd8cSGregory Greenman 		}
729d405fd8cSGregory Greenman 	}
730d405fd8cSGregory Greenman 	rcu_read_unlock();
731d405fd8cSGregory Greenman 
732d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
733d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
734*d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
735*d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
736d405fd8cSGregory Greenman 	}
737d405fd8cSGregory Greenman }
738d405fd8cSGregory Greenman 
73934e89507SJohannes Berg /*
7408c71df7aSGuy Eilam  * should be called with sta_mtx locked
7418c71df7aSGuy Eilam  * this function replaces the mutex lock
7428c71df7aSGuy Eilam  * with a RCU lock
7438c71df7aSGuy Eilam  */
7444d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
7458c71df7aSGuy Eilam {
7468c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
7478c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
7480c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
7498c71df7aSGuy Eilam 	int err = 0;
7508c71df7aSGuy Eilam 
7518c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
7528c71df7aSGuy Eilam 
7537852e361SJohannes Berg 	/* check if STA exists already */
7547852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
7554d33960bSJohannes Berg 		err = -EEXIST;
7568f9dcc29SAhmed Zaki 		goto out_cleanup;
75734e89507SJohannes Berg 	}
75834e89507SJohannes Berg 
7590c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
7600c2e3842SKoen Vandeputte 	if (!sinfo) {
7610c2e3842SKoen Vandeputte 		err = -ENOMEM;
7628f9dcc29SAhmed Zaki 		goto out_cleanup;
7630c2e3842SKoen Vandeputte 	}
7640c2e3842SKoen Vandeputte 
7654d33960bSJohannes Berg 	local->num_sta++;
7664d33960bSJohannes Berg 	local->sta_generation++;
7674d33960bSJohannes Berg 	smp_mb();
7684d33960bSJohannes Berg 
7695108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
7705108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
7715108ca82SJohannes Berg 
7724d33960bSJohannes Berg 	/* make the station visible */
77362b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
77462b14b24SJohannes Berg 	if (err)
77562b14b24SJohannes Berg 		goto out_drop_sta;
7764d33960bSJohannes Berg 
7772bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
77883d5cc01SJohannes Berg 
7794dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
7804dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
7814dde3c36SMordechay Goodstein 	 */
7824dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
7834dde3c36SMordechay Goodstein 		ieee80211_recalc_min_chandef(sta->sdata);
7844dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
7854dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
7864dde3c36SMordechay Goodstein 	}
7874dde3c36SMordechay Goodstein 
7885108ca82SJohannes Berg 	/* notify driver */
7895108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
7905108ca82SJohannes Berg 	if (err)
7915108ca82SJohannes Berg 		goto out_remove;
7925108ca82SJohannes Berg 
79383d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
794d405fd8cSGregory Greenman 
7955108ca82SJohannes Berg 	/* accept BA sessions now */
7965108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
7974d33960bSJohannes Berg 
7984d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
7994d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
8004d33960bSJohannes Berg 
8010ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
8020ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
8030ef049dcSArnd Bergmann 	kfree(sinfo);
804d0709a65SJohannes Berg 
805bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
806f0706e82SJiri Benc 
80734e89507SJohannes Berg 	/* move reference to rcu-protected */
80834e89507SJohannes Berg 	rcu_read_lock();
80934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
810e9f207f0SJiri Benc 
81173651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
81273651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
81373651ee6SJohannes Berg 
81473651ee6SJohannes Berg 	return 0;
8155108ca82SJohannes Berg  out_remove:
8165108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
8175108ca82SJohannes Berg 	list_del_rcu(&sta->list);
81862b14b24SJohannes Berg  out_drop_sta:
8195108ca82SJohannes Berg 	local->num_sta--;
8205108ca82SJohannes Berg 	synchronize_net();
8218f9dcc29SAhmed Zaki  out_cleanup:
8227bc40aedSJohannes Berg 	cleanup_single_sta(sta);
8234d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
824ea32f065SSudip Mukherjee 	kfree(sinfo);
8254d33960bSJohannes Berg 	rcu_read_lock();
8264d33960bSJohannes Berg 	return err;
8278c71df7aSGuy Eilam }
8288c71df7aSGuy Eilam 
8298c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
8308c71df7aSGuy Eilam {
8318c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
832308f7fcfSZhao, Gang 	int err;
8338c71df7aSGuy Eilam 
8344d33960bSJohannes Berg 	might_sleep();
8354d33960bSJohannes Berg 
83631104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
83731104891SJohannes Berg 
8388c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
8398c71df7aSGuy Eilam 	if (err) {
8407bc40aedSJohannes Berg 		sta_info_free(local, sta);
84131104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
8428c71df7aSGuy Eilam 		rcu_read_lock();
8437bc40aedSJohannes Berg 		return err;
8448c71df7aSGuy Eilam 	}
8458c71df7aSGuy Eilam 
8467bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
847f0706e82SJiri Benc }
848f0706e82SJiri Benc 
84934e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
85034e89507SJohannes Berg {
85134e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
85234e89507SJohannes Berg 
85334e89507SJohannes Berg 	rcu_read_unlock();
85434e89507SJohannes Berg 
85534e89507SJohannes Berg 	return err;
85634e89507SJohannes Berg }
85734e89507SJohannes Berg 
858d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
859004c872eSJohannes Berg {
860004c872eSJohannes Berg 	/*
861004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
862004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
863004c872eSJohannes Berg 	 */
864d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
865004c872eSJohannes Berg }
866004c872eSJohannes Berg 
867d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
868004c872eSJohannes Berg {
869004c872eSJohannes Berg 	/*
870004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
871004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
872004c872eSJohannes Berg 	 */
873d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
874004c872eSJohannes Berg }
875004c872eSJohannes Berg 
8763d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
8773d5839b6SIlan Peer {
8783d5839b6SIlan Peer 	/*
8793d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
8803d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
8813d5839b6SIlan Peer 	 */
8823d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
8833d5839b6SIlan Peer }
8843d5839b6SIlan Peer 
885948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
886004c872eSJohannes Berg {
887948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
888948d887dSJohannes Berg 	switch (ac) {
889948d887dSJohannes Berg 	case IEEE80211_AC_VO:
890948d887dSJohannes Berg 		return BIT(6) | BIT(7);
891948d887dSJohannes Berg 	case IEEE80211_AC_VI:
892948d887dSJohannes Berg 		return BIT(4) | BIT(5);
893948d887dSJohannes Berg 	case IEEE80211_AC_BE:
894948d887dSJohannes Berg 		return BIT(0) | BIT(3);
895948d887dSJohannes Berg 	case IEEE80211_AC_BK:
896948d887dSJohannes Berg 		return BIT(1) | BIT(2);
897948d887dSJohannes Berg 	default:
898948d887dSJohannes Berg 		WARN_ON(1);
899948d887dSJohannes Berg 		return 0;
900d0709a65SJohannes Berg 	}
901004c872eSJohannes Berg }
902004c872eSJohannes Berg 
9039b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
904004c872eSJohannes Berg {
905c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
906d012a605SMarco Porsch 	struct ps_data *ps;
907948d887dSJohannes Berg 	bool indicate_tim = false;
908948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
909948d887dSJohannes Berg 	int ac;
910a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
911004c872eSJohannes Berg 
912d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
913d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
914c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
915c868cb35SJohannes Berg 			return;
9163e122be0SJohannes Berg 
917d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
9183f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
9193f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
9203f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
9213f52b7e3SMarco Porsch #endif
922d012a605SMarco Porsch 	} else {
923d012a605SMarco Porsch 		return;
924d012a605SMarco Porsch 	}
925d012a605SMarco Porsch 
926c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
927d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
928c868cb35SJohannes Berg 		return;
929004c872eSJohannes Berg 
930c868cb35SJohannes Berg 	if (sta->dead)
931c868cb35SJohannes Berg 		goto done;
9323e122be0SJohannes Berg 
933948d887dSJohannes Berg 	/*
934948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
935948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
936948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
937948d887dSJohannes Berg 	 * non-enabled ones.
938948d887dSJohannes Berg 	 */
939948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
940948d887dSJohannes Berg 		ignore_for_tim = 0;
941948d887dSJohannes Berg 
9429b7a86f3SJohannes Berg 	if (ignore_pending)
9439b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
9449b7a86f3SJohannes Berg 
945948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
946948d887dSJohannes Berg 		unsigned long tids;
947948d887dSJohannes Berg 
948f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
949948d887dSJohannes Berg 			continue;
950948d887dSJohannes Berg 
951948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
952948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
953948d887dSJohannes Berg 		if (indicate_tim)
954948d887dSJohannes Berg 			break;
955948d887dSJohannes Berg 
956948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
957948d887dSJohannes Berg 
958948d887dSJohannes Berg 		indicate_tim |=
959948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
960ba8c3d6fSFelix Fietkau 		indicate_tim |=
961ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
962004c872eSJohannes Berg 	}
963004c872eSJohannes Berg 
964c868cb35SJohannes Berg  done:
96565f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
966004c872eSJohannes Berg 
9673d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
9683d5839b6SIlan Peer 		goto out_unlock;
9693d5839b6SIlan Peer 
970948d887dSJohannes Berg 	if (indicate_tim)
971d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
972c868cb35SJohannes Berg 	else
973d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
9743e122be0SJohannes Berg 
9759b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
976c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
977948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
978c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
979004c872eSJohannes Berg 	}
980004c872eSJohannes Berg 
9813d5839b6SIlan Peer out_unlock:
98265f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
983004c872eSJohannes Berg }
984004c872eSJohannes Berg 
9859b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
9869b7a86f3SJohannes Berg {
9879b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
9889b7a86f3SJohannes Berg }
9899b7a86f3SJohannes Berg 
990cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
991f0706e82SJiri Benc {
992e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
993f0706e82SJiri Benc 	int timeout;
994f0706e82SJiri Benc 
995f0706e82SJiri Benc 	if (!skb)
996cd0b8d89SJohannes Berg 		return false;
997f0706e82SJiri Benc 
998e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
999f0706e82SJiri Benc 
1000f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
100157c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
100257c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
100357c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1004f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1005f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1006e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1007f0706e82SJiri Benc }
1008f0706e82SJiri Benc 
1009f0706e82SJiri Benc 
1010948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1011948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1012f0706e82SJiri Benc {
1013f0706e82SJiri Benc 	unsigned long flags;
1014f0706e82SJiri Benc 	struct sk_buff *skb;
1015f0706e82SJiri Benc 
101660750397SJohannes Berg 	/*
101760750397SJohannes Berg 	 * First check for frames that should expire on the filtered
101860750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
101960750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
102060750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
102160750397SJohannes Berg 	 * total_ps_buffered counter.
102260750397SJohannes Berg 	 */
1023f0706e82SJiri Benc 	for (;;) {
1024948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1025948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
102657c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1027948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1028836341a7SJohannes Berg 		else
1029f0706e82SJiri Benc 			skb = NULL;
1030948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1031f0706e82SJiri Benc 
103260750397SJohannes Berg 		/*
103360750397SJohannes Berg 		 * Frames are queued in order, so if this one
103460750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
103560750397SJohannes Berg 		 * we actually reached the end of the queue we
103660750397SJohannes Berg 		 * also need to stop, of course.
103760750397SJohannes Berg 		 */
103860750397SJohannes Berg 		if (!skb)
103960750397SJohannes Berg 			break;
1040d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
104160750397SJohannes Berg 	}
104260750397SJohannes Berg 
104360750397SJohannes Berg 	/*
104460750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
104560750397SJohannes Berg 	 * only find something if the filtered queue was emptied
104660750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
104760750397SJohannes Berg 	 * buffered frames.
104860750397SJohannes Berg 	 */
1049f0706e82SJiri Benc 	for (;;) {
1050948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1051948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1052f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1053948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1054f0706e82SJiri Benc 		else
1055f0706e82SJiri Benc 			skb = NULL;
1056948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1057f0706e82SJiri Benc 
105860750397SJohannes Berg 		/*
105960750397SJohannes Berg 		 * frames are queued in order, so if this one
106060750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
106160750397SJohannes Berg 		 * the queue) we can stop testing
106260750397SJohannes Berg 		 */
1063836341a7SJohannes Berg 		if (!skb)
1064836341a7SJohannes Berg 			break;
1065836341a7SJohannes Berg 
1066f0706e82SJiri Benc 		local->total_ps_buffered--;
1067bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1068bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1069d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1070f0706e82SJiri Benc 	}
10713393a608SJuuso Oikarinen 
107260750397SJohannes Berg 	/*
107360750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
107460750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
107560750397SJohannes Berg 	 * frames.
107660750397SJohannes Berg 	 */
107760750397SJohannes Berg 	sta_info_recalc_tim(sta);
107860750397SJohannes Berg 
107960750397SJohannes Berg 	/*
108060750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
108160750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
108260750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
108360750397SJohannes Berg 	 */
1084948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1085948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1086948d887dSJohannes Berg }
1087948d887dSJohannes Berg 
1088948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1089948d887dSJohannes Berg 					     struct sta_info *sta)
1090948d887dSJohannes Berg {
1091948d887dSJohannes Berg 	bool have_buffered = false;
1092948d887dSJohannes Berg 	int ac;
1093948d887dSJohannes Berg 
10943f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
10953f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
10963f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1097948d887dSJohannes Berg 		return false;
1098948d887dSJohannes Berg 
1099948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1100948d887dSJohannes Berg 		have_buffered |=
1101948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1102948d887dSJohannes Berg 
1103948d887dSJohannes Berg 	return have_buffered;
1104f0706e82SJiri Benc }
1105f0706e82SJiri Benc 
1106d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
110734e89507SJohannes Berg {
110834e89507SJohannes Berg 	struct ieee80211_local *local;
110934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
11106d10e46bSJohannes Berg 	int ret;
111134e89507SJohannes Berg 
111234e89507SJohannes Berg 	might_sleep();
111334e89507SJohannes Berg 
111434e89507SJohannes Berg 	if (!sta)
111534e89507SJohannes Berg 		return -ENOENT;
111634e89507SJohannes Berg 
111734e89507SJohannes Berg 	local = sta->local;
111834e89507SJohannes Berg 	sdata = sta->sdata;
111934e89507SJohannes Berg 
112083d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
112183d5cc01SJohannes Berg 
1122098a6070SJohannes Berg 	/*
1123098a6070SJohannes Berg 	 * Before removing the station from the driver and
1124098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1125098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1126098a6070SJohannes Berg 	 * will be sufficient.
1127098a6070SJohannes Berg 	 */
1128c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1129c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1130098a6070SJohannes Berg 
1131f59374ebSSara Sharon 	/*
1132f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1133f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1134f59374ebSSara Sharon 	 * all such frames to be processed.
1135f59374ebSSara Sharon 	 */
1136f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1137f59374ebSSara Sharon 
113834e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1139b01711beSJohannes Berg 	if (WARN_ON(ret))
114034e89507SJohannes Berg 		return ret;
114134e89507SJohannes Berg 
1142a7a6bdd0SArik Nemtsov 	/*
1143a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1144a7a6bdd0SArik Nemtsov 	 * removal.
1145a7a6bdd0SArik Nemtsov 	 */
1146a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1147a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1148a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1149a7a6bdd0SArik Nemtsov 	}
1150a7a6bdd0SArik Nemtsov 
1151794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1152ef044763SEliad Peller 	sta->removed = true;
11534d33960bSJohannes Berg 
11546a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
11556a9d1b91SJohannes Berg 
1156a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1157a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1158a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1159a710c816SJohannes Berg 
1160d778207bSJohannes Berg 	return 0;
1161d778207bSJohannes Berg }
1162d778207bSJohannes Berg 
1163d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1164d778207bSJohannes Berg {
1165d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1166d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
11670ef049dcSArnd Bergmann 	struct station_info *sinfo;
1168d778207bSJohannes Berg 	int ret;
1169d778207bSJohannes Berg 
1170d778207bSJohannes Berg 	/*
1171d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1172d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1173d778207bSJohannes Berg 	 */
1174d778207bSJohannes Berg 
1175d778207bSJohannes Berg 	might_sleep();
1176d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1177d778207bSJohannes Berg 
11785981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1179b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1180b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1181b16798f5SJohannes Berg 	}
1182b16798f5SJohannes Berg 
1183c8782078SJohannes Berg 	/* now keys can no longer be reached */
11846d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
118534e89507SJohannes Berg 
11869b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
11879b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
11889b7a86f3SJohannes Berg 
118934e89507SJohannes Berg 	sta->dead = true;
119034e89507SJohannes Berg 
119134e89507SJohannes Berg 	local->num_sta--;
119234e89507SJohannes Berg 	local->sta_generation++;
119334e89507SJohannes Berg 
119483d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1195f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1196f09603a2SJohannes Berg 		if (ret) {
119783d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
119883d5cc01SJohannes Berg 			break;
119983d5cc01SJohannes Berg 		}
120083d5cc01SJohannes Berg 	}
1201d9a7ddb0SJohannes Berg 
1202f09603a2SJohannes Berg 	if (sta->uploaded) {
1203f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1204f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1205f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1206f09603a2SJohannes Berg 	}
120734e89507SJohannes Berg 
1208bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1209bdcbd8e0SJohannes Berg 
12100ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
12110ef049dcSArnd Bergmann 	if (sinfo)
12120fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
12130ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
12140ef049dcSArnd Bergmann 	kfree(sinfo);
1215ec15e68bSJouni Malinen 
121634e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
121734e89507SJohannes Berg 
12183a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
12193a11ce08SJohannes Berg 
1220d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1221d778207bSJohannes Berg }
1222d778207bSJohannes Berg 
1223d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1224d778207bSJohannes Berg {
1225d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1226d778207bSJohannes Berg 
1227d778207bSJohannes Berg 	if (err)
1228d778207bSJohannes Berg 		return err;
1229d778207bSJohannes Berg 
1230d778207bSJohannes Berg 	synchronize_net();
1231d778207bSJohannes Berg 
1232d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
123334e89507SJohannes Berg 
123434e89507SJohannes Berg 	return 0;
123534e89507SJohannes Berg }
123634e89507SJohannes Berg 
123734e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
123834e89507SJohannes Berg {
123934e89507SJohannes Berg 	struct sta_info *sta;
124034e89507SJohannes Berg 	int ret;
124134e89507SJohannes Berg 
124234e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12437852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
124434e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
124534e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
124634e89507SJohannes Berg 
124734e89507SJohannes Berg 	return ret;
124834e89507SJohannes Berg }
124934e89507SJohannes Berg 
125034e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
125134e89507SJohannes Berg 			      const u8 *addr)
125234e89507SJohannes Berg {
125334e89507SJohannes Berg 	struct sta_info *sta;
125434e89507SJohannes Berg 	int ret;
125534e89507SJohannes Berg 
125634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12577852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
125834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
125934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
126034e89507SJohannes Berg 
126134e89507SJohannes Berg 	return ret;
126234e89507SJohannes Berg }
1263f0706e82SJiri Benc 
126434f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1265f0706e82SJiri Benc {
126634f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1267f0706e82SJiri Benc 	struct sta_info *sta;
12683393a608SJuuso Oikarinen 	bool timer_needed = false;
1269f0706e82SJiri Benc 
1270d0709a65SJohannes Berg 	rcu_read_lock();
1271d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
12723393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
12733393a608SJuuso Oikarinen 			timer_needed = true;
1274d0709a65SJohannes Berg 	rcu_read_unlock();
1275f0706e82SJiri Benc 
12765bb644a0SJohannes Berg 	if (local->quiescing)
12775bb644a0SJohannes Berg 		return;
12785bb644a0SJohannes Berg 
12793393a608SJuuso Oikarinen 	if (!timer_needed)
12803393a608SJuuso Oikarinen 		return;
12813393a608SJuuso Oikarinen 
128226d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
128326d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1284f0706e82SJiri Benc }
1285f0706e82SJiri Benc 
12867bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
12877bedd0cfSJohannes Berg {
12887bedd0cfSJohannes Berg 	int err;
12897bedd0cfSJohannes Berg 
129083e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
12917bedd0cfSJohannes Berg 	if (err)
12927bedd0cfSJohannes Berg 		return err;
12937bedd0cfSJohannes Berg 
1294ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1295ba6ddab9SJohannes Berg 	if (err) {
1296ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1297ba6ddab9SJohannes Berg 		return err;
1298ba6ddab9SJohannes Berg 	}
1299ba6ddab9SJohannes Berg 
13004d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
130134e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1302f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1303f0706e82SJiri Benc 
130434f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
13057bedd0cfSJohannes Berg 	return 0;
1306f0706e82SJiri Benc }
1307f0706e82SJiri Benc 
1308f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1309f0706e82SJiri Benc {
1310a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
131183e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1312ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1313f0706e82SJiri Benc }
1314f0706e82SJiri Benc 
1315051007d9SJohannes Berg 
1316e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1317f0706e82SJiri Benc {
1318b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1319f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1320d778207bSJohannes Berg 	LIST_HEAD(free_list);
132144213b5eSJohannes Berg 	int ret = 0;
1322f0706e82SJiri Benc 
1323d0709a65SJohannes Berg 	might_sleep();
1324d0709a65SJohannes Berg 
1325e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1326e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1327e716251dSJohannes Berg 
132834e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
132934e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1330e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1331e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1332d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1333d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
133434316837SJohannes Berg 			ret++;
133534316837SJohannes Berg 		}
133634e89507SJohannes Berg 	}
1337d778207bSJohannes Berg 
1338d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1339d778207bSJohannes Berg 		synchronize_net();
1340d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1341d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1342d778207bSJohannes Berg 	}
134334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
134444213b5eSJohannes Berg 
1345051007d9SJohannes Berg 	return ret;
1346051007d9SJohannes Berg }
1347051007d9SJohannes Berg 
134824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
134924723d1bSJohannes Berg 			  unsigned long exp_time)
135024723d1bSJohannes Berg {
135124723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
135224723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
135324723d1bSJohannes Berg 
135434e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1355e46a2cf9SMohammed Shafi Shajakhan 
1356e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1357b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1358b8da6b6aSJohannes Berg 
1359ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1360ec2b774eSMarek Lindner 			continue;
1361ec2b774eSMarek Lindner 
1362b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1363eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1364bdcbd8e0SJohannes Berg 				sta->sta.addr);
13653f52b7e3SMarco Porsch 
13663f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
13673f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
13683f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
13693f52b7e3SMarco Porsch 
137034e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
137124723d1bSJohannes Berg 		}
1372e46a2cf9SMohammed Shafi Shajakhan 	}
1373e46a2cf9SMohammed Shafi Shajakhan 
137434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
137524723d1bSJohannes Berg }
137617741cdcSJohannes Berg 
1377686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1378686b9cb9SBen Greear 						   const u8 *addr,
1379686b9cb9SBen Greear 						   const u8 *localaddr)
138017741cdcSJohannes Berg {
13817bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
138283e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
13837bedd0cfSJohannes Berg 	struct sta_info *sta;
138417741cdcSJohannes Berg 
1385686b9cb9SBen Greear 	/*
1386686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1387686b9cb9SBen Greear 	 * ... first in list.
1388686b9cb9SBen Greear 	 */
138983e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1390686b9cb9SBen Greear 		if (localaddr &&
1391b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1392686b9cb9SBen Greear 			continue;
1393f7c65594SJohannes Berg 		if (!sta->uploaded)
1394f7c65594SJohannes Berg 			return NULL;
139517741cdcSJohannes Berg 		return &sta->sta;
1396f7c65594SJohannes Berg 	}
1397f7c65594SJohannes Berg 
1398abe60632SJohannes Berg 	return NULL;
139917741cdcSJohannes Berg }
1400686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
14015ed176e1SJohannes Berg 
14025ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
14035ed176e1SJohannes Berg 					 const u8 *addr)
14045ed176e1SJohannes Berg {
1405f7c65594SJohannes Berg 	struct sta_info *sta;
14065ed176e1SJohannes Berg 
14075ed176e1SJohannes Berg 	if (!vif)
14085ed176e1SJohannes Berg 		return NULL;
14095ed176e1SJohannes Berg 
1410f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1411f7c65594SJohannes Berg 	if (!sta)
1412f7c65594SJohannes Berg 		return NULL;
14135ed176e1SJohannes Berg 
1414f7c65594SJohannes Berg 	if (!sta->uploaded)
1415f7c65594SJohannes Berg 		return NULL;
1416f7c65594SJohannes Berg 
1417f7c65594SJohannes Berg 	return &sta->sta;
14185ed176e1SJohannes Berg }
141917741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1420af818581SJohannes Berg 
1421e3685e03SJohannes Berg /* powersave support code */
1422e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
142350a9432dSJohannes Berg {
1424608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1425e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1426e3685e03SJohannes Berg 	struct sk_buff_head pending;
1427ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1428e3685e03SJohannes Berg 	unsigned long flags;
1429d012a605SMarco Porsch 	struct ps_data *ps;
1430d012a605SMarco Porsch 
14313918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
14323918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
14333918edb0SFelix Fietkau 				     u.ap);
14343918edb0SFelix Fietkau 
14353918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1436d012a605SMarco Porsch 		ps = &sdata->bss->ps;
14373f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
14383f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1439d012a605SMarco Porsch 	else
1440d012a605SMarco Porsch 		return;
144150a9432dSJohannes Berg 
1442c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
144347086fc5SJohannes Berg 
14445a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1445948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1446ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1447948d887dSJohannes Berg 
144830686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
144912375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1450af818581SJohannes Berg 
1451ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1452adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1453ba8c3d6fSFelix Fietkau 			continue;
1454ba8c3d6fSFelix Fietkau 
145518667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1456ba8c3d6fSFelix Fietkau 	}
1457ba8c3d6fSFelix Fietkau 
1458948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1459af818581SJohannes Berg 
14601d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
14611d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1462af818581SJohannes Berg 	/* Send all buffered frames to the station */
1463948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1464948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1465948d887dSJohannes Berg 
1466987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1467948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1468987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1469948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1470948d887dSJohannes Berg 		filtered += tmp - count;
1471948d887dSJohannes Berg 		count = tmp;
1472948d887dSJohannes Berg 
1473987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1474948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1475987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1476948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1477948d887dSJohannes Berg 		buffered += tmp - count;
1478948d887dSJohannes Berg 	}
1479948d887dSJohannes Berg 
1480e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
14815ac2e350SJohannes Berg 
14825ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
14835ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
14845ac2e350SJohannes Berg 
14855ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
14865ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
14875ac2e350SJohannes Berg 	 */
14885ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
14895ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
14901d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1491948d887dSJohannes Berg 
1492e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1493e3685e03SJohannes Berg 
1494af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1495af818581SJohannes Berg 
1496c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1497c868cb35SJohannes Berg 
1498bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
14992595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1500bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
150117c18bf8SJohannes Berg 
150217c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1503af818581SJohannes Berg }
1504af818581SJohannes Berg 
15050ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1506b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
15070ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1508ce662b44SJohannes Berg {
15090ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1510ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1511ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1512ce662b44SJohannes Berg 	struct sk_buff *skb;
1513ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1514ce662b44SJohannes Berg 	__le16 fc;
1515a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1516ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
151755de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1518ce662b44SJohannes Berg 
1519ce662b44SJohannes Berg 	if (qos) {
1520ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1521ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1522ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1523ce662b44SJohannes Berg 	} else {
1524ce662b44SJohannes Berg 		size -= 2;
1525ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1526ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1527ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1528ce662b44SJohannes Berg 	}
1529ce662b44SJohannes Berg 
1530ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1531ce662b44SJohannes Berg 	if (!skb)
1532ce662b44SJohannes Berg 		return;
1533ce662b44SJohannes Berg 
1534ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1535ce662b44SJohannes Berg 
15364df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1537ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1538ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1539ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1540ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1541ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1542864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1543ce662b44SJohannes Berg 
1544ce662b44SJohannes Berg 	skb->priority = tid;
1545ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
154659b66255SJohannes Berg 	if (qos) {
1547ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1548ce662b44SJohannes Berg 
15490ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1550ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1551ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
15520ead2510SEmmanuel Grumbach 			if (more_data)
15530ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
15540ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
15550ead2510SEmmanuel Grumbach 		}
1556ce662b44SJohannes Berg 	}
1557ce662b44SJohannes Berg 
1558ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1559ce662b44SJohannes Berg 
1560ce662b44SJohannes Berg 	/*
1561ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1562ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1563deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1564deeaee19SJohannes Berg 	 * ends the poll/service period.
1565ce662b44SJohannes Berg 	 */
156602f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1567deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1568ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1569ce662b44SJohannes Berg 
15706b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
15716b127c71SSujith Manoharan 
1572b77cf4f8SJohannes Berg 	if (call_driver)
1573b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1574b77cf4f8SJohannes Berg 					  reason, false);
157540b96408SJohannes Berg 
157689afe614SJohannes Berg 	skb->dev = sdata->dev;
157789afe614SJohannes Berg 
157855de908aSJohannes Berg 	rcu_read_lock();
1579d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
158055de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
158155de908aSJohannes Berg 		rcu_read_unlock();
158255de908aSJohannes Berg 		kfree_skb(skb);
158355de908aSJohannes Berg 		return;
158455de908aSJohannes Berg 	}
158555de908aSJohannes Berg 
158673c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
158708aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
158855de908aSJohannes Berg 	rcu_read_unlock();
1589ce662b44SJohannes Berg }
1590ce662b44SJohannes Berg 
15910a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
15920a1cb809SJohannes Berg {
15930a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
15940a1cb809SJohannes Berg 	if (tids & 0xF8)
15950a1cb809SJohannes Berg 		return fls(tids) - 1;
15960a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
15970a1cb809SJohannes Berg 	if (tids & BIT(0))
15980a1cb809SJohannes Berg 		return 0;
15990a1cb809SJohannes Berg 	return fls(tids) - 1;
16000a1cb809SJohannes Berg }
16010a1cb809SJohannes Berg 
16020ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
16030ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
16040ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
16050ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
16060ead2510SEmmanuel Grumbach  */
16070ead2510SEmmanuel Grumbach static bool
16080ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
16090ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
16100ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
16110ead2510SEmmanuel Grumbach {
16120ead2510SEmmanuel Grumbach 	int ac;
16130ead2510SEmmanuel Grumbach 
16140ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
16150ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
16160ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
16170ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
16180ead2510SEmmanuel Grumbach 	 */
16190ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
16200ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
16210ead2510SEmmanuel Grumbach 		return true;
16220ead2510SEmmanuel Grumbach 
16230ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1624f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
16250ead2510SEmmanuel Grumbach 			continue;
16260ead2510SEmmanuel Grumbach 
16270ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
16280ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
16290ead2510SEmmanuel Grumbach 			return true;
16300ead2510SEmmanuel Grumbach 	}
16310ead2510SEmmanuel Grumbach 
16320ead2510SEmmanuel Grumbach 	return false;
16330ead2510SEmmanuel Grumbach }
16340ead2510SEmmanuel Grumbach 
163547086fc5SJohannes Berg static void
16360ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
16370ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
16380ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
16390ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1640af818581SJohannes Berg {
1641af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1642af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1643948d887dSJohannes Berg 	int ac;
1644af818581SJohannes Berg 
1645f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1646948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
16474049e09aSJohannes Berg 		unsigned long tids;
16484049e09aSJohannes Berg 
1649f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1650948d887dSJohannes Berg 			continue;
1651948d887dSJohannes Berg 
16524049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
16534049e09aSJohannes Berg 
1654f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1655f9f760b4SJohannes Berg 		 * release from hardware queues
1656f9f760b4SJohannes Berg 		 */
16570ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
16580ead2510SEmmanuel Grumbach 			*driver_release_tids |=
16590ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
16600ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1661ba8c3d6fSFelix Fietkau 		}
1662f9f760b4SJohannes Berg 
16630ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
166447086fc5SJohannes Berg 			struct sk_buff *skb;
166547086fc5SJohannes Berg 
166647086fc5SJohannes Berg 			while (n_frames > 0) {
1667948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1668948d887dSJohannes Berg 				if (!skb) {
166947086fc5SJohannes Berg 					skb = skb_dequeue(
167047086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1671af818581SJohannes Berg 					if (skb)
1672af818581SJohannes Berg 						local->total_ps_buffered--;
1673af818581SJohannes Berg 				}
167447086fc5SJohannes Berg 				if (!skb)
167547086fc5SJohannes Berg 					break;
167647086fc5SJohannes Berg 				n_frames--;
16770ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
167847086fc5SJohannes Berg 			}
1679948d887dSJohannes Berg 		}
1680948d887dSJohannes Berg 
16810ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
16820ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
16830ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
16844049e09aSJohannes Berg 		 */
1685948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
16860ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1687948d887dSJohannes Berg 			break;
1688948d887dSJohannes Berg 	}
1689948d887dSJohannes Berg }
1690af818581SJohannes Berg 
16910ead2510SEmmanuel Grumbach static void
16920ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
16930ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
16940ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
16950ead2510SEmmanuel Grumbach {
16960ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
16970ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
16980ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
16990ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
17000ead2510SEmmanuel Grumbach 	bool more_data;
17010ead2510SEmmanuel Grumbach 
17020ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
17030ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
17040ead2510SEmmanuel Grumbach 
17050ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
17060ead2510SEmmanuel Grumbach 
17070ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
17080ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
17090ead2510SEmmanuel Grumbach 
17100ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
17110ead2510SEmmanuel Grumbach 
17121a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
17130ead2510SEmmanuel Grumbach 		driver_release_tids =
17140ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
17150ead2510SEmmanuel Grumbach 
1716f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1717f438ceb8SEmmanuel Grumbach 		int tid, ac;
17184049e09aSJohannes Berg 
1719ce662b44SJohannes Berg 		/*
1720ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1721ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1722ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1723ce662b44SJohannes Berg 		 *
1724ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1725ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1726ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1727ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1728ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1729ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1730ce662b44SJohannes Berg 		 *
1731ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1732ce662b44SJohannes Berg 		 */
1733ce662b44SJohannes Berg 
1734ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1735f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1736d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1737d7f84244SEmmanuel Grumbach 				break;
1738f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1739ce662b44SJohannes Berg 
17400ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1741f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
174247086fc5SJohannes Berg 		struct sk_buff_head pending;
174347086fc5SJohannes Berg 		struct sk_buff *skb;
174440b96408SJohannes Berg 		int num = 0;
174540b96408SJohannes Berg 		u16 tids = 0;
1746b77cf4f8SJohannes Berg 		bool need_null = false;
174747086fc5SJohannes Berg 
174847086fc5SJohannes Berg 		skb_queue_head_init(&pending);
174947086fc5SJohannes Berg 
175047086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1751af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
175247086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
175340b96408SJohannes Berg 			u8 *qoshdr = NULL;
175440b96408SJohannes Berg 
175540b96408SJohannes Berg 			num++;
1756af818581SJohannes Berg 
1757af818581SJohannes Berg 			/*
175847086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
175947086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
176047086fc5SJohannes Berg 			 * exchange.
1761af818581SJohannes Berg 			 */
17626b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
17636b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1764af818581SJohannes Berg 
176547086fc5SJohannes Berg 			/*
176647086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
176747086fc5SJohannes Berg 			 * more buffered frames for this STA
176847086fc5SJohannes Berg 			 */
176924b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
177047086fc5SJohannes Berg 				hdr->frame_control |=
177147086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
177224b9c373SJanusz.Dziedzic@tieto.com 			else
177324b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
177424b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1775af818581SJohannes Berg 
177640b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
177740b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
177840b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
177940b96408SJohannes Berg 
1780b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1781b77cf4f8SJohannes Berg 
1782b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1783b77cf4f8SJohannes Berg 
1784b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1785b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1786b77cf4f8SJohannes Berg 				continue;
1787b77cf4f8SJohannes Berg 
1788b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1789b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1790b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1791b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1792b77cf4f8SJohannes Berg 				break;
1793b77cf4f8SJohannes Berg 			}
1794b77cf4f8SJohannes Berg 
1795b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1796b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1797b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1798b77cf4f8SJohannes Berg 			 * and be done.
1799b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1800b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1801b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1802b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1803b77cf4f8SJohannes Berg 			 *
1804b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1805b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1806b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1807b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1808b77cf4f8SJohannes Berg 			 *
1809b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1810b77cf4f8SJohannes Berg 			 */
1811b77cf4f8SJohannes Berg 			if (qoshdr) {
181240b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1813deeaee19SJohannes Berg 
181447086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
181547086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1816b77cf4f8SJohannes Berg 			} else {
1817b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1818b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1819b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1820b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1821b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1822b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1823b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1824b77cf4f8SJohannes Berg 				 */
1825b77cf4f8SJohannes Berg 				hdr->frame_control |=
1826b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1827b77cf4f8SJohannes Berg 				need_null = true;
1828b77cf4f8SJohannes Berg 				num++;
182952a3f20cSMarco Porsch 			}
1830b77cf4f8SJohannes Berg 			break;
183147086fc5SJohannes Berg 		}
183247086fc5SJohannes Berg 
183340b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
183440b96408SJohannes Berg 					  reason, more_data);
183540b96408SJohannes Berg 
183647086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1837af818581SJohannes Berg 
1838b77cf4f8SJohannes Berg 		if (need_null)
1839b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
18400ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
18410ead2510SEmmanuel Grumbach 				reason, false, false);
1842b77cf4f8SJohannes Berg 
1843c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1844af818581SJohannes Berg 	} else {
1845ba8c3d6fSFelix Fietkau 		int tid;
1846ba8c3d6fSFelix Fietkau 
1847af818581SJohannes Berg 		/*
18484049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
18494049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1850f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1851f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1852f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1853f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1854f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1855f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1856af818581SJohannes Berg 		 */
18574049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
185847086fc5SJohannes Berg 					    n_frames, reason, more_data);
18594049e09aSJohannes Berg 
18604049e09aSJohannes Berg 		/*
18614049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
18624049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1863f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
18644049e09aSJohannes Berg 		 * release function.
1865f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1866ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1867ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
18684049e09aSJohannes Berg 		 */
1869ba8c3d6fSFelix Fietkau 
1870ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1871ba8c3d6fSFelix Fietkau 			return;
1872ba8c3d6fSFelix Fietkau 
1873ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1874adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1875adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
18761e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1877ba8c3d6fSFelix Fietkau 				continue;
1878ba8c3d6fSFelix Fietkau 
1879ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1880ba8c3d6fSFelix Fietkau 			break;
1881ba8c3d6fSFelix Fietkau 		}
1882af818581SJohannes Berg 	}
1883af818581SJohannes Berg }
1884af818581SJohannes Berg 
1885af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1886af818581SJohannes Berg {
188747086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1888af818581SJohannes Berg 
1889af818581SJohannes Berg 	/*
189047086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
189147086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
189247086fc5SJohannes Berg 	 * only from the non-enabled ones.
1893af818581SJohannes Berg 	 */
189447086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
189547086fc5SJohannes Berg 		ignore_for_response = 0;
1896af818581SJohannes Berg 
189747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
189847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1899af818581SJohannes Berg }
190047086fc5SJohannes Berg 
190147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
190247086fc5SJohannes Berg {
190347086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
190447086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
190547086fc5SJohannes Berg 
190647086fc5SJohannes Berg 	/*
190747086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
190847086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
190947086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
191047086fc5SJohannes Berg 	 * actually getting called.
191147086fc5SJohannes Berg 	 */
191247086fc5SJohannes Berg 	if (!delivery_enabled)
191347086fc5SJohannes Berg 		return;
191447086fc5SJohannes Berg 
191547086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
191647086fc5SJohannes Berg 	case 1:
191747086fc5SJohannes Berg 		n_frames = 2;
191847086fc5SJohannes Berg 		break;
191947086fc5SJohannes Berg 	case 2:
192047086fc5SJohannes Berg 		n_frames = 4;
192147086fc5SJohannes Berg 		break;
192247086fc5SJohannes Berg 	case 3:
192347086fc5SJohannes Berg 		n_frames = 6;
192447086fc5SJohannes Berg 		break;
192547086fc5SJohannes Berg 	case 0:
192647086fc5SJohannes Berg 		/* XXX: what is a good value? */
192713a8098aSAndrei Otcheretianski 		n_frames = 128;
192847086fc5SJohannes Berg 		break;
192947086fc5SJohannes Berg 	}
193047086fc5SJohannes Berg 
193147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
193247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1933af818581SJohannes Berg }
1934af818581SJohannes Berg 
1935af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1936af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1937af818581SJohannes Berg {
1938af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1939af818581SJohannes Berg 
1940b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1941b5878a2dSJohannes Berg 
19425ac2e350SJohannes Berg 	if (block) {
1943c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
194417c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
19455ac2e350SJohannes Berg 		return;
19465ac2e350SJohannes Berg 	}
19475ac2e350SJohannes Berg 
19485ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
19495ac2e350SJohannes Berg 		return;
19505ac2e350SJohannes Berg 
19515ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
19525ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
19535ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
19545ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
19555ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
19565ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
19575ac2e350SJohannes Berg 		/* must be asleep in this case */
19585ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
19595ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
19605ac2e350SJohannes Berg 	} else {
19615ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
196217c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
19635ac2e350SJohannes Berg 	}
1964af818581SJohannes Berg }
1965af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1966dcf55fb5SFelix Fietkau 
1967e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
196837fbd908SJohannes Berg {
196937fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
197037fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
197137fbd908SJohannes Berg 
197237fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
197337fbd908SJohannes Berg 
197437fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
197537fbd908SJohannes Berg }
1976e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
197737fbd908SJohannes Berg 
19780ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
19790ead2510SEmmanuel Grumbach {
19800ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
19810ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
19820ead2510SEmmanuel Grumbach 	bool more_data;
19830ead2510SEmmanuel Grumbach 
19840ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
19850ead2510SEmmanuel Grumbach 
19860ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
19870ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
19880ead2510SEmmanuel Grumbach 					       reason, 0);
19890ead2510SEmmanuel Grumbach 
19900ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
19910ead2510SEmmanuel Grumbach }
19920ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
19930ead2510SEmmanuel Grumbach 
1994042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1995042ec453SJohannes Berg 				u8 tid, bool buffered)
1996dcf55fb5SFelix Fietkau {
1997dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1998dcf55fb5SFelix Fietkau 
19995a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2000042ec453SJohannes Berg 		return;
2001042ec453SJohannes Berg 
20021b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
20031b000789SJohannes Berg 
2004948d887dSJohannes Berg 	if (buffered)
2005948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2006948d887dSJohannes Berg 	else
2007948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2008948d887dSJohannes Berg 
2009c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2010dcf55fb5SFelix Fietkau }
2011042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2012d9a7ddb0SJohannes Berg 
2013b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2014b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2015b4809e94SToke Høiland-Jørgensen {
2016942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2017942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2018942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2019942741daSFelix Fietkau 	u32 airtime = 0;
2020c77bfab9SFelix Fietkau 	u32 diff;
2021b4809e94SToke Høiland-Jørgensen 
2022942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2023942741daSFelix Fietkau 		airtime += tx_airtime;
2024942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2025942741daSFelix Fietkau 		airtime += rx_airtime;
2026b4809e94SToke Høiland-Jørgensen 
2027942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2028942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2029942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2030c77bfab9SFelix Fietkau 
2031c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2032c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2033942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2034c77bfab9SFelix Fietkau 
2035942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2036b4809e94SToke Høiland-Jørgensen }
2037b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2038b4809e94SToke Høiland-Jørgensen 
20393ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
20403ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
20413ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
20423ace10f5SKan Yan {
20433ace10f5SKan Yan 	int tx_pending;
20443ace10f5SKan Yan 
2045911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2046911bde0fSToke Høiland-Jørgensen 		return;
2047911bde0fSToke Høiland-Jørgensen 
20483ace10f5SKan Yan 	if (!tx_completed) {
20493ace10f5SKan Yan 		if (sta)
20503ace10f5SKan Yan 			atomic_add(tx_airtime,
20513ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
20523ace10f5SKan Yan 
20533ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
20548e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
20553ace10f5SKan Yan 		return;
20563ace10f5SKan Yan 	}
20573ace10f5SKan Yan 
20583ace10f5SKan Yan 	if (sta) {
20593ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
20603ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
206104e35caaSFelix Fietkau 		if (tx_pending < 0)
20623ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
20633ace10f5SKan Yan 				       tx_pending, 0);
20643ace10f5SKan Yan 	}
20653ace10f5SKan Yan 
20668e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
20673ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
20688e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
20693ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
20703ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
20713ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
20728e4bac06SFelix Fietkau 		      tx_airtime)) {
20738e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
20743ace10f5SKan Yan 			       tx_pending, 0);
20758e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
20768e4bac06SFelix Fietkau 	}
20773ace10f5SKan Yan }
20783ace10f5SKan Yan 
207983d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2080d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2081d9a7ddb0SJohannes Berg {
20828bf11d8dSJohannes Berg 	might_sleep();
2083d9a7ddb0SJohannes Berg 
2084d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2085d9a7ddb0SJohannes Berg 		return 0;
2086d9a7ddb0SJohannes Berg 
2087f09603a2SJohannes Berg 	/* check allowed transitions first */
2088f09603a2SJohannes Berg 
2089d9a7ddb0SJohannes Berg 	switch (new_state) {
2090d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2091f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2092d9a7ddb0SJohannes Berg 			return -EINVAL;
2093d9a7ddb0SJohannes Berg 		break;
2094d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2095f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2096f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2097d9a7ddb0SJohannes Berg 			return -EINVAL;
2098d9a7ddb0SJohannes Berg 		break;
2099d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2100f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2101f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2102d9a7ddb0SJohannes Berg 			return -EINVAL;
2103d9a7ddb0SJohannes Berg 		break;
2104d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2105f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2106d9a7ddb0SJohannes Berg 			return -EINVAL;
2107d9a7ddb0SJohannes Berg 		break;
2108d9a7ddb0SJohannes Berg 	default:
2109d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2110d9a7ddb0SJohannes Berg 		return -EINVAL;
2111d9a7ddb0SJohannes Berg 	}
2112d9a7ddb0SJohannes Berg 
2113bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2114bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2115f09603a2SJohannes Berg 
2116f09603a2SJohannes Berg 	/*
2117f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2118f09603a2SJohannes Berg 	 * fail the transition
2119f09603a2SJohannes Berg 	 */
2120f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2121f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2122f09603a2SJohannes Berg 					sta->sta_state, new_state);
2123f09603a2SJohannes Berg 		if (err)
2124f09603a2SJohannes Berg 			return err;
2125f09603a2SJohannes Berg 	}
2126f09603a2SJohannes Berg 
2127f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2128f09603a2SJohannes Berg 
2129f09603a2SJohannes Berg 	switch (new_state) {
2130f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2131f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2132f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2133f09603a2SJohannes Berg 		break;
2134f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2135a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2136f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2137a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2138f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2139a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
214052cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
214152cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2142a7201a6cSIlan Peer 		}
2143f09603a2SJohannes Berg 		break;
2144f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2145f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2146f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
21479cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
2148a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
214952cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
215052cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2151f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
215272f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2153f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
215417c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
215549ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2156f09603a2SJohannes Berg 		}
2157f09603a2SJohannes Berg 		break;
2158f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2159f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
216072f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2161f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
216217c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
216349ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2164f09603a2SJohannes Berg 		}
21653e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
21663e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
21673e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
21683e493173SJouni Malinen 						    sta->sta.addr);
2169f09603a2SJohannes Berg 		break;
2170f09603a2SJohannes Berg 	default:
2171f09603a2SJohannes Berg 		break;
2172f09603a2SJohannes Berg 	}
2173f09603a2SJohannes Berg 
2174d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2175d9a7ddb0SJohannes Berg 
2176d9a7ddb0SJohannes Berg 	return 0;
2177d9a7ddb0SJohannes Berg }
2178687da132SEmmanuel Grumbach 
2179c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2180c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2181c9c5962bSJohannes Berg {
2182046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2183c9c5962bSJohannes Berg 	int cpu;
2184c9c5962bSJohannes Berg 
2185046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2186c9c5962bSJohannes Berg 		return stats;
2187c9c5962bSJohannes Berg 
2188c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2189c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2190c9c5962bSJohannes Berg 
2191046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2192c9c5962bSJohannes Berg 
2193c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2194c9c5962bSJohannes Berg 			stats = cpustats;
2195c9c5962bSJohannes Berg 	}
2196c9c5962bSJohannes Berg 
2197c9c5962bSJohannes Berg 	return stats;
2198c9c5962bSJohannes Berg }
2199c9c5962bSJohannes Berg 
220041cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
22014f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2202fbd6ff5cSJohannes Berg {
2203dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2204fbd6ff5cSJohannes Berg 
2205dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
22067f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
22074f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2208dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2209dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2210dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2211dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22127f406cd1SJohannes Berg 		break;
22137f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
22144f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2215dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2216dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2217dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22187f406cd1SJohannes Berg 		break;
22197f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2220fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2221fbd6ff5cSJohannes Berg 		u16 brate;
22224f6b1b3dSJohannes Berg 		unsigned int shift;
2223dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2224dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2225fbd6ff5cSJohannes Berg 
2226dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
22278b783d10SThomas Pedersen 
22288b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
22298b783d10SThomas Pedersen 			break;
22308b783d10SThomas Pedersen 
2231dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
22324f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
22334f6b1b3dSJohannes Berg 			shift = 2;
22344f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
22354f6b1b3dSJohannes Berg 			shift = 1;
22364f6b1b3dSJohannes Berg 		else
22374f6b1b3dSJohannes Berg 			shift = 0;
2238fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
22397f406cd1SJohannes Berg 		break;
22407f406cd1SJohannes Berg 		}
224141cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
224241cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
224341cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
224441cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
224541cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
224641cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
224741cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
224841cbb0f5SLuca Coelho 		break;
2249fbd6ff5cSJohannes Berg 	}
22504f6b1b3dSJohannes Berg }
2251fbd6ff5cSJohannes Berg 
2252a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
22534f6b1b3dSJohannes Berg {
22546aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
22554f6b1b3dSJohannes Berg 
22564f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2257a17d93ffSBen Greear 		return -EINVAL;
2258a17d93ffSBen Greear 
22594f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2260a17d93ffSBen Greear 	return 0;
2261fbd6ff5cSJohannes Berg }
2262fbd6ff5cSJohannes Berg 
2263b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2264b255b72bSSeevalamuthu Mariappan 					int tid)
2265b255b72bSSeevalamuthu Mariappan {
2266b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2267b255b72bSSeevalamuthu Mariappan 	u64 value;
2268b255b72bSSeevalamuthu Mariappan 
2269b255b72bSSeevalamuthu Mariappan 	do {
2270b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2271b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2272b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2273b255b72bSSeevalamuthu Mariappan 
2274b255b72bSSeevalamuthu Mariappan 	return value;
2275b255b72bSSeevalamuthu Mariappan }
2276b255b72bSSeevalamuthu Mariappan 
22770f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
22780f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
22790f9c5a61SJohannes Berg 			     int tid)
22800f9c5a61SJohannes Berg {
22810f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2282b255b72bSSeevalamuthu Mariappan 	int cpu;
22830f9c5a61SJohannes Berg 
22840f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2285046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2286046d2e7cSSriram R 							   tid);
22870f9c5a61SJohannes Berg 
2288046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2289b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2290b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2291b255b72bSSeevalamuthu Mariappan 
2292046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2293046d2e7cSSriram R 						     cpu);
2294b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2295b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2296b255b72bSSeevalamuthu Mariappan 			}
2297b255b72bSSeevalamuthu Mariappan 		}
22980f9c5a61SJohannes Berg 
22990f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
23000f9c5a61SJohannes Berg 	}
23010f9c5a61SJohannes Berg 
23020f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
23030f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2304046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
23050f9c5a61SJohannes Berg 	}
23060f9c5a61SJohannes Berg 
23070f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
23080f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23090f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2310046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
23110f9c5a61SJohannes Berg 	}
23120f9c5a61SJohannes Berg 
23130f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
23140f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23150f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2316046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
23170f9c5a61SJohannes Berg 	}
23182fe4a29aSToke Høiland-Jørgensen 
23192fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
23202fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
23212fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
23222fe4a29aSToke Høiland-Jørgensen 
23232fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
23242fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
23252fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
23262fe4a29aSToke Høiland-Jørgensen 
23272fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
23282fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
23292fe4a29aSToke Høiland-Jørgensen 	}
23300f9c5a61SJohannes Berg }
23310f9c5a61SJohannes Berg 
2332c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2333c9c5962bSJohannes Berg {
2334c9c5962bSJohannes Berg 	unsigned int start;
2335c9c5962bSJohannes Berg 	u64 value;
2336c9c5962bSJohannes Berg 
2337c9c5962bSJohannes Berg 	do {
2338c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2339c9c5962bSJohannes Berg 		value = rxstats->bytes;
2340c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2341c9c5962bSJohannes Berg 
2342c9c5962bSJohannes Berg 	return value;
2343c9c5962bSJohannes Berg }
2344c9c5962bSJohannes Berg 
23450fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
23460fdf1493SJohannes Berg 		   bool tidstats)
2347b7ffbd7eSJohannes Berg {
2348b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2349b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2350b7ffbd7eSJohannes Berg 	u32 thr = 0;
2351c9c5962bSJohannes Berg 	int i, ac, cpu;
2352c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2353c9c5962bSJohannes Berg 
2354c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2355b7ffbd7eSJohannes Berg 
2356b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2357b7ffbd7eSJohannes Berg 
2358225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2359225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2360225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2361225b8189SJohannes Berg 	 */
2362225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2363bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2364225b8189SJohannes Berg 
23652b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2366a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2367a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2368a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2369a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
23709cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2371a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2372976bd9efSJohannes Berg 
2373976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2374bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2375bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2376a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2377976bd9efSJohannes Berg 	}
2378b7ffbd7eSJohannes Berg 
237984b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
23809cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2381e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2382b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
23832b9a7e1bSJohannes Berg 
2384a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2385a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2386b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
23872b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2388046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2389a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2390b7ffbd7eSJohannes Berg 	}
23912b9a7e1bSJohannes Berg 
2392a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
23932b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
23942b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2395046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2396a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
23972b9a7e1bSJohannes Berg 	}
23982b9a7e1bSJohannes Berg 
2399a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2400a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2401046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
24020f9c5a61SJohannes Berg 
2403046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2404c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2405c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2406c9c5962bSJohannes Berg 
2407046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2408046d2e7cSSriram R 						     cpu);
2409c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2410c9c5962bSJohannes Berg 			}
2411c9c5962bSJohannes Berg 		}
2412c9c5962bSJohannes Berg 
2413a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
24142b9a7e1bSJohannes Berg 	}
24152b9a7e1bSJohannes Berg 
2416a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2417046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2418046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2419c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2420c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2421c9c5962bSJohannes Berg 
2422046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2423046d2e7cSSriram R 						     cpu);
2424c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2425c9c5962bSJohannes Berg 			}
2426c9c5962bSJohannes Berg 		}
2427a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
24282b9a7e1bSJohannes Berg 	}
24292b9a7e1bSJohannes Berg 
2430a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2431046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2432a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
24332b9a7e1bSJohannes Berg 	}
24342b9a7e1bSJohannes Berg 
2435a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2436046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2437a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
24382b9a7e1bSJohannes Berg 	}
24392b9a7e1bSJohannes Berg 
2440b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2441b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2442b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2443b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2444b4809e94SToke Høiland-Jørgensen 	}
2445b4809e94SToke Høiland-Jørgensen 
2446b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2447b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2448b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2449b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2450b4809e94SToke Høiland-Jørgensen 	}
2451b4809e94SToke Høiland-Jørgensen 
2452b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2453942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2454b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2455b4809e94SToke Høiland-Jørgensen 	}
2456b4809e94SToke Høiland-Jørgensen 
2457046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2458046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2459c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2460c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2461c9c5962bSJohannes Berg 
2462046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2463e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2464c9c5962bSJohannes Berg 		}
2465c9c5962bSJohannes Berg 	}
2466b7ffbd7eSJohannes Berg 
2467225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2468225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2469a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2470a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2471225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2472225b8189SJohannes Berg 	}
2473225b8189SJohannes Berg 
247430686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
247530686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2476a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2477c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2478a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2479b7ffbd7eSJohannes Berg 		}
24802b9a7e1bSJohannes Berg 
2481046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2482a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
248340d9a38aSJohannes Berg 			sinfo->signal_avg =
2484046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2485a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
24862b9a7e1bSJohannes Berg 		}
24872b9a7e1bSJohannes Berg 	}
24882b9a7e1bSJohannes Berg 
2489c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2490c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2491c9c5962bSJohannes Berg 	 * pcpu statistics
2492c9c5962bSJohannes Berg 	 */
2493c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2494a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2495a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2496a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2497046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2498a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2499b7ffbd7eSJohannes Berg 
2500c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2501c9c5962bSJohannes Berg 
2502b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2503e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2504c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2505b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2506046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2507b7ffbd7eSJohannes Berg 		}
2508b7ffbd7eSJohannes Berg 	}
2509b7ffbd7eSJohannes Berg 
2510a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2511046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2512e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2513a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
25142b9a7e1bSJohannes Berg 	}
25152b9a7e1bSJohannes Berg 
2516a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2517a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2518a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
25192b9a7e1bSJohannes Berg 	}
2520b7ffbd7eSJohannes Berg 
25210fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
25226af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
25236af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
25248689c051SArend van Spriel 	}
252579c892b8SJohannes Berg 
2526b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2527b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2528a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2529a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2530a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2531a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2532a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2533dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
25341303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
25351303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2536b7ffbd7eSJohannes Berg 
2537433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2538433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2539433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2540b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2541a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2542433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2543b7ffbd7eSJohannes Berg 		}
2544433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2545433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2546433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2547dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
25481303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2549b7ffbd7eSJohannes Berg #endif
2550b7ffbd7eSJohannes Berg 	}
2551b7ffbd7eSJohannes Berg 
2552b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2553b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2554b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2555b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2556b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2557b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2558b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2559785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2560b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2561b7ffbd7eSJohannes Berg 
2562b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2563b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2564b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2565b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2566b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2567b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2568b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2569b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2570b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2571b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2572b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2573b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2574a74a8c84SJohannes Berg 	if (sta->sta.wme)
2575b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2576b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2577b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2578b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2579b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2580b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2581b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2582b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2583b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2584b7ffbd7eSJohannes Berg 
25853b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
25863b17fbf8SMaxim Altshul 
25873b17fbf8SMaxim Altshul 	if (thr != 0) {
2588a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
25893b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
25903b17fbf8SMaxim Altshul 	}
2591a78b26ffSVenkateswara Naralasetty 
2592a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2593046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2594046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2595a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2596a78b26ffSVenkateswara Naralasetty 	}
2597cc60dbbfSBalaji Pothunoori 
25989c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2599046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2600cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2601cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2602046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2603cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
26049c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2605cc60dbbfSBalaji Pothunoori 	}
2606ab60633cSNarayanraddi Masti 
2607ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2608ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2609ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2610ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2611ab60633cSNarayanraddi Masti 	}
26123b17fbf8SMaxim Altshul }
26133b17fbf8SMaxim Altshul 
26143b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
26153b17fbf8SMaxim Altshul {
26163b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
26173b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
26183b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
26193b17fbf8SMaxim Altshul 	u32 thr = 0;
26203b17fbf8SMaxim Altshul 
26213b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
26223b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
26233b17fbf8SMaxim Altshul 
2624b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2625b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2626b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2627b7ffbd7eSJohannes Berg 	else
26284fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2629b7ffbd7eSJohannes Berg 
26303b17fbf8SMaxim Altshul 	return thr;
2631b7ffbd7eSJohannes Berg }
2632b8da6b6aSJohannes Berg 
2633b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2634b8da6b6aSJohannes Berg {
2635c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2636c9c5962bSJohannes Berg 
2637046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2638046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2639c9c5962bSJohannes Berg 		return stats->last_rx;
2640046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2641b8da6b6aSJohannes Berg }
2642484a54c2SToke Høiland-Jørgensen 
2643484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2644484a54c2SToke Høiland-Jørgensen {
2645484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2646484a54c2SToke Høiland-Jørgensen 		return;
2647484a54c2SToke Høiland-Jørgensen 
2648484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2649484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2650484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2651484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2652484a54c2SToke Høiland-Jørgensen 	} else {
2653484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2654484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2655484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2656484a54c2SToke Høiland-Jørgensen 	}
2657484a54c2SToke Høiland-Jørgensen }
2658484a54c2SToke Høiland-Jørgensen 
2659484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2660484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2661484a54c2SToke Høiland-Jørgensen {
2662484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2663484a54c2SToke Høiland-Jørgensen 
2664484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2665484a54c2SToke Høiland-Jørgensen }
2666cb71f1d1SJohannes Berg 
2667cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2668cb71f1d1SJohannes Berg {
2669cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2670cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2671cb71f1d1SJohannes Berg 	int ret;
2672cb71f1d1SJohannes Berg 
2673cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2674cb71f1d1SJohannes Berg 
2675cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2676cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2677cb71f1d1SJohannes Berg 		return -EINVAL;
2678cb71f1d1SJohannes Berg 
2679cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2680cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2681cb71f1d1SJohannes Berg 		return -EBUSY;
2682cb71f1d1SJohannes Berg 
2683cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2684cb71f1d1SJohannes Berg 	if (!alloc)
2685cb71f1d1SJohannes Berg 		return -ENOMEM;
2686cb71f1d1SJohannes Berg 
2687cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2688cb71f1d1SJohannes Berg 	if (ret) {
2689cb71f1d1SJohannes Berg 		kfree(alloc);
2690cb71f1d1SJohannes Berg 		return ret;
2691cb71f1d1SJohannes Berg 	}
2692cb71f1d1SJohannes Berg 
2693cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2694cb71f1d1SJohannes Berg 
2695cb71f1d1SJohannes Berg 	return 0;
2696cb71f1d1SJohannes Berg }
2697cb71f1d1SJohannes Berg 
2698ba6ddab9SJohannes Berg static int link_sta_info_hash_add(struct ieee80211_local *local,
2699ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
2700ba6ddab9SJohannes Berg {
2701ba6ddab9SJohannes Berg 	return rhltable_insert(&local->link_sta_hash,
2702ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
2703ba6ddab9SJohannes Berg 			       link_sta_rht_params);
2704ba6ddab9SJohannes Berg }
2705ba6ddab9SJohannes Berg 
270621476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
270721476ad1SShaul Triebitz {
270821476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
270921476ad1SShaul Triebitz 
271021476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
271121476ad1SShaul Triebitz }
271221476ad1SShaul Triebitz 
2713cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2714cb71f1d1SJohannes Berg {
2715cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2716c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2717cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2718cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2719cb71f1d1SJohannes Berg 	int ret;
2720cb71f1d1SJohannes Berg 
2721c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2722c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2723cb71f1d1SJohannes Berg 
2724c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2725cb71f1d1SJohannes Berg 		return -EINVAL;
2726cb71f1d1SJohannes Berg 
2727cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2728cb71f1d1SJohannes Berg 
2729ba6ddab9SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_INSERTED)) {
2730ba6ddab9SJohannes Berg 		ret = 0;
2731ba6ddab9SJohannes Berg 		goto hash;
2732ba6ddab9SJohannes Berg 	}
2733cb71f1d1SJohannes Berg 
2734cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2735cb71f1d1SJohannes Berg 				   old_links, new_links);
2736cb71f1d1SJohannes Berg 	if (ret) {
2737cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2738ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2739cb71f1d1SJohannes Berg 	}
2740cb71f1d1SJohannes Berg 
2741ba6ddab9SJohannes Berg hash:
2742ba6ddab9SJohannes Berg 	link_sta_info_hash_add(sdata->local, link_sta);
2743ba6ddab9SJohannes Berg 
2744cb71f1d1SJohannes Berg 	return ret;
2745cb71f1d1SJohannes Berg }
2746cb71f1d1SJohannes Berg 
2747cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2748cb71f1d1SJohannes Berg {
2749cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2750cb71f1d1SJohannes Berg 
2751cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2752cb71f1d1SJohannes Berg 
2753cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2754cb71f1d1SJohannes Berg 
2755cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2756cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2757cb71f1d1SJohannes Berg 				     sta->sta.valid_links,
2758cb71f1d1SJohannes Berg 				     sta->sta.valid_links & ~BIT(link_id));
2759cb71f1d1SJohannes Berg 
2760ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2761cb71f1d1SJohannes Berg }
2762