xref: /openbmc/linux/net/mac80211/sta_info.c (revision a8f62399daa6917e7f9efeb79bce4dd2cd494a1e)
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;
475c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
476c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
477cb71f1d1SJohannes Berg }
478cb71f1d1SJohannes Berg 
479f36fe0a2SJohannes Berg static struct sta_info *
480f36fe0a2SJohannes Berg __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
481f36fe0a2SJohannes Berg 		 const u8 *addr, int link_id, const u8 *link_addr,
482f36fe0a2SJohannes Berg 		 gfp_t gfp)
483f0706e82SJiri Benc {
484d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
485ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
486f0706e82SJiri Benc 	struct sta_info *sta;
48716c5f15cSRon Rindjunsky 	int i;
488f0706e82SJiri Benc 
489ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
490f0706e82SJiri Benc 	if (!sta)
49173651ee6SJohannes Berg 		return NULL;
492f0706e82SJiri Benc 
493246b39e4SJohannes Berg 	sta->local = local;
494246b39e4SJohannes Berg 	sta->sdata = sdata;
495246b39e4SJohannes Berg 
496cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
497246b39e4SJohannes Berg 		return NULL;
498c9c5962bSJohannes Berg 
499cb71f1d1SJohannes Berg 	if (link_id >= 0) {
500cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
501cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
502cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
503cb71f1d1SJohannes Berg 	} else {
504cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
505cb71f1d1SJohannes Berg 	}
506cb71f1d1SJohannes Berg 
50707346f81SJohannes Berg 	spin_lock_init(&sta->lock);
5081d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
5095ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
51067c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
511a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
51287f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
513433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
514433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
515433f5bc1SJohannes Berg 		if (!sta->mesh)
516433f5bc1SJohannes Berg 			goto free;
5174c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
518433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
51945d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5204c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5214c02d62fSKees Cook 				    0);
522433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
523433f5bc1SJohannes Berg 	}
52487f59c70SThomas Pedersen #endif
52507346f81SJohannes Berg 
526ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
52717741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
528f36fe0a2SJohannes Berg 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
529f36fe0a2SJohannes Berg 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
530480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
531480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
532480dd46bSMaxim Altshul 
533046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
534046d2e7cSSriram R 
53596fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
53696fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
53796fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
53896fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
53996fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
54096fc6efbSAlexander Wetzel 	 */
54196fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
54296fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
54396fc6efbSAlexander Wetzel 
5440f9c5a61SJohannes Berg 
5453a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5463a11ce08SJohannes Berg 
54771ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
54871ec375cSJohannes Berg 
549b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
550b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
551b6da911bSLiad Kaufman 
55284b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
553541a45a1SBruno Randolf 
554ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
555ba8c3d6fSFelix Fietkau 		void *txq_data;
556ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
557ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
558ba8c3d6fSFelix Fietkau 
559ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
560ba8c3d6fSFelix Fietkau 		if (!txq_data)
561ba8c3d6fSFelix Fietkau 			goto free;
562ba8c3d6fSFelix Fietkau 
563ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
564ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
565ba8c3d6fSFelix Fietkau 
566adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
567fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
568abfbc3afSJohannes Berg 		}
569ba8c3d6fSFelix Fietkau 	}
570ba8c3d6fSFelix Fietkau 
571ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
572ba8c3d6fSFelix Fietkau 		goto free_txq;
573f0706e82SJiri Benc 
574942741daSFelix Fietkau 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
575b4809e94SToke Høiland-Jørgensen 
576948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
577948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
578948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
579942741daSFelix Fietkau 		sta->airtime[i].deficit = sta->airtime_weight;
580942741daSFelix Fietkau 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
581942741daSFelix Fietkau 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
582942741daSFelix Fietkau 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
583948d887dSJohannes Berg 	}
58473651ee6SJohannes Berg 
5855a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
5864be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
587cccaec98SSenthil Balasubramanian 
588bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
589bd718fc1SJohannes Berg 		u32 mandatory = 0;
590bd718fc1SJohannes Berg 		int r;
591bd718fc1SJohannes Berg 
592bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
593bd718fc1SJohannes Berg 			continue;
594bd718fc1SJohannes Berg 
595bd718fc1SJohannes Berg 		switch (i) {
596bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
59763fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
598bd718fc1SJohannes Berg 			/*
599bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
600bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
601bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
602bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
603bd718fc1SJohannes Berg 			 * possible rate anyway.
604bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
605bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
606bd718fc1SJohannes Berg 			 */
607bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
608bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
609bd718fc1SJohannes Berg 			break;
610bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
611bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
612bd718fc1SJohannes Berg 			break;
613bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
614bd718fc1SJohannes Berg 			WARN_ON(1);
615bd718fc1SJohannes Berg 			mandatory = 0;
616bd718fc1SJohannes Berg 			break;
617bd718fc1SJohannes Berg 		}
618bd718fc1SJohannes Berg 
619bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
620bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
621bd718fc1SJohannes Berg 
622bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
623bd718fc1SJohannes Berg 
624bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
625bd718fc1SJohannes Berg 				continue;
626046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
627bd718fc1SJohannes Berg 		}
628bd718fc1SJohannes Berg 	}
629bd718fc1SJohannes Berg 
630af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
6316e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
6326e0456b5SFelix Fietkau 
633484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
634484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
635484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
636484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
637dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
638dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
639484a54c2SToke Høiland-Jørgensen 
640bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
641ef04a297SJohannes Berg 
642abfbc3afSJohannes Berg 	return sta;
643ba8c3d6fSFelix Fietkau 
644ba8c3d6fSFelix Fietkau free_txq:
645ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
646ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
647ba8c3d6fSFelix Fietkau free:
648cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
649433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
650433f5bc1SJohannes Berg 	kfree(sta->mesh);
651433f5bc1SJohannes Berg #endif
652ba8c3d6fSFelix Fietkau 	kfree(sta);
653ba8c3d6fSFelix Fietkau 	return NULL;
65473651ee6SJohannes Berg }
65573651ee6SJohannes Berg 
656f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
657f36fe0a2SJohannes Berg 				const u8 *addr, gfp_t gfp)
658f36fe0a2SJohannes Berg {
659f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
660f36fe0a2SJohannes Berg }
661f36fe0a2SJohannes Berg 
662f36fe0a2SJohannes Berg struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
663f36fe0a2SJohannes Berg 					  const u8 *mld_addr,
664f36fe0a2SJohannes Berg 					  unsigned int link_id,
665f36fe0a2SJohannes Berg 					  const u8 *link_addr,
666f36fe0a2SJohannes Berg 					  gfp_t gfp)
667f36fe0a2SJohannes Berg {
668f36fe0a2SJohannes Berg 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
669f36fe0a2SJohannes Berg }
670f36fe0a2SJohannes Berg 
6718c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
67234e89507SJohannes Berg {
67334e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
67434e89507SJohannes Berg 
67503e4497eSJohannes Berg 	/*
67603e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
67703e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
67803e4497eSJohannes Berg 	 * and another CPU turns off the net device.
67903e4497eSJohannes Berg 	 */
6808c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
6818c71df7aSGuy Eilam 		return -ENETDOWN;
68203e4497eSJohannes Berg 
683b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
684deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
6858c71df7aSGuy Eilam 		return -EINVAL;
6868c71df7aSGuy Eilam 
68783e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
68883e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
68983e7e4ceSHerbert Xu 	 * for correctness.
69031104891SJohannes Berg 	 */
69131104891SJohannes Berg 	rcu_read_lock();
69231104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
69331104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
69431104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
69531104891SJohannes Berg 		rcu_read_unlock();
69631104891SJohannes Berg 		return -ENOTUNIQ;
69731104891SJohannes Berg 	}
69831104891SJohannes Berg 	rcu_read_unlock();
69931104891SJohannes Berg 
7008c71df7aSGuy Eilam 	return 0;
70193e5deb1SJohannes Berg }
70244213b5eSJohannes Berg 
703f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
704f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
705f09603a2SJohannes Berg 				     struct sta_info *sta)
706f09603a2SJohannes Berg {
707f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
708f09603a2SJohannes Berg 	int err = 0;
709f09603a2SJohannes Berg 
710f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
711f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
712f09603a2SJohannes Berg 		if (err)
713f09603a2SJohannes Berg 			break;
714f09603a2SJohannes Berg 	}
715f09603a2SJohannes Berg 
716f09603a2SJohannes Berg 	if (!err) {
717a4ec45a4SJohannes Berg 		/*
718a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
719a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
720a4ec45a4SJohannes Berg 		 */
721a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
722f09603a2SJohannes Berg 			sta->uploaded = true;
723f09603a2SJohannes Berg 		return 0;
724f09603a2SJohannes Berg 	}
725f09603a2SJohannes Berg 
726f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
727bdcbd8e0SJohannes Berg 		sdata_info(sdata,
728bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
729bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
730f09603a2SJohannes Berg 		err = 0;
731f09603a2SJohannes Berg 	}
732f09603a2SJohannes Berg 
733f09603a2SJohannes Berg 	/* unwind on error */
734f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
735f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
736f09603a2SJohannes Berg 
737f09603a2SJohannes Berg 	return err;
738f09603a2SJohannes Berg }
739f09603a2SJohannes Berg 
740d405fd8cSGregory Greenman static void
741d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
742d405fd8cSGregory Greenman {
743d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
744d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
745d405fd8cSGregory Greenman 	struct sta_info *sta;
746d405fd8cSGregory Greenman 
747d405fd8cSGregory Greenman 	rcu_read_lock();
748d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
749d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
750d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
751d405fd8cSGregory Greenman 			continue;
752d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
753d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
754d405fd8cSGregory Greenman 			break;
755d405fd8cSGregory Greenman 		}
756d405fd8cSGregory Greenman 	}
757d405fd8cSGregory Greenman 	rcu_read_unlock();
758d405fd8cSGregory Greenman 
759d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
760d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
761d8675a63SJohannes Berg 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
762d8675a63SJohannes Berg 						  BSS_CHANGED_P2P_PS);
763d405fd8cSGregory Greenman 	}
764d405fd8cSGregory Greenman }
765d405fd8cSGregory Greenman 
76634e89507SJohannes Berg /*
7678c71df7aSGuy Eilam  * should be called with sta_mtx locked
7688c71df7aSGuy Eilam  * this function replaces the mutex lock
7698c71df7aSGuy Eilam  * with a RCU lock
7708c71df7aSGuy Eilam  */
7714d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
7728c71df7aSGuy Eilam {
7738c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
7748c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
7750c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
7768c71df7aSGuy Eilam 	int err = 0;
7778c71df7aSGuy Eilam 
7788c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
7798c71df7aSGuy Eilam 
7807852e361SJohannes Berg 	/* check if STA exists already */
7817852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
7824d33960bSJohannes Berg 		err = -EEXIST;
7838f9dcc29SAhmed Zaki 		goto out_cleanup;
78434e89507SJohannes Berg 	}
78534e89507SJohannes Berg 
7860c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
7870c2e3842SKoen Vandeputte 	if (!sinfo) {
7880c2e3842SKoen Vandeputte 		err = -ENOMEM;
7898f9dcc29SAhmed Zaki 		goto out_cleanup;
7900c2e3842SKoen Vandeputte 	}
7910c2e3842SKoen Vandeputte 
7924d33960bSJohannes Berg 	local->num_sta++;
7934d33960bSJohannes Berg 	local->sta_generation++;
7944d33960bSJohannes Berg 	smp_mb();
7954d33960bSJohannes Berg 
7965108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
7975108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
7985108ca82SJohannes Berg 
7994d33960bSJohannes Berg 	/* make the station visible */
80062b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
80162b14b24SJohannes Berg 	if (err)
80262b14b24SJohannes Berg 		goto out_drop_sta;
8034d33960bSJohannes Berg 
804f36fe0a2SJohannes Berg 	if (sta->sta.valid_links) {
805f36fe0a2SJohannes Berg 		err = link_sta_info_hash_add(local, &sta->deflink);
806f36fe0a2SJohannes Berg 		if (err) {
807f36fe0a2SJohannes Berg 			sta_info_hash_del(local, sta);
808f36fe0a2SJohannes Berg 			goto out_drop_sta;
809f36fe0a2SJohannes Berg 		}
810f36fe0a2SJohannes Berg 	}
811f36fe0a2SJohannes Berg 
8122bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
81383d5cc01SJohannes Berg 
8144dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8154dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8164dde3c36SMordechay Goodstein 	 */
8174dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8180cbf348aSAndrei Otcheretianski 		ieee80211_recalc_min_chandef(sta->sdata, -1);
8194dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8204dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8214dde3c36SMordechay Goodstein 	}
8224dde3c36SMordechay Goodstein 
8235108ca82SJohannes Berg 	/* notify driver */
8245108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8255108ca82SJohannes Berg 	if (err)
8265108ca82SJohannes Berg 		goto out_remove;
8275108ca82SJohannes Berg 
82883d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
829d405fd8cSGregory Greenman 
8305108ca82SJohannes Berg 	/* accept BA sessions now */
8315108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8324d33960bSJohannes Berg 
8334d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8344d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
8354d33960bSJohannes Berg 
8360ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
8370ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
8380ef049dcSArnd Bergmann 	kfree(sinfo);
839d0709a65SJohannes Berg 
840bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
841f0706e82SJiri Benc 
84234e89507SJohannes Berg 	/* move reference to rcu-protected */
84334e89507SJohannes Berg 	rcu_read_lock();
84434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
845e9f207f0SJiri Benc 
84673651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
84773651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
84873651ee6SJohannes Berg 
84973651ee6SJohannes Berg 	return 0;
8505108ca82SJohannes Berg  out_remove:
8510ad49045SJohannes Berg 	if (sta->sta.valid_links)
8520ad49045SJohannes Berg 		link_sta_info_hash_del(local, &sta->deflink);
8535108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
8545108ca82SJohannes Berg 	list_del_rcu(&sta->list);
85562b14b24SJohannes Berg  out_drop_sta:
8565108ca82SJohannes Berg 	local->num_sta--;
8575108ca82SJohannes Berg 	synchronize_net();
8588f9dcc29SAhmed Zaki  out_cleanup:
8597bc40aedSJohannes Berg 	cleanup_single_sta(sta);
8604d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
861ea32f065SSudip Mukherjee 	kfree(sinfo);
8624d33960bSJohannes Berg 	rcu_read_lock();
8634d33960bSJohannes Berg 	return err;
8648c71df7aSGuy Eilam }
8658c71df7aSGuy Eilam 
8668c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
8678c71df7aSGuy Eilam {
8688c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
869308f7fcfSZhao, Gang 	int err;
8708c71df7aSGuy Eilam 
8714d33960bSJohannes Berg 	might_sleep();
8724d33960bSJohannes Berg 
87331104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
87431104891SJohannes Berg 
8758c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
8768c71df7aSGuy Eilam 	if (err) {
8777bc40aedSJohannes Berg 		sta_info_free(local, sta);
87831104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
8798c71df7aSGuy Eilam 		rcu_read_lock();
8807bc40aedSJohannes Berg 		return err;
8818c71df7aSGuy Eilam 	}
8828c71df7aSGuy Eilam 
8837bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
884f0706e82SJiri Benc }
885f0706e82SJiri Benc 
88634e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
88734e89507SJohannes Berg {
88834e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
88934e89507SJohannes Berg 
89034e89507SJohannes Berg 	rcu_read_unlock();
89134e89507SJohannes Berg 
89234e89507SJohannes Berg 	return err;
89334e89507SJohannes Berg }
89434e89507SJohannes Berg 
895d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
896004c872eSJohannes Berg {
897004c872eSJohannes Berg 	/*
898004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
899004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
900004c872eSJohannes Berg 	 */
901d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
902004c872eSJohannes Berg }
903004c872eSJohannes Berg 
904d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
905004c872eSJohannes Berg {
906004c872eSJohannes Berg 	/*
907004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
908004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
909004c872eSJohannes Berg 	 */
910d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
911004c872eSJohannes Berg }
912004c872eSJohannes Berg 
9133d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9143d5839b6SIlan Peer {
9153d5839b6SIlan Peer 	/*
9163d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9173d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9183d5839b6SIlan Peer 	 */
9193d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9203d5839b6SIlan Peer }
9213d5839b6SIlan Peer 
922948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
923004c872eSJohannes Berg {
924948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
925948d887dSJohannes Berg 	switch (ac) {
926948d887dSJohannes Berg 	case IEEE80211_AC_VO:
927948d887dSJohannes Berg 		return BIT(6) | BIT(7);
928948d887dSJohannes Berg 	case IEEE80211_AC_VI:
929948d887dSJohannes Berg 		return BIT(4) | BIT(5);
930948d887dSJohannes Berg 	case IEEE80211_AC_BE:
931948d887dSJohannes Berg 		return BIT(0) | BIT(3);
932948d887dSJohannes Berg 	case IEEE80211_AC_BK:
933948d887dSJohannes Berg 		return BIT(1) | BIT(2);
934948d887dSJohannes Berg 	default:
935948d887dSJohannes Berg 		WARN_ON(1);
936948d887dSJohannes Berg 		return 0;
937d0709a65SJohannes Berg 	}
938004c872eSJohannes Berg }
939004c872eSJohannes Berg 
9409b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
941004c872eSJohannes Berg {
942c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
943d012a605SMarco Porsch 	struct ps_data *ps;
944948d887dSJohannes Berg 	bool indicate_tim = false;
945948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
946948d887dSJohannes Berg 	int ac;
947a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
948004c872eSJohannes Berg 
949d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
950d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
951c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
952c868cb35SJohannes Berg 			return;
9533e122be0SJohannes Berg 
954d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
9553f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
9563f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
9573f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
9583f52b7e3SMarco Porsch #endif
959d012a605SMarco Porsch 	} else {
960d012a605SMarco Porsch 		return;
961d012a605SMarco Porsch 	}
962d012a605SMarco Porsch 
963c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
964d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
965c868cb35SJohannes Berg 		return;
966004c872eSJohannes Berg 
967c868cb35SJohannes Berg 	if (sta->dead)
968c868cb35SJohannes Berg 		goto done;
9693e122be0SJohannes Berg 
970948d887dSJohannes Berg 	/*
971948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
972948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
973948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
974948d887dSJohannes Berg 	 * non-enabled ones.
975948d887dSJohannes Berg 	 */
976948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
977948d887dSJohannes Berg 		ignore_for_tim = 0;
978948d887dSJohannes Berg 
9799b7a86f3SJohannes Berg 	if (ignore_pending)
9809b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
9819b7a86f3SJohannes Berg 
982948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
983948d887dSJohannes Berg 		unsigned long tids;
984948d887dSJohannes Berg 
985f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
986948d887dSJohannes Berg 			continue;
987948d887dSJohannes Berg 
988948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
989948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
990948d887dSJohannes Berg 		if (indicate_tim)
991948d887dSJohannes Berg 			break;
992948d887dSJohannes Berg 
993948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
994948d887dSJohannes Berg 
995948d887dSJohannes Berg 		indicate_tim |=
996948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
997ba8c3d6fSFelix Fietkau 		indicate_tim |=
998ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
999004c872eSJohannes Berg 	}
1000004c872eSJohannes Berg 
1001c868cb35SJohannes Berg  done:
100265f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
1003004c872eSJohannes Berg 
10043d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
10053d5839b6SIlan Peer 		goto out_unlock;
10063d5839b6SIlan Peer 
1007948d887dSJohannes Berg 	if (indicate_tim)
1008d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
1009c868cb35SJohannes Berg 	else
1010d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
10113e122be0SJohannes Berg 
10129b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1013c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1014948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1015c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1016004c872eSJohannes Berg 	}
1017004c872eSJohannes Berg 
10183d5839b6SIlan Peer out_unlock:
101965f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1020004c872eSJohannes Berg }
1021004c872eSJohannes Berg 
10229b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10239b7a86f3SJohannes Berg {
10249b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10259b7a86f3SJohannes Berg }
10269b7a86f3SJohannes Berg 
1027cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1028f0706e82SJiri Benc {
1029e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1030f0706e82SJiri Benc 	int timeout;
1031f0706e82SJiri Benc 
1032f0706e82SJiri Benc 	if (!skb)
1033cd0b8d89SJohannes Berg 		return false;
1034f0706e82SJiri Benc 
1035e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1036f0706e82SJiri Benc 
1037f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
103857c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
103957c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
104057c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1041f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1042f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1043e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1044f0706e82SJiri Benc }
1045f0706e82SJiri Benc 
1046f0706e82SJiri Benc 
1047948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1048948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1049f0706e82SJiri Benc {
1050f0706e82SJiri Benc 	unsigned long flags;
1051f0706e82SJiri Benc 	struct sk_buff *skb;
1052f0706e82SJiri Benc 
105360750397SJohannes Berg 	/*
105460750397SJohannes Berg 	 * First check for frames that should expire on the filtered
105560750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
105660750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
105760750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
105860750397SJohannes Berg 	 * total_ps_buffered counter.
105960750397SJohannes Berg 	 */
1060f0706e82SJiri Benc 	for (;;) {
1061948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1062948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
106357c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1064948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1065836341a7SJohannes Berg 		else
1066f0706e82SJiri Benc 			skb = NULL;
1067948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1068f0706e82SJiri Benc 
106960750397SJohannes Berg 		/*
107060750397SJohannes Berg 		 * Frames are queued in order, so if this one
107160750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
107260750397SJohannes Berg 		 * we actually reached the end of the queue we
107360750397SJohannes Berg 		 * also need to stop, of course.
107460750397SJohannes Berg 		 */
107560750397SJohannes Berg 		if (!skb)
107660750397SJohannes Berg 			break;
1077d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
107860750397SJohannes Berg 	}
107960750397SJohannes Berg 
108060750397SJohannes Berg 	/*
108160750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
108260750397SJohannes Berg 	 * only find something if the filtered queue was emptied
108360750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
108460750397SJohannes Berg 	 * buffered frames.
108560750397SJohannes Berg 	 */
1086f0706e82SJiri Benc 	for (;;) {
1087948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1088948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1089f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1090948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1091f0706e82SJiri Benc 		else
1092f0706e82SJiri Benc 			skb = NULL;
1093948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1094f0706e82SJiri Benc 
109560750397SJohannes Berg 		/*
109660750397SJohannes Berg 		 * frames are queued in order, so if this one
109760750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
109860750397SJohannes Berg 		 * the queue) we can stop testing
109960750397SJohannes Berg 		 */
1100836341a7SJohannes Berg 		if (!skb)
1101836341a7SJohannes Berg 			break;
1102836341a7SJohannes Berg 
1103f0706e82SJiri Benc 		local->total_ps_buffered--;
1104bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1105bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1106d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1107f0706e82SJiri Benc 	}
11083393a608SJuuso Oikarinen 
110960750397SJohannes Berg 	/*
111060750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
111160750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
111260750397SJohannes Berg 	 * frames.
111360750397SJohannes Berg 	 */
111460750397SJohannes Berg 	sta_info_recalc_tim(sta);
111560750397SJohannes Berg 
111660750397SJohannes Berg 	/*
111760750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
111860750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
111960750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
112060750397SJohannes Berg 	 */
1121948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1122948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1123948d887dSJohannes Berg }
1124948d887dSJohannes Berg 
1125948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1126948d887dSJohannes Berg 					     struct sta_info *sta)
1127948d887dSJohannes Berg {
1128948d887dSJohannes Berg 	bool have_buffered = false;
1129948d887dSJohannes Berg 	int ac;
1130948d887dSJohannes Berg 
11313f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11323f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11333f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1134948d887dSJohannes Berg 		return false;
1135948d887dSJohannes Berg 
1136948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1137948d887dSJohannes Berg 		have_buffered |=
1138948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1139948d887dSJohannes Berg 
1140948d887dSJohannes Berg 	return have_buffered;
1141f0706e82SJiri Benc }
1142f0706e82SJiri Benc 
1143d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
114434e89507SJohannes Berg {
114534e89507SJohannes Berg 	struct ieee80211_local *local;
114634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
11470ad49045SJohannes Berg 	int ret, i;
114834e89507SJohannes Berg 
114934e89507SJohannes Berg 	might_sleep();
115034e89507SJohannes Berg 
115134e89507SJohannes Berg 	if (!sta)
115234e89507SJohannes Berg 		return -ENOENT;
115334e89507SJohannes Berg 
115434e89507SJohannes Berg 	local = sta->local;
115534e89507SJohannes Berg 	sdata = sta->sdata;
115634e89507SJohannes Berg 
115783d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
115883d5cc01SJohannes Berg 
1159098a6070SJohannes Berg 	/*
1160098a6070SJohannes Berg 	 * Before removing the station from the driver and
1161098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1162098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1163098a6070SJohannes Berg 	 * will be sufficient.
1164098a6070SJohannes Berg 	 */
1165c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1166c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1167098a6070SJohannes Berg 
1168f59374ebSSara Sharon 	/*
1169f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1170f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1171f59374ebSSara Sharon 	 * all such frames to be processed.
1172f59374ebSSara Sharon 	 */
1173f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1174f59374ebSSara Sharon 
11750ad49045SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
11760ad49045SJohannes Berg 		struct link_sta_info *link_sta;
11770ad49045SJohannes Berg 
11780ad49045SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
11790ad49045SJohannes Berg 			continue;
11800ad49045SJohannes Berg 
11810ad49045SJohannes Berg 		link_sta = rcu_dereference_protected(sta->link[i],
11820ad49045SJohannes Berg 						     lockdep_is_held(&local->sta_mtx));
11830ad49045SJohannes Berg 
11840ad49045SJohannes Berg 		link_sta_info_hash_del(local, link_sta);
11850ad49045SJohannes Berg 	}
11860ad49045SJohannes Berg 
118734e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1188b01711beSJohannes Berg 	if (WARN_ON(ret))
118934e89507SJohannes Berg 		return ret;
119034e89507SJohannes Berg 
1191a7a6bdd0SArik Nemtsov 	/*
1192a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1193a7a6bdd0SArik Nemtsov 	 * removal.
1194a7a6bdd0SArik Nemtsov 	 */
1195a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1196a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1197a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1198a7a6bdd0SArik Nemtsov 	}
1199a7a6bdd0SArik Nemtsov 
1200794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1201ef044763SEliad Peller 	sta->removed = true;
12024d33960bSJohannes Berg 
12036a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
12046a9d1b91SJohannes Berg 
1205a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1206a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1207a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1208a710c816SJohannes Berg 
1209d778207bSJohannes Berg 	return 0;
1210d778207bSJohannes Berg }
1211d778207bSJohannes Berg 
1212d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1213d778207bSJohannes Berg {
1214d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1215d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
12160ef049dcSArnd Bergmann 	struct station_info *sinfo;
1217d778207bSJohannes Berg 	int ret;
1218d778207bSJohannes Berg 
1219d778207bSJohannes Berg 	/*
1220d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1221d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1222d778207bSJohannes Berg 	 */
1223d778207bSJohannes Berg 
1224d778207bSJohannes Berg 	might_sleep();
1225d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1226d778207bSJohannes Berg 
12275981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1228b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1229b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1230b16798f5SJohannes Berg 	}
1231b16798f5SJohannes Berg 
1232c8782078SJohannes Berg 	/* now keys can no longer be reached */
12336d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
123434e89507SJohannes Berg 
12359b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
12369b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
12379b7a86f3SJohannes Berg 
123834e89507SJohannes Berg 	sta->dead = true;
123934e89507SJohannes Berg 
124034e89507SJohannes Berg 	local->num_sta--;
124134e89507SJohannes Berg 	local->sta_generation++;
124234e89507SJohannes Berg 
124383d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1244f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1245f09603a2SJohannes Berg 		if (ret) {
124683d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
124783d5cc01SJohannes Berg 			break;
124883d5cc01SJohannes Berg 		}
124983d5cc01SJohannes Berg 	}
1250d9a7ddb0SJohannes Berg 
1251f09603a2SJohannes Berg 	if (sta->uploaded) {
1252f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1253f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1254f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1255f09603a2SJohannes Berg 	}
125634e89507SJohannes Berg 
1257bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1258bdcbd8e0SJohannes Berg 
12590ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
12600ef049dcSArnd Bergmann 	if (sinfo)
12610fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
12620ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
12630ef049dcSArnd Bergmann 	kfree(sinfo);
1264ec15e68bSJouni Malinen 
126534e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
126634e89507SJohannes Berg 
12673a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
12683a11ce08SJohannes Berg 
1269d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1270d778207bSJohannes Berg }
1271d778207bSJohannes Berg 
1272d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1273d778207bSJohannes Berg {
1274d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1275d778207bSJohannes Berg 
1276d778207bSJohannes Berg 	if (err)
1277d778207bSJohannes Berg 		return err;
1278d778207bSJohannes Berg 
1279d778207bSJohannes Berg 	synchronize_net();
1280d778207bSJohannes Berg 
1281d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
128234e89507SJohannes Berg 
128334e89507SJohannes Berg 	return 0;
128434e89507SJohannes Berg }
128534e89507SJohannes Berg 
128634e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
128734e89507SJohannes Berg {
128834e89507SJohannes Berg 	struct sta_info *sta;
128934e89507SJohannes Berg 	int ret;
129034e89507SJohannes Berg 
129134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12927852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
129334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
129434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
129534e89507SJohannes Berg 
129634e89507SJohannes Berg 	return ret;
129734e89507SJohannes Berg }
129834e89507SJohannes Berg 
129934e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
130034e89507SJohannes Berg 			      const u8 *addr)
130134e89507SJohannes Berg {
130234e89507SJohannes Berg 	struct sta_info *sta;
130334e89507SJohannes Berg 	int ret;
130434e89507SJohannes Berg 
130534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
13067852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
130734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
130834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
130934e89507SJohannes Berg 
131034e89507SJohannes Berg 	return ret;
131134e89507SJohannes Berg }
1312f0706e82SJiri Benc 
131334f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1314f0706e82SJiri Benc {
131534f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1316f0706e82SJiri Benc 	struct sta_info *sta;
13173393a608SJuuso Oikarinen 	bool timer_needed = false;
1318f0706e82SJiri Benc 
1319d0709a65SJohannes Berg 	rcu_read_lock();
1320d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
13213393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
13223393a608SJuuso Oikarinen 			timer_needed = true;
1323d0709a65SJohannes Berg 	rcu_read_unlock();
1324f0706e82SJiri Benc 
13255bb644a0SJohannes Berg 	if (local->quiescing)
13265bb644a0SJohannes Berg 		return;
13275bb644a0SJohannes Berg 
13283393a608SJuuso Oikarinen 	if (!timer_needed)
13293393a608SJuuso Oikarinen 		return;
13303393a608SJuuso Oikarinen 
133126d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
133226d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1333f0706e82SJiri Benc }
1334f0706e82SJiri Benc 
13357bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
13367bedd0cfSJohannes Berg {
13377bedd0cfSJohannes Berg 	int err;
13387bedd0cfSJohannes Berg 
133983e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
13407bedd0cfSJohannes Berg 	if (err)
13417bedd0cfSJohannes Berg 		return err;
13427bedd0cfSJohannes Berg 
1343ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1344ba6ddab9SJohannes Berg 	if (err) {
1345ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1346ba6ddab9SJohannes Berg 		return err;
1347ba6ddab9SJohannes Berg 	}
1348ba6ddab9SJohannes Berg 
13494d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
135034e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1351f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1352f0706e82SJiri Benc 
135334f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
13547bedd0cfSJohannes Berg 	return 0;
1355f0706e82SJiri Benc }
1356f0706e82SJiri Benc 
1357f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1358f0706e82SJiri Benc {
1359a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
136083e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1361ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1362f0706e82SJiri Benc }
1363f0706e82SJiri Benc 
1364051007d9SJohannes Berg 
1365e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1366f0706e82SJiri Benc {
1367b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1368f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1369d778207bSJohannes Berg 	LIST_HEAD(free_list);
137044213b5eSJohannes Berg 	int ret = 0;
1371f0706e82SJiri Benc 
1372d0709a65SJohannes Berg 	might_sleep();
1373d0709a65SJohannes Berg 
1374e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1375e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1376e716251dSJohannes Berg 
137734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
137834e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1379e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1380e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1381d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1382d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
138334316837SJohannes Berg 			ret++;
138434316837SJohannes Berg 		}
138534e89507SJohannes Berg 	}
1386d778207bSJohannes Berg 
1387d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1388d778207bSJohannes Berg 		synchronize_net();
1389d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1390d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1391d778207bSJohannes Berg 	}
139234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
139344213b5eSJohannes Berg 
1394051007d9SJohannes Berg 	return ret;
1395051007d9SJohannes Berg }
1396051007d9SJohannes Berg 
139724723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
139824723d1bSJohannes Berg 			  unsigned long exp_time)
139924723d1bSJohannes Berg {
140024723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
140124723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
140224723d1bSJohannes Berg 
140334e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1404e46a2cf9SMohammed Shafi Shajakhan 
1405e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1406b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1407b8da6b6aSJohannes Berg 
1408ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1409ec2b774eSMarek Lindner 			continue;
1410ec2b774eSMarek Lindner 
1411b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1412eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1413bdcbd8e0SJohannes Berg 				sta->sta.addr);
14143f52b7e3SMarco Porsch 
14153f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
14163f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
14173f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
14183f52b7e3SMarco Porsch 
141934e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
142024723d1bSJohannes Berg 		}
1421e46a2cf9SMohammed Shafi Shajakhan 	}
1422e46a2cf9SMohammed Shafi Shajakhan 
142334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
142424723d1bSJohannes Berg }
142517741cdcSJohannes Berg 
1426686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1427686b9cb9SBen Greear 						   const u8 *addr,
1428686b9cb9SBen Greear 						   const u8 *localaddr)
142917741cdcSJohannes Berg {
14307bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
143183e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
14327bedd0cfSJohannes Berg 	struct sta_info *sta;
143317741cdcSJohannes Berg 
1434686b9cb9SBen Greear 	/*
1435686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1436686b9cb9SBen Greear 	 * ... first in list.
1437686b9cb9SBen Greear 	 */
143883e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1439686b9cb9SBen Greear 		if (localaddr &&
1440b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1441686b9cb9SBen Greear 			continue;
1442f7c65594SJohannes Berg 		if (!sta->uploaded)
1443f7c65594SJohannes Berg 			return NULL;
144417741cdcSJohannes Berg 		return &sta->sta;
1445f7c65594SJohannes Berg 	}
1446f7c65594SJohannes Berg 
1447abe60632SJohannes Berg 	return NULL;
144817741cdcSJohannes Berg }
1449686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
14505ed176e1SJohannes Berg 
14515ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
14525ed176e1SJohannes Berg 					 const u8 *addr)
14535ed176e1SJohannes Berg {
1454f7c65594SJohannes Berg 	struct sta_info *sta;
14555ed176e1SJohannes Berg 
14565ed176e1SJohannes Berg 	if (!vif)
14575ed176e1SJohannes Berg 		return NULL;
14585ed176e1SJohannes Berg 
1459f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1460f7c65594SJohannes Berg 	if (!sta)
1461f7c65594SJohannes Berg 		return NULL;
14625ed176e1SJohannes Berg 
1463f7c65594SJohannes Berg 	if (!sta->uploaded)
1464f7c65594SJohannes Berg 		return NULL;
1465f7c65594SJohannes Berg 
1466f7c65594SJohannes Berg 	return &sta->sta;
14675ed176e1SJohannes Berg }
146817741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1469af818581SJohannes Berg 
1470e3685e03SJohannes Berg /* powersave support code */
1471e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
147250a9432dSJohannes Berg {
1473608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1474e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1475e3685e03SJohannes Berg 	struct sk_buff_head pending;
1476ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1477e3685e03SJohannes Berg 	unsigned long flags;
1478d012a605SMarco Porsch 	struct ps_data *ps;
1479d012a605SMarco Porsch 
14803918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
14813918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
14823918edb0SFelix Fietkau 				     u.ap);
14833918edb0SFelix Fietkau 
14843918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1485d012a605SMarco Porsch 		ps = &sdata->bss->ps;
14863f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
14873f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1488d012a605SMarco Porsch 	else
1489d012a605SMarco Porsch 		return;
149050a9432dSJohannes Berg 
1491c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
149247086fc5SJohannes Berg 
14935a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1494948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1495ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1496948d887dSJohannes Berg 
149730686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
149812375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1499af818581SJohannes Berg 
1500ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1501adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1502ba8c3d6fSFelix Fietkau 			continue;
1503ba8c3d6fSFelix Fietkau 
150418667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1505ba8c3d6fSFelix Fietkau 	}
1506ba8c3d6fSFelix Fietkau 
1507948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1508af818581SJohannes Berg 
15091d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
15101d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1511af818581SJohannes Berg 	/* Send all buffered frames to the station */
1512948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1513948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1514948d887dSJohannes Berg 
1515987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1516948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1517987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1518948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1519948d887dSJohannes Berg 		filtered += tmp - count;
1520948d887dSJohannes Berg 		count = tmp;
1521948d887dSJohannes Berg 
1522987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1523948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1524987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1525948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1526948d887dSJohannes Berg 		buffered += tmp - count;
1527948d887dSJohannes Berg 	}
1528948d887dSJohannes Berg 
1529e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
15305ac2e350SJohannes Berg 
15315ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
15325ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
15335ac2e350SJohannes Berg 
15345ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
15355ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
15365ac2e350SJohannes Berg 	 */
15375ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
15385ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
15391d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1540948d887dSJohannes Berg 
1541e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1542e3685e03SJohannes Berg 
1543af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1544af818581SJohannes Berg 
1545c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1546c868cb35SJohannes Berg 
1547bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
15482595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1549bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
155017c18bf8SJohannes Berg 
155117c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1552af818581SJohannes Berg }
1553af818581SJohannes Berg 
15540ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1555b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
15560ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1557ce662b44SJohannes Berg {
15580ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1559ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1560ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1561ce662b44SJohannes Berg 	struct sk_buff *skb;
1562ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1563ce662b44SJohannes Berg 	__le16 fc;
1564a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1565ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
156655de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1567ce662b44SJohannes Berg 
1568ce662b44SJohannes Berg 	if (qos) {
1569ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1570ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1571ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1572ce662b44SJohannes Berg 	} else {
1573ce662b44SJohannes Berg 		size -= 2;
1574ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1575ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1576ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1577ce662b44SJohannes Berg 	}
1578ce662b44SJohannes Berg 
1579ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1580ce662b44SJohannes Berg 	if (!skb)
1581ce662b44SJohannes Berg 		return;
1582ce662b44SJohannes Berg 
1583ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1584ce662b44SJohannes Berg 
15854df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1586ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1587ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1588ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1589ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1590ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1591864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1592ce662b44SJohannes Berg 
1593ce662b44SJohannes Berg 	skb->priority = tid;
1594ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
159559b66255SJohannes Berg 	if (qos) {
1596ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1597ce662b44SJohannes Berg 
15980ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1599ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1600ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
16010ead2510SEmmanuel Grumbach 			if (more_data)
16020ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
16030ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
16040ead2510SEmmanuel Grumbach 		}
1605ce662b44SJohannes Berg 	}
1606ce662b44SJohannes Berg 
1607ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1608ce662b44SJohannes Berg 
1609ce662b44SJohannes Berg 	/*
1610ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1611ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1612deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1613deeaee19SJohannes Berg 	 * ends the poll/service period.
1614ce662b44SJohannes Berg 	 */
161502f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1616deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1617ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1618ce662b44SJohannes Berg 
16196b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
16206b127c71SSujith Manoharan 
1621b77cf4f8SJohannes Berg 	if (call_driver)
1622b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1623b77cf4f8SJohannes Berg 					  reason, false);
162440b96408SJohannes Berg 
162589afe614SJohannes Berg 	skb->dev = sdata->dev;
162689afe614SJohannes Berg 
162755de908aSJohannes Berg 	rcu_read_lock();
1628d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
162955de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
163055de908aSJohannes Berg 		rcu_read_unlock();
163155de908aSJohannes Berg 		kfree_skb(skb);
163255de908aSJohannes Berg 		return;
163355de908aSJohannes Berg 	}
163455de908aSJohannes Berg 
163573c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
163608aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
163755de908aSJohannes Berg 	rcu_read_unlock();
1638ce662b44SJohannes Berg }
1639ce662b44SJohannes Berg 
16400a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
16410a1cb809SJohannes Berg {
16420a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
16430a1cb809SJohannes Berg 	if (tids & 0xF8)
16440a1cb809SJohannes Berg 		return fls(tids) - 1;
16450a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
16460a1cb809SJohannes Berg 	if (tids & BIT(0))
16470a1cb809SJohannes Berg 		return 0;
16480a1cb809SJohannes Berg 	return fls(tids) - 1;
16490a1cb809SJohannes Berg }
16500a1cb809SJohannes Berg 
16510ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
16520ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
16530ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
16540ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
16550ead2510SEmmanuel Grumbach  */
16560ead2510SEmmanuel Grumbach static bool
16570ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
16580ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
16590ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
16600ead2510SEmmanuel Grumbach {
16610ead2510SEmmanuel Grumbach 	int ac;
16620ead2510SEmmanuel Grumbach 
16630ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
16640ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
16650ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
16660ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
16670ead2510SEmmanuel Grumbach 	 */
16680ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
16690ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
16700ead2510SEmmanuel Grumbach 		return true;
16710ead2510SEmmanuel Grumbach 
16720ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1673f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
16740ead2510SEmmanuel Grumbach 			continue;
16750ead2510SEmmanuel Grumbach 
16760ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
16770ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
16780ead2510SEmmanuel Grumbach 			return true;
16790ead2510SEmmanuel Grumbach 	}
16800ead2510SEmmanuel Grumbach 
16810ead2510SEmmanuel Grumbach 	return false;
16820ead2510SEmmanuel Grumbach }
16830ead2510SEmmanuel Grumbach 
168447086fc5SJohannes Berg static void
16850ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
16860ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
16870ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
16880ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1689af818581SJohannes Berg {
1690af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1691af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1692948d887dSJohannes Berg 	int ac;
1693af818581SJohannes Berg 
1694f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1695948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
16964049e09aSJohannes Berg 		unsigned long tids;
16974049e09aSJohannes Berg 
1698f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1699948d887dSJohannes Berg 			continue;
1700948d887dSJohannes Berg 
17014049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
17024049e09aSJohannes Berg 
1703f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1704f9f760b4SJohannes Berg 		 * release from hardware queues
1705f9f760b4SJohannes Berg 		 */
17060ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
17070ead2510SEmmanuel Grumbach 			*driver_release_tids |=
17080ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
17090ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1710ba8c3d6fSFelix Fietkau 		}
1711f9f760b4SJohannes Berg 
17120ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
171347086fc5SJohannes Berg 			struct sk_buff *skb;
171447086fc5SJohannes Berg 
171547086fc5SJohannes Berg 			while (n_frames > 0) {
1716948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1717948d887dSJohannes Berg 				if (!skb) {
171847086fc5SJohannes Berg 					skb = skb_dequeue(
171947086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1720af818581SJohannes Berg 					if (skb)
1721af818581SJohannes Berg 						local->total_ps_buffered--;
1722af818581SJohannes Berg 				}
172347086fc5SJohannes Berg 				if (!skb)
172447086fc5SJohannes Berg 					break;
172547086fc5SJohannes Berg 				n_frames--;
17260ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
172747086fc5SJohannes Berg 			}
1728948d887dSJohannes Berg 		}
1729948d887dSJohannes Berg 
17300ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
17310ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
17320ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
17334049e09aSJohannes Berg 		 */
1734948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17350ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1736948d887dSJohannes Berg 			break;
1737948d887dSJohannes Berg 	}
1738948d887dSJohannes Berg }
1739af818581SJohannes Berg 
17400ead2510SEmmanuel Grumbach static void
17410ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
17420ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
17430ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
17440ead2510SEmmanuel Grumbach {
17450ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
17460ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
17470ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
17480ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
17490ead2510SEmmanuel Grumbach 	bool more_data;
17500ead2510SEmmanuel Grumbach 
17510ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
17520ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
17530ead2510SEmmanuel Grumbach 
17540ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
17550ead2510SEmmanuel Grumbach 
17560ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
17570ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
17580ead2510SEmmanuel Grumbach 
17590ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
17600ead2510SEmmanuel Grumbach 
17611a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
17620ead2510SEmmanuel Grumbach 		driver_release_tids =
17630ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
17640ead2510SEmmanuel Grumbach 
1765f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1766f438ceb8SEmmanuel Grumbach 		int tid, ac;
17674049e09aSJohannes Berg 
1768ce662b44SJohannes Berg 		/*
1769ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1770ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1771ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1772ce662b44SJohannes Berg 		 *
1773ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1774ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1775ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1776ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1777ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1778ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1779ce662b44SJohannes Berg 		 *
1780ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1781ce662b44SJohannes Berg 		 */
1782ce662b44SJohannes Berg 
1783ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1784f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1785d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1786d7f84244SEmmanuel Grumbach 				break;
1787f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1788ce662b44SJohannes Berg 
17890ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1790f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
179147086fc5SJohannes Berg 		struct sk_buff_head pending;
179247086fc5SJohannes Berg 		struct sk_buff *skb;
179340b96408SJohannes Berg 		int num = 0;
179440b96408SJohannes Berg 		u16 tids = 0;
1795b77cf4f8SJohannes Berg 		bool need_null = false;
179647086fc5SJohannes Berg 
179747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
179847086fc5SJohannes Berg 
179947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1800af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
180147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
180240b96408SJohannes Berg 			u8 *qoshdr = NULL;
180340b96408SJohannes Berg 
180440b96408SJohannes Berg 			num++;
1805af818581SJohannes Berg 
1806af818581SJohannes Berg 			/*
180747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
180847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
180947086fc5SJohannes Berg 			 * exchange.
1810af818581SJohannes Berg 			 */
18116b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
18126b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1813af818581SJohannes Berg 
181447086fc5SJohannes Berg 			/*
181547086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
181647086fc5SJohannes Berg 			 * more buffered frames for this STA
181747086fc5SJohannes Berg 			 */
181824b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
181947086fc5SJohannes Berg 				hdr->frame_control |=
182047086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
182124b9c373SJanusz.Dziedzic@tieto.com 			else
182224b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
182324b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1824af818581SJohannes Berg 
182540b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
182640b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
182740b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
182840b96408SJohannes Berg 
1829b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1830b77cf4f8SJohannes Berg 
1831b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1832b77cf4f8SJohannes Berg 
1833b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1834b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1835b77cf4f8SJohannes Berg 				continue;
1836b77cf4f8SJohannes Berg 
1837b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1838b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1839b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1840b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1841b77cf4f8SJohannes Berg 				break;
1842b77cf4f8SJohannes Berg 			}
1843b77cf4f8SJohannes Berg 
1844b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1845b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1846b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1847b77cf4f8SJohannes Berg 			 * and be done.
1848b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1849b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1850b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1851b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1852b77cf4f8SJohannes Berg 			 *
1853b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1854b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1855b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1856b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1857b77cf4f8SJohannes Berg 			 *
1858b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1859b77cf4f8SJohannes Berg 			 */
1860b77cf4f8SJohannes Berg 			if (qoshdr) {
186140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1862deeaee19SJohannes Berg 
186347086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
186447086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1865b77cf4f8SJohannes Berg 			} else {
1866b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1867b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1868b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1869b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1870b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1871b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1872b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1873b77cf4f8SJohannes Berg 				 */
1874b77cf4f8SJohannes Berg 				hdr->frame_control |=
1875b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1876b77cf4f8SJohannes Berg 				need_null = true;
1877b77cf4f8SJohannes Berg 				num++;
187852a3f20cSMarco Porsch 			}
1879b77cf4f8SJohannes Berg 			break;
188047086fc5SJohannes Berg 		}
188147086fc5SJohannes Berg 
188240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
188340b96408SJohannes Berg 					  reason, more_data);
188440b96408SJohannes Berg 
188547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1886af818581SJohannes Berg 
1887b77cf4f8SJohannes Berg 		if (need_null)
1888b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
18890ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
18900ead2510SEmmanuel Grumbach 				reason, false, false);
1891b77cf4f8SJohannes Berg 
1892c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1893af818581SJohannes Berg 	} else {
1894ba8c3d6fSFelix Fietkau 		int tid;
1895ba8c3d6fSFelix Fietkau 
1896af818581SJohannes Berg 		/*
18974049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
18984049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1899f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1900f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1901f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1902f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1903f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1904f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1905af818581SJohannes Berg 		 */
19064049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
190747086fc5SJohannes Berg 					    n_frames, reason, more_data);
19084049e09aSJohannes Berg 
19094049e09aSJohannes Berg 		/*
19104049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
19114049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1912f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
19134049e09aSJohannes Berg 		 * release function.
1914f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1915ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1916ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
19174049e09aSJohannes Berg 		 */
1918ba8c3d6fSFelix Fietkau 
1919ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1920ba8c3d6fSFelix Fietkau 			return;
1921ba8c3d6fSFelix Fietkau 
1922ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1923adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1924adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
19251e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1926ba8c3d6fSFelix Fietkau 				continue;
1927ba8c3d6fSFelix Fietkau 
1928ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1929ba8c3d6fSFelix Fietkau 			break;
1930ba8c3d6fSFelix Fietkau 		}
1931af818581SJohannes Berg 	}
1932af818581SJohannes Berg }
1933af818581SJohannes Berg 
1934af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1935af818581SJohannes Berg {
193647086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1937af818581SJohannes Berg 
1938af818581SJohannes Berg 	/*
193947086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
194047086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
194147086fc5SJohannes Berg 	 * only from the non-enabled ones.
1942af818581SJohannes Berg 	 */
194347086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
194447086fc5SJohannes Berg 		ignore_for_response = 0;
1945af818581SJohannes Berg 
194647086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
194747086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1948af818581SJohannes Berg }
194947086fc5SJohannes Berg 
195047086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
195147086fc5SJohannes Berg {
195247086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
195347086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
195447086fc5SJohannes Berg 
195547086fc5SJohannes Berg 	/*
195647086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
195747086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
195847086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
195947086fc5SJohannes Berg 	 * actually getting called.
196047086fc5SJohannes Berg 	 */
196147086fc5SJohannes Berg 	if (!delivery_enabled)
196247086fc5SJohannes Berg 		return;
196347086fc5SJohannes Berg 
196447086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
196547086fc5SJohannes Berg 	case 1:
196647086fc5SJohannes Berg 		n_frames = 2;
196747086fc5SJohannes Berg 		break;
196847086fc5SJohannes Berg 	case 2:
196947086fc5SJohannes Berg 		n_frames = 4;
197047086fc5SJohannes Berg 		break;
197147086fc5SJohannes Berg 	case 3:
197247086fc5SJohannes Berg 		n_frames = 6;
197347086fc5SJohannes Berg 		break;
197447086fc5SJohannes Berg 	case 0:
197547086fc5SJohannes Berg 		/* XXX: what is a good value? */
197613a8098aSAndrei Otcheretianski 		n_frames = 128;
197747086fc5SJohannes Berg 		break;
197847086fc5SJohannes Berg 	}
197947086fc5SJohannes Berg 
198047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
198147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1982af818581SJohannes Berg }
1983af818581SJohannes Berg 
1984af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1985af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1986af818581SJohannes Berg {
1987af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1988af818581SJohannes Berg 
1989b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1990b5878a2dSJohannes Berg 
19915ac2e350SJohannes Berg 	if (block) {
1992c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
199317c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
19945ac2e350SJohannes Berg 		return;
19955ac2e350SJohannes Berg 	}
19965ac2e350SJohannes Berg 
19975ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
19985ac2e350SJohannes Berg 		return;
19995ac2e350SJohannes Berg 
20005ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
20015ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
20025ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20035ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20045ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
20055ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
20065ac2e350SJohannes Berg 		/* must be asleep in this case */
20075ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
20085ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
20095ac2e350SJohannes Berg 	} else {
20105ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
201117c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
20125ac2e350SJohannes Berg 	}
2013af818581SJohannes Berg }
2014af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
2015dcf55fb5SFelix Fietkau 
2016e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
201737fbd908SJohannes Berg {
201837fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
201937fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
202037fbd908SJohannes Berg 
202137fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
202237fbd908SJohannes Berg 
202337fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
202437fbd908SJohannes Berg }
2025e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
202637fbd908SJohannes Berg 
20270ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
20280ead2510SEmmanuel Grumbach {
20290ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
20300ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
20310ead2510SEmmanuel Grumbach 	bool more_data;
20320ead2510SEmmanuel Grumbach 
20330ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
20340ead2510SEmmanuel Grumbach 
20350ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
20360ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
20370ead2510SEmmanuel Grumbach 					       reason, 0);
20380ead2510SEmmanuel Grumbach 
20390ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
20400ead2510SEmmanuel Grumbach }
20410ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
20420ead2510SEmmanuel Grumbach 
2043042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2044042ec453SJohannes Berg 				u8 tid, bool buffered)
2045dcf55fb5SFelix Fietkau {
2046dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2047dcf55fb5SFelix Fietkau 
20485a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2049042ec453SJohannes Berg 		return;
2050042ec453SJohannes Berg 
20511b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
20521b000789SJohannes Berg 
2053948d887dSJohannes Berg 	if (buffered)
2054948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2055948d887dSJohannes Berg 	else
2056948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2057948d887dSJohannes Berg 
2058c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2059dcf55fb5SFelix Fietkau }
2060042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2061d9a7ddb0SJohannes Berg 
2062b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2063b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2064b4809e94SToke Høiland-Jørgensen {
2065942741daSFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2066942741daSFelix Fietkau 	struct ieee80211_local *local = sta->sdata->local;
2067942741daSFelix Fietkau 	u8 ac = ieee80211_ac_from_tid(tid);
2068942741daSFelix Fietkau 	u32 airtime = 0;
2069c77bfab9SFelix Fietkau 	u32 diff;
2070b4809e94SToke Høiland-Jørgensen 
2071942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2072942741daSFelix Fietkau 		airtime += tx_airtime;
2073942741daSFelix Fietkau 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2074942741daSFelix Fietkau 		airtime += rx_airtime;
2075b4809e94SToke Høiland-Jørgensen 
2076942741daSFelix Fietkau 	spin_lock_bh(&local->active_txq_lock[ac]);
2077942741daSFelix Fietkau 	sta->airtime[ac].tx_airtime += tx_airtime;
2078942741daSFelix Fietkau 	sta->airtime[ac].rx_airtime += rx_airtime;
2079c77bfab9SFelix Fietkau 
2080c77bfab9SFelix Fietkau 	diff = (u32)jiffies - sta->airtime[ac].last_active;
2081c77bfab9SFelix Fietkau 	if (diff <= AIRTIME_ACTIVE_DURATION)
2082942741daSFelix Fietkau 		sta->airtime[ac].deficit -= airtime;
2083c77bfab9SFelix Fietkau 
2084942741daSFelix Fietkau 	spin_unlock_bh(&local->active_txq_lock[ac]);
2085b4809e94SToke Høiland-Jørgensen }
2086b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2087b4809e94SToke Høiland-Jørgensen 
20883ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
20893ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
20903ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
20913ace10f5SKan Yan {
20923ace10f5SKan Yan 	int tx_pending;
20933ace10f5SKan Yan 
2094911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2095911bde0fSToke Høiland-Jørgensen 		return;
2096911bde0fSToke Høiland-Jørgensen 
20973ace10f5SKan Yan 	if (!tx_completed) {
20983ace10f5SKan Yan 		if (sta)
20993ace10f5SKan Yan 			atomic_add(tx_airtime,
21003ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
21013ace10f5SKan Yan 
21023ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
21038e4bac06SFelix Fietkau 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
21043ace10f5SKan Yan 		return;
21053ace10f5SKan Yan 	}
21063ace10f5SKan Yan 
21073ace10f5SKan Yan 	if (sta) {
21083ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
21093ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
211004e35caaSFelix Fietkau 		if (tx_pending < 0)
21113ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
21123ace10f5SKan Yan 				       tx_pending, 0);
21133ace10f5SKan Yan 	}
21143ace10f5SKan Yan 
21158e4bac06SFelix Fietkau 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
21163ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
21178e4bac06SFelix Fietkau 				       &local->aql_ac_pending_airtime[ac]);
21183ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
21193ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
21203ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
21218e4bac06SFelix Fietkau 		      tx_airtime)) {
21228e4bac06SFelix Fietkau 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
21233ace10f5SKan Yan 			       tx_pending, 0);
21248e4bac06SFelix Fietkau 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
21258e4bac06SFelix Fietkau 	}
21263ace10f5SKan Yan }
21273ace10f5SKan Yan 
212883d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2129d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2130d9a7ddb0SJohannes Berg {
21318bf11d8dSJohannes Berg 	might_sleep();
2132d9a7ddb0SJohannes Berg 
2133d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2134d9a7ddb0SJohannes Berg 		return 0;
2135d9a7ddb0SJohannes Berg 
2136f09603a2SJohannes Berg 	/* check allowed transitions first */
2137f09603a2SJohannes Berg 
2138d9a7ddb0SJohannes Berg 	switch (new_state) {
2139d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2140f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2141d9a7ddb0SJohannes Berg 			return -EINVAL;
2142d9a7ddb0SJohannes Berg 		break;
2143d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2144f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2145f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2146d9a7ddb0SJohannes Berg 			return -EINVAL;
2147d9a7ddb0SJohannes Berg 		break;
2148d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2149f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2150f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2151d9a7ddb0SJohannes Berg 			return -EINVAL;
2152d9a7ddb0SJohannes Berg 		break;
2153d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2154f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2155d9a7ddb0SJohannes Berg 			return -EINVAL;
2156d9a7ddb0SJohannes Berg 		break;
2157d9a7ddb0SJohannes Berg 	default:
2158d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2159d9a7ddb0SJohannes Berg 		return -EINVAL;
2160d9a7ddb0SJohannes Berg 	}
2161d9a7ddb0SJohannes Berg 
2162bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2163bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2164f09603a2SJohannes Berg 
2165f09603a2SJohannes Berg 	/*
2166f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2167f09603a2SJohannes Berg 	 * fail the transition
2168f09603a2SJohannes Berg 	 */
2169f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2170f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2171f09603a2SJohannes Berg 					sta->sta_state, new_state);
2172f09603a2SJohannes Berg 		if (err)
2173f09603a2SJohannes Berg 			return err;
2174f09603a2SJohannes Berg 	}
2175f09603a2SJohannes Berg 
2176f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2177f09603a2SJohannes Berg 
2178f09603a2SJohannes Berg 	switch (new_state) {
2179f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2180f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2181f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2182f09603a2SJohannes Berg 		break;
2183f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2184a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2185f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2186a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2187f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
21880cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
218952cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
219052cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2191a7201a6cSIlan Peer 		}
2192f09603a2SJohannes Berg 		break;
2193f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2194f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2195f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
21969cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
21970cbf348aSAndrei Otcheretianski 			ieee80211_recalc_min_chandef(sta->sdata, -1);
219852cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
219952cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2200f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
220172f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2202f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
220317c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
220449ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2205f09603a2SJohannes Berg 		}
2206f09603a2SJohannes Berg 		break;
2207f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2208f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
220972f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2210f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
221117c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
221249ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2213f09603a2SJohannes Berg 		}
22143e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
22153e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
22163e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
22173e493173SJouni Malinen 						    sta->sta.addr);
2218f09603a2SJohannes Berg 		break;
2219f09603a2SJohannes Berg 	default:
2220f09603a2SJohannes Berg 		break;
2221f09603a2SJohannes Berg 	}
2222f09603a2SJohannes Berg 
2223d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2224d9a7ddb0SJohannes Berg 
2225d9a7ddb0SJohannes Berg 	return 0;
2226d9a7ddb0SJohannes Berg }
2227687da132SEmmanuel Grumbach 
2228c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2229c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2230c9c5962bSJohannes Berg {
2231046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2232c9c5962bSJohannes Berg 	int cpu;
2233c9c5962bSJohannes Berg 
2234046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2235c9c5962bSJohannes Berg 		return stats;
2236c9c5962bSJohannes Berg 
2237c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2238c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2239c9c5962bSJohannes Berg 
2240046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2241c9c5962bSJohannes Berg 
2242c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2243c9c5962bSJohannes Berg 			stats = cpustats;
2244c9c5962bSJohannes Berg 	}
2245c9c5962bSJohannes Berg 
2246c9c5962bSJohannes Berg 	return stats;
2247c9c5962bSJohannes Berg }
2248c9c5962bSJohannes Berg 
224941cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
22504f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2251fbd6ff5cSJohannes Berg {
2252dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2253fbd6ff5cSJohannes Berg 
2254dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
22557f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
22564f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2257dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2258dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2259dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2260dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22617f406cd1SJohannes Berg 		break;
22627f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
22634f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2264dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2265dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2266dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22677f406cd1SJohannes Berg 		break;
22687f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2269fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2270fbd6ff5cSJohannes Berg 		u16 brate;
22714f6b1b3dSJohannes Berg 		unsigned int shift;
2272dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2273dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2274fbd6ff5cSJohannes Berg 
2275dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
22768b783d10SThomas Pedersen 
22778b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
22788b783d10SThomas Pedersen 			break;
22798b783d10SThomas Pedersen 
2280dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
22814f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
22824f6b1b3dSJohannes Berg 			shift = 2;
22834f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
22844f6b1b3dSJohannes Berg 			shift = 1;
22854f6b1b3dSJohannes Berg 		else
22864f6b1b3dSJohannes Berg 			shift = 0;
2287fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
22887f406cd1SJohannes Berg 		break;
22897f406cd1SJohannes Berg 		}
229041cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
229141cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
229241cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
229341cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
229441cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
229541cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
229641cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
229741cbb0f5SLuca Coelho 		break;
2298fbd6ff5cSJohannes Berg 	}
22994f6b1b3dSJohannes Berg }
2300fbd6ff5cSJohannes Berg 
2301a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
23024f6b1b3dSJohannes Berg {
23036aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
23044f6b1b3dSJohannes Berg 
23054f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2306a17d93ffSBen Greear 		return -EINVAL;
2307a17d93ffSBen Greear 
23084f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2309a17d93ffSBen Greear 	return 0;
2310fbd6ff5cSJohannes Berg }
2311fbd6ff5cSJohannes Berg 
2312b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2313b255b72bSSeevalamuthu Mariappan 					int tid)
2314b255b72bSSeevalamuthu Mariappan {
2315b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2316b255b72bSSeevalamuthu Mariappan 	u64 value;
2317b255b72bSSeevalamuthu Mariappan 
2318b255b72bSSeevalamuthu Mariappan 	do {
2319b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2320b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2321b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2322b255b72bSSeevalamuthu Mariappan 
2323b255b72bSSeevalamuthu Mariappan 	return value;
2324b255b72bSSeevalamuthu Mariappan }
2325b255b72bSSeevalamuthu Mariappan 
23260f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
23270f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
23280f9c5a61SJohannes Berg 			     int tid)
23290f9c5a61SJohannes Berg {
23300f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2331b255b72bSSeevalamuthu Mariappan 	int cpu;
23320f9c5a61SJohannes Berg 
23330f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2334046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2335046d2e7cSSriram R 							   tid);
23360f9c5a61SJohannes Berg 
2337046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2338b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2339b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2340b255b72bSSeevalamuthu Mariappan 
2341046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2342046d2e7cSSriram R 						     cpu);
2343b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2344b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2345b255b72bSSeevalamuthu Mariappan 			}
2346b255b72bSSeevalamuthu Mariappan 		}
23470f9c5a61SJohannes Berg 
23480f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
23490f9c5a61SJohannes Berg 	}
23500f9c5a61SJohannes Berg 
23510f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
23520f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2353046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
23540f9c5a61SJohannes Berg 	}
23550f9c5a61SJohannes Berg 
23560f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
23570f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23580f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2359046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
23600f9c5a61SJohannes Berg 	}
23610f9c5a61SJohannes Berg 
23620f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
23630f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23640f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2365046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
23660f9c5a61SJohannes Berg 	}
23672fe4a29aSToke Høiland-Jørgensen 
23682fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
23692fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
23702fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
23712fe4a29aSToke Høiland-Jørgensen 
23722fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
23732fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
23742fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
23752fe4a29aSToke Høiland-Jørgensen 
23762fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
23772fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
23782fe4a29aSToke Høiland-Jørgensen 	}
23790f9c5a61SJohannes Berg }
23800f9c5a61SJohannes Berg 
2381c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2382c9c5962bSJohannes Berg {
2383c9c5962bSJohannes Berg 	unsigned int start;
2384c9c5962bSJohannes Berg 	u64 value;
2385c9c5962bSJohannes Berg 
2386c9c5962bSJohannes Berg 	do {
2387c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2388c9c5962bSJohannes Berg 		value = rxstats->bytes;
2389c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2390c9c5962bSJohannes Berg 
2391c9c5962bSJohannes Berg 	return value;
2392c9c5962bSJohannes Berg }
2393c9c5962bSJohannes Berg 
23940fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
23950fdf1493SJohannes Berg 		   bool tidstats)
2396b7ffbd7eSJohannes Berg {
2397b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2398b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2399b7ffbd7eSJohannes Berg 	u32 thr = 0;
2400c9c5962bSJohannes Berg 	int i, ac, cpu;
2401c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2402c9c5962bSJohannes Berg 
2403c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2404b7ffbd7eSJohannes Berg 
2405b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2406b7ffbd7eSJohannes Berg 
2407225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2408225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2409225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2410225b8189SJohannes Berg 	 */
2411225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2412bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2413225b8189SJohannes Berg 
24142b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2415a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2416a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2417a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2418a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
24199cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2420a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2421976bd9efSJohannes Berg 
2422976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2423bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2424bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2425a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2426976bd9efSJohannes Berg 	}
2427b7ffbd7eSJohannes Berg 
242884b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
24299cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2430e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2431b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
24322b9a7e1bSJohannes Berg 
2433a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2434a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2435b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
24362b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2437046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2438a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2439b7ffbd7eSJohannes Berg 	}
24402b9a7e1bSJohannes Berg 
2441a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
24422b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
24432b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2444046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2445a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
24462b9a7e1bSJohannes Berg 	}
24472b9a7e1bSJohannes Berg 
2448a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2449a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2450046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
24510f9c5a61SJohannes Berg 
2452046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2453c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2454c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2455c9c5962bSJohannes Berg 
2456046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2457046d2e7cSSriram R 						     cpu);
2458c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2459c9c5962bSJohannes Berg 			}
2460c9c5962bSJohannes Berg 		}
2461c9c5962bSJohannes Berg 
2462a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
24632b9a7e1bSJohannes Berg 	}
24642b9a7e1bSJohannes Berg 
2465a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2466046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2467046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2468c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2469c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2470c9c5962bSJohannes Berg 
2471046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2472046d2e7cSSriram R 						     cpu);
2473c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2474c9c5962bSJohannes Berg 			}
2475c9c5962bSJohannes Berg 		}
2476a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
24772b9a7e1bSJohannes Berg 	}
24782b9a7e1bSJohannes Berg 
2479a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2480046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2481a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
24822b9a7e1bSJohannes Berg 	}
24832b9a7e1bSJohannes Berg 
2484a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2485046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2486a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
24872b9a7e1bSJohannes Berg 	}
24882b9a7e1bSJohannes Berg 
2489b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2490b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2491b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2492b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2493b4809e94SToke Høiland-Jørgensen 	}
2494b4809e94SToke Høiland-Jørgensen 
2495b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2496b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2497b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2498b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2499b4809e94SToke Høiland-Jørgensen 	}
2500b4809e94SToke Høiland-Jørgensen 
2501b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2502942741daSFelix Fietkau 		sinfo->airtime_weight = sta->airtime_weight;
2503b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2504b4809e94SToke Høiland-Jørgensen 	}
2505b4809e94SToke Høiland-Jørgensen 
2506046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2507046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2508c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2509c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2510c9c5962bSJohannes Berg 
2511046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2512e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2513c9c5962bSJohannes Berg 		}
2514c9c5962bSJohannes Berg 	}
2515b7ffbd7eSJohannes Berg 
2516225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2517225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2518a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2519a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2520225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2521225b8189SJohannes Berg 	}
2522225b8189SJohannes Berg 
252330686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
252430686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2525a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2526c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2527a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2528b7ffbd7eSJohannes Berg 		}
25292b9a7e1bSJohannes Berg 
2530046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2531a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
253240d9a38aSJohannes Berg 			sinfo->signal_avg =
2533046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2534a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
25352b9a7e1bSJohannes Berg 		}
25362b9a7e1bSJohannes Berg 	}
25372b9a7e1bSJohannes Berg 
2538c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2539c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2540c9c5962bSJohannes Berg 	 * pcpu statistics
2541c9c5962bSJohannes Berg 	 */
2542c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2543a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2544a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2545a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2546046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2547a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2548b7ffbd7eSJohannes Berg 
2549c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2550c9c5962bSJohannes Berg 
2551b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2552e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2553c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2554b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2555046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2556b7ffbd7eSJohannes Berg 		}
2557b7ffbd7eSJohannes Berg 	}
2558b7ffbd7eSJohannes Berg 
25598a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
25608a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2561046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2562e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2563a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
25642b9a7e1bSJohannes Berg 	}
25652b9a7e1bSJohannes Berg 
25668a263dcbSJohannes Berg 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
25678a263dcbSJohannes Berg 	    !sta->sta.valid_links) {
2568a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2569a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
25702b9a7e1bSJohannes Berg 	}
2571b7ffbd7eSJohannes Berg 
25720fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
25736af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
25746af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
25758689c051SArend van Spriel 	}
257679c892b8SJohannes Berg 
2577b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2578b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2579a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2580a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2581a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2582a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2583a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2584dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
25851303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
25861303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2587b7ffbd7eSJohannes Berg 
2588433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2589433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2590433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2591b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2592a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2593433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2594b7ffbd7eSJohannes Berg 		}
2595433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2596433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2597433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2598dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
25991303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2600b7ffbd7eSJohannes Berg #endif
2601b7ffbd7eSJohannes Berg 	}
2602b7ffbd7eSJohannes Berg 
2603b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2604b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2605b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2606b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2607b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2608b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2609b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2610785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2611b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2612b7ffbd7eSJohannes Berg 
2613b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2614b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2615b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2616b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2617b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2618b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2619b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2620b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2621b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2622b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2623b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2624b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2625a74a8c84SJohannes Berg 	if (sta->sta.wme)
2626b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2627b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2628b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2629b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2630b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2631b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2632b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2633b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2634b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2635b7ffbd7eSJohannes Berg 
26363b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
26373b17fbf8SMaxim Altshul 
26383b17fbf8SMaxim Altshul 	if (thr != 0) {
2639a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
26403b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
26413b17fbf8SMaxim Altshul 	}
2642a78b26ffSVenkateswara Naralasetty 
2643a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2644046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2645046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2646a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2647a78b26ffSVenkateswara Naralasetty 	}
2648cc60dbbfSBalaji Pothunoori 
26499c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2650046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2651cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2652cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2653046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2654cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
26559c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2656cc60dbbfSBalaji Pothunoori 	}
2657ab60633cSNarayanraddi Masti 
2658ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2659ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2660ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2661ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2662ab60633cSNarayanraddi Masti 	}
26633b17fbf8SMaxim Altshul }
26643b17fbf8SMaxim Altshul 
26653b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
26663b17fbf8SMaxim Altshul {
26673b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
26683b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
26693b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
26703b17fbf8SMaxim Altshul 	u32 thr = 0;
26713b17fbf8SMaxim Altshul 
26723b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
26733b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
26743b17fbf8SMaxim Altshul 
2675b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2676b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2677b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2678b7ffbd7eSJohannes Berg 	else
26794fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2680b7ffbd7eSJohannes Berg 
26813b17fbf8SMaxim Altshul 	return thr;
2682b7ffbd7eSJohannes Berg }
2683b8da6b6aSJohannes Berg 
2684b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2685b8da6b6aSJohannes Berg {
2686c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2687c9c5962bSJohannes Berg 
2688046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2689046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2690c9c5962bSJohannes Berg 		return stats->last_rx;
2691046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2692b8da6b6aSJohannes Berg }
2693484a54c2SToke Høiland-Jørgensen 
2694484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2695484a54c2SToke Høiland-Jørgensen {
2696484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2697484a54c2SToke Høiland-Jørgensen 		return;
2698484a54c2SToke Høiland-Jørgensen 
2699484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2700484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2701484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2702484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2703484a54c2SToke Høiland-Jørgensen 	} else {
2704484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2705484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2706484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2707484a54c2SToke Høiland-Jørgensen 	}
2708484a54c2SToke Høiland-Jørgensen }
2709484a54c2SToke Høiland-Jørgensen 
2710484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2711484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2712484a54c2SToke Høiland-Jørgensen {
2713484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2714484a54c2SToke Høiland-Jørgensen 
2715484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2716484a54c2SToke Høiland-Jørgensen }
2717cb71f1d1SJohannes Berg 
2718cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2719cb71f1d1SJohannes Berg {
2720cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2721cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2722cb71f1d1SJohannes Berg 	int ret;
2723cb71f1d1SJohannes Berg 
2724cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2725cb71f1d1SJohannes Berg 
2726cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2727cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2728cb71f1d1SJohannes Berg 		return -EINVAL;
2729cb71f1d1SJohannes Berg 
2730cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2731cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2732cb71f1d1SJohannes Berg 		return -EBUSY;
2733cb71f1d1SJohannes Berg 
2734cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2735cb71f1d1SJohannes Berg 	if (!alloc)
2736cb71f1d1SJohannes Berg 		return -ENOMEM;
2737cb71f1d1SJohannes Berg 
2738cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2739cb71f1d1SJohannes Berg 	if (ret) {
2740cb71f1d1SJohannes Berg 		kfree(alloc);
2741cb71f1d1SJohannes Berg 		return ret;
2742cb71f1d1SJohannes Berg 	}
2743cb71f1d1SJohannes Berg 
2744cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2745cb71f1d1SJohannes Berg 
2746cb71f1d1SJohannes Berg 	return 0;
2747cb71f1d1SJohannes Berg }
2748cb71f1d1SJohannes Berg 
274921476ad1SShaul Triebitz void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
275021476ad1SShaul Triebitz {
275121476ad1SShaul Triebitz 	lockdep_assert_held(&sta->sdata->local->sta_mtx);
275221476ad1SShaul Triebitz 
275321476ad1SShaul Triebitz 	sta_remove_link(sta, link_id, false);
275421476ad1SShaul Triebitz }
275521476ad1SShaul Triebitz 
2756cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2757cb71f1d1SJohannes Berg {
2758cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2759c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2760cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2761cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2762cb71f1d1SJohannes Berg 	int ret;
2763cb71f1d1SJohannes Berg 
2764c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2765c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2766cb71f1d1SJohannes Berg 
2767c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2768cb71f1d1SJohannes Berg 		return -EINVAL;
2769cb71f1d1SJohannes Berg 
2770177577dbSJohannes Berg 	rcu_read_lock();
2771177577dbSJohannes Berg 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2772177577dbSJohannes Berg 		rcu_read_unlock();
2773177577dbSJohannes Berg 		return -EALREADY;
2774177577dbSJohannes Berg 	}
2775177577dbSJohannes Berg 	/* we only modify under the mutex so this is fine */
2776177577dbSJohannes Berg 	rcu_read_unlock();
2777177577dbSJohannes Berg 
2778cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2779cb71f1d1SJohannes Berg 
2780ba6ddab9SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_INSERTED)) {
2781ba6ddab9SJohannes Berg 		ret = 0;
2782ba6ddab9SJohannes Berg 		goto hash;
2783ba6ddab9SJohannes Berg 	}
2784cb71f1d1SJohannes Berg 
2785cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2786cb71f1d1SJohannes Berg 				   old_links, new_links);
2787cb71f1d1SJohannes Berg 	if (ret) {
2788cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2789ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2790177577dbSJohannes Berg 		return ret;
2791cb71f1d1SJohannes Berg 	}
2792cb71f1d1SJohannes Berg 
2793ba6ddab9SJohannes Berg hash:
2794177577dbSJohannes Berg 	ret = link_sta_info_hash_add(sdata->local, link_sta);
2795177577dbSJohannes Berg 	WARN_ON(ret);
2796177577dbSJohannes Berg 	return 0;
2797cb71f1d1SJohannes Berg }
2798cb71f1d1SJohannes Berg 
2799cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2800cb71f1d1SJohannes Berg {
2801cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2802*a8f62399SShaul Triebitz 	u16 old_links = sta->sta.valid_links;
2803cb71f1d1SJohannes Berg 
2804cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2805cb71f1d1SJohannes Berg 
2806cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2807cb71f1d1SJohannes Berg 
2808cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2809cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2810*a8f62399SShaul Triebitz 				     old_links, sta->sta.valid_links);
2811cb71f1d1SJohannes Berg 
2812ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2813cb71f1d1SJohannes Berg }
2814175ad2ecSJohannes Berg 
2815175ad2ecSJohannes Berg void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2816175ad2ecSJohannes Berg 					   const u8 *ext_capab,
2817175ad2ecSJohannes Berg 					   unsigned int ext_capab_len)
2818175ad2ecSJohannes Berg {
2819175ad2ecSJohannes Berg 	u8 val;
2820175ad2ecSJohannes Berg 
2821175ad2ecSJohannes Berg 	sta->sta.max_amsdu_subframes = 0;
2822175ad2ecSJohannes Berg 
2823175ad2ecSJohannes Berg 	if (ext_capab_len < 8)
2824175ad2ecSJohannes Berg 		return;
2825175ad2ecSJohannes Berg 
2826175ad2ecSJohannes Berg 	/* The sender might not have sent the last bit, consider it to be 0 */
2827175ad2ecSJohannes Berg 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
2828175ad2ecSJohannes Berg 
2829175ad2ecSJohannes Berg 	/* we did get all the bits, take the MSB as well */
2830175ad2ecSJohannes Berg 	if (ext_capab_len >= 9)
2831175ad2ecSJohannes Berg 		val |= u8_get_bits(ext_capab[8],
2832175ad2ecSJohannes Berg 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
2833175ad2ecSJohannes Berg 
2834175ad2ecSJohannes Berg 	if (val)
2835175ad2ecSJohannes Berg 		sta->sta.max_amsdu_subframes = 4 << val;
2836175ad2ecSJohannes Berg }
2837