xref: /openbmc/linux/net/mac80211/sta_info.c (revision ac100ce52a2d3b6261a06939d22e4382d9aa0bb2)
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),
71*ac100ce5SJohannes 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));
25293e5deb1SJohannes Berg 	kfree(sta);
25393e5deb1SJohannes Berg }
25493e5deb1SJohannes Berg 
2554d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
256d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
257d0709a65SJohannes Berg 			      struct sta_info *sta)
258f0706e82SJiri Benc {
2597bedd0cfSJohannes Berg 	rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
2607bedd0cfSJohannes Berg 			       sta_rht_params);
261f0706e82SJiri Benc }
262f0706e82SJiri Benc 
2635ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
264af818581SJohannes Berg {
265af818581SJohannes Berg 	struct sta_info *sta;
266af818581SJohannes Berg 
2675ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
268af818581SJohannes Berg 
269af818581SJohannes Berg 	if (sta->dead)
270af818581SJohannes Berg 		return;
271af818581SJohannes Berg 
27254420473SHelmut Schaa 	local_bh_disable();
2735ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
274af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2755ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
276af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2775ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
27847086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
279ce662b44SJohannes Berg 	local_bh_enable();
280af818581SJohannes Berg }
281af818581SJohannes Berg 
282af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
283af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
284af65cd96SJohannes Berg {
28530686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
286af65cd96SJohannes Berg 		return 0;
287af65cd96SJohannes Berg 
288889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
289af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29035c347acSJohannes Berg 						     sta, gfp);
291889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
292af65cd96SJohannes Berg 		return -ENOMEM;
293af65cd96SJohannes Berg 
294af65cd96SJohannes Berg 	return 0;
295af65cd96SJohannes Berg }
296af65cd96SJohannes Berg 
29773651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
29856544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
299f0706e82SJiri Benc {
300d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
301ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
302f0706e82SJiri Benc 	struct sta_info *sta;
303ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
30416c5f15cSRon Rindjunsky 	int i;
305f0706e82SJiri Benc 
306ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
307f0706e82SJiri Benc 	if (!sta)
30873651ee6SJohannes Berg 		return NULL;
309f0706e82SJiri Benc 
31007346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3111d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3125ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
31367c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
314a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
31587f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
31648bf6bedSBob Copeland 	spin_lock_init(&sta->plink_lock);
31787f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
31887f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
31987f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3206c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
32187f59c70SThomas Pedersen #endif
32207346f81SJohannes Berg 
323*ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
32417741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
325d0709a65SJohannes Berg 	sta->local = local;
326d0709a65SJohannes Berg 	sta->sdata = sdata;
3278bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
328f0706e82SJiri Benc 
32971ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
33071ec375cSJohannes Berg 
331b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
332b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
333b6da911bSLiad Kaufman 
33418171520SThomas Gleixner 	ktime_get_ts(&uptime);
335ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
336541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
337ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
338ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
339541a45a1SBruno Randolf 
340ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
341ba8c3d6fSFelix Fietkau 		void *txq_data;
342ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
343ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
344ba8c3d6fSFelix Fietkau 
345ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
346ba8c3d6fSFelix Fietkau 		if (!txq_data)
347ba8c3d6fSFelix Fietkau 			goto free;
348ba8c3d6fSFelix Fietkau 
349ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
350ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
351ba8c3d6fSFelix Fietkau 
352ba8c3d6fSFelix Fietkau 			ieee80211_init_tx_queue(sdata, sta, txq, i);
353abfbc3afSJohannes Berg 		}
354ba8c3d6fSFelix Fietkau 	}
355ba8c3d6fSFelix Fietkau 
356ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
357ba8c3d6fSFelix Fietkau 		goto free_txq;
358f0706e82SJiri Benc 
3595a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
360a622ab72SJohannes Berg 		/*
361a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
362a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
363a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
364a622ab72SJohannes Berg 		 */
36516c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
36616c5f15cSRon Rindjunsky 	}
367948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
368948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
369948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
370948d887dSJohannes Berg 	}
37173651ee6SJohannes Berg 
3725a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3734be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
374cccaec98SSenthil Balasubramanian 
375af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
376687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
377687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
378687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
379ba8c3d6fSFelix Fietkau 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
380687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
381687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
382687da132SEmmanuel Grumbach 		/*
383687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
384687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
385687da132SEmmanuel Grumbach 		 */
386687da132SEmmanuel Grumbach 		switch (smps) {
387687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
388687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
389687da132SEmmanuel Grumbach 			break;
390687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
391687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
392687da132SEmmanuel Grumbach 			break;
393687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
394687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
395687da132SEmmanuel Grumbach 			break;
396687da132SEmmanuel Grumbach 		default:
397687da132SEmmanuel Grumbach 			WARN_ON(1);
398687da132SEmmanuel Grumbach 		}
399687da132SEmmanuel Grumbach 	}
400af0ed69bSJohannes Berg 
401bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
402ef04a297SJohannes Berg 
403abfbc3afSJohannes Berg 	return sta;
404ba8c3d6fSFelix Fietkau 
405ba8c3d6fSFelix Fietkau free_txq:
406ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
407ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
408ba8c3d6fSFelix Fietkau free:
409ba8c3d6fSFelix Fietkau 	kfree(sta);
410ba8c3d6fSFelix Fietkau 	return NULL;
41173651ee6SJohannes Berg }
41273651ee6SJohannes Berg 
4138c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
41434e89507SJohannes Berg {
41534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
41634e89507SJohannes Berg 
41703e4497eSJohannes Berg 	/*
41803e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
41903e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
42003e4497eSJohannes Berg 	 * and another CPU turns off the net device.
42103e4497eSJohannes Berg 	 */
4228c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4238c71df7aSGuy Eilam 		return -ENETDOWN;
42403e4497eSJohannes Berg 
425b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4268c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4278c71df7aSGuy Eilam 		return -EINVAL;
4288c71df7aSGuy Eilam 
4298c71df7aSGuy Eilam 	return 0;
43093e5deb1SJohannes Berg }
43144213b5eSJohannes Berg 
432f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
433f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
434f09603a2SJohannes Berg 				     struct sta_info *sta)
435f09603a2SJohannes Berg {
436f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
437f09603a2SJohannes Berg 	int err = 0;
438f09603a2SJohannes Berg 
439f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
440f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
441f09603a2SJohannes Berg 		if (err)
442f09603a2SJohannes Berg 			break;
443f09603a2SJohannes Berg 	}
444f09603a2SJohannes Berg 
445f09603a2SJohannes Berg 	if (!err) {
446a4ec45a4SJohannes Berg 		/*
447a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
448a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
449a4ec45a4SJohannes Berg 		 */
450a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
451f09603a2SJohannes Berg 			sta->uploaded = true;
452f09603a2SJohannes Berg 		return 0;
453f09603a2SJohannes Berg 	}
454f09603a2SJohannes Berg 
455f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
456bdcbd8e0SJohannes Berg 		sdata_info(sdata,
457bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
458bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
459f09603a2SJohannes Berg 		err = 0;
460f09603a2SJohannes Berg 	}
461f09603a2SJohannes Berg 
462f09603a2SJohannes Berg 	/* unwind on error */
463f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
464f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
465f09603a2SJohannes Berg 
466f09603a2SJohannes Berg 	return err;
467f09603a2SJohannes Berg }
468f09603a2SJohannes Berg 
46934e89507SJohannes Berg /*
4708c71df7aSGuy Eilam  * should be called with sta_mtx locked
4718c71df7aSGuy Eilam  * this function replaces the mutex lock
4728c71df7aSGuy Eilam  * with a RCU lock
4738c71df7aSGuy Eilam  */
4744d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4758c71df7aSGuy Eilam {
4768c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4778c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4787852e361SJohannes Berg 	struct station_info sinfo;
4798c71df7aSGuy Eilam 	int err = 0;
4808c71df7aSGuy Eilam 
4818c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4828c71df7aSGuy Eilam 
4837852e361SJohannes Berg 	/* check if STA exists already */
4847852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4854d33960bSJohannes Berg 		err = -EEXIST;
4864d33960bSJohannes Berg 		goto out_err;
48734e89507SJohannes Berg 	}
48834e89507SJohannes Berg 
4894d33960bSJohannes Berg 	local->num_sta++;
4904d33960bSJohannes Berg 	local->sta_generation++;
4914d33960bSJohannes Berg 	smp_mb();
4924d33960bSJohannes Berg 
4935108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
4945108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4955108ca82SJohannes Berg 
4964d33960bSJohannes Berg 	/* make the station visible */
4974d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
4984d33960bSJohannes Berg 
4992bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
50083d5cc01SJohannes Berg 
5015108ca82SJohannes Berg 	/* notify driver */
5025108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5035108ca82SJohannes Berg 	if (err)
5045108ca82SJohannes Berg 		goto out_remove;
5055108ca82SJohannes Berg 
50683d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5075108ca82SJohannes Berg 	/* accept BA sessions now */
5085108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5094d33960bSJohannes Berg 
51021f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
5114d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5124d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5134d33960bSJohannes Berg 
5144d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5154d33960bSJohannes Berg 	sinfo.filled = 0;
5164d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5174d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
518d0709a65SJohannes Berg 
519bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
520f0706e82SJiri Benc 
52134e89507SJohannes Berg 	/* move reference to rcu-protected */
52234e89507SJohannes Berg 	rcu_read_lock();
52334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
524e9f207f0SJiri Benc 
52573651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
52673651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
52773651ee6SJohannes Berg 
52873651ee6SJohannes Berg 	return 0;
5295108ca82SJohannes Berg  out_remove:
5305108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5315108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5325108ca82SJohannes Berg 	local->num_sta--;
5335108ca82SJohannes Berg 	synchronize_net();
5345108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5354d33960bSJohannes Berg  out_err:
5364d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5374d33960bSJohannes Berg 	rcu_read_lock();
5384d33960bSJohannes Berg 	return err;
5398c71df7aSGuy Eilam }
5408c71df7aSGuy Eilam 
5418c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5428c71df7aSGuy Eilam {
5438c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
544308f7fcfSZhao, Gang 	int err;
5458c71df7aSGuy Eilam 
5464d33960bSJohannes Berg 	might_sleep();
5474d33960bSJohannes Berg 
5488c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5498c71df7aSGuy Eilam 	if (err) {
5508c71df7aSGuy Eilam 		rcu_read_lock();
5518c71df7aSGuy Eilam 		goto out_free;
5528c71df7aSGuy Eilam 	}
5538c71df7aSGuy Eilam 
554004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
555004c872eSJohannes Berg 
5564d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5578c71df7aSGuy Eilam 	if (err)
558004c872eSJohannes Berg 		goto out_free;
559d0709a65SJohannes Berg 
560004c872eSJohannes Berg 	return 0;
56193e5deb1SJohannes Berg  out_free:
562d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
56393e5deb1SJohannes Berg 	return err;
564f0706e82SJiri Benc }
565f0706e82SJiri Benc 
56634e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
56734e89507SJohannes Berg {
56834e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
56934e89507SJohannes Berg 
57034e89507SJohannes Berg 	rcu_read_unlock();
57134e89507SJohannes Berg 
57234e89507SJohannes Berg 	return err;
57334e89507SJohannes Berg }
57434e89507SJohannes Berg 
575d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
576004c872eSJohannes Berg {
577004c872eSJohannes Berg 	/*
578004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
579004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
580004c872eSJohannes Berg 	 */
581d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
582004c872eSJohannes Berg }
583004c872eSJohannes Berg 
584d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
585004c872eSJohannes Berg {
586004c872eSJohannes Berg 	/*
587004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
588004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
589004c872eSJohannes Berg 	 */
590d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
591004c872eSJohannes Berg }
592004c872eSJohannes Berg 
5933d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
5943d5839b6SIlan Peer {
5953d5839b6SIlan Peer 	/*
5963d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
5973d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
5983d5839b6SIlan Peer 	 */
5993d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6003d5839b6SIlan Peer }
6013d5839b6SIlan Peer 
602948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
603004c872eSJohannes Berg {
604948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
605948d887dSJohannes Berg 	switch (ac) {
606948d887dSJohannes Berg 	case IEEE80211_AC_VO:
607948d887dSJohannes Berg 		return BIT(6) | BIT(7);
608948d887dSJohannes Berg 	case IEEE80211_AC_VI:
609948d887dSJohannes Berg 		return BIT(4) | BIT(5);
610948d887dSJohannes Berg 	case IEEE80211_AC_BE:
611948d887dSJohannes Berg 		return BIT(0) | BIT(3);
612948d887dSJohannes Berg 	case IEEE80211_AC_BK:
613948d887dSJohannes Berg 		return BIT(1) | BIT(2);
614948d887dSJohannes Berg 	default:
615948d887dSJohannes Berg 		WARN_ON(1);
616948d887dSJohannes Berg 		return 0;
617d0709a65SJohannes Berg 	}
618004c872eSJohannes Berg }
619004c872eSJohannes Berg 
6209b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
621004c872eSJohannes Berg {
622c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
623d012a605SMarco Porsch 	struct ps_data *ps;
624948d887dSJohannes Berg 	bool indicate_tim = false;
625948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
626948d887dSJohannes Berg 	int ac;
627d012a605SMarco Porsch 	u16 id;
628004c872eSJohannes Berg 
629d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
630d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
631c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
632c868cb35SJohannes Berg 			return;
6333e122be0SJohannes Berg 
634d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
635d012a605SMarco Porsch 		id = sta->sta.aid;
6363f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6373f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6383f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
639204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6406f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6413f52b7e3SMarco Porsch #endif
642d012a605SMarco Porsch 	} else {
643d012a605SMarco Porsch 		return;
644d012a605SMarco Porsch 	}
645d012a605SMarco Porsch 
646c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
64730686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
648c868cb35SJohannes Berg 		return;
649004c872eSJohannes Berg 
650c868cb35SJohannes Berg 	if (sta->dead)
651c868cb35SJohannes Berg 		goto done;
6523e122be0SJohannes Berg 
653948d887dSJohannes Berg 	/*
654948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
655948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
656948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
657948d887dSJohannes Berg 	 * non-enabled ones.
658948d887dSJohannes Berg 	 */
659948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
660948d887dSJohannes Berg 		ignore_for_tim = 0;
661948d887dSJohannes Berg 
6629b7a86f3SJohannes Berg 	if (ignore_pending)
6639b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6649b7a86f3SJohannes Berg 
665948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
666948d887dSJohannes Berg 		unsigned long tids;
667948d887dSJohannes Berg 
668948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
669948d887dSJohannes Berg 			continue;
670948d887dSJohannes Berg 
671948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
672948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
673948d887dSJohannes Berg 		if (indicate_tim)
674948d887dSJohannes Berg 			break;
675948d887dSJohannes Berg 
676948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
677948d887dSJohannes Berg 
678948d887dSJohannes Berg 		indicate_tim |=
679948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
680ba8c3d6fSFelix Fietkau 		indicate_tim |=
681ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
682004c872eSJohannes Berg 	}
683004c872eSJohannes Berg 
684c868cb35SJohannes Berg  done:
68565f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
686004c872eSJohannes Berg 
6873d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6883d5839b6SIlan Peer 		goto out_unlock;
6893d5839b6SIlan Peer 
690948d887dSJohannes Berg 	if (indicate_tim)
691d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
692c868cb35SJohannes Berg 	else
693d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6943e122be0SJohannes Berg 
6959b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
696c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
697948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
698c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
699004c872eSJohannes Berg 	}
700004c872eSJohannes Berg 
7013d5839b6SIlan Peer out_unlock:
70265f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
703004c872eSJohannes Berg }
704004c872eSJohannes Berg 
7059b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7069b7a86f3SJohannes Berg {
7079b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7089b7a86f3SJohannes Berg }
7099b7a86f3SJohannes Berg 
710cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
711f0706e82SJiri Benc {
712e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
713f0706e82SJiri Benc 	int timeout;
714f0706e82SJiri Benc 
715f0706e82SJiri Benc 	if (!skb)
716cd0b8d89SJohannes Berg 		return false;
717f0706e82SJiri Benc 
718e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
719f0706e82SJiri Benc 
720f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
72157c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
72257c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
72357c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
724f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
725f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
726e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
727f0706e82SJiri Benc }
728f0706e82SJiri Benc 
729f0706e82SJiri Benc 
730948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
731948d887dSJohannes Berg 						struct sta_info *sta, int ac)
732f0706e82SJiri Benc {
733f0706e82SJiri Benc 	unsigned long flags;
734f0706e82SJiri Benc 	struct sk_buff *skb;
735f0706e82SJiri Benc 
73660750397SJohannes Berg 	/*
73760750397SJohannes Berg 	 * First check for frames that should expire on the filtered
73860750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
73960750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
74060750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
74160750397SJohannes Berg 	 * total_ps_buffered counter.
74260750397SJohannes Berg 	 */
743f0706e82SJiri Benc 	for (;;) {
744948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
745948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
74657c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
747948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
748836341a7SJohannes Berg 		else
749f0706e82SJiri Benc 			skb = NULL;
750948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
751f0706e82SJiri Benc 
75260750397SJohannes Berg 		/*
75360750397SJohannes Berg 		 * Frames are queued in order, so if this one
75460750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
75560750397SJohannes Berg 		 * we actually reached the end of the queue we
75660750397SJohannes Berg 		 * also need to stop, of course.
75760750397SJohannes Berg 		 */
75860750397SJohannes Berg 		if (!skb)
75960750397SJohannes Berg 			break;
760d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
76160750397SJohannes Berg 	}
76260750397SJohannes Berg 
76360750397SJohannes Berg 	/*
76460750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
76560750397SJohannes Berg 	 * only find something if the filtered queue was emptied
76660750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
76760750397SJohannes Berg 	 * buffered frames.
76860750397SJohannes Berg 	 */
769f0706e82SJiri Benc 	for (;;) {
770948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
771948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
772f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
773948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
774f0706e82SJiri Benc 		else
775f0706e82SJiri Benc 			skb = NULL;
776948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
777f0706e82SJiri Benc 
77860750397SJohannes Berg 		/*
77960750397SJohannes Berg 		 * frames are queued in order, so if this one
78060750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
78160750397SJohannes Berg 		 * the queue) we can stop testing
78260750397SJohannes Berg 		 */
783836341a7SJohannes Berg 		if (!skb)
784836341a7SJohannes Berg 			break;
785836341a7SJohannes Berg 
786f0706e82SJiri Benc 		local->total_ps_buffered--;
787bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
788bdcbd8e0SJohannes Berg 		       sta->sta.addr);
789d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
790f0706e82SJiri Benc 	}
7913393a608SJuuso Oikarinen 
79260750397SJohannes Berg 	/*
79360750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
79460750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
79560750397SJohannes Berg 	 * frames.
79660750397SJohannes Berg 	 */
79760750397SJohannes Berg 	sta_info_recalc_tim(sta);
79860750397SJohannes Berg 
79960750397SJohannes Berg 	/*
80060750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
80160750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
80260750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
80360750397SJohannes Berg 	 */
804948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
805948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
806948d887dSJohannes Berg }
807948d887dSJohannes Berg 
808948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
809948d887dSJohannes Berg 					     struct sta_info *sta)
810948d887dSJohannes Berg {
811948d887dSJohannes Berg 	bool have_buffered = false;
812948d887dSJohannes Berg 	int ac;
813948d887dSJohannes Berg 
8143f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8153f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8163f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
817948d887dSJohannes Berg 		return false;
818948d887dSJohannes Berg 
819948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
820948d887dSJohannes Berg 		have_buffered |=
821948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
822948d887dSJohannes Berg 
823948d887dSJohannes Berg 	return have_buffered;
824f0706e82SJiri Benc }
825f0706e82SJiri Benc 
826d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
82734e89507SJohannes Berg {
82834e89507SJohannes Berg 	struct ieee80211_local *local;
82934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8306d10e46bSJohannes Berg 	int ret;
83134e89507SJohannes Berg 
83234e89507SJohannes Berg 	might_sleep();
83334e89507SJohannes Berg 
83434e89507SJohannes Berg 	if (!sta)
83534e89507SJohannes Berg 		return -ENOENT;
83634e89507SJohannes Berg 
83734e89507SJohannes Berg 	local = sta->local;
83834e89507SJohannes Berg 	sdata = sta->sdata;
83934e89507SJohannes Berg 
84083d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
84183d5cc01SJohannes Berg 
842098a6070SJohannes Berg 	/*
843098a6070SJohannes Berg 	 * Before removing the station from the driver and
844098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
845098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
846098a6070SJohannes Berg 	 * will be sufficient.
847098a6070SJohannes Berg 	 */
848c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
849c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
850098a6070SJohannes Berg 
85134e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
852b01711beSJohannes Berg 	if (WARN_ON(ret))
85334e89507SJohannes Berg 		return ret;
85434e89507SJohannes Berg 
855a7a6bdd0SArik Nemtsov 	/*
856a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
857a7a6bdd0SArik Nemtsov 	 * removal.
858a7a6bdd0SArik Nemtsov 	 */
859a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
860a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
861a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
862a7a6bdd0SArik Nemtsov 	}
863a7a6bdd0SArik Nemtsov 
864794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8654d33960bSJohannes Berg 
8666a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8676a9d1b91SJohannes Berg 
868a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
869a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
870a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
871a710c816SJohannes Berg 
872d778207bSJohannes Berg 	return 0;
873d778207bSJohannes Berg }
874d778207bSJohannes Berg 
875d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
876d778207bSJohannes Berg {
877d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
878d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8796f7a8d26SJohannes Berg 	struct station_info sinfo = {};
880d778207bSJohannes Berg 	int ret;
881d778207bSJohannes Berg 
882d778207bSJohannes Berg 	/*
883d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
884d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
885d778207bSJohannes Berg 	 */
886d778207bSJohannes Berg 
887d778207bSJohannes Berg 	might_sleep();
888d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
889d778207bSJohannes Berg 
890c8782078SJohannes Berg 	/* now keys can no longer be reached */
8916d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
89234e89507SJohannes Berg 
8939b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
8949b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
8959b7a86f3SJohannes Berg 
89634e89507SJohannes Berg 	sta->dead = true;
89734e89507SJohannes Berg 
89834e89507SJohannes Berg 	local->num_sta--;
89934e89507SJohannes Berg 	local->sta_generation++;
90034e89507SJohannes Berg 
90183d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
902f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
903f09603a2SJohannes Berg 		if (ret) {
90483d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
90583d5cc01SJohannes Berg 			break;
90683d5cc01SJohannes Berg 		}
90783d5cc01SJohannes Berg 	}
908d9a7ddb0SJohannes Berg 
909f09603a2SJohannes Berg 	if (sta->uploaded) {
910f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
911f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
912f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
913f09603a2SJohannes Berg 	}
91434e89507SJohannes Berg 
915bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
916bdcbd8e0SJohannes Berg 
9176f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
9186f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
919ec15e68bSJouni Malinen 
92034e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
92134e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
92221f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
92334e89507SJohannes Berg 
924d34ba216SJohannes Berg 	cleanup_single_sta(sta);
925d778207bSJohannes Berg }
926d778207bSJohannes Berg 
927d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
928d778207bSJohannes Berg {
929d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
930d778207bSJohannes Berg 
931d778207bSJohannes Berg 	if (err)
932d778207bSJohannes Berg 		return err;
933d778207bSJohannes Berg 
934d778207bSJohannes Berg 	synchronize_net();
935d778207bSJohannes Berg 
936d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
93734e89507SJohannes Berg 
93834e89507SJohannes Berg 	return 0;
93934e89507SJohannes Berg }
94034e89507SJohannes Berg 
94134e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
94234e89507SJohannes Berg {
94334e89507SJohannes Berg 	struct sta_info *sta;
94434e89507SJohannes Berg 	int ret;
94534e89507SJohannes Berg 
94634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9477852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
94834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
94934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
95034e89507SJohannes Berg 
95134e89507SJohannes Berg 	return ret;
95234e89507SJohannes Berg }
95334e89507SJohannes Berg 
95434e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
95534e89507SJohannes Berg 			      const u8 *addr)
95634e89507SJohannes Berg {
95734e89507SJohannes Berg 	struct sta_info *sta;
95834e89507SJohannes Berg 	int ret;
95934e89507SJohannes Berg 
96034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9617852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
96234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
96334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
96434e89507SJohannes Berg 
96534e89507SJohannes Berg 	return ret;
96634e89507SJohannes Berg }
967f0706e82SJiri Benc 
968f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
969f0706e82SJiri Benc {
970f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
971f0706e82SJiri Benc 	struct sta_info *sta;
9723393a608SJuuso Oikarinen 	bool timer_needed = false;
973f0706e82SJiri Benc 
974d0709a65SJohannes Berg 	rcu_read_lock();
975d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9763393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9773393a608SJuuso Oikarinen 			timer_needed = true;
978d0709a65SJohannes Berg 	rcu_read_unlock();
979f0706e82SJiri Benc 
9805bb644a0SJohannes Berg 	if (local->quiescing)
9815bb644a0SJohannes Berg 		return;
9825bb644a0SJohannes Berg 
9833393a608SJuuso Oikarinen 	if (!timer_needed)
9843393a608SJuuso Oikarinen 		return;
9853393a608SJuuso Oikarinen 
98626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
98726d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
988f0706e82SJiri Benc }
989f0706e82SJiri Benc 
9907bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
991f0706e82SJiri Benc {
9927bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
9937bedd0cfSJohannes Berg }
9947bedd0cfSJohannes Berg 
9957bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
9967bedd0cfSJohannes Berg {
9977bedd0cfSJohannes Berg 	int err;
9987bedd0cfSJohannes Berg 
9997bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
10007bedd0cfSJohannes Berg 	if (err)
10017bedd0cfSJohannes Berg 		return err;
10027bedd0cfSJohannes Berg 
10034d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
100434e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1005f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1006f0706e82SJiri Benc 
1007b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1008b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10097bedd0cfSJohannes Berg 	return 0;
1010f0706e82SJiri Benc }
1011f0706e82SJiri Benc 
1012f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1013f0706e82SJiri Benc {
1014a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
10157bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
1016f0706e82SJiri Benc }
1017f0706e82SJiri Benc 
1018051007d9SJohannes Berg 
1019e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1020f0706e82SJiri Benc {
1021b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1022f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1023d778207bSJohannes Berg 	LIST_HEAD(free_list);
102444213b5eSJohannes Berg 	int ret = 0;
1025f0706e82SJiri Benc 
1026d0709a65SJohannes Berg 	might_sleep();
1027d0709a65SJohannes Berg 
1028e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1029e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1030e716251dSJohannes Berg 
103134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
103234e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1033e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1034e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1035d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1036d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
103734316837SJohannes Berg 			ret++;
103834316837SJohannes Berg 		}
103934e89507SJohannes Berg 	}
1040d778207bSJohannes Berg 
1041d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1042d778207bSJohannes Berg 		synchronize_net();
1043d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1044d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1045d778207bSJohannes Berg 	}
104634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
104744213b5eSJohannes Berg 
1048051007d9SJohannes Berg 	return ret;
1049051007d9SJohannes Berg }
1050051007d9SJohannes Berg 
105124723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
105224723d1bSJohannes Berg 			  unsigned long exp_time)
105324723d1bSJohannes Berg {
105424723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
105524723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
105624723d1bSJohannes Berg 
105734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1058e46a2cf9SMohammed Shafi Shajakhan 
1059e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1060ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1061ec2b774eSMarek Lindner 			continue;
1062ec2b774eSMarek Lindner 
106324723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1064eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1065bdcbd8e0SJohannes Berg 				sta->sta.addr);
10663f52b7e3SMarco Porsch 
10673f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10683f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10693f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10703f52b7e3SMarco Porsch 
107134e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
107224723d1bSJohannes Berg 		}
1073e46a2cf9SMohammed Shafi Shajakhan 	}
1074e46a2cf9SMohammed Shafi Shajakhan 
107534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
107624723d1bSJohannes Berg }
107717741cdcSJohannes Berg 
1078686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1079686b9cb9SBen Greear 						   const u8 *addr,
1080686b9cb9SBen Greear 						   const u8 *localaddr)
108117741cdcSJohannes Berg {
10827bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
10837bedd0cfSJohannes Berg 	struct sta_info *sta;
10847bedd0cfSJohannes Berg 	struct rhash_head *tmp;
10857bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
10867bedd0cfSJohannes Berg 
10877bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
108817741cdcSJohannes Berg 
1089686b9cb9SBen Greear 	/*
1090686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1091686b9cb9SBen Greear 	 * ... first in list.
1092686b9cb9SBen Greear 	 */
10937bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1094686b9cb9SBen Greear 		if (localaddr &&
1095b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1096686b9cb9SBen Greear 			continue;
1097f7c65594SJohannes Berg 		if (!sta->uploaded)
1098f7c65594SJohannes Berg 			return NULL;
109917741cdcSJohannes Berg 		return &sta->sta;
1100f7c65594SJohannes Berg 	}
1101f7c65594SJohannes Berg 
1102abe60632SJohannes Berg 	return NULL;
110317741cdcSJohannes Berg }
1104686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11055ed176e1SJohannes Berg 
11065ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11075ed176e1SJohannes Berg 					 const u8 *addr)
11085ed176e1SJohannes Berg {
1109f7c65594SJohannes Berg 	struct sta_info *sta;
11105ed176e1SJohannes Berg 
11115ed176e1SJohannes Berg 	if (!vif)
11125ed176e1SJohannes Berg 		return NULL;
11135ed176e1SJohannes Berg 
1114f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1115f7c65594SJohannes Berg 	if (!sta)
1116f7c65594SJohannes Berg 		return NULL;
11175ed176e1SJohannes Berg 
1118f7c65594SJohannes Berg 	if (!sta->uploaded)
1119f7c65594SJohannes Berg 		return NULL;
1120f7c65594SJohannes Berg 
1121f7c65594SJohannes Berg 	return &sta->sta;
11225ed176e1SJohannes Berg }
112317741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1124af818581SJohannes Berg 
1125e3685e03SJohannes Berg /* powersave support code */
1126e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
112750a9432dSJohannes Berg {
1128608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1129e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1130e3685e03SJohannes Berg 	struct sk_buff_head pending;
1131ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1132e3685e03SJohannes Berg 	unsigned long flags;
1133d012a605SMarco Porsch 	struct ps_data *ps;
1134d012a605SMarco Porsch 
11353918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11363918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11373918edb0SFelix Fietkau 				     u.ap);
11383918edb0SFelix Fietkau 
11393918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1140d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11413f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11423f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1143d012a605SMarco Porsch 	else
1144d012a605SMarco Porsch 		return;
114550a9432dSJohannes Berg 
1146c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
114747086fc5SJohannes Berg 
11485a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1149948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1150ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1151948d887dSJohannes Berg 
115230686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
115312375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1154af818581SJohannes Berg 
1155ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1156ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1157ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1158ba8c3d6fSFelix Fietkau 
1159ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1160ba8c3d6fSFelix Fietkau 				continue;
1161ba8c3d6fSFelix Fietkau 
1162ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1163ba8c3d6fSFelix Fietkau 		}
1164ba8c3d6fSFelix Fietkau 	}
1165ba8c3d6fSFelix Fietkau 
1166948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1167af818581SJohannes Berg 
11681d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11691d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1170af818581SJohannes Berg 	/* Send all buffered frames to the station */
1171948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1172948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1173948d887dSJohannes Berg 
1174987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1175948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1176987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1177948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1178948d887dSJohannes Berg 		filtered += tmp - count;
1179948d887dSJohannes Berg 		count = tmp;
1180948d887dSJohannes Berg 
1181987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1182948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1183987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1184948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1185948d887dSJohannes Berg 		buffered += tmp - count;
1186948d887dSJohannes Berg 	}
1187948d887dSJohannes Berg 
1188e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11895ac2e350SJohannes Berg 
11905ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11915ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11925ac2e350SJohannes Berg 
11935ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
11945ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
11955ac2e350SJohannes Berg 	 */
11965ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
11975ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
11981d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1199948d887dSJohannes Berg 
1200e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1201e3685e03SJohannes Berg 
1202687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1203062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1204062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1205687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1206687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1207687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1208687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1209687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1210687da132SEmmanuel Grumbach 		       sta->sta.addr);
1211687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1212687da132SEmmanuel Grumbach 					   sta->sta.addr,
1213687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1214687da132SEmmanuel Grumbach 	}
1215687da132SEmmanuel Grumbach 
1216af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1217af818581SJohannes Berg 
1218c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1219c868cb35SJohannes Berg 
1220bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1221bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1222bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
122317c18bf8SJohannes Berg 
122417c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1225af818581SJohannes Berg }
1226af818581SJohannes Berg 
1227ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1228ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1229b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1230b77cf4f8SJohannes Berg 					 bool call_driver)
1231ce662b44SJohannes Berg {
1232ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1233ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1234ce662b44SJohannes Berg 	struct sk_buff *skb;
1235ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1236ce662b44SJohannes Berg 	__le16 fc;
1237a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1238ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
123955de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1240ce662b44SJohannes Berg 
1241ce662b44SJohannes Berg 	if (qos) {
1242ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1243ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1244ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1245ce662b44SJohannes Berg 	} else {
1246ce662b44SJohannes Berg 		size -= 2;
1247ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1248ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1249ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1250ce662b44SJohannes Berg 	}
1251ce662b44SJohannes Berg 
1252ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1253ce662b44SJohannes Berg 	if (!skb)
1254ce662b44SJohannes Berg 		return;
1255ce662b44SJohannes Berg 
1256ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1257ce662b44SJohannes Berg 
1258ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1259ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1260ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1261ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1262ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1263ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1264864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1265ce662b44SJohannes Berg 
1266ce662b44SJohannes Berg 	skb->priority = tid;
1267ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
126859b66255SJohannes Berg 	if (qos) {
1269ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1270ce662b44SJohannes Berg 
127140b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1272ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1273ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1274ce662b44SJohannes Berg 	}
1275ce662b44SJohannes Berg 
1276ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1277ce662b44SJohannes Berg 
1278ce662b44SJohannes Berg 	/*
1279ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1280ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1281deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1282deeaee19SJohannes Berg 	 * ends the poll/service period.
1283ce662b44SJohannes Berg 	 */
128402f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1285deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1286ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1287ce662b44SJohannes Berg 
12886b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
12896b127c71SSujith Manoharan 
1290b77cf4f8SJohannes Berg 	if (call_driver)
1291b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1292b77cf4f8SJohannes Berg 					  reason, false);
129340b96408SJohannes Berg 
129489afe614SJohannes Berg 	skb->dev = sdata->dev;
129589afe614SJohannes Berg 
129655de908aSJohannes Berg 	rcu_read_lock();
129755de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
129855de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
129955de908aSJohannes Berg 		rcu_read_unlock();
130055de908aSJohannes Berg 		kfree_skb(skb);
130155de908aSJohannes Berg 		return;
130255de908aSJohannes Berg 	}
130355de908aSJohannes Berg 
130473c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13057c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
130655de908aSJohannes Berg 	rcu_read_unlock();
1307ce662b44SJohannes Berg }
1308ce662b44SJohannes Berg 
13090a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13100a1cb809SJohannes Berg {
13110a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13120a1cb809SJohannes Berg 	if (tids & 0xF8)
13130a1cb809SJohannes Berg 		return fls(tids) - 1;
13140a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13150a1cb809SJohannes Berg 	if (tids & BIT(0))
13160a1cb809SJohannes Berg 		return 0;
13170a1cb809SJohannes Berg 	return fls(tids) - 1;
13180a1cb809SJohannes Berg }
13190a1cb809SJohannes Berg 
132047086fc5SJohannes Berg static void
132147086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
132247086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
132347086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1324af818581SJohannes Berg {
1325af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1326af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1327948d887dSJohannes Berg 	bool more_data = false;
1328948d887dSJohannes Berg 	int ac;
13294049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
133047086fc5SJohannes Berg 	struct sk_buff_head frames;
133147086fc5SJohannes Berg 
1332deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1333c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1334deeaee19SJohannes Berg 
133547086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1336af818581SJohannes Berg 
1337f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1338948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13394049e09aSJohannes Berg 		unsigned long tids;
13404049e09aSJohannes Berg 
134147086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1342948d887dSJohannes Berg 			continue;
1343948d887dSJohannes Berg 
13444049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13454049e09aSJohannes Berg 
1346f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1347f9f760b4SJohannes Berg 		 * release from hardware queues
1348f9f760b4SJohannes Berg 		 */
1349ba8c3d6fSFelix Fietkau 		if (skb_queue_empty(&frames)) {
1350f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1351ba8c3d6fSFelix Fietkau 			driver_release_tids |= sta->txq_buffered_tids & tids;
1352ba8c3d6fSFelix Fietkau 		}
1353f9f760b4SJohannes Berg 
13544049e09aSJohannes Berg 		if (driver_release_tids) {
1355f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1356f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1357f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1358f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1359f9f760b4SJohannes Berg 			 */
1360f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1361f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1362f9f760b4SJohannes Berg 				more_data = true;
1363f9f760b4SJohannes Berg 				driver_release_tids =
1364f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1365f9f760b4SJohannes Berg 						driver_release_tids));
1366f9f760b4SJohannes Berg 				break;
1367f9f760b4SJohannes Berg 			}
13684049e09aSJohannes Berg 		} else {
136947086fc5SJohannes Berg 			struct sk_buff *skb;
137047086fc5SJohannes Berg 
137147086fc5SJohannes Berg 			while (n_frames > 0) {
1372948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1373948d887dSJohannes Berg 				if (!skb) {
137447086fc5SJohannes Berg 					skb = skb_dequeue(
137547086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1376af818581SJohannes Berg 					if (skb)
1377af818581SJohannes Berg 						local->total_ps_buffered--;
1378af818581SJohannes Berg 				}
137947086fc5SJohannes Berg 				if (!skb)
138047086fc5SJohannes Berg 					break;
138147086fc5SJohannes Berg 				n_frames--;
138247086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
138347086fc5SJohannes Berg 			}
1384948d887dSJohannes Berg 		}
1385948d887dSJohannes Berg 
1386f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1387f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1388f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13894049e09aSJohannes Berg 		 */
1390948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1391948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1392948d887dSJohannes Berg 			more_data = true;
1393948d887dSJohannes Berg 			break;
1394948d887dSJohannes Berg 		}
1395948d887dSJohannes Berg 	}
1396af818581SJohannes Berg 
1397f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1398ce662b44SJohannes Berg 		int tid;
13994049e09aSJohannes Berg 
1400ce662b44SJohannes Berg 		/*
1401ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1402ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1403ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1404ce662b44SJohannes Berg 		 *
1405ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1406ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1407ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1408ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1409ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1410ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1411ce662b44SJohannes Berg 		 *
1412ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1413ce662b44SJohannes Berg 		 */
1414ce662b44SJohannes Berg 
1415ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1416ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1417ce662b44SJohannes Berg 
1418b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1419f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
142047086fc5SJohannes Berg 		struct sk_buff_head pending;
142147086fc5SJohannes Berg 		struct sk_buff *skb;
142240b96408SJohannes Berg 		int num = 0;
142340b96408SJohannes Berg 		u16 tids = 0;
1424b77cf4f8SJohannes Berg 		bool need_null = false;
142547086fc5SJohannes Berg 
142647086fc5SJohannes Berg 		skb_queue_head_init(&pending);
142747086fc5SJohannes Berg 
142847086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1429af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
143047086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
143140b96408SJohannes Berg 			u8 *qoshdr = NULL;
143240b96408SJohannes Berg 
143340b96408SJohannes Berg 			num++;
1434af818581SJohannes Berg 
1435af818581SJohannes Berg 			/*
143647086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
143747086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
143847086fc5SJohannes Berg 			 * exchange.
1439af818581SJohannes Berg 			 */
14406b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
14416b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1442af818581SJohannes Berg 
144347086fc5SJohannes Berg 			/*
144447086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
144547086fc5SJohannes Berg 			 * more buffered frames for this STA
144647086fc5SJohannes Berg 			 */
144724b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
144847086fc5SJohannes Berg 				hdr->frame_control |=
144947086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
145024b9c373SJanusz.Dziedzic@tieto.com 			else
145124b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
145224b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1453af818581SJohannes Berg 
145440b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
145540b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
145640b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
145740b96408SJohannes Berg 
1458b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1459b77cf4f8SJohannes Berg 
1460b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1461b77cf4f8SJohannes Berg 
1462b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1463b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1464b77cf4f8SJohannes Berg 				continue;
1465b77cf4f8SJohannes Berg 
1466b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1467b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1468b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1469b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1470b77cf4f8SJohannes Berg 				break;
1471b77cf4f8SJohannes Berg 			}
1472b77cf4f8SJohannes Berg 
1473b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1474b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1475b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1476b77cf4f8SJohannes Berg 			 * and be done.
1477b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1478b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1479b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1480b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1481b77cf4f8SJohannes Berg 			 *
1482b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1483b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1484b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1485b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1486b77cf4f8SJohannes Berg 			 *
1487b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1488b77cf4f8SJohannes Berg 			 */
1489b77cf4f8SJohannes Berg 			if (qoshdr) {
149040b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1491deeaee19SJohannes Berg 
149247086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
149347086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1494b77cf4f8SJohannes Berg 			} else {
1495b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1496b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1497b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1498b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1499b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1500b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1501b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1502b77cf4f8SJohannes Berg 				 */
1503b77cf4f8SJohannes Berg 				hdr->frame_control |=
1504b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1505b77cf4f8SJohannes Berg 				need_null = true;
1506b77cf4f8SJohannes Berg 				num++;
150752a3f20cSMarco Porsch 			}
1508b77cf4f8SJohannes Berg 			break;
150947086fc5SJohannes Berg 		}
151047086fc5SJohannes Berg 
151140b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
151240b96408SJohannes Berg 					  reason, more_data);
151340b96408SJohannes Berg 
151447086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1515af818581SJohannes Berg 
1516b77cf4f8SJohannes Berg 		if (need_null)
1517b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1518b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1519b77cf4f8SJohannes Berg 				reason, false);
1520b77cf4f8SJohannes Berg 
1521c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1522af818581SJohannes Berg 	} else {
1523ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1524ba8c3d6fSFelix Fietkau 		int tid;
1525ba8c3d6fSFelix Fietkau 
1526af818581SJohannes Berg 		/*
15274049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
15284049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1529f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1530f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1531f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1532f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1533f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1534f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1535af818581SJohannes Berg 		 */
15364049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
153747086fc5SJohannes Berg 					    n_frames, reason, more_data);
15384049e09aSJohannes Berg 
15394049e09aSJohannes Berg 		/*
15404049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
15414049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1542f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
15434049e09aSJohannes Berg 		 * release function.
1544f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1545ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1546ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
15474049e09aSJohannes Berg 		 */
1548ba8c3d6fSFelix Fietkau 
1549ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1550ba8c3d6fSFelix Fietkau 			return;
1551ba8c3d6fSFelix Fietkau 
1552ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1553ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1554ba8c3d6fSFelix Fietkau 
1555ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1556ba8c3d6fSFelix Fietkau 				continue;
1557ba8c3d6fSFelix Fietkau 
1558ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1559ba8c3d6fSFelix Fietkau 			break;
1560ba8c3d6fSFelix Fietkau 		}
1561af818581SJohannes Berg 	}
1562af818581SJohannes Berg }
1563af818581SJohannes Berg 
1564af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1565af818581SJohannes Berg {
156647086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1567af818581SJohannes Berg 
1568af818581SJohannes Berg 	/*
156947086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
157047086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
157147086fc5SJohannes Berg 	 * only from the non-enabled ones.
1572af818581SJohannes Berg 	 */
157347086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
157447086fc5SJohannes Berg 		ignore_for_response = 0;
1575af818581SJohannes Berg 
157647086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
157747086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1578af818581SJohannes Berg }
157947086fc5SJohannes Berg 
158047086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
158147086fc5SJohannes Berg {
158247086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
158347086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
158447086fc5SJohannes Berg 
158547086fc5SJohannes Berg 	/*
158647086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
158747086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
158847086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
158947086fc5SJohannes Berg 	 * actually getting called.
159047086fc5SJohannes Berg 	 */
159147086fc5SJohannes Berg 	if (!delivery_enabled)
159247086fc5SJohannes Berg 		return;
159347086fc5SJohannes Berg 
159447086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
159547086fc5SJohannes Berg 	case 1:
159647086fc5SJohannes Berg 		n_frames = 2;
159747086fc5SJohannes Berg 		break;
159847086fc5SJohannes Berg 	case 2:
159947086fc5SJohannes Berg 		n_frames = 4;
160047086fc5SJohannes Berg 		break;
160147086fc5SJohannes Berg 	case 3:
160247086fc5SJohannes Berg 		n_frames = 6;
160347086fc5SJohannes Berg 		break;
160447086fc5SJohannes Berg 	case 0:
160547086fc5SJohannes Berg 		/* XXX: what is a good value? */
160613a8098aSAndrei Otcheretianski 		n_frames = 128;
160747086fc5SJohannes Berg 		break;
160847086fc5SJohannes Berg 	}
160947086fc5SJohannes Berg 
161047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
161147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1612af818581SJohannes Berg }
1613af818581SJohannes Berg 
1614af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1615af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1616af818581SJohannes Berg {
1617af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1618af818581SJohannes Berg 
1619b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1620b5878a2dSJohannes Berg 
16215ac2e350SJohannes Berg 	if (block) {
1622c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
162317c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
16245ac2e350SJohannes Berg 		return;
16255ac2e350SJohannes Berg 	}
16265ac2e350SJohannes Berg 
16275ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
16285ac2e350SJohannes Berg 		return;
16295ac2e350SJohannes Berg 
16305ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
16315ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
16325ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16335ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16345ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
16355ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
16365ac2e350SJohannes Berg 		/* must be asleep in this case */
16375ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16385ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16395ac2e350SJohannes Berg 	} else {
16405ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
164117c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
16425ac2e350SJohannes Berg 	}
1643af818581SJohannes Berg }
1644af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1645dcf55fb5SFelix Fietkau 
1646e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
164737fbd908SJohannes Berg {
164837fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
164937fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
165037fbd908SJohannes Berg 
165137fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
165237fbd908SJohannes Berg 
165337fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
165437fbd908SJohannes Berg }
1655e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
165637fbd908SJohannes Berg 
1657042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1658042ec453SJohannes Berg 				u8 tid, bool buffered)
1659dcf55fb5SFelix Fietkau {
1660dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1661dcf55fb5SFelix Fietkau 
16625a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1663042ec453SJohannes Berg 		return;
1664042ec453SJohannes Berg 
16651b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
16661b000789SJohannes Berg 
1667948d887dSJohannes Berg 	if (buffered)
1668948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1669948d887dSJohannes Berg 	else
1670948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1671948d887dSJohannes Berg 
1672c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1673dcf55fb5SFelix Fietkau }
1674042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1675d9a7ddb0SJohannes Berg 
167683d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1677d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1678d9a7ddb0SJohannes Berg {
16798bf11d8dSJohannes Berg 	might_sleep();
1680d9a7ddb0SJohannes Berg 
1681d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1682d9a7ddb0SJohannes Berg 		return 0;
1683d9a7ddb0SJohannes Berg 
1684f09603a2SJohannes Berg 	/* check allowed transitions first */
1685f09603a2SJohannes Berg 
1686d9a7ddb0SJohannes Berg 	switch (new_state) {
1687d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1688f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1689d9a7ddb0SJohannes Berg 			return -EINVAL;
1690d9a7ddb0SJohannes Berg 		break;
1691d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1692f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1693f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1694d9a7ddb0SJohannes Berg 			return -EINVAL;
1695d9a7ddb0SJohannes Berg 		break;
1696d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1697f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1698f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1699d9a7ddb0SJohannes Berg 			return -EINVAL;
1700d9a7ddb0SJohannes Berg 		break;
1701d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1702f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1703d9a7ddb0SJohannes Berg 			return -EINVAL;
1704d9a7ddb0SJohannes Berg 		break;
1705d9a7ddb0SJohannes Berg 	default:
1706d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1707d9a7ddb0SJohannes Berg 		return -EINVAL;
1708d9a7ddb0SJohannes Berg 	}
1709d9a7ddb0SJohannes Berg 
1710bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1711bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1712f09603a2SJohannes Berg 
1713f09603a2SJohannes Berg 	/*
1714f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1715f09603a2SJohannes Berg 	 * fail the transition
1716f09603a2SJohannes Berg 	 */
1717f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1718f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1719f09603a2SJohannes Berg 					sta->sta_state, new_state);
1720f09603a2SJohannes Berg 		if (err)
1721f09603a2SJohannes Berg 			return err;
1722f09603a2SJohannes Berg 	}
1723f09603a2SJohannes Berg 
1724f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1725f09603a2SJohannes Berg 
1726f09603a2SJohannes Berg 	switch (new_state) {
1727f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1728f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1729f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1730f09603a2SJohannes Berg 		break;
1731f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1732f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1733f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1734f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1735f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1736f09603a2SJohannes Berg 		break;
1737f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1738f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1739f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1740f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
17417e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17427e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17437e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17447e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1745f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
174617c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
1747f09603a2SJohannes Berg 		}
1748f09603a2SJohannes Berg 		break;
1749f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1750f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
17517e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17527e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17537e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17547e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1755f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
175617c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
1757f09603a2SJohannes Berg 		}
1758f09603a2SJohannes Berg 		break;
1759f09603a2SJohannes Berg 	default:
1760f09603a2SJohannes Berg 		break;
1761f09603a2SJohannes Berg 	}
1762f09603a2SJohannes Berg 
1763d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1764d9a7ddb0SJohannes Berg 
1765d9a7ddb0SJohannes Berg 	return 0;
1766d9a7ddb0SJohannes Berg }
1767687da132SEmmanuel Grumbach 
1768687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1769687da132SEmmanuel Grumbach {
1770687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1771687da132SEmmanuel Grumbach 	u8 rx_streams;
1772687da132SEmmanuel Grumbach 
1773687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1774687da132SEmmanuel Grumbach 		return 1;
1775687da132SEmmanuel Grumbach 
1776687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1777687da132SEmmanuel Grumbach 		int i;
1778687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1779687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1780687da132SEmmanuel Grumbach 
1781687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1782687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1783687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1784687da132SEmmanuel Grumbach 				return i + 1;
1785687da132SEmmanuel Grumbach 	}
1786687da132SEmmanuel Grumbach 
1787687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1788687da132SEmmanuel Grumbach 		rx_streams = 4;
1789687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1790687da132SEmmanuel Grumbach 		rx_streams = 3;
1791687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1792687da132SEmmanuel Grumbach 		rx_streams = 2;
1793687da132SEmmanuel Grumbach 	else
1794687da132SEmmanuel Grumbach 		rx_streams = 1;
1795687da132SEmmanuel Grumbach 
1796687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1797687da132SEmmanuel Grumbach 		return rx_streams;
1798687da132SEmmanuel Grumbach 
1799687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1800687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1801687da132SEmmanuel Grumbach }
1802b7ffbd7eSJohannes Berg 
1803b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1804b7ffbd7eSJohannes Berg {
1805b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1806b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
18079a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1808b7ffbd7eSJohannes Berg 	struct timespec uptime;
1809b7ffbd7eSJohannes Berg 	u32 thr = 0;
1810b7ffbd7eSJohannes Berg 	int i, ac;
1811b7ffbd7eSJohannes Berg 
18129a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
18139a244409SJohn W. Linville 		ref = local->rate_ctrl;
18149a244409SJohn W. Linville 
1815b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1816b7ffbd7eSJohannes Berg 
1817225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1818225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1819225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1820225b8189SJohannes Berg 	 */
1821225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1822225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1823225b8189SJohannes Berg 
18242b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
18252b9a7e1bSJohannes Berg 
1826319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1827319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1828319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1829319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1830319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1831319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BEACON_LOSS);
1832b7ffbd7eSJohannes Berg 
183318171520SThomas Gleixner 	ktime_get_ts(&uptime);
1834b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1835b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
18362b9a7e1bSJohannes Berg 
1837319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1838319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1839b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
18402b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1841b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1842319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1843b7ffbd7eSJohannes Berg 	}
18442b9a7e1bSJohannes Berg 
1845319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
18462b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
18472b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
18482b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1849319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
18502b9a7e1bSJohannes Berg 	}
18512b9a7e1bSJohannes Berg 
1852319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1853319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1854b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1855319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
18562b9a7e1bSJohannes Berg 	}
18572b9a7e1bSJohannes Berg 
1858319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1859b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1860319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
18612b9a7e1bSJohannes Berg 	}
18622b9a7e1bSJohannes Berg 
1863319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1864b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1865319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
18662b9a7e1bSJohannes Berg 	}
18672b9a7e1bSJohannes Berg 
1868319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1869b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1870319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
18712b9a7e1bSJohannes Berg 	}
18722b9a7e1bSJohannes Berg 
1873b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1874b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1875b7ffbd7eSJohannes Berg 
1876225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1877225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1878225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1879225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1880225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1881225b8189SJohannes Berg 	}
1882225b8189SJohannes Berg 
188330686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
188430686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
1885319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1886b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1887319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1888b7ffbd7eSJohannes Berg 		}
18892b9a7e1bSJohannes Berg 
1890319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
18912b9a7e1bSJohannes Berg 			sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1892319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
18932b9a7e1bSJohannes Berg 		}
18942b9a7e1bSJohannes Berg 	}
18952b9a7e1bSJohannes Berg 
18962b9a7e1bSJohannes Berg 	if (sta->chains &&
1897319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1898319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1899319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1900319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1901b7ffbd7eSJohannes Berg 
1902b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1903b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1904b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1905b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1906b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1907b7ffbd7eSJohannes Berg 		}
1908b7ffbd7eSJohannes Berg 	}
1909b7ffbd7eSJohannes Berg 
1910319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1911b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1912319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
19132b9a7e1bSJohannes Berg 	}
19142b9a7e1bSJohannes Berg 
1915319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1916b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1917319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
19182b9a7e1bSJohannes Berg 	}
1919b7ffbd7eSJohannes Berg 
192079c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
192179c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
192279c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
192379c892b8SJohannes Berg 
192479c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
192579c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
192679c892b8SJohannes Berg 			tidstats->rx_msdu = sta->rx_msdu[i];
192779c892b8SJohannes Berg 		}
192879c892b8SJohannes Berg 
192979c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
193079c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
193179c892b8SJohannes Berg 			tidstats->tx_msdu = sta->tx_msdu[i];
193279c892b8SJohannes Berg 		}
193379c892b8SJohannes Berg 
193479c892b8SJohannes Berg 		if (!(tidstats->filled &
193579c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
193630686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
193779c892b8SJohannes Berg 			tidstats->filled |=
193879c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
193979c892b8SJohannes Berg 			tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
194079c892b8SJohannes Berg 		}
194179c892b8SJohannes Berg 
194279c892b8SJohannes Berg 		if (!(tidstats->filled &
194379c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
194430686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
194579c892b8SJohannes Berg 			tidstats->filled |=
194679c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
194779c892b8SJohannes Berg 			tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
194879c892b8SJohannes Berg 		}
194979c892b8SJohannes Berg 	}
195079c892b8SJohannes Berg 
1951b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1952b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1953319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1954319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
1955319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
1956319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
1957319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
1958319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
1959b7ffbd7eSJohannes Berg 
1960b7ffbd7eSJohannes Berg 		sinfo->llid = sta->llid;
1961b7ffbd7eSJohannes Berg 		sinfo->plid = sta->plid;
1962b7ffbd7eSJohannes Berg 		sinfo->plink_state = sta->plink_state;
1963b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1964319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1965b7ffbd7eSJohannes Berg 			sinfo->t_offset = sta->t_offset;
1966b7ffbd7eSJohannes Berg 		}
1967b7ffbd7eSJohannes Berg 		sinfo->local_pm = sta->local_pm;
1968b7ffbd7eSJohannes Berg 		sinfo->peer_pm = sta->peer_pm;
1969b7ffbd7eSJohannes Berg 		sinfo->nonpeer_pm = sta->nonpeer_pm;
1970b7ffbd7eSJohannes Berg #endif
1971b7ffbd7eSJohannes Berg 	}
1972b7ffbd7eSJohannes Berg 
1973b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1974b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1975b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1976b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1977b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1978b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1979b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1980785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1981b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1982b7ffbd7eSJohannes Berg 
1983b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1984b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1985b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1986b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1987b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1988b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1989b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1990b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1991b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1992b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1993b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1994b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1995a74a8c84SJohannes Berg 	if (sta->sta.wme)
1996b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1997b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
1998b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1999b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2000b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2001b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2002b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2003b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2004b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2005b7ffbd7eSJohannes Berg 
2006b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2007b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2008b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2009b7ffbd7eSJohannes Berg 	else
2010b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
2011b7ffbd7eSJohannes Berg 
2012b7ffbd7eSJohannes Berg 	if (thr != 0) {
2013319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2014b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
2015b7ffbd7eSJohannes Berg 	}
2016b7ffbd7eSJohannes Berg }
2017