xref: /openbmc/linux/net/mac80211/sta_info.c (revision c73993b865bf0b2ea1cbb3e9616f18a6508fc49c)
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;
475*c73993b8SJohannes 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);
478cb71f1d1SJohannes Berg }
479cb71f1d1SJohannes Berg 
480f36fe0a2SJohannes Berg static struct sta_info *
481f36fe0a2SJohannes Berg __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
482f36fe0a2SJohannes Berg 		 const u8 *addr, int link_id, const u8 *link_addr,
483f36fe0a2SJohannes Berg 		 gfp_t gfp)
484f0706e82SJiri Benc {
485d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
486ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
487f0706e82SJiri Benc 	struct sta_info *sta;
48816c5f15cSRon Rindjunsky 	int i;
489f0706e82SJiri Benc 
490ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
491f0706e82SJiri Benc 	if (!sta)
49273651ee6SJohannes Berg 		return NULL;
493f0706e82SJiri Benc 
494246b39e4SJohannes Berg 	sta->local = local;
495246b39e4SJohannes Berg 	sta->sdata = sdata;
496246b39e4SJohannes Berg 
497cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
498246b39e4SJohannes Berg 		return NULL;
499c9c5962bSJohannes Berg 
500cb71f1d1SJohannes Berg 	if (link_id >= 0) {
501cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
502cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
503cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
504cb71f1d1SJohannes Berg 	} else {
505cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
506cb71f1d1SJohannes Berg 	}
507cb71f1d1SJohannes Berg 
50807346f81SJohannes Berg 	spin_lock_init(&sta->lock);
5091d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
5105ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
51167c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
512a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
51387f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
514433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
515433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
516433f5bc1SJohannes Berg 		if (!sta->mesh)
517433f5bc1SJohannes Berg 			goto free;
5184c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
519433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
52045d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5214c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5224c02d62fSKees Cook 				    0);
523433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
524433f5bc1SJohannes Berg 	}
52587f59c70SThomas Pedersen #endif
52607346f81SJohannes Berg 
527ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
52817741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
529f36fe0a2SJohannes Berg 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
530f36fe0a2SJohannes Berg 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
531480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
532480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
533480dd46bSMaxim Altshul 
534046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
535046d2e7cSSriram R 
53696fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
53796fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
53896fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
53996fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
54096fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
54196fc6efbSAlexander Wetzel 	 */
54296fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
54396fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
54496fc6efbSAlexander Wetzel 
5450f9c5a61SJohannes Berg 
5463a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5473a11ce08SJohannes Berg 
54871ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
54971ec375cSJohannes Berg 
550b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
551b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
552b6da911bSLiad Kaufman 
55384b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
554541a45a1SBruno Randolf 
555ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
556ba8c3d6fSFelix Fietkau 		void *txq_data;
557ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
558ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
559ba8c3d6fSFelix Fietkau 
560ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
561ba8c3d6fSFelix Fietkau 		if (!txq_data)
562ba8c3d6fSFelix Fietkau 			goto free;
563ba8c3d6fSFelix Fietkau 
564ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
565ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
566ba8c3d6fSFelix Fietkau 
567adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
568fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
569abfbc3afSJohannes Berg 		}
570ba8c3d6fSFelix Fietkau 	}
571ba8c3d6fSFelix Fietkau 
572ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
573ba8c3d6fSFelix Fietkau 		goto free_txq;
574f0706e82SJiri Benc 
575942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
576b4809e94SToke Høiland-Jørgensen 
577948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
578948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
579948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
580942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
581942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
582942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
583942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
584948d887dSJohannes Berg 	}
58573651ee6SJohannes Berg 
5865a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
5874be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
588cccaec98SSenthil Balasubramanian 
589bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
590bd718fc1SJohannes Berg 		u32 mandatory = 0;
591bd718fc1SJohannes Berg 		int r;
592bd718fc1SJohannes Berg 
593bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
594bd718fc1SJohannes Berg 			continue;
595bd718fc1SJohannes Berg 
596bd718fc1SJohannes Berg 		switch (i) {
597bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
59863fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
599bd718fc1SJohannes Berg 			/*
600bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
601bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
602bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
603bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
604bd718fc1SJohannes Berg 			 * possible rate anyway.
605bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
606bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
607bd718fc1SJohannes Berg 			 */
608bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
609bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
610bd718fc1SJohannes Berg 			break;
611bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
612bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
613bd718fc1SJohannes Berg 			break;
614bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
615bd718fc1SJohannes Berg 			WARN_ON(1);
616bd718fc1SJohannes Berg 			mandatory = 0;
617bd718fc1SJohannes Berg 			break;
618bd718fc1SJohannes Berg 		}
619bd718fc1SJohannes Berg 
620bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
621bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
622bd718fc1SJohannes Berg 
623bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
624bd718fc1SJohannes Berg 
625bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
626bd718fc1SJohannes Berg 				continue;
627046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
628bd718fc1SJohannes Berg 		}
629bd718fc1SJohannes Berg 	}
630bd718fc1SJohannes Berg 
631af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
6326e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
6336e0456b5SFelix Fietkau 
634484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
635484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
636484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
637484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
638dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
639dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
640484a54c2SToke Høiland-Jørgensen 
641bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
642ef04a297SJohannes Berg 
643abfbc3afSJohannes Berg 	return sta;
644ba8c3d6fSFelix Fietkau 
645ba8c3d6fSFelix Fietkau free_txq:
646ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
647ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
648ba8c3d6fSFelix Fietkau free:
649cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
650433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
651433f5bc1SJohannes Berg 	kfree(sta->mesh);
652433f5bc1SJohannes Berg #endif
653ba8c3d6fSFelix Fietkau 	kfree(sta);
654ba8c3d6fSFelix Fietkau 	return NULL;
65573651ee6SJohannes Berg }
65673651ee6SJohannes Berg 
657f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
658f36fe0a2SJohannes Berg 				const u8 *addr, gfp_t gfp)
659f36fe0a2SJohannes Berg {
660f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
661f36fe0a2SJohannes Berg }
662f36fe0a2SJohannes Berg 
663f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
664f36fe0a2SJohannes Berg 					  const u8 *mld_addr,
665f36fe0a2SJohannes Berg 					  unsigned int link_id,
666f36fe0a2SJohannes Berg 					  const u8 *link_addr,
667f36fe0a2SJohannes Berg 					  gfp_t gfp)
668f36fe0a2SJohannes Berg {
669f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
670f36fe0a2SJohannes Berg }
671f36fe0a2SJohannes Berg 
6728c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
67334e89507SJohannes Berg {
67434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
67534e89507SJohannes Berg 
67603e4497eSJohannes Berg 	/*
67703e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
67803e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
67903e4497eSJohannes Berg 	 * and another CPU turns off the net device.
68003e4497eSJohannes Berg 	 */
6818c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
6828c71df7aSGuy Eilam 		return -ENETDOWN;
68303e4497eSJohannes Berg 
684b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
685deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
6868c71df7aSGuy Eilam 		return -EINVAL;
6878c71df7aSGuy Eilam 
68883e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
68983e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
69083e7e4ceSHerbert Xu 	 * for correctness.
69131104891SJohannes Berg 	 */
69231104891SJohannes Berg 	rcu_read_lock();
69331104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
69431104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
69531104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
69631104891SJohannes Berg 		rcu_read_unlock();
69731104891SJohannes Berg 		return -ENOTUNIQ;
69831104891SJohannes Berg 	}
69931104891SJohannes Berg 	rcu_read_unlock();
70031104891SJohannes Berg 
7018c71df7aSGuy Eilam 	return 0;
70293e5deb1SJohannes Berg }
70344213b5eSJohannes Berg 
704f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
705f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
706f09603a2SJohannes Berg 				     struct sta_info *sta)
707f09603a2SJohannes Berg {
708f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
709f09603a2SJohannes Berg 	int err = 0;
710f09603a2SJohannes Berg 
711f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
712f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
713f09603a2SJohannes Berg 		if (err)
714f09603a2SJohannes Berg 			break;
715f09603a2SJohannes Berg 	}
716f09603a2SJohannes Berg 
717f09603a2SJohannes Berg 	if (!err) {
718a4ec45a4SJohannes Berg 		/*
719a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
720a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
721a4ec45a4SJohannes Berg 		 */
722a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
723f09603a2SJohannes Berg 			sta->uploaded = true;
724f09603a2SJohannes Berg 		return 0;
725f09603a2SJohannes Berg 	}
726f09603a2SJohannes Berg 
727f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
728bdcbd8e0SJohannes Berg 		sdata_info(sdata,
729bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
730bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
731f09603a2SJohannes Berg 		err = 0;
732f09603a2SJohannes Berg 	}
733f09603a2SJohannes Berg 
734f09603a2SJohannes Berg 	/* unwind on error */
735f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
736f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
737f09603a2SJohannes Berg 
738f09603a2SJohannes Berg 	return err;
739f09603a2SJohannes Berg }
740f09603a2SJohannes Berg 
741d405fd8cSGregory Greenman static void
742d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
743d405fd8cSGregory Greenman {
744d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
745d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
746d405fd8cSGregory Greenman 	struct sta_info *sta;
747d405fd8cSGregory Greenman 
748d405fd8cSGregory Greenman 	rcu_read_lock();
749d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
750d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
751d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
752d405fd8cSGregory Greenman 			continue;
753d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
754d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
755d405fd8cSGregory Greenman 			break;
756d405fd8cSGregory Greenman 		}
757d405fd8cSGregory Greenman 	}
758d405fd8cSGregory Greenman 	rcu_read_unlock();
759d405fd8cSGregory Greenman 
760d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
761d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
762d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
763d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
764d405fd8cSGregory Greenman 	}
765d405fd8cSGregory Greenman }
766d405fd8cSGregory Greenman 
76734e89507SJohannes Berg /*
7688c71df7aSGuy Eilam  * should be called with sta_mtx locked
7698c71df7aSGuy Eilam  * this function replaces the mutex lock
7708c71df7aSGuy Eilam  * with a RCU lock
7718c71df7aSGuy Eilam  */
7724d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
7738c71df7aSGuy Eilam {
7748c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
7758c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
7760c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
7778c71df7aSGuy Eilam 	int err = 0;
7788c71df7aSGuy Eilam 
7798c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
7808c71df7aSGuy Eilam 
7817852e361SJohannes Berg 	/* check if STA exists already */
7827852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
7834d33960bSJohannes Berg 		err = -EEXIST;
7848f9dcc29SAhmed Zaki 		goto out_cleanup;
78534e89507SJohannes Berg 	}
78634e89507SJohannes Berg 
7870c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
7880c2e3842SKoen Vandeputte 	if (!sinfo) {
7890c2e3842SKoen Vandeputte 		err = -ENOMEM;
7908f9dcc29SAhmed Zaki 		goto out_cleanup;
7910c2e3842SKoen Vandeputte 	}
7920c2e3842SKoen Vandeputte 
7934d33960bSJohannes Berg 	local->num_sta++;
7944d33960bSJohannes Berg 	local->sta_generation++;
7954d33960bSJohannes Berg 	smp_mb();
7964d33960bSJohannes Berg 
7975108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
7985108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
7995108ca82SJohannes Berg 
8004d33960bSJohannes Berg 	/* make the station visible */
80162b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
80262b14b24SJohannes Berg 	if (err)
80362b14b24SJohannes Berg 		goto out_drop_sta;
8044d33960bSJohannes Berg 
805f36fe0a2SJohannes Berg 	if (sta->sta.valid_links) {
806f36fe0a2SJohannes Berg 		err = link_sta_info_hash_add(local, &sta->deflink);
807f36fe0a2SJohannes Berg 		if (err) {
808f36fe0a2SJohannes Berg 			sta_info_hash_del(local, sta);
809f36fe0a2SJohannes Berg 			goto out_drop_sta;
810f36fe0a2SJohannes Berg 		}
811f36fe0a2SJohannes Berg 	}
812f36fe0a2SJohannes Berg 
8132bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
81483d5cc01SJohannes Berg 
8154dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8164dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8174dde3c36SMordechay Goodstein 	 */
8184dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8190cbf348aSAndrei Otcheretianski 		ieee80211_recalc_min_chandef(sta->sdata, -1);
8204dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8214dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8224dde3c36SMordechay Goodstein 	}
8234dde3c36SMordechay Goodstein 
8245108ca82SJohannes Berg 	/* notify driver */
8255108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8265108ca82SJohannes Berg 	if (err)
8275108ca82SJohannes Berg 		goto out_remove;
8285108ca82SJohannes Berg 
82983d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
830d405fd8cSGregory Greenman 
8315108ca82SJohannes Berg 	/* accept BA sessions now */
8325108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8334d33960bSJohannes Berg 
8344d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8354d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
8364d33960bSJohannes Berg 
8370ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
8380ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
8390ef049dcSArnd Bergmann 	kfree(sinfo);
840d0709a65SJohannes Berg 
841bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
842f0706e82SJiri Benc 
84334e89507SJohannes Berg 	/* move reference to rcu-protected */
84434e89507SJohannes Berg 	rcu_read_lock();
84534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
846e9f207f0SJiri Benc 
84773651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
84873651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
84973651ee6SJohannes Berg 
85073651ee6SJohannes Berg 	return 0;
8515108ca82SJohannes Berg  out_remove:
8520ad49045SJohannes Berg 	if (sta->sta.valid_links)
8530ad49045SJohannes Berg 		link_sta_info_hash_del(local, &sta->deflink);
8545108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
8555108ca82SJohannes Berg 	list_del_rcu(&sta->list);
85662b14b24SJohannes Berg  out_drop_sta:
8575108ca82SJohannes Berg 	local->num_sta--;
8585108ca82SJohannes Berg 	synchronize_net();
8598f9dcc29SAhmed Zaki  out_cleanup:
8607bc40aedSJohannes Berg 	cleanup_single_sta(sta);
8614d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
862ea32f065SSudip Mukherjee 	kfree(sinfo);
8634d33960bSJohannes Berg 	rcu_read_lock();
8644d33960bSJohannes Berg 	return err;
8658c71df7aSGuy Eilam }
8668c71df7aSGuy Eilam 
8678c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
8688c71df7aSGuy Eilam {
8698c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
870308f7fcfSZhao, Gang 	int err;
8718c71df7aSGuy Eilam 
8724d33960bSJohannes Berg 	might_sleep();
8734d33960bSJohannes Berg 
87431104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
87531104891SJohannes Berg 
8768c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
8778c71df7aSGuy Eilam 	if (err) {
8787bc40aedSJohannes Berg 		sta_info_free(local, sta);
87931104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
8808c71df7aSGuy Eilam 		rcu_read_lock();
8817bc40aedSJohannes Berg 		return err;
8828c71df7aSGuy Eilam 	}
8838c71df7aSGuy Eilam 
8847bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
885f0706e82SJiri Benc }
886f0706e82SJiri Benc 
88734e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
88834e89507SJohannes Berg {
88934e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
89034e89507SJohannes Berg 
89134e89507SJohannes Berg 	rcu_read_unlock();
89234e89507SJohannes Berg 
89334e89507SJohannes Berg 	return err;
89434e89507SJohannes Berg }
89534e89507SJohannes Berg 
896d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
897004c872eSJohannes Berg {
898004c872eSJohannes Berg 	/*
899004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
900004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
901004c872eSJohannes Berg 	 */
902d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
903004c872eSJohannes Berg }
904004c872eSJohannes Berg 
905d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
906004c872eSJohannes Berg {
907004c872eSJohannes Berg 	/*
908004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
909004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
910004c872eSJohannes Berg 	 */
911d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
912004c872eSJohannes Berg }
913004c872eSJohannes Berg 
9143d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9153d5839b6SIlan Peer {
9163d5839b6SIlan Peer 	/*
9173d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9183d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9193d5839b6SIlan Peer 	 */
9203d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9213d5839b6SIlan Peer }
9223d5839b6SIlan Peer 
923948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
924004c872eSJohannes Berg {
925948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
926948d887dSJohannes Berg 	switch (ac) {
927948d887dSJohannes Berg 	case IEEE80211_AC_VO:
928948d887dSJohannes Berg 		return BIT(6) | BIT(7);
929948d887dSJohannes Berg 	case IEEE80211_AC_VI:
930948d887dSJohannes Berg 		return BIT(4) | BIT(5);
931948d887dSJohannes Berg 	case IEEE80211_AC_BE:
932948d887dSJohannes Berg 		return BIT(0) | BIT(3);
933948d887dSJohannes Berg 	case IEEE80211_AC_BK:
934948d887dSJohannes Berg 		return BIT(1) | BIT(2);
935948d887dSJohannes Berg 	default:
936948d887dSJohannes Berg 		WARN_ON(1);
937948d887dSJohannes Berg 		return 0;
938d0709a65SJohannes Berg 	}
939004c872eSJohannes Berg }
940004c872eSJohannes Berg 
9419b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
942004c872eSJohannes Berg {
943c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
944d012a605SMarco Porsch 	struct ps_data *ps;
945948d887dSJohannes Berg 	bool indicate_tim = false;
946948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
947948d887dSJohannes Berg 	int ac;
948a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
949004c872eSJohannes Berg 
950d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
951d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
952c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
953c868cb35SJohannes Berg 			return;
9543e122be0SJohannes Berg 
955d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
9563f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
9573f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
9583f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
9593f52b7e3SMarco Porsch #endif
960d012a605SMarco Porsch 	} else {
961d012a605SMarco Porsch 		return;
962d012a605SMarco Porsch 	}
963d012a605SMarco Porsch 
964c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
965d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
966c868cb35SJohannes Berg 		return;
967004c872eSJohannes Berg 
968c868cb35SJohannes Berg 	if (sta->dead)
969c868cb35SJohannes Berg 		goto done;
9703e122be0SJohannes Berg 
971948d887dSJohannes Berg 	/*
972948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
973948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
974948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
975948d887dSJohannes Berg 	 * non-enabled ones.
976948d887dSJohannes Berg 	 */
977948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
978948d887dSJohannes Berg 		ignore_for_tim = 0;
979948d887dSJohannes Berg 
9809b7a86f3SJohannes Berg 	if (ignore_pending)
9819b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
9829b7a86f3SJohannes Berg 
983948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
984948d887dSJohannes Berg 		unsigned long tids;
985948d887dSJohannes Berg 
986f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
987948d887dSJohannes Berg 			continue;
988948d887dSJohannes Berg 
989948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
990948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
991948d887dSJohannes Berg 		if (indicate_tim)
992948d887dSJohannes Berg 			break;
993948d887dSJohannes Berg 
994948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
995948d887dSJohannes Berg 
996948d887dSJohannes Berg 		indicate_tim |=
997948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
998ba8c3d6fSFelix Fietkau 		indicate_tim |=
999ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
1000004c872eSJohannes Berg 	}
1001004c872eSJohannes Berg 
1002c868cb35SJohannes Berg  done:
100365f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
1004004c872eSJohannes Berg 
10053d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
10063d5839b6SIlan Peer 		goto out_unlock;
10073d5839b6SIlan Peer 
1008948d887dSJohannes Berg 	if (indicate_tim)
1009d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1010c868cb35SJohannes Berg 	else
1011d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10123e122be0SJohannes Berg 
10139b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1014c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1015948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1016c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1017004c872eSJohannes Berg 	}
1018004c872eSJohannes Berg 
10193d5839b6SIlan Peer out_unlock:
102065f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1021004c872eSJohannes Berg }
1022004c872eSJohannes Berg 
10239b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10249b7a86f3SJohannes Berg {
10259b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10269b7a86f3SJohannes Berg }
10279b7a86f3SJohannes Berg 
1028cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1029f0706e82SJiri Benc {
1030e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1031f0706e82SJiri Benc 	int timeout;
1032f0706e82SJiri Benc 
1033f0706e82SJiri Benc 	if (!skb)
1034cd0b8d89SJohannes Berg 		return false;
1035f0706e82SJiri Benc 
1036e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1037f0706e82SJiri Benc 
1038f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
103957c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
104057c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
104157c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1042f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1043f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1044e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1045f0706e82SJiri Benc }
1046f0706e82SJiri Benc 
1047f0706e82SJiri Benc 
1048948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1049948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1050f0706e82SJiri Benc {
1051f0706e82SJiri Benc 	unsigned long flags;
1052f0706e82SJiri Benc 	struct sk_buff *skb;
1053f0706e82SJiri Benc 
105460750397SJohannes Berg 	/*
105560750397SJohannes Berg 	 * First check for frames that should expire on the filtered
105660750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
105760750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
105860750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
105960750397SJohannes Berg 	 * total_ps_buffered counter.
106060750397SJohannes Berg 	 */
1061f0706e82SJiri Benc 	for (;;) {
1062948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1063948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
106457c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1065948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1066836341a7SJohannes Berg 		else
1067f0706e82SJiri Benc 			skb = NULL;
1068948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1069f0706e82SJiri Benc 
107060750397SJohannes Berg 		/*
107160750397SJohannes Berg 		 * Frames are queued in order, so if this one
107260750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
107360750397SJohannes Berg 		 * we actually reached the end of the queue we
107460750397SJohannes Berg 		 * also need to stop, of course.
107560750397SJohannes Berg 		 */
107660750397SJohannes Berg 		if (!skb)
107760750397SJohannes Berg 			break;
1078d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
107960750397SJohannes Berg 	}
108060750397SJohannes Berg 
108160750397SJohannes Berg 	/*
108260750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
108360750397SJohannes Berg 	 * only find something if the filtered queue was emptied
108460750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
108560750397SJohannes Berg 	 * buffered frames.
108660750397SJohannes Berg 	 */
1087f0706e82SJiri Benc 	for (;;) {
1088948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1089948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1090f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1091948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1092f0706e82SJiri Benc 		else
1093f0706e82SJiri Benc 			skb = NULL;
1094948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1095f0706e82SJiri Benc 
109660750397SJohannes Berg 		/*
109760750397SJohannes Berg 		 * frames are queued in order, so if this one
109860750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
109960750397SJohannes Berg 		 * the queue) we can stop testing
110060750397SJohannes Berg 		 */
1101836341a7SJohannes Berg 		if (!skb)
1102836341a7SJohannes Berg 			break;
1103836341a7SJohannes Berg 
1104f0706e82SJiri Benc 		local->total_ps_buffered--;
1105bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1106bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1107d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1108f0706e82SJiri Benc 	}
11093393a608SJuuso Oikarinen 
111060750397SJohannes Berg 	/*
111160750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
111260750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
111360750397SJohannes Berg 	 * frames.
111460750397SJohannes Berg 	 */
111560750397SJohannes Berg 	sta_info_recalc_tim(sta);
111660750397SJohannes Berg 
111760750397SJohannes Berg 	/*
111860750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
111960750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
112060750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
112160750397SJohannes Berg 	 */
1122948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1123948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1124948d887dSJohannes Berg }
1125948d887dSJohannes Berg 
1126948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1127948d887dSJohannes Berg 					     struct sta_info *sta)
1128948d887dSJohannes Berg {
1129948d887dSJohannes Berg 	bool have_buffered = false;
1130948d887dSJohannes Berg 	int ac;
1131948d887dSJohannes Berg 
11323f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11333f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11343f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1135948d887dSJohannes Berg 		return false;
1136948d887dSJohannes Berg 
1137948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1138948d887dSJohannes Berg 		have_buffered |=
1139948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1140948d887dSJohannes Berg 
1141948d887dSJohannes Berg 	return have_buffered;
1142f0706e82SJiri Benc }
1143f0706e82SJiri Benc 
1144d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
114534e89507SJohannes Berg {
114634e89507SJohannes Berg 	struct ieee80211_local *local;
114734e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
11480ad49045SJohannes Berg 	int ret, i;
114934e89507SJohannes Berg 
115034e89507SJohannes Berg 	might_sleep();
115134e89507SJohannes Berg 
115234e89507SJohannes Berg 	if (!sta)
115334e89507SJohannes Berg 		return -ENOENT;
115434e89507SJohannes Berg 
115534e89507SJohannes Berg 	local = sta->local;
115634e89507SJohannes Berg 	sdata = sta->sdata;
115734e89507SJohannes Berg 
115883d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
115983d5cc01SJohannes Berg 
1160098a6070SJohannes Berg 	/*
1161098a6070SJohannes Berg 	 * Before removing the station from the driver and
1162098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1163098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1164098a6070SJohannes Berg 	 * will be sufficient.
1165098a6070SJohannes Berg 	 */
1166c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1167c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1168098a6070SJohannes Berg 
1169f59374ebSSara Sharon 	/*
1170f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1171f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1172f59374ebSSara Sharon 	 * all such frames to be processed.
1173f59374ebSSara Sharon 	 */
1174f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1175f59374ebSSara Sharon 
11760ad49045SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
11770ad49045SJohannes Berg 		struct link_sta_info *link_sta;
11780ad49045SJohannes Berg 
11790ad49045SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
11800ad49045SJohannes Berg 			continue;
11810ad49045SJohannes Berg 
11820ad49045SJohannes Berg 		link_sta = rcu_dereference_protected(sta->link[i],
11830ad49045SJohannes Berg 						     lockdep_is_held(&local->sta_mtx));
11840ad49045SJohannes Berg 
11850ad49045SJohannes Berg 		link_sta_info_hash_del(local, link_sta);
11860ad49045SJohannes Berg 	}
11870ad49045SJohannes Berg 
118834e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1189b01711beSJohannes Berg 	if (WARN_ON(ret))
119034e89507SJohannes Berg 		return ret;
119134e89507SJohannes Berg 
1192a7a6bdd0SArik Nemtsov 	/*
1193a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1194a7a6bdd0SArik Nemtsov 	 * removal.
1195a7a6bdd0SArik Nemtsov 	 */
1196a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1197a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1198a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1199a7a6bdd0SArik Nemtsov 	}
1200a7a6bdd0SArik Nemtsov 
1201794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1202ef044763SEliad Peller 	sta->removed = true;
12034d33960bSJohannes Berg 
12046a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
12056a9d1b91SJohannes Berg 
1206a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1207a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1208a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1209a710c816SJohannes Berg 
1210d778207bSJohannes Berg 	return 0;
1211d778207bSJohannes Berg }
1212d778207bSJohannes Berg 
1213d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1214d778207bSJohannes Berg {
1215d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1216d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
12170ef049dcSArnd Bergmann 	struct station_info *sinfo;
1218d778207bSJohannes Berg 	int ret;
1219d778207bSJohannes Berg 
1220d778207bSJohannes Berg 	/*
1221d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1222d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1223d778207bSJohannes Berg 	 */
1224d778207bSJohannes Berg 
1225d778207bSJohannes Berg 	might_sleep();
1226d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1227d778207bSJohannes Berg 
12285981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1229b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1230b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1231b16798f5SJohannes Berg 	}
1232b16798f5SJohannes Berg 
1233c8782078SJohannes Berg 	/* now keys can no longer be reached */
12346d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
123534e89507SJohannes Berg 
12369b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
12379b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
12389b7a86f3SJohannes Berg 
123934e89507SJohannes Berg 	sta->dead = true;
124034e89507SJohannes Berg 
124134e89507SJohannes Berg 	local->num_sta--;
124234e89507SJohannes Berg 	local->sta_generation++;
124334e89507SJohannes Berg 
124483d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1245f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1246f09603a2SJohannes Berg 		if (ret) {
124783d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
124883d5cc01SJohannes Berg 			break;
124983d5cc01SJohannes Berg 		}
125083d5cc01SJohannes Berg 	}
1251d9a7ddb0SJohannes Berg 
1252f09603a2SJohannes Berg 	if (sta->uploaded) {
1253f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1254f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1255f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1256f09603a2SJohannes Berg 	}
125734e89507SJohannes Berg 
1258bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1259bdcbd8e0SJohannes Berg 
12600ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
12610ef049dcSArnd Bergmann 	if (sinfo)
12620fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
12630ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
12640ef049dcSArnd Bergmann 	kfree(sinfo);
1265ec15e68bSJouni Malinen 
126634e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
126734e89507SJohannes Berg 
12683a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
12693a11ce08SJohannes Berg 
1270d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1271d778207bSJohannes Berg }
1272d778207bSJohannes Berg 
1273d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1274d778207bSJohannes Berg {
1275d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1276d778207bSJohannes Berg 
1277d778207bSJohannes Berg 	if (err)
1278d778207bSJohannes Berg 		return err;
1279d778207bSJohannes Berg 
1280d778207bSJohannes Berg 	synchronize_net();
1281d778207bSJohannes Berg 
1282d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
128334e89507SJohannes Berg 
128434e89507SJohannes Berg 	return 0;
128534e89507SJohannes Berg }
128634e89507SJohannes Berg 
128734e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
128834e89507SJohannes Berg {
128934e89507SJohannes Berg 	struct sta_info *sta;
129034e89507SJohannes Berg 	int ret;
129134e89507SJohannes Berg 
129234e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12937852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
129434e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
129534e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
129634e89507SJohannes Berg 
129734e89507SJohannes Berg 	return ret;
129834e89507SJohannes Berg }
129934e89507SJohannes Berg 
130034e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
130134e89507SJohannes Berg 			      const u8 *addr)
130234e89507SJohannes Berg {
130334e89507SJohannes Berg 	struct sta_info *sta;
130434e89507SJohannes Berg 	int ret;
130534e89507SJohannes Berg 
130634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
13077852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
130834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
130934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
131034e89507SJohannes Berg 
131134e89507SJohannes Berg 	return ret;
131234e89507SJohannes Berg }
1313f0706e82SJiri Benc 
131434f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1315f0706e82SJiri Benc {
131634f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1317f0706e82SJiri Benc 	struct sta_info *sta;
13183393a608SJuuso Oikarinen 	bool timer_needed = false;
1319f0706e82SJiri Benc 
1320d0709a65SJohannes Berg 	rcu_read_lock();
1321d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
13223393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
13233393a608SJuuso Oikarinen 			timer_needed = true;
1324d0709a65SJohannes Berg 	rcu_read_unlock();
1325f0706e82SJiri Benc 
13265bb644a0SJohannes Berg 	if (local->quiescing)
13275bb644a0SJohannes Berg 		return;
13285bb644a0SJohannes Berg 
13293393a608SJuuso Oikarinen 	if (!timer_needed)
13303393a608SJuuso Oikarinen 		return;
13313393a608SJuuso Oikarinen 
133226d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
133326d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1334f0706e82SJiri Benc }
1335f0706e82SJiri Benc 
13367bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
13377bedd0cfSJohannes Berg {
13387bedd0cfSJohannes Berg 	int err;
13397bedd0cfSJohannes Berg 
134083e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
13417bedd0cfSJohannes Berg 	if (err)
13427bedd0cfSJohannes Berg 		return err;
13437bedd0cfSJohannes Berg 
1344ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1345ba6ddab9SJohannes Berg 	if (err) {
1346ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1347ba6ddab9SJohannes Berg 		return err;
1348ba6ddab9SJohannes Berg 	}
1349ba6ddab9SJohannes Berg 
13504d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
135134e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1352f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1353f0706e82SJiri Benc 
135434f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
13557bedd0cfSJohannes Berg 	return 0;
1356f0706e82SJiri Benc }
1357f0706e82SJiri Benc 
1358f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1359f0706e82SJiri Benc {
1360a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
136183e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1362ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1363f0706e82SJiri Benc }
1364f0706e82SJiri Benc 
1365051007d9SJohannes Berg 
1366e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1367f0706e82SJiri Benc {
1368b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1369f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1370d778207bSJohannes Berg 	LIST_HEAD(free_list);
137144213b5eSJohannes Berg 	int ret = 0;
1372f0706e82SJiri Benc 
1373d0709a65SJohannes Berg 	might_sleep();
1374d0709a65SJohannes Berg 
1375e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1376e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1377e716251dSJohannes Berg 
137834e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
137934e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1380e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1381e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1382d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1383d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
138434316837SJohannes Berg 			ret++;
138534316837SJohannes Berg 		}
138634e89507SJohannes Berg 	}
1387d778207bSJohannes Berg 
1388d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1389d778207bSJohannes Berg 		synchronize_net();
1390d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1391d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1392d778207bSJohannes Berg 	}
139334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
139444213b5eSJohannes Berg 
1395051007d9SJohannes Berg 	return ret;
1396051007d9SJohannes Berg }
1397051007d9SJohannes Berg 
139824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
139924723d1bSJohannes Berg 			  unsigned long exp_time)
140024723d1bSJohannes Berg {
140124723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
140224723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
140324723d1bSJohannes Berg 
140434e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1405e46a2cf9SMohammed Shafi Shajakhan 
1406e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1407b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1408b8da6b6aSJohannes Berg 
1409ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1410ec2b774eSMarek Lindner 			continue;
1411ec2b774eSMarek Lindner 
1412b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1413eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1414bdcbd8e0SJohannes Berg 				sta->sta.addr);
14153f52b7e3SMarco Porsch 
14163f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
14173f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
14183f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
14193f52b7e3SMarco Porsch 
142034e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
142124723d1bSJohannes Berg 		}
1422e46a2cf9SMohammed Shafi Shajakhan 	}
1423e46a2cf9SMohammed Shafi Shajakhan 
142434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
142524723d1bSJohannes Berg }
142617741cdcSJohannes Berg 
1427686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1428686b9cb9SBen Greear 						   const u8 *addr,
1429686b9cb9SBen Greear 						   const u8 *localaddr)
143017741cdcSJohannes Berg {
14317bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
143283e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
14337bedd0cfSJohannes Berg 	struct sta_info *sta;
143417741cdcSJohannes Berg 
1435686b9cb9SBen Greear 	/*
1436686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1437686b9cb9SBen Greear 	 * ... first in list.
1438686b9cb9SBen Greear 	 */
143983e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1440686b9cb9SBen Greear 		if (localaddr &&
1441b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1442686b9cb9SBen Greear 			continue;
1443f7c65594SJohannes Berg 		if (!sta->uploaded)
1444f7c65594SJohannes Berg 			return NULL;
144517741cdcSJohannes Berg 		return &sta->sta;
1446f7c65594SJohannes Berg 	}
1447f7c65594SJohannes Berg 
1448abe60632SJohannes Berg 	return NULL;
144917741cdcSJohannes Berg }
1450686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
14515ed176e1SJohannes Berg 
14525ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
14535ed176e1SJohannes Berg 					 const u8 *addr)
14545ed176e1SJohannes Berg {
1455f7c65594SJohannes Berg 	struct sta_info *sta;
14565ed176e1SJohannes Berg 
14575ed176e1SJohannes Berg 	if (!vif)
14585ed176e1SJohannes Berg 		return NULL;
14595ed176e1SJohannes Berg 
1460f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1461f7c65594SJohannes Berg 	if (!sta)
1462f7c65594SJohannes Berg 		return NULL;
14635ed176e1SJohannes Berg 
1464f7c65594SJohannes Berg 	if (!sta->uploaded)
1465f7c65594SJohannes Berg 		return NULL;
1466f7c65594SJohannes Berg 
1467f7c65594SJohannes Berg 	return &sta->sta;
14685ed176e1SJohannes Berg }
146917741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1470af818581SJohannes Berg 
1471e3685e03SJohannes Berg /* powersave support code */
1472e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
147350a9432dSJohannes Berg {
1474608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1475e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1476e3685e03SJohannes Berg 	struct sk_buff_head pending;
1477ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1478e3685e03SJohannes Berg 	unsigned long flags;
1479d012a605SMarco Porsch 	struct ps_data *ps;
1480d012a605SMarco Porsch 
14813918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
14823918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
14833918edb0SFelix Fietkau 				     u.ap);
14843918edb0SFelix Fietkau 
14853918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1486d012a605SMarco Porsch 		ps = &sdata->bss->ps;
14873f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
14883f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1489d012a605SMarco Porsch 	else
1490d012a605SMarco Porsch 		return;
149150a9432dSJohannes Berg 
1492c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
149347086fc5SJohannes Berg 
14945a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1495948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1496ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1497948d887dSJohannes Berg 
149830686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
149912375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1500af818581SJohannes Berg 
1501ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1502adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1503ba8c3d6fSFelix Fietkau 			continue;
1504ba8c3d6fSFelix Fietkau 
150518667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1506ba8c3d6fSFelix Fietkau 	}
1507ba8c3d6fSFelix Fietkau 
1508948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1509af818581SJohannes Berg 
15101d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
15111d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1512af818581SJohannes Berg 	/* Send all buffered frames to the station */
1513948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1514948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1515948d887dSJohannes Berg 
1516987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1517948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1518987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1519948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1520948d887dSJohannes Berg 		filtered += tmp - count;
1521948d887dSJohannes Berg 		count = tmp;
1522948d887dSJohannes Berg 
1523987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1524948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1525987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1526948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1527948d887dSJohannes Berg 		buffered += tmp - count;
1528948d887dSJohannes Berg 	}
1529948d887dSJohannes Berg 
1530e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
15315ac2e350SJohannes Berg 
15325ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
15335ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
15345ac2e350SJohannes Berg 
15355ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
15365ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
15375ac2e350SJohannes Berg 	 */
15385ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
15395ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
15401d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1541948d887dSJohannes Berg 
1542e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1543e3685e03SJohannes Berg 
1544af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1545af818581SJohannes Berg 
1546c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1547c868cb35SJohannes Berg 
1548bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
15492595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1550bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
155117c18bf8SJohannes Berg 
155217c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1553af818581SJohannes Berg }
1554af818581SJohannes Berg 
15550ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1556b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
15570ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1558ce662b44SJohannes Berg {
15590ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1560ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1561ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1562ce662b44SJohannes Berg 	struct sk_buff *skb;
1563ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1564ce662b44SJohannes Berg 	__le16 fc;
1565a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1566ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
156755de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1568ce662b44SJohannes Berg 
1569ce662b44SJohannes Berg 	if (qos) {
1570ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1571ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1572ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1573ce662b44SJohannes Berg 	} else {
1574ce662b44SJohannes Berg 		size -= 2;
1575ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1576ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1577ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1578ce662b44SJohannes Berg 	}
1579ce662b44SJohannes Berg 
1580ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1581ce662b44SJohannes Berg 	if (!skb)
1582ce662b44SJohannes Berg 		return;
1583ce662b44SJohannes Berg 
1584ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1585ce662b44SJohannes Berg 
15864df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1587ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1588ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1589ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1590ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1591ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1592864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1593ce662b44SJohannes Berg 
1594ce662b44SJohannes Berg 	skb->priority = tid;
1595ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
159659b66255SJohannes Berg 	if (qos) {
1597ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1598ce662b44SJohannes Berg 
15990ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1600ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1601ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
16020ead2510SEmmanuel Grumbach 			if (more_data)
16030ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
16040ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
16050ead2510SEmmanuel Grumbach 		}
1606ce662b44SJohannes Berg 	}
1607ce662b44SJohannes Berg 
1608ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1609ce662b44SJohannes Berg 
1610ce662b44SJohannes Berg 	/*
1611ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1612ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1613deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1614deeaee19SJohannes Berg 	 * ends the poll/service period.
1615ce662b44SJohannes Berg 	 */
161602f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1617deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1618ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1619ce662b44SJohannes Berg 
16206b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
16216b127c71SSujith Manoharan 
1622b77cf4f8SJohannes Berg 	if (call_driver)
1623b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1624b77cf4f8SJohannes Berg 					  reason, false);
162540b96408SJohannes Berg 
162689afe614SJohannes Berg 	skb->dev = sdata->dev;
162789afe614SJohannes Berg 
162855de908aSJohannes Berg 	rcu_read_lock();
1629d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
163055de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
163155de908aSJohannes Berg 		rcu_read_unlock();
163255de908aSJohannes Berg 		kfree_skb(skb);
163355de908aSJohannes Berg 		return;
163455de908aSJohannes Berg 	}
163555de908aSJohannes Berg 
163673c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
163708aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
163855de908aSJohannes Berg 	rcu_read_unlock();
1639ce662b44SJohannes Berg }
1640ce662b44SJohannes Berg 
16410a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
16420a1cb809SJohannes Berg {
16430a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
16440a1cb809SJohannes Berg 	if (tids & 0xF8)
16450a1cb809SJohannes Berg 		return fls(tids) - 1;
16460a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
16470a1cb809SJohannes Berg 	if (tids & BIT(0))
16480a1cb809SJohannes Berg 		return 0;
16490a1cb809SJohannes Berg 	return fls(tids) - 1;
16500a1cb809SJohannes Berg }
16510a1cb809SJohannes Berg 
16520ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
16530ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
16540ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
16550ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
16560ead2510SEmmanuel Grumbach  */
16570ead2510SEmmanuel Grumbach static bool
16580ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
16590ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
16600ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
16610ead2510SEmmanuel Grumbach {
16620ead2510SEmmanuel Grumbach 	int ac;
16630ead2510SEmmanuel Grumbach 
16640ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
16650ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
16660ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
16670ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
16680ead2510SEmmanuel Grumbach 	 */
16690ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
16700ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
16710ead2510SEmmanuel Grumbach 		return true;
16720ead2510SEmmanuel Grumbach 
16730ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1674f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
16750ead2510SEmmanuel Grumbach 			continue;
16760ead2510SEmmanuel Grumbach 
16770ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
16780ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
16790ead2510SEmmanuel Grumbach 			return true;
16800ead2510SEmmanuel Grumbach 	}
16810ead2510SEmmanuel Grumbach 
16820ead2510SEmmanuel Grumbach 	return false;
16830ead2510SEmmanuel Grumbach }
16840ead2510SEmmanuel Grumbach 
168547086fc5SJohannes Berg static void
16860ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
16870ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
16880ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
16890ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1690af818581SJohannes Berg {
1691af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1692af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1693948d887dSJohannes Berg 	int ac;
1694af818581SJohannes Berg 
1695f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1696948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
16974049e09aSJohannes Berg 		unsigned long tids;
16984049e09aSJohannes Berg 
1699f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1700948d887dSJohannes Berg 			continue;
1701948d887dSJohannes Berg 
17024049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
17034049e09aSJohannes Berg 
1704f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1705f9f760b4SJohannes Berg 		 * release from hardware queues
1706f9f760b4SJohannes Berg 		 */
17070ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
17080ead2510SEmmanuel Grumbach 			*driver_release_tids |=
17090ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
17100ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1711ba8c3d6fSFelix Fietkau 		}
1712f9f760b4SJohannes Berg 
17130ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
171447086fc5SJohannes Berg 			struct sk_buff *skb;
171547086fc5SJohannes Berg 
171647086fc5SJohannes Berg 			while (n_frames > 0) {
1717948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1718948d887dSJohannes Berg 				if (!skb) {
171947086fc5SJohannes Berg 					skb = skb_dequeue(
172047086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1721af818581SJohannes Berg 					if (skb)
1722af818581SJohannes Berg 						local->total_ps_buffered--;
1723af818581SJohannes Berg 				}
172447086fc5SJohannes Berg 				if (!skb)
172547086fc5SJohannes Berg 					break;
172647086fc5SJohannes Berg 				n_frames--;
17270ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
172847086fc5SJohannes Berg 			}
1729948d887dSJohannes Berg 		}
1730948d887dSJohannes Berg 
17310ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
17320ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
17330ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
17344049e09aSJohannes Berg 		 */
1735948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17360ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1737948d887dSJohannes Berg 			break;
1738948d887dSJohannes Berg 	}
1739948d887dSJohannes Berg }
1740af818581SJohannes Berg 
17410ead2510SEmmanuel Grumbach static void
17420ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
17430ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
17440ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
17450ead2510SEmmanuel Grumbach {
17460ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
17470ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
17480ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
17490ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
17500ead2510SEmmanuel Grumbach 	bool more_data;
17510ead2510SEmmanuel Grumbach 
17520ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
17530ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
17540ead2510SEmmanuel Grumbach 
17550ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
17560ead2510SEmmanuel Grumbach 
17570ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
17580ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
17590ead2510SEmmanuel Grumbach 
17600ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
17610ead2510SEmmanuel Grumbach 
17621a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
17630ead2510SEmmanuel Grumbach 		driver_release_tids =
17640ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
17650ead2510SEmmanuel Grumbach 
1766f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1767f438ceb8SEmmanuel Grumbach 		int tid, ac;
17684049e09aSJohannes Berg 
1769ce662b44SJohannes Berg 		/*
1770ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1771ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1772ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1773ce662b44SJohannes Berg 		 *
1774ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1775ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1776ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1777ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1778ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1779ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1780ce662b44SJohannes Berg 		 *
1781ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1782ce662b44SJohannes Berg 		 */
1783ce662b44SJohannes Berg 
1784ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1785f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1786d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1787d7f84244SEmmanuel Grumbach 				break;
1788f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1789ce662b44SJohannes Berg 
17900ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1791f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
179247086fc5SJohannes Berg 		struct sk_buff_head pending;
179347086fc5SJohannes Berg 		struct sk_buff *skb;
179440b96408SJohannes Berg 		int num = 0;
179540b96408SJohannes Berg 		u16 tids = 0;
1796b77cf4f8SJohannes Berg 		bool need_null = false;
179747086fc5SJohannes Berg 
179847086fc5SJohannes Berg 		skb_queue_head_init(&pending);
179947086fc5SJohannes Berg 
180047086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1801af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
180247086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
180340b96408SJohannes Berg 			u8 *qoshdr = NULL;
180440b96408SJohannes Berg 
180540b96408SJohannes Berg 			num++;
1806af818581SJohannes Berg 
1807af818581SJohannes Berg 			/*
180847086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
180947086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
181047086fc5SJohannes Berg 			 * exchange.
1811af818581SJohannes Berg 			 */
18126b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
18136b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1814af818581SJohannes Berg 
181547086fc5SJohannes Berg 			/*
181647086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
181747086fc5SJohannes Berg 			 * more buffered frames for this STA
181847086fc5SJohannes Berg 			 */
181924b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
182047086fc5SJohannes Berg 				hdr->frame_control |=
182147086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
182224b9c373SJanusz.Dziedzic@tieto.com 			else
182324b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
182424b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1825af818581SJohannes Berg 
182640b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
182740b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
182840b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
182940b96408SJohannes Berg 
1830b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1831b77cf4f8SJohannes Berg 
1832b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1833b77cf4f8SJohannes Berg 
1834b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1835b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1836b77cf4f8SJohannes Berg 				continue;
1837b77cf4f8SJohannes Berg 
1838b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1839b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1840b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1841b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1842b77cf4f8SJohannes Berg 				break;
1843b77cf4f8SJohannes Berg 			}
1844b77cf4f8SJohannes Berg 
1845b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1846b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1847b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1848b77cf4f8SJohannes Berg 			 * and be done.
1849b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1850b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1851b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1852b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1853b77cf4f8SJohannes Berg 			 *
1854b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1855b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1856b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1857b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1858b77cf4f8SJohannes Berg 			 *
1859b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1860b77cf4f8SJohannes Berg 			 */
1861b77cf4f8SJohannes Berg 			if (qoshdr) {
186240b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1863deeaee19SJohannes Berg 
186447086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
186547086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1866b77cf4f8SJohannes Berg 			} else {
1867b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1868b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1869b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1870b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1871b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1872b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1873b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1874b77cf4f8SJohannes Berg 				 */
1875b77cf4f8SJohannes Berg 				hdr->frame_control |=
1876b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1877b77cf4f8SJohannes Berg 				need_null = true;
1878b77cf4f8SJohannes Berg 				num++;
187952a3f20cSMarco Porsch 			}
1880b77cf4f8SJohannes Berg 			break;
188147086fc5SJohannes Berg 		}
188247086fc5SJohannes Berg 
188340b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
188440b96408SJohannes Berg 					  reason, more_data);
188540b96408SJohannes Berg 
188647086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1887af818581SJohannes Berg 
1888b77cf4f8SJohannes Berg 		if (need_null)
1889b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
18900ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
18910ead2510SEmmanuel Grumbach 				reason, false, false);
1892b77cf4f8SJohannes Berg 
1893c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1894af818581SJohannes Berg 	} else {
1895ba8c3d6fSFelix Fietkau 		int tid;
1896ba8c3d6fSFelix Fietkau 
1897af818581SJohannes Berg 		/*
18984049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
18994049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1900f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1901f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1902f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1903f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1904f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1905f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1906af818581SJohannes Berg 		 */
19074049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
190847086fc5SJohannes Berg 					    n_frames, reason, more_data);
19094049e09aSJohannes Berg 
19104049e09aSJohannes Berg 		/*
19114049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
19124049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1913f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
19144049e09aSJohannes Berg 		 * release function.
1915f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1916ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1917ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
19184049e09aSJohannes Berg 		 */
1919ba8c3d6fSFelix Fietkau 
1920ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1921ba8c3d6fSFelix Fietkau 			return;
1922ba8c3d6fSFelix Fietkau 
1923ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1924adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1925adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
19261e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1927ba8c3d6fSFelix Fietkau 				continue;
1928ba8c3d6fSFelix Fietkau 
1929ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1930ba8c3d6fSFelix Fietkau 			break;
1931ba8c3d6fSFelix Fietkau 		}
1932af818581SJohannes Berg 	}
1933af818581SJohannes Berg }
1934af818581SJohannes Berg 
1935af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1936af818581SJohannes Berg {
193747086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1938af818581SJohannes Berg 
1939af818581SJohannes Berg 	/*
194047086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
194147086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
194247086fc5SJohannes Berg 	 * only from the non-enabled ones.
1943af818581SJohannes Berg 	 */
194447086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
194547086fc5SJohannes Berg 		ignore_for_response = 0;
1946af818581SJohannes Berg 
194747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
194847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1949af818581SJohannes Berg }
195047086fc5SJohannes Berg 
195147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
195247086fc5SJohannes Berg {
195347086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
195447086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
195547086fc5SJohannes Berg 
195647086fc5SJohannes Berg 	/*
195747086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
195847086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
195947086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
196047086fc5SJohannes Berg 	 * actually getting called.
196147086fc5SJohannes Berg 	 */
196247086fc5SJohannes Berg 	if (!delivery_enabled)
196347086fc5SJohannes Berg 		return;
196447086fc5SJohannes Berg 
196547086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
196647086fc5SJohannes Berg 	case 1:
196747086fc5SJohannes Berg 		n_frames = 2;
196847086fc5SJohannes Berg 		break;
196947086fc5SJohannes Berg 	case 2:
197047086fc5SJohannes Berg 		n_frames = 4;
197147086fc5SJohannes Berg 		break;
197247086fc5SJohannes Berg 	case 3:
197347086fc5SJohannes Berg 		n_frames = 6;
197447086fc5SJohannes Berg 		break;
197547086fc5SJohannes Berg 	case 0:
197647086fc5SJohannes Berg 		/* XXX: what is a good value? */
197713a8098aSAndrei Otcheretianski 		n_frames = 128;
197847086fc5SJohannes Berg 		break;
197947086fc5SJohannes Berg 	}
198047086fc5SJohannes Berg 
198147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
198247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1983af818581SJohannes Berg }
1984af818581SJohannes Berg 
1985af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1986af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1987af818581SJohannes Berg {
1988af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1989af818581SJohannes Berg 
1990b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1991b5878a2dSJohannes Berg 
19925ac2e350SJohannes Berg 	if (block) {
1993c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
199417c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
19955ac2e350SJohannes Berg 		return;
19965ac2e350SJohannes Berg 	}
19975ac2e350SJohannes Berg 
19985ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
19995ac2e350SJohannes Berg 		return;
20005ac2e350SJohannes Berg 
20015ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
20025ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
20035ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20045ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20055ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
20065ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
20075ac2e350SJohannes Berg 		/* must be asleep in this case */
20085ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20095ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20105ac2e350SJohannes Berg 	} else {
20115ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
201217c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
20135ac2e350SJohannes Berg 	}
2014af818581SJohannes Berg }
2015af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
2016dcf55fb5SFelix Fietkau 
2017e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
201837fbd908SJohannes Berg {
201937fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
202037fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
202137fbd908SJohannes Berg 
202237fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
202337fbd908SJohannes Berg 
202437fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
202537fbd908SJohannes Berg }
2026e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
202737fbd908SJohannes Berg 
20280ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
20290ead2510SEmmanuel Grumbach {
20300ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
20310ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
20320ead2510SEmmanuel Grumbach 	bool more_data;
20330ead2510SEmmanuel Grumbach 
20340ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
20350ead2510SEmmanuel Grumbach 
20360ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
20370ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
20380ead2510SEmmanuel Grumbach 					       reason, 0);
20390ead2510SEmmanuel Grumbach 
20400ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
20410ead2510SEmmanuel Grumbach }
20420ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
20430ead2510SEmmanuel Grumbach 
2044042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2045042ec453SJohannes Berg 				u8 tid, bool buffered)
2046dcf55fb5SFelix Fietkau {
2047dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2048dcf55fb5SFelix Fietkau 
20495a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2050042ec453SJohannes Berg 		return;
2051042ec453SJohannes Berg 
20521b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
20531b000789SJohannes Berg 
2054948d887dSJohannes Berg 	if (buffered)
2055948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2056948d887dSJohannes Berg 	else
2057948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2058948d887dSJohannes Berg 
2059c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2060dcf55fb5SFelix Fietkau }
2061042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2062d9a7ddb0SJohannes Berg 
2063b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2064b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2065b4809e94SToke Høiland-Jørgensen {
2066942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2067942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2068942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2069942741daSFelix Fietkau 	u32 airtime = 0;
2070c77bfab9SFelix Fietkau 	u32 diff;
2071b4809e94SToke Høiland-Jørgensen 
2072942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2073942741daSFelix Fietkau 		airtime += tx_airtime;
2074942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2075942741daSFelix Fietkau 		airtime += rx_airtime;
2076b4809e94SToke Høiland-Jørgensen 
2077942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2078942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2079942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2080c77bfab9SFelix Fietkau 
2081c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2082c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2083942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2084c77bfab9SFelix Fietkau 
2085942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2086b4809e94SToke Høiland-Jørgensen }
2087b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2088b4809e94SToke Høiland-Jørgensen 
20893ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
20903ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
20913ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
20923ace10f5SKan Yan {
20933ace10f5SKan Yan 	int tx_pending;
20943ace10f5SKan Yan 
2095911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2096911bde0fSToke Høiland-Jørgensen 		return;
2097911bde0fSToke Høiland-Jørgensen 
20983ace10f5SKan Yan 	if (!tx_completed) {
20993ace10f5SKan Yan 		if (sta)
21003ace10f5SKan Yan 			atomic_add(tx_airtime,
21013ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
21023ace10f5SKan Yan 
21033ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
21048e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
21053ace10f5SKan Yan 		return;
21063ace10f5SKan Yan 	}
21073ace10f5SKan Yan 
21083ace10f5SKan Yan 	if (sta) {
21093ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
21103ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
211104e35caaSFelix Fietkau 		if (tx_pending < 0)
21123ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
21133ace10f5SKan Yan 				       tx_pending, 0);
21143ace10f5SKan Yan 	}
21153ace10f5SKan Yan 
21168e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
21173ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
21188e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
21193ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
21203ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
21213ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
21228e4bac06SFelix Fietkau 		      tx_airtime)) {
21238e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
21243ace10f5SKan Yan 			       tx_pending, 0);
21258e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
21268e4bac06SFelix Fietkau 	}
21273ace10f5SKan Yan }
21283ace10f5SKan Yan 
212983d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2130d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2131d9a7ddb0SJohannes Berg {
21328bf11d8dSJohannes Berg 	might_sleep();
2133d9a7ddb0SJohannes Berg 
2134d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2135d9a7ddb0SJohannes Berg 		return 0;
2136d9a7ddb0SJohannes Berg 
2137f09603a2SJohannes Berg 	/* check allowed transitions first */
2138f09603a2SJohannes Berg 
2139d9a7ddb0SJohannes Berg 	switch (new_state) {
2140d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2141f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2142d9a7ddb0SJohannes Berg 			return -EINVAL;
2143d9a7ddb0SJohannes Berg 		break;
2144d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2145f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2146f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2147d9a7ddb0SJohannes Berg 			return -EINVAL;
2148d9a7ddb0SJohannes Berg 		break;
2149d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2150f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2151f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2152d9a7ddb0SJohannes Berg 			return -EINVAL;
2153d9a7ddb0SJohannes Berg 		break;
2154d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2155f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2156d9a7ddb0SJohannes Berg 			return -EINVAL;
2157d9a7ddb0SJohannes Berg 		break;
2158d9a7ddb0SJohannes Berg 	default:
2159d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2160d9a7ddb0SJohannes Berg 		return -EINVAL;
2161d9a7ddb0SJohannes Berg 	}
2162d9a7ddb0SJohannes Berg 
2163bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2164bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2165f09603a2SJohannes Berg 
2166f09603a2SJohannes Berg 	/*
2167f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2168f09603a2SJohannes Berg 	 * fail the transition
2169f09603a2SJohannes Berg 	 */
2170f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2171f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2172f09603a2SJohannes Berg 					sta->sta_state, new_state);
2173f09603a2SJohannes Berg 		if (err)
2174f09603a2SJohannes Berg 			return err;
2175f09603a2SJohannes Berg 	}
2176f09603a2SJohannes Berg 
2177f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2178f09603a2SJohannes Berg 
2179f09603a2SJohannes Berg 	switch (new_state) {
2180f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2181f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2182f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2183f09603a2SJohannes Berg 		break;
2184f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2185a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2186f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2187a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2188f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
21890cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
219052cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
219152cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2192a7201a6cSIlan Peer 		}
2193f09603a2SJohannes Berg 		break;
2194f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2195f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2196f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
21979cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
21980cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
219952cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
220052cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2201f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
220272f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2203f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
220417c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
220549ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2206f09603a2SJohannes Berg 		}
2207f09603a2SJohannes Berg 		break;
2208f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2209f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
221072f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2211f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
221217c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
221349ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2214f09603a2SJohannes Berg 		}
22153e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
22163e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
22173e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
22183e493173SJouni Malinen 						    sta->sta.addr);
2219f09603a2SJohannes Berg 		break;
2220f09603a2SJohannes Berg 	default:
2221f09603a2SJohannes Berg 		break;
2222f09603a2SJohannes Berg 	}
2223f09603a2SJohannes Berg 
2224d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2225d9a7ddb0SJohannes Berg 
2226d9a7ddb0SJohannes Berg 	return 0;
2227d9a7ddb0SJohannes Berg }
2228687da132SEmmanuel Grumbach 
2229c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2230c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2231c9c5962bSJohannes Berg {
2232046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2233c9c5962bSJohannes Berg 	int cpu;
2234c9c5962bSJohannes Berg 
2235046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2236c9c5962bSJohannes Berg 		return stats;
2237c9c5962bSJohannes Berg 
2238c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2239c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2240c9c5962bSJohannes Berg 
2241046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2242c9c5962bSJohannes Berg 
2243c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2244c9c5962bSJohannes Berg 			stats = cpustats;
2245c9c5962bSJohannes Berg 	}
2246c9c5962bSJohannes Berg 
2247c9c5962bSJohannes Berg 	return stats;
2248c9c5962bSJohannes Berg }
2249c9c5962bSJohannes Berg 
225041cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
22514f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2252fbd6ff5cSJohannes Berg {
2253dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2254fbd6ff5cSJohannes Berg 
2255dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
22567f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
22574f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2258dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2259dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2260dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2261dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22627f406cd1SJohannes Berg 		break;
22637f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
22644f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2265dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2266dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2267dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22687f406cd1SJohannes Berg 		break;
22697f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2270fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2271fbd6ff5cSJohannes Berg 		u16 brate;
22724f6b1b3dSJohannes Berg 		unsigned int shift;
2273dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2274dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2275fbd6ff5cSJohannes Berg 
2276dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
22778b783d10SThomas Pedersen 
22788b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
22798b783d10SThomas Pedersen 			break;
22808b783d10SThomas Pedersen 
2281dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
22824f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
22834f6b1b3dSJohannes Berg 			shift = 2;
22844f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
22854f6b1b3dSJohannes Berg 			shift = 1;
22864f6b1b3dSJohannes Berg 		else
22874f6b1b3dSJohannes Berg 			shift = 0;
2288fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
22897f406cd1SJohannes Berg 		break;
22907f406cd1SJohannes Berg 		}
229141cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
229241cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
229341cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
229441cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
229541cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
229641cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
229741cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
229841cbb0f5SLuca Coelho 		break;
2299fbd6ff5cSJohannes Berg 	}
23004f6b1b3dSJohannes Berg }
2301fbd6ff5cSJohannes Berg 
2302a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
23034f6b1b3dSJohannes Berg {
23046aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
23054f6b1b3dSJohannes Berg 
23064f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2307a17d93ffSBen Greear 		return -EINVAL;
2308a17d93ffSBen Greear 
23094f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2310a17d93ffSBen Greear 	return 0;
2311fbd6ff5cSJohannes Berg }
2312fbd6ff5cSJohannes Berg 
2313b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2314b255b72bSSeevalamuthu Mariappan 					int tid)
2315b255b72bSSeevalamuthu Mariappan {
2316b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2317b255b72bSSeevalamuthu Mariappan 	u64 value;
2318b255b72bSSeevalamuthu Mariappan 
2319b255b72bSSeevalamuthu Mariappan 	do {
2320b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2321b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2322b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2323b255b72bSSeevalamuthu Mariappan 
2324b255b72bSSeevalamuthu Mariappan 	return value;
2325b255b72bSSeevalamuthu Mariappan }
2326b255b72bSSeevalamuthu Mariappan 
23270f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
23280f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
23290f9c5a61SJohannes Berg 			     int tid)
23300f9c5a61SJohannes Berg {
23310f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2332b255b72bSSeevalamuthu Mariappan 	int cpu;
23330f9c5a61SJohannes Berg 
23340f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2335046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2336046d2e7cSSriram R 							   tid);
23370f9c5a61SJohannes Berg 
2338046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2339b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2340b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2341b255b72bSSeevalamuthu Mariappan 
2342046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2343046d2e7cSSriram R 						     cpu);
2344b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2345b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2346b255b72bSSeevalamuthu Mariappan 			}
2347b255b72bSSeevalamuthu Mariappan 		}
23480f9c5a61SJohannes Berg 
23490f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
23500f9c5a61SJohannes Berg 	}
23510f9c5a61SJohannes Berg 
23520f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
23530f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2354046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
23550f9c5a61SJohannes Berg 	}
23560f9c5a61SJohannes Berg 
23570f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
23580f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23590f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2360046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
23610f9c5a61SJohannes Berg 	}
23620f9c5a61SJohannes Berg 
23630f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
23640f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23650f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2366046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
23670f9c5a61SJohannes Berg 	}
23682fe4a29aSToke Høiland-Jørgensen 
23692fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
23702fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
23712fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
23722fe4a29aSToke Høiland-Jørgensen 
23732fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
23742fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
23752fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
23762fe4a29aSToke Høiland-Jørgensen 
23772fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
23782fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
23792fe4a29aSToke Høiland-Jørgensen 	}
23800f9c5a61SJohannes Berg }
23810f9c5a61SJohannes Berg 
2382c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2383c9c5962bSJohannes Berg {
2384c9c5962bSJohannes Berg 	unsigned int start;
2385c9c5962bSJohannes Berg 	u64 value;
2386c9c5962bSJohannes Berg 
2387c9c5962bSJohannes Berg 	do {
2388c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2389c9c5962bSJohannes Berg 		value = rxstats->bytes;
2390c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2391c9c5962bSJohannes Berg 
2392c9c5962bSJohannes Berg 	return value;
2393c9c5962bSJohannes Berg }
2394c9c5962bSJohannes Berg 
23950fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
23960fdf1493SJohannes Berg 		   bool tidstats)
2397b7ffbd7eSJohannes Berg {
2398b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2399b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2400b7ffbd7eSJohannes Berg 	u32 thr = 0;
2401c9c5962bSJohannes Berg 	int i, ac, cpu;
2402c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2403c9c5962bSJohannes Berg 
2404c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2405b7ffbd7eSJohannes Berg 
2406b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2407b7ffbd7eSJohannes Berg 
2408225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2409225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2410225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2411225b8189SJohannes Berg 	 */
2412225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2413bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2414225b8189SJohannes Berg 
24152b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2416a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2417a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2418a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2419a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
24209cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2421a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2422976bd9efSJohannes Berg 
2423976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2424bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2425bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2426a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2427976bd9efSJohannes Berg 	}
2428b7ffbd7eSJohannes Berg 
242984b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
24309cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2431e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2432b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
24332b9a7e1bSJohannes Berg 
2434a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2435a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2436b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
24372b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2438046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2439a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2440b7ffbd7eSJohannes Berg 	}
24412b9a7e1bSJohannes Berg 
2442a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
24432b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
24442b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2445046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2446a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
24472b9a7e1bSJohannes Berg 	}
24482b9a7e1bSJohannes Berg 
2449a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2450a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2451046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
24520f9c5a61SJohannes Berg 
2453046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2454c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2455c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2456c9c5962bSJohannes Berg 
2457046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2458046d2e7cSSriram R 						     cpu);
2459c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2460c9c5962bSJohannes Berg 			}
2461c9c5962bSJohannes Berg 		}
2462c9c5962bSJohannes Berg 
2463a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
24642b9a7e1bSJohannes Berg 	}
24652b9a7e1bSJohannes Berg 
2466a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2467046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2468046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2469c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2470c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2471c9c5962bSJohannes Berg 
2472046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2473046d2e7cSSriram R 						     cpu);
2474c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2475c9c5962bSJohannes Berg 			}
2476c9c5962bSJohannes Berg 		}
2477a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
24782b9a7e1bSJohannes Berg 	}
24792b9a7e1bSJohannes Berg 
2480a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2481046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2482a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
24832b9a7e1bSJohannes Berg 	}
24842b9a7e1bSJohannes Berg 
2485a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2486046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2487a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
24882b9a7e1bSJohannes Berg 	}
24892b9a7e1bSJohannes Berg 
2490b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2491b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2492b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2493b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2494b4809e94SToke Høiland-Jørgensen 	}
2495b4809e94SToke Høiland-Jørgensen 
2496b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2497b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2498b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2499b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2500b4809e94SToke Høiland-Jørgensen 	}
2501b4809e94SToke Høiland-Jørgensen 
2502b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2503942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2504b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2505b4809e94SToke Høiland-Jørgensen 	}
2506b4809e94SToke Høiland-Jørgensen 
2507046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2508046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2509c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2510c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2511c9c5962bSJohannes Berg 
2512046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2513e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2514c9c5962bSJohannes Berg 		}
2515c9c5962bSJohannes Berg 	}
2516b7ffbd7eSJohannes Berg 
2517225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2518225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2519a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2520a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2521225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2522225b8189SJohannes Berg 	}
2523225b8189SJohannes Berg 
252430686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
252530686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2526a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2527c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2528a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2529b7ffbd7eSJohannes Berg 		}
25302b9a7e1bSJohannes Berg 
2531046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2532a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
253340d9a38aSJohannes Berg 			sinfo->signal_avg =
2534046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2535a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
25362b9a7e1bSJohannes Berg 		}
25372b9a7e1bSJohannes Berg 	}
25382b9a7e1bSJohannes Berg 
2539c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2540c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2541c9c5962bSJohannes Berg 	 * pcpu statistics
2542c9c5962bSJohannes Berg 	 */
2543c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2544a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2545a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2546a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2547046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2548a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2549b7ffbd7eSJohannes Berg 
2550c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2551c9c5962bSJohannes Berg 
2552b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2553e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2554c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2555b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2556046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2557b7ffbd7eSJohannes Berg 		}
2558b7ffbd7eSJohannes Berg 	}
2559b7ffbd7eSJohannes Berg 
25608a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
25618a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2562046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2563e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2564a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
25652b9a7e1bSJohannes Berg 	}
25662b9a7e1bSJohannes Berg 
25678a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
25688a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2569a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2570a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
25712b9a7e1bSJohannes Berg 	}
2572b7ffbd7eSJohannes Berg 
25730fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
25746af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
25756af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
25768689c051SArend van Spriel 	}
257779c892b8SJohannes Berg 
2578b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2579b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2580a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2581a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2582a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2583a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2584a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2585dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
25861303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
25871303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2588b7ffbd7eSJohannes Berg 
2589433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2590433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2591433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2592b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2593a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2594433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2595b7ffbd7eSJohannes Berg 		}
2596433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2597433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2598433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2599dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
26001303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2601b7ffbd7eSJohannes Berg #endif
2602b7ffbd7eSJohannes Berg 	}
2603b7ffbd7eSJohannes Berg 
2604b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2605b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2606b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2607b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2608b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2609b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2610b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2611785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2612b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2613b7ffbd7eSJohannes Berg 
2614b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2615b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2616b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2617b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2618b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2619b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2620b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2621b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2622b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2623b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2624b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2625b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2626a74a8c84SJohannes Berg 	if (sta->sta.wme)
2627b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2628b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2629b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2630b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2631b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2632b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2633b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2634b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2635b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2636b7ffbd7eSJohannes Berg 
26373b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
26383b17fbf8SMaxim Altshul 
26393b17fbf8SMaxim Altshul 	if (thr != 0) {
2640a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
26413b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
26423b17fbf8SMaxim Altshul 	}
2643a78b26ffSVenkateswara Naralasetty 
2644a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2645046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2646046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2647a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2648a78b26ffSVenkateswara Naralasetty 	}
2649cc60dbbfSBalaji Pothunoori 
26509c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2651046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2652cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2653cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2654046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2655cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
26569c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2657cc60dbbfSBalaji Pothunoori 	}
2658ab60633cSNarayanraddi Masti 
2659ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2660ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2661ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2662ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2663ab60633cSNarayanraddi Masti 	}
26643b17fbf8SMaxim Altshul }
26653b17fbf8SMaxim Altshul 
26663b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
26673b17fbf8SMaxim Altshul {
26683b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
26693b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
26703b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
26713b17fbf8SMaxim Altshul 	u32 thr = 0;
26723b17fbf8SMaxim Altshul 
26733b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
26743b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
26753b17fbf8SMaxim Altshul 
2676b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2677b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2678b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2679b7ffbd7eSJohannes Berg 	else
26804fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2681b7ffbd7eSJohannes Berg 
26823b17fbf8SMaxim Altshul 	return thr;
2683b7ffbd7eSJohannes Berg }
2684b8da6b6aSJohannes Berg 
2685b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2686b8da6b6aSJohannes Berg {
2687c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2688c9c5962bSJohannes Berg 
2689046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2690046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2691c9c5962bSJohannes Berg 		return stats->last_rx;
2692046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2693b8da6b6aSJohannes Berg }
2694484a54c2SToke Høiland-Jørgensen 
2695484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2696484a54c2SToke Høiland-Jørgensen {
2697484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2698484a54c2SToke Høiland-Jørgensen 		return;
2699484a54c2SToke Høiland-Jørgensen 
2700484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2701484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2702484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2703484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2704484a54c2SToke Høiland-Jørgensen 	} else {
2705484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2706484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2707484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2708484a54c2SToke Høiland-Jørgensen 	}
2709484a54c2SToke Høiland-Jørgensen }
2710484a54c2SToke Høiland-Jørgensen 
2711484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2712484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2713484a54c2SToke Høiland-Jørgensen {
2714484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2715484a54c2SToke Høiland-Jørgensen 
2716484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2717484a54c2SToke Høiland-Jørgensen }
2718cb71f1d1SJohannes Berg 
2719cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2720cb71f1d1SJohannes Berg {
2721cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2722cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2723cb71f1d1SJohannes Berg 	int ret;
2724cb71f1d1SJohannes Berg 
2725cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2726cb71f1d1SJohannes Berg 
2727cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2728cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2729cb71f1d1SJohannes Berg 		return -EINVAL;
2730cb71f1d1SJohannes Berg 
2731cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2732cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2733cb71f1d1SJohannes Berg 		return -EBUSY;
2734cb71f1d1SJohannes Berg 
2735cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2736cb71f1d1SJohannes Berg 	if (!alloc)
2737cb71f1d1SJohannes Berg 		return -ENOMEM;
2738cb71f1d1SJohannes Berg 
2739cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2740cb71f1d1SJohannes Berg 	if (ret) {
2741cb71f1d1SJohannes Berg 		kfree(alloc);
2742cb71f1d1SJohannes Berg 		return ret;
2743cb71f1d1SJohannes Berg 	}
2744cb71f1d1SJohannes Berg 
2745cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2746cb71f1d1SJohannes Berg 
2747cb71f1d1SJohannes Berg 	return 0;
2748cb71f1d1SJohannes Berg }
2749cb71f1d1SJohannes Berg 
275021476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
275121476ad1SShaul Triebitz {
275221476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
275321476ad1SShaul Triebitz 
275421476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
275521476ad1SShaul Triebitz }
275621476ad1SShaul Triebitz 
2757cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2758cb71f1d1SJohannes Berg {
2759cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2760c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2761cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2762cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2763cb71f1d1SJohannes Berg 	int ret;
2764cb71f1d1SJohannes Berg 
2765c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2766c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2767cb71f1d1SJohannes Berg 
2768c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2769cb71f1d1SJohannes Berg 		return -EINVAL;
2770cb71f1d1SJohannes Berg 
2771177577dbSJohannes Berg 	rcu_read_lock();
2772177577dbSJohannes Berg 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2773177577dbSJohannes Berg 		rcu_read_unlock();
2774177577dbSJohannes Berg 		return -EALREADY;
2775177577dbSJohannes Berg 	}
2776177577dbSJohannes Berg 	/* we only modify under the mutex so this is fine */
2777177577dbSJohannes Berg 	rcu_read_unlock();
2778177577dbSJohannes Berg 
2779cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2780cb71f1d1SJohannes Berg 
2781ba6ddab9SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_INSERTED)) {
2782ba6ddab9SJohannes Berg 		ret = 0;
2783ba6ddab9SJohannes Berg 		goto hash;
2784ba6ddab9SJohannes Berg 	}
2785cb71f1d1SJohannes Berg 
2786cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2787cb71f1d1SJohannes Berg 				   old_links, new_links);
2788cb71f1d1SJohannes Berg 	if (ret) {
2789cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2790ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2791177577dbSJohannes Berg 		return ret;
2792cb71f1d1SJohannes Berg 	}
2793cb71f1d1SJohannes Berg 
2794ba6ddab9SJohannes Berg hash:
2795177577dbSJohannes Berg 	ret = link_sta_info_hash_add(sdata->local, link_sta);
2796177577dbSJohannes Berg 	WARN_ON(ret);
2797177577dbSJohannes Berg 	return 0;
2798cb71f1d1SJohannes Berg }
2799cb71f1d1SJohannes Berg 
2800cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2801cb71f1d1SJohannes Berg {
2802cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2803a8f62399SShaul Triebitz 	u16 old_links = sta->sta.valid_links;
2804cb71f1d1SJohannes Berg 
2805cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2806cb71f1d1SJohannes Berg 
2807cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2808cb71f1d1SJohannes Berg 
2809cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2810cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2811a8f62399SShaul Triebitz 				     old_links, sta->sta.valid_links);
2812cb71f1d1SJohannes Berg 
2813ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2814cb71f1d1SJohannes Berg }
2815175ad2ecSJohannes Berg 
2816175ad2ecSJohannes Berg void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2817175ad2ecSJohannes Berg 					   const u8 *ext_capab,
2818175ad2ecSJohannes Berg 					   unsigned int ext_capab_len)
2819175ad2ecSJohannes Berg {
2820175ad2ecSJohannes Berg 	u8 val;
2821175ad2ecSJohannes Berg 
2822175ad2ecSJohannes Berg 	sta->sta.max_amsdu_subframes = 0;
2823175ad2ecSJohannes Berg 
2824175ad2ecSJohannes Berg 	if (ext_capab_len < 8)
2825175ad2ecSJohannes Berg 		return;
2826175ad2ecSJohannes Berg 
2827175ad2ecSJohannes Berg 	/* The sender might not have sent the last bit, consider it to be 0 */
2828175ad2ecSJohannes Berg 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2829175ad2ecSJohannes Berg 
2830175ad2ecSJohannes Berg 	/* we did get all the bits, take the MSB as well */
2831175ad2ecSJohannes Berg 	if (ext_capab_len >= 9)
2832175ad2ecSJohannes Berg 		val |= u8_get_bits(ext_capab[8],
2833175ad2ecSJohannes Berg 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2834175ad2ecSJohannes Berg 
2835175ad2ecSJohannes Berg 	if (val)
2836175ad2ecSJohannes Berg 		sta->sta.max_amsdu_subframes = 4 << val;
2837175ad2ecSJohannes Berg }
2838