xref: /openbmc/linux/net/mac80211/sta_info.c (revision a69bd8e60b02946896c097439b94eb77c0c2c9e4)
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));
252433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
253433f5bc1SJohannes Berg 	kfree(sta->mesh);
254433f5bc1SJohannes 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
319433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
320433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
321433f5bc1SJohannes Berg 		if (!sta->mesh)
322433f5bc1SJohannes Berg 			goto free;
323433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
32487f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
32587f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
326433f5bc1SJohannes Berg 			init_timer(&sta->mesh->plink_timer);
327433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
328433f5bc1SJohannes 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:
417433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
418433f5bc1SJohannes Berg 	kfree(sta->mesh);
419433f5bc1SJohannes Berg #endif
420ba8c3d6fSFelix Fietkau 	kfree(sta);
421ba8c3d6fSFelix Fietkau 	return NULL;
42273651ee6SJohannes Berg }
42373651ee6SJohannes Berg 
4248c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
42534e89507SJohannes Berg {
42634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42734e89507SJohannes Berg 
42803e4497eSJohannes Berg 	/*
42903e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
43003e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
43103e4497eSJohannes Berg 	 * and another CPU turns off the net device.
43203e4497eSJohannes Berg 	 */
4338c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4348c71df7aSGuy Eilam 		return -ENETDOWN;
43503e4497eSJohannes Berg 
436b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4378c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4388c71df7aSGuy Eilam 		return -EINVAL;
4398c71df7aSGuy Eilam 
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;
638*a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
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;
6463f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6473f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6483f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
6493f52b7e3SMarco Porsch #endif
650d012a605SMarco Porsch 	} else {
651d012a605SMarco Porsch 		return;
652d012a605SMarco Porsch 	}
653d012a605SMarco Porsch 
654c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
65530686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
656c868cb35SJohannes Berg 		return;
657004c872eSJohannes Berg 
658c868cb35SJohannes Berg 	if (sta->dead)
659c868cb35SJohannes Berg 		goto done;
6603e122be0SJohannes Berg 
661948d887dSJohannes Berg 	/*
662948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
663948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
664948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
665948d887dSJohannes Berg 	 * non-enabled ones.
666948d887dSJohannes Berg 	 */
667948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
668948d887dSJohannes Berg 		ignore_for_tim = 0;
669948d887dSJohannes Berg 
6709b7a86f3SJohannes Berg 	if (ignore_pending)
6719b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6729b7a86f3SJohannes Berg 
673948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
674948d887dSJohannes Berg 		unsigned long tids;
675948d887dSJohannes Berg 
676948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
677948d887dSJohannes Berg 			continue;
678948d887dSJohannes Berg 
679948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
680948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
681948d887dSJohannes Berg 		if (indicate_tim)
682948d887dSJohannes Berg 			break;
683948d887dSJohannes Berg 
684948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
685948d887dSJohannes Berg 
686948d887dSJohannes Berg 		indicate_tim |=
687948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
688ba8c3d6fSFelix Fietkau 		indicate_tim |=
689ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
690004c872eSJohannes Berg 	}
691004c872eSJohannes Berg 
692c868cb35SJohannes Berg  done:
69365f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
694004c872eSJohannes Berg 
6953d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6963d5839b6SIlan Peer 		goto out_unlock;
6973d5839b6SIlan Peer 
698948d887dSJohannes Berg 	if (indicate_tim)
699d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
700c868cb35SJohannes Berg 	else
701d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7023e122be0SJohannes Berg 
7039b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
704c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
705948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
706c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
707004c872eSJohannes Berg 	}
708004c872eSJohannes Berg 
7093d5839b6SIlan Peer out_unlock:
71065f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
711004c872eSJohannes Berg }
712004c872eSJohannes Berg 
7139b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7149b7a86f3SJohannes Berg {
7159b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7169b7a86f3SJohannes Berg }
7179b7a86f3SJohannes Berg 
718cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
719f0706e82SJiri Benc {
720e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
721f0706e82SJiri Benc 	int timeout;
722f0706e82SJiri Benc 
723f0706e82SJiri Benc 	if (!skb)
724cd0b8d89SJohannes Berg 		return false;
725f0706e82SJiri Benc 
726e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
727f0706e82SJiri Benc 
728f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
72957c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
73057c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
73157c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
732f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
733f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
734e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
735f0706e82SJiri Benc }
736f0706e82SJiri Benc 
737f0706e82SJiri Benc 
738948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
739948d887dSJohannes Berg 						struct sta_info *sta, int ac)
740f0706e82SJiri Benc {
741f0706e82SJiri Benc 	unsigned long flags;
742f0706e82SJiri Benc 	struct sk_buff *skb;
743f0706e82SJiri Benc 
74460750397SJohannes Berg 	/*
74560750397SJohannes Berg 	 * First check for frames that should expire on the filtered
74660750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
74760750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
74860750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
74960750397SJohannes Berg 	 * total_ps_buffered counter.
75060750397SJohannes Berg 	 */
751f0706e82SJiri Benc 	for (;;) {
752948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
753948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
75457c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
755948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
756836341a7SJohannes Berg 		else
757f0706e82SJiri Benc 			skb = NULL;
758948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
759f0706e82SJiri Benc 
76060750397SJohannes Berg 		/*
76160750397SJohannes Berg 		 * Frames are queued in order, so if this one
76260750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
76360750397SJohannes Berg 		 * we actually reached the end of the queue we
76460750397SJohannes Berg 		 * also need to stop, of course.
76560750397SJohannes Berg 		 */
76660750397SJohannes Berg 		if (!skb)
76760750397SJohannes Berg 			break;
768d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
76960750397SJohannes Berg 	}
77060750397SJohannes Berg 
77160750397SJohannes Berg 	/*
77260750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
77360750397SJohannes Berg 	 * only find something if the filtered queue was emptied
77460750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
77560750397SJohannes Berg 	 * buffered frames.
77660750397SJohannes Berg 	 */
777f0706e82SJiri Benc 	for (;;) {
778948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
779948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
780f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
781948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
782f0706e82SJiri Benc 		else
783f0706e82SJiri Benc 			skb = NULL;
784948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
785f0706e82SJiri Benc 
78660750397SJohannes Berg 		/*
78760750397SJohannes Berg 		 * frames are queued in order, so if this one
78860750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
78960750397SJohannes Berg 		 * the queue) we can stop testing
79060750397SJohannes Berg 		 */
791836341a7SJohannes Berg 		if (!skb)
792836341a7SJohannes Berg 			break;
793836341a7SJohannes Berg 
794f0706e82SJiri Benc 		local->total_ps_buffered--;
795bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
796bdcbd8e0SJohannes Berg 		       sta->sta.addr);
797d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
798f0706e82SJiri Benc 	}
7993393a608SJuuso Oikarinen 
80060750397SJohannes Berg 	/*
80160750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
80260750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
80360750397SJohannes Berg 	 * frames.
80460750397SJohannes Berg 	 */
80560750397SJohannes Berg 	sta_info_recalc_tim(sta);
80660750397SJohannes Berg 
80760750397SJohannes Berg 	/*
80860750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
80960750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
81060750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
81160750397SJohannes Berg 	 */
812948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
813948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
814948d887dSJohannes Berg }
815948d887dSJohannes Berg 
816948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
817948d887dSJohannes Berg 					     struct sta_info *sta)
818948d887dSJohannes Berg {
819948d887dSJohannes Berg 	bool have_buffered = false;
820948d887dSJohannes Berg 	int ac;
821948d887dSJohannes Berg 
8223f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8233f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8243f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
825948d887dSJohannes Berg 		return false;
826948d887dSJohannes Berg 
827948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
828948d887dSJohannes Berg 		have_buffered |=
829948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
830948d887dSJohannes Berg 
831948d887dSJohannes Berg 	return have_buffered;
832f0706e82SJiri Benc }
833f0706e82SJiri Benc 
834d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
83534e89507SJohannes Berg {
83634e89507SJohannes Berg 	struct ieee80211_local *local;
83734e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8386d10e46bSJohannes Berg 	int ret;
83934e89507SJohannes Berg 
84034e89507SJohannes Berg 	might_sleep();
84134e89507SJohannes Berg 
84234e89507SJohannes Berg 	if (!sta)
84334e89507SJohannes Berg 		return -ENOENT;
84434e89507SJohannes Berg 
84534e89507SJohannes Berg 	local = sta->local;
84634e89507SJohannes Berg 	sdata = sta->sdata;
84734e89507SJohannes Berg 
84883d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
84983d5cc01SJohannes Berg 
850098a6070SJohannes Berg 	/*
851098a6070SJohannes Berg 	 * Before removing the station from the driver and
852098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
853098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
854098a6070SJohannes Berg 	 * will be sufficient.
855098a6070SJohannes Berg 	 */
856c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
857c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
858098a6070SJohannes Berg 
85934e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
860b01711beSJohannes Berg 	if (WARN_ON(ret))
86134e89507SJohannes Berg 		return ret;
86234e89507SJohannes Berg 
863a7a6bdd0SArik Nemtsov 	/*
864a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
865a7a6bdd0SArik Nemtsov 	 * removal.
866a7a6bdd0SArik Nemtsov 	 */
867a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
868a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
869a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
870a7a6bdd0SArik Nemtsov 	}
871a7a6bdd0SArik Nemtsov 
872794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8734d33960bSJohannes Berg 
8746a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8756a9d1b91SJohannes Berg 
876a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
877a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
878a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
879a710c816SJohannes Berg 
880d778207bSJohannes Berg 	return 0;
881d778207bSJohannes Berg }
882d778207bSJohannes Berg 
883d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
884d778207bSJohannes Berg {
885d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
886d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8876f7a8d26SJohannes Berg 	struct station_info sinfo = {};
888d778207bSJohannes Berg 	int ret;
889d778207bSJohannes Berg 
890d778207bSJohannes Berg 	/*
891d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
892d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
893d778207bSJohannes Berg 	 */
894d778207bSJohannes Berg 
895d778207bSJohannes Berg 	might_sleep();
896d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
897d778207bSJohannes Berg 
898c8782078SJohannes Berg 	/* now keys can no longer be reached */
8996d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
90034e89507SJohannes Berg 
9019b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9029b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9039b7a86f3SJohannes Berg 
90434e89507SJohannes Berg 	sta->dead = true;
90534e89507SJohannes Berg 
90634e89507SJohannes Berg 	local->num_sta--;
90734e89507SJohannes Berg 	local->sta_generation++;
90834e89507SJohannes Berg 
90983d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
910f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
911f09603a2SJohannes Berg 		if (ret) {
91283d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
91383d5cc01SJohannes Berg 			break;
91483d5cc01SJohannes Berg 		}
91583d5cc01SJohannes Berg 	}
916d9a7ddb0SJohannes Berg 
917f09603a2SJohannes Berg 	if (sta->uploaded) {
918f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
919f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
920f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
921f09603a2SJohannes Berg 	}
92234e89507SJohannes Berg 
923bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
924bdcbd8e0SJohannes Berg 
9256f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
9266f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
927ec15e68bSJouni Malinen 
92834e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
92934e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
93021f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
93134e89507SJohannes Berg 
932d34ba216SJohannes Berg 	cleanup_single_sta(sta);
933d778207bSJohannes Berg }
934d778207bSJohannes Berg 
935d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
936d778207bSJohannes Berg {
937d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
938d778207bSJohannes Berg 
939d778207bSJohannes Berg 	if (err)
940d778207bSJohannes Berg 		return err;
941d778207bSJohannes Berg 
942d778207bSJohannes Berg 	synchronize_net();
943d778207bSJohannes Berg 
944d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
94534e89507SJohannes Berg 
94634e89507SJohannes Berg 	return 0;
94734e89507SJohannes Berg }
94834e89507SJohannes Berg 
94934e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
95034e89507SJohannes Berg {
95134e89507SJohannes Berg 	struct sta_info *sta;
95234e89507SJohannes Berg 	int ret;
95334e89507SJohannes Berg 
95434e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9557852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
95634e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
95734e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
95834e89507SJohannes Berg 
95934e89507SJohannes Berg 	return ret;
96034e89507SJohannes Berg }
96134e89507SJohannes Berg 
96234e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
96334e89507SJohannes Berg 			      const u8 *addr)
96434e89507SJohannes Berg {
96534e89507SJohannes Berg 	struct sta_info *sta;
96634e89507SJohannes Berg 	int ret;
96734e89507SJohannes Berg 
96834e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9697852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
97034e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
97134e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
97234e89507SJohannes Berg 
97334e89507SJohannes Berg 	return ret;
97434e89507SJohannes Berg }
975f0706e82SJiri Benc 
976f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
977f0706e82SJiri Benc {
978f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
979f0706e82SJiri Benc 	struct sta_info *sta;
9803393a608SJuuso Oikarinen 	bool timer_needed = false;
981f0706e82SJiri Benc 
982d0709a65SJohannes Berg 	rcu_read_lock();
983d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9843393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9853393a608SJuuso Oikarinen 			timer_needed = true;
986d0709a65SJohannes Berg 	rcu_read_unlock();
987f0706e82SJiri Benc 
9885bb644a0SJohannes Berg 	if (local->quiescing)
9895bb644a0SJohannes Berg 		return;
9905bb644a0SJohannes Berg 
9913393a608SJuuso Oikarinen 	if (!timer_needed)
9923393a608SJuuso Oikarinen 		return;
9933393a608SJuuso Oikarinen 
99426d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
99526d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
996f0706e82SJiri Benc }
997f0706e82SJiri Benc 
9987bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
999f0706e82SJiri Benc {
10007bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
10017bedd0cfSJohannes Berg }
10027bedd0cfSJohannes Berg 
10037bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10047bedd0cfSJohannes Berg {
10057bedd0cfSJohannes Berg 	int err;
10067bedd0cfSJohannes Berg 
10077bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
10087bedd0cfSJohannes Berg 	if (err)
10097bedd0cfSJohannes Berg 		return err;
10107bedd0cfSJohannes Berg 
10114d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
101234e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1013f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1014f0706e82SJiri Benc 
1015b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1016b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10177bedd0cfSJohannes Berg 	return 0;
1018f0706e82SJiri Benc }
1019f0706e82SJiri Benc 
1020f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1021f0706e82SJiri Benc {
1022a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
10237bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
1024f0706e82SJiri Benc }
1025f0706e82SJiri Benc 
1026051007d9SJohannes Berg 
1027e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1028f0706e82SJiri Benc {
1029b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1030f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1031d778207bSJohannes Berg 	LIST_HEAD(free_list);
103244213b5eSJohannes Berg 	int ret = 0;
1033f0706e82SJiri Benc 
1034d0709a65SJohannes Berg 	might_sleep();
1035d0709a65SJohannes Berg 
1036e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1037e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1038e716251dSJohannes Berg 
103934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
104034e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1041e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1042e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1043d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1044d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
104534316837SJohannes Berg 			ret++;
104634316837SJohannes Berg 		}
104734e89507SJohannes Berg 	}
1048d778207bSJohannes Berg 
1049d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1050d778207bSJohannes Berg 		synchronize_net();
1051d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1052d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1053d778207bSJohannes Berg 	}
105434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
105544213b5eSJohannes Berg 
1056051007d9SJohannes Berg 	return ret;
1057051007d9SJohannes Berg }
1058051007d9SJohannes Berg 
105924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
106024723d1bSJohannes Berg 			  unsigned long exp_time)
106124723d1bSJohannes Berg {
106224723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
106324723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
106424723d1bSJohannes Berg 
106534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1066e46a2cf9SMohammed Shafi Shajakhan 
1067e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1068ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1069ec2b774eSMarek Lindner 			continue;
1070ec2b774eSMarek Lindner 
107124723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1072eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1073bdcbd8e0SJohannes Berg 				sta->sta.addr);
10743f52b7e3SMarco Porsch 
10753f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10763f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10773f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10783f52b7e3SMarco Porsch 
107934e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
108024723d1bSJohannes Berg 		}
1081e46a2cf9SMohammed Shafi Shajakhan 	}
1082e46a2cf9SMohammed Shafi Shajakhan 
108334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
108424723d1bSJohannes Berg }
108517741cdcSJohannes Berg 
1086686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1087686b9cb9SBen Greear 						   const u8 *addr,
1088686b9cb9SBen Greear 						   const u8 *localaddr)
108917741cdcSJohannes Berg {
10907bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
10917bedd0cfSJohannes Berg 	struct sta_info *sta;
10927bedd0cfSJohannes Berg 	struct rhash_head *tmp;
10937bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
10947bedd0cfSJohannes Berg 
10957bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
109617741cdcSJohannes Berg 
1097686b9cb9SBen Greear 	/*
1098686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1099686b9cb9SBen Greear 	 * ... first in list.
1100686b9cb9SBen Greear 	 */
11017bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1102686b9cb9SBen Greear 		if (localaddr &&
1103b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1104686b9cb9SBen Greear 			continue;
1105f7c65594SJohannes Berg 		if (!sta->uploaded)
1106f7c65594SJohannes Berg 			return NULL;
110717741cdcSJohannes Berg 		return &sta->sta;
1108f7c65594SJohannes Berg 	}
1109f7c65594SJohannes Berg 
1110abe60632SJohannes Berg 	return NULL;
111117741cdcSJohannes Berg }
1112686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11135ed176e1SJohannes Berg 
11145ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11155ed176e1SJohannes Berg 					 const u8 *addr)
11165ed176e1SJohannes Berg {
1117f7c65594SJohannes Berg 	struct sta_info *sta;
11185ed176e1SJohannes Berg 
11195ed176e1SJohannes Berg 	if (!vif)
11205ed176e1SJohannes Berg 		return NULL;
11215ed176e1SJohannes Berg 
1122f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1123f7c65594SJohannes Berg 	if (!sta)
1124f7c65594SJohannes Berg 		return NULL;
11255ed176e1SJohannes Berg 
1126f7c65594SJohannes Berg 	if (!sta->uploaded)
1127f7c65594SJohannes Berg 		return NULL;
1128f7c65594SJohannes Berg 
1129f7c65594SJohannes Berg 	return &sta->sta;
11305ed176e1SJohannes Berg }
113117741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1132af818581SJohannes Berg 
1133e3685e03SJohannes Berg /* powersave support code */
1134e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
113550a9432dSJohannes Berg {
1136608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1137e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1138e3685e03SJohannes Berg 	struct sk_buff_head pending;
1139ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1140e3685e03SJohannes Berg 	unsigned long flags;
1141d012a605SMarco Porsch 	struct ps_data *ps;
1142d012a605SMarco Porsch 
11433918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11443918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11453918edb0SFelix Fietkau 				     u.ap);
11463918edb0SFelix Fietkau 
11473918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1148d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11493f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11503f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1151d012a605SMarco Porsch 	else
1152d012a605SMarco Porsch 		return;
115350a9432dSJohannes Berg 
1154c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
115547086fc5SJohannes Berg 
11565a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1157948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1158ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1159948d887dSJohannes Berg 
116030686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
116112375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1162af818581SJohannes Berg 
1163ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1164ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1165ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1166ba8c3d6fSFelix Fietkau 
1167ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1168ba8c3d6fSFelix Fietkau 				continue;
1169ba8c3d6fSFelix Fietkau 
1170ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1171ba8c3d6fSFelix Fietkau 		}
1172ba8c3d6fSFelix Fietkau 	}
1173ba8c3d6fSFelix Fietkau 
1174948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1175af818581SJohannes Berg 
11761d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11771d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1178af818581SJohannes Berg 	/* Send all buffered frames to the station */
1179948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1180948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1181948d887dSJohannes Berg 
1182987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1183948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1184987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1185948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1186948d887dSJohannes Berg 		filtered += tmp - count;
1187948d887dSJohannes Berg 		count = tmp;
1188948d887dSJohannes Berg 
1189987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1190948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1191987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1192948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1193948d887dSJohannes Berg 		buffered += tmp - count;
1194948d887dSJohannes Berg 	}
1195948d887dSJohannes Berg 
1196e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11975ac2e350SJohannes Berg 
11985ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11995ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
12005ac2e350SJohannes Berg 
12015ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12025ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12035ac2e350SJohannes Berg 	 */
12045ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12055ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12061d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1207948d887dSJohannes Berg 
1208e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1209e3685e03SJohannes Berg 
1210687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1211062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1212062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1213687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1214687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1215687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1216687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1217687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1218687da132SEmmanuel Grumbach 		       sta->sta.addr);
1219687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1220687da132SEmmanuel Grumbach 					   sta->sta.addr,
1221687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1222687da132SEmmanuel Grumbach 	}
1223687da132SEmmanuel Grumbach 
1224af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1225af818581SJohannes Berg 
1226c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1227c868cb35SJohannes Berg 
1228bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1229bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1230bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
123117c18bf8SJohannes Berg 
123217c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1233af818581SJohannes Berg }
1234af818581SJohannes Berg 
1235ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1236ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1237b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1238b77cf4f8SJohannes Berg 					 bool call_driver)
1239ce662b44SJohannes Berg {
1240ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1241ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1242ce662b44SJohannes Berg 	struct sk_buff *skb;
1243ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1244ce662b44SJohannes Berg 	__le16 fc;
1245a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1246ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
124755de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1248ce662b44SJohannes Berg 
1249ce662b44SJohannes Berg 	if (qos) {
1250ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1251ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1252ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1253ce662b44SJohannes Berg 	} else {
1254ce662b44SJohannes Berg 		size -= 2;
1255ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1256ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1257ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1258ce662b44SJohannes Berg 	}
1259ce662b44SJohannes Berg 
1260ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1261ce662b44SJohannes Berg 	if (!skb)
1262ce662b44SJohannes Berg 		return;
1263ce662b44SJohannes Berg 
1264ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1265ce662b44SJohannes Berg 
1266ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1267ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1268ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1269ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1270ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1271ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1272864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1273ce662b44SJohannes Berg 
1274ce662b44SJohannes Berg 	skb->priority = tid;
1275ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
127659b66255SJohannes Berg 	if (qos) {
1277ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1278ce662b44SJohannes Berg 
127940b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1280ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1281ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1282ce662b44SJohannes Berg 	}
1283ce662b44SJohannes Berg 
1284ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1285ce662b44SJohannes Berg 
1286ce662b44SJohannes Berg 	/*
1287ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1288ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1289deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1290deeaee19SJohannes Berg 	 * ends the poll/service period.
1291ce662b44SJohannes Berg 	 */
129202f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1293deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1294ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1295ce662b44SJohannes Berg 
12966b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
12976b127c71SSujith Manoharan 
1298b77cf4f8SJohannes Berg 	if (call_driver)
1299b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1300b77cf4f8SJohannes Berg 					  reason, false);
130140b96408SJohannes Berg 
130289afe614SJohannes Berg 	skb->dev = sdata->dev;
130389afe614SJohannes Berg 
130455de908aSJohannes Berg 	rcu_read_lock();
130555de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
130655de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
130755de908aSJohannes Berg 		rcu_read_unlock();
130855de908aSJohannes Berg 		kfree_skb(skb);
130955de908aSJohannes Berg 		return;
131055de908aSJohannes Berg 	}
131155de908aSJohannes Berg 
131273c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13137c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
131455de908aSJohannes Berg 	rcu_read_unlock();
1315ce662b44SJohannes Berg }
1316ce662b44SJohannes Berg 
13170a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13180a1cb809SJohannes Berg {
13190a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13200a1cb809SJohannes Berg 	if (tids & 0xF8)
13210a1cb809SJohannes Berg 		return fls(tids) - 1;
13220a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13230a1cb809SJohannes Berg 	if (tids & BIT(0))
13240a1cb809SJohannes Berg 		return 0;
13250a1cb809SJohannes Berg 	return fls(tids) - 1;
13260a1cb809SJohannes Berg }
13270a1cb809SJohannes Berg 
132847086fc5SJohannes Berg static void
132947086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
133047086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
133147086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1332af818581SJohannes Berg {
1333af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1334af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1335948d887dSJohannes Berg 	bool more_data = false;
1336948d887dSJohannes Berg 	int ac;
13374049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
133847086fc5SJohannes Berg 	struct sk_buff_head frames;
133947086fc5SJohannes Berg 
1340deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1341c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1342deeaee19SJohannes Berg 
134347086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1344af818581SJohannes Berg 
1345f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1346948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13474049e09aSJohannes Berg 		unsigned long tids;
13484049e09aSJohannes Berg 
134947086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1350948d887dSJohannes Berg 			continue;
1351948d887dSJohannes Berg 
13524049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13534049e09aSJohannes Berg 
1354f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1355f9f760b4SJohannes Berg 		 * release from hardware queues
1356f9f760b4SJohannes Berg 		 */
1357ba8c3d6fSFelix Fietkau 		if (skb_queue_empty(&frames)) {
1358f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1359ba8c3d6fSFelix Fietkau 			driver_release_tids |= sta->txq_buffered_tids & tids;
1360ba8c3d6fSFelix Fietkau 		}
1361f9f760b4SJohannes Berg 
13624049e09aSJohannes Berg 		if (driver_release_tids) {
1363f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1364f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1365f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1366f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1367f9f760b4SJohannes Berg 			 */
1368f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1369f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1370f9f760b4SJohannes Berg 				more_data = true;
1371f9f760b4SJohannes Berg 				driver_release_tids =
1372f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1373f9f760b4SJohannes Berg 						driver_release_tids));
1374f9f760b4SJohannes Berg 				break;
1375f9f760b4SJohannes Berg 			}
13764049e09aSJohannes Berg 		} else {
137747086fc5SJohannes Berg 			struct sk_buff *skb;
137847086fc5SJohannes Berg 
137947086fc5SJohannes Berg 			while (n_frames > 0) {
1380948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1381948d887dSJohannes Berg 				if (!skb) {
138247086fc5SJohannes Berg 					skb = skb_dequeue(
138347086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1384af818581SJohannes Berg 					if (skb)
1385af818581SJohannes Berg 						local->total_ps_buffered--;
1386af818581SJohannes Berg 				}
138747086fc5SJohannes Berg 				if (!skb)
138847086fc5SJohannes Berg 					break;
138947086fc5SJohannes Berg 				n_frames--;
139047086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
139147086fc5SJohannes Berg 			}
1392948d887dSJohannes Berg 		}
1393948d887dSJohannes Berg 
1394f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1395f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1396f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13974049e09aSJohannes Berg 		 */
1398948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1399948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1400948d887dSJohannes Berg 			more_data = true;
1401948d887dSJohannes Berg 			break;
1402948d887dSJohannes Berg 		}
1403948d887dSJohannes Berg 	}
1404af818581SJohannes Berg 
1405f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1406ce662b44SJohannes Berg 		int tid;
14074049e09aSJohannes Berg 
1408ce662b44SJohannes Berg 		/*
1409ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1410ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1411ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1412ce662b44SJohannes Berg 		 *
1413ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1414ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1415ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1416ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1417ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1418ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1419ce662b44SJohannes Berg 		 *
1420ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1421ce662b44SJohannes Berg 		 */
1422ce662b44SJohannes Berg 
1423ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1424ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1425ce662b44SJohannes Berg 
1426b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1427f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
142847086fc5SJohannes Berg 		struct sk_buff_head pending;
142947086fc5SJohannes Berg 		struct sk_buff *skb;
143040b96408SJohannes Berg 		int num = 0;
143140b96408SJohannes Berg 		u16 tids = 0;
1432b77cf4f8SJohannes Berg 		bool need_null = false;
143347086fc5SJohannes Berg 
143447086fc5SJohannes Berg 		skb_queue_head_init(&pending);
143547086fc5SJohannes Berg 
143647086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1437af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
143847086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
143940b96408SJohannes Berg 			u8 *qoshdr = NULL;
144040b96408SJohannes Berg 
144140b96408SJohannes Berg 			num++;
1442af818581SJohannes Berg 
1443af818581SJohannes Berg 			/*
144447086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
144547086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
144647086fc5SJohannes Berg 			 * exchange.
1447af818581SJohannes Berg 			 */
14486b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
14496b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1450af818581SJohannes Berg 
145147086fc5SJohannes Berg 			/*
145247086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
145347086fc5SJohannes Berg 			 * more buffered frames for this STA
145447086fc5SJohannes Berg 			 */
145524b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
145647086fc5SJohannes Berg 				hdr->frame_control |=
145747086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
145824b9c373SJanusz.Dziedzic@tieto.com 			else
145924b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
146024b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1461af818581SJohannes Berg 
146240b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
146340b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
146440b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
146540b96408SJohannes Berg 
1466b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1467b77cf4f8SJohannes Berg 
1468b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1469b77cf4f8SJohannes Berg 
1470b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1471b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1472b77cf4f8SJohannes Berg 				continue;
1473b77cf4f8SJohannes Berg 
1474b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1475b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1476b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1477b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1478b77cf4f8SJohannes Berg 				break;
1479b77cf4f8SJohannes Berg 			}
1480b77cf4f8SJohannes Berg 
1481b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1482b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1483b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1484b77cf4f8SJohannes Berg 			 * and be done.
1485b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1486b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1487b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1488b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1489b77cf4f8SJohannes Berg 			 *
1490b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1491b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1492b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1493b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1494b77cf4f8SJohannes Berg 			 *
1495b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1496b77cf4f8SJohannes Berg 			 */
1497b77cf4f8SJohannes Berg 			if (qoshdr) {
149840b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1499deeaee19SJohannes Berg 
150047086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
150147086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1502b77cf4f8SJohannes Berg 			} else {
1503b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1504b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1505b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1506b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1507b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1508b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1509b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1510b77cf4f8SJohannes Berg 				 */
1511b77cf4f8SJohannes Berg 				hdr->frame_control |=
1512b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1513b77cf4f8SJohannes Berg 				need_null = true;
1514b77cf4f8SJohannes Berg 				num++;
151552a3f20cSMarco Porsch 			}
1516b77cf4f8SJohannes Berg 			break;
151747086fc5SJohannes Berg 		}
151847086fc5SJohannes Berg 
151940b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
152040b96408SJohannes Berg 					  reason, more_data);
152140b96408SJohannes Berg 
152247086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1523af818581SJohannes Berg 
1524b77cf4f8SJohannes Berg 		if (need_null)
1525b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1526b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1527b77cf4f8SJohannes Berg 				reason, false);
1528b77cf4f8SJohannes Berg 
1529c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1530af818581SJohannes Berg 	} else {
1531ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1532ba8c3d6fSFelix Fietkau 		int tid;
1533ba8c3d6fSFelix Fietkau 
1534af818581SJohannes Berg 		/*
15354049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
15364049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1537f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1538f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1539f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1540f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1541f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1542f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1543af818581SJohannes Berg 		 */
15444049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
154547086fc5SJohannes Berg 					    n_frames, reason, more_data);
15464049e09aSJohannes Berg 
15474049e09aSJohannes Berg 		/*
15484049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
15494049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1550f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
15514049e09aSJohannes Berg 		 * release function.
1552f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1553ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1554ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
15554049e09aSJohannes Berg 		 */
1556ba8c3d6fSFelix Fietkau 
1557ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1558ba8c3d6fSFelix Fietkau 			return;
1559ba8c3d6fSFelix Fietkau 
1560ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1561ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1562ba8c3d6fSFelix Fietkau 
1563ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1564ba8c3d6fSFelix Fietkau 				continue;
1565ba8c3d6fSFelix Fietkau 
1566ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1567ba8c3d6fSFelix Fietkau 			break;
1568ba8c3d6fSFelix Fietkau 		}
1569af818581SJohannes Berg 	}
1570af818581SJohannes Berg }
1571af818581SJohannes Berg 
1572af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1573af818581SJohannes Berg {
157447086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1575af818581SJohannes Berg 
1576af818581SJohannes Berg 	/*
157747086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
157847086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
157947086fc5SJohannes Berg 	 * only from the non-enabled ones.
1580af818581SJohannes Berg 	 */
158147086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
158247086fc5SJohannes Berg 		ignore_for_response = 0;
1583af818581SJohannes Berg 
158447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
158547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1586af818581SJohannes Berg }
158747086fc5SJohannes Berg 
158847086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
158947086fc5SJohannes Berg {
159047086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
159147086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
159247086fc5SJohannes Berg 
159347086fc5SJohannes Berg 	/*
159447086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
159547086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
159647086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
159747086fc5SJohannes Berg 	 * actually getting called.
159847086fc5SJohannes Berg 	 */
159947086fc5SJohannes Berg 	if (!delivery_enabled)
160047086fc5SJohannes Berg 		return;
160147086fc5SJohannes Berg 
160247086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
160347086fc5SJohannes Berg 	case 1:
160447086fc5SJohannes Berg 		n_frames = 2;
160547086fc5SJohannes Berg 		break;
160647086fc5SJohannes Berg 	case 2:
160747086fc5SJohannes Berg 		n_frames = 4;
160847086fc5SJohannes Berg 		break;
160947086fc5SJohannes Berg 	case 3:
161047086fc5SJohannes Berg 		n_frames = 6;
161147086fc5SJohannes Berg 		break;
161247086fc5SJohannes Berg 	case 0:
161347086fc5SJohannes Berg 		/* XXX: what is a good value? */
161413a8098aSAndrei Otcheretianski 		n_frames = 128;
161547086fc5SJohannes Berg 		break;
161647086fc5SJohannes Berg 	}
161747086fc5SJohannes Berg 
161847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
161947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1620af818581SJohannes Berg }
1621af818581SJohannes Berg 
1622af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1623af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1624af818581SJohannes Berg {
1625af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1626af818581SJohannes Berg 
1627b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1628b5878a2dSJohannes Berg 
16295ac2e350SJohannes Berg 	if (block) {
1630c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
163117c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
16325ac2e350SJohannes Berg 		return;
16335ac2e350SJohannes Berg 	}
16345ac2e350SJohannes Berg 
16355ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
16365ac2e350SJohannes Berg 		return;
16375ac2e350SJohannes Berg 
16385ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
16395ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
16405ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16415ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16425ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
16435ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
16445ac2e350SJohannes Berg 		/* must be asleep in this case */
16455ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16465ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16475ac2e350SJohannes Berg 	} else {
16485ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
164917c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
16505ac2e350SJohannes Berg 	}
1651af818581SJohannes Berg }
1652af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1653dcf55fb5SFelix Fietkau 
1654e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
165537fbd908SJohannes Berg {
165637fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
165737fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
165837fbd908SJohannes Berg 
165937fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
166037fbd908SJohannes Berg 
166137fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
166237fbd908SJohannes Berg }
1663e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
166437fbd908SJohannes Berg 
1665042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1666042ec453SJohannes Berg 				u8 tid, bool buffered)
1667dcf55fb5SFelix Fietkau {
1668dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1669dcf55fb5SFelix Fietkau 
16705a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1671042ec453SJohannes Berg 		return;
1672042ec453SJohannes Berg 
16731b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
16741b000789SJohannes Berg 
1675948d887dSJohannes Berg 	if (buffered)
1676948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1677948d887dSJohannes Berg 	else
1678948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1679948d887dSJohannes Berg 
1680c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1681dcf55fb5SFelix Fietkau }
1682042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1683d9a7ddb0SJohannes Berg 
168483d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1685d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1686d9a7ddb0SJohannes Berg {
16878bf11d8dSJohannes Berg 	might_sleep();
1688d9a7ddb0SJohannes Berg 
1689d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1690d9a7ddb0SJohannes Berg 		return 0;
1691d9a7ddb0SJohannes Berg 
1692f09603a2SJohannes Berg 	/* check allowed transitions first */
1693f09603a2SJohannes Berg 
1694d9a7ddb0SJohannes Berg 	switch (new_state) {
1695d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1696f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1697d9a7ddb0SJohannes Berg 			return -EINVAL;
1698d9a7ddb0SJohannes Berg 		break;
1699d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1700f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1701f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1702d9a7ddb0SJohannes Berg 			return -EINVAL;
1703d9a7ddb0SJohannes Berg 		break;
1704d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1705f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1706f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1707d9a7ddb0SJohannes Berg 			return -EINVAL;
1708d9a7ddb0SJohannes Berg 		break;
1709d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1710f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1711d9a7ddb0SJohannes Berg 			return -EINVAL;
1712d9a7ddb0SJohannes Berg 		break;
1713d9a7ddb0SJohannes Berg 	default:
1714d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1715d9a7ddb0SJohannes Berg 		return -EINVAL;
1716d9a7ddb0SJohannes Berg 	}
1717d9a7ddb0SJohannes Berg 
1718bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1719bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1720f09603a2SJohannes Berg 
1721f09603a2SJohannes Berg 	/*
1722f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1723f09603a2SJohannes Berg 	 * fail the transition
1724f09603a2SJohannes Berg 	 */
1725f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1726f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1727f09603a2SJohannes Berg 					sta->sta_state, new_state);
1728f09603a2SJohannes Berg 		if (err)
1729f09603a2SJohannes Berg 			return err;
1730f09603a2SJohannes Berg 	}
1731f09603a2SJohannes Berg 
1732f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1733f09603a2SJohannes Berg 
1734f09603a2SJohannes Berg 	switch (new_state) {
1735f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1736f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1737f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1738f09603a2SJohannes Berg 		break;
1739f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1740f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1741f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1742f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1743f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1744f09603a2SJohannes Berg 		break;
1745f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1746f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1747f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1748f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
17497e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17507e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17517e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17527e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1753f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
175417c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
1755f09603a2SJohannes Berg 		}
1756f09603a2SJohannes Berg 		break;
1757f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1758f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
17597e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17607e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17617e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17627e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1763f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
176417c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
1765f09603a2SJohannes Berg 		}
1766f09603a2SJohannes Berg 		break;
1767f09603a2SJohannes Berg 	default:
1768f09603a2SJohannes Berg 		break;
1769f09603a2SJohannes Berg 	}
1770f09603a2SJohannes Berg 
1771d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1772d9a7ddb0SJohannes Berg 
1773d9a7ddb0SJohannes Berg 	return 0;
1774d9a7ddb0SJohannes Berg }
1775687da132SEmmanuel Grumbach 
1776687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1777687da132SEmmanuel Grumbach {
1778687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1779687da132SEmmanuel Grumbach 	u8 rx_streams;
1780687da132SEmmanuel Grumbach 
1781687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1782687da132SEmmanuel Grumbach 		return 1;
1783687da132SEmmanuel Grumbach 
1784687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1785687da132SEmmanuel Grumbach 		int i;
1786687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1787687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1788687da132SEmmanuel Grumbach 
1789687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1790687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1791687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1792687da132SEmmanuel Grumbach 				return i + 1;
1793687da132SEmmanuel Grumbach 	}
1794687da132SEmmanuel Grumbach 
1795687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1796687da132SEmmanuel Grumbach 		rx_streams = 4;
1797687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1798687da132SEmmanuel Grumbach 		rx_streams = 3;
1799687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1800687da132SEmmanuel Grumbach 		rx_streams = 2;
1801687da132SEmmanuel Grumbach 	else
1802687da132SEmmanuel Grumbach 		rx_streams = 1;
1803687da132SEmmanuel Grumbach 
1804687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1805687da132SEmmanuel Grumbach 		return rx_streams;
1806687da132SEmmanuel Grumbach 
1807687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1808687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1809687da132SEmmanuel Grumbach }
1810b7ffbd7eSJohannes Berg 
1811b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1812b7ffbd7eSJohannes Berg {
1813b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1814b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
18159a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1816b7ffbd7eSJohannes Berg 	struct timespec uptime;
1817b7ffbd7eSJohannes Berg 	u32 thr = 0;
1818b7ffbd7eSJohannes Berg 	int i, ac;
1819b7ffbd7eSJohannes Berg 
18209a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
18219a244409SJohn W. Linville 		ref = local->rate_ctrl;
18229a244409SJohn W. Linville 
1823b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1824b7ffbd7eSJohannes Berg 
1825225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1826225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1827225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1828225b8189SJohannes Berg 	 */
1829225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1830225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1831225b8189SJohannes Berg 
18322b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
18332b9a7e1bSJohannes Berg 
1834319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1835319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1836319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1837319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1838319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1839319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BEACON_LOSS);
1840b7ffbd7eSJohannes Berg 
184118171520SThomas Gleixner 	ktime_get_ts(&uptime);
1842b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1843b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
18442b9a7e1bSJohannes Berg 
1845319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1846319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1847b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
18482b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1849b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1850319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1851b7ffbd7eSJohannes Berg 	}
18522b9a7e1bSJohannes Berg 
1853319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
18542b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
18552b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
18562b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1857319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
18582b9a7e1bSJohannes Berg 	}
18592b9a7e1bSJohannes Berg 
1860319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1861319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1862b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1863319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
18642b9a7e1bSJohannes Berg 	}
18652b9a7e1bSJohannes Berg 
1866319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1867b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1868319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
18692b9a7e1bSJohannes Berg 	}
18702b9a7e1bSJohannes Berg 
1871319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1872b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1873319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
18742b9a7e1bSJohannes Berg 	}
18752b9a7e1bSJohannes Berg 
1876319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1877b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1878319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
18792b9a7e1bSJohannes Berg 	}
18802b9a7e1bSJohannes Berg 
1881b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1882b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1883b7ffbd7eSJohannes Berg 
1884225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1885225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1886225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1887225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1888225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1889225b8189SJohannes Berg 	}
1890225b8189SJohannes Berg 
189130686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
189230686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
1893319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1894b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1895319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1896b7ffbd7eSJohannes Berg 		}
18972b9a7e1bSJohannes Berg 
1898319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
18992b9a7e1bSJohannes Berg 			sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1900319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
19012b9a7e1bSJohannes Berg 		}
19022b9a7e1bSJohannes Berg 	}
19032b9a7e1bSJohannes Berg 
19042b9a7e1bSJohannes Berg 	if (sta->chains &&
1905319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1906319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1907319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1908319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1909b7ffbd7eSJohannes Berg 
1910b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1911b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1912b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1913b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1914b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1915b7ffbd7eSJohannes Berg 		}
1916b7ffbd7eSJohannes Berg 	}
1917b7ffbd7eSJohannes Berg 
1918319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1919b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1920319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
19212b9a7e1bSJohannes Berg 	}
19222b9a7e1bSJohannes Berg 
1923319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1924b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1925319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
19262b9a7e1bSJohannes Berg 	}
1927b7ffbd7eSJohannes Berg 
192879c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
192979c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
193079c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
193179c892b8SJohannes Berg 
193279c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
193379c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
193479c892b8SJohannes Berg 			tidstats->rx_msdu = sta->rx_msdu[i];
193579c892b8SJohannes Berg 		}
193679c892b8SJohannes Berg 
193779c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
193879c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
193979c892b8SJohannes Berg 			tidstats->tx_msdu = sta->tx_msdu[i];
194079c892b8SJohannes Berg 		}
194179c892b8SJohannes Berg 
194279c892b8SJohannes Berg 		if (!(tidstats->filled &
194379c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
194430686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
194579c892b8SJohannes Berg 			tidstats->filled |=
194679c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
194779c892b8SJohannes Berg 			tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
194879c892b8SJohannes Berg 		}
194979c892b8SJohannes Berg 
195079c892b8SJohannes Berg 		if (!(tidstats->filled &
195179c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
195230686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
195379c892b8SJohannes Berg 			tidstats->filled |=
195479c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
195579c892b8SJohannes Berg 			tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
195679c892b8SJohannes Berg 		}
195779c892b8SJohannes Berg 	}
195879c892b8SJohannes Berg 
1959b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1960b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1961319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1962319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
1963319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
1964319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
1965319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
1966319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
1967b7ffbd7eSJohannes Berg 
1968433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
1969433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
1970433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
1971b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1972319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1973433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
1974b7ffbd7eSJohannes Berg 		}
1975433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
1976433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
1977433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
1978b7ffbd7eSJohannes Berg #endif
1979b7ffbd7eSJohannes Berg 	}
1980b7ffbd7eSJohannes Berg 
1981b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1982b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1983b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1984b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1985b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1986b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1987b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1988785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1989b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1990b7ffbd7eSJohannes Berg 
1991b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1992b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1993b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1994b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1995b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1996b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1997b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1998b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1999b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2000b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2001b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2002b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2003a74a8c84SJohannes Berg 	if (sta->sta.wme)
2004b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2005b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2006b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2007b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2008b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2009b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2010b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2011b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2012b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2013b7ffbd7eSJohannes Berg 
2014b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2015b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2016b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2017b7ffbd7eSJohannes Berg 	else
2018b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
2019b7ffbd7eSJohannes Berg 
2020b7ffbd7eSJohannes Berg 	if (thr != 0) {
2021319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2022b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
2023b7ffbd7eSJohannes Berg 	}
2024b7ffbd7eSJohannes Berg }
2025