xref: /openbmc/linux/net/mac80211/sta_info.c (revision 433f5bc1c0efc67a86433e47a14b115510fc1409)
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
5f0706e82SJiri Benc  *
6f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
7f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
8f0706e82SJiri Benc  * published by the Free Software Foundation.
9f0706e82SJiri Benc  */
10f0706e82SJiri Benc 
11f0706e82SJiri Benc #include <linux/module.h>
12f0706e82SJiri Benc #include <linux/init.h>
13888d04dfSFelix Fietkau #include <linux/etherdevice.h>
14f0706e82SJiri Benc #include <linux/netdevice.h>
15f0706e82SJiri Benc #include <linux/types.h>
16f0706e82SJiri Benc #include <linux/slab.h>
17f0706e82SJiri Benc #include <linux/skbuff.h>
18f0706e82SJiri Benc #include <linux/if_arp.h>
190d174406SJohannes Berg #include <linux/timer.h>
20d0709a65SJohannes Berg #include <linux/rtnetlink.h>
21f0706e82SJiri Benc 
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
677bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
687bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
69caf22d31SJohannes Berg 	.automatic_shrinking = true,
707bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
71ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
727bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
737bedd0cfSJohannes Berg 	.hashfn = sta_addr_hash,
74ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
757bedd0cfSJohannes Berg };
767bedd0cfSJohannes Berg 
774d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
78be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
79f0706e82SJiri Benc 			     struct sta_info *sta)
80f0706e82SJiri Benc {
817bedd0cfSJohannes Berg 	return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
827bedd0cfSJohannes Berg 				      sta_rht_params);
83f0706e82SJiri Benc }
84f0706e82SJiri Benc 
855108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
86b22cfcfcSEliad Peller {
87b22cfcfcSEliad Peller 	int ac, i;
88b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
89b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
90b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
91d012a605SMarco Porsch 	struct ps_data *ps;
92b22cfcfcSEliad Peller 
93e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
945ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
955ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
96d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
97d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
98d012a605SMarco Porsch 			ps = &sdata->bss->ps;
993f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1003f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
101d012a605SMarco Porsch 		else
102d012a605SMarco Porsch 			return;
103b22cfcfcSEliad Peller 
104b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
105e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1065ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
107b22cfcfcSEliad Peller 
108d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
109b22cfcfcSEliad Peller 	}
110b22cfcfcSEliad Peller 
111ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
112ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
113ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
114ba8c3d6fSFelix Fietkau 			int n = skb_queue_len(&txqi->queue);
115ba8c3d6fSFelix Fietkau 
116ba8c3d6fSFelix Fietkau 			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
117ba8c3d6fSFelix Fietkau 			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
118ba8c3d6fSFelix Fietkau 		}
119ba8c3d6fSFelix Fietkau 	}
120ba8c3d6fSFelix Fietkau 
121b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
122b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1231f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1241f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
125b22cfcfcSEliad Peller 	}
126b22cfcfcSEliad Peller 
12745b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12845b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
129b22cfcfcSEliad Peller 
1305ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
131b22cfcfcSEliad Peller 
132b22cfcfcSEliad Peller 	/*
133b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
134b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
135b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
136b22cfcfcSEliad Peller 	 * directly by station destruction.
137b22cfcfcSEliad Peller 	 */
1385a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
139661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
140b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
141b22cfcfcSEliad Peller 		if (!tid_tx)
142b22cfcfcSEliad Peller 			continue;
1431f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
144b22cfcfcSEliad Peller 		kfree(tid_tx);
145b22cfcfcSEliad Peller 	}
1465108ca82SJohannes Berg }
147b22cfcfcSEliad Peller 
1485108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1495108ca82SJohannes Berg {
1505108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1515108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1525108ca82SJohannes Berg 
1535108ca82SJohannes Berg 	__cleanup_single_sta(sta);
154b22cfcfcSEliad Peller 	sta_info_free(local, sta);
155b22cfcfcSEliad Peller }
156b22cfcfcSEliad Peller 
157d0709a65SJohannes Berg /* protected by RCU */
158abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
159abe60632SJohannes Berg 			      const u8 *addr)
16043ba7e95SJohannes Berg {
161abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16260f4b626SJohannes Berg 	struct sta_info *sta;
16360f4b626SJohannes Berg 	struct rhash_head *tmp;
16460f4b626SJohannes Berg 	const struct bucket_table *tbl;
16543ba7e95SJohannes Berg 
16660f4b626SJohannes Berg 	rcu_read_lock();
16760f4b626SJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
16860f4b626SJohannes Berg 
16960f4b626SJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
17060f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17160f4b626SJohannes Berg 			rcu_read_unlock();
17260f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17360f4b626SJohannes Berg 			 * another rcu read section or the mutex
17460f4b626SJohannes Berg 			 */
17560f4b626SJohannes Berg 			return sta;
17660f4b626SJohannes Berg 		}
17760f4b626SJohannes Berg 	}
17860f4b626SJohannes Berg 	rcu_read_unlock();
17960f4b626SJohannes Berg 	return NULL;
18043ba7e95SJohannes Berg }
18143ba7e95SJohannes Berg 
1820e5ded5aSFelix Fietkau /*
1830e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1840e5ded5aSFelix Fietkau  * or from one of its vlans
1850e5ded5aSFelix Fietkau  */
1860e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1870e5ded5aSFelix Fietkau 				  const u8 *addr)
1880e5ded5aSFelix Fietkau {
1890e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1900e5ded5aSFelix Fietkau 	struct sta_info *sta;
1917bedd0cfSJohannes Berg 	struct rhash_head *tmp;
1927bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
1930e5ded5aSFelix Fietkau 
1947bedd0cfSJohannes Berg 	rcu_read_lock();
1957bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1967bedd0cfSJohannes Berg 
1977bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1987bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
1997bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2007bedd0cfSJohannes Berg 			rcu_read_unlock();
2017bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2027bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2037bedd0cfSJohannes Berg 			 */
2040e5ded5aSFelix Fietkau 			return sta;
2050e5ded5aSFelix Fietkau 		}
2067bedd0cfSJohannes Berg 	}
2077bedd0cfSJohannes Berg 	rcu_read_unlock();
2087bedd0cfSJohannes Berg 	return NULL;
2097bedd0cfSJohannes Berg }
2100e5ded5aSFelix Fietkau 
2113b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2123b53fde8SJohannes Berg 				     int idx)
213ee385855SLuis Carlos Cobo {
2143b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
215ee385855SLuis Carlos Cobo 	struct sta_info *sta;
216ee385855SLuis Carlos Cobo 	int i = 0;
217ee385855SLuis Carlos Cobo 
218d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2193b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2202a8ca29aSLuis Carlos Cobo 			continue;
221ee385855SLuis Carlos Cobo 		if (i < idx) {
222ee385855SLuis Carlos Cobo 			++i;
223ee385855SLuis Carlos Cobo 			continue;
224ee385855SLuis Carlos Cobo 		}
2252a8ca29aSLuis Carlos Cobo 		return sta;
226ee385855SLuis Carlos Cobo 	}
227ee385855SLuis Carlos Cobo 
228ee385855SLuis Carlos Cobo 	return NULL;
229ee385855SLuis Carlos Cobo }
230f0706e82SJiri Benc 
23193e5deb1SJohannes Berg /**
232d9a7ddb0SJohannes Berg  * sta_info_free - free STA
23393e5deb1SJohannes Berg  *
2346ef307bcSRandy Dunlap  * @local: pointer to the global information
23593e5deb1SJohannes Berg  * @sta: STA info to free
23693e5deb1SJohannes Berg  *
23793e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
238d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
239d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
240d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
24193e5deb1SJohannes Berg  */
242d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
24393e5deb1SJohannes Berg {
244889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2454b7679a5SJohannes Berg 		rate_control_free_sta(sta);
24693e5deb1SJohannes Berg 
247bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
24893e5deb1SJohannes Berg 
249ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
250ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
25153d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
252*433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
253*433f5bc1SJohannes Berg 	kfree(sta->mesh);
254*433f5bc1SJohannes Berg #endif
25593e5deb1SJohannes Berg 	kfree(sta);
25693e5deb1SJohannes Berg }
25793e5deb1SJohannes Berg 
2584d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
259d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
260d0709a65SJohannes Berg 			      struct sta_info *sta)
261f0706e82SJiri Benc {
2627bedd0cfSJohannes Berg 	rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
2637bedd0cfSJohannes Berg 			       sta_rht_params);
264f0706e82SJiri Benc }
265f0706e82SJiri Benc 
2665ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
267af818581SJohannes Berg {
268af818581SJohannes Berg 	struct sta_info *sta;
269af818581SJohannes Berg 
2705ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
271af818581SJohannes Berg 
272af818581SJohannes Berg 	if (sta->dead)
273af818581SJohannes Berg 		return;
274af818581SJohannes Berg 
27554420473SHelmut Schaa 	local_bh_disable();
2765ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
277af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2785ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
279af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2805ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
28147086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
282ce662b44SJohannes Berg 	local_bh_enable();
283af818581SJohannes Berg }
284af818581SJohannes Berg 
285af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
286af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
287af65cd96SJohannes Berg {
28830686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
289af65cd96SJohannes Berg 		return 0;
290af65cd96SJohannes Berg 
291889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
292af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29335c347acSJohannes Berg 						     sta, gfp);
294889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
295af65cd96SJohannes Berg 		return -ENOMEM;
296af65cd96SJohannes Berg 
297af65cd96SJohannes Berg 	return 0;
298af65cd96SJohannes Berg }
299af65cd96SJohannes Berg 
30073651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30156544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
302f0706e82SJiri Benc {
303d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
304ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
305f0706e82SJiri Benc 	struct sta_info *sta;
306ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
30716c5f15cSRon Rindjunsky 	int i;
308f0706e82SJiri Benc 
309ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
310f0706e82SJiri Benc 	if (!sta)
31173651ee6SJohannes Berg 		return NULL;
312f0706e82SJiri Benc 
31307346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3141d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3155ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
31667c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
317a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
31887f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
319*433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
320*433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
321*433f5bc1SJohannes Berg 		if (!sta->mesh)
322*433f5bc1SJohannes Berg 			goto free;
323*433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
32487f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
32587f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
326*433f5bc1SJohannes Berg 			init_timer(&sta->mesh->plink_timer);
327*433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
328*433f5bc1SJohannes Berg 	}
32987f59c70SThomas Pedersen #endif
33007346f81SJohannes Berg 
331ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
33217741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
333d0709a65SJohannes Berg 	sta->local = local;
334d0709a65SJohannes Berg 	sta->sdata = sdata;
3358bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
336f0706e82SJiri Benc 
33771ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
33871ec375cSJohannes Berg 
339b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
340b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
341b6da911bSLiad Kaufman 
34218171520SThomas Gleixner 	ktime_get_ts(&uptime);
343ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
344541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
345ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
346ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
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:
417*433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
418*433f5bc1SJohannes Berg 	kfree(sta->mesh);
419*433f5bc1SJohannes 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 
4408c71df7aSGuy Eilam 	return 0;
44193e5deb1SJohannes Berg }
44244213b5eSJohannes Berg 
443f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
444f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
445f09603a2SJohannes Berg 				     struct sta_info *sta)
446f09603a2SJohannes Berg {
447f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
448f09603a2SJohannes Berg 	int err = 0;
449f09603a2SJohannes Berg 
450f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
451f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
452f09603a2SJohannes Berg 		if (err)
453f09603a2SJohannes Berg 			break;
454f09603a2SJohannes Berg 	}
455f09603a2SJohannes Berg 
456f09603a2SJohannes Berg 	if (!err) {
457a4ec45a4SJohannes Berg 		/*
458a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
459a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
460a4ec45a4SJohannes Berg 		 */
461a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
462f09603a2SJohannes Berg 			sta->uploaded = true;
463f09603a2SJohannes Berg 		return 0;
464f09603a2SJohannes Berg 	}
465f09603a2SJohannes Berg 
466f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
467bdcbd8e0SJohannes Berg 		sdata_info(sdata,
468bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
469bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
470f09603a2SJohannes Berg 		err = 0;
471f09603a2SJohannes Berg 	}
472f09603a2SJohannes Berg 
473f09603a2SJohannes Berg 	/* unwind on error */
474f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
475f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
476f09603a2SJohannes Berg 
477f09603a2SJohannes Berg 	return err;
478f09603a2SJohannes Berg }
479f09603a2SJohannes Berg 
48034e89507SJohannes Berg /*
4818c71df7aSGuy Eilam  * should be called with sta_mtx locked
4828c71df7aSGuy Eilam  * this function replaces the mutex lock
4838c71df7aSGuy Eilam  * with a RCU lock
4848c71df7aSGuy Eilam  */
4854d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4868c71df7aSGuy Eilam {
4878c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4888c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4897852e361SJohannes Berg 	struct station_info sinfo;
4908c71df7aSGuy Eilam 	int err = 0;
4918c71df7aSGuy Eilam 
4928c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4938c71df7aSGuy Eilam 
4947852e361SJohannes Berg 	/* check if STA exists already */
4957852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4964d33960bSJohannes Berg 		err = -EEXIST;
4974d33960bSJohannes Berg 		goto out_err;
49834e89507SJohannes Berg 	}
49934e89507SJohannes Berg 
5004d33960bSJohannes Berg 	local->num_sta++;
5014d33960bSJohannes Berg 	local->sta_generation++;
5024d33960bSJohannes Berg 	smp_mb();
5034d33960bSJohannes Berg 
5045108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5055108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5065108ca82SJohannes Berg 
5074d33960bSJohannes Berg 	/* make the station visible */
5084d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5094d33960bSJohannes Berg 
5102bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
51183d5cc01SJohannes Berg 
5125108ca82SJohannes Berg 	/* notify driver */
5135108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5145108ca82SJohannes Berg 	if (err)
5155108ca82SJohannes Berg 		goto out_remove;
5165108ca82SJohannes Berg 
51783d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5185108ca82SJohannes Berg 	/* accept BA sessions now */
5195108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5204d33960bSJohannes Berg 
52121f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
5224d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5234d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5244d33960bSJohannes Berg 
5254d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5264d33960bSJohannes Berg 	sinfo.filled = 0;
5274d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5284d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
529d0709a65SJohannes Berg 
530bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
531f0706e82SJiri Benc 
53234e89507SJohannes Berg 	/* move reference to rcu-protected */
53334e89507SJohannes Berg 	rcu_read_lock();
53434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
535e9f207f0SJiri Benc 
53673651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
53773651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
53873651ee6SJohannes Berg 
53973651ee6SJohannes Berg 	return 0;
5405108ca82SJohannes Berg  out_remove:
5415108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5425108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5435108ca82SJohannes Berg 	local->num_sta--;
5445108ca82SJohannes Berg 	synchronize_net();
5455108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5464d33960bSJohannes Berg  out_err:
5474d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5484d33960bSJohannes Berg 	rcu_read_lock();
5494d33960bSJohannes Berg 	return err;
5508c71df7aSGuy Eilam }
5518c71df7aSGuy Eilam 
5528c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5538c71df7aSGuy Eilam {
5548c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
555308f7fcfSZhao, Gang 	int err;
5568c71df7aSGuy Eilam 
5574d33960bSJohannes Berg 	might_sleep();
5584d33960bSJohannes Berg 
5598c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5608c71df7aSGuy Eilam 	if (err) {
5618c71df7aSGuy Eilam 		rcu_read_lock();
5628c71df7aSGuy Eilam 		goto out_free;
5638c71df7aSGuy Eilam 	}
5648c71df7aSGuy Eilam 
565004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
566004c872eSJohannes Berg 
5674d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5688c71df7aSGuy Eilam 	if (err)
569004c872eSJohannes Berg 		goto out_free;
570d0709a65SJohannes Berg 
571004c872eSJohannes Berg 	return 0;
57293e5deb1SJohannes Berg  out_free:
573d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
57493e5deb1SJohannes Berg 	return err;
575f0706e82SJiri Benc }
576f0706e82SJiri Benc 
57734e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
57834e89507SJohannes Berg {
57934e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
58034e89507SJohannes Berg 
58134e89507SJohannes Berg 	rcu_read_unlock();
58234e89507SJohannes Berg 
58334e89507SJohannes Berg 	return err;
58434e89507SJohannes Berg }
58534e89507SJohannes Berg 
586d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
587004c872eSJohannes Berg {
588004c872eSJohannes Berg 	/*
589004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
590004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
591004c872eSJohannes Berg 	 */
592d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
593004c872eSJohannes Berg }
594004c872eSJohannes Berg 
595d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
596004c872eSJohannes Berg {
597004c872eSJohannes Berg 	/*
598004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
599004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
600004c872eSJohannes Berg 	 */
601d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
602004c872eSJohannes Berg }
603004c872eSJohannes Berg 
6043d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6053d5839b6SIlan Peer {
6063d5839b6SIlan Peer 	/*
6073d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6083d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6093d5839b6SIlan Peer 	 */
6103d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6113d5839b6SIlan Peer }
6123d5839b6SIlan Peer 
613948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
614004c872eSJohannes Berg {
615948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
616948d887dSJohannes Berg 	switch (ac) {
617948d887dSJohannes Berg 	case IEEE80211_AC_VO:
618948d887dSJohannes Berg 		return BIT(6) | BIT(7);
619948d887dSJohannes Berg 	case IEEE80211_AC_VI:
620948d887dSJohannes Berg 		return BIT(4) | BIT(5);
621948d887dSJohannes Berg 	case IEEE80211_AC_BE:
622948d887dSJohannes Berg 		return BIT(0) | BIT(3);
623948d887dSJohannes Berg 	case IEEE80211_AC_BK:
624948d887dSJohannes Berg 		return BIT(1) | BIT(2);
625948d887dSJohannes Berg 	default:
626948d887dSJohannes Berg 		WARN_ON(1);
627948d887dSJohannes Berg 		return 0;
628d0709a65SJohannes Berg 	}
629004c872eSJohannes Berg }
630004c872eSJohannes Berg 
6319b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
632004c872eSJohannes Berg {
633c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
634d012a605SMarco Porsch 	struct ps_data *ps;
635948d887dSJohannes Berg 	bool indicate_tim = false;
636948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
637948d887dSJohannes Berg 	int ac;
638d012a605SMarco Porsch 	u16 id;
639004c872eSJohannes Berg 
640d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
641d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
642c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
643c868cb35SJohannes Berg 			return;
6443e122be0SJohannes Berg 
645d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
646d012a605SMarco Porsch 		id = sta->sta.aid;
6473f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6483f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6493f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
650204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
651*433f5bc1SJohannes Berg 		id = sta->mesh->plid % (IEEE80211_MAX_AID + 1);
6523f52b7e3SMarco Porsch #endif
653d012a605SMarco Porsch 	} else {
654d012a605SMarco Porsch 		return;
655d012a605SMarco Porsch 	}
656d012a605SMarco Porsch 
657c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
65830686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
659c868cb35SJohannes Berg 		return;
660004c872eSJohannes Berg 
661c868cb35SJohannes Berg 	if (sta->dead)
662c868cb35SJohannes Berg 		goto done;
6633e122be0SJohannes Berg 
664948d887dSJohannes Berg 	/*
665948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
666948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
667948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
668948d887dSJohannes Berg 	 * non-enabled ones.
669948d887dSJohannes Berg 	 */
670948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
671948d887dSJohannes Berg 		ignore_for_tim = 0;
672948d887dSJohannes Berg 
6739b7a86f3SJohannes Berg 	if (ignore_pending)
6749b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6759b7a86f3SJohannes Berg 
676948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
677948d887dSJohannes Berg 		unsigned long tids;
678948d887dSJohannes Berg 
679948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
680948d887dSJohannes Berg 			continue;
681948d887dSJohannes Berg 
682948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
683948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
684948d887dSJohannes Berg 		if (indicate_tim)
685948d887dSJohannes Berg 			break;
686948d887dSJohannes Berg 
687948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
688948d887dSJohannes Berg 
689948d887dSJohannes Berg 		indicate_tim |=
690948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
691ba8c3d6fSFelix Fietkau 		indicate_tim |=
692ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
693004c872eSJohannes Berg 	}
694004c872eSJohannes Berg 
695c868cb35SJohannes Berg  done:
69665f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
697004c872eSJohannes Berg 
6983d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6993d5839b6SIlan Peer 		goto out_unlock;
7003d5839b6SIlan Peer 
701948d887dSJohannes Berg 	if (indicate_tim)
702d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
703c868cb35SJohannes Berg 	else
704d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7053e122be0SJohannes Berg 
7069b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
707c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
708948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
709c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
710004c872eSJohannes Berg 	}
711004c872eSJohannes Berg 
7123d5839b6SIlan Peer out_unlock:
71365f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
714004c872eSJohannes Berg }
715004c872eSJohannes Berg 
7169b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7179b7a86f3SJohannes Berg {
7189b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7199b7a86f3SJohannes Berg }
7209b7a86f3SJohannes Berg 
721cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
722f0706e82SJiri Benc {
723e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
724f0706e82SJiri Benc 	int timeout;
725f0706e82SJiri Benc 
726f0706e82SJiri Benc 	if (!skb)
727cd0b8d89SJohannes Berg 		return false;
728f0706e82SJiri Benc 
729e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
730f0706e82SJiri Benc 
731f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
73257c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
73357c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
73457c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
735f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
736f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
737e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
738f0706e82SJiri Benc }
739f0706e82SJiri Benc 
740f0706e82SJiri Benc 
741948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
742948d887dSJohannes Berg 						struct sta_info *sta, int ac)
743f0706e82SJiri Benc {
744f0706e82SJiri Benc 	unsigned long flags;
745f0706e82SJiri Benc 	struct sk_buff *skb;
746f0706e82SJiri Benc 
74760750397SJohannes Berg 	/*
74860750397SJohannes Berg 	 * First check for frames that should expire on the filtered
74960750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
75060750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
75160750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
75260750397SJohannes Berg 	 * total_ps_buffered counter.
75360750397SJohannes Berg 	 */
754f0706e82SJiri Benc 	for (;;) {
755948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
756948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
75757c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
758948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
759836341a7SJohannes Berg 		else
760f0706e82SJiri Benc 			skb = NULL;
761948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
762f0706e82SJiri Benc 
76360750397SJohannes Berg 		/*
76460750397SJohannes Berg 		 * Frames are queued in order, so if this one
76560750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
76660750397SJohannes Berg 		 * we actually reached the end of the queue we
76760750397SJohannes Berg 		 * also need to stop, of course.
76860750397SJohannes Berg 		 */
76960750397SJohannes Berg 		if (!skb)
77060750397SJohannes Berg 			break;
771d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
77260750397SJohannes Berg 	}
77360750397SJohannes Berg 
77460750397SJohannes Berg 	/*
77560750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
77660750397SJohannes Berg 	 * only find something if the filtered queue was emptied
77760750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
77860750397SJohannes Berg 	 * buffered frames.
77960750397SJohannes Berg 	 */
780f0706e82SJiri Benc 	for (;;) {
781948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
782948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
783f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
784948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
785f0706e82SJiri Benc 		else
786f0706e82SJiri Benc 			skb = NULL;
787948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
788f0706e82SJiri Benc 
78960750397SJohannes Berg 		/*
79060750397SJohannes Berg 		 * frames are queued in order, so if this one
79160750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
79260750397SJohannes Berg 		 * the queue) we can stop testing
79360750397SJohannes Berg 		 */
794836341a7SJohannes Berg 		if (!skb)
795836341a7SJohannes Berg 			break;
796836341a7SJohannes Berg 
797f0706e82SJiri Benc 		local->total_ps_buffered--;
798bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
799bdcbd8e0SJohannes Berg 		       sta->sta.addr);
800d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
801f0706e82SJiri Benc 	}
8023393a608SJuuso Oikarinen 
80360750397SJohannes Berg 	/*
80460750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
80560750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
80660750397SJohannes Berg 	 * frames.
80760750397SJohannes Berg 	 */
80860750397SJohannes Berg 	sta_info_recalc_tim(sta);
80960750397SJohannes Berg 
81060750397SJohannes Berg 	/*
81160750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
81260750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
81360750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
81460750397SJohannes Berg 	 */
815948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
816948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
817948d887dSJohannes Berg }
818948d887dSJohannes Berg 
819948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
820948d887dSJohannes Berg 					     struct sta_info *sta)
821948d887dSJohannes Berg {
822948d887dSJohannes Berg 	bool have_buffered = false;
823948d887dSJohannes Berg 	int ac;
824948d887dSJohannes Berg 
8253f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8263f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8273f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
828948d887dSJohannes Berg 		return false;
829948d887dSJohannes Berg 
830948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
831948d887dSJohannes Berg 		have_buffered |=
832948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
833948d887dSJohannes Berg 
834948d887dSJohannes Berg 	return have_buffered;
835f0706e82SJiri Benc }
836f0706e82SJiri Benc 
837d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
83834e89507SJohannes Berg {
83934e89507SJohannes Berg 	struct ieee80211_local *local;
84034e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8416d10e46bSJohannes Berg 	int ret;
84234e89507SJohannes Berg 
84334e89507SJohannes Berg 	might_sleep();
84434e89507SJohannes Berg 
84534e89507SJohannes Berg 	if (!sta)
84634e89507SJohannes Berg 		return -ENOENT;
84734e89507SJohannes Berg 
84834e89507SJohannes Berg 	local = sta->local;
84934e89507SJohannes Berg 	sdata = sta->sdata;
85034e89507SJohannes Berg 
85183d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
85283d5cc01SJohannes Berg 
853098a6070SJohannes Berg 	/*
854098a6070SJohannes Berg 	 * Before removing the station from the driver and
855098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
856098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
857098a6070SJohannes Berg 	 * will be sufficient.
858098a6070SJohannes Berg 	 */
859c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
860c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
861098a6070SJohannes Berg 
86234e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
863b01711beSJohannes Berg 	if (WARN_ON(ret))
86434e89507SJohannes Berg 		return ret;
86534e89507SJohannes Berg 
866a7a6bdd0SArik Nemtsov 	/*
867a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
868a7a6bdd0SArik Nemtsov 	 * removal.
869a7a6bdd0SArik Nemtsov 	 */
870a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
871a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
872a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
873a7a6bdd0SArik Nemtsov 	}
874a7a6bdd0SArik Nemtsov 
875794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8764d33960bSJohannes Berg 
8776a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8786a9d1b91SJohannes Berg 
879a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
880a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
881a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
882a710c816SJohannes Berg 
883d778207bSJohannes Berg 	return 0;
884d778207bSJohannes Berg }
885d778207bSJohannes Berg 
886d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
887d778207bSJohannes Berg {
888d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
889d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8906f7a8d26SJohannes Berg 	struct station_info sinfo = {};
891d778207bSJohannes Berg 	int ret;
892d778207bSJohannes Berg 
893d778207bSJohannes Berg 	/*
894d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
895d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
896d778207bSJohannes Berg 	 */
897d778207bSJohannes Berg 
898d778207bSJohannes Berg 	might_sleep();
899d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
900d778207bSJohannes Berg 
901c8782078SJohannes Berg 	/* now keys can no longer be reached */
9026d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
90334e89507SJohannes Berg 
9049b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9059b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9069b7a86f3SJohannes Berg 
90734e89507SJohannes Berg 	sta->dead = true;
90834e89507SJohannes Berg 
90934e89507SJohannes Berg 	local->num_sta--;
91034e89507SJohannes Berg 	local->sta_generation++;
91134e89507SJohannes Berg 
91283d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
913f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
914f09603a2SJohannes Berg 		if (ret) {
91583d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
91683d5cc01SJohannes Berg 			break;
91783d5cc01SJohannes Berg 		}
91883d5cc01SJohannes Berg 	}
919d9a7ddb0SJohannes Berg 
920f09603a2SJohannes Berg 	if (sta->uploaded) {
921f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
922f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
923f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
924f09603a2SJohannes Berg 	}
92534e89507SJohannes Berg 
926bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
927bdcbd8e0SJohannes Berg 
9286f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
9296f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
930ec15e68bSJouni Malinen 
93134e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
93234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
93321f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
93434e89507SJohannes Berg 
935d34ba216SJohannes Berg 	cleanup_single_sta(sta);
936d778207bSJohannes Berg }
937d778207bSJohannes Berg 
938d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
939d778207bSJohannes Berg {
940d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
941d778207bSJohannes Berg 
942d778207bSJohannes Berg 	if (err)
943d778207bSJohannes Berg 		return err;
944d778207bSJohannes Berg 
945d778207bSJohannes Berg 	synchronize_net();
946d778207bSJohannes Berg 
947d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
94834e89507SJohannes Berg 
94934e89507SJohannes Berg 	return 0;
95034e89507SJohannes Berg }
95134e89507SJohannes Berg 
95234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
95334e89507SJohannes Berg {
95434e89507SJohannes Berg 	struct sta_info *sta;
95534e89507SJohannes Berg 	int ret;
95634e89507SJohannes Berg 
95734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9587852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
95934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
96034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
96134e89507SJohannes Berg 
96234e89507SJohannes Berg 	return ret;
96334e89507SJohannes Berg }
96434e89507SJohannes Berg 
96534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
96634e89507SJohannes Berg 			      const u8 *addr)
96734e89507SJohannes Berg {
96834e89507SJohannes Berg 	struct sta_info *sta;
96934e89507SJohannes Berg 	int ret;
97034e89507SJohannes Berg 
97134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9727852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
97334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
97434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
97534e89507SJohannes Berg 
97634e89507SJohannes Berg 	return ret;
97734e89507SJohannes Berg }
978f0706e82SJiri Benc 
979f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
980f0706e82SJiri Benc {
981f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
982f0706e82SJiri Benc 	struct sta_info *sta;
9833393a608SJuuso Oikarinen 	bool timer_needed = false;
984f0706e82SJiri Benc 
985d0709a65SJohannes Berg 	rcu_read_lock();
986d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9873393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9883393a608SJuuso Oikarinen 			timer_needed = true;
989d0709a65SJohannes Berg 	rcu_read_unlock();
990f0706e82SJiri Benc 
9915bb644a0SJohannes Berg 	if (local->quiescing)
9925bb644a0SJohannes Berg 		return;
9935bb644a0SJohannes Berg 
9943393a608SJuuso Oikarinen 	if (!timer_needed)
9953393a608SJuuso Oikarinen 		return;
9963393a608SJuuso Oikarinen 
99726d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
99826d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
999f0706e82SJiri Benc }
1000f0706e82SJiri Benc 
10017bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1002f0706e82SJiri Benc {
10037bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
10047bedd0cfSJohannes Berg }
10057bedd0cfSJohannes Berg 
10067bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10077bedd0cfSJohannes Berg {
10087bedd0cfSJohannes Berg 	int err;
10097bedd0cfSJohannes Berg 
10107bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
10117bedd0cfSJohannes Berg 	if (err)
10127bedd0cfSJohannes Berg 		return err;
10137bedd0cfSJohannes Berg 
10144d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
101534e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1016f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1017f0706e82SJiri Benc 
1018b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1019b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10207bedd0cfSJohannes Berg 	return 0;
1021f0706e82SJiri Benc }
1022f0706e82SJiri Benc 
1023f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1024f0706e82SJiri Benc {
1025a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
10267bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
1027f0706e82SJiri Benc }
1028f0706e82SJiri Benc 
1029051007d9SJohannes Berg 
1030e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1031f0706e82SJiri Benc {
1032b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1033f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1034d778207bSJohannes Berg 	LIST_HEAD(free_list);
103544213b5eSJohannes Berg 	int ret = 0;
1036f0706e82SJiri Benc 
1037d0709a65SJohannes Berg 	might_sleep();
1038d0709a65SJohannes Berg 
1039e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1040e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1041e716251dSJohannes Berg 
104234e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
104334e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1044e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1045e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1046d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1047d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
104834316837SJohannes Berg 			ret++;
104934316837SJohannes Berg 		}
105034e89507SJohannes Berg 	}
1051d778207bSJohannes Berg 
1052d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1053d778207bSJohannes Berg 		synchronize_net();
1054d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1055d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1056d778207bSJohannes Berg 	}
105734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
105844213b5eSJohannes Berg 
1059051007d9SJohannes Berg 	return ret;
1060051007d9SJohannes Berg }
1061051007d9SJohannes Berg 
106224723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
106324723d1bSJohannes Berg 			  unsigned long exp_time)
106424723d1bSJohannes Berg {
106524723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
106624723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
106724723d1bSJohannes Berg 
106834e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1069e46a2cf9SMohammed Shafi Shajakhan 
1070e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1071ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1072ec2b774eSMarek Lindner 			continue;
1073ec2b774eSMarek Lindner 
107424723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1075eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1076bdcbd8e0SJohannes Berg 				sta->sta.addr);
10773f52b7e3SMarco Porsch 
10783f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10793f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10803f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10813f52b7e3SMarco Porsch 
108234e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
108324723d1bSJohannes Berg 		}
1084e46a2cf9SMohammed Shafi Shajakhan 	}
1085e46a2cf9SMohammed Shafi Shajakhan 
108634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
108724723d1bSJohannes Berg }
108817741cdcSJohannes Berg 
1089686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1090686b9cb9SBen Greear 						   const u8 *addr,
1091686b9cb9SBen Greear 						   const u8 *localaddr)
109217741cdcSJohannes Berg {
10937bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
10947bedd0cfSJohannes Berg 	struct sta_info *sta;
10957bedd0cfSJohannes Berg 	struct rhash_head *tmp;
10967bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
10977bedd0cfSJohannes Berg 
10987bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
109917741cdcSJohannes Berg 
1100686b9cb9SBen Greear 	/*
1101686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1102686b9cb9SBen Greear 	 * ... first in list.
1103686b9cb9SBen Greear 	 */
11047bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1105686b9cb9SBen Greear 		if (localaddr &&
1106b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1107686b9cb9SBen Greear 			continue;
1108f7c65594SJohannes Berg 		if (!sta->uploaded)
1109f7c65594SJohannes Berg 			return NULL;
111017741cdcSJohannes Berg 		return &sta->sta;
1111f7c65594SJohannes Berg 	}
1112f7c65594SJohannes Berg 
1113abe60632SJohannes Berg 	return NULL;
111417741cdcSJohannes Berg }
1115686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11165ed176e1SJohannes Berg 
11175ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11185ed176e1SJohannes Berg 					 const u8 *addr)
11195ed176e1SJohannes Berg {
1120f7c65594SJohannes Berg 	struct sta_info *sta;
11215ed176e1SJohannes Berg 
11225ed176e1SJohannes Berg 	if (!vif)
11235ed176e1SJohannes Berg 		return NULL;
11245ed176e1SJohannes Berg 
1125f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1126f7c65594SJohannes Berg 	if (!sta)
1127f7c65594SJohannes Berg 		return NULL;
11285ed176e1SJohannes Berg 
1129f7c65594SJohannes Berg 	if (!sta->uploaded)
1130f7c65594SJohannes Berg 		return NULL;
1131f7c65594SJohannes Berg 
1132f7c65594SJohannes Berg 	return &sta->sta;
11335ed176e1SJohannes Berg }
113417741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1135af818581SJohannes Berg 
1136e3685e03SJohannes Berg /* powersave support code */
1137e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
113850a9432dSJohannes Berg {
1139608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1140e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1141e3685e03SJohannes Berg 	struct sk_buff_head pending;
1142ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1143e3685e03SJohannes Berg 	unsigned long flags;
1144d012a605SMarco Porsch 	struct ps_data *ps;
1145d012a605SMarco Porsch 
11463918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11473918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11483918edb0SFelix Fietkau 				     u.ap);
11493918edb0SFelix Fietkau 
11503918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1151d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11523f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11533f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1154d012a605SMarco Porsch 	else
1155d012a605SMarco Porsch 		return;
115650a9432dSJohannes Berg 
1157c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
115847086fc5SJohannes Berg 
11595a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1160948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1161ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1162948d887dSJohannes Berg 
116330686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
116412375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1165af818581SJohannes Berg 
1166ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1167ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1168ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1169ba8c3d6fSFelix Fietkau 
1170ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1171ba8c3d6fSFelix Fietkau 				continue;
1172ba8c3d6fSFelix Fietkau 
1173ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1174ba8c3d6fSFelix Fietkau 		}
1175ba8c3d6fSFelix Fietkau 	}
1176ba8c3d6fSFelix Fietkau 
1177948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1178af818581SJohannes Berg 
11791d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11801d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1181af818581SJohannes Berg 	/* Send all buffered frames to the station */
1182948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1183948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1184948d887dSJohannes Berg 
1185987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1186948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1187987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1188948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1189948d887dSJohannes Berg 		filtered += tmp - count;
1190948d887dSJohannes Berg 		count = tmp;
1191948d887dSJohannes Berg 
1192987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1193948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1194987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1195948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1196948d887dSJohannes Berg 		buffered += tmp - count;
1197948d887dSJohannes Berg 	}
1198948d887dSJohannes Berg 
1199e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
12005ac2e350SJohannes Berg 
12015ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
12025ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
12035ac2e350SJohannes Berg 
12045ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12055ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12065ac2e350SJohannes Berg 	 */
12075ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12085ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12091d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1210948d887dSJohannes Berg 
1211e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1212e3685e03SJohannes Berg 
1213687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1214062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1215062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1216687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1217687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1218687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1219687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1220687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1221687da132SEmmanuel Grumbach 		       sta->sta.addr);
1222687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1223687da132SEmmanuel Grumbach 					   sta->sta.addr,
1224687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1225687da132SEmmanuel Grumbach 	}
1226687da132SEmmanuel Grumbach 
1227af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1228af818581SJohannes Berg 
1229c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1230c868cb35SJohannes Berg 
1231bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1232bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1233bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
123417c18bf8SJohannes Berg 
123517c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1236af818581SJohannes Berg }
1237af818581SJohannes Berg 
1238ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1239ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1240b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1241b77cf4f8SJohannes Berg 					 bool call_driver)
1242ce662b44SJohannes Berg {
1243ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1244ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1245ce662b44SJohannes Berg 	struct sk_buff *skb;
1246ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1247ce662b44SJohannes Berg 	__le16 fc;
1248a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1249ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
125055de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1251ce662b44SJohannes Berg 
1252ce662b44SJohannes Berg 	if (qos) {
1253ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1254ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1255ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1256ce662b44SJohannes Berg 	} else {
1257ce662b44SJohannes Berg 		size -= 2;
1258ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1259ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1260ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1261ce662b44SJohannes Berg 	}
1262ce662b44SJohannes Berg 
1263ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1264ce662b44SJohannes Berg 	if (!skb)
1265ce662b44SJohannes Berg 		return;
1266ce662b44SJohannes Berg 
1267ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1268ce662b44SJohannes Berg 
1269ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1270ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1271ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1272ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1273ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1274ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1275864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1276ce662b44SJohannes Berg 
1277ce662b44SJohannes Berg 	skb->priority = tid;
1278ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
127959b66255SJohannes Berg 	if (qos) {
1280ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1281ce662b44SJohannes Berg 
128240b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1283ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1284ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1285ce662b44SJohannes Berg 	}
1286ce662b44SJohannes Berg 
1287ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1288ce662b44SJohannes Berg 
1289ce662b44SJohannes Berg 	/*
1290ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1291ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1292deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1293deeaee19SJohannes Berg 	 * ends the poll/service period.
1294ce662b44SJohannes Berg 	 */
129502f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1296deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1297ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1298ce662b44SJohannes Berg 
12996b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
13006b127c71SSujith Manoharan 
1301b77cf4f8SJohannes Berg 	if (call_driver)
1302b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1303b77cf4f8SJohannes Berg 					  reason, false);
130440b96408SJohannes Berg 
130589afe614SJohannes Berg 	skb->dev = sdata->dev;
130689afe614SJohannes Berg 
130755de908aSJohannes Berg 	rcu_read_lock();
130855de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
130955de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
131055de908aSJohannes Berg 		rcu_read_unlock();
131155de908aSJohannes Berg 		kfree_skb(skb);
131255de908aSJohannes Berg 		return;
131355de908aSJohannes Berg 	}
131455de908aSJohannes Berg 
131573c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13167c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
131755de908aSJohannes Berg 	rcu_read_unlock();
1318ce662b44SJohannes Berg }
1319ce662b44SJohannes Berg 
13200a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13210a1cb809SJohannes Berg {
13220a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13230a1cb809SJohannes Berg 	if (tids & 0xF8)
13240a1cb809SJohannes Berg 		return fls(tids) - 1;
13250a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13260a1cb809SJohannes Berg 	if (tids & BIT(0))
13270a1cb809SJohannes Berg 		return 0;
13280a1cb809SJohannes Berg 	return fls(tids) - 1;
13290a1cb809SJohannes Berg }
13300a1cb809SJohannes Berg 
133147086fc5SJohannes Berg static void
133247086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
133347086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
133447086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1335af818581SJohannes Berg {
1336af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1337af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1338948d887dSJohannes Berg 	bool more_data = false;
1339948d887dSJohannes Berg 	int ac;
13404049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
134147086fc5SJohannes Berg 	struct sk_buff_head frames;
134247086fc5SJohannes Berg 
1343deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1344c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1345deeaee19SJohannes Berg 
134647086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1347af818581SJohannes Berg 
1348f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1349948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13504049e09aSJohannes Berg 		unsigned long tids;
13514049e09aSJohannes Berg 
135247086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1353948d887dSJohannes Berg 			continue;
1354948d887dSJohannes Berg 
13554049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13564049e09aSJohannes Berg 
1357f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1358f9f760b4SJohannes Berg 		 * release from hardware queues
1359f9f760b4SJohannes Berg 		 */
1360ba8c3d6fSFelix Fietkau 		if (skb_queue_empty(&frames)) {
1361f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1362ba8c3d6fSFelix Fietkau 			driver_release_tids |= sta->txq_buffered_tids & tids;
1363ba8c3d6fSFelix Fietkau 		}
1364f9f760b4SJohannes Berg 
13654049e09aSJohannes Berg 		if (driver_release_tids) {
1366f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1367f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1368f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1369f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1370f9f760b4SJohannes Berg 			 */
1371f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1372f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1373f9f760b4SJohannes Berg 				more_data = true;
1374f9f760b4SJohannes Berg 				driver_release_tids =
1375f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1376f9f760b4SJohannes Berg 						driver_release_tids));
1377f9f760b4SJohannes Berg 				break;
1378f9f760b4SJohannes Berg 			}
13794049e09aSJohannes Berg 		} else {
138047086fc5SJohannes Berg 			struct sk_buff *skb;
138147086fc5SJohannes Berg 
138247086fc5SJohannes Berg 			while (n_frames > 0) {
1383948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1384948d887dSJohannes Berg 				if (!skb) {
138547086fc5SJohannes Berg 					skb = skb_dequeue(
138647086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1387af818581SJohannes Berg 					if (skb)
1388af818581SJohannes Berg 						local->total_ps_buffered--;
1389af818581SJohannes Berg 				}
139047086fc5SJohannes Berg 				if (!skb)
139147086fc5SJohannes Berg 					break;
139247086fc5SJohannes Berg 				n_frames--;
139347086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
139447086fc5SJohannes Berg 			}
1395948d887dSJohannes Berg 		}
1396948d887dSJohannes Berg 
1397f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1398f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1399f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
14004049e09aSJohannes Berg 		 */
1401948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1402948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1403948d887dSJohannes Berg 			more_data = true;
1404948d887dSJohannes Berg 			break;
1405948d887dSJohannes Berg 		}
1406948d887dSJohannes Berg 	}
1407af818581SJohannes Berg 
1408f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1409ce662b44SJohannes Berg 		int tid;
14104049e09aSJohannes Berg 
1411ce662b44SJohannes Berg 		/*
1412ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1413ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1414ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1415ce662b44SJohannes Berg 		 *
1416ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1417ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1418ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1419ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1420ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1421ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1422ce662b44SJohannes Berg 		 *
1423ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1424ce662b44SJohannes Berg 		 */
1425ce662b44SJohannes Berg 
1426ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1427ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1428ce662b44SJohannes Berg 
1429b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1430f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
143147086fc5SJohannes Berg 		struct sk_buff_head pending;
143247086fc5SJohannes Berg 		struct sk_buff *skb;
143340b96408SJohannes Berg 		int num = 0;
143440b96408SJohannes Berg 		u16 tids = 0;
1435b77cf4f8SJohannes Berg 		bool need_null = false;
143647086fc5SJohannes Berg 
143747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
143847086fc5SJohannes Berg 
143947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1440af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
144147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
144240b96408SJohannes Berg 			u8 *qoshdr = NULL;
144340b96408SJohannes Berg 
144440b96408SJohannes Berg 			num++;
1445af818581SJohannes Berg 
1446af818581SJohannes Berg 			/*
144747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
144847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
144947086fc5SJohannes Berg 			 * exchange.
1450af818581SJohannes Berg 			 */
14516b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
14526b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1453af818581SJohannes Berg 
145447086fc5SJohannes Berg 			/*
145547086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
145647086fc5SJohannes Berg 			 * more buffered frames for this STA
145747086fc5SJohannes Berg 			 */
145824b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
145947086fc5SJohannes Berg 				hdr->frame_control |=
146047086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
146124b9c373SJanusz.Dziedzic@tieto.com 			else
146224b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
146324b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1464af818581SJohannes Berg 
146540b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
146640b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
146740b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
146840b96408SJohannes Berg 
1469b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1470b77cf4f8SJohannes Berg 
1471b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1472b77cf4f8SJohannes Berg 
1473b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1474b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1475b77cf4f8SJohannes Berg 				continue;
1476b77cf4f8SJohannes Berg 
1477b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1478b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1479b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1480b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1481b77cf4f8SJohannes Berg 				break;
1482b77cf4f8SJohannes Berg 			}
1483b77cf4f8SJohannes Berg 
1484b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1485b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1486b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1487b77cf4f8SJohannes Berg 			 * and be done.
1488b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1489b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1490b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1491b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1492b77cf4f8SJohannes Berg 			 *
1493b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1494b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1495b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1496b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1497b77cf4f8SJohannes Berg 			 *
1498b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1499b77cf4f8SJohannes Berg 			 */
1500b77cf4f8SJohannes Berg 			if (qoshdr) {
150140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1502deeaee19SJohannes Berg 
150347086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
150447086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1505b77cf4f8SJohannes Berg 			} else {
1506b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1507b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1508b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1509b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1510b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1511b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1512b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1513b77cf4f8SJohannes Berg 				 */
1514b77cf4f8SJohannes Berg 				hdr->frame_control |=
1515b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1516b77cf4f8SJohannes Berg 				need_null = true;
1517b77cf4f8SJohannes Berg 				num++;
151852a3f20cSMarco Porsch 			}
1519b77cf4f8SJohannes Berg 			break;
152047086fc5SJohannes Berg 		}
152147086fc5SJohannes Berg 
152240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
152340b96408SJohannes Berg 					  reason, more_data);
152440b96408SJohannes Berg 
152547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1526af818581SJohannes Berg 
1527b77cf4f8SJohannes Berg 		if (need_null)
1528b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1529b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1530b77cf4f8SJohannes Berg 				reason, false);
1531b77cf4f8SJohannes Berg 
1532c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1533af818581SJohannes Berg 	} else {
1534ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1535ba8c3d6fSFelix Fietkau 		int tid;
1536ba8c3d6fSFelix Fietkau 
1537af818581SJohannes Berg 		/*
15384049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
15394049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1540f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1541f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1542f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1543f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1544f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1545f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1546af818581SJohannes Berg 		 */
15474049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
154847086fc5SJohannes Berg 					    n_frames, reason, more_data);
15494049e09aSJohannes Berg 
15504049e09aSJohannes Berg 		/*
15514049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
15524049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1553f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
15544049e09aSJohannes Berg 		 * release function.
1555f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1556ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1557ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
15584049e09aSJohannes Berg 		 */
1559ba8c3d6fSFelix Fietkau 
1560ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1561ba8c3d6fSFelix Fietkau 			return;
1562ba8c3d6fSFelix Fietkau 
1563ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1564ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1565ba8c3d6fSFelix Fietkau 
1566ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1567ba8c3d6fSFelix Fietkau 				continue;
1568ba8c3d6fSFelix Fietkau 
1569ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1570ba8c3d6fSFelix Fietkau 			break;
1571ba8c3d6fSFelix Fietkau 		}
1572af818581SJohannes Berg 	}
1573af818581SJohannes Berg }
1574af818581SJohannes Berg 
1575af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1576af818581SJohannes Berg {
157747086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1578af818581SJohannes Berg 
1579af818581SJohannes Berg 	/*
158047086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
158147086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
158247086fc5SJohannes Berg 	 * only from the non-enabled ones.
1583af818581SJohannes Berg 	 */
158447086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
158547086fc5SJohannes Berg 		ignore_for_response = 0;
1586af818581SJohannes Berg 
158747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
158847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1589af818581SJohannes Berg }
159047086fc5SJohannes Berg 
159147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
159247086fc5SJohannes Berg {
159347086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
159447086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
159547086fc5SJohannes Berg 
159647086fc5SJohannes Berg 	/*
159747086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
159847086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
159947086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
160047086fc5SJohannes Berg 	 * actually getting called.
160147086fc5SJohannes Berg 	 */
160247086fc5SJohannes Berg 	if (!delivery_enabled)
160347086fc5SJohannes Berg 		return;
160447086fc5SJohannes Berg 
160547086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
160647086fc5SJohannes Berg 	case 1:
160747086fc5SJohannes Berg 		n_frames = 2;
160847086fc5SJohannes Berg 		break;
160947086fc5SJohannes Berg 	case 2:
161047086fc5SJohannes Berg 		n_frames = 4;
161147086fc5SJohannes Berg 		break;
161247086fc5SJohannes Berg 	case 3:
161347086fc5SJohannes Berg 		n_frames = 6;
161447086fc5SJohannes Berg 		break;
161547086fc5SJohannes Berg 	case 0:
161647086fc5SJohannes Berg 		/* XXX: what is a good value? */
161713a8098aSAndrei Otcheretianski 		n_frames = 128;
161847086fc5SJohannes Berg 		break;
161947086fc5SJohannes Berg 	}
162047086fc5SJohannes Berg 
162147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
162247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1623af818581SJohannes Berg }
1624af818581SJohannes Berg 
1625af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1626af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1627af818581SJohannes Berg {
1628af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1629af818581SJohannes Berg 
1630b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1631b5878a2dSJohannes Berg 
16325ac2e350SJohannes Berg 	if (block) {
1633c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
163417c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
16355ac2e350SJohannes Berg 		return;
16365ac2e350SJohannes Berg 	}
16375ac2e350SJohannes Berg 
16385ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
16395ac2e350SJohannes Berg 		return;
16405ac2e350SJohannes Berg 
16415ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
16425ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
16435ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16445ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16455ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
16465ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
16475ac2e350SJohannes Berg 		/* must be asleep in this case */
16485ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16495ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16505ac2e350SJohannes Berg 	} else {
16515ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
165217c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
16535ac2e350SJohannes Berg 	}
1654af818581SJohannes Berg }
1655af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1656dcf55fb5SFelix Fietkau 
1657e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
165837fbd908SJohannes Berg {
165937fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
166037fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
166137fbd908SJohannes Berg 
166237fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
166337fbd908SJohannes Berg 
166437fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
166537fbd908SJohannes Berg }
1666e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
166737fbd908SJohannes Berg 
1668042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1669042ec453SJohannes Berg 				u8 tid, bool buffered)
1670dcf55fb5SFelix Fietkau {
1671dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1672dcf55fb5SFelix Fietkau 
16735a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1674042ec453SJohannes Berg 		return;
1675042ec453SJohannes Berg 
16761b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
16771b000789SJohannes Berg 
1678948d887dSJohannes Berg 	if (buffered)
1679948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1680948d887dSJohannes Berg 	else
1681948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1682948d887dSJohannes Berg 
1683c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1684dcf55fb5SFelix Fietkau }
1685042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1686d9a7ddb0SJohannes Berg 
168783d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1688d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1689d9a7ddb0SJohannes Berg {
16908bf11d8dSJohannes Berg 	might_sleep();
1691d9a7ddb0SJohannes Berg 
1692d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1693d9a7ddb0SJohannes Berg 		return 0;
1694d9a7ddb0SJohannes Berg 
1695f09603a2SJohannes Berg 	/* check allowed transitions first */
1696f09603a2SJohannes Berg 
1697d9a7ddb0SJohannes Berg 	switch (new_state) {
1698d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1699f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1700d9a7ddb0SJohannes Berg 			return -EINVAL;
1701d9a7ddb0SJohannes Berg 		break;
1702d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1703f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1704f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1705d9a7ddb0SJohannes Berg 			return -EINVAL;
1706d9a7ddb0SJohannes Berg 		break;
1707d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1708f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1709f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1710d9a7ddb0SJohannes Berg 			return -EINVAL;
1711d9a7ddb0SJohannes Berg 		break;
1712d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1713f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1714d9a7ddb0SJohannes Berg 			return -EINVAL;
1715d9a7ddb0SJohannes Berg 		break;
1716d9a7ddb0SJohannes Berg 	default:
1717d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1718d9a7ddb0SJohannes Berg 		return -EINVAL;
1719d9a7ddb0SJohannes Berg 	}
1720d9a7ddb0SJohannes Berg 
1721bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1722bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1723f09603a2SJohannes Berg 
1724f09603a2SJohannes Berg 	/*
1725f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1726f09603a2SJohannes Berg 	 * fail the transition
1727f09603a2SJohannes Berg 	 */
1728f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1729f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1730f09603a2SJohannes Berg 					sta->sta_state, new_state);
1731f09603a2SJohannes Berg 		if (err)
1732f09603a2SJohannes Berg 			return err;
1733f09603a2SJohannes Berg 	}
1734f09603a2SJohannes Berg 
1735f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1736f09603a2SJohannes Berg 
1737f09603a2SJohannes Berg 	switch (new_state) {
1738f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1739f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1740f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1741f09603a2SJohannes Berg 		break;
1742f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1743f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1744f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1745f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1746f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1747f09603a2SJohannes Berg 		break;
1748f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1749f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1750f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1751f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
17527e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17537e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17547e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17557e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1756f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
175717c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
1758f09603a2SJohannes Berg 		}
1759f09603a2SJohannes Berg 		break;
1760f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1761f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
17627e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17637e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17647e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17657e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1766f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
176717c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
1768f09603a2SJohannes Berg 		}
1769f09603a2SJohannes Berg 		break;
1770f09603a2SJohannes Berg 	default:
1771f09603a2SJohannes Berg 		break;
1772f09603a2SJohannes Berg 	}
1773f09603a2SJohannes Berg 
1774d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1775d9a7ddb0SJohannes Berg 
1776d9a7ddb0SJohannes Berg 	return 0;
1777d9a7ddb0SJohannes Berg }
1778687da132SEmmanuel Grumbach 
1779687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1780687da132SEmmanuel Grumbach {
1781687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1782687da132SEmmanuel Grumbach 	u8 rx_streams;
1783687da132SEmmanuel Grumbach 
1784687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1785687da132SEmmanuel Grumbach 		return 1;
1786687da132SEmmanuel Grumbach 
1787687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1788687da132SEmmanuel Grumbach 		int i;
1789687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1790687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1791687da132SEmmanuel Grumbach 
1792687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1793687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1794687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1795687da132SEmmanuel Grumbach 				return i + 1;
1796687da132SEmmanuel Grumbach 	}
1797687da132SEmmanuel Grumbach 
1798687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1799687da132SEmmanuel Grumbach 		rx_streams = 4;
1800687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1801687da132SEmmanuel Grumbach 		rx_streams = 3;
1802687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1803687da132SEmmanuel Grumbach 		rx_streams = 2;
1804687da132SEmmanuel Grumbach 	else
1805687da132SEmmanuel Grumbach 		rx_streams = 1;
1806687da132SEmmanuel Grumbach 
1807687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1808687da132SEmmanuel Grumbach 		return rx_streams;
1809687da132SEmmanuel Grumbach 
1810687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1811687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1812687da132SEmmanuel Grumbach }
1813b7ffbd7eSJohannes Berg 
1814b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1815b7ffbd7eSJohannes Berg {
1816b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1817b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
18189a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1819b7ffbd7eSJohannes Berg 	struct timespec uptime;
1820b7ffbd7eSJohannes Berg 	u32 thr = 0;
1821b7ffbd7eSJohannes Berg 	int i, ac;
1822b7ffbd7eSJohannes Berg 
18239a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
18249a244409SJohn W. Linville 		ref = local->rate_ctrl;
18259a244409SJohn W. Linville 
1826b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1827b7ffbd7eSJohannes Berg 
1828225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1829225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1830225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1831225b8189SJohannes Berg 	 */
1832225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1833225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1834225b8189SJohannes Berg 
18352b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
18362b9a7e1bSJohannes Berg 
1837319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1838319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1839319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1840319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1841319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1842319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BEACON_LOSS);
1843b7ffbd7eSJohannes Berg 
184418171520SThomas Gleixner 	ktime_get_ts(&uptime);
1845b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1846b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
18472b9a7e1bSJohannes Berg 
1848319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1849319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1850b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
18512b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1852b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1853319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1854b7ffbd7eSJohannes Berg 	}
18552b9a7e1bSJohannes Berg 
1856319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
18572b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
18582b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
18592b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1860319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
18612b9a7e1bSJohannes Berg 	}
18622b9a7e1bSJohannes Berg 
1863319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1864319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1865b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1866319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
18672b9a7e1bSJohannes Berg 	}
18682b9a7e1bSJohannes Berg 
1869319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1870b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1871319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
18722b9a7e1bSJohannes Berg 	}
18732b9a7e1bSJohannes Berg 
1874319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1875b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1876319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
18772b9a7e1bSJohannes Berg 	}
18782b9a7e1bSJohannes Berg 
1879319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1880b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1881319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
18822b9a7e1bSJohannes Berg 	}
18832b9a7e1bSJohannes Berg 
1884b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1885b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1886b7ffbd7eSJohannes Berg 
1887225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1888225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1889225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1890225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1891225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1892225b8189SJohannes Berg 	}
1893225b8189SJohannes Berg 
189430686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
189530686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
1896319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1897b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1898319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1899b7ffbd7eSJohannes Berg 		}
19002b9a7e1bSJohannes Berg 
1901319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
19022b9a7e1bSJohannes Berg 			sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1903319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
19042b9a7e1bSJohannes Berg 		}
19052b9a7e1bSJohannes Berg 	}
19062b9a7e1bSJohannes Berg 
19072b9a7e1bSJohannes Berg 	if (sta->chains &&
1908319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1909319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1910319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1911319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1912b7ffbd7eSJohannes Berg 
1913b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1914b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1915b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1916b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1917b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1918b7ffbd7eSJohannes Berg 		}
1919b7ffbd7eSJohannes Berg 	}
1920b7ffbd7eSJohannes Berg 
1921319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1922b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1923319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
19242b9a7e1bSJohannes Berg 	}
19252b9a7e1bSJohannes Berg 
1926319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1927b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1928319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
19292b9a7e1bSJohannes Berg 	}
1930b7ffbd7eSJohannes Berg 
193179c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
193279c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
193379c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
193479c892b8SJohannes Berg 
193579c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
193679c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
193779c892b8SJohannes Berg 			tidstats->rx_msdu = sta->rx_msdu[i];
193879c892b8SJohannes Berg 		}
193979c892b8SJohannes Berg 
194079c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
194179c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
194279c892b8SJohannes Berg 			tidstats->tx_msdu = sta->tx_msdu[i];
194379c892b8SJohannes Berg 		}
194479c892b8SJohannes Berg 
194579c892b8SJohannes Berg 		if (!(tidstats->filled &
194679c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
194730686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
194879c892b8SJohannes Berg 			tidstats->filled |=
194979c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
195079c892b8SJohannes Berg 			tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
195179c892b8SJohannes Berg 		}
195279c892b8SJohannes Berg 
195379c892b8SJohannes Berg 		if (!(tidstats->filled &
195479c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
195530686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
195679c892b8SJohannes Berg 			tidstats->filled |=
195779c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
195879c892b8SJohannes Berg 			tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
195979c892b8SJohannes Berg 		}
196079c892b8SJohannes Berg 	}
196179c892b8SJohannes Berg 
1962b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1963b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1964319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1965319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
1966319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
1967319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
1968319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
1969319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
1970b7ffbd7eSJohannes Berg 
1971*433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
1972*433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
1973*433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
1974b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1975319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1976*433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
1977b7ffbd7eSJohannes Berg 		}
1978*433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
1979*433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
1980*433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
1981b7ffbd7eSJohannes Berg #endif
1982b7ffbd7eSJohannes Berg 	}
1983b7ffbd7eSJohannes Berg 
1984b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1985b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1986b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1987b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1988b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1989b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1990b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1991785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1992b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1993b7ffbd7eSJohannes Berg 
1994b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1995b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1996b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1997b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1998b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1999b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2000b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2001b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2002b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2003b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2004b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2005b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2006a74a8c84SJohannes Berg 	if (sta->sta.wme)
2007b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2008b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2009b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2010b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2011b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2012b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2013b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2014b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2015b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2016b7ffbd7eSJohannes Berg 
2017b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2018b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2019b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2020b7ffbd7eSJohannes Berg 	else
2021b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
2022b7ffbd7eSJohannes Berg 
2023b7ffbd7eSJohannes Berg 	if (thr != 0) {
2024319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2025b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
2026b7ffbd7eSJohannes Berg 	}
2027b7ffbd7eSJohannes Berg }
2028