xref: /openbmc/linux/net/mac80211/sta_info.c (revision 261ce8879578f42bc1ff3385ff1be8e31d6fb160)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f0706e82SJiri Benc /*
3f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
4f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
6dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
73a11ce08SJohannes Berg  * Copyright (C) 2018-2021 Intel Corporation
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
67cb71f1d1SJohannes Berg struct sta_link_alloc {
68cb71f1d1SJohannes Berg 	struct link_sta_info info;
69cb71f1d1SJohannes Berg 	struct ieee80211_link_sta sta;
70c71420dbSJohannes Berg 	struct rcu_head rcu_head;
71cb71f1d1SJohannes Berg };
72cb71f1d1SJohannes Berg 
737bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
747bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
75caf22d31SJohannes Berg 	.automatic_shrinking = true,
767bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
77ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
787bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
79ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
807bedd0cfSJohannes Berg };
817bedd0cfSJohannes Berg 
82ba6ddab9SJohannes Berg static const struct rhashtable_params link_sta_rht_params = {
83ba6ddab9SJohannes Berg 	.nelem_hint = 3, /* start small */
84ba6ddab9SJohannes Berg 	.automatic_shrinking = true,
85ba6ddab9SJohannes Berg 	.head_offset = offsetof(struct link_sta_info, link_hash_node),
86ba6ddab9SJohannes Berg 	.key_offset = offsetof(struct link_sta_info, addr),
87ba6ddab9SJohannes Berg 	.key_len = ETH_ALEN,
88ba6ddab9SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89ba6ddab9SJohannes Berg };
90ba6ddab9SJohannes Berg 
914d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
92be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
93f0706e82SJiri Benc 			     struct sta_info *sta)
94f0706e82SJiri Benc {
9583e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
967bedd0cfSJohannes Berg 			       sta_rht_params);
97f0706e82SJiri Benc }
98f0706e82SJiri Benc 
99f36fe0a2SJohannes Berg static int link_sta_info_hash_add(struct ieee80211_local *local,
100f36fe0a2SJohannes Berg 				  struct link_sta_info *link_sta)
101f36fe0a2SJohannes Berg {
102177577dbSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
103f36fe0a2SJohannes Berg 	return rhltable_insert(&local->link_sta_hash,
104f36fe0a2SJohannes Berg 			       &link_sta->link_hash_node,
105f36fe0a2SJohannes Berg 			       link_sta_rht_params);
106f36fe0a2SJohannes Berg }
107f36fe0a2SJohannes Berg 
108ba6ddab9SJohannes Berg static int link_sta_info_hash_del(struct ieee80211_local *local,
109ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
110ba6ddab9SJohannes Berg {
111177577dbSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
112ba6ddab9SJohannes Berg 	return rhltable_remove(&local->link_sta_hash,
113ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
114ba6ddab9SJohannes Berg 			       link_sta_rht_params);
115ba6ddab9SJohannes Berg }
116ba6ddab9SJohannes Berg 
1175108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
118b22cfcfcSEliad Peller {
119b22cfcfcSEliad Peller 	int ac, i;
120b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
121b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
122b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
123d012a605SMarco Porsch 	struct ps_data *ps;
124b22cfcfcSEliad Peller 
125e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1265ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1275ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
128d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
129d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
130d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1313f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1323f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
133d012a605SMarco Porsch 		else
134d012a605SMarco Porsch 			return;
135b22cfcfcSEliad Peller 
136b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
137e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1385ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
139b22cfcfcSEliad Peller 
140d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
141b22cfcfcSEliad Peller 	}
142b22cfcfcSEliad Peller 
143ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
144ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
145adf8ed01SJohannes Berg 			struct txq_info *txqi;
146adf8ed01SJohannes Berg 
147adf8ed01SJohannes Berg 			if (!sta->sta.txq[i])
148adf8ed01SJohannes Berg 				continue;
149adf8ed01SJohannes Berg 
150adf8ed01SJohannes Berg 			txqi = to_txq_info(sta->sta.txq[i]);
151ba8c3d6fSFelix Fietkau 
152fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
153ba8c3d6fSFelix Fietkau 		}
154ba8c3d6fSFelix Fietkau 	}
155ba8c3d6fSFelix Fietkau 
156b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
157b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1581f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1591f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
160b22cfcfcSEliad Peller 	}
161b22cfcfcSEliad Peller 
16245b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
16345b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
164b22cfcfcSEliad Peller 
1655ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
166b22cfcfcSEliad Peller 
167b22cfcfcSEliad Peller 	/*
168b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
169b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
170b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
171b22cfcfcSEliad Peller 	 * directly by station destruction.
172b22cfcfcSEliad Peller 	 */
1735a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
174661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
175b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
176b22cfcfcSEliad Peller 		if (!tid_tx)
177b22cfcfcSEliad Peller 			continue;
1781f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
179b22cfcfcSEliad Peller 		kfree(tid_tx);
180b22cfcfcSEliad Peller 	}
1815108ca82SJohannes Berg }
182b22cfcfcSEliad Peller 
1835108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1845108ca82SJohannes Berg {
1855108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1865108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1875108ca82SJohannes Berg 
1885108ca82SJohannes Berg 	__cleanup_single_sta(sta);
189b22cfcfcSEliad Peller 	sta_info_free(local, sta);
190b22cfcfcSEliad Peller }
191b22cfcfcSEliad Peller 
19283e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
19383e7e4ceSHerbert Xu 					 const u8 *addr)
19483e7e4ceSHerbert Xu {
19583e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
19683e7e4ceSHerbert Xu }
19783e7e4ceSHerbert Xu 
198d0709a65SJohannes Berg /* protected by RCU */
199abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
200abe60632SJohannes Berg 			      const u8 *addr)
20143ba7e95SJohannes Berg {
202abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
20383e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
20460f4b626SJohannes Berg 	struct sta_info *sta;
20543ba7e95SJohannes Berg 
20660f4b626SJohannes Berg 	rcu_read_lock();
20783e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
20860f4b626SJohannes Berg 		if (sta->sdata == sdata) {
20960f4b626SJohannes Berg 			rcu_read_unlock();
21060f4b626SJohannes Berg 			/* this is safe as the caller must already hold
21160f4b626SJohannes Berg 			 * another rcu read section or the mutex
21260f4b626SJohannes Berg 			 */
21360f4b626SJohannes Berg 			return sta;
21460f4b626SJohannes Berg 		}
21560f4b626SJohannes Berg 	}
21660f4b626SJohannes Berg 	rcu_read_unlock();
21760f4b626SJohannes Berg 	return NULL;
21843ba7e95SJohannes Berg }
21943ba7e95SJohannes Berg 
2200e5ded5aSFelix Fietkau /*
2210e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
2220e5ded5aSFelix Fietkau  * or from one of its vlans
2230e5ded5aSFelix Fietkau  */
2240e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
2250e5ded5aSFelix Fietkau 				  const u8 *addr)
2260e5ded5aSFelix Fietkau {
2270e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
22883e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
2290e5ded5aSFelix Fietkau 	struct sta_info *sta;
2300e5ded5aSFelix Fietkau 
2317bedd0cfSJohannes Berg 	rcu_read_lock();
23283e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2337bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2347bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2357bedd0cfSJohannes Berg 			rcu_read_unlock();
2367bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2377bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2387bedd0cfSJohannes Berg 			 */
2390e5ded5aSFelix Fietkau 			return sta;
2400e5ded5aSFelix Fietkau 		}
2417bedd0cfSJohannes Berg 	}
2427bedd0cfSJohannes Berg 	rcu_read_unlock();
2437bedd0cfSJohannes Berg 	return NULL;
2447bedd0cfSJohannes Berg }
2450e5ded5aSFelix Fietkau 
246ba6ddab9SJohannes Berg struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
247ba6ddab9SJohannes Berg 					      const u8 *addr)
248ba6ddab9SJohannes Berg {
249ba6ddab9SJohannes Berg 	return rhltable_lookup(&local->link_sta_hash, addr,
250ba6ddab9SJohannes Berg 			       link_sta_rht_params);
251ba6ddab9SJohannes Berg }
252ba6ddab9SJohannes Berg 
253ba6ddab9SJohannes Berg struct link_sta_info *
254ba6ddab9SJohannes Berg link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
255ba6ddab9SJohannes Berg {
256ba6ddab9SJohannes Berg 	struct ieee80211_local *local = sdata->local;
257ba6ddab9SJohannes Berg 	struct rhlist_head *tmp;
258ba6ddab9SJohannes Berg 	struct link_sta_info *link_sta;
259ba6ddab9SJohannes Berg 
260ba6ddab9SJohannes Berg 	rcu_read_lock();
261ba6ddab9SJohannes Berg 	for_each_link_sta_info(local, addr, link_sta, tmp) {
262ba6ddab9SJohannes Berg 		struct sta_info *sta = link_sta->sta;
263ba6ddab9SJohannes Berg 
264ba6ddab9SJohannes Berg 		if (sta->sdata == sdata ||
265ba6ddab9SJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
266ba6ddab9SJohannes Berg 			rcu_read_unlock();
267ba6ddab9SJohannes Berg 			/* this is safe as the caller must already hold
268ba6ddab9SJohannes Berg 			 * another rcu read section or the mutex
269ba6ddab9SJohannes Berg 			 */
270ba6ddab9SJohannes Berg 			return link_sta;
271ba6ddab9SJohannes Berg 		}
272ba6ddab9SJohannes Berg 	}
273ba6ddab9SJohannes Berg 	rcu_read_unlock();
274ba6ddab9SJohannes Berg 	return NULL;
275ba6ddab9SJohannes Berg }
276ba6ddab9SJohannes Berg 
2775072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
2785072f73cSToke Høiland-Jørgensen 				       const u8 *sta_addr, const u8 *vif_addr)
2795072f73cSToke Høiland-Jørgensen {
2805072f73cSToke Høiland-Jørgensen 	struct rhlist_head *tmp;
2815072f73cSToke Høiland-Jørgensen 	struct sta_info *sta;
2825072f73cSToke Høiland-Jørgensen 
2835072f73cSToke Høiland-Jørgensen 	for_each_sta_info(local, sta_addr, sta, tmp) {
2845072f73cSToke Høiland-Jørgensen 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
2855072f73cSToke Høiland-Jørgensen 			return sta;
2865072f73cSToke Høiland-Jørgensen 	}
2875072f73cSToke Høiland-Jørgensen 
2885072f73cSToke Høiland-Jørgensen 	return NULL;
2895072f73cSToke Høiland-Jørgensen }
2905072f73cSToke Høiland-Jørgensen 
2913b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2923b53fde8SJohannes Berg 				     int idx)
293ee385855SLuis Carlos Cobo {
2943b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
295ee385855SLuis Carlos Cobo 	struct sta_info *sta;
296ee385855SLuis Carlos Cobo 	int i = 0;
297ee385855SLuis Carlos Cobo 
2988ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
2998ca47eb9SMadhuparna Bhowmik 				lockdep_is_held(&local->sta_mtx)) {
3003b53fde8SJohannes Berg 		if (sdata != sta->sdata)
3012a8ca29aSLuis Carlos Cobo 			continue;
302ee385855SLuis Carlos Cobo 		if (i < idx) {
303ee385855SLuis Carlos Cobo 			++i;
304ee385855SLuis Carlos Cobo 			continue;
305ee385855SLuis Carlos Cobo 		}
3062a8ca29aSLuis Carlos Cobo 		return sta;
307ee385855SLuis Carlos Cobo 	}
308ee385855SLuis Carlos Cobo 
309ee385855SLuis Carlos Cobo 	return NULL;
310ee385855SLuis Carlos Cobo }
311f0706e82SJiri Benc 
312cb71f1d1SJohannes Berg static void sta_info_free_link(struct link_sta_info *link_sta)
313246b39e4SJohannes Berg {
314cb71f1d1SJohannes Berg 	free_percpu(link_sta->pcpu_rx_stats);
315cb71f1d1SJohannes Berg }
316246b39e4SJohannes Berg 
317ba6ddab9SJohannes Berg static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
318ba6ddab9SJohannes Berg 			    bool unhash)
319cb71f1d1SJohannes Berg {
320cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc = NULL;
321c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
322cb71f1d1SJohannes Berg 
323c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
324c71420dbSJohannes Berg 					     lockdep_is_held(&sta->local->sta_mtx));
325c71420dbSJohannes Berg 
326c71420dbSJohannes Berg 	if (WARN_ON(!link_sta))
327cb71f1d1SJohannes Berg 		return;
328246b39e4SJohannes Berg 
329ba6ddab9SJohannes Berg 	if (unhash)
330ba6ddab9SJohannes Berg 		link_sta_info_hash_del(sta->local, link_sta);
331ba6ddab9SJohannes Berg 
332c71420dbSJohannes Berg 	if (link_sta != &sta->deflink)
333c71420dbSJohannes Berg 		alloc = container_of(link_sta, typeof(*alloc), info);
334cb71f1d1SJohannes Berg 
335cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
336c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->link[link_id], NULL);
337c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
338cb71f1d1SJohannes Berg 	if (alloc) {
339cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
340c71420dbSJohannes Berg 		kfree_rcu(alloc, rcu_head);
341246b39e4SJohannes Berg 	}
342246b39e4SJohannes Berg }
343246b39e4SJohannes Berg 
34493e5deb1SJohannes Berg /**
345d9a7ddb0SJohannes Berg  * sta_info_free - free STA
34693e5deb1SJohannes Berg  *
3476ef307bcSRandy Dunlap  * @local: pointer to the global information
34893e5deb1SJohannes Berg  * @sta: STA info to free
34993e5deb1SJohannes Berg  *
35093e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
351d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
352d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
353d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
35493e5deb1SJohannes Berg  */
355d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
35693e5deb1SJohannes Berg {
357cb71f1d1SJohannes Berg 	int i;
358cb71f1d1SJohannes Berg 
359cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
360cb71f1d1SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
361cb71f1d1SJohannes Berg 			continue;
362cb71f1d1SJohannes Berg 
3630ad49045SJohannes Berg 		sta_remove_link(sta, i, false);
364cb71f1d1SJohannes Berg 	}
365cb71f1d1SJohannes Berg 
366dcd479e1SJohannes Berg 	/*
367dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
368dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
369dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
370dcd479e1SJohannes Berg 	 *
371dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
372dcd479e1SJohannes Berg 	 * allocated during state transitions.
373dcd479e1SJohannes Berg 	 */
374dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
375dcd479e1SJohannes Berg 		int ret;
376dcd479e1SJohannes Berg 
377dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
378dcd479e1SJohannes Berg 
379dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
380dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
381dcd479e1SJohannes Berg 			break;
382dcd479e1SJohannes Berg 	}
383dcd479e1SJohannes Berg 
384889cbb91SJohannes Berg 	if (sta->rate_ctrl)
3854b7679a5SJohannes Berg 		rate_control_free_sta(sta);
38693e5deb1SJohannes Berg 
387bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
38893e5deb1SJohannes Berg 
389ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
390ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
39153d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
392433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
393433f5bc1SJohannes Berg 	kfree(sta->mesh);
394433f5bc1SJohannes Berg #endif
395246b39e4SJohannes Berg 
396cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
39793e5deb1SJohannes Berg 	kfree(sta);
39893e5deb1SJohannes Berg }
39993e5deb1SJohannes Berg 
4004d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
40162b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
402d0709a65SJohannes Berg 			     struct sta_info *sta)
403f0706e82SJiri Benc {
40483e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
4057bedd0cfSJohannes Berg 			       sta_rht_params);
406f0706e82SJiri Benc }
407f0706e82SJiri Benc 
4085ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
409af818581SJohannes Berg {
410af818581SJohannes Berg 	struct sta_info *sta;
411af818581SJohannes Berg 
4125ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
413af818581SJohannes Berg 
414af818581SJohannes Berg 	if (sta->dead)
415af818581SJohannes Berg 		return;
416af818581SJohannes Berg 
41754420473SHelmut Schaa 	local_bh_disable();
4185ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
419af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
4205ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
421af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
4225ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
42347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
424ce662b44SJohannes Berg 	local_bh_enable();
425af818581SJohannes Berg }
426af818581SJohannes Berg 
427af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
428af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
429af65cd96SJohannes Berg {
43030686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
431af65cd96SJohannes Berg 		return 0;
432af65cd96SJohannes Berg 
433889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
434af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
43535c347acSJohannes Berg 						     sta, gfp);
436889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
437af65cd96SJohannes Berg 		return -ENOMEM;
438af65cd96SJohannes Berg 
439af65cd96SJohannes Berg 	return 0;
440af65cd96SJohannes Berg }
441af65cd96SJohannes Berg 
442cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
443246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
444246b39e4SJohannes Berg 			       gfp_t gfp)
445246b39e4SJohannes Berg {
446246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
447246b39e4SJohannes Berg 	int i;
448246b39e4SJohannes Berg 
449246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
450246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
451246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
452246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
453246b39e4SJohannes Berg 			return -ENOMEM;
454246b39e4SJohannes Berg 	}
455246b39e4SJohannes Berg 
456246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
457246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
458246b39e4SJohannes Berg 
459246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
460246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
461246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
462246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
463246b39e4SJohannes Berg 
464246b39e4SJohannes Berg 	return 0;
465246b39e4SJohannes Berg }
466246b39e4SJohannes Berg 
467cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
468cb71f1d1SJohannes Berg 			      unsigned int link_id,
469cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
470cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
471cb71f1d1SJohannes Berg {
472cb71f1d1SJohannes Berg 	link_info->sta = sta;
473cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
474c71420dbSJohannes Berg 	link_info->pub = link_sta;
475c73993b8SJohannes Berg 	link_sta->link_id = link_id;
476c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
477c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
478*261ce887SBenjamin Berg 
479*261ce887SBenjamin Berg 	link_sta->smps_mode = IEEE80211_SMPS_OFF;
480cb71f1d1SJohannes Berg }
481cb71f1d1SJohannes Berg 
482f36fe0a2SJohannes Berg static struct sta_info *
483f36fe0a2SJohannes Berg __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
484f36fe0a2SJohannes Berg 		 const u8 *addr, int link_id, const u8 *link_addr,
485f36fe0a2SJohannes Berg 		 gfp_t gfp)
486f0706e82SJiri Benc {
487d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
488ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
489f0706e82SJiri Benc 	struct sta_info *sta;
49016c5f15cSRon Rindjunsky 	int i;
491f0706e82SJiri Benc 
492ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
493f0706e82SJiri Benc 	if (!sta)
49473651ee6SJohannes Berg 		return NULL;
495f0706e82SJiri Benc 
496246b39e4SJohannes Berg 	sta->local = local;
497246b39e4SJohannes Berg 	sta->sdata = sdata;
498246b39e4SJohannes Berg 
499cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
50036fe8e4eSLorenzo Bianconi 		goto free;
501c9c5962bSJohannes Berg 
502cb71f1d1SJohannes Berg 	if (link_id >= 0) {
503cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
504cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
505cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
506cb71f1d1SJohannes Berg 	} else {
507cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
508cb71f1d1SJohannes Berg 	}
509cb71f1d1SJohannes Berg 
51007346f81SJohannes Berg 	spin_lock_init(&sta->lock);
5111d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
5125ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
51367c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
514a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
51587f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
516433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
517433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
518433f5bc1SJohannes Berg 		if (!sta->mesh)
519433f5bc1SJohannes Berg 			goto free;
5204c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
521433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
52245d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5234c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5244c02d62fSKees Cook 				    0);
525433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
526433f5bc1SJohannes Berg 	}
52787f59c70SThomas Pedersen #endif
52807346f81SJohannes Berg 
529ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
53017741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
531f36fe0a2SJohannes Berg 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
532f36fe0a2SJohannes Berg 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
533480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
534480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
535480dd46bSMaxim Altshul 
536046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
537046d2e7cSSriram R 
53896fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
53996fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
54096fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
54196fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
54296fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
54396fc6efbSAlexander Wetzel 	 */
54496fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
54596fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
54696fc6efbSAlexander Wetzel 
5470f9c5a61SJohannes Berg 
5483a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5493a11ce08SJohannes Berg 
55071ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
55171ec375cSJohannes Berg 
552b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
553b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
554b6da911bSLiad Kaufman 
55584b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
556541a45a1SBruno Randolf 
557ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
558ba8c3d6fSFelix Fietkau 		void *txq_data;
559ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
560ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
561ba8c3d6fSFelix Fietkau 
562ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
563ba8c3d6fSFelix Fietkau 		if (!txq_data)
564ba8c3d6fSFelix Fietkau 			goto free;
565ba8c3d6fSFelix Fietkau 
566ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
567ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
568ba8c3d6fSFelix Fietkau 
569adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
570fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
571abfbc3afSJohannes Berg 		}
572ba8c3d6fSFelix Fietkau 	}
573ba8c3d6fSFelix Fietkau 
574ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
575ba8c3d6fSFelix Fietkau 		goto free_txq;
576f0706e82SJiri Benc 
577942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
578b4809e94SToke Høiland-Jørgensen 
579948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
580948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
581948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
582942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
583942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
584942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
585942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
586948d887dSJohannes Berg 	}
58773651ee6SJohannes Berg 
5885a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
5894be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
590cccaec98SSenthil Balasubramanian 
591bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
592bd718fc1SJohannes Berg 		u32 mandatory = 0;
593bd718fc1SJohannes Berg 		int r;
594bd718fc1SJohannes Berg 
595bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
596bd718fc1SJohannes Berg 			continue;
597bd718fc1SJohannes Berg 
598bd718fc1SJohannes Berg 		switch (i) {
599bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
60063fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
601bd718fc1SJohannes Berg 			/*
602bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
603bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
604bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
605bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
606bd718fc1SJohannes Berg 			 * possible rate anyway.
607bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
608bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
609bd718fc1SJohannes Berg 			 */
610bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
611bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
612bd718fc1SJohannes Berg 			break;
613bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
614bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
615bd718fc1SJohannes Berg 			break;
616bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
617bd718fc1SJohannes Berg 			WARN_ON(1);
618bd718fc1SJohannes Berg 			mandatory = 0;
619bd718fc1SJohannes Berg 			break;
620bd718fc1SJohannes Berg 		}
621bd718fc1SJohannes Berg 
622bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
623bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
624bd718fc1SJohannes Berg 
625bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
626bd718fc1SJohannes Berg 
627bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
628bd718fc1SJohannes Berg 				continue;
629046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
630bd718fc1SJohannes Berg 		}
631bd718fc1SJohannes Berg 	}
632bd718fc1SJohannes Berg 
6336e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
6346e0456b5SFelix Fietkau 
635484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
636484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
637484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
638484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
639dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
640dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
641484a54c2SToke Høiland-Jørgensen 
642bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
643ef04a297SJohannes Berg 
644abfbc3afSJohannes Berg 	return sta;
645ba8c3d6fSFelix Fietkau 
646ba8c3d6fSFelix Fietkau free_txq:
647ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
648ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
649ba8c3d6fSFelix Fietkau free:
650cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
651433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
652433f5bc1SJohannes Berg 	kfree(sta->mesh);
653433f5bc1SJohannes Berg #endif
654ba8c3d6fSFelix Fietkau 	kfree(sta);
655ba8c3d6fSFelix Fietkau 	return NULL;
65673651ee6SJohannes Berg }
65773651ee6SJohannes Berg 
658f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
659f36fe0a2SJohannes Berg 				const u8 *addr, gfp_t gfp)
660f36fe0a2SJohannes Berg {
661f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
662f36fe0a2SJohannes Berg }
663f36fe0a2SJohannes Berg 
664f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
665f36fe0a2SJohannes Berg 					  const u8 *mld_addr,
666f36fe0a2SJohannes Berg 					  unsigned int link_id,
667f36fe0a2SJohannes Berg 					  const u8 *link_addr,
668f36fe0a2SJohannes Berg 					  gfp_t gfp)
669f36fe0a2SJohannes Berg {
670f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
671f36fe0a2SJohannes Berg }
672f36fe0a2SJohannes Berg 
6738c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
67434e89507SJohannes Berg {
67534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
67634e89507SJohannes Berg 
67703e4497eSJohannes Berg 	/*
67803e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
67903e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
68003e4497eSJohannes Berg 	 * and another CPU turns off the net device.
68103e4497eSJohannes Berg 	 */
6828c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
6838c71df7aSGuy Eilam 		return -ENETDOWN;
68403e4497eSJohannes Berg 
685b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
686deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
6878c71df7aSGuy Eilam 		return -EINVAL;
6888c71df7aSGuy Eilam 
68983e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
69083e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
69183e7e4ceSHerbert Xu 	 * for correctness.
69231104891SJohannes Berg 	 */
69331104891SJohannes Berg 	rcu_read_lock();
69431104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
69531104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
69631104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
69731104891SJohannes Berg 		rcu_read_unlock();
69831104891SJohannes Berg 		return -ENOTUNIQ;
69931104891SJohannes Berg 	}
70031104891SJohannes Berg 	rcu_read_unlock();
70131104891SJohannes Berg 
7028c71df7aSGuy Eilam 	return 0;
70393e5deb1SJohannes Berg }
70444213b5eSJohannes Berg 
705f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
706f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
707f09603a2SJohannes Berg 				     struct sta_info *sta)
708f09603a2SJohannes Berg {
709f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
710f09603a2SJohannes Berg 	int err = 0;
711f09603a2SJohannes Berg 
712f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
713f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
714f09603a2SJohannes Berg 		if (err)
715f09603a2SJohannes Berg 			break;
716f09603a2SJohannes Berg 	}
717f09603a2SJohannes Berg 
718f09603a2SJohannes Berg 	if (!err) {
719a4ec45a4SJohannes Berg 		/*
720a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
721a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
722a4ec45a4SJohannes Berg 		 */
723a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
724f09603a2SJohannes Berg 			sta->uploaded = true;
725f09603a2SJohannes Berg 		return 0;
726f09603a2SJohannes Berg 	}
727f09603a2SJohannes Berg 
728f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
729bdcbd8e0SJohannes Berg 		sdata_info(sdata,
730bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
731bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
732f09603a2SJohannes Berg 		err = 0;
733f09603a2SJohannes Berg 	}
734f09603a2SJohannes Berg 
735f09603a2SJohannes Berg 	/* unwind on error */
736f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
737f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
738f09603a2SJohannes Berg 
739f09603a2SJohannes Berg 	return err;
740f09603a2SJohannes Berg }
741f09603a2SJohannes Berg 
742d405fd8cSGregory Greenman static void
743d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
744d405fd8cSGregory Greenman {
745d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
746d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
747d405fd8cSGregory Greenman 	struct sta_info *sta;
748d405fd8cSGregory Greenman 
749d405fd8cSGregory Greenman 	rcu_read_lock();
750d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
751d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
752d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
753d405fd8cSGregory Greenman 			continue;
754d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
755d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
756d405fd8cSGregory Greenman 			break;
757d405fd8cSGregory Greenman 		}
758d405fd8cSGregory Greenman 	}
759d405fd8cSGregory Greenman 	rcu_read_unlock();
760d405fd8cSGregory Greenman 
761d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
762d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
763d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
764d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
765d405fd8cSGregory Greenman 	}
766d405fd8cSGregory Greenman }
767d405fd8cSGregory Greenman 
76834e89507SJohannes Berg /*
7698c71df7aSGuy Eilam  * should be called with sta_mtx locked
7708c71df7aSGuy Eilam  * this function replaces the mutex lock
7718c71df7aSGuy Eilam  * with a RCU lock
7728c71df7aSGuy Eilam  */
7734d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
7748c71df7aSGuy Eilam {
7758c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
7768c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
7770c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
7788c71df7aSGuy Eilam 	int err = 0;
7798c71df7aSGuy Eilam 
7808c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
7818c71df7aSGuy Eilam 
7827852e361SJohannes Berg 	/* check if STA exists already */
7837852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
7844d33960bSJohannes Berg 		err = -EEXIST;
7858f9dcc29SAhmed Zaki 		goto out_cleanup;
78634e89507SJohannes Berg 	}
78734e89507SJohannes Berg 
7880c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
7890c2e3842SKoen Vandeputte 	if (!sinfo) {
7900c2e3842SKoen Vandeputte 		err = -ENOMEM;
7918f9dcc29SAhmed Zaki 		goto out_cleanup;
7920c2e3842SKoen Vandeputte 	}
7930c2e3842SKoen Vandeputte 
7944d33960bSJohannes Berg 	local->num_sta++;
7954d33960bSJohannes Berg 	local->sta_generation++;
7964d33960bSJohannes Berg 	smp_mb();
7974d33960bSJohannes Berg 
7985108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
7995108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
8005108ca82SJohannes Berg 
8014d33960bSJohannes Berg 	/* make the station visible */
80262b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
80362b14b24SJohannes Berg 	if (err)
80462b14b24SJohannes Berg 		goto out_drop_sta;
8054d33960bSJohannes Berg 
806f36fe0a2SJohannes Berg 	if (sta->sta.valid_links) {
807f36fe0a2SJohannes Berg 		err = link_sta_info_hash_add(local, &sta->deflink);
808f36fe0a2SJohannes Berg 		if (err) {
809f36fe0a2SJohannes Berg 			sta_info_hash_del(local, sta);
810f36fe0a2SJohannes Berg 			goto out_drop_sta;
811f36fe0a2SJohannes Berg 		}
812f36fe0a2SJohannes Berg 	}
813f36fe0a2SJohannes Berg 
8142bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
81583d5cc01SJohannes Berg 
8164dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8174dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8184dde3c36SMordechay Goodstein 	 */
8194dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8200cbf348aSAndrei Otcheretianski 		ieee80211_recalc_min_chandef(sta->sdata, -1);
8214dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8224dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8234dde3c36SMordechay Goodstein 	}
8244dde3c36SMordechay Goodstein 
8255108ca82SJohannes Berg 	/* notify driver */
8265108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8275108ca82SJohannes Berg 	if (err)
8285108ca82SJohannes Berg 		goto out_remove;
8295108ca82SJohannes Berg 
83083d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
831d405fd8cSGregory Greenman 
8325108ca82SJohannes Berg 	/* accept BA sessions now */
8335108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8344d33960bSJohannes Berg 
8354d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8364d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
8374d33960bSJohannes Berg 
8380ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
8390ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
8400ef049dcSArnd Bergmann 	kfree(sinfo);
841d0709a65SJohannes Berg 
842bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
843f0706e82SJiri Benc 
84434e89507SJohannes Berg 	/* move reference to rcu-protected */
84534e89507SJohannes Berg 	rcu_read_lock();
84634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
847e9f207f0SJiri Benc 
84873651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
84973651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
85073651ee6SJohannes Berg 
85173651ee6SJohannes Berg 	return 0;
8525108ca82SJohannes Berg  out_remove:
8530ad49045SJohannes Berg 	if (sta->sta.valid_links)
8540ad49045SJohannes Berg 		link_sta_info_hash_del(local, &sta->deflink);
8555108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
8565108ca82SJohannes Berg 	list_del_rcu(&sta->list);
85762b14b24SJohannes Berg  out_drop_sta:
8585108ca82SJohannes Berg 	local->num_sta--;
8595108ca82SJohannes Berg 	synchronize_net();
8608f9dcc29SAhmed Zaki  out_cleanup:
8617bc40aedSJohannes Berg 	cleanup_single_sta(sta);
8624d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
863ea32f065SSudip Mukherjee 	kfree(sinfo);
8644d33960bSJohannes Berg 	rcu_read_lock();
8654d33960bSJohannes Berg 	return err;
8668c71df7aSGuy Eilam }
8678c71df7aSGuy Eilam 
8688c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
8698c71df7aSGuy Eilam {
8708c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
871308f7fcfSZhao, Gang 	int err;
8728c71df7aSGuy Eilam 
8734d33960bSJohannes Berg 	might_sleep();
8744d33960bSJohannes Berg 
87531104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
87631104891SJohannes Berg 
8778c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
8788c71df7aSGuy Eilam 	if (err) {
8797bc40aedSJohannes Berg 		sta_info_free(local, sta);
88031104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
8818c71df7aSGuy Eilam 		rcu_read_lock();
8827bc40aedSJohannes Berg 		return err;
8838c71df7aSGuy Eilam 	}
8848c71df7aSGuy Eilam 
8857bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
886f0706e82SJiri Benc }
887f0706e82SJiri Benc 
88834e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
88934e89507SJohannes Berg {
89034e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
89134e89507SJohannes Berg 
89234e89507SJohannes Berg 	rcu_read_unlock();
89334e89507SJohannes Berg 
89434e89507SJohannes Berg 	return err;
89534e89507SJohannes Berg }
89634e89507SJohannes Berg 
897d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
898004c872eSJohannes Berg {
899004c872eSJohannes Berg 	/*
900004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
901004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
902004c872eSJohannes Berg 	 */
903d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
904004c872eSJohannes Berg }
905004c872eSJohannes Berg 
906d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
907004c872eSJohannes Berg {
908004c872eSJohannes Berg 	/*
909004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
910004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
911004c872eSJohannes Berg 	 */
912d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
913004c872eSJohannes Berg }
914004c872eSJohannes Berg 
9153d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9163d5839b6SIlan Peer {
9173d5839b6SIlan Peer 	/*
9183d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9193d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9203d5839b6SIlan Peer 	 */
9213d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9223d5839b6SIlan Peer }
9233d5839b6SIlan Peer 
924948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
925004c872eSJohannes Berg {
926948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
927948d887dSJohannes Berg 	switch (ac) {
928948d887dSJohannes Berg 	case IEEE80211_AC_VO:
929948d887dSJohannes Berg 		return BIT(6) | BIT(7);
930948d887dSJohannes Berg 	case IEEE80211_AC_VI:
931948d887dSJohannes Berg 		return BIT(4) | BIT(5);
932948d887dSJohannes Berg 	case IEEE80211_AC_BE:
933948d887dSJohannes Berg 		return BIT(0) | BIT(3);
934948d887dSJohannes Berg 	case IEEE80211_AC_BK:
935948d887dSJohannes Berg 		return BIT(1) | BIT(2);
936948d887dSJohannes Berg 	default:
937948d887dSJohannes Berg 		WARN_ON(1);
938948d887dSJohannes Berg 		return 0;
939d0709a65SJohannes Berg 	}
940004c872eSJohannes Berg }
941004c872eSJohannes Berg 
9429b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
943004c872eSJohannes Berg {
944c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
945d012a605SMarco Porsch 	struct ps_data *ps;
946948d887dSJohannes Berg 	bool indicate_tim = false;
947948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
948948d887dSJohannes Berg 	int ac;
949a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
950004c872eSJohannes Berg 
951d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
952d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
953c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
954c868cb35SJohannes Berg 			return;
9553e122be0SJohannes Berg 
956d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
9573f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
9583f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
9593f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
9603f52b7e3SMarco Porsch #endif
961d012a605SMarco Porsch 	} else {
962d012a605SMarco Porsch 		return;
963d012a605SMarco Porsch 	}
964d012a605SMarco Porsch 
965c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
966d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
967c868cb35SJohannes Berg 		return;
968004c872eSJohannes Berg 
969c868cb35SJohannes Berg 	if (sta->dead)
970c868cb35SJohannes Berg 		goto done;
9713e122be0SJohannes Berg 
972948d887dSJohannes Berg 	/*
973948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
974948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
975948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
976948d887dSJohannes Berg 	 * non-enabled ones.
977948d887dSJohannes Berg 	 */
978948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
979948d887dSJohannes Berg 		ignore_for_tim = 0;
980948d887dSJohannes Berg 
9819b7a86f3SJohannes Berg 	if (ignore_pending)
9829b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
9839b7a86f3SJohannes Berg 
984948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
985948d887dSJohannes Berg 		unsigned long tids;
986948d887dSJohannes Berg 
987f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
988948d887dSJohannes Berg 			continue;
989948d887dSJohannes Berg 
990948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
991948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
992948d887dSJohannes Berg 		if (indicate_tim)
993948d887dSJohannes Berg 			break;
994948d887dSJohannes Berg 
995948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
996948d887dSJohannes Berg 
997948d887dSJohannes Berg 		indicate_tim |=
998948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
999ba8c3d6fSFelix Fietkau 		indicate_tim |=
1000ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
1001004c872eSJohannes Berg 	}
1002004c872eSJohannes Berg 
1003c868cb35SJohannes Berg  done:
100465f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
1005004c872eSJohannes Berg 
10063d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
10073d5839b6SIlan Peer 		goto out_unlock;
10083d5839b6SIlan Peer 
1009948d887dSJohannes Berg 	if (indicate_tim)
1010d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1011c868cb35SJohannes Berg 	else
1012d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10133e122be0SJohannes Berg 
10149b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1015c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1016948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1017c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1018004c872eSJohannes Berg 	}
1019004c872eSJohannes Berg 
10203d5839b6SIlan Peer out_unlock:
102165f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1022004c872eSJohannes Berg }
1023004c872eSJohannes Berg 
10249b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10259b7a86f3SJohannes Berg {
10269b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10279b7a86f3SJohannes Berg }
10289b7a86f3SJohannes Berg 
1029cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1030f0706e82SJiri Benc {
1031e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1032f0706e82SJiri Benc 	int timeout;
1033f0706e82SJiri Benc 
1034f0706e82SJiri Benc 	if (!skb)
1035cd0b8d89SJohannes Berg 		return false;
1036f0706e82SJiri Benc 
1037e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1038f0706e82SJiri Benc 
1039f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
104057c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
104157c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
104257c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1043f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1044f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1045e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1046f0706e82SJiri Benc }
1047f0706e82SJiri Benc 
1048f0706e82SJiri Benc 
1049948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1050948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1051f0706e82SJiri Benc {
1052f0706e82SJiri Benc 	unsigned long flags;
1053f0706e82SJiri Benc 	struct sk_buff *skb;
1054f0706e82SJiri Benc 
105560750397SJohannes Berg 	/*
105660750397SJohannes Berg 	 * First check for frames that should expire on the filtered
105760750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
105860750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
105960750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
106060750397SJohannes Berg 	 * total_ps_buffered counter.
106160750397SJohannes Berg 	 */
1062f0706e82SJiri Benc 	for (;;) {
1063948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1064948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
106557c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1066948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1067836341a7SJohannes Berg 		else
1068f0706e82SJiri Benc 			skb = NULL;
1069948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1070f0706e82SJiri Benc 
107160750397SJohannes Berg 		/*
107260750397SJohannes Berg 		 * Frames are queued in order, so if this one
107360750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
107460750397SJohannes Berg 		 * we actually reached the end of the queue we
107560750397SJohannes Berg 		 * also need to stop, of course.
107660750397SJohannes Berg 		 */
107760750397SJohannes Berg 		if (!skb)
107860750397SJohannes Berg 			break;
1079d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
108060750397SJohannes Berg 	}
108160750397SJohannes Berg 
108260750397SJohannes Berg 	/*
108360750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
108460750397SJohannes Berg 	 * only find something if the filtered queue was emptied
108560750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
108660750397SJohannes Berg 	 * buffered frames.
108760750397SJohannes Berg 	 */
1088f0706e82SJiri Benc 	for (;;) {
1089948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1090948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1091f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1092948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1093f0706e82SJiri Benc 		else
1094f0706e82SJiri Benc 			skb = NULL;
1095948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1096f0706e82SJiri Benc 
109760750397SJohannes Berg 		/*
109860750397SJohannes Berg 		 * frames are queued in order, so if this one
109960750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
110060750397SJohannes Berg 		 * the queue) we can stop testing
110160750397SJohannes Berg 		 */
1102836341a7SJohannes Berg 		if (!skb)
1103836341a7SJohannes Berg 			break;
1104836341a7SJohannes Berg 
1105f0706e82SJiri Benc 		local->total_ps_buffered--;
1106bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1107bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1108d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1109f0706e82SJiri Benc 	}
11103393a608SJuuso Oikarinen 
111160750397SJohannes Berg 	/*
111260750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
111360750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
111460750397SJohannes Berg 	 * frames.
111560750397SJohannes Berg 	 */
111660750397SJohannes Berg 	sta_info_recalc_tim(sta);
111760750397SJohannes Berg 
111860750397SJohannes Berg 	/*
111960750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
112060750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
112160750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
112260750397SJohannes Berg 	 */
1123948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1124948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1125948d887dSJohannes Berg }
1126948d887dSJohannes Berg 
1127948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1128948d887dSJohannes Berg 					     struct sta_info *sta)
1129948d887dSJohannes Berg {
1130948d887dSJohannes Berg 	bool have_buffered = false;
1131948d887dSJohannes Berg 	int ac;
1132948d887dSJohannes Berg 
11333f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11343f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11353f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1136948d887dSJohannes Berg 		return false;
1137948d887dSJohannes Berg 
1138948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1139948d887dSJohannes Berg 		have_buffered |=
1140948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1141948d887dSJohannes Berg 
1142948d887dSJohannes Berg 	return have_buffered;
1143f0706e82SJiri Benc }
1144f0706e82SJiri Benc 
1145d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
114634e89507SJohannes Berg {
114734e89507SJohannes Berg 	struct ieee80211_local *local;
114834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
11490ad49045SJohannes Berg 	int ret, i;
115034e89507SJohannes Berg 
115134e89507SJohannes Berg 	might_sleep();
115234e89507SJohannes Berg 
115334e89507SJohannes Berg 	if (!sta)
115434e89507SJohannes Berg 		return -ENOENT;
115534e89507SJohannes Berg 
115634e89507SJohannes Berg 	local = sta->local;
115734e89507SJohannes Berg 	sdata = sta->sdata;
115834e89507SJohannes Berg 
115983d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
116083d5cc01SJohannes Berg 
1161098a6070SJohannes Berg 	/*
1162098a6070SJohannes Berg 	 * Before removing the station from the driver and
1163098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1164098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1165098a6070SJohannes Berg 	 * will be sufficient.
1166098a6070SJohannes Berg 	 */
1167c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1168c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1169098a6070SJohannes Berg 
1170f59374ebSSara Sharon 	/*
1171f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1172f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1173f59374ebSSara Sharon 	 * all such frames to be processed.
1174f59374ebSSara Sharon 	 */
1175f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1176f59374ebSSara Sharon 
11770ad49045SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
11780ad49045SJohannes Berg 		struct link_sta_info *link_sta;
11790ad49045SJohannes Berg 
11800ad49045SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
11810ad49045SJohannes Berg 			continue;
11820ad49045SJohannes Berg 
11830ad49045SJohannes Berg 		link_sta = rcu_dereference_protected(sta->link[i],
11840ad49045SJohannes Berg 						     lockdep_is_held(&local->sta_mtx));
11850ad49045SJohannes Berg 
11860ad49045SJohannes Berg 		link_sta_info_hash_del(local, link_sta);
11870ad49045SJohannes Berg 	}
11880ad49045SJohannes Berg 
118934e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1190b01711beSJohannes Berg 	if (WARN_ON(ret))
119134e89507SJohannes Berg 		return ret;
119234e89507SJohannes Berg 
1193a7a6bdd0SArik Nemtsov 	/*
1194a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1195a7a6bdd0SArik Nemtsov 	 * removal.
1196a7a6bdd0SArik Nemtsov 	 */
1197a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1198a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1199a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1200a7a6bdd0SArik Nemtsov 	}
1201a7a6bdd0SArik Nemtsov 
1202794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1203ef044763SEliad Peller 	sta->removed = true;
12044d33960bSJohannes Berg 
12056a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
12066a9d1b91SJohannes Berg 
1207a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1208a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1209a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1210a710c816SJohannes Berg 
1211d778207bSJohannes Berg 	return 0;
1212d778207bSJohannes Berg }
1213d778207bSJohannes Berg 
1214d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1215d778207bSJohannes Berg {
1216d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1217d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
12180ef049dcSArnd Bergmann 	struct station_info *sinfo;
1219d778207bSJohannes Berg 	int ret;
1220d778207bSJohannes Berg 
1221d778207bSJohannes Berg 	/*
1222d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1223d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1224d778207bSJohannes Berg 	 */
1225d778207bSJohannes Berg 
1226d778207bSJohannes Berg 	might_sleep();
1227d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1228d778207bSJohannes Berg 
12295981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1230b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1231b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1232b16798f5SJohannes Berg 	}
1233b16798f5SJohannes Berg 
1234c8782078SJohannes Berg 	/* now keys can no longer be reached */
12356d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
123634e89507SJohannes Berg 
12379b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
12389b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
12399b7a86f3SJohannes Berg 
124034e89507SJohannes Berg 	sta->dead = true;
124134e89507SJohannes Berg 
124234e89507SJohannes Berg 	local->num_sta--;
124334e89507SJohannes Berg 	local->sta_generation++;
124434e89507SJohannes Berg 
124583d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1246f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1247f09603a2SJohannes Berg 		if (ret) {
124883d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
124983d5cc01SJohannes Berg 			break;
125083d5cc01SJohannes Berg 		}
125183d5cc01SJohannes Berg 	}
1252d9a7ddb0SJohannes Berg 
1253f09603a2SJohannes Berg 	if (sta->uploaded) {
1254f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1255f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1256f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1257f09603a2SJohannes Berg 	}
125834e89507SJohannes Berg 
1259bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1260bdcbd8e0SJohannes Berg 
12610ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
12620ef049dcSArnd Bergmann 	if (sinfo)
12630fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
12640ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
12650ef049dcSArnd Bergmann 	kfree(sinfo);
1266ec15e68bSJouni Malinen 
126734e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
126834e89507SJohannes Berg 
12693a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
12703a11ce08SJohannes Berg 
1271d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1272d778207bSJohannes Berg }
1273d778207bSJohannes Berg 
1274d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1275d778207bSJohannes Berg {
1276d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1277d778207bSJohannes Berg 
1278d778207bSJohannes Berg 	if (err)
1279d778207bSJohannes Berg 		return err;
1280d778207bSJohannes Berg 
1281d778207bSJohannes Berg 	synchronize_net();
1282d778207bSJohannes Berg 
1283d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
128434e89507SJohannes Berg 
128534e89507SJohannes Berg 	return 0;
128634e89507SJohannes Berg }
128734e89507SJohannes Berg 
128834e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
128934e89507SJohannes Berg {
129034e89507SJohannes Berg 	struct sta_info *sta;
129134e89507SJohannes Berg 	int ret;
129234e89507SJohannes Berg 
129334e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12947852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
129534e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
129634e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
129734e89507SJohannes Berg 
129834e89507SJohannes Berg 	return ret;
129934e89507SJohannes Berg }
130034e89507SJohannes Berg 
130134e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
130234e89507SJohannes Berg 			      const u8 *addr)
130334e89507SJohannes Berg {
130434e89507SJohannes Berg 	struct sta_info *sta;
130534e89507SJohannes Berg 	int ret;
130634e89507SJohannes Berg 
130734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
13087852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
130934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
131034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
131134e89507SJohannes Berg 
131234e89507SJohannes Berg 	return ret;
131334e89507SJohannes Berg }
1314f0706e82SJiri Benc 
131534f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1316f0706e82SJiri Benc {
131734f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1318f0706e82SJiri Benc 	struct sta_info *sta;
13193393a608SJuuso Oikarinen 	bool timer_needed = false;
1320f0706e82SJiri Benc 
1321d0709a65SJohannes Berg 	rcu_read_lock();
1322d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
13233393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
13243393a608SJuuso Oikarinen 			timer_needed = true;
1325d0709a65SJohannes Berg 	rcu_read_unlock();
1326f0706e82SJiri Benc 
13275bb644a0SJohannes Berg 	if (local->quiescing)
13285bb644a0SJohannes Berg 		return;
13295bb644a0SJohannes Berg 
13303393a608SJuuso Oikarinen 	if (!timer_needed)
13313393a608SJuuso Oikarinen 		return;
13323393a608SJuuso Oikarinen 
133326d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
133426d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1335f0706e82SJiri Benc }
1336f0706e82SJiri Benc 
13377bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
13387bedd0cfSJohannes Berg {
13397bedd0cfSJohannes Berg 	int err;
13407bedd0cfSJohannes Berg 
134183e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
13427bedd0cfSJohannes Berg 	if (err)
13437bedd0cfSJohannes Berg 		return err;
13447bedd0cfSJohannes Berg 
1345ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1346ba6ddab9SJohannes Berg 	if (err) {
1347ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1348ba6ddab9SJohannes Berg 		return err;
1349ba6ddab9SJohannes Berg 	}
1350ba6ddab9SJohannes Berg 
13514d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
135234e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1353f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1354f0706e82SJiri Benc 
135534f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
13567bedd0cfSJohannes Berg 	return 0;
1357f0706e82SJiri Benc }
1358f0706e82SJiri Benc 
1359f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1360f0706e82SJiri Benc {
1361a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
136283e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1363ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1364f0706e82SJiri Benc }
1365f0706e82SJiri Benc 
1366051007d9SJohannes Berg 
1367e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1368f0706e82SJiri Benc {
1369b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1370f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1371d778207bSJohannes Berg 	LIST_HEAD(free_list);
137244213b5eSJohannes Berg 	int ret = 0;
1373f0706e82SJiri Benc 
1374d0709a65SJohannes Berg 	might_sleep();
1375d0709a65SJohannes Berg 
1376e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1377e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1378e716251dSJohannes Berg 
137934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
138034e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1381e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1382e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1383d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1384d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
138534316837SJohannes Berg 			ret++;
138634316837SJohannes Berg 		}
138734e89507SJohannes Berg 	}
1388d778207bSJohannes Berg 
1389d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1390d778207bSJohannes Berg 		synchronize_net();
1391d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1392d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1393d778207bSJohannes Berg 	}
139434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
139544213b5eSJohannes Berg 
1396051007d9SJohannes Berg 	return ret;
1397051007d9SJohannes Berg }
1398051007d9SJohannes Berg 
139924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
140024723d1bSJohannes Berg 			  unsigned long exp_time)
140124723d1bSJohannes Berg {
140224723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
140324723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
140424723d1bSJohannes Berg 
140534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1406e46a2cf9SMohammed Shafi Shajakhan 
1407e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1408b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1409b8da6b6aSJohannes Berg 
1410ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1411ec2b774eSMarek Lindner 			continue;
1412ec2b774eSMarek Lindner 
1413b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1414eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1415bdcbd8e0SJohannes Berg 				sta->sta.addr);
14163f52b7e3SMarco Porsch 
14173f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
14183f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
14193f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
14203f52b7e3SMarco Porsch 
142134e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
142224723d1bSJohannes Berg 		}
1423e46a2cf9SMohammed Shafi Shajakhan 	}
1424e46a2cf9SMohammed Shafi Shajakhan 
142534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
142624723d1bSJohannes Berg }
142717741cdcSJohannes Berg 
1428686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1429686b9cb9SBen Greear 						   const u8 *addr,
1430686b9cb9SBen Greear 						   const u8 *localaddr)
143117741cdcSJohannes Berg {
14327bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
143383e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
14347bedd0cfSJohannes Berg 	struct sta_info *sta;
143517741cdcSJohannes Berg 
1436686b9cb9SBen Greear 	/*
1437686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1438686b9cb9SBen Greear 	 * ... first in list.
1439686b9cb9SBen Greear 	 */
144083e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1441686b9cb9SBen Greear 		if (localaddr &&
1442b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1443686b9cb9SBen Greear 			continue;
1444f7c65594SJohannes Berg 		if (!sta->uploaded)
1445f7c65594SJohannes Berg 			return NULL;
144617741cdcSJohannes Berg 		return &sta->sta;
1447f7c65594SJohannes Berg 	}
1448f7c65594SJohannes Berg 
1449abe60632SJohannes Berg 	return NULL;
145017741cdcSJohannes Berg }
1451686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
14525ed176e1SJohannes Berg 
14535ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
14545ed176e1SJohannes Berg 					 const u8 *addr)
14555ed176e1SJohannes Berg {
1456f7c65594SJohannes Berg 	struct sta_info *sta;
14575ed176e1SJohannes Berg 
14585ed176e1SJohannes Berg 	if (!vif)
14595ed176e1SJohannes Berg 		return NULL;
14605ed176e1SJohannes Berg 
1461f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1462f7c65594SJohannes Berg 	if (!sta)
1463f7c65594SJohannes Berg 		return NULL;
14645ed176e1SJohannes Berg 
1465f7c65594SJohannes Berg 	if (!sta->uploaded)
1466f7c65594SJohannes Berg 		return NULL;
1467f7c65594SJohannes Berg 
1468f7c65594SJohannes Berg 	return &sta->sta;
14695ed176e1SJohannes Berg }
147017741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1471af818581SJohannes Berg 
1472e3685e03SJohannes Berg /* powersave support code */
1473e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
147450a9432dSJohannes Berg {
1475608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1476e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1477e3685e03SJohannes Berg 	struct sk_buff_head pending;
1478ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1479e3685e03SJohannes Berg 	unsigned long flags;
1480d012a605SMarco Porsch 	struct ps_data *ps;
1481d012a605SMarco Porsch 
14823918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
14833918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
14843918edb0SFelix Fietkau 				     u.ap);
14853918edb0SFelix Fietkau 
14863918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1487d012a605SMarco Porsch 		ps = &sdata->bss->ps;
14883f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
14893f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1490d012a605SMarco Porsch 	else
1491d012a605SMarco Porsch 		return;
149250a9432dSJohannes Berg 
1493c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
149447086fc5SJohannes Berg 
14955a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1496948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1497ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1498948d887dSJohannes Berg 
149930686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
150012375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1501af818581SJohannes Berg 
1502ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1503adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1504ba8c3d6fSFelix Fietkau 			continue;
1505ba8c3d6fSFelix Fietkau 
150618667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1507ba8c3d6fSFelix Fietkau 	}
1508ba8c3d6fSFelix Fietkau 
1509948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1510af818581SJohannes Berg 
15111d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
15121d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1513af818581SJohannes Berg 	/* Send all buffered frames to the station */
1514948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1515948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1516948d887dSJohannes Berg 
1517987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1518948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1519987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1520948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1521948d887dSJohannes Berg 		filtered += tmp - count;
1522948d887dSJohannes Berg 		count = tmp;
1523948d887dSJohannes Berg 
1524987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1525948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1526987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1527948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1528948d887dSJohannes Berg 		buffered += tmp - count;
1529948d887dSJohannes Berg 	}
1530948d887dSJohannes Berg 
1531e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
15325ac2e350SJohannes Berg 
15335ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
15345ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
15355ac2e350SJohannes Berg 
15365ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
15375ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
15385ac2e350SJohannes Berg 	 */
15395ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
15405ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
15411d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1542948d887dSJohannes Berg 
1543e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1544e3685e03SJohannes Berg 
1545af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1546af818581SJohannes Berg 
1547c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1548c868cb35SJohannes Berg 
1549bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
15502595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1551bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
155217c18bf8SJohannes Berg 
155317c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1554af818581SJohannes Berg }
1555af818581SJohannes Berg 
15560ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1557b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
15580ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1559ce662b44SJohannes Berg {
15600ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1561ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1562ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1563ce662b44SJohannes Berg 	struct sk_buff *skb;
1564ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1565ce662b44SJohannes Berg 	__le16 fc;
1566a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1567ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
156855de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1569ce662b44SJohannes Berg 
1570ce662b44SJohannes Berg 	if (qos) {
1571ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1572ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1573ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1574ce662b44SJohannes Berg 	} else {
1575ce662b44SJohannes Berg 		size -= 2;
1576ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1577ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1578ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1579ce662b44SJohannes Berg 	}
1580ce662b44SJohannes Berg 
1581ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1582ce662b44SJohannes Berg 	if (!skb)
1583ce662b44SJohannes Berg 		return;
1584ce662b44SJohannes Berg 
1585ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1586ce662b44SJohannes Berg 
15874df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1588ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1589ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1590ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1591ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1592ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1593864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1594ce662b44SJohannes Berg 
1595ce662b44SJohannes Berg 	skb->priority = tid;
1596ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
159759b66255SJohannes Berg 	if (qos) {
1598ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1599ce662b44SJohannes Berg 
16000ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1601ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1602ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
16030ead2510SEmmanuel Grumbach 			if (more_data)
16040ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
16050ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
16060ead2510SEmmanuel Grumbach 		}
1607ce662b44SJohannes Berg 	}
1608ce662b44SJohannes Berg 
1609ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1610ce662b44SJohannes Berg 
1611ce662b44SJohannes Berg 	/*
1612ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1613ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1614deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1615deeaee19SJohannes Berg 	 * ends the poll/service period.
1616ce662b44SJohannes Berg 	 */
161702f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1618deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1619ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1620ce662b44SJohannes Berg 
16216b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
16226b127c71SSujith Manoharan 
1623b77cf4f8SJohannes Berg 	if (call_driver)
1624b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1625b77cf4f8SJohannes Berg 					  reason, false);
162640b96408SJohannes Berg 
162789afe614SJohannes Berg 	skb->dev = sdata->dev;
162889afe614SJohannes Berg 
162955de908aSJohannes Berg 	rcu_read_lock();
1630d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
163155de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
163255de908aSJohannes Berg 		rcu_read_unlock();
163355de908aSJohannes Berg 		kfree_skb(skb);
163455de908aSJohannes Berg 		return;
163555de908aSJohannes Berg 	}
163655de908aSJohannes Berg 
163773c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
163808aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
163955de908aSJohannes Berg 	rcu_read_unlock();
1640ce662b44SJohannes Berg }
1641ce662b44SJohannes Berg 
16420a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
16430a1cb809SJohannes Berg {
16440a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
16450a1cb809SJohannes Berg 	if (tids & 0xF8)
16460a1cb809SJohannes Berg 		return fls(tids) - 1;
16470a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
16480a1cb809SJohannes Berg 	if (tids & BIT(0))
16490a1cb809SJohannes Berg 		return 0;
16500a1cb809SJohannes Berg 	return fls(tids) - 1;
16510a1cb809SJohannes Berg }
16520a1cb809SJohannes Berg 
16530ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
16540ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
16550ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
16560ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
16570ead2510SEmmanuel Grumbach  */
16580ead2510SEmmanuel Grumbach static bool
16590ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
16600ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
16610ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
16620ead2510SEmmanuel Grumbach {
16630ead2510SEmmanuel Grumbach 	int ac;
16640ead2510SEmmanuel Grumbach 
16650ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
16660ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
16670ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
16680ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
16690ead2510SEmmanuel Grumbach 	 */
16700ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
16710ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
16720ead2510SEmmanuel Grumbach 		return true;
16730ead2510SEmmanuel Grumbach 
16740ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1675f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
16760ead2510SEmmanuel Grumbach 			continue;
16770ead2510SEmmanuel Grumbach 
16780ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
16790ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
16800ead2510SEmmanuel Grumbach 			return true;
16810ead2510SEmmanuel Grumbach 	}
16820ead2510SEmmanuel Grumbach 
16830ead2510SEmmanuel Grumbach 	return false;
16840ead2510SEmmanuel Grumbach }
16850ead2510SEmmanuel Grumbach 
168647086fc5SJohannes Berg static void
16870ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
16880ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
16890ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
16900ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1691af818581SJohannes Berg {
1692af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1693af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1694948d887dSJohannes Berg 	int ac;
1695af818581SJohannes Berg 
1696f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1697948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
16984049e09aSJohannes Berg 		unsigned long tids;
16994049e09aSJohannes Berg 
1700f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1701948d887dSJohannes Berg 			continue;
1702948d887dSJohannes Berg 
17034049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
17044049e09aSJohannes Berg 
1705f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1706f9f760b4SJohannes Berg 		 * release from hardware queues
1707f9f760b4SJohannes Berg 		 */
17080ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
17090ead2510SEmmanuel Grumbach 			*driver_release_tids |=
17100ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
17110ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1712ba8c3d6fSFelix Fietkau 		}
1713f9f760b4SJohannes Berg 
17140ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
171547086fc5SJohannes Berg 			struct sk_buff *skb;
171647086fc5SJohannes Berg 
171747086fc5SJohannes Berg 			while (n_frames > 0) {
1718948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1719948d887dSJohannes Berg 				if (!skb) {
172047086fc5SJohannes Berg 					skb = skb_dequeue(
172147086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1722af818581SJohannes Berg 					if (skb)
1723af818581SJohannes Berg 						local->total_ps_buffered--;
1724af818581SJohannes Berg 				}
172547086fc5SJohannes Berg 				if (!skb)
172647086fc5SJohannes Berg 					break;
172747086fc5SJohannes Berg 				n_frames--;
17280ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
172947086fc5SJohannes Berg 			}
1730948d887dSJohannes Berg 		}
1731948d887dSJohannes Berg 
17320ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
17330ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
17340ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
17354049e09aSJohannes Berg 		 */
1736948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17370ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1738948d887dSJohannes Berg 			break;
1739948d887dSJohannes Berg 	}
1740948d887dSJohannes Berg }
1741af818581SJohannes Berg 
17420ead2510SEmmanuel Grumbach static void
17430ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
17440ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
17450ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
17460ead2510SEmmanuel Grumbach {
17470ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
17480ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
17490ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
17500ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
17510ead2510SEmmanuel Grumbach 	bool more_data;
17520ead2510SEmmanuel Grumbach 
17530ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
17540ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
17550ead2510SEmmanuel Grumbach 
17560ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
17570ead2510SEmmanuel Grumbach 
17580ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
17590ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
17600ead2510SEmmanuel Grumbach 
17610ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
17620ead2510SEmmanuel Grumbach 
17631a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
17640ead2510SEmmanuel Grumbach 		driver_release_tids =
17650ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
17660ead2510SEmmanuel Grumbach 
1767f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1768f438ceb8SEmmanuel Grumbach 		int tid, ac;
17694049e09aSJohannes Berg 
1770ce662b44SJohannes Berg 		/*
1771ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1772ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1773ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1774ce662b44SJohannes Berg 		 *
1775ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1776ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1777ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1778ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1779ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1780ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1781ce662b44SJohannes Berg 		 *
1782ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1783ce662b44SJohannes Berg 		 */
1784ce662b44SJohannes Berg 
1785ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1786f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1787d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1788d7f84244SEmmanuel Grumbach 				break;
1789f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1790ce662b44SJohannes Berg 
17910ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1792f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
179347086fc5SJohannes Berg 		struct sk_buff_head pending;
179447086fc5SJohannes Berg 		struct sk_buff *skb;
179540b96408SJohannes Berg 		int num = 0;
179640b96408SJohannes Berg 		u16 tids = 0;
1797b77cf4f8SJohannes Berg 		bool need_null = false;
179847086fc5SJohannes Berg 
179947086fc5SJohannes Berg 		skb_queue_head_init(&pending);
180047086fc5SJohannes Berg 
180147086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1802af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
180347086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
180440b96408SJohannes Berg 			u8 *qoshdr = NULL;
180540b96408SJohannes Berg 
180640b96408SJohannes Berg 			num++;
1807af818581SJohannes Berg 
1808af818581SJohannes Berg 			/*
180947086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
181047086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
181147086fc5SJohannes Berg 			 * exchange.
1812af818581SJohannes Berg 			 */
18136b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
18146b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1815af818581SJohannes Berg 
181647086fc5SJohannes Berg 			/*
181747086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
181847086fc5SJohannes Berg 			 * more buffered frames for this STA
181947086fc5SJohannes Berg 			 */
182024b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
182147086fc5SJohannes Berg 				hdr->frame_control |=
182247086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
182324b9c373SJanusz.Dziedzic@tieto.com 			else
182424b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
182524b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1826af818581SJohannes Berg 
182740b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
182840b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
182940b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
183040b96408SJohannes Berg 
1831b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1832b77cf4f8SJohannes Berg 
1833b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1834b77cf4f8SJohannes Berg 
1835b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1836b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1837b77cf4f8SJohannes Berg 				continue;
1838b77cf4f8SJohannes Berg 
1839b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1840b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1841b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1842b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1843b77cf4f8SJohannes Berg 				break;
1844b77cf4f8SJohannes Berg 			}
1845b77cf4f8SJohannes Berg 
1846b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1847b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1848b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1849b77cf4f8SJohannes Berg 			 * and be done.
1850b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1851b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1852b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1853b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1854b77cf4f8SJohannes Berg 			 *
1855b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1856b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1857b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1858b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1859b77cf4f8SJohannes Berg 			 *
1860b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1861b77cf4f8SJohannes Berg 			 */
1862b77cf4f8SJohannes Berg 			if (qoshdr) {
186340b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1864deeaee19SJohannes Berg 
186547086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
186647086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1867b77cf4f8SJohannes Berg 			} else {
1868b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1869b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1870b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1871b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1872b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1873b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1874b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1875b77cf4f8SJohannes Berg 				 */
1876b77cf4f8SJohannes Berg 				hdr->frame_control |=
1877b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1878b77cf4f8SJohannes Berg 				need_null = true;
1879b77cf4f8SJohannes Berg 				num++;
188052a3f20cSMarco Porsch 			}
1881b77cf4f8SJohannes Berg 			break;
188247086fc5SJohannes Berg 		}
188347086fc5SJohannes Berg 
188440b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
188540b96408SJohannes Berg 					  reason, more_data);
188640b96408SJohannes Berg 
188747086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1888af818581SJohannes Berg 
1889b77cf4f8SJohannes Berg 		if (need_null)
1890b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
18910ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
18920ead2510SEmmanuel Grumbach 				reason, false, false);
1893b77cf4f8SJohannes Berg 
1894c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1895af818581SJohannes Berg 	} else {
1896ba8c3d6fSFelix Fietkau 		int tid;
1897ba8c3d6fSFelix Fietkau 
1898af818581SJohannes Berg 		/*
18994049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
19004049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1901f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1902f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1903f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1904f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1905f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1906f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1907af818581SJohannes Berg 		 */
19084049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
190947086fc5SJohannes Berg 					    n_frames, reason, more_data);
19104049e09aSJohannes Berg 
19114049e09aSJohannes Berg 		/*
19124049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
19134049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1914f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
19154049e09aSJohannes Berg 		 * release function.
1916f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1917ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1918ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
19194049e09aSJohannes Berg 		 */
1920ba8c3d6fSFelix Fietkau 
1921ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1922ba8c3d6fSFelix Fietkau 			return;
1923ba8c3d6fSFelix Fietkau 
1924ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1925adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1926adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
19271e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1928ba8c3d6fSFelix Fietkau 				continue;
1929ba8c3d6fSFelix Fietkau 
1930ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1931ba8c3d6fSFelix Fietkau 			break;
1932ba8c3d6fSFelix Fietkau 		}
1933af818581SJohannes Berg 	}
1934af818581SJohannes Berg }
1935af818581SJohannes Berg 
1936af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1937af818581SJohannes Berg {
193847086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1939af818581SJohannes Berg 
1940af818581SJohannes Berg 	/*
194147086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
194247086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
194347086fc5SJohannes Berg 	 * only from the non-enabled ones.
1944af818581SJohannes Berg 	 */
194547086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
194647086fc5SJohannes Berg 		ignore_for_response = 0;
1947af818581SJohannes Berg 
194847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
194947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1950af818581SJohannes Berg }
195147086fc5SJohannes Berg 
195247086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
195347086fc5SJohannes Berg {
195447086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
195547086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
195647086fc5SJohannes Berg 
195747086fc5SJohannes Berg 	/*
195847086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
195947086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
196047086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
196147086fc5SJohannes Berg 	 * actually getting called.
196247086fc5SJohannes Berg 	 */
196347086fc5SJohannes Berg 	if (!delivery_enabled)
196447086fc5SJohannes Berg 		return;
196547086fc5SJohannes Berg 
196647086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
196747086fc5SJohannes Berg 	case 1:
196847086fc5SJohannes Berg 		n_frames = 2;
196947086fc5SJohannes Berg 		break;
197047086fc5SJohannes Berg 	case 2:
197147086fc5SJohannes Berg 		n_frames = 4;
197247086fc5SJohannes Berg 		break;
197347086fc5SJohannes Berg 	case 3:
197447086fc5SJohannes Berg 		n_frames = 6;
197547086fc5SJohannes Berg 		break;
197647086fc5SJohannes Berg 	case 0:
197747086fc5SJohannes Berg 		/* XXX: what is a good value? */
197813a8098aSAndrei Otcheretianski 		n_frames = 128;
197947086fc5SJohannes Berg 		break;
198047086fc5SJohannes Berg 	}
198147086fc5SJohannes Berg 
198247086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
198347086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1984af818581SJohannes Berg }
1985af818581SJohannes Berg 
1986af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1987af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1988af818581SJohannes Berg {
1989af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1990af818581SJohannes Berg 
1991b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1992b5878a2dSJohannes Berg 
19935ac2e350SJohannes Berg 	if (block) {
1994c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
199517c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
19965ac2e350SJohannes Berg 		return;
19975ac2e350SJohannes Berg 	}
19985ac2e350SJohannes Berg 
19995ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
20005ac2e350SJohannes Berg 		return;
20015ac2e350SJohannes Berg 
20025ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
20035ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
20045ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20055ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20065ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
20075ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
20085ac2e350SJohannes Berg 		/* must be asleep in this case */
20095ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20105ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20115ac2e350SJohannes Berg 	} else {
20125ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
201317c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
20145ac2e350SJohannes Berg 	}
2015af818581SJohannes Berg }
2016af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
2017dcf55fb5SFelix Fietkau 
2018e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
201937fbd908SJohannes Berg {
202037fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
202137fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
202237fbd908SJohannes Berg 
202337fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
202437fbd908SJohannes Berg 
202537fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
202637fbd908SJohannes Berg }
2027e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
202837fbd908SJohannes Berg 
20290ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
20300ead2510SEmmanuel Grumbach {
20310ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
20320ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
20330ead2510SEmmanuel Grumbach 	bool more_data;
20340ead2510SEmmanuel Grumbach 
20350ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
20360ead2510SEmmanuel Grumbach 
20370ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
20380ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
20390ead2510SEmmanuel Grumbach 					       reason, 0);
20400ead2510SEmmanuel Grumbach 
20410ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
20420ead2510SEmmanuel Grumbach }
20430ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
20440ead2510SEmmanuel Grumbach 
2045042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2046042ec453SJohannes Berg 				u8 tid, bool buffered)
2047dcf55fb5SFelix Fietkau {
2048dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2049dcf55fb5SFelix Fietkau 
20505a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2051042ec453SJohannes Berg 		return;
2052042ec453SJohannes Berg 
20531b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
20541b000789SJohannes Berg 
2055948d887dSJohannes Berg 	if (buffered)
2056948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2057948d887dSJohannes Berg 	else
2058948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2059948d887dSJohannes Berg 
2060c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2061dcf55fb5SFelix Fietkau }
2062042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2063d9a7ddb0SJohannes Berg 
2064b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2065b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2066b4809e94SToke Høiland-Jørgensen {
2067942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2068942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2069942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2070942741daSFelix Fietkau 	u32 airtime = 0;
2071c77bfab9SFelix Fietkau 	u32 diff;
2072b4809e94SToke Høiland-Jørgensen 
2073942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2074942741daSFelix Fietkau 		airtime += tx_airtime;
2075942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2076942741daSFelix Fietkau 		airtime += rx_airtime;
2077b4809e94SToke Høiland-Jørgensen 
2078942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2079942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2080942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2081c77bfab9SFelix Fietkau 
2082c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2083c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2084942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2085c77bfab9SFelix Fietkau 
2086942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2087b4809e94SToke Høiland-Jørgensen }
2088b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2089b4809e94SToke Høiland-Jørgensen 
20903ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
20913ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
20923ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
20933ace10f5SKan Yan {
20943ace10f5SKan Yan 	int tx_pending;
20953ace10f5SKan Yan 
2096911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2097911bde0fSToke Høiland-Jørgensen 		return;
2098911bde0fSToke Høiland-Jørgensen 
20993ace10f5SKan Yan 	if (!tx_completed) {
21003ace10f5SKan Yan 		if (sta)
21013ace10f5SKan Yan 			atomic_add(tx_airtime,
21023ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
21033ace10f5SKan Yan 
21043ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
21058e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
21063ace10f5SKan Yan 		return;
21073ace10f5SKan Yan 	}
21083ace10f5SKan Yan 
21093ace10f5SKan Yan 	if (sta) {
21103ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
21113ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
211204e35caaSFelix Fietkau 		if (tx_pending < 0)
21133ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
21143ace10f5SKan Yan 				       tx_pending, 0);
21153ace10f5SKan Yan 	}
21163ace10f5SKan Yan 
21178e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
21183ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
21198e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
21203ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
21213ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
21223ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
21238e4bac06SFelix Fietkau 		      tx_airtime)) {
21248e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
21253ace10f5SKan Yan 			       tx_pending, 0);
21268e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
21278e4bac06SFelix Fietkau 	}
21283ace10f5SKan Yan }
21293ace10f5SKan Yan 
213083d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2131d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2132d9a7ddb0SJohannes Berg {
21338bf11d8dSJohannes Berg 	might_sleep();
2134d9a7ddb0SJohannes Berg 
2135d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2136d9a7ddb0SJohannes Berg 		return 0;
2137d9a7ddb0SJohannes Berg 
2138f09603a2SJohannes Berg 	/* check allowed transitions first */
2139f09603a2SJohannes Berg 
2140d9a7ddb0SJohannes Berg 	switch (new_state) {
2141d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2142f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2143d9a7ddb0SJohannes Berg 			return -EINVAL;
2144d9a7ddb0SJohannes Berg 		break;
2145d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2146f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2147f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2148d9a7ddb0SJohannes Berg 			return -EINVAL;
2149d9a7ddb0SJohannes Berg 		break;
2150d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2151f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2152f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2153d9a7ddb0SJohannes Berg 			return -EINVAL;
2154d9a7ddb0SJohannes Berg 		break;
2155d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2156f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2157d9a7ddb0SJohannes Berg 			return -EINVAL;
2158d9a7ddb0SJohannes Berg 		break;
2159d9a7ddb0SJohannes Berg 	default:
2160d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2161d9a7ddb0SJohannes Berg 		return -EINVAL;
2162d9a7ddb0SJohannes Berg 	}
2163d9a7ddb0SJohannes Berg 
2164bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2165bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2166f09603a2SJohannes Berg 
2167f09603a2SJohannes Berg 	/*
2168f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2169f09603a2SJohannes Berg 	 * fail the transition
2170f09603a2SJohannes Berg 	 */
2171f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2172f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2173f09603a2SJohannes Berg 					sta->sta_state, new_state);
2174f09603a2SJohannes Berg 		if (err)
2175f09603a2SJohannes Berg 			return err;
2176f09603a2SJohannes Berg 	}
2177f09603a2SJohannes Berg 
2178f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2179f09603a2SJohannes Berg 
2180f09603a2SJohannes Berg 	switch (new_state) {
2181f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2182f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2183f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2184f09603a2SJohannes Berg 		break;
2185f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2186a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2187f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2188a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2189f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
21900cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
219152cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
219252cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2193a7201a6cSIlan Peer 		}
2194f09603a2SJohannes Berg 		break;
2195f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2196f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2197f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
21989cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
21990cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
220052cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
220152cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2202f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
220372f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2204f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
220517c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
220649ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2207f09603a2SJohannes Berg 		}
2208f09603a2SJohannes Berg 		break;
2209f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2210f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
221172f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2212f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
221317c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
221449ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2215f09603a2SJohannes Berg 		}
22163e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
22173e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
22183e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
22193e493173SJouni Malinen 						    sta->sta.addr);
2220f09603a2SJohannes Berg 		break;
2221f09603a2SJohannes Berg 	default:
2222f09603a2SJohannes Berg 		break;
2223f09603a2SJohannes Berg 	}
2224f09603a2SJohannes Berg 
2225d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2226d9a7ddb0SJohannes Berg 
2227d9a7ddb0SJohannes Berg 	return 0;
2228d9a7ddb0SJohannes Berg }
2229687da132SEmmanuel Grumbach 
2230c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2231c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2232c9c5962bSJohannes Berg {
2233046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2234c9c5962bSJohannes Berg 	int cpu;
2235c9c5962bSJohannes Berg 
2236046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2237c9c5962bSJohannes Berg 		return stats;
2238c9c5962bSJohannes Berg 
2239c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2240c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2241c9c5962bSJohannes Berg 
2242046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2243c9c5962bSJohannes Berg 
2244c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2245c9c5962bSJohannes Berg 			stats = cpustats;
2246c9c5962bSJohannes Berg 	}
2247c9c5962bSJohannes Berg 
2248c9c5962bSJohannes Berg 	return stats;
2249c9c5962bSJohannes Berg }
2250c9c5962bSJohannes Berg 
225141cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
22524f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2253fbd6ff5cSJohannes Berg {
2254dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2255fbd6ff5cSJohannes Berg 
2256dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
22577f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
22584f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2259dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2260dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2261dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2262dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22637f406cd1SJohannes Berg 		break;
22647f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
22654f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2266dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2267dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2268dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22697f406cd1SJohannes Berg 		break;
22707f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2271fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2272fbd6ff5cSJohannes Berg 		u16 brate;
22734f6b1b3dSJohannes Berg 		unsigned int shift;
2274dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2275dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2276fbd6ff5cSJohannes Berg 
2277dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
22788b783d10SThomas Pedersen 
22798b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
22808b783d10SThomas Pedersen 			break;
22818b783d10SThomas Pedersen 
2282dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
22834f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
22844f6b1b3dSJohannes Berg 			shift = 2;
22854f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
22864f6b1b3dSJohannes Berg 			shift = 1;
22874f6b1b3dSJohannes Berg 		else
22884f6b1b3dSJohannes Berg 			shift = 0;
2289fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
22907f406cd1SJohannes Berg 		break;
22917f406cd1SJohannes Berg 		}
229241cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
229341cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
229441cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
229541cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
229641cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
229741cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
229841cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
229941cbb0f5SLuca Coelho 		break;
2300fbd6ff5cSJohannes Berg 	}
23014f6b1b3dSJohannes Berg }
2302fbd6ff5cSJohannes Berg 
2303a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
23044f6b1b3dSJohannes Berg {
23056aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
23064f6b1b3dSJohannes Berg 
23074f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2308a17d93ffSBen Greear 		return -EINVAL;
2309a17d93ffSBen Greear 
23104f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2311a17d93ffSBen Greear 	return 0;
2312fbd6ff5cSJohannes Berg }
2313fbd6ff5cSJohannes Berg 
2314b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2315b255b72bSSeevalamuthu Mariappan 					int tid)
2316b255b72bSSeevalamuthu Mariappan {
2317b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2318b255b72bSSeevalamuthu Mariappan 	u64 value;
2319b255b72bSSeevalamuthu Mariappan 
2320b255b72bSSeevalamuthu Mariappan 	do {
2321278d3ba6SSebastian Andrzej Siewior 		start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2322b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2323278d3ba6SSebastian Andrzej Siewior 	} while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2324b255b72bSSeevalamuthu Mariappan 
2325b255b72bSSeevalamuthu Mariappan 	return value;
2326b255b72bSSeevalamuthu Mariappan }
2327b255b72bSSeevalamuthu Mariappan 
23280f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
23290f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
23300f9c5a61SJohannes Berg 			     int tid)
23310f9c5a61SJohannes Berg {
23320f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2333b255b72bSSeevalamuthu Mariappan 	int cpu;
23340f9c5a61SJohannes Berg 
23350f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2336046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2337046d2e7cSSriram R 							   tid);
23380f9c5a61SJohannes Berg 
2339046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2340b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2341b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2342b255b72bSSeevalamuthu Mariappan 
2343046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2344046d2e7cSSriram R 						     cpu);
2345b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2346b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2347b255b72bSSeevalamuthu Mariappan 			}
2348b255b72bSSeevalamuthu Mariappan 		}
23490f9c5a61SJohannes Berg 
23500f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
23510f9c5a61SJohannes Berg 	}
23520f9c5a61SJohannes Berg 
23530f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
23540f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2355046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
23560f9c5a61SJohannes Berg 	}
23570f9c5a61SJohannes Berg 
23580f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
23590f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23600f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2361046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
23620f9c5a61SJohannes Berg 	}
23630f9c5a61SJohannes Berg 
23640f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
23650f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23660f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2367046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
23680f9c5a61SJohannes Berg 	}
23692fe4a29aSToke Høiland-Jørgensen 
23702fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
23712fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
23722fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
23732fe4a29aSToke Høiland-Jørgensen 
23742fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
23752fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
23762fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
23772fe4a29aSToke Høiland-Jørgensen 
23782fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
23792fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
23802fe4a29aSToke Høiland-Jørgensen 	}
23810f9c5a61SJohannes Berg }
23820f9c5a61SJohannes Berg 
2383c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2384c9c5962bSJohannes Berg {
2385c9c5962bSJohannes Berg 	unsigned int start;
2386c9c5962bSJohannes Berg 	u64 value;
2387c9c5962bSJohannes Berg 
2388c9c5962bSJohannes Berg 	do {
2389278d3ba6SSebastian Andrzej Siewior 		start = u64_stats_fetch_begin_irq(&rxstats->syncp);
2390c9c5962bSJohannes Berg 		value = rxstats->bytes;
2391278d3ba6SSebastian Andrzej Siewior 	} while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
2392c9c5962bSJohannes Berg 
2393c9c5962bSJohannes Berg 	return value;
2394c9c5962bSJohannes Berg }
2395c9c5962bSJohannes Berg 
23960fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
23970fdf1493SJohannes Berg 		   bool tidstats)
2398b7ffbd7eSJohannes Berg {
2399b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2400b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2401b7ffbd7eSJohannes Berg 	u32 thr = 0;
2402c9c5962bSJohannes Berg 	int i, ac, cpu;
2403c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2404c9c5962bSJohannes Berg 
2405c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2406b7ffbd7eSJohannes Berg 
2407b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2408b7ffbd7eSJohannes Berg 
2409225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2410225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2411225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2412225b8189SJohannes Berg 	 */
2413225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2414bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2415225b8189SJohannes Berg 
24162b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2417a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2418a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2419a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2420a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
24219cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2422a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2423976bd9efSJohannes Berg 
2424976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2425bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2426bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2427a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2428976bd9efSJohannes Berg 	}
2429b7ffbd7eSJohannes Berg 
243084b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
24319cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2432e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2433b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
24342b9a7e1bSJohannes Berg 
2435a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2436a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2437b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
24382b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2439046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2440a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2441b7ffbd7eSJohannes Berg 	}
24422b9a7e1bSJohannes Berg 
2443a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
24442b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
24452b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2446046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2447a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
24482b9a7e1bSJohannes Berg 	}
24492b9a7e1bSJohannes Berg 
2450a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2451a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2452046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
24530f9c5a61SJohannes Berg 
2454046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2455c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2456c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2457c9c5962bSJohannes Berg 
2458046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2459046d2e7cSSriram R 						     cpu);
2460c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2461c9c5962bSJohannes Berg 			}
2462c9c5962bSJohannes Berg 		}
2463c9c5962bSJohannes Berg 
2464a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
24652b9a7e1bSJohannes Berg 	}
24662b9a7e1bSJohannes Berg 
2467a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2468046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2469046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2470c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2471c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2472c9c5962bSJohannes Berg 
2473046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2474046d2e7cSSriram R 						     cpu);
2475c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2476c9c5962bSJohannes Berg 			}
2477c9c5962bSJohannes Berg 		}
2478a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
24792b9a7e1bSJohannes Berg 	}
24802b9a7e1bSJohannes Berg 
2481a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2482046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2483a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
24842b9a7e1bSJohannes Berg 	}
24852b9a7e1bSJohannes Berg 
2486a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2487046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2488a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
24892b9a7e1bSJohannes Berg 	}
24902b9a7e1bSJohannes Berg 
2491b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2492b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2493b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2494b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2495b4809e94SToke Høiland-Jørgensen 	}
2496b4809e94SToke Høiland-Jørgensen 
2497b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2498b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2499b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2500b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2501b4809e94SToke Høiland-Jørgensen 	}
2502b4809e94SToke Høiland-Jørgensen 
2503b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2504942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2505b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2506b4809e94SToke Høiland-Jørgensen 	}
2507b4809e94SToke Høiland-Jørgensen 
2508046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2509046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2510c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2511c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2512c9c5962bSJohannes Berg 
2513046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2514e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2515c9c5962bSJohannes Berg 		}
2516c9c5962bSJohannes Berg 	}
2517b7ffbd7eSJohannes Berg 
2518225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2519225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2520a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2521a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2522225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2523225b8189SJohannes Berg 	}
2524225b8189SJohannes Berg 
252530686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
252630686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2527a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2528c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2529a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2530b7ffbd7eSJohannes Berg 		}
25312b9a7e1bSJohannes Berg 
2532046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2533a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
253440d9a38aSJohannes Berg 			sinfo->signal_avg =
2535046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2536a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
25372b9a7e1bSJohannes Berg 		}
25382b9a7e1bSJohannes Berg 	}
25392b9a7e1bSJohannes Berg 
2540c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2541c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2542c9c5962bSJohannes Berg 	 * pcpu statistics
2543c9c5962bSJohannes Berg 	 */
2544c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2545a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2546a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2547a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2548046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2549a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2550b7ffbd7eSJohannes Berg 
2551c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2552c9c5962bSJohannes Berg 
2553b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2554e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2555c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2556b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2557046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2558b7ffbd7eSJohannes Berg 		}
2559b7ffbd7eSJohannes Berg 	}
2560b7ffbd7eSJohannes Berg 
25618a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
25628a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2563046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2564e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2565a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
25662b9a7e1bSJohannes Berg 	}
25672b9a7e1bSJohannes Berg 
25688a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
25698a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2570a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2571a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
25722b9a7e1bSJohannes Berg 	}
2573b7ffbd7eSJohannes Berg 
25740fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
25756af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
25766af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
25778689c051SArend van Spriel 	}
257879c892b8SJohannes Berg 
2579b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2580b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2581a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2582a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2583a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2584a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2585a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2586dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
25871303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
25881303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2589b7ffbd7eSJohannes Berg 
2590433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2591433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2592433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2593b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2594a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2595433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2596b7ffbd7eSJohannes Berg 		}
2597433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2598433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2599433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2600dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
26011303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2602b7ffbd7eSJohannes Berg #endif
2603b7ffbd7eSJohannes Berg 	}
2604b7ffbd7eSJohannes Berg 
2605b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2606b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2607b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2608b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2609b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2610b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2611b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2612785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2613b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2614b7ffbd7eSJohannes Berg 
2615b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2616b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2617b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2618b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2619b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2620b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2621b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2622b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2623b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2624b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2625b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2626b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2627a74a8c84SJohannes Berg 	if (sta->sta.wme)
2628b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2629b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2630b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2631b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2632b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2633b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2634b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2635b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2636b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2637b7ffbd7eSJohannes Berg 
26383b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
26393b17fbf8SMaxim Altshul 
26403b17fbf8SMaxim Altshul 	if (thr != 0) {
2641a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
26423b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
26433b17fbf8SMaxim Altshul 	}
2644a78b26ffSVenkateswara Naralasetty 
2645a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2646046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2647046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2648a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2649a78b26ffSVenkateswara Naralasetty 	}
2650cc60dbbfSBalaji Pothunoori 
26519c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2652046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2653cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2654cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2655046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2656cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
26579c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2658cc60dbbfSBalaji Pothunoori 	}
2659ab60633cSNarayanraddi Masti 
2660ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2661ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2662ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2663ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2664ab60633cSNarayanraddi Masti 	}
26653b17fbf8SMaxim Altshul }
26663b17fbf8SMaxim Altshul 
26673b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
26683b17fbf8SMaxim Altshul {
26693b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
26703b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
26713b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
26723b17fbf8SMaxim Altshul 	u32 thr = 0;
26733b17fbf8SMaxim Altshul 
26743b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
26753b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
26763b17fbf8SMaxim Altshul 
2677b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2678b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2679b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2680b7ffbd7eSJohannes Berg 	else
26814fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2682b7ffbd7eSJohannes Berg 
26833b17fbf8SMaxim Altshul 	return thr;
2684b7ffbd7eSJohannes Berg }
2685b8da6b6aSJohannes Berg 
2686b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2687b8da6b6aSJohannes Berg {
2688c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2689c9c5962bSJohannes Berg 
2690046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2691046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2692c9c5962bSJohannes Berg 		return stats->last_rx;
2693046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2694b8da6b6aSJohannes Berg }
2695484a54c2SToke Høiland-Jørgensen 
2696484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2697484a54c2SToke Høiland-Jørgensen {
2698484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2699484a54c2SToke Høiland-Jørgensen 		return;
2700484a54c2SToke Høiland-Jørgensen 
2701484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2702484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2703484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2704484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2705484a54c2SToke Høiland-Jørgensen 	} else {
2706484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2707484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2708484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2709484a54c2SToke Høiland-Jørgensen 	}
2710484a54c2SToke Høiland-Jørgensen }
2711484a54c2SToke Høiland-Jørgensen 
2712484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2713484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2714484a54c2SToke Høiland-Jørgensen {
2715484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2716484a54c2SToke Høiland-Jørgensen 
2717484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2718484a54c2SToke Høiland-Jørgensen }
2719cb71f1d1SJohannes Berg 
2720cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2721cb71f1d1SJohannes Berg {
2722cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2723cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2724cb71f1d1SJohannes Berg 	int ret;
2725cb71f1d1SJohannes Berg 
2726cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2727cb71f1d1SJohannes Berg 
2728cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2729cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2730cb71f1d1SJohannes Berg 		return -EINVAL;
2731cb71f1d1SJohannes Berg 
2732cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2733cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2734cb71f1d1SJohannes Berg 		return -EBUSY;
2735cb71f1d1SJohannes Berg 
2736cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2737cb71f1d1SJohannes Berg 	if (!alloc)
2738cb71f1d1SJohannes Berg 		return -ENOMEM;
2739cb71f1d1SJohannes Berg 
2740cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2741cb71f1d1SJohannes Berg 	if (ret) {
2742cb71f1d1SJohannes Berg 		kfree(alloc);
2743cb71f1d1SJohannes Berg 		return ret;
2744cb71f1d1SJohannes Berg 	}
2745cb71f1d1SJohannes Berg 
2746cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2747cb71f1d1SJohannes Berg 
2748cb71f1d1SJohannes Berg 	return 0;
2749cb71f1d1SJohannes Berg }
2750cb71f1d1SJohannes Berg 
275121476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
275221476ad1SShaul Triebitz {
275321476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
275421476ad1SShaul Triebitz 
275521476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
275621476ad1SShaul Triebitz }
275721476ad1SShaul Triebitz 
2758cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2759cb71f1d1SJohannes Berg {
2760cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2761c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2762cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2763cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2764cb71f1d1SJohannes Berg 	int ret;
2765cb71f1d1SJohannes Berg 
2766c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2767c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2768cb71f1d1SJohannes Berg 
2769c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2770cb71f1d1SJohannes Berg 		return -EINVAL;
2771cb71f1d1SJohannes Berg 
2772177577dbSJohannes Berg 	rcu_read_lock();
2773177577dbSJohannes Berg 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2774177577dbSJohannes Berg 		rcu_read_unlock();
2775177577dbSJohannes Berg 		return -EALREADY;
2776177577dbSJohannes Berg 	}
2777177577dbSJohannes Berg 	/* we only modify under the mutex so this is fine */
2778177577dbSJohannes Berg 	rcu_read_unlock();
2779177577dbSJohannes Berg 
2780cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2781cb71f1d1SJohannes Berg 
278280e2b1faSLukas Bulwahn 	if (!test_sta_flag(sta, WLAN_STA_INSERTED))
2783ba6ddab9SJohannes Berg 		goto hash;
2784cb71f1d1SJohannes Berg 
2785cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2786cb71f1d1SJohannes Berg 				   old_links, new_links);
2787cb71f1d1SJohannes Berg 	if (ret) {
2788cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2789ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2790177577dbSJohannes Berg 		return ret;
2791cb71f1d1SJohannes Berg 	}
2792cb71f1d1SJohannes Berg 
2793ba6ddab9SJohannes Berg hash:
2794177577dbSJohannes Berg 	ret = link_sta_info_hash_add(sdata->local, link_sta);
2795177577dbSJohannes Berg 	WARN_ON(ret);
2796177577dbSJohannes Berg 	return 0;
2797cb71f1d1SJohannes Berg }
2798cb71f1d1SJohannes Berg 
2799cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2800cb71f1d1SJohannes Berg {
2801cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2802a8f62399SShaul Triebitz 	u16 old_links = sta->sta.valid_links;
2803cb71f1d1SJohannes Berg 
2804cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2805cb71f1d1SJohannes Berg 
2806cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2807cb71f1d1SJohannes Berg 
2808cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2809cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2810a8f62399SShaul Triebitz 				     old_links, sta->sta.valid_links);
2811cb71f1d1SJohannes Berg 
2812ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2813cb71f1d1SJohannes Berg }
2814175ad2ecSJohannes Berg 
2815175ad2ecSJohannes Berg void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2816175ad2ecSJohannes Berg 					   const u8 *ext_capab,
2817175ad2ecSJohannes Berg 					   unsigned int ext_capab_len)
2818175ad2ecSJohannes Berg {
2819175ad2ecSJohannes Berg 	u8 val;
2820175ad2ecSJohannes Berg 
2821175ad2ecSJohannes Berg 	sta->sta.max_amsdu_subframes = 0;
2822175ad2ecSJohannes Berg 
2823175ad2ecSJohannes Berg 	if (ext_capab_len < 8)
2824175ad2ecSJohannes Berg 		return;
2825175ad2ecSJohannes Berg 
2826175ad2ecSJohannes Berg 	/* The sender might not have sent the last bit, consider it to be 0 */
2827175ad2ecSJohannes Berg 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2828175ad2ecSJohannes Berg 
2829175ad2ecSJohannes Berg 	/* we did get all the bits, take the MSB as well */
2830175ad2ecSJohannes Berg 	if (ext_capab_len >= 9)
2831175ad2ecSJohannes Berg 		val |= u8_get_bits(ext_capab[8],
2832175ad2ecSJohannes Berg 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2833175ad2ecSJohannes Berg 
2834175ad2ecSJohannes Berg 	if (val)
2835175ad2ecSJohannes Berg 		sta->sta.max_amsdu_subframes = 4 << val;
2836175ad2ecSJohannes Berg }
2837