xref: /openbmc/linux/net/mac80211/sta_info.c (revision 976bd9efdae6a844079ba4a7898a38d229ef246c)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
5f0706e82SJiri Benc  *
6f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
7f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
8f0706e82SJiri Benc  * published by the Free Software Foundation.
9f0706e82SJiri Benc  */
10f0706e82SJiri Benc 
11f0706e82SJiri Benc #include <linux/module.h>
12f0706e82SJiri Benc #include <linux/init.h>
13888d04dfSFelix Fietkau #include <linux/etherdevice.h>
14f0706e82SJiri Benc #include <linux/netdevice.h>
15f0706e82SJiri Benc #include <linux/types.h>
16f0706e82SJiri Benc #include <linux/slab.h>
17f0706e82SJiri Benc #include <linux/skbuff.h>
18f0706e82SJiri Benc #include <linux/if_arp.h>
190d174406SJohannes Berg #include <linux/timer.h>
20d0709a65SJohannes Berg #include <linux/rtnetlink.h>
21f0706e82SJiri Benc 
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
677bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
687bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
69caf22d31SJohannes Berg 	.automatic_shrinking = true,
707bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
71ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
727bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
737bedd0cfSJohannes Berg 	.hashfn = sta_addr_hash,
74ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
757bedd0cfSJohannes Berg };
767bedd0cfSJohannes Berg 
774d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
78be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
79f0706e82SJiri Benc 			     struct sta_info *sta)
80f0706e82SJiri Benc {
817bedd0cfSJohannes Berg 	return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
827bedd0cfSJohannes Berg 				      sta_rht_params);
83f0706e82SJiri Benc }
84f0706e82SJiri Benc 
855108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
86b22cfcfcSEliad Peller {
87b22cfcfcSEliad Peller 	int ac, i;
88b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
89b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
90b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
91d012a605SMarco Porsch 	struct ps_data *ps;
92b22cfcfcSEliad Peller 
93e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
945ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
955ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
96d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
97d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
98d012a605SMarco Porsch 			ps = &sdata->bss->ps;
993f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1003f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
101d012a605SMarco Porsch 		else
102d012a605SMarco Porsch 			return;
103b22cfcfcSEliad Peller 
104b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
105e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1065ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
107b22cfcfcSEliad Peller 
108d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
109b22cfcfcSEliad Peller 	}
110b22cfcfcSEliad Peller 
111ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
112ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
113ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
114ba8c3d6fSFelix Fietkau 			int n = skb_queue_len(&txqi->queue);
115ba8c3d6fSFelix Fietkau 
116ba8c3d6fSFelix Fietkau 			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
117ba8c3d6fSFelix Fietkau 			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
118ba8c3d6fSFelix Fietkau 		}
119ba8c3d6fSFelix Fietkau 	}
120ba8c3d6fSFelix Fietkau 
121b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
122b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1231f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1241f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
125b22cfcfcSEliad Peller 	}
126b22cfcfcSEliad Peller 
12745b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12845b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
129b22cfcfcSEliad Peller 
1305ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
131b22cfcfcSEliad Peller 
132b22cfcfcSEliad Peller 	/*
133b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
134b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
135b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
136b22cfcfcSEliad Peller 	 * directly by station destruction.
137b22cfcfcSEliad Peller 	 */
1385a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
139661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
140b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
141b22cfcfcSEliad Peller 		if (!tid_tx)
142b22cfcfcSEliad Peller 			continue;
1431f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
144b22cfcfcSEliad Peller 		kfree(tid_tx);
145b22cfcfcSEliad Peller 	}
1465108ca82SJohannes Berg }
147b22cfcfcSEliad Peller 
1485108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1495108ca82SJohannes Berg {
1505108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1515108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1525108ca82SJohannes Berg 
1535108ca82SJohannes Berg 	__cleanup_single_sta(sta);
154b22cfcfcSEliad Peller 	sta_info_free(local, sta);
155b22cfcfcSEliad Peller }
156b22cfcfcSEliad Peller 
157d0709a65SJohannes Berg /* protected by RCU */
158abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
159abe60632SJohannes Berg 			      const u8 *addr)
16043ba7e95SJohannes Berg {
161abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16260f4b626SJohannes Berg 	struct sta_info *sta;
16360f4b626SJohannes Berg 	struct rhash_head *tmp;
16460f4b626SJohannes Berg 	const struct bucket_table *tbl;
16543ba7e95SJohannes Berg 
16660f4b626SJohannes Berg 	rcu_read_lock();
16760f4b626SJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
16860f4b626SJohannes Berg 
16960f4b626SJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
17060f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17160f4b626SJohannes Berg 			rcu_read_unlock();
17260f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17360f4b626SJohannes Berg 			 * another rcu read section or the mutex
17460f4b626SJohannes Berg 			 */
17560f4b626SJohannes Berg 			return sta;
17660f4b626SJohannes Berg 		}
17760f4b626SJohannes Berg 	}
17860f4b626SJohannes Berg 	rcu_read_unlock();
17960f4b626SJohannes Berg 	return NULL;
18043ba7e95SJohannes Berg }
18143ba7e95SJohannes Berg 
1820e5ded5aSFelix Fietkau /*
1830e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1840e5ded5aSFelix Fietkau  * or from one of its vlans
1850e5ded5aSFelix Fietkau  */
1860e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1870e5ded5aSFelix Fietkau 				  const u8 *addr)
1880e5ded5aSFelix Fietkau {
1890e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1900e5ded5aSFelix Fietkau 	struct sta_info *sta;
1917bedd0cfSJohannes Berg 	struct rhash_head *tmp;
1927bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
1930e5ded5aSFelix Fietkau 
1947bedd0cfSJohannes Berg 	rcu_read_lock();
1957bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1967bedd0cfSJohannes Berg 
1977bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1987bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
1997bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2007bedd0cfSJohannes Berg 			rcu_read_unlock();
2017bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2027bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2037bedd0cfSJohannes Berg 			 */
2040e5ded5aSFelix Fietkau 			return sta;
2050e5ded5aSFelix Fietkau 		}
2067bedd0cfSJohannes Berg 	}
2077bedd0cfSJohannes Berg 	rcu_read_unlock();
2087bedd0cfSJohannes Berg 	return NULL;
2097bedd0cfSJohannes Berg }
2100e5ded5aSFelix Fietkau 
2113b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2123b53fde8SJohannes Berg 				     int idx)
213ee385855SLuis Carlos Cobo {
2143b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
215ee385855SLuis Carlos Cobo 	struct sta_info *sta;
216ee385855SLuis Carlos Cobo 	int i = 0;
217ee385855SLuis Carlos Cobo 
218d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2193b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2202a8ca29aSLuis Carlos Cobo 			continue;
221ee385855SLuis Carlos Cobo 		if (i < idx) {
222ee385855SLuis Carlos Cobo 			++i;
223ee385855SLuis Carlos Cobo 			continue;
224ee385855SLuis Carlos Cobo 		}
2252a8ca29aSLuis Carlos Cobo 		return sta;
226ee385855SLuis Carlos Cobo 	}
227ee385855SLuis Carlos Cobo 
228ee385855SLuis Carlos Cobo 	return NULL;
229ee385855SLuis Carlos Cobo }
230f0706e82SJiri Benc 
23193e5deb1SJohannes Berg /**
232d9a7ddb0SJohannes Berg  * sta_info_free - free STA
23393e5deb1SJohannes Berg  *
2346ef307bcSRandy Dunlap  * @local: pointer to the global information
23593e5deb1SJohannes Berg  * @sta: STA info to free
23693e5deb1SJohannes Berg  *
23793e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
238d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
239d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
240d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
24193e5deb1SJohannes Berg  */
242d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
24393e5deb1SJohannes Berg {
244889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2454b7679a5SJohannes Berg 		rate_control_free_sta(sta);
24693e5deb1SJohannes Berg 
247bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
24893e5deb1SJohannes Berg 
249ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
250ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
25153d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
252433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
253433f5bc1SJohannes Berg 	kfree(sta->mesh);
254433f5bc1SJohannes Berg #endif
25593e5deb1SJohannes Berg 	kfree(sta);
25693e5deb1SJohannes Berg }
25793e5deb1SJohannes Berg 
2584d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
259d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
260d0709a65SJohannes Berg 			      struct sta_info *sta)
261f0706e82SJiri Benc {
2627bedd0cfSJohannes Berg 	rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
2637bedd0cfSJohannes Berg 			       sta_rht_params);
264f0706e82SJiri Benc }
265f0706e82SJiri Benc 
2665ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
267af818581SJohannes Berg {
268af818581SJohannes Berg 	struct sta_info *sta;
269af818581SJohannes Berg 
2705ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
271af818581SJohannes Berg 
272af818581SJohannes Berg 	if (sta->dead)
273af818581SJohannes Berg 		return;
274af818581SJohannes Berg 
27554420473SHelmut Schaa 	local_bh_disable();
2765ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
277af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2785ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
279af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2805ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
28147086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
282ce662b44SJohannes Berg 	local_bh_enable();
283af818581SJohannes Berg }
284af818581SJohannes Berg 
285af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
286af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
287af65cd96SJohannes Berg {
28830686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
289af65cd96SJohannes Berg 		return 0;
290af65cd96SJohannes Berg 
291889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
292af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29335c347acSJohannes Berg 						     sta, gfp);
294889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
295af65cd96SJohannes Berg 		return -ENOMEM;
296af65cd96SJohannes Berg 
297af65cd96SJohannes Berg 	return 0;
298af65cd96SJohannes Berg }
299af65cd96SJohannes Berg 
30073651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30156544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
302f0706e82SJiri Benc {
303d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
304ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
305f0706e82SJiri Benc 	struct sta_info *sta;
30616c5f15cSRon Rindjunsky 	int i;
307f0706e82SJiri Benc 
308ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
309f0706e82SJiri Benc 	if (!sta)
31073651ee6SJohannes Berg 		return NULL;
311f0706e82SJiri Benc 
31207346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3131d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3145ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
31567c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
316a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
31787f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
318433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
319433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
320433f5bc1SJohannes Berg 		if (!sta->mesh)
321433f5bc1SJohannes Berg 			goto free;
322433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
32387f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
32487f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
325433f5bc1SJohannes Berg 			init_timer(&sta->mesh->plink_timer);
326433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
327433f5bc1SJohannes Berg 	}
32887f59c70SThomas Pedersen #endif
32907346f81SJohannes Berg 
330ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
33117741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
332d0709a65SJohannes Berg 	sta->local = local;
333d0709a65SJohannes Berg 	sta->sdata = sdata;
3348bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
335f0706e82SJiri Benc 
33671ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
33771ec375cSJohannes Berg 
338b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
339b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
340b6da911bSLiad Kaufman 
34184b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
34240d9a38aSJohannes Berg 	ewma_signal_init(&sta->avg_signal);
343ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
34440d9a38aSJohannes Berg 		ewma_signal_init(&sta->chain_signal_avg[i]);
345541a45a1SBruno Randolf 
346ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
347ba8c3d6fSFelix Fietkau 		void *txq_data;
348ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
349ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
350ba8c3d6fSFelix Fietkau 
351ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
352ba8c3d6fSFelix Fietkau 		if (!txq_data)
353ba8c3d6fSFelix Fietkau 			goto free;
354ba8c3d6fSFelix Fietkau 
355ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
356ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
357ba8c3d6fSFelix Fietkau 
358ba8c3d6fSFelix Fietkau 			ieee80211_init_tx_queue(sdata, sta, txq, i);
359abfbc3afSJohannes Berg 		}
360ba8c3d6fSFelix Fietkau 	}
361ba8c3d6fSFelix Fietkau 
362ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
363ba8c3d6fSFelix Fietkau 		goto free_txq;
364f0706e82SJiri Benc 
3655a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
366a622ab72SJohannes Berg 		/*
367a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
368a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
369a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
370a622ab72SJohannes Berg 		 */
37116c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
37216c5f15cSRon Rindjunsky 	}
373948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
374948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
375948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
376948d887dSJohannes Berg 	}
37773651ee6SJohannes Berg 
3785a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3794be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
380cccaec98SSenthil Balasubramanian 
381af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
382687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
383687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
384687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
385ba8c3d6fSFelix Fietkau 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
386687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
387687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
388687da132SEmmanuel Grumbach 		/*
389687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
390687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
391687da132SEmmanuel Grumbach 		 */
392687da132SEmmanuel Grumbach 		switch (smps) {
393687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
394687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
395687da132SEmmanuel Grumbach 			break;
396687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
397687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
398687da132SEmmanuel Grumbach 			break;
399687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
400687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
401687da132SEmmanuel Grumbach 			break;
402687da132SEmmanuel Grumbach 		default:
403687da132SEmmanuel Grumbach 			WARN_ON(1);
404687da132SEmmanuel Grumbach 		}
405687da132SEmmanuel Grumbach 	}
406af0ed69bSJohannes Berg 
407bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
408ef04a297SJohannes Berg 
409abfbc3afSJohannes Berg 	return sta;
410ba8c3d6fSFelix Fietkau 
411ba8c3d6fSFelix Fietkau free_txq:
412ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
413ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
414ba8c3d6fSFelix Fietkau free:
415433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
416433f5bc1SJohannes Berg 	kfree(sta->mesh);
417433f5bc1SJohannes Berg #endif
418ba8c3d6fSFelix Fietkau 	kfree(sta);
419ba8c3d6fSFelix Fietkau 	return NULL;
42073651ee6SJohannes Berg }
42173651ee6SJohannes Berg 
4228c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
42334e89507SJohannes Berg {
42434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42534e89507SJohannes Berg 
42603e4497eSJohannes Berg 	/*
42703e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
42803e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
42903e4497eSJohannes Berg 	 * and another CPU turns off the net device.
43003e4497eSJohannes Berg 	 */
4318c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4328c71df7aSGuy Eilam 		return -ENETDOWN;
43303e4497eSJohannes Berg 
434b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4358c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4368c71df7aSGuy Eilam 		return -EINVAL;
4378c71df7aSGuy Eilam 
4388c71df7aSGuy Eilam 	return 0;
43993e5deb1SJohannes Berg }
44044213b5eSJohannes Berg 
441f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
442f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
443f09603a2SJohannes Berg 				     struct sta_info *sta)
444f09603a2SJohannes Berg {
445f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
446f09603a2SJohannes Berg 	int err = 0;
447f09603a2SJohannes Berg 
448f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
449f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
450f09603a2SJohannes Berg 		if (err)
451f09603a2SJohannes Berg 			break;
452f09603a2SJohannes Berg 	}
453f09603a2SJohannes Berg 
454f09603a2SJohannes Berg 	if (!err) {
455a4ec45a4SJohannes Berg 		/*
456a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
457a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
458a4ec45a4SJohannes Berg 		 */
459a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
460f09603a2SJohannes Berg 			sta->uploaded = true;
461f09603a2SJohannes Berg 		return 0;
462f09603a2SJohannes Berg 	}
463f09603a2SJohannes Berg 
464f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
465bdcbd8e0SJohannes Berg 		sdata_info(sdata,
466bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
467bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
468f09603a2SJohannes Berg 		err = 0;
469f09603a2SJohannes Berg 	}
470f09603a2SJohannes Berg 
471f09603a2SJohannes Berg 	/* unwind on error */
472f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
473f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
474f09603a2SJohannes Berg 
475f09603a2SJohannes Berg 	return err;
476f09603a2SJohannes Berg }
477f09603a2SJohannes Berg 
47834e89507SJohannes Berg /*
4798c71df7aSGuy Eilam  * should be called with sta_mtx locked
4808c71df7aSGuy Eilam  * this function replaces the mutex lock
4818c71df7aSGuy Eilam  * with a RCU lock
4828c71df7aSGuy Eilam  */
4834d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4848c71df7aSGuy Eilam {
4858c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4868c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4877852e361SJohannes Berg 	struct station_info sinfo;
4888c71df7aSGuy Eilam 	int err = 0;
4898c71df7aSGuy Eilam 
4908c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4918c71df7aSGuy Eilam 
4927852e361SJohannes Berg 	/* check if STA exists already */
4937852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4944d33960bSJohannes Berg 		err = -EEXIST;
4954d33960bSJohannes Berg 		goto out_err;
49634e89507SJohannes Berg 	}
49734e89507SJohannes Berg 
4984d33960bSJohannes Berg 	local->num_sta++;
4994d33960bSJohannes Berg 	local->sta_generation++;
5004d33960bSJohannes Berg 	smp_mb();
5014d33960bSJohannes Berg 
5025108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5035108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5045108ca82SJohannes Berg 
5054d33960bSJohannes Berg 	/* make the station visible */
5064d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5074d33960bSJohannes Berg 
5082bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
50983d5cc01SJohannes Berg 
5105108ca82SJohannes Berg 	/* notify driver */
5115108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5125108ca82SJohannes Berg 	if (err)
5135108ca82SJohannes Berg 		goto out_remove;
5145108ca82SJohannes Berg 
51583d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5165108ca82SJohannes Berg 	/* accept BA sessions now */
5175108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5184d33960bSJohannes Berg 
51921f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
5204d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5214d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5224d33960bSJohannes Berg 
5234d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5244d33960bSJohannes Berg 	sinfo.filled = 0;
5254d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5264d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
527d0709a65SJohannes Berg 
528bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
529f0706e82SJiri Benc 
53034e89507SJohannes Berg 	/* move reference to rcu-protected */
53134e89507SJohannes Berg 	rcu_read_lock();
53234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
533e9f207f0SJiri Benc 
53473651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
53573651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
53673651ee6SJohannes Berg 
53773651ee6SJohannes Berg 	return 0;
5385108ca82SJohannes Berg  out_remove:
5395108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5405108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5415108ca82SJohannes Berg 	local->num_sta--;
5425108ca82SJohannes Berg 	synchronize_net();
5435108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5444d33960bSJohannes Berg  out_err:
5454d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5464d33960bSJohannes Berg 	rcu_read_lock();
5474d33960bSJohannes Berg 	return err;
5488c71df7aSGuy Eilam }
5498c71df7aSGuy Eilam 
5508c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5518c71df7aSGuy Eilam {
5528c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
553308f7fcfSZhao, Gang 	int err;
5548c71df7aSGuy Eilam 
5554d33960bSJohannes Berg 	might_sleep();
5564d33960bSJohannes Berg 
5578c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5588c71df7aSGuy Eilam 	if (err) {
5598c71df7aSGuy Eilam 		rcu_read_lock();
5608c71df7aSGuy Eilam 		goto out_free;
5618c71df7aSGuy Eilam 	}
5628c71df7aSGuy Eilam 
563004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
564004c872eSJohannes Berg 
5654d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5668c71df7aSGuy Eilam 	if (err)
567004c872eSJohannes Berg 		goto out_free;
568d0709a65SJohannes Berg 
569004c872eSJohannes Berg 	return 0;
57093e5deb1SJohannes Berg  out_free:
571d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
57293e5deb1SJohannes Berg 	return err;
573f0706e82SJiri Benc }
574f0706e82SJiri Benc 
57534e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
57634e89507SJohannes Berg {
57734e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
57834e89507SJohannes Berg 
57934e89507SJohannes Berg 	rcu_read_unlock();
58034e89507SJohannes Berg 
58134e89507SJohannes Berg 	return err;
58234e89507SJohannes Berg }
58334e89507SJohannes Berg 
584d012a605SMarco Porsch static inline void __bss_tim_set(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 __set_bit() format.
589004c872eSJohannes Berg 	 */
590d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
591004c872eSJohannes Berg }
592004c872eSJohannes Berg 
593d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
594004c872eSJohannes Berg {
595004c872eSJohannes Berg 	/*
596004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
597004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
598004c872eSJohannes Berg 	 */
599d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
600004c872eSJohannes Berg }
601004c872eSJohannes Berg 
6023d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6033d5839b6SIlan Peer {
6043d5839b6SIlan Peer 	/*
6053d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6063d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6073d5839b6SIlan Peer 	 */
6083d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6093d5839b6SIlan Peer }
6103d5839b6SIlan Peer 
611948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
612004c872eSJohannes Berg {
613948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
614948d887dSJohannes Berg 	switch (ac) {
615948d887dSJohannes Berg 	case IEEE80211_AC_VO:
616948d887dSJohannes Berg 		return BIT(6) | BIT(7);
617948d887dSJohannes Berg 	case IEEE80211_AC_VI:
618948d887dSJohannes Berg 		return BIT(4) | BIT(5);
619948d887dSJohannes Berg 	case IEEE80211_AC_BE:
620948d887dSJohannes Berg 		return BIT(0) | BIT(3);
621948d887dSJohannes Berg 	case IEEE80211_AC_BK:
622948d887dSJohannes Berg 		return BIT(1) | BIT(2);
623948d887dSJohannes Berg 	default:
624948d887dSJohannes Berg 		WARN_ON(1);
625948d887dSJohannes Berg 		return 0;
626d0709a65SJohannes Berg 	}
627004c872eSJohannes Berg }
628004c872eSJohannes Berg 
6299b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
630004c872eSJohannes Berg {
631c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
632d012a605SMarco Porsch 	struct ps_data *ps;
633948d887dSJohannes Berg 	bool indicate_tim = false;
634948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
635948d887dSJohannes Berg 	int ac;
636a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
637004c872eSJohannes Berg 
638d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
639d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
640c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
641c868cb35SJohannes Berg 			return;
6423e122be0SJohannes Berg 
643d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
6443f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6453f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6463f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
6473f52b7e3SMarco Porsch #endif
648d012a605SMarco Porsch 	} else {
649d012a605SMarco Porsch 		return;
650d012a605SMarco Porsch 	}
651d012a605SMarco Porsch 
652c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
65330686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
654c868cb35SJohannes Berg 		return;
655004c872eSJohannes Berg 
656c868cb35SJohannes Berg 	if (sta->dead)
657c868cb35SJohannes Berg 		goto done;
6583e122be0SJohannes Berg 
659948d887dSJohannes Berg 	/*
660948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
661948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
662948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
663948d887dSJohannes Berg 	 * non-enabled ones.
664948d887dSJohannes Berg 	 */
665948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
666948d887dSJohannes Berg 		ignore_for_tim = 0;
667948d887dSJohannes Berg 
6689b7a86f3SJohannes Berg 	if (ignore_pending)
6699b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6709b7a86f3SJohannes Berg 
671948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
672948d887dSJohannes Berg 		unsigned long tids;
673948d887dSJohannes Berg 
674948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
675948d887dSJohannes Berg 			continue;
676948d887dSJohannes Berg 
677948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
678948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
679948d887dSJohannes Berg 		if (indicate_tim)
680948d887dSJohannes Berg 			break;
681948d887dSJohannes Berg 
682948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
683948d887dSJohannes Berg 
684948d887dSJohannes Berg 		indicate_tim |=
685948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
686ba8c3d6fSFelix Fietkau 		indicate_tim |=
687ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
688004c872eSJohannes Berg 	}
689004c872eSJohannes Berg 
690c868cb35SJohannes Berg  done:
69165f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
692004c872eSJohannes Berg 
6933d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6943d5839b6SIlan Peer 		goto out_unlock;
6953d5839b6SIlan Peer 
696948d887dSJohannes Berg 	if (indicate_tim)
697d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
698c868cb35SJohannes Berg 	else
699d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7003e122be0SJohannes Berg 
7019b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
702c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
703948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
704c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
705004c872eSJohannes Berg 	}
706004c872eSJohannes Berg 
7073d5839b6SIlan Peer out_unlock:
70865f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
709004c872eSJohannes Berg }
710004c872eSJohannes Berg 
7119b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7129b7a86f3SJohannes Berg {
7139b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7149b7a86f3SJohannes Berg }
7159b7a86f3SJohannes Berg 
716cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
717f0706e82SJiri Benc {
718e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
719f0706e82SJiri Benc 	int timeout;
720f0706e82SJiri Benc 
721f0706e82SJiri Benc 	if (!skb)
722cd0b8d89SJohannes Berg 		return false;
723f0706e82SJiri Benc 
724e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
725f0706e82SJiri Benc 
726f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
72757c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
72857c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
72957c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
730f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
731f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
732e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
733f0706e82SJiri Benc }
734f0706e82SJiri Benc 
735f0706e82SJiri Benc 
736948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
737948d887dSJohannes Berg 						struct sta_info *sta, int ac)
738f0706e82SJiri Benc {
739f0706e82SJiri Benc 	unsigned long flags;
740f0706e82SJiri Benc 	struct sk_buff *skb;
741f0706e82SJiri Benc 
74260750397SJohannes Berg 	/*
74360750397SJohannes Berg 	 * First check for frames that should expire on the filtered
74460750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
74560750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
74660750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
74760750397SJohannes Berg 	 * total_ps_buffered counter.
74860750397SJohannes Berg 	 */
749f0706e82SJiri Benc 	for (;;) {
750948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
751948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
75257c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
753948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
754836341a7SJohannes Berg 		else
755f0706e82SJiri Benc 			skb = NULL;
756948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
757f0706e82SJiri Benc 
75860750397SJohannes Berg 		/*
75960750397SJohannes Berg 		 * Frames are queued in order, so if this one
76060750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
76160750397SJohannes Berg 		 * we actually reached the end of the queue we
76260750397SJohannes Berg 		 * also need to stop, of course.
76360750397SJohannes Berg 		 */
76460750397SJohannes Berg 		if (!skb)
76560750397SJohannes Berg 			break;
766d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
76760750397SJohannes Berg 	}
76860750397SJohannes Berg 
76960750397SJohannes Berg 	/*
77060750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
77160750397SJohannes Berg 	 * only find something if the filtered queue was emptied
77260750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
77360750397SJohannes Berg 	 * buffered frames.
77460750397SJohannes Berg 	 */
775f0706e82SJiri Benc 	for (;;) {
776948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
777948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
778f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
779948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
780f0706e82SJiri Benc 		else
781f0706e82SJiri Benc 			skb = NULL;
782948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
783f0706e82SJiri Benc 
78460750397SJohannes Berg 		/*
78560750397SJohannes Berg 		 * frames are queued in order, so if this one
78660750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
78760750397SJohannes Berg 		 * the queue) we can stop testing
78860750397SJohannes Berg 		 */
789836341a7SJohannes Berg 		if (!skb)
790836341a7SJohannes Berg 			break;
791836341a7SJohannes Berg 
792f0706e82SJiri Benc 		local->total_ps_buffered--;
793bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
794bdcbd8e0SJohannes Berg 		       sta->sta.addr);
795d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
796f0706e82SJiri Benc 	}
7973393a608SJuuso Oikarinen 
79860750397SJohannes Berg 	/*
79960750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
80060750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
80160750397SJohannes Berg 	 * frames.
80260750397SJohannes Berg 	 */
80360750397SJohannes Berg 	sta_info_recalc_tim(sta);
80460750397SJohannes Berg 
80560750397SJohannes Berg 	/*
80660750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
80760750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
80860750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
80960750397SJohannes Berg 	 */
810948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
811948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
812948d887dSJohannes Berg }
813948d887dSJohannes Berg 
814948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
815948d887dSJohannes Berg 					     struct sta_info *sta)
816948d887dSJohannes Berg {
817948d887dSJohannes Berg 	bool have_buffered = false;
818948d887dSJohannes Berg 	int ac;
819948d887dSJohannes Berg 
8203f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8213f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8223f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
823948d887dSJohannes Berg 		return false;
824948d887dSJohannes Berg 
825948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
826948d887dSJohannes Berg 		have_buffered |=
827948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
828948d887dSJohannes Berg 
829948d887dSJohannes Berg 	return have_buffered;
830f0706e82SJiri Benc }
831f0706e82SJiri Benc 
832d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
83334e89507SJohannes Berg {
83434e89507SJohannes Berg 	struct ieee80211_local *local;
83534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8366d10e46bSJohannes Berg 	int ret;
83734e89507SJohannes Berg 
83834e89507SJohannes Berg 	might_sleep();
83934e89507SJohannes Berg 
84034e89507SJohannes Berg 	if (!sta)
84134e89507SJohannes Berg 		return -ENOENT;
84234e89507SJohannes Berg 
84334e89507SJohannes Berg 	local = sta->local;
84434e89507SJohannes Berg 	sdata = sta->sdata;
84534e89507SJohannes Berg 
84683d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
84783d5cc01SJohannes Berg 
848098a6070SJohannes Berg 	/*
849098a6070SJohannes Berg 	 * Before removing the station from the driver and
850098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
851098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
852098a6070SJohannes Berg 	 * will be sufficient.
853098a6070SJohannes Berg 	 */
854c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
855c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
856098a6070SJohannes Berg 
85734e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
858b01711beSJohannes Berg 	if (WARN_ON(ret))
85934e89507SJohannes Berg 		return ret;
86034e89507SJohannes Berg 
861a7a6bdd0SArik Nemtsov 	/*
862a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
863a7a6bdd0SArik Nemtsov 	 * removal.
864a7a6bdd0SArik Nemtsov 	 */
865a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
866a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
867a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
868a7a6bdd0SArik Nemtsov 	}
869a7a6bdd0SArik Nemtsov 
870794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8714d33960bSJohannes Berg 
8726a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8736a9d1b91SJohannes Berg 
874a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
875a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
876a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
877a710c816SJohannes Berg 
878d778207bSJohannes Berg 	return 0;
879d778207bSJohannes Berg }
880d778207bSJohannes Berg 
881d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
882d778207bSJohannes Berg {
883d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
884d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8856f7a8d26SJohannes Berg 	struct station_info sinfo = {};
886d778207bSJohannes Berg 	int ret;
887d778207bSJohannes Berg 
888d778207bSJohannes Berg 	/*
889d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
890d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
891d778207bSJohannes Berg 	 */
892d778207bSJohannes Berg 
893d778207bSJohannes Berg 	might_sleep();
894d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
895d778207bSJohannes Berg 
896c8782078SJohannes Berg 	/* now keys can no longer be reached */
8976d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
89834e89507SJohannes Berg 
8999b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9009b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9019b7a86f3SJohannes Berg 
90234e89507SJohannes Berg 	sta->dead = true;
90334e89507SJohannes Berg 
90434e89507SJohannes Berg 	local->num_sta--;
90534e89507SJohannes Berg 	local->sta_generation++;
90634e89507SJohannes Berg 
90783d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
908f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
909f09603a2SJohannes Berg 		if (ret) {
91083d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
91183d5cc01SJohannes Berg 			break;
91283d5cc01SJohannes Berg 		}
91383d5cc01SJohannes Berg 	}
914d9a7ddb0SJohannes Berg 
915f09603a2SJohannes Berg 	if (sta->uploaded) {
916f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
917f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
918f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
919f09603a2SJohannes Berg 	}
92034e89507SJohannes Berg 
921bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
922bdcbd8e0SJohannes Berg 
9236f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
9246f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
925ec15e68bSJouni Malinen 
92634e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
92734e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
92821f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
92934e89507SJohannes Berg 
930d34ba216SJohannes Berg 	cleanup_single_sta(sta);
931d778207bSJohannes Berg }
932d778207bSJohannes Berg 
933d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
934d778207bSJohannes Berg {
935d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
936d778207bSJohannes Berg 
937d778207bSJohannes Berg 	if (err)
938d778207bSJohannes Berg 		return err;
939d778207bSJohannes Berg 
940d778207bSJohannes Berg 	synchronize_net();
941d778207bSJohannes Berg 
942d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
94334e89507SJohannes Berg 
94434e89507SJohannes Berg 	return 0;
94534e89507SJohannes Berg }
94634e89507SJohannes Berg 
94734e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
94834e89507SJohannes Berg {
94934e89507SJohannes Berg 	struct sta_info *sta;
95034e89507SJohannes Berg 	int ret;
95134e89507SJohannes Berg 
95234e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9537852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
95434e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
95534e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
95634e89507SJohannes Berg 
95734e89507SJohannes Berg 	return ret;
95834e89507SJohannes Berg }
95934e89507SJohannes Berg 
96034e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
96134e89507SJohannes Berg 			      const u8 *addr)
96234e89507SJohannes Berg {
96334e89507SJohannes Berg 	struct sta_info *sta;
96434e89507SJohannes Berg 	int ret;
96534e89507SJohannes Berg 
96634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9677852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
96834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
96934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
97034e89507SJohannes Berg 
97134e89507SJohannes Berg 	return ret;
97234e89507SJohannes Berg }
973f0706e82SJiri Benc 
974f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
975f0706e82SJiri Benc {
976f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
977f0706e82SJiri Benc 	struct sta_info *sta;
9783393a608SJuuso Oikarinen 	bool timer_needed = false;
979f0706e82SJiri Benc 
980d0709a65SJohannes Berg 	rcu_read_lock();
981d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9823393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9833393a608SJuuso Oikarinen 			timer_needed = true;
984d0709a65SJohannes Berg 	rcu_read_unlock();
985f0706e82SJiri Benc 
9865bb644a0SJohannes Berg 	if (local->quiescing)
9875bb644a0SJohannes Berg 		return;
9885bb644a0SJohannes Berg 
9893393a608SJuuso Oikarinen 	if (!timer_needed)
9903393a608SJuuso Oikarinen 		return;
9913393a608SJuuso Oikarinen 
99226d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
99326d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
994f0706e82SJiri Benc }
995f0706e82SJiri Benc 
9967bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
997f0706e82SJiri Benc {
9987bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
9997bedd0cfSJohannes Berg }
10007bedd0cfSJohannes Berg 
10017bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10027bedd0cfSJohannes Berg {
10037bedd0cfSJohannes Berg 	int err;
10047bedd0cfSJohannes Berg 
10057bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
10067bedd0cfSJohannes Berg 	if (err)
10077bedd0cfSJohannes Berg 		return err;
10087bedd0cfSJohannes Berg 
10094d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
101034e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1011f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1012f0706e82SJiri Benc 
1013b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1014b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10157bedd0cfSJohannes Berg 	return 0;
1016f0706e82SJiri Benc }
1017f0706e82SJiri Benc 
1018f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1019f0706e82SJiri Benc {
1020a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
10217bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
1022f0706e82SJiri Benc }
1023f0706e82SJiri Benc 
1024051007d9SJohannes Berg 
1025e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1026f0706e82SJiri Benc {
1027b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1028f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1029d778207bSJohannes Berg 	LIST_HEAD(free_list);
103044213b5eSJohannes Berg 	int ret = 0;
1031f0706e82SJiri Benc 
1032d0709a65SJohannes Berg 	might_sleep();
1033d0709a65SJohannes Berg 
1034e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1035e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1036e716251dSJohannes Berg 
103734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
103834e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1039e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1040e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1041d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1042d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
104334316837SJohannes Berg 			ret++;
104434316837SJohannes Berg 		}
104534e89507SJohannes Berg 	}
1046d778207bSJohannes Berg 
1047d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1048d778207bSJohannes Berg 		synchronize_net();
1049d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1050d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1051d778207bSJohannes Berg 	}
105234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
105344213b5eSJohannes Berg 
1054051007d9SJohannes Berg 	return ret;
1055051007d9SJohannes Berg }
1056051007d9SJohannes Berg 
105724723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
105824723d1bSJohannes Berg 			  unsigned long exp_time)
105924723d1bSJohannes Berg {
106024723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
106124723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
106224723d1bSJohannes Berg 
106334e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1064e46a2cf9SMohammed Shafi Shajakhan 
1065e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1066ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1067ec2b774eSMarek Lindner 			continue;
1068ec2b774eSMarek Lindner 
106924723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1070eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1071bdcbd8e0SJohannes Berg 				sta->sta.addr);
10723f52b7e3SMarco Porsch 
10733f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10743f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10753f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10763f52b7e3SMarco Porsch 
107734e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
107824723d1bSJohannes Berg 		}
1079e46a2cf9SMohammed Shafi Shajakhan 	}
1080e46a2cf9SMohammed Shafi Shajakhan 
108134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
108224723d1bSJohannes Berg }
108317741cdcSJohannes Berg 
1084686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1085686b9cb9SBen Greear 						   const u8 *addr,
1086686b9cb9SBen Greear 						   const u8 *localaddr)
108717741cdcSJohannes Berg {
10887bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
10897bedd0cfSJohannes Berg 	struct sta_info *sta;
10907bedd0cfSJohannes Berg 	struct rhash_head *tmp;
10917bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
10927bedd0cfSJohannes Berg 
10937bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
109417741cdcSJohannes Berg 
1095686b9cb9SBen Greear 	/*
1096686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1097686b9cb9SBen Greear 	 * ... first in list.
1098686b9cb9SBen Greear 	 */
10997bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1100686b9cb9SBen Greear 		if (localaddr &&
1101b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1102686b9cb9SBen Greear 			continue;
1103f7c65594SJohannes Berg 		if (!sta->uploaded)
1104f7c65594SJohannes Berg 			return NULL;
110517741cdcSJohannes Berg 		return &sta->sta;
1106f7c65594SJohannes Berg 	}
1107f7c65594SJohannes Berg 
1108abe60632SJohannes Berg 	return NULL;
110917741cdcSJohannes Berg }
1110686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11115ed176e1SJohannes Berg 
11125ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11135ed176e1SJohannes Berg 					 const u8 *addr)
11145ed176e1SJohannes Berg {
1115f7c65594SJohannes Berg 	struct sta_info *sta;
11165ed176e1SJohannes Berg 
11175ed176e1SJohannes Berg 	if (!vif)
11185ed176e1SJohannes Berg 		return NULL;
11195ed176e1SJohannes Berg 
1120f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1121f7c65594SJohannes Berg 	if (!sta)
1122f7c65594SJohannes Berg 		return NULL;
11235ed176e1SJohannes Berg 
1124f7c65594SJohannes Berg 	if (!sta->uploaded)
1125f7c65594SJohannes Berg 		return NULL;
1126f7c65594SJohannes Berg 
1127f7c65594SJohannes Berg 	return &sta->sta;
11285ed176e1SJohannes Berg }
112917741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1130af818581SJohannes Berg 
1131e3685e03SJohannes Berg /* powersave support code */
1132e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
113350a9432dSJohannes Berg {
1134608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1135e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1136e3685e03SJohannes Berg 	struct sk_buff_head pending;
1137ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1138e3685e03SJohannes Berg 	unsigned long flags;
1139d012a605SMarco Porsch 	struct ps_data *ps;
1140d012a605SMarco Porsch 
11413918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11423918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11433918edb0SFelix Fietkau 				     u.ap);
11443918edb0SFelix Fietkau 
11453918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1146d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11473f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11483f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1149d012a605SMarco Porsch 	else
1150d012a605SMarco Porsch 		return;
115150a9432dSJohannes Berg 
1152c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
115347086fc5SJohannes Berg 
11545a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1155948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1156ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1157948d887dSJohannes Berg 
115830686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
115912375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1160af818581SJohannes Berg 
1161ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1162ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1163ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1164ba8c3d6fSFelix Fietkau 
1165ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1166ba8c3d6fSFelix Fietkau 				continue;
1167ba8c3d6fSFelix Fietkau 
1168ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1169ba8c3d6fSFelix Fietkau 		}
1170ba8c3d6fSFelix Fietkau 	}
1171ba8c3d6fSFelix Fietkau 
1172948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1173af818581SJohannes Berg 
11741d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11751d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1176af818581SJohannes Berg 	/* Send all buffered frames to the station */
1177948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1178948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1179948d887dSJohannes Berg 
1180987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1181948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1182987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1183948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1184948d887dSJohannes Berg 		filtered += tmp - count;
1185948d887dSJohannes Berg 		count = tmp;
1186948d887dSJohannes Berg 
1187987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1188948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1189987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1190948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1191948d887dSJohannes Berg 		buffered += tmp - count;
1192948d887dSJohannes Berg 	}
1193948d887dSJohannes Berg 
1194e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11955ac2e350SJohannes Berg 
11965ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11975ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11985ac2e350SJohannes Berg 
11995ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12005ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12015ac2e350SJohannes Berg 	 */
12025ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12035ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12041d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1205948d887dSJohannes Berg 
1206e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1207e3685e03SJohannes Berg 
1208687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1209062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1210062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1211687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1212687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1213687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1214687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1215687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1216687da132SEmmanuel Grumbach 		       sta->sta.addr);
1217687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1218687da132SEmmanuel Grumbach 					   sta->sta.addr,
1219687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1220687da132SEmmanuel Grumbach 	}
1221687da132SEmmanuel Grumbach 
1222af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1223af818581SJohannes Berg 
1224c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1225c868cb35SJohannes Berg 
1226bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1227bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1228bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
122917c18bf8SJohannes Berg 
123017c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1231af818581SJohannes Berg }
1232af818581SJohannes Berg 
1233ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1234ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1235b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1236b77cf4f8SJohannes Berg 					 bool call_driver)
1237ce662b44SJohannes Berg {
1238ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1239ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1240ce662b44SJohannes Berg 	struct sk_buff *skb;
1241ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1242ce662b44SJohannes Berg 	__le16 fc;
1243a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1244ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
124555de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1246ce662b44SJohannes Berg 
1247ce662b44SJohannes Berg 	if (qos) {
1248ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1249ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1250ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1251ce662b44SJohannes Berg 	} else {
1252ce662b44SJohannes Berg 		size -= 2;
1253ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1254ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1255ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1256ce662b44SJohannes Berg 	}
1257ce662b44SJohannes Berg 
1258ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1259ce662b44SJohannes Berg 	if (!skb)
1260ce662b44SJohannes Berg 		return;
1261ce662b44SJohannes Berg 
1262ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1263ce662b44SJohannes Berg 
1264ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1265ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1266ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1267ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1268ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1269ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1270864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1271ce662b44SJohannes Berg 
1272ce662b44SJohannes Berg 	skb->priority = tid;
1273ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
127459b66255SJohannes Berg 	if (qos) {
1275ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1276ce662b44SJohannes Berg 
127740b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1278ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1279ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1280ce662b44SJohannes Berg 	}
1281ce662b44SJohannes Berg 
1282ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1283ce662b44SJohannes Berg 
1284ce662b44SJohannes Berg 	/*
1285ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1286ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1287deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1288deeaee19SJohannes Berg 	 * ends the poll/service period.
1289ce662b44SJohannes Berg 	 */
129002f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1291deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1292ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1293ce662b44SJohannes Berg 
12946b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
12956b127c71SSujith Manoharan 
1296b77cf4f8SJohannes Berg 	if (call_driver)
1297b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1298b77cf4f8SJohannes Berg 					  reason, false);
129940b96408SJohannes Berg 
130089afe614SJohannes Berg 	skb->dev = sdata->dev;
130189afe614SJohannes Berg 
130255de908aSJohannes Berg 	rcu_read_lock();
130355de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
130455de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
130555de908aSJohannes Berg 		rcu_read_unlock();
130655de908aSJohannes Berg 		kfree_skb(skb);
130755de908aSJohannes Berg 		return;
130855de908aSJohannes Berg 	}
130955de908aSJohannes Berg 
131073c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13117c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
131255de908aSJohannes Berg 	rcu_read_unlock();
1313ce662b44SJohannes Berg }
1314ce662b44SJohannes Berg 
13150a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13160a1cb809SJohannes Berg {
13170a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13180a1cb809SJohannes Berg 	if (tids & 0xF8)
13190a1cb809SJohannes Berg 		return fls(tids) - 1;
13200a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13210a1cb809SJohannes Berg 	if (tids & BIT(0))
13220a1cb809SJohannes Berg 		return 0;
13230a1cb809SJohannes Berg 	return fls(tids) - 1;
13240a1cb809SJohannes Berg }
13250a1cb809SJohannes Berg 
132647086fc5SJohannes Berg static void
132747086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
132847086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
132947086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1330af818581SJohannes Berg {
1331af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1332af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1333948d887dSJohannes Berg 	bool more_data = false;
1334948d887dSJohannes Berg 	int ac;
13354049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
133647086fc5SJohannes Berg 	struct sk_buff_head frames;
133747086fc5SJohannes Berg 
1338deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1339c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1340deeaee19SJohannes Berg 
134147086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1342af818581SJohannes Berg 
1343f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1344948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13454049e09aSJohannes Berg 		unsigned long tids;
13464049e09aSJohannes Berg 
134747086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1348948d887dSJohannes Berg 			continue;
1349948d887dSJohannes Berg 
13504049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13514049e09aSJohannes Berg 
1352f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1353f9f760b4SJohannes Berg 		 * release from hardware queues
1354f9f760b4SJohannes Berg 		 */
1355ba8c3d6fSFelix Fietkau 		if (skb_queue_empty(&frames)) {
1356f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1357ba8c3d6fSFelix Fietkau 			driver_release_tids |= sta->txq_buffered_tids & tids;
1358ba8c3d6fSFelix Fietkau 		}
1359f9f760b4SJohannes Berg 
13604049e09aSJohannes Berg 		if (driver_release_tids) {
1361f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1362f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1363f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1364f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1365f9f760b4SJohannes Berg 			 */
1366f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1367f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1368f9f760b4SJohannes Berg 				more_data = true;
1369f9f760b4SJohannes Berg 				driver_release_tids =
1370f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1371f9f760b4SJohannes Berg 						driver_release_tids));
1372f9f760b4SJohannes Berg 				break;
1373f9f760b4SJohannes Berg 			}
13744049e09aSJohannes Berg 		} else {
137547086fc5SJohannes Berg 			struct sk_buff *skb;
137647086fc5SJohannes Berg 
137747086fc5SJohannes Berg 			while (n_frames > 0) {
1378948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1379948d887dSJohannes Berg 				if (!skb) {
138047086fc5SJohannes Berg 					skb = skb_dequeue(
138147086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1382af818581SJohannes Berg 					if (skb)
1383af818581SJohannes Berg 						local->total_ps_buffered--;
1384af818581SJohannes Berg 				}
138547086fc5SJohannes Berg 				if (!skb)
138647086fc5SJohannes Berg 					break;
138747086fc5SJohannes Berg 				n_frames--;
138847086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
138947086fc5SJohannes Berg 			}
1390948d887dSJohannes Berg 		}
1391948d887dSJohannes Berg 
1392f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1393f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1394f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13954049e09aSJohannes Berg 		 */
1396948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1397948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1398948d887dSJohannes Berg 			more_data = true;
1399948d887dSJohannes Berg 			break;
1400948d887dSJohannes Berg 		}
1401948d887dSJohannes Berg 	}
1402af818581SJohannes Berg 
1403f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1404ce662b44SJohannes Berg 		int tid;
14054049e09aSJohannes Berg 
1406ce662b44SJohannes Berg 		/*
1407ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1408ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1409ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1410ce662b44SJohannes Berg 		 *
1411ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1412ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1413ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1414ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1415ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1416ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1417ce662b44SJohannes Berg 		 *
1418ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1419ce662b44SJohannes Berg 		 */
1420ce662b44SJohannes Berg 
1421ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1422ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1423ce662b44SJohannes Berg 
1424b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1425f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
142647086fc5SJohannes Berg 		struct sk_buff_head pending;
142747086fc5SJohannes Berg 		struct sk_buff *skb;
142840b96408SJohannes Berg 		int num = 0;
142940b96408SJohannes Berg 		u16 tids = 0;
1430b77cf4f8SJohannes Berg 		bool need_null = false;
143147086fc5SJohannes Berg 
143247086fc5SJohannes Berg 		skb_queue_head_init(&pending);
143347086fc5SJohannes Berg 
143447086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1435af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
143647086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
143740b96408SJohannes Berg 			u8 *qoshdr = NULL;
143840b96408SJohannes Berg 
143940b96408SJohannes Berg 			num++;
1440af818581SJohannes Berg 
1441af818581SJohannes Berg 			/*
144247086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
144347086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
144447086fc5SJohannes Berg 			 * exchange.
1445af818581SJohannes Berg 			 */
14466b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
14476b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1448af818581SJohannes Berg 
144947086fc5SJohannes Berg 			/*
145047086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
145147086fc5SJohannes Berg 			 * more buffered frames for this STA
145247086fc5SJohannes Berg 			 */
145324b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
145447086fc5SJohannes Berg 				hdr->frame_control |=
145547086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
145624b9c373SJanusz.Dziedzic@tieto.com 			else
145724b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
145824b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1459af818581SJohannes Berg 
146040b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
146140b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
146240b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
146340b96408SJohannes Berg 
1464b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1465b77cf4f8SJohannes Berg 
1466b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1467b77cf4f8SJohannes Berg 
1468b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1469b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1470b77cf4f8SJohannes Berg 				continue;
1471b77cf4f8SJohannes Berg 
1472b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1473b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1474b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1475b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1476b77cf4f8SJohannes Berg 				break;
1477b77cf4f8SJohannes Berg 			}
1478b77cf4f8SJohannes Berg 
1479b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1480b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1481b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1482b77cf4f8SJohannes Berg 			 * and be done.
1483b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1484b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1485b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1486b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1487b77cf4f8SJohannes Berg 			 *
1488b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1489b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1490b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1491b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1492b77cf4f8SJohannes Berg 			 *
1493b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1494b77cf4f8SJohannes Berg 			 */
1495b77cf4f8SJohannes Berg 			if (qoshdr) {
149640b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1497deeaee19SJohannes Berg 
149847086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
149947086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1500b77cf4f8SJohannes Berg 			} else {
1501b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1502b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1503b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1504b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1505b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1506b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1507b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1508b77cf4f8SJohannes Berg 				 */
1509b77cf4f8SJohannes Berg 				hdr->frame_control |=
1510b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1511b77cf4f8SJohannes Berg 				need_null = true;
1512b77cf4f8SJohannes Berg 				num++;
151352a3f20cSMarco Porsch 			}
1514b77cf4f8SJohannes Berg 			break;
151547086fc5SJohannes Berg 		}
151647086fc5SJohannes Berg 
151740b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
151840b96408SJohannes Berg 					  reason, more_data);
151940b96408SJohannes Berg 
152047086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1521af818581SJohannes Berg 
1522b77cf4f8SJohannes Berg 		if (need_null)
1523b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1524b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1525b77cf4f8SJohannes Berg 				reason, false);
1526b77cf4f8SJohannes Berg 
1527c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1528af818581SJohannes Berg 	} else {
1529ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1530ba8c3d6fSFelix Fietkau 		int tid;
1531ba8c3d6fSFelix Fietkau 
1532af818581SJohannes Berg 		/*
15334049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
15344049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1535f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1536f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1537f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1538f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1539f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1540f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1541af818581SJohannes Berg 		 */
15424049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
154347086fc5SJohannes Berg 					    n_frames, reason, more_data);
15444049e09aSJohannes Berg 
15454049e09aSJohannes Berg 		/*
15464049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
15474049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1548f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
15494049e09aSJohannes Berg 		 * release function.
1550f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1551ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1552ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
15534049e09aSJohannes Berg 		 */
1554ba8c3d6fSFelix Fietkau 
1555ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1556ba8c3d6fSFelix Fietkau 			return;
1557ba8c3d6fSFelix Fietkau 
1558ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1559ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1560ba8c3d6fSFelix Fietkau 
1561ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1562ba8c3d6fSFelix Fietkau 				continue;
1563ba8c3d6fSFelix Fietkau 
1564ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1565ba8c3d6fSFelix Fietkau 			break;
1566ba8c3d6fSFelix Fietkau 		}
1567af818581SJohannes Berg 	}
1568af818581SJohannes Berg }
1569af818581SJohannes Berg 
1570af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1571af818581SJohannes Berg {
157247086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1573af818581SJohannes Berg 
1574af818581SJohannes Berg 	/*
157547086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
157647086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
157747086fc5SJohannes Berg 	 * only from the non-enabled ones.
1578af818581SJohannes Berg 	 */
157947086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
158047086fc5SJohannes Berg 		ignore_for_response = 0;
1581af818581SJohannes Berg 
158247086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
158347086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1584af818581SJohannes Berg }
158547086fc5SJohannes Berg 
158647086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
158747086fc5SJohannes Berg {
158847086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
158947086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
159047086fc5SJohannes Berg 
159147086fc5SJohannes Berg 	/*
159247086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
159347086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
159447086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
159547086fc5SJohannes Berg 	 * actually getting called.
159647086fc5SJohannes Berg 	 */
159747086fc5SJohannes Berg 	if (!delivery_enabled)
159847086fc5SJohannes Berg 		return;
159947086fc5SJohannes Berg 
160047086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
160147086fc5SJohannes Berg 	case 1:
160247086fc5SJohannes Berg 		n_frames = 2;
160347086fc5SJohannes Berg 		break;
160447086fc5SJohannes Berg 	case 2:
160547086fc5SJohannes Berg 		n_frames = 4;
160647086fc5SJohannes Berg 		break;
160747086fc5SJohannes Berg 	case 3:
160847086fc5SJohannes Berg 		n_frames = 6;
160947086fc5SJohannes Berg 		break;
161047086fc5SJohannes Berg 	case 0:
161147086fc5SJohannes Berg 		/* XXX: what is a good value? */
161213a8098aSAndrei Otcheretianski 		n_frames = 128;
161347086fc5SJohannes Berg 		break;
161447086fc5SJohannes Berg 	}
161547086fc5SJohannes Berg 
161647086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
161747086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1618af818581SJohannes Berg }
1619af818581SJohannes Berg 
1620af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1621af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1622af818581SJohannes Berg {
1623af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1624af818581SJohannes Berg 
1625b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1626b5878a2dSJohannes Berg 
16275ac2e350SJohannes Berg 	if (block) {
1628c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
162917c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
16305ac2e350SJohannes Berg 		return;
16315ac2e350SJohannes Berg 	}
16325ac2e350SJohannes Berg 
16335ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
16345ac2e350SJohannes Berg 		return;
16355ac2e350SJohannes Berg 
16365ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
16375ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
16385ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16395ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16405ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
16415ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
16425ac2e350SJohannes Berg 		/* must be asleep in this case */
16435ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16445ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16455ac2e350SJohannes Berg 	} else {
16465ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
164717c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
16485ac2e350SJohannes Berg 	}
1649af818581SJohannes Berg }
1650af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1651dcf55fb5SFelix Fietkau 
1652e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
165337fbd908SJohannes Berg {
165437fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
165537fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
165637fbd908SJohannes Berg 
165737fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
165837fbd908SJohannes Berg 
165937fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
166037fbd908SJohannes Berg }
1661e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
166237fbd908SJohannes Berg 
1663042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1664042ec453SJohannes Berg 				u8 tid, bool buffered)
1665dcf55fb5SFelix Fietkau {
1666dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1667dcf55fb5SFelix Fietkau 
16685a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1669042ec453SJohannes Berg 		return;
1670042ec453SJohannes Berg 
16711b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
16721b000789SJohannes Berg 
1673948d887dSJohannes Berg 	if (buffered)
1674948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1675948d887dSJohannes Berg 	else
1676948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1677948d887dSJohannes Berg 
1678c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1679dcf55fb5SFelix Fietkau }
1680042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1681d9a7ddb0SJohannes Berg 
168283d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1683d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1684d9a7ddb0SJohannes Berg {
16858bf11d8dSJohannes Berg 	might_sleep();
1686d9a7ddb0SJohannes Berg 
1687d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1688d9a7ddb0SJohannes Berg 		return 0;
1689d9a7ddb0SJohannes Berg 
1690f09603a2SJohannes Berg 	/* check allowed transitions first */
1691f09603a2SJohannes Berg 
1692d9a7ddb0SJohannes Berg 	switch (new_state) {
1693d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1694f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1695d9a7ddb0SJohannes Berg 			return -EINVAL;
1696d9a7ddb0SJohannes Berg 		break;
1697d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1698f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1699f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1700d9a7ddb0SJohannes Berg 			return -EINVAL;
1701d9a7ddb0SJohannes Berg 		break;
1702d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1703f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1704f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1705d9a7ddb0SJohannes Berg 			return -EINVAL;
1706d9a7ddb0SJohannes Berg 		break;
1707d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1708f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1709d9a7ddb0SJohannes Berg 			return -EINVAL;
1710d9a7ddb0SJohannes Berg 		break;
1711d9a7ddb0SJohannes Berg 	default:
1712d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1713d9a7ddb0SJohannes Berg 		return -EINVAL;
1714d9a7ddb0SJohannes Berg 	}
1715d9a7ddb0SJohannes Berg 
1716bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1717bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1718f09603a2SJohannes Berg 
1719f09603a2SJohannes Berg 	/*
1720f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1721f09603a2SJohannes Berg 	 * fail the transition
1722f09603a2SJohannes Berg 	 */
1723f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1724f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1725f09603a2SJohannes Berg 					sta->sta_state, new_state);
1726f09603a2SJohannes Berg 		if (err)
1727f09603a2SJohannes Berg 			return err;
1728f09603a2SJohannes Berg 	}
1729f09603a2SJohannes Berg 
1730f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1731f09603a2SJohannes Berg 
1732f09603a2SJohannes Berg 	switch (new_state) {
1733f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1734f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1735f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1736f09603a2SJohannes Berg 		break;
1737f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1738f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1739f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1740f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1741f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1742f09603a2SJohannes Berg 		break;
1743f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1744f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1745f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1746f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
17477e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17487e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17497e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17507e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1751f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
175217c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
1753f09603a2SJohannes Berg 		}
1754f09603a2SJohannes Berg 		break;
1755f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1756f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
17577e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17587e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17597e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17607e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1761f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
176217c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
1763f09603a2SJohannes Berg 		}
1764f09603a2SJohannes Berg 		break;
1765f09603a2SJohannes Berg 	default:
1766f09603a2SJohannes Berg 		break;
1767f09603a2SJohannes Berg 	}
1768f09603a2SJohannes Berg 
1769d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1770d9a7ddb0SJohannes Berg 
1771d9a7ddb0SJohannes Berg 	return 0;
1772d9a7ddb0SJohannes Berg }
1773687da132SEmmanuel Grumbach 
1774687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1775687da132SEmmanuel Grumbach {
1776687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1777687da132SEmmanuel Grumbach 	u8 rx_streams;
1778687da132SEmmanuel Grumbach 
1779687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1780687da132SEmmanuel Grumbach 		return 1;
1781687da132SEmmanuel Grumbach 
1782687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1783687da132SEmmanuel Grumbach 		int i;
1784687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1785687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1786687da132SEmmanuel Grumbach 
1787687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1788687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1789687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1790687da132SEmmanuel Grumbach 				return i + 1;
1791687da132SEmmanuel Grumbach 	}
1792687da132SEmmanuel Grumbach 
1793687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1794687da132SEmmanuel Grumbach 		rx_streams = 4;
1795687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1796687da132SEmmanuel Grumbach 		rx_streams = 3;
1797687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1798687da132SEmmanuel Grumbach 		rx_streams = 2;
1799687da132SEmmanuel Grumbach 	else
1800687da132SEmmanuel Grumbach 		rx_streams = 1;
1801687da132SEmmanuel Grumbach 
1802687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1803687da132SEmmanuel Grumbach 		return rx_streams;
1804687da132SEmmanuel Grumbach 
1805687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1806687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1807687da132SEmmanuel Grumbach }
1808b7ffbd7eSJohannes Berg 
1809fbd6ff5cSJohannes Berg static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1810fbd6ff5cSJohannes Berg {
1811fbd6ff5cSJohannes Berg 	rinfo->flags = 0;
1812fbd6ff5cSJohannes Berg 
1813fbd6ff5cSJohannes Berg 	if (sta->last_rx_rate_flag & RX_FLAG_HT) {
1814fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
1815fbd6ff5cSJohannes Berg 		rinfo->mcs = sta->last_rx_rate_idx;
1816fbd6ff5cSJohannes Berg 	} else if (sta->last_rx_rate_flag & RX_FLAG_VHT) {
1817fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
1818fbd6ff5cSJohannes Berg 		rinfo->nss = sta->last_rx_rate_vht_nss;
1819fbd6ff5cSJohannes Berg 		rinfo->mcs = sta->last_rx_rate_idx;
1820fbd6ff5cSJohannes Berg 	} else {
1821fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
1822fbd6ff5cSJohannes Berg 		int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1823fbd6ff5cSJohannes Berg 		u16 brate;
1824fbd6ff5cSJohannes Berg 
1825fbd6ff5cSJohannes Berg 		sband = sta->local->hw.wiphy->bands[
1826fbd6ff5cSJohannes Berg 				ieee80211_get_sdata_band(sta->sdata)];
1827fbd6ff5cSJohannes Berg 		brate = sband->bitrates[sta->last_rx_rate_idx].bitrate;
1828fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1829fbd6ff5cSJohannes Berg 	}
1830fbd6ff5cSJohannes Berg 
1831fbd6ff5cSJohannes Berg 	if (sta->last_rx_rate_flag & RX_FLAG_SHORT_GI)
1832fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1833fbd6ff5cSJohannes Berg 
1834fbd6ff5cSJohannes Berg 	if (sta->last_rx_rate_flag & RX_FLAG_5MHZ)
1835fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_5;
1836fbd6ff5cSJohannes Berg 	else if (sta->last_rx_rate_flag & RX_FLAG_10MHZ)
1837fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_10;
1838fbd6ff5cSJohannes Berg 	else if (sta->last_rx_rate_flag & RX_FLAG_40MHZ)
1839fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
1840fbd6ff5cSJohannes Berg 	else if (sta->last_rx_rate_vht_flag & RX_VHT_FLAG_80MHZ)
1841fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
1842fbd6ff5cSJohannes Berg 	else if (sta->last_rx_rate_vht_flag & RX_VHT_FLAG_160MHZ)
1843fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
1844fbd6ff5cSJohannes Berg 	else
1845fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
1846fbd6ff5cSJohannes Berg }
1847fbd6ff5cSJohannes Berg 
1848b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1849b7ffbd7eSJohannes Berg {
1850b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1851b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
18529a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1853b7ffbd7eSJohannes Berg 	u32 thr = 0;
1854b7ffbd7eSJohannes Berg 	int i, ac;
1855b7ffbd7eSJohannes Berg 
18569a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
18579a244409SJohn W. Linville 		ref = local->rate_ctrl;
18589a244409SJohn W. Linville 
1859b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1860b7ffbd7eSJohannes Berg 
1861225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1862225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1863225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1864225b8189SJohannes Berg 	 */
1865225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1866225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1867225b8189SJohannes Berg 
18682b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
18692b9a7e1bSJohannes Berg 
1870319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1871319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1872319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1873319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1874*976bd9efSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1875*976bd9efSJohannes Berg 
1876*976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1877*976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1878*976bd9efSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1879*976bd9efSJohannes Berg 	}
1880b7ffbd7eSJohannes Berg 
188184b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
1882b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
18832b9a7e1bSJohannes Berg 
1884319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1885319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1886b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
18872b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1888b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1889319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1890b7ffbd7eSJohannes Berg 	}
18912b9a7e1bSJohannes Berg 
1892319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
18932b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
18942b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
18952b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1896319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
18972b9a7e1bSJohannes Berg 	}
18982b9a7e1bSJohannes Berg 
1899319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1900319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1901b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1902319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
19032b9a7e1bSJohannes Berg 	}
19042b9a7e1bSJohannes Berg 
1905319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1906b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1907319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
19082b9a7e1bSJohannes Berg 	}
19092b9a7e1bSJohannes Berg 
1910319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1911b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1912319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
19132b9a7e1bSJohannes Berg 	}
19142b9a7e1bSJohannes Berg 
1915319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1916b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1917319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
19182b9a7e1bSJohannes Berg 	}
19192b9a7e1bSJohannes Berg 
1920b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1921b7ffbd7eSJohannes Berg 
1922225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1923225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1924225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1925225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1926225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1927225b8189SJohannes Berg 	}
1928225b8189SJohannes Berg 
192930686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
193030686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
1931319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1932b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1933319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1934b7ffbd7eSJohannes Berg 		}
19352b9a7e1bSJohannes Berg 
1936319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
193740d9a38aSJohannes Berg 			sinfo->signal_avg =
193840d9a38aSJohannes Berg 				(s8) -ewma_signal_read(&sta->avg_signal);
1939319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
19402b9a7e1bSJohannes Berg 		}
19412b9a7e1bSJohannes Berg 	}
19422b9a7e1bSJohannes Berg 
19432b9a7e1bSJohannes Berg 	if (sta->chains &&
1944319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1945319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1946319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1947319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1948b7ffbd7eSJohannes Berg 
1949b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1950b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1951b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1952b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
195340d9a38aSJohannes Berg 				(s8) -ewma_signal_read(&sta->chain_signal_avg[i]);
1954b7ffbd7eSJohannes Berg 		}
1955b7ffbd7eSJohannes Berg 	}
1956b7ffbd7eSJohannes Berg 
1957319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1958b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1959319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
19602b9a7e1bSJohannes Berg 	}
19612b9a7e1bSJohannes Berg 
1962319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1963b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1964319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
19652b9a7e1bSJohannes Berg 	}
1966b7ffbd7eSJohannes Berg 
196779c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
196879c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
196979c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
197079c892b8SJohannes Berg 
197179c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
197279c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
197379c892b8SJohannes Berg 			tidstats->rx_msdu = sta->rx_msdu[i];
197479c892b8SJohannes Berg 		}
197579c892b8SJohannes Berg 
197679c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
197779c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
197879c892b8SJohannes Berg 			tidstats->tx_msdu = sta->tx_msdu[i];
197979c892b8SJohannes Berg 		}
198079c892b8SJohannes Berg 
198179c892b8SJohannes Berg 		if (!(tidstats->filled &
198279c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
198330686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
198479c892b8SJohannes Berg 			tidstats->filled |=
198579c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
198679c892b8SJohannes Berg 			tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
198779c892b8SJohannes Berg 		}
198879c892b8SJohannes Berg 
198979c892b8SJohannes Berg 		if (!(tidstats->filled &
199079c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
199130686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
199279c892b8SJohannes Berg 			tidstats->filled |=
199379c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
199479c892b8SJohannes Berg 			tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
199579c892b8SJohannes Berg 		}
199679c892b8SJohannes Berg 	}
199779c892b8SJohannes Berg 
1998b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1999b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2000319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2001319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
2002319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
2003319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
2004319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
2005319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
2006b7ffbd7eSJohannes Berg 
2007433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2008433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2009433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2010b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2011319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2012433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2013b7ffbd7eSJohannes Berg 		}
2014433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2015433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2016433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2017b7ffbd7eSJohannes Berg #endif
2018b7ffbd7eSJohannes Berg 	}
2019b7ffbd7eSJohannes Berg 
2020b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2021b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2022b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2023b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2024b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2025b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2026b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2027785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2028b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2029b7ffbd7eSJohannes Berg 
2030b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2031b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2032b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2033b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2034b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2035b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2036b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2037b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2038b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2039b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2040b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2041b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2042a74a8c84SJohannes Berg 	if (sta->sta.wme)
2043b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2044b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2045b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2046b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2047b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2048b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2049b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2050b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2051b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2052b7ffbd7eSJohannes Berg 
2053b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2054b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2055b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2056b7ffbd7eSJohannes Berg 	else
2057b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
2058b7ffbd7eSJohannes Berg 
2059b7ffbd7eSJohannes Berg 	if (thr != 0) {
2060319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2061b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
2062b7ffbd7eSJohannes Berg 	}
2063b7ffbd7eSJohannes Berg }
2064