xref: /openbmc/linux/net/mac80211/sta_info.c (revision ea32f065bd3e3e09f0bcb3042f1664caf6b3e233)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
50ead2510SEmmanuel Grumbach  * Copyright (C) 2015 Intel Deutschland GmbH
6f0706e82SJiri Benc  *
7f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
8f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
9f0706e82SJiri Benc  * published by the Free Software Foundation.
10f0706e82SJiri Benc  */
11f0706e82SJiri Benc 
12f0706e82SJiri Benc #include <linux/module.h>
13f0706e82SJiri Benc #include <linux/init.h>
14888d04dfSFelix Fietkau #include <linux/etherdevice.h>
15f0706e82SJiri Benc #include <linux/netdevice.h>
16f0706e82SJiri Benc #include <linux/types.h>
17f0706e82SJiri Benc #include <linux/slab.h>
18f0706e82SJiri Benc #include <linux/skbuff.h>
19f0706e82SJiri Benc #include <linux/if_arp.h>
200d174406SJohannes Berg #include <linux/timer.h>
21d0709a65SJohannes Berg #include <linux/rtnetlink.h>
22f0706e82SJiri Benc 
23f0706e82SJiri Benc #include <net/mac80211.h>
24f0706e82SJiri Benc #include "ieee80211_i.h"
2524487981SJohannes Berg #include "driver-ops.h"
262c8dccc7SJohannes Berg #include "rate.h"
27f0706e82SJiri Benc #include "sta_info.h"
28e9f207f0SJiri Benc #include "debugfs_sta.h"
29ee385855SLuis Carlos Cobo #include "mesh.h"
30ce662b44SJohannes Berg #include "wme.h"
31f0706e82SJiri Benc 
32d0709a65SJohannes Berg /**
33d0709a65SJohannes Berg  * DOC: STA information lifetime rules
34d0709a65SJohannes Berg  *
35d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
36d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
37d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
38d0709a65SJohannes Berg  *
3934e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
4034e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4134e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4234e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4334e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4434e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4534e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4634e89507SJohannes Berg  * encryption keys.
4793e5deb1SJohannes Berg  *
4893e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4993e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
50d0709a65SJohannes Berg  *
5134e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
527e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
537e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5425985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5534e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5625985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5734e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
587e189a12SLuis R. Rodriguez  *
5934e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
6034e89507SJohannes Berg  * calls are available.
61d0709a65SJohannes Berg  *
6234e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6334e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6434e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6534e89507SJohannes Berg  * freed before they are done using it.
66d0709a65SJohannes Berg  */
67f0706e82SJiri Benc 
687bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
697bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
70caf22d31SJohannes Berg 	.automatic_shrinking = true,
717bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
72ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
737bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
747bedd0cfSJohannes Berg 	.hashfn = sta_addr_hash,
75ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
767bedd0cfSJohannes Berg };
777bedd0cfSJohannes Berg 
784d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
79be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
80f0706e82SJiri Benc 			     struct sta_info *sta)
81f0706e82SJiri Benc {
827bedd0cfSJohannes Berg 	return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
837bedd0cfSJohannes Berg 				      sta_rht_params);
84f0706e82SJiri Benc }
85f0706e82SJiri Benc 
865108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
87b22cfcfcSEliad Peller {
88b22cfcfcSEliad Peller 	int ac, i;
89b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
90b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
91b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
92d012a605SMarco Porsch 	struct ps_data *ps;
93b22cfcfcSEliad Peller 
94e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
955ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
965ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
97d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
98d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
99d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1003f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1013f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
102d012a605SMarco Porsch 		else
103d012a605SMarco Porsch 			return;
104b22cfcfcSEliad Peller 
105b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
106e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1075ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
108b22cfcfcSEliad Peller 
109d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
110b22cfcfcSEliad Peller 	}
111b22cfcfcSEliad Peller 
112ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
113ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
114ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
115ba8c3d6fSFelix Fietkau 			int n = skb_queue_len(&txqi->queue);
116ba8c3d6fSFelix Fietkau 
117ba8c3d6fSFelix Fietkau 			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
118ba8c3d6fSFelix Fietkau 			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
119f2ac7e30SMichal Kazior 			txqi->byte_cnt = 0;
120ba8c3d6fSFelix Fietkau 		}
121ba8c3d6fSFelix Fietkau 	}
122ba8c3d6fSFelix Fietkau 
123b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1251f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1261f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
127b22cfcfcSEliad Peller 	}
128b22cfcfcSEliad Peller 
12945b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
13045b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
131b22cfcfcSEliad Peller 
1325ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
133b22cfcfcSEliad Peller 
134b22cfcfcSEliad Peller 	/*
135b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
136b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
137b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
138b22cfcfcSEliad Peller 	 * directly by station destruction.
139b22cfcfcSEliad Peller 	 */
1405a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
141661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
142b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
143b22cfcfcSEliad Peller 		if (!tid_tx)
144b22cfcfcSEliad Peller 			continue;
1451f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
146b22cfcfcSEliad Peller 		kfree(tid_tx);
147b22cfcfcSEliad Peller 	}
1485108ca82SJohannes Berg }
149b22cfcfcSEliad Peller 
1505108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1515108ca82SJohannes Berg {
1525108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1535108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1545108ca82SJohannes Berg 
1555108ca82SJohannes Berg 	__cleanup_single_sta(sta);
156b22cfcfcSEliad Peller 	sta_info_free(local, sta);
157b22cfcfcSEliad Peller }
158b22cfcfcSEliad Peller 
159d0709a65SJohannes Berg /* protected by RCU */
160abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
161abe60632SJohannes Berg 			      const u8 *addr)
16243ba7e95SJohannes Berg {
163abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16460f4b626SJohannes Berg 	struct sta_info *sta;
16560f4b626SJohannes Berg 	struct rhash_head *tmp;
16660f4b626SJohannes Berg 	const struct bucket_table *tbl;
16743ba7e95SJohannes Berg 
16860f4b626SJohannes Berg 	rcu_read_lock();
16960f4b626SJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
17060f4b626SJohannes Berg 
17160f4b626SJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
17260f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17360f4b626SJohannes Berg 			rcu_read_unlock();
17460f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17560f4b626SJohannes Berg 			 * another rcu read section or the mutex
17660f4b626SJohannes Berg 			 */
17760f4b626SJohannes Berg 			return sta;
17860f4b626SJohannes Berg 		}
17960f4b626SJohannes Berg 	}
18060f4b626SJohannes Berg 	rcu_read_unlock();
18160f4b626SJohannes Berg 	return NULL;
18243ba7e95SJohannes Berg }
18343ba7e95SJohannes Berg 
1840e5ded5aSFelix Fietkau /*
1850e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1860e5ded5aSFelix Fietkau  * or from one of its vlans
1870e5ded5aSFelix Fietkau  */
1880e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1890e5ded5aSFelix Fietkau 				  const u8 *addr)
1900e5ded5aSFelix Fietkau {
1910e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1920e5ded5aSFelix Fietkau 	struct sta_info *sta;
1937bedd0cfSJohannes Berg 	struct rhash_head *tmp;
1947bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
1950e5ded5aSFelix Fietkau 
1967bedd0cfSJohannes Berg 	rcu_read_lock();
1977bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1987bedd0cfSJohannes Berg 
1997bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
2007bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2017bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2027bedd0cfSJohannes Berg 			rcu_read_unlock();
2037bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2047bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2057bedd0cfSJohannes Berg 			 */
2060e5ded5aSFelix Fietkau 			return sta;
2070e5ded5aSFelix Fietkau 		}
2087bedd0cfSJohannes Berg 	}
2097bedd0cfSJohannes Berg 	rcu_read_unlock();
2107bedd0cfSJohannes Berg 	return NULL;
2117bedd0cfSJohannes Berg }
2120e5ded5aSFelix Fietkau 
2133b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2143b53fde8SJohannes Berg 				     int idx)
215ee385855SLuis Carlos Cobo {
2163b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
217ee385855SLuis Carlos Cobo 	struct sta_info *sta;
218ee385855SLuis Carlos Cobo 	int i = 0;
219ee385855SLuis Carlos Cobo 
220d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2213b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2222a8ca29aSLuis Carlos Cobo 			continue;
223ee385855SLuis Carlos Cobo 		if (i < idx) {
224ee385855SLuis Carlos Cobo 			++i;
225ee385855SLuis Carlos Cobo 			continue;
226ee385855SLuis Carlos Cobo 		}
2272a8ca29aSLuis Carlos Cobo 		return sta;
228ee385855SLuis Carlos Cobo 	}
229ee385855SLuis Carlos Cobo 
230ee385855SLuis Carlos Cobo 	return NULL;
231ee385855SLuis Carlos Cobo }
232f0706e82SJiri Benc 
23393e5deb1SJohannes Berg /**
234d9a7ddb0SJohannes Berg  * sta_info_free - free STA
23593e5deb1SJohannes Berg  *
2366ef307bcSRandy Dunlap  * @local: pointer to the global information
23793e5deb1SJohannes Berg  * @sta: STA info to free
23893e5deb1SJohannes Berg  *
23993e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
240d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
241d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
242d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
24393e5deb1SJohannes Berg  */
244d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
24593e5deb1SJohannes Berg {
246889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2474b7679a5SJohannes Berg 		rate_control_free_sta(sta);
24893e5deb1SJohannes Berg 
249bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
25093e5deb1SJohannes Berg 
251ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
252ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
25353d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
254433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
255433f5bc1SJohannes Berg 	kfree(sta->mesh);
256433f5bc1SJohannes Berg #endif
25793e5deb1SJohannes Berg 	kfree(sta);
25893e5deb1SJohannes Berg }
25993e5deb1SJohannes Berg 
2604d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
261d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
262d0709a65SJohannes Berg 			      struct sta_info *sta)
263f0706e82SJiri Benc {
2647bedd0cfSJohannes Berg 	rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
2657bedd0cfSJohannes Berg 			       sta_rht_params);
266f0706e82SJiri Benc }
267f0706e82SJiri Benc 
2685ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
269af818581SJohannes Berg {
270af818581SJohannes Berg 	struct sta_info *sta;
271af818581SJohannes Berg 
2725ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
273af818581SJohannes Berg 
274af818581SJohannes Berg 	if (sta->dead)
275af818581SJohannes Berg 		return;
276af818581SJohannes Berg 
27754420473SHelmut Schaa 	local_bh_disable();
2785ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
279af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2805ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
281af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2825ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
28347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
284ce662b44SJohannes Berg 	local_bh_enable();
285af818581SJohannes Berg }
286af818581SJohannes Berg 
287af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
288af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
289af65cd96SJohannes Berg {
29030686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
291af65cd96SJohannes Berg 		return 0;
292af65cd96SJohannes Berg 
293889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
294af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29535c347acSJohannes Berg 						     sta, gfp);
296889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
297af65cd96SJohannes Berg 		return -ENOMEM;
298af65cd96SJohannes Berg 
299af65cd96SJohannes Berg 	return 0;
300af65cd96SJohannes Berg }
301af65cd96SJohannes Berg 
30273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30356544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
304f0706e82SJiri Benc {
305d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
306ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
307f0706e82SJiri Benc 	struct sta_info *sta;
30816c5f15cSRon Rindjunsky 	int i;
309f0706e82SJiri Benc 
310ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
311f0706e82SJiri Benc 	if (!sta)
31273651ee6SJohannes Berg 		return NULL;
313f0706e82SJiri Benc 
31407346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3151d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3165ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
31767c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
318a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
31987f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
320433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
321433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
322433f5bc1SJohannes Berg 		if (!sta->mesh)
323433f5bc1SJohannes Berg 			goto free;
324433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
32587f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
32687f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
327433f5bc1SJohannes Berg 			init_timer(&sta->mesh->plink_timer);
328433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
329433f5bc1SJohannes Berg 	}
33087f59c70SThomas Pedersen #endif
33107346f81SJohannes Berg 
332ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
33317741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
334d0709a65SJohannes Berg 	sta->local = local;
335d0709a65SJohannes Berg 	sta->sdata = sdata;
336e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
337f0706e82SJiri Benc 
33871ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
33971ec375cSJohannes Berg 
340b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
341b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
342b6da911bSLiad Kaufman 
34384b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
344e5a9f8d0SJohannes Berg 	ewma_signal_init(&sta->rx_stats.avg_signal);
345e5a9f8d0SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++)
346e5a9f8d0SJohannes Berg 		ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]);
347541a45a1SBruno Randolf 
348ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
349ba8c3d6fSFelix Fietkau 		void *txq_data;
350ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
351ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
352ba8c3d6fSFelix Fietkau 
353ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
354ba8c3d6fSFelix Fietkau 		if (!txq_data)
355ba8c3d6fSFelix Fietkau 			goto free;
356ba8c3d6fSFelix Fietkau 
357ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
358ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
359ba8c3d6fSFelix Fietkau 
360ba8c3d6fSFelix Fietkau 			ieee80211_init_tx_queue(sdata, sta, txq, i);
361abfbc3afSJohannes Berg 		}
362ba8c3d6fSFelix Fietkau 	}
363ba8c3d6fSFelix Fietkau 
364ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
365ba8c3d6fSFelix Fietkau 		goto free_txq;
366f0706e82SJiri Benc 
3675a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
368a622ab72SJohannes Berg 		/*
369a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
370a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
371a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
372a622ab72SJohannes Berg 		 */
37316c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
37416c5f15cSRon Rindjunsky 	}
375948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
376948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
377948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
378948d887dSJohannes Berg 	}
37973651ee6SJohannes Berg 
3805a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3814be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
382cccaec98SSenthil Balasubramanian 
383af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
384687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
385687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
386687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
387ba8c3d6fSFelix Fietkau 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
388687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
389687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
390687da132SEmmanuel Grumbach 		/*
391687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
392687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
393687da132SEmmanuel Grumbach 		 */
394687da132SEmmanuel Grumbach 		switch (smps) {
395687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
396687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
397687da132SEmmanuel Grumbach 			break;
398687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
399687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
400687da132SEmmanuel Grumbach 			break;
401687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
402687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
403687da132SEmmanuel Grumbach 			break;
404687da132SEmmanuel Grumbach 		default:
405687da132SEmmanuel Grumbach 			WARN_ON(1);
406687da132SEmmanuel Grumbach 		}
407687da132SEmmanuel Grumbach 	}
408af0ed69bSJohannes Berg 
409bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
410ef04a297SJohannes Berg 
411abfbc3afSJohannes Berg 	return sta;
412ba8c3d6fSFelix Fietkau 
413ba8c3d6fSFelix Fietkau free_txq:
414ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
415ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
416ba8c3d6fSFelix Fietkau free:
417433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
418433f5bc1SJohannes Berg 	kfree(sta->mesh);
419433f5bc1SJohannes Berg #endif
420ba8c3d6fSFelix Fietkau 	kfree(sta);
421ba8c3d6fSFelix Fietkau 	return NULL;
42273651ee6SJohannes Berg }
42373651ee6SJohannes Berg 
4248c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
42534e89507SJohannes Berg {
42634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42734e89507SJohannes Berg 
42803e4497eSJohannes Berg 	/*
42903e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
43003e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
43103e4497eSJohannes Berg 	 * and another CPU turns off the net device.
43203e4497eSJohannes Berg 	 */
4338c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4348c71df7aSGuy Eilam 		return -ENETDOWN;
43503e4497eSJohannes Berg 
436b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4378c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4388c71df7aSGuy Eilam 		return -EINVAL;
4398c71df7aSGuy Eilam 
44031104891SJohannes Berg 	/* Strictly speaking this isn't necessary as we hold the mutex, but
44131104891SJohannes Berg 	 * the rhashtable code can't really deal with that distinction. We
44231104891SJohannes Berg 	 * do require the mutex for correctness though.
44331104891SJohannes Berg 	 */
44431104891SJohannes Berg 	rcu_read_lock();
44531104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
44631104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
44731104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
44831104891SJohannes Berg 		rcu_read_unlock();
44931104891SJohannes Berg 		return -ENOTUNIQ;
45031104891SJohannes Berg 	}
45131104891SJohannes Berg 	rcu_read_unlock();
45231104891SJohannes Berg 
4538c71df7aSGuy Eilam 	return 0;
45493e5deb1SJohannes Berg }
45544213b5eSJohannes Berg 
456f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
457f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
458f09603a2SJohannes Berg 				     struct sta_info *sta)
459f09603a2SJohannes Berg {
460f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
461f09603a2SJohannes Berg 	int err = 0;
462f09603a2SJohannes Berg 
463f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
464f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
465f09603a2SJohannes Berg 		if (err)
466f09603a2SJohannes Berg 			break;
467f09603a2SJohannes Berg 	}
468f09603a2SJohannes Berg 
469f09603a2SJohannes Berg 	if (!err) {
470a4ec45a4SJohannes Berg 		/*
471a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
472a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
473a4ec45a4SJohannes Berg 		 */
474a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
475f09603a2SJohannes Berg 			sta->uploaded = true;
476f09603a2SJohannes Berg 		return 0;
477f09603a2SJohannes Berg 	}
478f09603a2SJohannes Berg 
479f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
480bdcbd8e0SJohannes Berg 		sdata_info(sdata,
481bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
482bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
483f09603a2SJohannes Berg 		err = 0;
484f09603a2SJohannes Berg 	}
485f09603a2SJohannes Berg 
486f09603a2SJohannes Berg 	/* unwind on error */
487f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
488f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
489f09603a2SJohannes Berg 
490f09603a2SJohannes Berg 	return err;
491f09603a2SJohannes Berg }
492f09603a2SJohannes Berg 
49334e89507SJohannes Berg /*
4948c71df7aSGuy Eilam  * should be called with sta_mtx locked
4958c71df7aSGuy Eilam  * this function replaces the mutex lock
4968c71df7aSGuy Eilam  * with a RCU lock
4978c71df7aSGuy Eilam  */
4984d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4998c71df7aSGuy Eilam {
5008c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5018c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5020ef049dcSArnd Bergmann 	struct station_info *sinfo;
5038c71df7aSGuy Eilam 	int err = 0;
5048c71df7aSGuy Eilam 
5058c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
5068c71df7aSGuy Eilam 
5070ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
5080ef049dcSArnd Bergmann 	if (!sinfo) {
5090ef049dcSArnd Bergmann 		err = -ENOMEM;
5100ef049dcSArnd Bergmann 		goto out_err;
5110ef049dcSArnd Bergmann 	}
5120ef049dcSArnd Bergmann 
5137852e361SJohannes Berg 	/* check if STA exists already */
5147852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
5154d33960bSJohannes Berg 		err = -EEXIST;
5164d33960bSJohannes Berg 		goto out_err;
51734e89507SJohannes Berg 	}
51834e89507SJohannes Berg 
5194d33960bSJohannes Berg 	local->num_sta++;
5204d33960bSJohannes Berg 	local->sta_generation++;
5214d33960bSJohannes Berg 	smp_mb();
5224d33960bSJohannes Berg 
5235108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5245108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5255108ca82SJohannes Berg 
5264d33960bSJohannes Berg 	/* make the station visible */
5274d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5284d33960bSJohannes Berg 
5292bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
53083d5cc01SJohannes Berg 
5315108ca82SJohannes Berg 	/* notify driver */
5325108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5335108ca82SJohannes Berg 	if (err)
5345108ca82SJohannes Berg 		goto out_remove;
5355108ca82SJohannes Berg 
53683d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5375108ca82SJohannes Berg 	/* accept BA sessions now */
5385108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5394d33960bSJohannes Berg 
5404d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5414d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5424d33960bSJohannes Berg 
5430ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
5440ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
5450ef049dcSArnd Bergmann 	kfree(sinfo);
546d0709a65SJohannes Berg 
547bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
548f0706e82SJiri Benc 
54934e89507SJohannes Berg 	/* move reference to rcu-protected */
55034e89507SJohannes Berg 	rcu_read_lock();
55134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
552e9f207f0SJiri Benc 
55373651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
55473651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
55573651ee6SJohannes Berg 
55673651ee6SJohannes Berg 	return 0;
5575108ca82SJohannes Berg  out_remove:
5585108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5595108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5605108ca82SJohannes Berg 	local->num_sta--;
5615108ca82SJohannes Berg 	synchronize_net();
5625108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5634d33960bSJohannes Berg  out_err:
5644d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
565*ea32f065SSudip Mukherjee 	kfree(sinfo);
5664d33960bSJohannes Berg 	rcu_read_lock();
5674d33960bSJohannes Berg 	return err;
5688c71df7aSGuy Eilam }
5698c71df7aSGuy Eilam 
5708c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5718c71df7aSGuy Eilam {
5728c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
573308f7fcfSZhao, Gang 	int err;
5748c71df7aSGuy Eilam 
5754d33960bSJohannes Berg 	might_sleep();
5764d33960bSJohannes Berg 
57731104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
57831104891SJohannes Berg 
5798c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5808c71df7aSGuy Eilam 	if (err) {
58131104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
5828c71df7aSGuy Eilam 		rcu_read_lock();
5838c71df7aSGuy Eilam 		goto out_free;
5848c71df7aSGuy Eilam 	}
5858c71df7aSGuy Eilam 
5864d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5878c71df7aSGuy Eilam 	if (err)
588004c872eSJohannes Berg 		goto out_free;
589d0709a65SJohannes Berg 
590004c872eSJohannes Berg 	return 0;
59193e5deb1SJohannes Berg  out_free:
592d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
59393e5deb1SJohannes Berg 	return err;
594f0706e82SJiri Benc }
595f0706e82SJiri Benc 
59634e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
59734e89507SJohannes Berg {
59834e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
59934e89507SJohannes Berg 
60034e89507SJohannes Berg 	rcu_read_unlock();
60134e89507SJohannes Berg 
60234e89507SJohannes Berg 	return err;
60334e89507SJohannes Berg }
60434e89507SJohannes Berg 
605d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
606004c872eSJohannes Berg {
607004c872eSJohannes Berg 	/*
608004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
609004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
610004c872eSJohannes Berg 	 */
611d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
612004c872eSJohannes Berg }
613004c872eSJohannes Berg 
614d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
615004c872eSJohannes Berg {
616004c872eSJohannes Berg 	/*
617004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
618004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
619004c872eSJohannes Berg 	 */
620d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
621004c872eSJohannes Berg }
622004c872eSJohannes Berg 
6233d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6243d5839b6SIlan Peer {
6253d5839b6SIlan Peer 	/*
6263d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6273d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6283d5839b6SIlan Peer 	 */
6293d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6303d5839b6SIlan Peer }
6313d5839b6SIlan Peer 
632948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
633004c872eSJohannes Berg {
634948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
635948d887dSJohannes Berg 	switch (ac) {
636948d887dSJohannes Berg 	case IEEE80211_AC_VO:
637948d887dSJohannes Berg 		return BIT(6) | BIT(7);
638948d887dSJohannes Berg 	case IEEE80211_AC_VI:
639948d887dSJohannes Berg 		return BIT(4) | BIT(5);
640948d887dSJohannes Berg 	case IEEE80211_AC_BE:
641948d887dSJohannes Berg 		return BIT(0) | BIT(3);
642948d887dSJohannes Berg 	case IEEE80211_AC_BK:
643948d887dSJohannes Berg 		return BIT(1) | BIT(2);
644948d887dSJohannes Berg 	default:
645948d887dSJohannes Berg 		WARN_ON(1);
646948d887dSJohannes Berg 		return 0;
647d0709a65SJohannes Berg 	}
648004c872eSJohannes Berg }
649004c872eSJohannes Berg 
6509b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
651004c872eSJohannes Berg {
652c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
653d012a605SMarco Porsch 	struct ps_data *ps;
654948d887dSJohannes Berg 	bool indicate_tim = false;
655948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
656948d887dSJohannes Berg 	int ac;
657a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
658004c872eSJohannes Berg 
659d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
660d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
661c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
662c868cb35SJohannes Berg 			return;
6633e122be0SJohannes Berg 
664d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
6653f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6663f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6673f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
6683f52b7e3SMarco Porsch #endif
669d012a605SMarco Porsch 	} else {
670d012a605SMarco Porsch 		return;
671d012a605SMarco Porsch 	}
672d012a605SMarco Porsch 
673c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
67430686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
675c868cb35SJohannes Berg 		return;
676004c872eSJohannes Berg 
677c868cb35SJohannes Berg 	if (sta->dead)
678c868cb35SJohannes Berg 		goto done;
6793e122be0SJohannes Berg 
680948d887dSJohannes Berg 	/*
681948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
682948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
683948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
684948d887dSJohannes Berg 	 * non-enabled ones.
685948d887dSJohannes Berg 	 */
686948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
687948d887dSJohannes Berg 		ignore_for_tim = 0;
688948d887dSJohannes Berg 
6899b7a86f3SJohannes Berg 	if (ignore_pending)
6909b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6919b7a86f3SJohannes Berg 
692948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
693948d887dSJohannes Berg 		unsigned long tids;
694948d887dSJohannes Berg 
695948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
696948d887dSJohannes Berg 			continue;
697948d887dSJohannes Berg 
698948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
699948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
700948d887dSJohannes Berg 		if (indicate_tim)
701948d887dSJohannes Berg 			break;
702948d887dSJohannes Berg 
703948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
704948d887dSJohannes Berg 
705948d887dSJohannes Berg 		indicate_tim |=
706948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
707ba8c3d6fSFelix Fietkau 		indicate_tim |=
708ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
709004c872eSJohannes Berg 	}
710004c872eSJohannes Berg 
711c868cb35SJohannes Berg  done:
71265f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
713004c872eSJohannes Berg 
7143d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
7153d5839b6SIlan Peer 		goto out_unlock;
7163d5839b6SIlan Peer 
717948d887dSJohannes Berg 	if (indicate_tim)
718d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
719c868cb35SJohannes Berg 	else
720d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7213e122be0SJohannes Berg 
7229b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
723c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
724948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
725c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
726004c872eSJohannes Berg 	}
727004c872eSJohannes Berg 
7283d5839b6SIlan Peer out_unlock:
72965f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
730004c872eSJohannes Berg }
731004c872eSJohannes Berg 
7329b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7339b7a86f3SJohannes Berg {
7349b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7359b7a86f3SJohannes Berg }
7369b7a86f3SJohannes Berg 
737cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
738f0706e82SJiri Benc {
739e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
740f0706e82SJiri Benc 	int timeout;
741f0706e82SJiri Benc 
742f0706e82SJiri Benc 	if (!skb)
743cd0b8d89SJohannes Berg 		return false;
744f0706e82SJiri Benc 
745e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
746f0706e82SJiri Benc 
747f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
74857c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
74957c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
75057c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
751f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
752f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
753e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
754f0706e82SJiri Benc }
755f0706e82SJiri Benc 
756f0706e82SJiri Benc 
757948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
758948d887dSJohannes Berg 						struct sta_info *sta, int ac)
759f0706e82SJiri Benc {
760f0706e82SJiri Benc 	unsigned long flags;
761f0706e82SJiri Benc 	struct sk_buff *skb;
762f0706e82SJiri Benc 
76360750397SJohannes Berg 	/*
76460750397SJohannes Berg 	 * First check for frames that should expire on the filtered
76560750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
76660750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
76760750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
76860750397SJohannes Berg 	 * total_ps_buffered counter.
76960750397SJohannes Berg 	 */
770f0706e82SJiri Benc 	for (;;) {
771948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
772948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
77357c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
774948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
775836341a7SJohannes Berg 		else
776f0706e82SJiri Benc 			skb = NULL;
777948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
778f0706e82SJiri Benc 
77960750397SJohannes Berg 		/*
78060750397SJohannes Berg 		 * Frames are queued in order, so if this one
78160750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
78260750397SJohannes Berg 		 * we actually reached the end of the queue we
78360750397SJohannes Berg 		 * also need to stop, of course.
78460750397SJohannes Berg 		 */
78560750397SJohannes Berg 		if (!skb)
78660750397SJohannes Berg 			break;
787d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
78860750397SJohannes Berg 	}
78960750397SJohannes Berg 
79060750397SJohannes Berg 	/*
79160750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
79260750397SJohannes Berg 	 * only find something if the filtered queue was emptied
79360750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
79460750397SJohannes Berg 	 * buffered frames.
79560750397SJohannes Berg 	 */
796f0706e82SJiri Benc 	for (;;) {
797948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
798948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
799f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
800948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
801f0706e82SJiri Benc 		else
802f0706e82SJiri Benc 			skb = NULL;
803948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
804f0706e82SJiri Benc 
80560750397SJohannes Berg 		/*
80660750397SJohannes Berg 		 * frames are queued in order, so if this one
80760750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
80860750397SJohannes Berg 		 * the queue) we can stop testing
80960750397SJohannes Berg 		 */
810836341a7SJohannes Berg 		if (!skb)
811836341a7SJohannes Berg 			break;
812836341a7SJohannes Berg 
813f0706e82SJiri Benc 		local->total_ps_buffered--;
814bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
815bdcbd8e0SJohannes Berg 		       sta->sta.addr);
816d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
817f0706e82SJiri Benc 	}
8183393a608SJuuso Oikarinen 
81960750397SJohannes Berg 	/*
82060750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
82160750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
82260750397SJohannes Berg 	 * frames.
82360750397SJohannes Berg 	 */
82460750397SJohannes Berg 	sta_info_recalc_tim(sta);
82560750397SJohannes Berg 
82660750397SJohannes Berg 	/*
82760750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
82860750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
82960750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
83060750397SJohannes Berg 	 */
831948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
832948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
833948d887dSJohannes Berg }
834948d887dSJohannes Berg 
835948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
836948d887dSJohannes Berg 					     struct sta_info *sta)
837948d887dSJohannes Berg {
838948d887dSJohannes Berg 	bool have_buffered = false;
839948d887dSJohannes Berg 	int ac;
840948d887dSJohannes Berg 
8413f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8423f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8433f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
844948d887dSJohannes Berg 		return false;
845948d887dSJohannes Berg 
846948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
847948d887dSJohannes Berg 		have_buffered |=
848948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
849948d887dSJohannes Berg 
850948d887dSJohannes Berg 	return have_buffered;
851f0706e82SJiri Benc }
852f0706e82SJiri Benc 
853d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
85434e89507SJohannes Berg {
85534e89507SJohannes Berg 	struct ieee80211_local *local;
85634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8576d10e46bSJohannes Berg 	int ret;
85834e89507SJohannes Berg 
85934e89507SJohannes Berg 	might_sleep();
86034e89507SJohannes Berg 
86134e89507SJohannes Berg 	if (!sta)
86234e89507SJohannes Berg 		return -ENOENT;
86334e89507SJohannes Berg 
86434e89507SJohannes Berg 	local = sta->local;
86534e89507SJohannes Berg 	sdata = sta->sdata;
86634e89507SJohannes Berg 
86783d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
86883d5cc01SJohannes Berg 
869098a6070SJohannes Berg 	/*
870098a6070SJohannes Berg 	 * Before removing the station from the driver and
871098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
872098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
873098a6070SJohannes Berg 	 * will be sufficient.
874098a6070SJohannes Berg 	 */
875c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
876c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
877098a6070SJohannes Berg 
87834e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
879b01711beSJohannes Berg 	if (WARN_ON(ret))
88034e89507SJohannes Berg 		return ret;
88134e89507SJohannes Berg 
882a7a6bdd0SArik Nemtsov 	/*
883a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
884a7a6bdd0SArik Nemtsov 	 * removal.
885a7a6bdd0SArik Nemtsov 	 */
886a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
887a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
888a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
889a7a6bdd0SArik Nemtsov 	}
890a7a6bdd0SArik Nemtsov 
891794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
892ef044763SEliad Peller 	sta->removed = true;
8934d33960bSJohannes Berg 
8946a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8956a9d1b91SJohannes Berg 
896a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
897a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
898a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
899a710c816SJohannes Berg 
900d778207bSJohannes Berg 	return 0;
901d778207bSJohannes Berg }
902d778207bSJohannes Berg 
903d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
904d778207bSJohannes Berg {
905d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
906d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
9070ef049dcSArnd Bergmann 	struct station_info *sinfo;
908d778207bSJohannes Berg 	int ret;
909d778207bSJohannes Berg 
910d778207bSJohannes Berg 	/*
911d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
912d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
913d778207bSJohannes Berg 	 */
914d778207bSJohannes Berg 
915d778207bSJohannes Berg 	might_sleep();
916d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
917d778207bSJohannes Berg 
918c8782078SJohannes Berg 	/* now keys can no longer be reached */
9196d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
92034e89507SJohannes Berg 
9219b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9229b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9239b7a86f3SJohannes Berg 
92434e89507SJohannes Berg 	sta->dead = true;
92534e89507SJohannes Berg 
92634e89507SJohannes Berg 	local->num_sta--;
92734e89507SJohannes Berg 	local->sta_generation++;
92834e89507SJohannes Berg 
92983d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
930f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
931f09603a2SJohannes Berg 		if (ret) {
93283d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
93383d5cc01SJohannes Berg 			break;
93483d5cc01SJohannes Berg 		}
93583d5cc01SJohannes Berg 	}
936d9a7ddb0SJohannes Berg 
937f09603a2SJohannes Berg 	if (sta->uploaded) {
938f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
939f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
940f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
941f09603a2SJohannes Berg 	}
94234e89507SJohannes Berg 
943bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
944bdcbd8e0SJohannes Berg 
9450ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
9460ef049dcSArnd Bergmann 	if (sinfo)
9470ef049dcSArnd Bergmann 		sta_set_sinfo(sta, sinfo);
9480ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
9490ef049dcSArnd Bergmann 	kfree(sinfo);
950ec15e68bSJouni Malinen 
95134e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
95234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
95334e89507SJohannes Berg 
954d34ba216SJohannes Berg 	cleanup_single_sta(sta);
955d778207bSJohannes Berg }
956d778207bSJohannes Berg 
957d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
958d778207bSJohannes Berg {
959d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
960d778207bSJohannes Berg 
961d778207bSJohannes Berg 	if (err)
962d778207bSJohannes Berg 		return err;
963d778207bSJohannes Berg 
964d778207bSJohannes Berg 	synchronize_net();
965d778207bSJohannes Berg 
966d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
96734e89507SJohannes Berg 
96834e89507SJohannes Berg 	return 0;
96934e89507SJohannes Berg }
97034e89507SJohannes Berg 
97134e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
97234e89507SJohannes Berg {
97334e89507SJohannes Berg 	struct sta_info *sta;
97434e89507SJohannes Berg 	int ret;
97534e89507SJohannes Berg 
97634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9777852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
97834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
97934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
98034e89507SJohannes Berg 
98134e89507SJohannes Berg 	return ret;
98234e89507SJohannes Berg }
98334e89507SJohannes Berg 
98434e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
98534e89507SJohannes Berg 			      const u8 *addr)
98634e89507SJohannes Berg {
98734e89507SJohannes Berg 	struct sta_info *sta;
98834e89507SJohannes Berg 	int ret;
98934e89507SJohannes Berg 
99034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9917852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
99234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
99334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
99434e89507SJohannes Berg 
99534e89507SJohannes Berg 	return ret;
99634e89507SJohannes Berg }
997f0706e82SJiri Benc 
998f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
999f0706e82SJiri Benc {
1000f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
1001f0706e82SJiri Benc 	struct sta_info *sta;
10023393a608SJuuso Oikarinen 	bool timer_needed = false;
1003f0706e82SJiri Benc 
1004d0709a65SJohannes Berg 	rcu_read_lock();
1005d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
10063393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
10073393a608SJuuso Oikarinen 			timer_needed = true;
1008d0709a65SJohannes Berg 	rcu_read_unlock();
1009f0706e82SJiri Benc 
10105bb644a0SJohannes Berg 	if (local->quiescing)
10115bb644a0SJohannes Berg 		return;
10125bb644a0SJohannes Berg 
10133393a608SJuuso Oikarinen 	if (!timer_needed)
10143393a608SJuuso Oikarinen 		return;
10153393a608SJuuso Oikarinen 
101626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
101726d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1018f0706e82SJiri Benc }
1019f0706e82SJiri Benc 
10207bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1021f0706e82SJiri Benc {
10227bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
10237bedd0cfSJohannes Berg }
10247bedd0cfSJohannes Berg 
10257bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10267bedd0cfSJohannes Berg {
10277bedd0cfSJohannes Berg 	int err;
10287bedd0cfSJohannes Berg 
10297bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
10307bedd0cfSJohannes Berg 	if (err)
10317bedd0cfSJohannes Berg 		return err;
10327bedd0cfSJohannes Berg 
10334d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
103434e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1035f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1036f0706e82SJiri Benc 
1037b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1038b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10397bedd0cfSJohannes Berg 	return 0;
1040f0706e82SJiri Benc }
1041f0706e82SJiri Benc 
1042f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1043f0706e82SJiri Benc {
1044a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
10457bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
1046f0706e82SJiri Benc }
1047f0706e82SJiri Benc 
1048051007d9SJohannes Berg 
1049e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1050f0706e82SJiri Benc {
1051b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1052f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1053d778207bSJohannes Berg 	LIST_HEAD(free_list);
105444213b5eSJohannes Berg 	int ret = 0;
1055f0706e82SJiri Benc 
1056d0709a65SJohannes Berg 	might_sleep();
1057d0709a65SJohannes Berg 
1058e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1059e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1060e716251dSJohannes Berg 
106134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
106234e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1063e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1064e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1065d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1066d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
106734316837SJohannes Berg 			ret++;
106834316837SJohannes Berg 		}
106934e89507SJohannes Berg 	}
1070d778207bSJohannes Berg 
1071d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1072d778207bSJohannes Berg 		synchronize_net();
1073d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1074d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1075d778207bSJohannes Berg 	}
107634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
107744213b5eSJohannes Berg 
1078051007d9SJohannes Berg 	return ret;
1079051007d9SJohannes Berg }
1080051007d9SJohannes Berg 
108124723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
108224723d1bSJohannes Berg 			  unsigned long exp_time)
108324723d1bSJohannes Berg {
108424723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
108524723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
108624723d1bSJohannes Berg 
108734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1088e46a2cf9SMohammed Shafi Shajakhan 
1089e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1090ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1091ec2b774eSMarek Lindner 			continue;
1092ec2b774eSMarek Lindner 
1093e5a9f8d0SJohannes Berg 		if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
1094eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1095bdcbd8e0SJohannes Berg 				sta->sta.addr);
10963f52b7e3SMarco Porsch 
10973f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10983f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10993f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
11003f52b7e3SMarco Porsch 
110134e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
110224723d1bSJohannes Berg 		}
1103e46a2cf9SMohammed Shafi Shajakhan 	}
1104e46a2cf9SMohammed Shafi Shajakhan 
110534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
110624723d1bSJohannes Berg }
110717741cdcSJohannes Berg 
1108686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1109686b9cb9SBen Greear 						   const u8 *addr,
1110686b9cb9SBen Greear 						   const u8 *localaddr)
111117741cdcSJohannes Berg {
11127bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
11137bedd0cfSJohannes Berg 	struct sta_info *sta;
11147bedd0cfSJohannes Berg 	struct rhash_head *tmp;
11157bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
11167bedd0cfSJohannes Berg 
11177bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
111817741cdcSJohannes Berg 
1119686b9cb9SBen Greear 	/*
1120686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1121686b9cb9SBen Greear 	 * ... first in list.
1122686b9cb9SBen Greear 	 */
11237bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1124686b9cb9SBen Greear 		if (localaddr &&
1125b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1126686b9cb9SBen Greear 			continue;
1127f7c65594SJohannes Berg 		if (!sta->uploaded)
1128f7c65594SJohannes Berg 			return NULL;
112917741cdcSJohannes Berg 		return &sta->sta;
1130f7c65594SJohannes Berg 	}
1131f7c65594SJohannes Berg 
1132abe60632SJohannes Berg 	return NULL;
113317741cdcSJohannes Berg }
1134686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11355ed176e1SJohannes Berg 
11365ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11375ed176e1SJohannes Berg 					 const u8 *addr)
11385ed176e1SJohannes Berg {
1139f7c65594SJohannes Berg 	struct sta_info *sta;
11405ed176e1SJohannes Berg 
11415ed176e1SJohannes Berg 	if (!vif)
11425ed176e1SJohannes Berg 		return NULL;
11435ed176e1SJohannes Berg 
1144f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1145f7c65594SJohannes Berg 	if (!sta)
1146f7c65594SJohannes Berg 		return NULL;
11475ed176e1SJohannes Berg 
1148f7c65594SJohannes Berg 	if (!sta->uploaded)
1149f7c65594SJohannes Berg 		return NULL;
1150f7c65594SJohannes Berg 
1151f7c65594SJohannes Berg 	return &sta->sta;
11525ed176e1SJohannes Berg }
115317741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1154af818581SJohannes Berg 
1155e3685e03SJohannes Berg /* powersave support code */
1156e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
115750a9432dSJohannes Berg {
1158608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1159e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1160e3685e03SJohannes Berg 	struct sk_buff_head pending;
1161ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1162e3685e03SJohannes Berg 	unsigned long flags;
1163d012a605SMarco Porsch 	struct ps_data *ps;
1164d012a605SMarco Porsch 
11653918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11663918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11673918edb0SFelix Fietkau 				     u.ap);
11683918edb0SFelix Fietkau 
11693918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1170d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11713f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11723f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1173d012a605SMarco Porsch 	else
1174d012a605SMarco Porsch 		return;
117550a9432dSJohannes Berg 
1176c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
117747086fc5SJohannes Berg 
11785a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1179948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1180ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1181948d887dSJohannes Berg 
118230686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
118312375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1184af818581SJohannes Berg 
1185ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1186ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1187ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1188ba8c3d6fSFelix Fietkau 
1189ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1190ba8c3d6fSFelix Fietkau 				continue;
1191ba8c3d6fSFelix Fietkau 
1192ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1193ba8c3d6fSFelix Fietkau 		}
1194ba8c3d6fSFelix Fietkau 	}
1195ba8c3d6fSFelix Fietkau 
1196948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1197af818581SJohannes Berg 
11981d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11991d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1200af818581SJohannes Berg 	/* Send all buffered frames to the station */
1201948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1202948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1203948d887dSJohannes Berg 
1204987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1205948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1206987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1207948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1208948d887dSJohannes Berg 		filtered += tmp - count;
1209948d887dSJohannes Berg 		count = tmp;
1210948d887dSJohannes Berg 
1211987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1212948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1213987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1214948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1215948d887dSJohannes Berg 		buffered += tmp - count;
1216948d887dSJohannes Berg 	}
1217948d887dSJohannes Berg 
1218e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
12195ac2e350SJohannes Berg 
12205ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
12215ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
12225ac2e350SJohannes Berg 
12235ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12245ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12255ac2e350SJohannes Berg 	 */
12265ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12275ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12281d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1229948d887dSJohannes Berg 
1230e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1231e3685e03SJohannes Berg 
1232687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1233062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1234062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1235687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1236687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1237687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1238687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1239687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1240687da132SEmmanuel Grumbach 		       sta->sta.addr);
1241687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1242687da132SEmmanuel Grumbach 					   sta->sta.addr,
1243687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1244687da132SEmmanuel Grumbach 	}
1245687da132SEmmanuel Grumbach 
1246af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1247af818581SJohannes Berg 
1248c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1249c868cb35SJohannes Berg 
1250bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1251bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1252bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
125317c18bf8SJohannes Berg 
125417c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1255af818581SJohannes Berg }
1256af818581SJohannes Berg 
12570ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1258b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
12590ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1260ce662b44SJohannes Berg {
12610ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1262ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1263ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1264ce662b44SJohannes Berg 	struct sk_buff *skb;
1265ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1266ce662b44SJohannes Berg 	__le16 fc;
1267a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1268ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
126955de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1270ce662b44SJohannes Berg 
1271ce662b44SJohannes Berg 	if (qos) {
1272ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1273ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1274ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1275ce662b44SJohannes Berg 	} else {
1276ce662b44SJohannes Berg 		size -= 2;
1277ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1278ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1279ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1280ce662b44SJohannes Berg 	}
1281ce662b44SJohannes Berg 
1282ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1283ce662b44SJohannes Berg 	if (!skb)
1284ce662b44SJohannes Berg 		return;
1285ce662b44SJohannes Berg 
1286ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1287ce662b44SJohannes Berg 
1288ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1289ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1290ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1291ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1292ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1293ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1294864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1295ce662b44SJohannes Berg 
1296ce662b44SJohannes Berg 	skb->priority = tid;
1297ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
129859b66255SJohannes Berg 	if (qos) {
1299ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1300ce662b44SJohannes Berg 
13010ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1302ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1303ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
13040ead2510SEmmanuel Grumbach 			if (more_data)
13050ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
13060ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
13070ead2510SEmmanuel Grumbach 		}
1308ce662b44SJohannes Berg 	}
1309ce662b44SJohannes Berg 
1310ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1311ce662b44SJohannes Berg 
1312ce662b44SJohannes Berg 	/*
1313ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1314ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1315deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1316deeaee19SJohannes Berg 	 * ends the poll/service period.
1317ce662b44SJohannes Berg 	 */
131802f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1319deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1320ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1321ce662b44SJohannes Berg 
13226b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
13236b127c71SSujith Manoharan 
1324b77cf4f8SJohannes Berg 	if (call_driver)
1325b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1326b77cf4f8SJohannes Berg 					  reason, false);
132740b96408SJohannes Berg 
132889afe614SJohannes Berg 	skb->dev = sdata->dev;
132989afe614SJohannes Berg 
133055de908aSJohannes Berg 	rcu_read_lock();
133155de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
133255de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
133355de908aSJohannes Berg 		rcu_read_unlock();
133455de908aSJohannes Berg 		kfree_skb(skb);
133555de908aSJohannes Berg 		return;
133655de908aSJohannes Berg 	}
133755de908aSJohannes Berg 
133873c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13397c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
134055de908aSJohannes Berg 	rcu_read_unlock();
1341ce662b44SJohannes Berg }
1342ce662b44SJohannes Berg 
13430a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13440a1cb809SJohannes Berg {
13450a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13460a1cb809SJohannes Berg 	if (tids & 0xF8)
13470a1cb809SJohannes Berg 		return fls(tids) - 1;
13480a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13490a1cb809SJohannes Berg 	if (tids & BIT(0))
13500a1cb809SJohannes Berg 		return 0;
13510a1cb809SJohannes Berg 	return fls(tids) - 1;
13520a1cb809SJohannes Berg }
13530a1cb809SJohannes Berg 
13540ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
13550ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
13560ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
13570ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
13580ead2510SEmmanuel Grumbach  */
13590ead2510SEmmanuel Grumbach static bool
13600ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
13610ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
13620ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
13630ead2510SEmmanuel Grumbach {
13640ead2510SEmmanuel Grumbach 	int ac;
13650ead2510SEmmanuel Grumbach 
13660ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
13670ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
13680ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
13690ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
13700ead2510SEmmanuel Grumbach 	 */
13710ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
13720ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
13730ead2510SEmmanuel Grumbach 		return true;
13740ead2510SEmmanuel Grumbach 
13750ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13760ead2510SEmmanuel Grumbach 		if (ignored_acs & BIT(ac))
13770ead2510SEmmanuel Grumbach 			continue;
13780ead2510SEmmanuel Grumbach 
13790ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
13800ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
13810ead2510SEmmanuel Grumbach 			return true;
13820ead2510SEmmanuel Grumbach 	}
13830ead2510SEmmanuel Grumbach 
13840ead2510SEmmanuel Grumbach 	return false;
13850ead2510SEmmanuel Grumbach }
13860ead2510SEmmanuel Grumbach 
138747086fc5SJohannes Berg static void
13880ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
13890ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
13900ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
13910ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1392af818581SJohannes Berg {
1393af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1394af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1395948d887dSJohannes Berg 	int ac;
1396af818581SJohannes Berg 
1397f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1398948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13994049e09aSJohannes Berg 		unsigned long tids;
14004049e09aSJohannes Berg 
140147086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1402948d887dSJohannes Berg 			continue;
1403948d887dSJohannes Berg 
14044049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
14054049e09aSJohannes Berg 
1406f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1407f9f760b4SJohannes Berg 		 * release from hardware queues
1408f9f760b4SJohannes Berg 		 */
14090ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
14100ead2510SEmmanuel Grumbach 			*driver_release_tids |=
14110ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
14120ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1413ba8c3d6fSFelix Fietkau 		}
1414f9f760b4SJohannes Berg 
14150ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
141647086fc5SJohannes Berg 			struct sk_buff *skb;
141747086fc5SJohannes Berg 
141847086fc5SJohannes Berg 			while (n_frames > 0) {
1419948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1420948d887dSJohannes Berg 				if (!skb) {
142147086fc5SJohannes Berg 					skb = skb_dequeue(
142247086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1423af818581SJohannes Berg 					if (skb)
1424af818581SJohannes Berg 						local->total_ps_buffered--;
1425af818581SJohannes Berg 				}
142647086fc5SJohannes Berg 				if (!skb)
142747086fc5SJohannes Berg 					break;
142847086fc5SJohannes Berg 				n_frames--;
14290ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
143047086fc5SJohannes Berg 			}
1431948d887dSJohannes Berg 		}
1432948d887dSJohannes Berg 
14330ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
14340ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
14350ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
14364049e09aSJohannes Berg 		 */
1437948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14380ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1439948d887dSJohannes Berg 			break;
1440948d887dSJohannes Berg 	}
1441948d887dSJohannes Berg }
1442af818581SJohannes Berg 
14430ead2510SEmmanuel Grumbach static void
14440ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
14450ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
14460ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
14470ead2510SEmmanuel Grumbach {
14480ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
14490ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
14500ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
14510ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
14520ead2510SEmmanuel Grumbach 	bool more_data;
14530ead2510SEmmanuel Grumbach 
14540ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
14550ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
14560ead2510SEmmanuel Grumbach 
14570ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
14580ead2510SEmmanuel Grumbach 
14590ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
14600ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
14610ead2510SEmmanuel Grumbach 
14620ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
14630ead2510SEmmanuel Grumbach 
14640ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL)
14650ead2510SEmmanuel Grumbach 		driver_release_tids =
14660ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
14670ead2510SEmmanuel Grumbach 
1468f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1469ce662b44SJohannes Berg 		int tid;
14704049e09aSJohannes Berg 
1471ce662b44SJohannes Berg 		/*
1472ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1473ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1474ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1475ce662b44SJohannes Berg 		 *
1476ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1477ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1478ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1479ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1480ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1481ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1482ce662b44SJohannes Berg 		 *
1483ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1484ce662b44SJohannes Berg 		 */
1485ce662b44SJohannes Berg 
1486ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1487ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1488ce662b44SJohannes Berg 
14890ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1490f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
149147086fc5SJohannes Berg 		struct sk_buff_head pending;
149247086fc5SJohannes Berg 		struct sk_buff *skb;
149340b96408SJohannes Berg 		int num = 0;
149440b96408SJohannes Berg 		u16 tids = 0;
1495b77cf4f8SJohannes Berg 		bool need_null = false;
149647086fc5SJohannes Berg 
149747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
149847086fc5SJohannes Berg 
149947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1500af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
150147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
150240b96408SJohannes Berg 			u8 *qoshdr = NULL;
150340b96408SJohannes Berg 
150440b96408SJohannes Berg 			num++;
1505af818581SJohannes Berg 
1506af818581SJohannes Berg 			/*
150747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
150847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
150947086fc5SJohannes Berg 			 * exchange.
1510af818581SJohannes Berg 			 */
15116b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
15126b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1513af818581SJohannes Berg 
151447086fc5SJohannes Berg 			/*
151547086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
151647086fc5SJohannes Berg 			 * more buffered frames for this STA
151747086fc5SJohannes Berg 			 */
151824b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
151947086fc5SJohannes Berg 				hdr->frame_control |=
152047086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
152124b9c373SJanusz.Dziedzic@tieto.com 			else
152224b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
152324b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1524af818581SJohannes Berg 
152540b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
152640b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
152740b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
152840b96408SJohannes Berg 
1529b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1530b77cf4f8SJohannes Berg 
1531b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1532b77cf4f8SJohannes Berg 
1533b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1534b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1535b77cf4f8SJohannes Berg 				continue;
1536b77cf4f8SJohannes Berg 
1537b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1538b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1539b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1540b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1541b77cf4f8SJohannes Berg 				break;
1542b77cf4f8SJohannes Berg 			}
1543b77cf4f8SJohannes Berg 
1544b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1545b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1546b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1547b77cf4f8SJohannes Berg 			 * and be done.
1548b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1549b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1550b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1551b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1552b77cf4f8SJohannes Berg 			 *
1553b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1554b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1555b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1556b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1557b77cf4f8SJohannes Berg 			 *
1558b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1559b77cf4f8SJohannes Berg 			 */
1560b77cf4f8SJohannes Berg 			if (qoshdr) {
156140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1562deeaee19SJohannes Berg 
156347086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
156447086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1565b77cf4f8SJohannes Berg 			} else {
1566b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1567b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1568b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1569b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1570b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1571b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1572b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1573b77cf4f8SJohannes Berg 				 */
1574b77cf4f8SJohannes Berg 				hdr->frame_control |=
1575b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1576b77cf4f8SJohannes Berg 				need_null = true;
1577b77cf4f8SJohannes Berg 				num++;
157852a3f20cSMarco Porsch 			}
1579b77cf4f8SJohannes Berg 			break;
158047086fc5SJohannes Berg 		}
158147086fc5SJohannes Berg 
158240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
158340b96408SJohannes Berg 					  reason, more_data);
158440b96408SJohannes Berg 
158547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1586af818581SJohannes Berg 
1587b77cf4f8SJohannes Berg 		if (need_null)
1588b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
15890ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
15900ead2510SEmmanuel Grumbach 				reason, false, false);
1591b77cf4f8SJohannes Berg 
1592c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1593af818581SJohannes Berg 	} else {
1594ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1595ba8c3d6fSFelix Fietkau 		int tid;
1596ba8c3d6fSFelix Fietkau 
1597af818581SJohannes Berg 		/*
15984049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
15994049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1600f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1601f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1602f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1603f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1604f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1605f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1606af818581SJohannes Berg 		 */
16074049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
160847086fc5SJohannes Berg 					    n_frames, reason, more_data);
16094049e09aSJohannes Berg 
16104049e09aSJohannes Berg 		/*
16114049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
16124049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1613f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
16144049e09aSJohannes Berg 		 * release function.
1615f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1616ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1617ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
16184049e09aSJohannes Berg 		 */
1619ba8c3d6fSFelix Fietkau 
1620ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1621ba8c3d6fSFelix Fietkau 			return;
1622ba8c3d6fSFelix Fietkau 
1623ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1624ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1625ba8c3d6fSFelix Fietkau 
1626ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1627ba8c3d6fSFelix Fietkau 				continue;
1628ba8c3d6fSFelix Fietkau 
1629ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1630ba8c3d6fSFelix Fietkau 			break;
1631ba8c3d6fSFelix Fietkau 		}
1632af818581SJohannes Berg 	}
1633af818581SJohannes Berg }
1634af818581SJohannes Berg 
1635af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1636af818581SJohannes Berg {
163747086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1638af818581SJohannes Berg 
1639af818581SJohannes Berg 	/*
164047086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
164147086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
164247086fc5SJohannes Berg 	 * only from the non-enabled ones.
1643af818581SJohannes Berg 	 */
164447086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
164547086fc5SJohannes Berg 		ignore_for_response = 0;
1646af818581SJohannes Berg 
164747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
164847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1649af818581SJohannes Berg }
165047086fc5SJohannes Berg 
165147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
165247086fc5SJohannes Berg {
165347086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
165447086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
165547086fc5SJohannes Berg 
165647086fc5SJohannes Berg 	/*
165747086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
165847086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
165947086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
166047086fc5SJohannes Berg 	 * actually getting called.
166147086fc5SJohannes Berg 	 */
166247086fc5SJohannes Berg 	if (!delivery_enabled)
166347086fc5SJohannes Berg 		return;
166447086fc5SJohannes Berg 
166547086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
166647086fc5SJohannes Berg 	case 1:
166747086fc5SJohannes Berg 		n_frames = 2;
166847086fc5SJohannes Berg 		break;
166947086fc5SJohannes Berg 	case 2:
167047086fc5SJohannes Berg 		n_frames = 4;
167147086fc5SJohannes Berg 		break;
167247086fc5SJohannes Berg 	case 3:
167347086fc5SJohannes Berg 		n_frames = 6;
167447086fc5SJohannes Berg 		break;
167547086fc5SJohannes Berg 	case 0:
167647086fc5SJohannes Berg 		/* XXX: what is a good value? */
167713a8098aSAndrei Otcheretianski 		n_frames = 128;
167847086fc5SJohannes Berg 		break;
167947086fc5SJohannes Berg 	}
168047086fc5SJohannes Berg 
168147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
168247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1683af818581SJohannes Berg }
1684af818581SJohannes Berg 
1685af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1686af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1687af818581SJohannes Berg {
1688af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1689af818581SJohannes Berg 
1690b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1691b5878a2dSJohannes Berg 
16925ac2e350SJohannes Berg 	if (block) {
1693c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
169417c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
16955ac2e350SJohannes Berg 		return;
16965ac2e350SJohannes Berg 	}
16975ac2e350SJohannes Berg 
16985ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
16995ac2e350SJohannes Berg 		return;
17005ac2e350SJohannes Berg 
17015ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
17025ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
17035ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17045ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17055ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
17065ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
17075ac2e350SJohannes Berg 		/* must be asleep in this case */
17085ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17095ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17105ac2e350SJohannes Berg 	} else {
17115ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
171217c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
17135ac2e350SJohannes Berg 	}
1714af818581SJohannes Berg }
1715af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1716dcf55fb5SFelix Fietkau 
1717e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
171837fbd908SJohannes Berg {
171937fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
172037fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
172137fbd908SJohannes Berg 
172237fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
172337fbd908SJohannes Berg 
172437fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
172537fbd908SJohannes Berg }
1726e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
172737fbd908SJohannes Berg 
17280ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
17290ead2510SEmmanuel Grumbach {
17300ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
17310ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
17320ead2510SEmmanuel Grumbach 	bool more_data;
17330ead2510SEmmanuel Grumbach 
17340ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
17350ead2510SEmmanuel Grumbach 
17360ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
17370ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
17380ead2510SEmmanuel Grumbach 					       reason, 0);
17390ead2510SEmmanuel Grumbach 
17400ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
17410ead2510SEmmanuel Grumbach }
17420ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
17430ead2510SEmmanuel Grumbach 
1744042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1745042ec453SJohannes Berg 				u8 tid, bool buffered)
1746dcf55fb5SFelix Fietkau {
1747dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1748dcf55fb5SFelix Fietkau 
17495a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1750042ec453SJohannes Berg 		return;
1751042ec453SJohannes Berg 
17521b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
17531b000789SJohannes Berg 
1754948d887dSJohannes Berg 	if (buffered)
1755948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1756948d887dSJohannes Berg 	else
1757948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1758948d887dSJohannes Berg 
1759c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1760dcf55fb5SFelix Fietkau }
1761042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1762d9a7ddb0SJohannes Berg 
176383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1764d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1765d9a7ddb0SJohannes Berg {
17668bf11d8dSJohannes Berg 	might_sleep();
1767d9a7ddb0SJohannes Berg 
1768d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1769d9a7ddb0SJohannes Berg 		return 0;
1770d9a7ddb0SJohannes Berg 
1771f09603a2SJohannes Berg 	/* check allowed transitions first */
1772f09603a2SJohannes Berg 
1773d9a7ddb0SJohannes Berg 	switch (new_state) {
1774d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1775f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1776d9a7ddb0SJohannes Berg 			return -EINVAL;
1777d9a7ddb0SJohannes Berg 		break;
1778d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1779f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1780f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1781d9a7ddb0SJohannes Berg 			return -EINVAL;
1782d9a7ddb0SJohannes Berg 		break;
1783d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1784f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1785f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1786d9a7ddb0SJohannes Berg 			return -EINVAL;
1787d9a7ddb0SJohannes Berg 		break;
1788d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1789f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1790d9a7ddb0SJohannes Berg 			return -EINVAL;
1791d9a7ddb0SJohannes Berg 		break;
1792d9a7ddb0SJohannes Berg 	default:
1793d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1794d9a7ddb0SJohannes Berg 		return -EINVAL;
1795d9a7ddb0SJohannes Berg 	}
1796d9a7ddb0SJohannes Berg 
1797bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1798bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1799f09603a2SJohannes Berg 
1800f09603a2SJohannes Berg 	/*
1801f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1802f09603a2SJohannes Berg 	 * fail the transition
1803f09603a2SJohannes Berg 	 */
1804f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1805f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1806f09603a2SJohannes Berg 					sta->sta_state, new_state);
1807f09603a2SJohannes Berg 		if (err)
1808f09603a2SJohannes Berg 			return err;
1809f09603a2SJohannes Berg 	}
1810f09603a2SJohannes Berg 
1811f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1812f09603a2SJohannes Berg 
1813f09603a2SJohannes Berg 	switch (new_state) {
1814f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1815f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1816f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1817f09603a2SJohannes Berg 		break;
1818f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1819a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
1820f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1821a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1822f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1823a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
1824a7201a6cSIlan Peer 		}
1825f09603a2SJohannes Berg 		break;
1826f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1827f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1828f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1829a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
1830f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
18317e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
18327e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
18337e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
18347e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1835f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
183617c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
1837f09603a2SJohannes Berg 		}
1838f09603a2SJohannes Berg 		break;
1839f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1840f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
18417e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
18427e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
18437e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
18447e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1845f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
184617c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
1847f09603a2SJohannes Berg 		}
1848f09603a2SJohannes Berg 		break;
1849f09603a2SJohannes Berg 	default:
1850f09603a2SJohannes Berg 		break;
1851f09603a2SJohannes Berg 	}
1852f09603a2SJohannes Berg 
1853d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1854d9a7ddb0SJohannes Berg 
1855d9a7ddb0SJohannes Berg 	return 0;
1856d9a7ddb0SJohannes Berg }
1857687da132SEmmanuel Grumbach 
1858687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1859687da132SEmmanuel Grumbach {
1860687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1861687da132SEmmanuel Grumbach 	u8 rx_streams;
1862687da132SEmmanuel Grumbach 
1863687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1864687da132SEmmanuel Grumbach 		return 1;
1865687da132SEmmanuel Grumbach 
1866687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1867687da132SEmmanuel Grumbach 		int i;
1868687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1869687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1870687da132SEmmanuel Grumbach 
1871687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1872687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1873687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1874687da132SEmmanuel Grumbach 				return i + 1;
1875687da132SEmmanuel Grumbach 	}
1876687da132SEmmanuel Grumbach 
1877687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1878687da132SEmmanuel Grumbach 		rx_streams = 4;
1879687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1880687da132SEmmanuel Grumbach 		rx_streams = 3;
1881687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1882687da132SEmmanuel Grumbach 		rx_streams = 2;
1883687da132SEmmanuel Grumbach 	else
1884687da132SEmmanuel Grumbach 		rx_streams = 1;
1885687da132SEmmanuel Grumbach 
1886687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1887687da132SEmmanuel Grumbach 		return rx_streams;
1888687da132SEmmanuel Grumbach 
1889687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1890687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1891687da132SEmmanuel Grumbach }
1892b7ffbd7eSJohannes Berg 
1893fbd6ff5cSJohannes Berg static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1894fbd6ff5cSJohannes Berg {
1895fbd6ff5cSJohannes Berg 	rinfo->flags = 0;
1896fbd6ff5cSJohannes Berg 
1897e5a9f8d0SJohannes Berg 	if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
1898fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
1899e5a9f8d0SJohannes Berg 		rinfo->mcs = sta->rx_stats.last_rate_idx;
1900e5a9f8d0SJohannes Berg 	} else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
1901fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
1902e5a9f8d0SJohannes Berg 		rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1903e5a9f8d0SJohannes Berg 		rinfo->mcs = sta->rx_stats.last_rate_idx;
1904fbd6ff5cSJohannes Berg 	} else {
1905fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
1906fbd6ff5cSJohannes Berg 		int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1907fbd6ff5cSJohannes Berg 		u16 brate;
1908fbd6ff5cSJohannes Berg 
1909fbd6ff5cSJohannes Berg 		sband = sta->local->hw.wiphy->bands[
1910fbd6ff5cSJohannes Berg 				ieee80211_get_sdata_band(sta->sdata)];
1911e5a9f8d0SJohannes Berg 		brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
1912fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1913fbd6ff5cSJohannes Berg 	}
1914fbd6ff5cSJohannes Berg 
1915e5a9f8d0SJohannes Berg 	if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
1916fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1917fbd6ff5cSJohannes Berg 
1918e5a9f8d0SJohannes Berg 	if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
1919fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_5;
1920e5a9f8d0SJohannes Berg 	else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
1921fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_10;
1922e5a9f8d0SJohannes Berg 	else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
1923fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
1924e5a9f8d0SJohannes Berg 	else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
1925fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
1926e5a9f8d0SJohannes Berg 	else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
1927fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
1928fbd6ff5cSJohannes Berg 	else
1929fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
1930fbd6ff5cSJohannes Berg }
1931fbd6ff5cSJohannes Berg 
1932b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1933b7ffbd7eSJohannes Berg {
1934b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1935b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
19369a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1937b7ffbd7eSJohannes Berg 	u32 thr = 0;
1938b7ffbd7eSJohannes Berg 	int i, ac;
1939b7ffbd7eSJohannes Berg 
19409a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
19419a244409SJohn W. Linville 		ref = local->rate_ctrl;
19429a244409SJohn W. Linville 
1943b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1944b7ffbd7eSJohannes Berg 
1945225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1946225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1947225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1948225b8189SJohannes Berg 	 */
1949225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1950225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1951225b8189SJohannes Berg 
19522b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
19532b9a7e1bSJohannes Berg 
1954319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1955319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1956319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1957319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1958976bd9efSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1959976bd9efSJohannes Berg 
1960976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1961976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1962976bd9efSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1963976bd9efSJohannes Berg 	}
1964b7ffbd7eSJohannes Berg 
196584b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
1966e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
1967e5a9f8d0SJohannes Berg 		jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
19682b9a7e1bSJohannes Berg 
1969319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1970319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1971b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
19722b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1973e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
1974319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1975b7ffbd7eSJohannes Berg 	}
19762b9a7e1bSJohannes Berg 
1977319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
19782b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
19792b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1980e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
1981319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
19822b9a7e1bSJohannes Berg 	}
19832b9a7e1bSJohannes Berg 
1984319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1985319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1986e5a9f8d0SJohannes Berg 		sinfo->rx_bytes = sta->rx_stats.bytes;
1987319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
19882b9a7e1bSJohannes Berg 	}
19892b9a7e1bSJohannes Berg 
1990319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1991e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
1992319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
19932b9a7e1bSJohannes Berg 	}
19942b9a7e1bSJohannes Berg 
1995319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1996e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
1997319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
19982b9a7e1bSJohannes Berg 	}
19992b9a7e1bSJohannes Berg 
2000319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2001e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
2002319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
20032b9a7e1bSJohannes Berg 	}
20042b9a7e1bSJohannes Berg 
2005e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2006b7ffbd7eSJohannes Berg 
2007225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2008225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2009225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2010225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2011225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2012225b8189SJohannes Berg 	}
2013225b8189SJohannes Berg 
201430686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
201530686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2016319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2017e5a9f8d0SJohannes Berg 			sinfo->signal = (s8)sta->rx_stats.last_signal;
2018319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2019b7ffbd7eSJohannes Berg 		}
20202b9a7e1bSJohannes Berg 
2021319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
202240d9a38aSJohannes Berg 			sinfo->signal_avg =
2023e5a9f8d0SJohannes Berg 				-ewma_signal_read(&sta->rx_stats.avg_signal);
2024319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
20252b9a7e1bSJohannes Berg 		}
20262b9a7e1bSJohannes Berg 	}
20272b9a7e1bSJohannes Berg 
2028e5a9f8d0SJohannes Berg 	if (sta->rx_stats.chains &&
2029319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2030319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2031319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2032319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2033b7ffbd7eSJohannes Berg 
2034e5a9f8d0SJohannes Berg 		sinfo->chains = sta->rx_stats.chains;
2035b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2036e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2037e5a9f8d0SJohannes Berg 				sta->rx_stats.chain_signal_last[i];
2038b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2039e5a9f8d0SJohannes Berg 				-ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]);
2040b7ffbd7eSJohannes Berg 		}
2041b7ffbd7eSJohannes Berg 	}
2042b7ffbd7eSJohannes Berg 
2043319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2044e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2045e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2046319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
20472b9a7e1bSJohannes Berg 	}
20482b9a7e1bSJohannes Berg 
2049319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2050b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
2051319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
20522b9a7e1bSJohannes Berg 	}
2053b7ffbd7eSJohannes Berg 
205479c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
205579c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
205679c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
205779c892b8SJohannes Berg 
205879c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
205979c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2060e5a9f8d0SJohannes Berg 			tidstats->rx_msdu = sta->rx_stats.msdu[i];
206179c892b8SJohannes Berg 		}
206279c892b8SJohannes Berg 
206379c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
206479c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2065e5a9f8d0SJohannes Berg 			tidstats->tx_msdu = sta->tx_stats.msdu[i];
206679c892b8SJohannes Berg 		}
206779c892b8SJohannes Berg 
206879c892b8SJohannes Berg 		if (!(tidstats->filled &
206979c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
207030686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
207179c892b8SJohannes Berg 			tidstats->filled |=
207279c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2073e5a9f8d0SJohannes Berg 			tidstats->tx_msdu_retries =
2074e5a9f8d0SJohannes Berg 				sta->status_stats.msdu_retries[i];
207579c892b8SJohannes Berg 		}
207679c892b8SJohannes Berg 
207779c892b8SJohannes Berg 		if (!(tidstats->filled &
207879c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
207930686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
208079c892b8SJohannes Berg 			tidstats->filled |=
208179c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2082e5a9f8d0SJohannes Berg 			tidstats->tx_msdu_failed =
2083e5a9f8d0SJohannes Berg 				sta->status_stats.msdu_failed[i];
208479c892b8SJohannes Berg 		}
208579c892b8SJohannes Berg 	}
208679c892b8SJohannes Berg 
2087b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2088b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2089319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2090319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
2091319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
2092319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
2093319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
2094319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
2095b7ffbd7eSJohannes Berg 
2096433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2097433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2098433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2099b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2100319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2101433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2102b7ffbd7eSJohannes Berg 		}
2103433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2104433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2105433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2106b7ffbd7eSJohannes Berg #endif
2107b7ffbd7eSJohannes Berg 	}
2108b7ffbd7eSJohannes Berg 
2109b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2110b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2111b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2112b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2113b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2114b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2115b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2116785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2117b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2118b7ffbd7eSJohannes Berg 
2119b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2120b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2121b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2122b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2123b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2124b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2125b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2126b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2127b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2128b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2129b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2130b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2131a74a8c84SJohannes Berg 	if (sta->sta.wme)
2132b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2133b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2134b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2135b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2136b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2137b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2138b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2139b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2140b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2141b7ffbd7eSJohannes Berg 
2142b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2143b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2144b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2145b7ffbd7eSJohannes Berg 	else
2146b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
2147b7ffbd7eSJohannes Berg 
2148b7ffbd7eSJohannes Berg 	if (thr != 0) {
2149319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2150b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
2151b7ffbd7eSJohannes Berg 	}
2152b7ffbd7eSJohannes Berg }
2153