xref: /openbmc/linux/net/mac80211/sta_info.c (revision caf22d311a35d1c0c2c73e1f0988276d51175c8f)
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 */
69*caf22d31SJohannes Berg 	.automatic_shrinking = true,
707bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
717bedd0cfSJohannes Berg 	.key_offset = offsetof(struct sta_info, sta.addr),
727bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
737bedd0cfSJohannes Berg 	.hashfn = sta_addr_hash,
747bedd0cfSJohannes Berg };
757bedd0cfSJohannes Berg 
764d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
77be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
78f0706e82SJiri Benc 			     struct sta_info *sta)
79f0706e82SJiri Benc {
807bedd0cfSJohannes Berg 	return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
817bedd0cfSJohannes Berg 				      sta_rht_params);
82f0706e82SJiri Benc }
83f0706e82SJiri Benc 
845108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
85b22cfcfcSEliad Peller {
86b22cfcfcSEliad Peller 	int ac, i;
87b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
88b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
89b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
90d012a605SMarco Porsch 	struct ps_data *ps;
91b22cfcfcSEliad Peller 
92e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
935ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
945ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
95d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
96d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
97d012a605SMarco Porsch 			ps = &sdata->bss->ps;
983f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
993f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
100d012a605SMarco Porsch 		else
101d012a605SMarco Porsch 			return;
102b22cfcfcSEliad Peller 
103b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
104e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1055ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
106b22cfcfcSEliad Peller 
107d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
108b22cfcfcSEliad Peller 	}
109b22cfcfcSEliad Peller 
110ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
111ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
112ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
113ba8c3d6fSFelix Fietkau 			int n = skb_queue_len(&txqi->queue);
114ba8c3d6fSFelix Fietkau 
115ba8c3d6fSFelix Fietkau 			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
116ba8c3d6fSFelix Fietkau 			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
117ba8c3d6fSFelix Fietkau 		}
118ba8c3d6fSFelix Fietkau 	}
119ba8c3d6fSFelix Fietkau 
120b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
121b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1221f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1231f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
124b22cfcfcSEliad Peller 	}
125b22cfcfcSEliad Peller 
12645b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12745b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
128b22cfcfcSEliad Peller 
1295ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
130b22cfcfcSEliad Peller 
131b22cfcfcSEliad Peller 	/*
132b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
133b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
134b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
135b22cfcfcSEliad Peller 	 * directly by station destruction.
136b22cfcfcSEliad Peller 	 */
1375a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
138661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
139b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
140b22cfcfcSEliad Peller 		if (!tid_tx)
141b22cfcfcSEliad Peller 			continue;
1421f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
143b22cfcfcSEliad Peller 		kfree(tid_tx);
144b22cfcfcSEliad Peller 	}
1455108ca82SJohannes Berg }
146b22cfcfcSEliad Peller 
1475108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1485108ca82SJohannes Berg {
1495108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1505108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1515108ca82SJohannes Berg 
1525108ca82SJohannes Berg 	__cleanup_single_sta(sta);
153b22cfcfcSEliad Peller 	sta_info_free(local, sta);
154b22cfcfcSEliad Peller }
155b22cfcfcSEliad Peller 
156d0709a65SJohannes Berg /* protected by RCU */
157abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
158abe60632SJohannes Berg 			      const u8 *addr)
15943ba7e95SJohannes Berg {
160abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16160f4b626SJohannes Berg 	struct sta_info *sta;
16260f4b626SJohannes Berg 	struct rhash_head *tmp;
16360f4b626SJohannes Berg 	const struct bucket_table *tbl;
16443ba7e95SJohannes Berg 
16560f4b626SJohannes Berg 	rcu_read_lock();
16660f4b626SJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
16760f4b626SJohannes Berg 
16860f4b626SJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
16960f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17060f4b626SJohannes Berg 			rcu_read_unlock();
17160f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17260f4b626SJohannes Berg 			 * another rcu read section or the mutex
17360f4b626SJohannes Berg 			 */
17460f4b626SJohannes Berg 			return sta;
17560f4b626SJohannes Berg 		}
17660f4b626SJohannes Berg 	}
17760f4b626SJohannes Berg 	rcu_read_unlock();
17860f4b626SJohannes Berg 	return NULL;
17943ba7e95SJohannes Berg }
18043ba7e95SJohannes Berg 
1810e5ded5aSFelix Fietkau /*
1820e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1830e5ded5aSFelix Fietkau  * or from one of its vlans
1840e5ded5aSFelix Fietkau  */
1850e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1860e5ded5aSFelix Fietkau 				  const u8 *addr)
1870e5ded5aSFelix Fietkau {
1880e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1890e5ded5aSFelix Fietkau 	struct sta_info *sta;
1907bedd0cfSJohannes Berg 	struct rhash_head *tmp;
1917bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
1920e5ded5aSFelix Fietkau 
1937bedd0cfSJohannes Berg 	rcu_read_lock();
1947bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1957bedd0cfSJohannes Berg 
1967bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1977bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
1987bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
1997bedd0cfSJohannes Berg 			rcu_read_unlock();
2007bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2017bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2027bedd0cfSJohannes Berg 			 */
2030e5ded5aSFelix Fietkau 			return sta;
2040e5ded5aSFelix Fietkau 		}
2057bedd0cfSJohannes Berg 	}
2067bedd0cfSJohannes Berg 	rcu_read_unlock();
2077bedd0cfSJohannes Berg 	return NULL;
2087bedd0cfSJohannes Berg }
2090e5ded5aSFelix Fietkau 
2103b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2113b53fde8SJohannes Berg 				     int idx)
212ee385855SLuis Carlos Cobo {
2133b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
214ee385855SLuis Carlos Cobo 	struct sta_info *sta;
215ee385855SLuis Carlos Cobo 	int i = 0;
216ee385855SLuis Carlos Cobo 
217d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2183b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2192a8ca29aSLuis Carlos Cobo 			continue;
220ee385855SLuis Carlos Cobo 		if (i < idx) {
221ee385855SLuis Carlos Cobo 			++i;
222ee385855SLuis Carlos Cobo 			continue;
223ee385855SLuis Carlos Cobo 		}
2242a8ca29aSLuis Carlos Cobo 		return sta;
225ee385855SLuis Carlos Cobo 	}
226ee385855SLuis Carlos Cobo 
227ee385855SLuis Carlos Cobo 	return NULL;
228ee385855SLuis Carlos Cobo }
229f0706e82SJiri Benc 
23093e5deb1SJohannes Berg /**
231d9a7ddb0SJohannes Berg  * sta_info_free - free STA
23293e5deb1SJohannes Berg  *
2336ef307bcSRandy Dunlap  * @local: pointer to the global information
23493e5deb1SJohannes Berg  * @sta: STA info to free
23593e5deb1SJohannes Berg  *
23693e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
237d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
238d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
239d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
24093e5deb1SJohannes Berg  */
241d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
24293e5deb1SJohannes Berg {
243889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2444b7679a5SJohannes Berg 		rate_control_free_sta(sta);
24593e5deb1SJohannes Berg 
246bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
24793e5deb1SJohannes Berg 
248ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
249ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
25053d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
25193e5deb1SJohannes Berg 	kfree(sta);
25293e5deb1SJohannes Berg }
25393e5deb1SJohannes Berg 
2544d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
255d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
256d0709a65SJohannes Berg 			      struct sta_info *sta)
257f0706e82SJiri Benc {
2587bedd0cfSJohannes Berg 	rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
2597bedd0cfSJohannes Berg 			       sta_rht_params);
260f0706e82SJiri Benc }
261f0706e82SJiri Benc 
2625ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
263af818581SJohannes Berg {
264af818581SJohannes Berg 	struct sta_info *sta;
265af818581SJohannes Berg 
2665ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
267af818581SJohannes Berg 
268af818581SJohannes Berg 	if (sta->dead)
269af818581SJohannes Berg 		return;
270af818581SJohannes Berg 
27154420473SHelmut Schaa 	local_bh_disable();
2725ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
273af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2745ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
275af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2765ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
27747086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
278ce662b44SJohannes Berg 	local_bh_enable();
279af818581SJohannes Berg }
280af818581SJohannes Berg 
281af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
282af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
283af65cd96SJohannes Berg {
284af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
285af65cd96SJohannes Berg 		return 0;
286af65cd96SJohannes Berg 
287889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
288af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
289af65cd96SJohannes Berg 						     &sta->sta, gfp);
290889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
291af65cd96SJohannes Berg 		return -ENOMEM;
292af65cd96SJohannes Berg 
293af65cd96SJohannes Berg 	return 0;
294af65cd96SJohannes Berg }
295af65cd96SJohannes Berg 
29673651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
29756544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
298f0706e82SJiri Benc {
299d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
300ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
301f0706e82SJiri Benc 	struct sta_info *sta;
302ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
30316c5f15cSRon Rindjunsky 	int i;
304f0706e82SJiri Benc 
305ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
306f0706e82SJiri Benc 	if (!sta)
30773651ee6SJohannes Berg 		return NULL;
308f0706e82SJiri Benc 
30907346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3101d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3115ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
31267c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
313a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
31487f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
31587f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
31687f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
31787f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3186c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
31987f59c70SThomas Pedersen #endif
32007346f81SJohannes Berg 
32117741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
322d0709a65SJohannes Berg 	sta->local = local;
323d0709a65SJohannes Berg 	sta->sdata = sdata;
3248bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
325f0706e82SJiri Benc 
32671ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
32771ec375cSJohannes Berg 
328b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
329b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
330b6da911bSLiad Kaufman 
33118171520SThomas Gleixner 	ktime_get_ts(&uptime);
332ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
333541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
334ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
335ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
336541a45a1SBruno Randolf 
337ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
338ba8c3d6fSFelix Fietkau 		void *txq_data;
339ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
340ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
341ba8c3d6fSFelix Fietkau 
342ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
343ba8c3d6fSFelix Fietkau 		if (!txq_data)
344ba8c3d6fSFelix Fietkau 			goto free;
345ba8c3d6fSFelix Fietkau 
346ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
347ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
348ba8c3d6fSFelix Fietkau 
349ba8c3d6fSFelix Fietkau 			ieee80211_init_tx_queue(sdata, sta, txq, i);
350abfbc3afSJohannes Berg 		}
351ba8c3d6fSFelix Fietkau 	}
352ba8c3d6fSFelix Fietkau 
353ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
354ba8c3d6fSFelix Fietkau 		goto free_txq;
355f0706e82SJiri Benc 
3565a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
357a622ab72SJohannes Berg 		/*
358a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
359a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
360a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
361a622ab72SJohannes Berg 		 */
36216c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
36316c5f15cSRon Rindjunsky 	}
364948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
365948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
366948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
367948d887dSJohannes Berg 	}
36873651ee6SJohannes Berg 
3695a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3704be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
371cccaec98SSenthil Balasubramanian 
372af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
373687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
374687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
375687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
376ba8c3d6fSFelix Fietkau 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
377687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
378687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
379687da132SEmmanuel Grumbach 		/*
380687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
381687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
382687da132SEmmanuel Grumbach 		 */
383687da132SEmmanuel Grumbach 		switch (smps) {
384687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
385687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
386687da132SEmmanuel Grumbach 			break;
387687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
388687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
389687da132SEmmanuel Grumbach 			break;
390687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
391687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
392687da132SEmmanuel Grumbach 			break;
393687da132SEmmanuel Grumbach 		default:
394687da132SEmmanuel Grumbach 			WARN_ON(1);
395687da132SEmmanuel Grumbach 		}
396687da132SEmmanuel Grumbach 	}
397af0ed69bSJohannes Berg 
398bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
399ef04a297SJohannes Berg 
400abfbc3afSJohannes Berg 	return sta;
401ba8c3d6fSFelix Fietkau 
402ba8c3d6fSFelix Fietkau free_txq:
403ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
404ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
405ba8c3d6fSFelix Fietkau free:
406ba8c3d6fSFelix Fietkau 	kfree(sta);
407ba8c3d6fSFelix Fietkau 	return NULL;
40873651ee6SJohannes Berg }
40973651ee6SJohannes Berg 
4108c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
41134e89507SJohannes Berg {
41234e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
41334e89507SJohannes Berg 
41403e4497eSJohannes Berg 	/*
41503e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
41603e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
41703e4497eSJohannes Berg 	 * and another CPU turns off the net device.
41803e4497eSJohannes Berg 	 */
4198c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4208c71df7aSGuy Eilam 		return -ENETDOWN;
42103e4497eSJohannes Berg 
422b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4238c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4248c71df7aSGuy Eilam 		return -EINVAL;
4258c71df7aSGuy Eilam 
4268c71df7aSGuy Eilam 	return 0;
42793e5deb1SJohannes Berg }
42844213b5eSJohannes Berg 
429f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
430f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
431f09603a2SJohannes Berg 				     struct sta_info *sta)
432f09603a2SJohannes Berg {
433f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
434f09603a2SJohannes Berg 	int err = 0;
435f09603a2SJohannes Berg 
436f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
437f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
438f09603a2SJohannes Berg 		if (err)
439f09603a2SJohannes Berg 			break;
440f09603a2SJohannes Berg 	}
441f09603a2SJohannes Berg 
442f09603a2SJohannes Berg 	if (!err) {
443a4ec45a4SJohannes Berg 		/*
444a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
445a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
446a4ec45a4SJohannes Berg 		 */
447a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
448f09603a2SJohannes Berg 			sta->uploaded = true;
449f09603a2SJohannes Berg 		return 0;
450f09603a2SJohannes Berg 	}
451f09603a2SJohannes Berg 
452f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
453bdcbd8e0SJohannes Berg 		sdata_info(sdata,
454bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
455bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
456f09603a2SJohannes Berg 		err = 0;
457f09603a2SJohannes Berg 	}
458f09603a2SJohannes Berg 
459f09603a2SJohannes Berg 	/* unwind on error */
460f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
461f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
462f09603a2SJohannes Berg 
463f09603a2SJohannes Berg 	return err;
464f09603a2SJohannes Berg }
465f09603a2SJohannes Berg 
46634e89507SJohannes Berg /*
4678c71df7aSGuy Eilam  * should be called with sta_mtx locked
4688c71df7aSGuy Eilam  * this function replaces the mutex lock
4698c71df7aSGuy Eilam  * with a RCU lock
4708c71df7aSGuy Eilam  */
4714d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4728c71df7aSGuy Eilam {
4738c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4748c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4757852e361SJohannes Berg 	struct station_info sinfo;
4768c71df7aSGuy Eilam 	int err = 0;
4778c71df7aSGuy Eilam 
4788c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4798c71df7aSGuy Eilam 
4807852e361SJohannes Berg 	/* check if STA exists already */
4817852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4824d33960bSJohannes Berg 		err = -EEXIST;
4834d33960bSJohannes Berg 		goto out_err;
48434e89507SJohannes Berg 	}
48534e89507SJohannes Berg 
4864d33960bSJohannes Berg 	local->num_sta++;
4874d33960bSJohannes Berg 	local->sta_generation++;
4884d33960bSJohannes Berg 	smp_mb();
4894d33960bSJohannes Berg 
4905108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
4915108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4925108ca82SJohannes Berg 
4934d33960bSJohannes Berg 	/* make the station visible */
4944d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
4954d33960bSJohannes Berg 
4962bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
49783d5cc01SJohannes Berg 
4985108ca82SJohannes Berg 	/* notify driver */
4995108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5005108ca82SJohannes Berg 	if (err)
5015108ca82SJohannes Berg 		goto out_remove;
5025108ca82SJohannes Berg 
50383d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5045108ca82SJohannes Berg 	/* accept BA sessions now */
5055108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5064d33960bSJohannes Berg 
50721f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
5084d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5094d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5104d33960bSJohannes Berg 
5114d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5124d33960bSJohannes Berg 	sinfo.filled = 0;
5134d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5144d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
515d0709a65SJohannes Berg 
516bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
517f0706e82SJiri Benc 
51834e89507SJohannes Berg 	/* move reference to rcu-protected */
51934e89507SJohannes Berg 	rcu_read_lock();
52034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
521e9f207f0SJiri Benc 
52273651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
52373651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
52473651ee6SJohannes Berg 
52573651ee6SJohannes Berg 	return 0;
5265108ca82SJohannes Berg  out_remove:
5275108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5285108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5295108ca82SJohannes Berg 	local->num_sta--;
5305108ca82SJohannes Berg 	synchronize_net();
5315108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5324d33960bSJohannes Berg  out_err:
5334d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5344d33960bSJohannes Berg 	rcu_read_lock();
5354d33960bSJohannes Berg 	return err;
5368c71df7aSGuy Eilam }
5378c71df7aSGuy Eilam 
5388c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5398c71df7aSGuy Eilam {
5408c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
541308f7fcfSZhao, Gang 	int err;
5428c71df7aSGuy Eilam 
5434d33960bSJohannes Berg 	might_sleep();
5444d33960bSJohannes Berg 
5458c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5468c71df7aSGuy Eilam 	if (err) {
5478c71df7aSGuy Eilam 		rcu_read_lock();
5488c71df7aSGuy Eilam 		goto out_free;
5498c71df7aSGuy Eilam 	}
5508c71df7aSGuy Eilam 
551004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
552004c872eSJohannes Berg 
5534d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5548c71df7aSGuy Eilam 	if (err)
555004c872eSJohannes Berg 		goto out_free;
556d0709a65SJohannes Berg 
557004c872eSJohannes Berg 	return 0;
55893e5deb1SJohannes Berg  out_free:
559d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
56093e5deb1SJohannes Berg 	return err;
561f0706e82SJiri Benc }
562f0706e82SJiri Benc 
56334e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
56434e89507SJohannes Berg {
56534e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
56634e89507SJohannes Berg 
56734e89507SJohannes Berg 	rcu_read_unlock();
56834e89507SJohannes Berg 
56934e89507SJohannes Berg 	return err;
57034e89507SJohannes Berg }
57134e89507SJohannes Berg 
572d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
573004c872eSJohannes Berg {
574004c872eSJohannes Berg 	/*
575004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
576004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
577004c872eSJohannes Berg 	 */
578d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
579004c872eSJohannes Berg }
580004c872eSJohannes Berg 
581d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
582004c872eSJohannes Berg {
583004c872eSJohannes Berg 	/*
584004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
585004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
586004c872eSJohannes Berg 	 */
587d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
588004c872eSJohannes Berg }
589004c872eSJohannes Berg 
5903d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
5913d5839b6SIlan Peer {
5923d5839b6SIlan Peer 	/*
5933d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
5943d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
5953d5839b6SIlan Peer 	 */
5963d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
5973d5839b6SIlan Peer }
5983d5839b6SIlan Peer 
599948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
600004c872eSJohannes Berg {
601948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
602948d887dSJohannes Berg 	switch (ac) {
603948d887dSJohannes Berg 	case IEEE80211_AC_VO:
604948d887dSJohannes Berg 		return BIT(6) | BIT(7);
605948d887dSJohannes Berg 	case IEEE80211_AC_VI:
606948d887dSJohannes Berg 		return BIT(4) | BIT(5);
607948d887dSJohannes Berg 	case IEEE80211_AC_BE:
608948d887dSJohannes Berg 		return BIT(0) | BIT(3);
609948d887dSJohannes Berg 	case IEEE80211_AC_BK:
610948d887dSJohannes Berg 		return BIT(1) | BIT(2);
611948d887dSJohannes Berg 	default:
612948d887dSJohannes Berg 		WARN_ON(1);
613948d887dSJohannes Berg 		return 0;
614d0709a65SJohannes Berg 	}
615004c872eSJohannes Berg }
616004c872eSJohannes Berg 
6179b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
618004c872eSJohannes Berg {
619c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
620d012a605SMarco Porsch 	struct ps_data *ps;
621948d887dSJohannes Berg 	bool indicate_tim = false;
622948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
623948d887dSJohannes Berg 	int ac;
624d012a605SMarco Porsch 	u16 id;
625004c872eSJohannes Berg 
626d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
627d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
628c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
629c868cb35SJohannes Berg 			return;
6303e122be0SJohannes Berg 
631d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
632d012a605SMarco Porsch 		id = sta->sta.aid;
6333f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6343f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6353f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
636204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6376f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6383f52b7e3SMarco Porsch #endif
639d012a605SMarco Porsch 	} else {
640d012a605SMarco Porsch 		return;
641d012a605SMarco Porsch 	}
642d012a605SMarco Porsch 
643c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
644c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
645c868cb35SJohannes Berg 		return;
646004c872eSJohannes Berg 
647c868cb35SJohannes Berg 	if (sta->dead)
648c868cb35SJohannes Berg 		goto done;
6493e122be0SJohannes Berg 
650948d887dSJohannes Berg 	/*
651948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
652948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
653948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
654948d887dSJohannes Berg 	 * non-enabled ones.
655948d887dSJohannes Berg 	 */
656948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
657948d887dSJohannes Berg 		ignore_for_tim = 0;
658948d887dSJohannes Berg 
6599b7a86f3SJohannes Berg 	if (ignore_pending)
6609b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6619b7a86f3SJohannes Berg 
662948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
663948d887dSJohannes Berg 		unsigned long tids;
664948d887dSJohannes Berg 
665948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
666948d887dSJohannes Berg 			continue;
667948d887dSJohannes Berg 
668948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
669948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
670948d887dSJohannes Berg 		if (indicate_tim)
671948d887dSJohannes Berg 			break;
672948d887dSJohannes Berg 
673948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
674948d887dSJohannes Berg 
675948d887dSJohannes Berg 		indicate_tim |=
676948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
677ba8c3d6fSFelix Fietkau 		indicate_tim |=
678ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
679004c872eSJohannes Berg 	}
680004c872eSJohannes Berg 
681c868cb35SJohannes Berg  done:
68265f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
683004c872eSJohannes Berg 
6843d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6853d5839b6SIlan Peer 		goto out_unlock;
6863d5839b6SIlan Peer 
687948d887dSJohannes Berg 	if (indicate_tim)
688d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
689c868cb35SJohannes Berg 	else
690d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6913e122be0SJohannes Berg 
6929b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
693c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
694948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
695c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
696004c872eSJohannes Berg 	}
697004c872eSJohannes Berg 
6983d5839b6SIlan Peer out_unlock:
69965f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
700004c872eSJohannes Berg }
701004c872eSJohannes Berg 
7029b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7039b7a86f3SJohannes Berg {
7049b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7059b7a86f3SJohannes Berg }
7069b7a86f3SJohannes Berg 
707cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
708f0706e82SJiri Benc {
709e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
710f0706e82SJiri Benc 	int timeout;
711f0706e82SJiri Benc 
712f0706e82SJiri Benc 	if (!skb)
713cd0b8d89SJohannes Berg 		return false;
714f0706e82SJiri Benc 
715e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
716f0706e82SJiri Benc 
717f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
71857c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
71957c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
72057c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
721f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
722f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
723e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
724f0706e82SJiri Benc }
725f0706e82SJiri Benc 
726f0706e82SJiri Benc 
727948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
728948d887dSJohannes Berg 						struct sta_info *sta, int ac)
729f0706e82SJiri Benc {
730f0706e82SJiri Benc 	unsigned long flags;
731f0706e82SJiri Benc 	struct sk_buff *skb;
732f0706e82SJiri Benc 
73360750397SJohannes Berg 	/*
73460750397SJohannes Berg 	 * First check for frames that should expire on the filtered
73560750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
73660750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
73760750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
73860750397SJohannes Berg 	 * total_ps_buffered counter.
73960750397SJohannes Berg 	 */
740f0706e82SJiri Benc 	for (;;) {
741948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
742948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
74357c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
744948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
745836341a7SJohannes Berg 		else
746f0706e82SJiri Benc 			skb = NULL;
747948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
748f0706e82SJiri Benc 
74960750397SJohannes Berg 		/*
75060750397SJohannes Berg 		 * Frames are queued in order, so if this one
75160750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
75260750397SJohannes Berg 		 * we actually reached the end of the queue we
75360750397SJohannes Berg 		 * also need to stop, of course.
75460750397SJohannes Berg 		 */
75560750397SJohannes Berg 		if (!skb)
75660750397SJohannes Berg 			break;
757d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
75860750397SJohannes Berg 	}
75960750397SJohannes Berg 
76060750397SJohannes Berg 	/*
76160750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
76260750397SJohannes Berg 	 * only find something if the filtered queue was emptied
76360750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
76460750397SJohannes Berg 	 * buffered frames.
76560750397SJohannes Berg 	 */
766f0706e82SJiri Benc 	for (;;) {
767948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
768948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
769f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
770948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
771f0706e82SJiri Benc 		else
772f0706e82SJiri Benc 			skb = NULL;
773948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
774f0706e82SJiri Benc 
77560750397SJohannes Berg 		/*
77660750397SJohannes Berg 		 * frames are queued in order, so if this one
77760750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
77860750397SJohannes Berg 		 * the queue) we can stop testing
77960750397SJohannes Berg 		 */
780836341a7SJohannes Berg 		if (!skb)
781836341a7SJohannes Berg 			break;
782836341a7SJohannes Berg 
783f0706e82SJiri Benc 		local->total_ps_buffered--;
784bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
785bdcbd8e0SJohannes Berg 		       sta->sta.addr);
786d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
787f0706e82SJiri Benc 	}
7883393a608SJuuso Oikarinen 
78960750397SJohannes Berg 	/*
79060750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
79160750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
79260750397SJohannes Berg 	 * frames.
79360750397SJohannes Berg 	 */
79460750397SJohannes Berg 	sta_info_recalc_tim(sta);
79560750397SJohannes Berg 
79660750397SJohannes Berg 	/*
79760750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
79860750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
79960750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
80060750397SJohannes Berg 	 */
801948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
802948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
803948d887dSJohannes Berg }
804948d887dSJohannes Berg 
805948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
806948d887dSJohannes Berg 					     struct sta_info *sta)
807948d887dSJohannes Berg {
808948d887dSJohannes Berg 	bool have_buffered = false;
809948d887dSJohannes Berg 	int ac;
810948d887dSJohannes Berg 
8113f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8123f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8133f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
814948d887dSJohannes Berg 		return false;
815948d887dSJohannes Berg 
816948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
817948d887dSJohannes Berg 		have_buffered |=
818948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
819948d887dSJohannes Berg 
820948d887dSJohannes Berg 	return have_buffered;
821f0706e82SJiri Benc }
822f0706e82SJiri Benc 
823d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
82434e89507SJohannes Berg {
82534e89507SJohannes Berg 	struct ieee80211_local *local;
82634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8276d10e46bSJohannes Berg 	int ret;
82834e89507SJohannes Berg 
82934e89507SJohannes Berg 	might_sleep();
83034e89507SJohannes Berg 
83134e89507SJohannes Berg 	if (!sta)
83234e89507SJohannes Berg 		return -ENOENT;
83334e89507SJohannes Berg 
83434e89507SJohannes Berg 	local = sta->local;
83534e89507SJohannes Berg 	sdata = sta->sdata;
83634e89507SJohannes Berg 
83783d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
83883d5cc01SJohannes Berg 
839098a6070SJohannes Berg 	/*
840098a6070SJohannes Berg 	 * Before removing the station from the driver and
841098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
842098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
843098a6070SJohannes Berg 	 * will be sufficient.
844098a6070SJohannes Berg 	 */
845c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
846c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
847098a6070SJohannes Berg 
84834e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
849b01711beSJohannes Berg 	if (WARN_ON(ret))
85034e89507SJohannes Berg 		return ret;
85134e89507SJohannes Berg 
852a7a6bdd0SArik Nemtsov 	/*
853a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
854a7a6bdd0SArik Nemtsov 	 * removal.
855a7a6bdd0SArik Nemtsov 	 */
856a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
857a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
858a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
859a7a6bdd0SArik Nemtsov 	}
860a7a6bdd0SArik Nemtsov 
861794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8624d33960bSJohannes Berg 
8636a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8646a9d1b91SJohannes Berg 
865a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
866a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
867a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
868a710c816SJohannes Berg 
869d778207bSJohannes Berg 	return 0;
870d778207bSJohannes Berg }
871d778207bSJohannes Berg 
872d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
873d778207bSJohannes Berg {
874d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
875d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8766f7a8d26SJohannes Berg 	struct station_info sinfo = {};
877d778207bSJohannes Berg 	int ret;
878d778207bSJohannes Berg 
879d778207bSJohannes Berg 	/*
880d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
881d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
882d778207bSJohannes Berg 	 */
883d778207bSJohannes Berg 
884d778207bSJohannes Berg 	might_sleep();
885d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
886d778207bSJohannes Berg 
887c8782078SJohannes Berg 	/* now keys can no longer be reached */
8886d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
88934e89507SJohannes Berg 
8909b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
8919b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
8929b7a86f3SJohannes Berg 
89334e89507SJohannes Berg 	sta->dead = true;
89434e89507SJohannes Berg 
89534e89507SJohannes Berg 	local->num_sta--;
89634e89507SJohannes Berg 	local->sta_generation++;
89734e89507SJohannes Berg 
89883d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
899f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
900f09603a2SJohannes Berg 		if (ret) {
90183d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
90283d5cc01SJohannes Berg 			break;
90383d5cc01SJohannes Berg 		}
90483d5cc01SJohannes Berg 	}
905d9a7ddb0SJohannes Berg 
906f09603a2SJohannes Berg 	if (sta->uploaded) {
907f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
908f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
909f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
910f09603a2SJohannes Berg 	}
91134e89507SJohannes Berg 
912bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
913bdcbd8e0SJohannes Berg 
9146f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
9156f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
916ec15e68bSJouni Malinen 
91734e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
91834e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
91921f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
92034e89507SJohannes Berg 
921d34ba216SJohannes Berg 	cleanup_single_sta(sta);
922d778207bSJohannes Berg }
923d778207bSJohannes Berg 
924d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
925d778207bSJohannes Berg {
926d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
927d778207bSJohannes Berg 
928d778207bSJohannes Berg 	if (err)
929d778207bSJohannes Berg 		return err;
930d778207bSJohannes Berg 
931d778207bSJohannes Berg 	synchronize_net();
932d778207bSJohannes Berg 
933d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
93434e89507SJohannes Berg 
93534e89507SJohannes Berg 	return 0;
93634e89507SJohannes Berg }
93734e89507SJohannes Berg 
93834e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
93934e89507SJohannes Berg {
94034e89507SJohannes Berg 	struct sta_info *sta;
94134e89507SJohannes Berg 	int ret;
94234e89507SJohannes Berg 
94334e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9447852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
94534e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
94634e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
94734e89507SJohannes Berg 
94834e89507SJohannes Berg 	return ret;
94934e89507SJohannes Berg }
95034e89507SJohannes Berg 
95134e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
95234e89507SJohannes Berg 			      const u8 *addr)
95334e89507SJohannes Berg {
95434e89507SJohannes Berg 	struct sta_info *sta;
95534e89507SJohannes Berg 	int ret;
95634e89507SJohannes Berg 
95734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9587852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
95934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
96034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
96134e89507SJohannes Berg 
96234e89507SJohannes Berg 	return ret;
96334e89507SJohannes Berg }
964f0706e82SJiri Benc 
965f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
966f0706e82SJiri Benc {
967f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
968f0706e82SJiri Benc 	struct sta_info *sta;
9693393a608SJuuso Oikarinen 	bool timer_needed = false;
970f0706e82SJiri Benc 
971d0709a65SJohannes Berg 	rcu_read_lock();
972d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9733393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9743393a608SJuuso Oikarinen 			timer_needed = true;
975d0709a65SJohannes Berg 	rcu_read_unlock();
976f0706e82SJiri Benc 
9775bb644a0SJohannes Berg 	if (local->quiescing)
9785bb644a0SJohannes Berg 		return;
9795bb644a0SJohannes Berg 
9803393a608SJuuso Oikarinen 	if (!timer_needed)
9813393a608SJuuso Oikarinen 		return;
9823393a608SJuuso Oikarinen 
98326d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
98426d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
985f0706e82SJiri Benc }
986f0706e82SJiri Benc 
9877bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
988f0706e82SJiri Benc {
9897bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
9907bedd0cfSJohannes Berg }
9917bedd0cfSJohannes Berg 
9927bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
9937bedd0cfSJohannes Berg {
9947bedd0cfSJohannes Berg 	int err;
9957bedd0cfSJohannes Berg 
9967bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
9977bedd0cfSJohannes Berg 	if (err)
9987bedd0cfSJohannes Berg 		return err;
9997bedd0cfSJohannes Berg 
10004d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
100134e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1002f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1003f0706e82SJiri Benc 
1004b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1005b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10067bedd0cfSJohannes Berg 	return 0;
1007f0706e82SJiri Benc }
1008f0706e82SJiri Benc 
1009f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1010f0706e82SJiri Benc {
1011a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
10127bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
1013f0706e82SJiri Benc }
1014f0706e82SJiri Benc 
1015051007d9SJohannes Berg 
1016e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1017f0706e82SJiri Benc {
1018b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1019f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1020d778207bSJohannes Berg 	LIST_HEAD(free_list);
102144213b5eSJohannes Berg 	int ret = 0;
1022f0706e82SJiri Benc 
1023d0709a65SJohannes Berg 	might_sleep();
1024d0709a65SJohannes Berg 
1025e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1026e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1027e716251dSJohannes Berg 
102834e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
102934e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1030e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1031e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1032d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1033d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
103434316837SJohannes Berg 			ret++;
103534316837SJohannes Berg 		}
103634e89507SJohannes Berg 	}
1037d778207bSJohannes Berg 
1038d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1039d778207bSJohannes Berg 		synchronize_net();
1040d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1041d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1042d778207bSJohannes Berg 	}
104334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
104444213b5eSJohannes Berg 
1045051007d9SJohannes Berg 	return ret;
1046051007d9SJohannes Berg }
1047051007d9SJohannes Berg 
104824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
104924723d1bSJohannes Berg 			  unsigned long exp_time)
105024723d1bSJohannes Berg {
105124723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
105224723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
105324723d1bSJohannes Berg 
105434e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1055e46a2cf9SMohammed Shafi Shajakhan 
1056e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1057ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1058ec2b774eSMarek Lindner 			continue;
1059ec2b774eSMarek Lindner 
106024723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1061eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1062bdcbd8e0SJohannes Berg 				sta->sta.addr);
10633f52b7e3SMarco Porsch 
10643f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10653f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10663f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10673f52b7e3SMarco Porsch 
106834e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
106924723d1bSJohannes Berg 		}
1070e46a2cf9SMohammed Shafi Shajakhan 	}
1071e46a2cf9SMohammed Shafi Shajakhan 
107234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
107324723d1bSJohannes Berg }
107417741cdcSJohannes Berg 
1075686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1076686b9cb9SBen Greear 						   const u8 *addr,
1077686b9cb9SBen Greear 						   const u8 *localaddr)
107817741cdcSJohannes Berg {
10797bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
10807bedd0cfSJohannes Berg 	struct sta_info *sta;
10817bedd0cfSJohannes Berg 	struct rhash_head *tmp;
10827bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
10837bedd0cfSJohannes Berg 
10847bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
108517741cdcSJohannes Berg 
1086686b9cb9SBen Greear 	/*
1087686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1088686b9cb9SBen Greear 	 * ... first in list.
1089686b9cb9SBen Greear 	 */
10907bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1091686b9cb9SBen Greear 		if (localaddr &&
1092b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1093686b9cb9SBen Greear 			continue;
1094f7c65594SJohannes Berg 		if (!sta->uploaded)
1095f7c65594SJohannes Berg 			return NULL;
109617741cdcSJohannes Berg 		return &sta->sta;
1097f7c65594SJohannes Berg 	}
1098f7c65594SJohannes Berg 
1099abe60632SJohannes Berg 	return NULL;
110017741cdcSJohannes Berg }
1101686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11025ed176e1SJohannes Berg 
11035ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11045ed176e1SJohannes Berg 					 const u8 *addr)
11055ed176e1SJohannes Berg {
1106f7c65594SJohannes Berg 	struct sta_info *sta;
11075ed176e1SJohannes Berg 
11085ed176e1SJohannes Berg 	if (!vif)
11095ed176e1SJohannes Berg 		return NULL;
11105ed176e1SJohannes Berg 
1111f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1112f7c65594SJohannes Berg 	if (!sta)
1113f7c65594SJohannes Berg 		return NULL;
11145ed176e1SJohannes Berg 
1115f7c65594SJohannes Berg 	if (!sta->uploaded)
1116f7c65594SJohannes Berg 		return NULL;
1117f7c65594SJohannes Berg 
1118f7c65594SJohannes Berg 	return &sta->sta;
11195ed176e1SJohannes Berg }
112017741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1121af818581SJohannes Berg 
1122e3685e03SJohannes Berg /* powersave support code */
1123e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
112450a9432dSJohannes Berg {
1125608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1126e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1127e3685e03SJohannes Berg 	struct sk_buff_head pending;
1128ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1129e3685e03SJohannes Berg 	unsigned long flags;
1130d012a605SMarco Porsch 	struct ps_data *ps;
1131d012a605SMarco Porsch 
11323918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11333918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11343918edb0SFelix Fietkau 				     u.ap);
11353918edb0SFelix Fietkau 
11363918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1137d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11383f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11393f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1140d012a605SMarco Porsch 	else
1141d012a605SMarco Porsch 		return;
114250a9432dSJohannes Berg 
1143c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
114447086fc5SJohannes Berg 
11455a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1146948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1147ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1148948d887dSJohannes Berg 
1149d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
115012375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1151af818581SJohannes Berg 
1152ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1153ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1154ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1155ba8c3d6fSFelix Fietkau 
1156ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1157ba8c3d6fSFelix Fietkau 				continue;
1158ba8c3d6fSFelix Fietkau 
1159ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1160ba8c3d6fSFelix Fietkau 		}
1161ba8c3d6fSFelix Fietkau 	}
1162ba8c3d6fSFelix Fietkau 
1163948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1164af818581SJohannes Berg 
11651d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11661d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1167af818581SJohannes Berg 	/* Send all buffered frames to the station */
1168948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1169948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1170948d887dSJohannes Berg 
1171987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1172948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1173987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1174948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1175948d887dSJohannes Berg 		filtered += tmp - count;
1176948d887dSJohannes Berg 		count = tmp;
1177948d887dSJohannes Berg 
1178987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1179948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1180987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1181948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1182948d887dSJohannes Berg 		buffered += tmp - count;
1183948d887dSJohannes Berg 	}
1184948d887dSJohannes Berg 
1185e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11865ac2e350SJohannes Berg 
11875ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11885ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11895ac2e350SJohannes Berg 
11905ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
11915ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
11925ac2e350SJohannes Berg 	 */
11935ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
11945ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
11951d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1196948d887dSJohannes Berg 
1197e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1198e3685e03SJohannes Berg 
1199687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1200062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1201062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1202687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1203687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1204687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1205687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1206687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1207687da132SEmmanuel Grumbach 		       sta->sta.addr);
1208687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1209687da132SEmmanuel Grumbach 					   sta->sta.addr,
1210687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1211687da132SEmmanuel Grumbach 	}
1212687da132SEmmanuel Grumbach 
1213af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1214af818581SJohannes Berg 
1215c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1216c868cb35SJohannes Berg 
1217bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1218bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1219bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1220af818581SJohannes Berg }
1221af818581SJohannes Berg 
1222ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1223ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1224b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1225b77cf4f8SJohannes Berg 					 bool call_driver)
1226ce662b44SJohannes Berg {
1227ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1228ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1229ce662b44SJohannes Berg 	struct sk_buff *skb;
1230ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1231ce662b44SJohannes Berg 	__le16 fc;
1232a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1233ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
123455de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1235ce662b44SJohannes Berg 
1236ce662b44SJohannes Berg 	if (qos) {
1237ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1238ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1239ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1240ce662b44SJohannes Berg 	} else {
1241ce662b44SJohannes Berg 		size -= 2;
1242ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1243ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1244ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1245ce662b44SJohannes Berg 	}
1246ce662b44SJohannes Berg 
1247ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1248ce662b44SJohannes Berg 	if (!skb)
1249ce662b44SJohannes Berg 		return;
1250ce662b44SJohannes Berg 
1251ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1252ce662b44SJohannes Berg 
1253ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1254ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1255ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1256ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1257ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1258ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1259864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1260ce662b44SJohannes Berg 
1261ce662b44SJohannes Berg 	skb->priority = tid;
1262ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
126359b66255SJohannes Berg 	if (qos) {
1264ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1265ce662b44SJohannes Berg 
126640b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1267ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1268ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1269ce662b44SJohannes Berg 	}
1270ce662b44SJohannes Berg 
1271ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1272ce662b44SJohannes Berg 
1273ce662b44SJohannes Berg 	/*
1274ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1275ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1276deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1277deeaee19SJohannes Berg 	 * ends the poll/service period.
1278ce662b44SJohannes Berg 	 */
127902f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1280deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1281ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1282ce662b44SJohannes Berg 
12836b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
12846b127c71SSujith Manoharan 
1285b77cf4f8SJohannes Berg 	if (call_driver)
1286b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1287b77cf4f8SJohannes Berg 					  reason, false);
128840b96408SJohannes Berg 
128989afe614SJohannes Berg 	skb->dev = sdata->dev;
129089afe614SJohannes Berg 
129155de908aSJohannes Berg 	rcu_read_lock();
129255de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
129355de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
129455de908aSJohannes Berg 		rcu_read_unlock();
129555de908aSJohannes Berg 		kfree_skb(skb);
129655de908aSJohannes Berg 		return;
129755de908aSJohannes Berg 	}
129855de908aSJohannes Berg 
129973c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13007c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
130155de908aSJohannes Berg 	rcu_read_unlock();
1302ce662b44SJohannes Berg }
1303ce662b44SJohannes Berg 
13040a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13050a1cb809SJohannes Berg {
13060a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13070a1cb809SJohannes Berg 	if (tids & 0xF8)
13080a1cb809SJohannes Berg 		return fls(tids) - 1;
13090a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13100a1cb809SJohannes Berg 	if (tids & BIT(0))
13110a1cb809SJohannes Berg 		return 0;
13120a1cb809SJohannes Berg 	return fls(tids) - 1;
13130a1cb809SJohannes Berg }
13140a1cb809SJohannes Berg 
131547086fc5SJohannes Berg static void
131647086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
131747086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
131847086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1319af818581SJohannes Berg {
1320af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1321af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1322948d887dSJohannes Berg 	bool more_data = false;
1323948d887dSJohannes Berg 	int ac;
13244049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
132547086fc5SJohannes Berg 	struct sk_buff_head frames;
132647086fc5SJohannes Berg 
1327deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1328c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1329deeaee19SJohannes Berg 
133047086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1331af818581SJohannes Berg 
1332f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1333948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13344049e09aSJohannes Berg 		unsigned long tids;
13354049e09aSJohannes Berg 
133647086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1337948d887dSJohannes Berg 			continue;
1338948d887dSJohannes Berg 
13394049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13404049e09aSJohannes Berg 
1341f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1342f9f760b4SJohannes Berg 		 * release from hardware queues
1343f9f760b4SJohannes Berg 		 */
1344ba8c3d6fSFelix Fietkau 		if (skb_queue_empty(&frames)) {
1345f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1346ba8c3d6fSFelix Fietkau 			driver_release_tids |= sta->txq_buffered_tids & tids;
1347ba8c3d6fSFelix Fietkau 		}
1348f9f760b4SJohannes Berg 
13494049e09aSJohannes Berg 		if (driver_release_tids) {
1350f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1351f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1352f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1353f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1354f9f760b4SJohannes Berg 			 */
1355f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1356f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1357f9f760b4SJohannes Berg 				more_data = true;
1358f9f760b4SJohannes Berg 				driver_release_tids =
1359f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1360f9f760b4SJohannes Berg 						driver_release_tids));
1361f9f760b4SJohannes Berg 				break;
1362f9f760b4SJohannes Berg 			}
13634049e09aSJohannes Berg 		} else {
136447086fc5SJohannes Berg 			struct sk_buff *skb;
136547086fc5SJohannes Berg 
136647086fc5SJohannes Berg 			while (n_frames > 0) {
1367948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1368948d887dSJohannes Berg 				if (!skb) {
136947086fc5SJohannes Berg 					skb = skb_dequeue(
137047086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1371af818581SJohannes Berg 					if (skb)
1372af818581SJohannes Berg 						local->total_ps_buffered--;
1373af818581SJohannes Berg 				}
137447086fc5SJohannes Berg 				if (!skb)
137547086fc5SJohannes Berg 					break;
137647086fc5SJohannes Berg 				n_frames--;
137747086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
137847086fc5SJohannes Berg 			}
1379948d887dSJohannes Berg 		}
1380948d887dSJohannes Berg 
1381f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1382f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1383f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13844049e09aSJohannes Berg 		 */
1385948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1386948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1387948d887dSJohannes Berg 			more_data = true;
1388948d887dSJohannes Berg 			break;
1389948d887dSJohannes Berg 		}
1390948d887dSJohannes Berg 	}
1391af818581SJohannes Berg 
1392f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1393ce662b44SJohannes Berg 		int tid;
13944049e09aSJohannes Berg 
1395ce662b44SJohannes Berg 		/*
1396ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1397ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1398ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1399ce662b44SJohannes Berg 		 *
1400ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1401ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1402ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1403ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1404ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1405ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1406ce662b44SJohannes Berg 		 *
1407ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1408ce662b44SJohannes Berg 		 */
1409ce662b44SJohannes Berg 
1410ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1411ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1412ce662b44SJohannes Berg 
1413b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1414f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
141547086fc5SJohannes Berg 		struct sk_buff_head pending;
141647086fc5SJohannes Berg 		struct sk_buff *skb;
141740b96408SJohannes Berg 		int num = 0;
141840b96408SJohannes Berg 		u16 tids = 0;
1419b77cf4f8SJohannes Berg 		bool need_null = false;
142047086fc5SJohannes Berg 
142147086fc5SJohannes Berg 		skb_queue_head_init(&pending);
142247086fc5SJohannes Berg 
142347086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1424af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
142547086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
142640b96408SJohannes Berg 			u8 *qoshdr = NULL;
142740b96408SJohannes Berg 
142840b96408SJohannes Berg 			num++;
1429af818581SJohannes Berg 
1430af818581SJohannes Berg 			/*
143147086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
143247086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
143347086fc5SJohannes Berg 			 * exchange.
1434af818581SJohannes Berg 			 */
14356b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
14366b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1437af818581SJohannes Berg 
143847086fc5SJohannes Berg 			/*
143947086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
144047086fc5SJohannes Berg 			 * more buffered frames for this STA
144147086fc5SJohannes Berg 			 */
144224b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
144347086fc5SJohannes Berg 				hdr->frame_control |=
144447086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
144524b9c373SJanusz.Dziedzic@tieto.com 			else
144624b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
144724b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1448af818581SJohannes Berg 
144940b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
145040b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
145140b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
145240b96408SJohannes Berg 
1453b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1454b77cf4f8SJohannes Berg 
1455b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1456b77cf4f8SJohannes Berg 
1457b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1458b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1459b77cf4f8SJohannes Berg 				continue;
1460b77cf4f8SJohannes Berg 
1461b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1462b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1463b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1464b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1465b77cf4f8SJohannes Berg 				break;
1466b77cf4f8SJohannes Berg 			}
1467b77cf4f8SJohannes Berg 
1468b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1469b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1470b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1471b77cf4f8SJohannes Berg 			 * and be done.
1472b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1473b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1474b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1475b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1476b77cf4f8SJohannes Berg 			 *
1477b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1478b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1479b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1480b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1481b77cf4f8SJohannes Berg 			 *
1482b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1483b77cf4f8SJohannes Berg 			 */
1484b77cf4f8SJohannes Berg 			if (qoshdr) {
148540b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1486deeaee19SJohannes Berg 
148747086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
148847086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1489b77cf4f8SJohannes Berg 			} else {
1490b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1491b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1492b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1493b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1494b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1495b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1496b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1497b77cf4f8SJohannes Berg 				 */
1498b77cf4f8SJohannes Berg 				hdr->frame_control |=
1499b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1500b77cf4f8SJohannes Berg 				need_null = true;
1501b77cf4f8SJohannes Berg 				num++;
150252a3f20cSMarco Porsch 			}
1503b77cf4f8SJohannes Berg 			break;
150447086fc5SJohannes Berg 		}
150547086fc5SJohannes Berg 
150640b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
150740b96408SJohannes Berg 					  reason, more_data);
150840b96408SJohannes Berg 
150947086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1510af818581SJohannes Berg 
1511b77cf4f8SJohannes Berg 		if (need_null)
1512b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1513b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1514b77cf4f8SJohannes Berg 				reason, false);
1515b77cf4f8SJohannes Berg 
1516c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1517af818581SJohannes Berg 	} else {
1518ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1519ba8c3d6fSFelix Fietkau 		int tid;
1520ba8c3d6fSFelix Fietkau 
1521af818581SJohannes Berg 		/*
15224049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
15234049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1524f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1525f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1526f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1527f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1528f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1529f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1530af818581SJohannes Berg 		 */
15314049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
153247086fc5SJohannes Berg 					    n_frames, reason, more_data);
15334049e09aSJohannes Berg 
15344049e09aSJohannes Berg 		/*
15354049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
15364049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1537f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
15384049e09aSJohannes Berg 		 * release function.
1539f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1540ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1541ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
15424049e09aSJohannes Berg 		 */
1543ba8c3d6fSFelix Fietkau 
1544ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1545ba8c3d6fSFelix Fietkau 			return;
1546ba8c3d6fSFelix Fietkau 
1547ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1548ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1549ba8c3d6fSFelix Fietkau 
1550ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1551ba8c3d6fSFelix Fietkau 				continue;
1552ba8c3d6fSFelix Fietkau 
1553ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1554ba8c3d6fSFelix Fietkau 			break;
1555ba8c3d6fSFelix Fietkau 		}
1556af818581SJohannes Berg 	}
1557af818581SJohannes Berg }
1558af818581SJohannes Berg 
1559af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1560af818581SJohannes Berg {
156147086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1562af818581SJohannes Berg 
1563af818581SJohannes Berg 	/*
156447086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
156547086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
156647086fc5SJohannes Berg 	 * only from the non-enabled ones.
1567af818581SJohannes Berg 	 */
156847086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
156947086fc5SJohannes Berg 		ignore_for_response = 0;
1570af818581SJohannes Berg 
157147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
157247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1573af818581SJohannes Berg }
157447086fc5SJohannes Berg 
157547086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
157647086fc5SJohannes Berg {
157747086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
157847086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
157947086fc5SJohannes Berg 
158047086fc5SJohannes Berg 	/*
158147086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
158247086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
158347086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
158447086fc5SJohannes Berg 	 * actually getting called.
158547086fc5SJohannes Berg 	 */
158647086fc5SJohannes Berg 	if (!delivery_enabled)
158747086fc5SJohannes Berg 		return;
158847086fc5SJohannes Berg 
158947086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
159047086fc5SJohannes Berg 	case 1:
159147086fc5SJohannes Berg 		n_frames = 2;
159247086fc5SJohannes Berg 		break;
159347086fc5SJohannes Berg 	case 2:
159447086fc5SJohannes Berg 		n_frames = 4;
159547086fc5SJohannes Berg 		break;
159647086fc5SJohannes Berg 	case 3:
159747086fc5SJohannes Berg 		n_frames = 6;
159847086fc5SJohannes Berg 		break;
159947086fc5SJohannes Berg 	case 0:
160047086fc5SJohannes Berg 		/* XXX: what is a good value? */
160113a8098aSAndrei Otcheretianski 		n_frames = 128;
160247086fc5SJohannes Berg 		break;
160347086fc5SJohannes Berg 	}
160447086fc5SJohannes Berg 
160547086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
160647086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1607af818581SJohannes Berg }
1608af818581SJohannes Berg 
1609af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1610af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1611af818581SJohannes Berg {
1612af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1613af818581SJohannes Berg 
1614b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1615b5878a2dSJohannes Berg 
16165ac2e350SJohannes Berg 	if (block) {
1617c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
16185ac2e350SJohannes Berg 		return;
16195ac2e350SJohannes Berg 	}
16205ac2e350SJohannes Berg 
16215ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
16225ac2e350SJohannes Berg 		return;
16235ac2e350SJohannes Berg 
16245ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
16255ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
16265ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16275ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16285ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
16295ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
16305ac2e350SJohannes Berg 		/* must be asleep in this case */
16315ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16325ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16335ac2e350SJohannes Berg 	} else {
16345ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16355ac2e350SJohannes Berg 	}
1636af818581SJohannes Berg }
1637af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1638dcf55fb5SFelix Fietkau 
1639e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
164037fbd908SJohannes Berg {
164137fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
164237fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
164337fbd908SJohannes Berg 
164437fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
164537fbd908SJohannes Berg 
164637fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
164737fbd908SJohannes Berg }
1648e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
164937fbd908SJohannes Berg 
1650042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1651042ec453SJohannes Berg 				u8 tid, bool buffered)
1652dcf55fb5SFelix Fietkau {
1653dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1654dcf55fb5SFelix Fietkau 
16555a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1656042ec453SJohannes Berg 		return;
1657042ec453SJohannes Berg 
16581b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
16591b000789SJohannes Berg 
1660948d887dSJohannes Berg 	if (buffered)
1661948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1662948d887dSJohannes Berg 	else
1663948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1664948d887dSJohannes Berg 
1665c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1666dcf55fb5SFelix Fietkau }
1667042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1668d9a7ddb0SJohannes Berg 
166983d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1670d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1671d9a7ddb0SJohannes Berg {
16728bf11d8dSJohannes Berg 	might_sleep();
1673d9a7ddb0SJohannes Berg 
1674d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1675d9a7ddb0SJohannes Berg 		return 0;
1676d9a7ddb0SJohannes Berg 
1677f09603a2SJohannes Berg 	/* check allowed transitions first */
1678f09603a2SJohannes Berg 
1679d9a7ddb0SJohannes Berg 	switch (new_state) {
1680d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1681f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1682d9a7ddb0SJohannes Berg 			return -EINVAL;
1683d9a7ddb0SJohannes Berg 		break;
1684d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1685f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1686f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1687d9a7ddb0SJohannes Berg 			return -EINVAL;
1688d9a7ddb0SJohannes Berg 		break;
1689d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1690f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1691f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1692d9a7ddb0SJohannes Berg 			return -EINVAL;
1693d9a7ddb0SJohannes Berg 		break;
1694d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1695f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1696d9a7ddb0SJohannes Berg 			return -EINVAL;
1697d9a7ddb0SJohannes Berg 		break;
1698d9a7ddb0SJohannes Berg 	default:
1699d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1700d9a7ddb0SJohannes Berg 		return -EINVAL;
1701d9a7ddb0SJohannes Berg 	}
1702d9a7ddb0SJohannes Berg 
1703bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1704bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1705f09603a2SJohannes Berg 
1706f09603a2SJohannes Berg 	/*
1707f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1708f09603a2SJohannes Berg 	 * fail the transition
1709f09603a2SJohannes Berg 	 */
1710f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1711f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1712f09603a2SJohannes Berg 					sta->sta_state, new_state);
1713f09603a2SJohannes Berg 		if (err)
1714f09603a2SJohannes Berg 			return err;
1715f09603a2SJohannes Berg 	}
1716f09603a2SJohannes Berg 
1717f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1718f09603a2SJohannes Berg 
1719f09603a2SJohannes Berg 	switch (new_state) {
1720f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1721f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1722f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1723f09603a2SJohannes Berg 		break;
1724f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1725f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1726f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1727f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1728f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1729f09603a2SJohannes Berg 		break;
1730f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1731f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1732f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1733f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
17347e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17357e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17367e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17377e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1738f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1739f09603a2SJohannes Berg 		}
1740f09603a2SJohannes Berg 		break;
1741f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1742f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
17437e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17447e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17457e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17467e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1747f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1748f09603a2SJohannes Berg 		}
1749f09603a2SJohannes Berg 		break;
1750f09603a2SJohannes Berg 	default:
1751f09603a2SJohannes Berg 		break;
1752f09603a2SJohannes Berg 	}
1753f09603a2SJohannes Berg 
1754d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1755d9a7ddb0SJohannes Berg 
1756d9a7ddb0SJohannes Berg 	return 0;
1757d9a7ddb0SJohannes Berg }
1758687da132SEmmanuel Grumbach 
1759687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1760687da132SEmmanuel Grumbach {
1761687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1762687da132SEmmanuel Grumbach 	u8 rx_streams;
1763687da132SEmmanuel Grumbach 
1764687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1765687da132SEmmanuel Grumbach 		return 1;
1766687da132SEmmanuel Grumbach 
1767687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1768687da132SEmmanuel Grumbach 		int i;
1769687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1770687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1771687da132SEmmanuel Grumbach 
1772687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1773687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1774687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1775687da132SEmmanuel Grumbach 				return i + 1;
1776687da132SEmmanuel Grumbach 	}
1777687da132SEmmanuel Grumbach 
1778687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1779687da132SEmmanuel Grumbach 		rx_streams = 4;
1780687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1781687da132SEmmanuel Grumbach 		rx_streams = 3;
1782687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1783687da132SEmmanuel Grumbach 		rx_streams = 2;
1784687da132SEmmanuel Grumbach 	else
1785687da132SEmmanuel Grumbach 		rx_streams = 1;
1786687da132SEmmanuel Grumbach 
1787687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1788687da132SEmmanuel Grumbach 		return rx_streams;
1789687da132SEmmanuel Grumbach 
1790687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1791687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1792687da132SEmmanuel Grumbach }
1793b7ffbd7eSJohannes Berg 
1794b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1795b7ffbd7eSJohannes Berg {
1796b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1797b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
17989a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1799b7ffbd7eSJohannes Berg 	struct timespec uptime;
1800b7ffbd7eSJohannes Berg 	u32 thr = 0;
1801b7ffbd7eSJohannes Berg 	int i, ac;
1802b7ffbd7eSJohannes Berg 
18039a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
18049a244409SJohn W. Linville 		ref = local->rate_ctrl;
18059a244409SJohn W. Linville 
1806b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1807b7ffbd7eSJohannes Berg 
1808225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1809225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1810225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1811225b8189SJohannes Berg 	 */
1812225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1813225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1814225b8189SJohannes Berg 
18152b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
18162b9a7e1bSJohannes Berg 
1817319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1818319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1819319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1820319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1821319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1822319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BEACON_LOSS);
1823b7ffbd7eSJohannes Berg 
182418171520SThomas Gleixner 	ktime_get_ts(&uptime);
1825b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1826b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
18272b9a7e1bSJohannes Berg 
1828319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1829319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1830b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
18312b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1832b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1833319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1834b7ffbd7eSJohannes Berg 	}
18352b9a7e1bSJohannes Berg 
1836319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
18372b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
18382b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
18392b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1840319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
18412b9a7e1bSJohannes Berg 	}
18422b9a7e1bSJohannes Berg 
1843319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1844319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1845b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1846319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
18472b9a7e1bSJohannes Berg 	}
18482b9a7e1bSJohannes Berg 
1849319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1850b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1851319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
18522b9a7e1bSJohannes Berg 	}
18532b9a7e1bSJohannes Berg 
1854319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1855b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1856319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
18572b9a7e1bSJohannes Berg 	}
18582b9a7e1bSJohannes Berg 
1859319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1860b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1861319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
18622b9a7e1bSJohannes Berg 	}
18632b9a7e1bSJohannes Berg 
1864b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1865b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1866b7ffbd7eSJohannes Berg 
1867225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1868225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1869225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1870225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1871225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1872225b8189SJohannes Berg 	}
1873225b8189SJohannes Berg 
1874b7ffbd7eSJohannes Berg 	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1875b7ffbd7eSJohannes Berg 	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1876319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1877b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1878319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1879b7ffbd7eSJohannes Berg 		}
18802b9a7e1bSJohannes Berg 
1881319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
18822b9a7e1bSJohannes Berg 			sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1883319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
18842b9a7e1bSJohannes Berg 		}
18852b9a7e1bSJohannes Berg 	}
18862b9a7e1bSJohannes Berg 
18872b9a7e1bSJohannes Berg 	if (sta->chains &&
1888319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1889319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1890319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1891319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1892b7ffbd7eSJohannes Berg 
1893b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1894b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1895b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1896b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1897b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1898b7ffbd7eSJohannes Berg 		}
1899b7ffbd7eSJohannes Berg 	}
1900b7ffbd7eSJohannes Berg 
1901319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1902b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1903319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
19042b9a7e1bSJohannes Berg 	}
19052b9a7e1bSJohannes Berg 
1906319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1907b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1908319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
19092b9a7e1bSJohannes Berg 	}
1910b7ffbd7eSJohannes Berg 
191179c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
191279c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
191379c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
191479c892b8SJohannes Berg 
191579c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
191679c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
191779c892b8SJohannes Berg 			tidstats->rx_msdu = sta->rx_msdu[i];
191879c892b8SJohannes Berg 		}
191979c892b8SJohannes Berg 
192079c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
192179c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
192279c892b8SJohannes Berg 			tidstats->tx_msdu = sta->tx_msdu[i];
192379c892b8SJohannes Berg 		}
192479c892b8SJohannes Berg 
192579c892b8SJohannes Berg 		if (!(tidstats->filled &
192679c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
192779c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
192879c892b8SJohannes Berg 			tidstats->filled |=
192979c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
193079c892b8SJohannes Berg 			tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
193179c892b8SJohannes Berg 		}
193279c892b8SJohannes Berg 
193379c892b8SJohannes Berg 		if (!(tidstats->filled &
193479c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
193579c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
193679c892b8SJohannes Berg 			tidstats->filled |=
193779c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
193879c892b8SJohannes Berg 			tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
193979c892b8SJohannes Berg 		}
194079c892b8SJohannes Berg 	}
194179c892b8SJohannes Berg 
1942b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1943b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1944319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1945319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
1946319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
1947319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
1948319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
1949319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
1950b7ffbd7eSJohannes Berg 
1951b7ffbd7eSJohannes Berg 		sinfo->llid = sta->llid;
1952b7ffbd7eSJohannes Berg 		sinfo->plid = sta->plid;
1953b7ffbd7eSJohannes Berg 		sinfo->plink_state = sta->plink_state;
1954b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1955319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1956b7ffbd7eSJohannes Berg 			sinfo->t_offset = sta->t_offset;
1957b7ffbd7eSJohannes Berg 		}
1958b7ffbd7eSJohannes Berg 		sinfo->local_pm = sta->local_pm;
1959b7ffbd7eSJohannes Berg 		sinfo->peer_pm = sta->peer_pm;
1960b7ffbd7eSJohannes Berg 		sinfo->nonpeer_pm = sta->nonpeer_pm;
1961b7ffbd7eSJohannes Berg #endif
1962b7ffbd7eSJohannes Berg 	}
1963b7ffbd7eSJohannes Berg 
1964b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1965b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1966b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1967b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1968b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1969b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1970b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1971785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1972b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1973b7ffbd7eSJohannes Berg 
1974b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1975b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1976b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1977b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1978b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1979b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1980b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1981b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1982b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1983b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1984b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1985b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1986a74a8c84SJohannes Berg 	if (sta->sta.wme)
1987b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1988b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
1989b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1990b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
1991b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1992b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
1993b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1994b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1995b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1996b7ffbd7eSJohannes Berg 
1997b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
1998b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
1999b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2000b7ffbd7eSJohannes Berg 	else
2001b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
2002b7ffbd7eSJohannes Berg 
2003b7ffbd7eSJohannes Berg 	if (thr != 0) {
2004319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2005b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
2006b7ffbd7eSJohannes Berg 	}
2007b7ffbd7eSJohannes Berg }
2008