xref: /openbmc/linux/net/mac80211/sta_info.c (revision ba8c3d6f16a1f9305c23ac1d2fd3992508c5ac03)
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 */
697bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
707bedd0cfSJohannes Berg 	.key_offset = offsetof(struct sta_info, sta.addr),
717bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
727bedd0cfSJohannes Berg 	.hashfn = sta_addr_hash,
737bedd0cfSJohannes Berg };
747bedd0cfSJohannes Berg 
754d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
76be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
77f0706e82SJiri Benc 			     struct sta_info *sta)
78f0706e82SJiri Benc {
797bedd0cfSJohannes Berg 	return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
807bedd0cfSJohannes Berg 				      sta_rht_params);
81f0706e82SJiri Benc }
82f0706e82SJiri Benc 
835108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
84b22cfcfcSEliad Peller {
85b22cfcfcSEliad Peller 	int ac, i;
86b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
87b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
88b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
89d012a605SMarco Porsch 	struct ps_data *ps;
90b22cfcfcSEliad Peller 
91e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
925ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
935ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
94d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
95d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
96d012a605SMarco Porsch 			ps = &sdata->bss->ps;
973f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
983f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
99d012a605SMarco Porsch 		else
100d012a605SMarco Porsch 			return;
101b22cfcfcSEliad Peller 
102b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
103e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1045ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
105b22cfcfcSEliad Peller 
106d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
107b22cfcfcSEliad Peller 	}
108b22cfcfcSEliad Peller 
109*ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
110*ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
111*ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
112*ba8c3d6fSFelix Fietkau 			int n = skb_queue_len(&txqi->queue);
113*ba8c3d6fSFelix Fietkau 
114*ba8c3d6fSFelix Fietkau 			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
115*ba8c3d6fSFelix Fietkau 			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
116*ba8c3d6fSFelix Fietkau 		}
117*ba8c3d6fSFelix Fietkau 	}
118*ba8c3d6fSFelix Fietkau 
119b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
120b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1211f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1221f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
123b22cfcfcSEliad Peller 	}
124b22cfcfcSEliad Peller 
12545b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12645b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
127b22cfcfcSEliad Peller 
1285ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
129b22cfcfcSEliad Peller 
130b22cfcfcSEliad Peller 	/*
131b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
132b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
133b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
134b22cfcfcSEliad Peller 	 * directly by station destruction.
135b22cfcfcSEliad Peller 	 */
1365a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
137661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
138b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
139b22cfcfcSEliad Peller 		if (!tid_tx)
140b22cfcfcSEliad Peller 			continue;
1411f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
142b22cfcfcSEliad Peller 		kfree(tid_tx);
143b22cfcfcSEliad Peller 	}
1445108ca82SJohannes Berg }
145b22cfcfcSEliad Peller 
1465108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1475108ca82SJohannes Berg {
1485108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1495108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1505108ca82SJohannes Berg 
1515108ca82SJohannes Berg 	__cleanup_single_sta(sta);
152b22cfcfcSEliad Peller 	sta_info_free(local, sta);
153b22cfcfcSEliad Peller }
154b22cfcfcSEliad Peller 
155d0709a65SJohannes Berg /* protected by RCU */
156abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
157abe60632SJohannes Berg 			      const u8 *addr)
15843ba7e95SJohannes Berg {
159abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16043ba7e95SJohannes Berg 
1617bedd0cfSJohannes Berg 	return rhashtable_lookup_fast(&local->sta_hash, addr, sta_rht_params);
16243ba7e95SJohannes Berg }
16343ba7e95SJohannes Berg 
1640e5ded5aSFelix Fietkau /*
1650e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1660e5ded5aSFelix Fietkau  * or from one of its vlans
1670e5ded5aSFelix Fietkau  */
1680e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1690e5ded5aSFelix Fietkau 				  const u8 *addr)
1700e5ded5aSFelix Fietkau {
1710e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1720e5ded5aSFelix Fietkau 	struct sta_info *sta;
1737bedd0cfSJohannes Berg 	struct rhash_head *tmp;
1747bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
1750e5ded5aSFelix Fietkau 
1767bedd0cfSJohannes Berg 	rcu_read_lock();
1777bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1787bedd0cfSJohannes Berg 
1797bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1807bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
1817bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
1827bedd0cfSJohannes Berg 			rcu_read_unlock();
1837bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
1847bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
1857bedd0cfSJohannes Berg 			 */
1860e5ded5aSFelix Fietkau 			return sta;
1870e5ded5aSFelix Fietkau 		}
1887bedd0cfSJohannes Berg 	}
1897bedd0cfSJohannes Berg 	rcu_read_unlock();
1907bedd0cfSJohannes Berg 	return NULL;
1917bedd0cfSJohannes Berg }
1920e5ded5aSFelix Fietkau 
1933b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1943b53fde8SJohannes Berg 				     int idx)
195ee385855SLuis Carlos Cobo {
1963b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
197ee385855SLuis Carlos Cobo 	struct sta_info *sta;
198ee385855SLuis Carlos Cobo 	int i = 0;
199ee385855SLuis Carlos Cobo 
200d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2013b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2022a8ca29aSLuis Carlos Cobo 			continue;
203ee385855SLuis Carlos Cobo 		if (i < idx) {
204ee385855SLuis Carlos Cobo 			++i;
205ee385855SLuis Carlos Cobo 			continue;
206ee385855SLuis Carlos Cobo 		}
2072a8ca29aSLuis Carlos Cobo 		return sta;
208ee385855SLuis Carlos Cobo 	}
209ee385855SLuis Carlos Cobo 
210ee385855SLuis Carlos Cobo 	return NULL;
211ee385855SLuis Carlos Cobo }
212f0706e82SJiri Benc 
21393e5deb1SJohannes Berg /**
214d9a7ddb0SJohannes Berg  * sta_info_free - free STA
21593e5deb1SJohannes Berg  *
2166ef307bcSRandy Dunlap  * @local: pointer to the global information
21793e5deb1SJohannes Berg  * @sta: STA info to free
21893e5deb1SJohannes Berg  *
21993e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
220d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
221d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
222d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
22393e5deb1SJohannes Berg  */
224d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
22593e5deb1SJohannes Berg {
226889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2274b7679a5SJohannes Berg 		rate_control_free_sta(sta);
22893e5deb1SJohannes Berg 
229bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
23093e5deb1SJohannes Berg 
231*ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
232*ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
23353d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
23493e5deb1SJohannes Berg 	kfree(sta);
23593e5deb1SJohannes Berg }
23693e5deb1SJohannes Berg 
2374d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
238d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
239d0709a65SJohannes Berg 			      struct sta_info *sta)
240f0706e82SJiri Benc {
2417bedd0cfSJohannes Berg 	rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
2427bedd0cfSJohannes Berg 			       sta_rht_params);
243f0706e82SJiri Benc }
244f0706e82SJiri Benc 
2455ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
246af818581SJohannes Berg {
247af818581SJohannes Berg 	struct sta_info *sta;
248af818581SJohannes Berg 
2495ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
250af818581SJohannes Berg 
251af818581SJohannes Berg 	if (sta->dead)
252af818581SJohannes Berg 		return;
253af818581SJohannes Berg 
25454420473SHelmut Schaa 	local_bh_disable();
2555ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
256af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2575ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
258af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2595ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
26047086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
261ce662b44SJohannes Berg 	local_bh_enable();
262af818581SJohannes Berg }
263af818581SJohannes Berg 
264af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
265af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
266af65cd96SJohannes Berg {
267af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
268af65cd96SJohannes Berg 		return 0;
269af65cd96SJohannes Berg 
270889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
271af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
272af65cd96SJohannes Berg 						     &sta->sta, gfp);
273889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
274af65cd96SJohannes Berg 		return -ENOMEM;
275af65cd96SJohannes Berg 
276af65cd96SJohannes Berg 	return 0;
277af65cd96SJohannes Berg }
278af65cd96SJohannes Berg 
27973651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
28056544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
281f0706e82SJiri Benc {
282d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
283*ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
284f0706e82SJiri Benc 	struct sta_info *sta;
285ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
28616c5f15cSRon Rindjunsky 	int i;
287f0706e82SJiri Benc 
288*ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
289f0706e82SJiri Benc 	if (!sta)
29073651ee6SJohannes Berg 		return NULL;
291f0706e82SJiri Benc 
29207346f81SJohannes Berg 	spin_lock_init(&sta->lock);
2931d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
2945ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
29567c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
296a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
29787f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
29887f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
29987f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
30087f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3016c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
30287f59c70SThomas Pedersen #endif
30307346f81SJohannes Berg 
30417741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
305d0709a65SJohannes Berg 	sta->local = local;
306d0709a65SJohannes Berg 	sta->sdata = sdata;
3078bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
308f0706e82SJiri Benc 
30971ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
31071ec375cSJohannes Berg 
311b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
312b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
313b6da911bSLiad Kaufman 
31418171520SThomas Gleixner 	ktime_get_ts(&uptime);
315ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
316541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
317ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
318ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
319541a45a1SBruno Randolf 
320*ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
321*ba8c3d6fSFelix Fietkau 		void *txq_data;
322*ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
323*ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
324*ba8c3d6fSFelix Fietkau 
325*ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
326*ba8c3d6fSFelix Fietkau 		if (!txq_data)
327*ba8c3d6fSFelix Fietkau 			goto free;
328*ba8c3d6fSFelix Fietkau 
329*ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
330*ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
331*ba8c3d6fSFelix Fietkau 
332*ba8c3d6fSFelix Fietkau 			ieee80211_init_tx_queue(sdata, sta, txq, i);
333abfbc3afSJohannes Berg 		}
334*ba8c3d6fSFelix Fietkau 	}
335*ba8c3d6fSFelix Fietkau 
336*ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
337*ba8c3d6fSFelix Fietkau 		goto free_txq;
338f0706e82SJiri Benc 
3395a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
340a622ab72SJohannes Berg 		/*
341a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
342a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
343a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
344a622ab72SJohannes Berg 		 */
34516c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
34616c5f15cSRon Rindjunsky 	}
347948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
348948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
349948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
350948d887dSJohannes Berg 	}
35173651ee6SJohannes Berg 
3525a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3534be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
354cccaec98SSenthil Balasubramanian 
355af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
356687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
357687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
358687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
359*ba8c3d6fSFelix Fietkau 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
360687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
361687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
362687da132SEmmanuel Grumbach 		/*
363687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
364687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
365687da132SEmmanuel Grumbach 		 */
366687da132SEmmanuel Grumbach 		switch (smps) {
367687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
368687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
369687da132SEmmanuel Grumbach 			break;
370687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
371687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
372687da132SEmmanuel Grumbach 			break;
373687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
374687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
375687da132SEmmanuel Grumbach 			break;
376687da132SEmmanuel Grumbach 		default:
377687da132SEmmanuel Grumbach 			WARN_ON(1);
378687da132SEmmanuel Grumbach 		}
379687da132SEmmanuel Grumbach 	}
380af0ed69bSJohannes Berg 
381bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
382ef04a297SJohannes Berg 
383abfbc3afSJohannes Berg 	return sta;
384*ba8c3d6fSFelix Fietkau 
385*ba8c3d6fSFelix Fietkau free_txq:
386*ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
387*ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
388*ba8c3d6fSFelix Fietkau free:
389*ba8c3d6fSFelix Fietkau 	kfree(sta);
390*ba8c3d6fSFelix Fietkau 	return NULL;
39173651ee6SJohannes Berg }
39273651ee6SJohannes Berg 
3938c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
39434e89507SJohannes Berg {
39534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
39634e89507SJohannes Berg 
39703e4497eSJohannes Berg 	/*
39803e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
39903e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
40003e4497eSJohannes Berg 	 * and another CPU turns off the net device.
40103e4497eSJohannes Berg 	 */
4028c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4038c71df7aSGuy Eilam 		return -ENETDOWN;
40403e4497eSJohannes Berg 
405b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4068c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4078c71df7aSGuy Eilam 		return -EINVAL;
4088c71df7aSGuy Eilam 
4098c71df7aSGuy Eilam 	return 0;
41093e5deb1SJohannes Berg }
41144213b5eSJohannes Berg 
412f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
413f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
414f09603a2SJohannes Berg 				     struct sta_info *sta)
415f09603a2SJohannes Berg {
416f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
417f09603a2SJohannes Berg 	int err = 0;
418f09603a2SJohannes Berg 
419f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
420f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
421f09603a2SJohannes Berg 		if (err)
422f09603a2SJohannes Berg 			break;
423f09603a2SJohannes Berg 	}
424f09603a2SJohannes Berg 
425f09603a2SJohannes Berg 	if (!err) {
426a4ec45a4SJohannes Berg 		/*
427a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
428a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
429a4ec45a4SJohannes Berg 		 */
430a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
431f09603a2SJohannes Berg 			sta->uploaded = true;
432f09603a2SJohannes Berg 		return 0;
433f09603a2SJohannes Berg 	}
434f09603a2SJohannes Berg 
435f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
436bdcbd8e0SJohannes Berg 		sdata_info(sdata,
437bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
438bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
439f09603a2SJohannes Berg 		err = 0;
440f09603a2SJohannes Berg 	}
441f09603a2SJohannes Berg 
442f09603a2SJohannes Berg 	/* unwind on error */
443f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
444f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
445f09603a2SJohannes Berg 
446f09603a2SJohannes Berg 	return err;
447f09603a2SJohannes Berg }
448f09603a2SJohannes Berg 
44934e89507SJohannes Berg /*
4508c71df7aSGuy Eilam  * should be called with sta_mtx locked
4518c71df7aSGuy Eilam  * this function replaces the mutex lock
4528c71df7aSGuy Eilam  * with a RCU lock
4538c71df7aSGuy Eilam  */
4544d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4558c71df7aSGuy Eilam {
4568c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4578c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4587852e361SJohannes Berg 	struct station_info sinfo;
4598c71df7aSGuy Eilam 	int err = 0;
4608c71df7aSGuy Eilam 
4618c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4628c71df7aSGuy Eilam 
4637852e361SJohannes Berg 	/* check if STA exists already */
4647852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4654d33960bSJohannes Berg 		err = -EEXIST;
4664d33960bSJohannes Berg 		goto out_err;
46734e89507SJohannes Berg 	}
46834e89507SJohannes Berg 
4694d33960bSJohannes Berg 	local->num_sta++;
4704d33960bSJohannes Berg 	local->sta_generation++;
4714d33960bSJohannes Berg 	smp_mb();
4724d33960bSJohannes Berg 
4735108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
4745108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4755108ca82SJohannes Berg 
4764d33960bSJohannes Berg 	/* make the station visible */
4774d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
4784d33960bSJohannes Berg 
4792bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
48083d5cc01SJohannes Berg 
4815108ca82SJohannes Berg 	/* notify driver */
4825108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
4835108ca82SJohannes Berg 	if (err)
4845108ca82SJohannes Berg 		goto out_remove;
4855108ca82SJohannes Berg 
48683d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
4875108ca82SJohannes Berg 	/* accept BA sessions now */
4885108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4894d33960bSJohannes Berg 
49021f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
4914d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
4924d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
4934d33960bSJohannes Berg 
4944d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
4954d33960bSJohannes Berg 	sinfo.filled = 0;
4964d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
4974d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
498d0709a65SJohannes Berg 
499bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
500f0706e82SJiri Benc 
50134e89507SJohannes Berg 	/* move reference to rcu-protected */
50234e89507SJohannes Berg 	rcu_read_lock();
50334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
504e9f207f0SJiri Benc 
50573651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
50673651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
50773651ee6SJohannes Berg 
50873651ee6SJohannes Berg 	return 0;
5095108ca82SJohannes Berg  out_remove:
5105108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5115108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5125108ca82SJohannes Berg 	local->num_sta--;
5135108ca82SJohannes Berg 	synchronize_net();
5145108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5154d33960bSJohannes Berg  out_err:
5164d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5174d33960bSJohannes Berg 	rcu_read_lock();
5184d33960bSJohannes Berg 	return err;
5198c71df7aSGuy Eilam }
5208c71df7aSGuy Eilam 
5218c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5228c71df7aSGuy Eilam {
5238c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
524308f7fcfSZhao, Gang 	int err;
5258c71df7aSGuy Eilam 
5264d33960bSJohannes Berg 	might_sleep();
5274d33960bSJohannes Berg 
5288c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5298c71df7aSGuy Eilam 	if (err) {
5308c71df7aSGuy Eilam 		rcu_read_lock();
5318c71df7aSGuy Eilam 		goto out_free;
5328c71df7aSGuy Eilam 	}
5338c71df7aSGuy Eilam 
534004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
535004c872eSJohannes Berg 
5364d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5378c71df7aSGuy Eilam 	if (err)
538004c872eSJohannes Berg 		goto out_free;
539d0709a65SJohannes Berg 
540004c872eSJohannes Berg 	return 0;
54193e5deb1SJohannes Berg  out_free:
542d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
54393e5deb1SJohannes Berg 	return err;
544f0706e82SJiri Benc }
545f0706e82SJiri Benc 
54634e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
54734e89507SJohannes Berg {
54834e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
54934e89507SJohannes Berg 
55034e89507SJohannes Berg 	rcu_read_unlock();
55134e89507SJohannes Berg 
55234e89507SJohannes Berg 	return err;
55334e89507SJohannes Berg }
55434e89507SJohannes Berg 
555d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
556004c872eSJohannes Berg {
557004c872eSJohannes Berg 	/*
558004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
559004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
560004c872eSJohannes Berg 	 */
561d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
562004c872eSJohannes Berg }
563004c872eSJohannes Berg 
564d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
565004c872eSJohannes Berg {
566004c872eSJohannes Berg 	/*
567004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
568004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
569004c872eSJohannes Berg 	 */
570d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
571004c872eSJohannes Berg }
572004c872eSJohannes Berg 
5733d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
5743d5839b6SIlan Peer {
5753d5839b6SIlan Peer 	/*
5763d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
5773d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
5783d5839b6SIlan Peer 	 */
5793d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
5803d5839b6SIlan Peer }
5813d5839b6SIlan Peer 
582948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
583004c872eSJohannes Berg {
584948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
585948d887dSJohannes Berg 	switch (ac) {
586948d887dSJohannes Berg 	case IEEE80211_AC_VO:
587948d887dSJohannes Berg 		return BIT(6) | BIT(7);
588948d887dSJohannes Berg 	case IEEE80211_AC_VI:
589948d887dSJohannes Berg 		return BIT(4) | BIT(5);
590948d887dSJohannes Berg 	case IEEE80211_AC_BE:
591948d887dSJohannes Berg 		return BIT(0) | BIT(3);
592948d887dSJohannes Berg 	case IEEE80211_AC_BK:
593948d887dSJohannes Berg 		return BIT(1) | BIT(2);
594948d887dSJohannes Berg 	default:
595948d887dSJohannes Berg 		WARN_ON(1);
596948d887dSJohannes Berg 		return 0;
597d0709a65SJohannes Berg 	}
598004c872eSJohannes Berg }
599004c872eSJohannes Berg 
6009b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
601004c872eSJohannes Berg {
602c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
603d012a605SMarco Porsch 	struct ps_data *ps;
604948d887dSJohannes Berg 	bool indicate_tim = false;
605948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
606948d887dSJohannes Berg 	int ac;
607d012a605SMarco Porsch 	u16 id;
608004c872eSJohannes Berg 
609d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
610d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
611c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
612c868cb35SJohannes Berg 			return;
6133e122be0SJohannes Berg 
614d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
615d012a605SMarco Porsch 		id = sta->sta.aid;
6163f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6173f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6183f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
619204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6206f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6213f52b7e3SMarco Porsch #endif
622d012a605SMarco Porsch 	} else {
623d012a605SMarco Porsch 		return;
624d012a605SMarco Porsch 	}
625d012a605SMarco Porsch 
626c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
627c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
628c868cb35SJohannes Berg 		return;
629004c872eSJohannes Berg 
630c868cb35SJohannes Berg 	if (sta->dead)
631c868cb35SJohannes Berg 		goto done;
6323e122be0SJohannes Berg 
633948d887dSJohannes Berg 	/*
634948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
635948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
636948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
637948d887dSJohannes Berg 	 * non-enabled ones.
638948d887dSJohannes Berg 	 */
639948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
640948d887dSJohannes Berg 		ignore_for_tim = 0;
641948d887dSJohannes Berg 
6429b7a86f3SJohannes Berg 	if (ignore_pending)
6439b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6449b7a86f3SJohannes Berg 
645948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
646948d887dSJohannes Berg 		unsigned long tids;
647948d887dSJohannes Berg 
648948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
649948d887dSJohannes Berg 			continue;
650948d887dSJohannes Berg 
651948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
652948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
653948d887dSJohannes Berg 		if (indicate_tim)
654948d887dSJohannes Berg 			break;
655948d887dSJohannes Berg 
656948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
657948d887dSJohannes Berg 
658948d887dSJohannes Berg 		indicate_tim |=
659948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
660*ba8c3d6fSFelix Fietkau 		indicate_tim |=
661*ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
662004c872eSJohannes Berg 	}
663004c872eSJohannes Berg 
664c868cb35SJohannes Berg  done:
66565f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
666004c872eSJohannes Berg 
6673d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6683d5839b6SIlan Peer 		goto out_unlock;
6693d5839b6SIlan Peer 
670948d887dSJohannes Berg 	if (indicate_tim)
671d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
672c868cb35SJohannes Berg 	else
673d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6743e122be0SJohannes Berg 
6759b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
676c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
677948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
678c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
679004c872eSJohannes Berg 	}
680004c872eSJohannes Berg 
6813d5839b6SIlan Peer out_unlock:
68265f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
683004c872eSJohannes Berg }
684004c872eSJohannes Berg 
6859b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
6869b7a86f3SJohannes Berg {
6879b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
6889b7a86f3SJohannes Berg }
6899b7a86f3SJohannes Berg 
690cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
691f0706e82SJiri Benc {
692e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
693f0706e82SJiri Benc 	int timeout;
694f0706e82SJiri Benc 
695f0706e82SJiri Benc 	if (!skb)
696cd0b8d89SJohannes Berg 		return false;
697f0706e82SJiri Benc 
698e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
699f0706e82SJiri Benc 
700f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
70157c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
70257c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
70357c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
704f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
705f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
706e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
707f0706e82SJiri Benc }
708f0706e82SJiri Benc 
709f0706e82SJiri Benc 
710948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
711948d887dSJohannes Berg 						struct sta_info *sta, int ac)
712f0706e82SJiri Benc {
713f0706e82SJiri Benc 	unsigned long flags;
714f0706e82SJiri Benc 	struct sk_buff *skb;
715f0706e82SJiri Benc 
71660750397SJohannes Berg 	/*
71760750397SJohannes Berg 	 * First check for frames that should expire on the filtered
71860750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
71960750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
72060750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
72160750397SJohannes Berg 	 * total_ps_buffered counter.
72260750397SJohannes Berg 	 */
723f0706e82SJiri Benc 	for (;;) {
724948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
725948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
72657c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
727948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
728836341a7SJohannes Berg 		else
729f0706e82SJiri Benc 			skb = NULL;
730948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
731f0706e82SJiri Benc 
73260750397SJohannes Berg 		/*
73360750397SJohannes Berg 		 * Frames are queued in order, so if this one
73460750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
73560750397SJohannes Berg 		 * we actually reached the end of the queue we
73660750397SJohannes Berg 		 * also need to stop, of course.
73760750397SJohannes Berg 		 */
73860750397SJohannes Berg 		if (!skb)
73960750397SJohannes Berg 			break;
740d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
74160750397SJohannes Berg 	}
74260750397SJohannes Berg 
74360750397SJohannes Berg 	/*
74460750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
74560750397SJohannes Berg 	 * only find something if the filtered queue was emptied
74660750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
74760750397SJohannes Berg 	 * buffered frames.
74860750397SJohannes Berg 	 */
749f0706e82SJiri Benc 	for (;;) {
750948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
751948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
752f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
753948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
754f0706e82SJiri Benc 		else
755f0706e82SJiri Benc 			skb = NULL;
756948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
757f0706e82SJiri Benc 
75860750397SJohannes Berg 		/*
75960750397SJohannes Berg 		 * frames are queued in order, so if this one
76060750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
76160750397SJohannes Berg 		 * the queue) we can stop testing
76260750397SJohannes Berg 		 */
763836341a7SJohannes Berg 		if (!skb)
764836341a7SJohannes Berg 			break;
765836341a7SJohannes Berg 
766f0706e82SJiri Benc 		local->total_ps_buffered--;
767bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
768bdcbd8e0SJohannes Berg 		       sta->sta.addr);
769d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
770f0706e82SJiri Benc 	}
7713393a608SJuuso Oikarinen 
77260750397SJohannes Berg 	/*
77360750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
77460750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
77560750397SJohannes Berg 	 * frames.
77660750397SJohannes Berg 	 */
77760750397SJohannes Berg 	sta_info_recalc_tim(sta);
77860750397SJohannes Berg 
77960750397SJohannes Berg 	/*
78060750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
78160750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
78260750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
78360750397SJohannes Berg 	 */
784948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
785948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
786948d887dSJohannes Berg }
787948d887dSJohannes Berg 
788948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
789948d887dSJohannes Berg 					     struct sta_info *sta)
790948d887dSJohannes Berg {
791948d887dSJohannes Berg 	bool have_buffered = false;
792948d887dSJohannes Berg 	int ac;
793948d887dSJohannes Berg 
7943f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
7953f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
7963f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
797948d887dSJohannes Berg 		return false;
798948d887dSJohannes Berg 
799948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
800948d887dSJohannes Berg 		have_buffered |=
801948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
802948d887dSJohannes Berg 
803948d887dSJohannes Berg 	return have_buffered;
804f0706e82SJiri Benc }
805f0706e82SJiri Benc 
806d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
80734e89507SJohannes Berg {
80834e89507SJohannes Berg 	struct ieee80211_local *local;
80934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8106d10e46bSJohannes Berg 	int ret;
81134e89507SJohannes Berg 
81234e89507SJohannes Berg 	might_sleep();
81334e89507SJohannes Berg 
81434e89507SJohannes Berg 	if (!sta)
81534e89507SJohannes Berg 		return -ENOENT;
81634e89507SJohannes Berg 
81734e89507SJohannes Berg 	local = sta->local;
81834e89507SJohannes Berg 	sdata = sta->sdata;
81934e89507SJohannes Berg 
82083d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
82183d5cc01SJohannes Berg 
822098a6070SJohannes Berg 	/*
823098a6070SJohannes Berg 	 * Before removing the station from the driver and
824098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
825098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
826098a6070SJohannes Berg 	 * will be sufficient.
827098a6070SJohannes Berg 	 */
828c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
829c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
830098a6070SJohannes Berg 
83134e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
832b01711beSJohannes Berg 	if (WARN_ON(ret))
83334e89507SJohannes Berg 		return ret;
83434e89507SJohannes Berg 
835a7a6bdd0SArik Nemtsov 	/*
836a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
837a7a6bdd0SArik Nemtsov 	 * removal.
838a7a6bdd0SArik Nemtsov 	 */
839a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
840a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
841a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
842a7a6bdd0SArik Nemtsov 	}
843a7a6bdd0SArik Nemtsov 
844794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8454d33960bSJohannes Berg 
8466a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8476a9d1b91SJohannes Berg 
848a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
849a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
850a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
851a710c816SJohannes Berg 
852d778207bSJohannes Berg 	return 0;
853d778207bSJohannes Berg }
854d778207bSJohannes Berg 
855d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
856d778207bSJohannes Berg {
857d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
858d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8596f7a8d26SJohannes Berg 	struct station_info sinfo = {};
860d778207bSJohannes Berg 	int ret;
861d778207bSJohannes Berg 
862d778207bSJohannes Berg 	/*
863d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
864d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
865d778207bSJohannes Berg 	 */
866d778207bSJohannes Berg 
867d778207bSJohannes Berg 	might_sleep();
868d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
869d778207bSJohannes Berg 
870c8782078SJohannes Berg 	/* now keys can no longer be reached */
8716d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
87234e89507SJohannes Berg 
8739b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
8749b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
8759b7a86f3SJohannes Berg 
87634e89507SJohannes Berg 	sta->dead = true;
87734e89507SJohannes Berg 
87834e89507SJohannes Berg 	local->num_sta--;
87934e89507SJohannes Berg 	local->sta_generation++;
88034e89507SJohannes Berg 
88183d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
882f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
883f09603a2SJohannes Berg 		if (ret) {
88483d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
88583d5cc01SJohannes Berg 			break;
88683d5cc01SJohannes Berg 		}
88783d5cc01SJohannes Berg 	}
888d9a7ddb0SJohannes Berg 
889f09603a2SJohannes Berg 	if (sta->uploaded) {
890f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
891f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
892f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
893f09603a2SJohannes Berg 	}
89434e89507SJohannes Berg 
895bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
896bdcbd8e0SJohannes Berg 
8976f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
8986f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
899ec15e68bSJouni Malinen 
90034e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
90134e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
90221f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
90334e89507SJohannes Berg 
904d34ba216SJohannes Berg 	cleanup_single_sta(sta);
905d778207bSJohannes Berg }
906d778207bSJohannes Berg 
907d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
908d778207bSJohannes Berg {
909d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
910d778207bSJohannes Berg 
911d778207bSJohannes Berg 	if (err)
912d778207bSJohannes Berg 		return err;
913d778207bSJohannes Berg 
914d778207bSJohannes Berg 	synchronize_net();
915d778207bSJohannes Berg 
916d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
91734e89507SJohannes Berg 
91834e89507SJohannes Berg 	return 0;
91934e89507SJohannes Berg }
92034e89507SJohannes Berg 
92134e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
92234e89507SJohannes Berg {
92334e89507SJohannes Berg 	struct sta_info *sta;
92434e89507SJohannes Berg 	int ret;
92534e89507SJohannes Berg 
92634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9277852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
92834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
92934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
93034e89507SJohannes Berg 
93134e89507SJohannes Berg 	return ret;
93234e89507SJohannes Berg }
93334e89507SJohannes Berg 
93434e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
93534e89507SJohannes Berg 			      const u8 *addr)
93634e89507SJohannes Berg {
93734e89507SJohannes Berg 	struct sta_info *sta;
93834e89507SJohannes Berg 	int ret;
93934e89507SJohannes Berg 
94034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9417852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
94234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
94334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
94434e89507SJohannes Berg 
94534e89507SJohannes Berg 	return ret;
94634e89507SJohannes Berg }
947f0706e82SJiri Benc 
948f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
949f0706e82SJiri Benc {
950f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
951f0706e82SJiri Benc 	struct sta_info *sta;
9523393a608SJuuso Oikarinen 	bool timer_needed = false;
953f0706e82SJiri Benc 
954d0709a65SJohannes Berg 	rcu_read_lock();
955d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9563393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9573393a608SJuuso Oikarinen 			timer_needed = true;
958d0709a65SJohannes Berg 	rcu_read_unlock();
959f0706e82SJiri Benc 
9605bb644a0SJohannes Berg 	if (local->quiescing)
9615bb644a0SJohannes Berg 		return;
9625bb644a0SJohannes Berg 
9633393a608SJuuso Oikarinen 	if (!timer_needed)
9643393a608SJuuso Oikarinen 		return;
9653393a608SJuuso Oikarinen 
96626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
96726d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
968f0706e82SJiri Benc }
969f0706e82SJiri Benc 
9707bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
971f0706e82SJiri Benc {
9727bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
9737bedd0cfSJohannes Berg }
9747bedd0cfSJohannes Berg 
9757bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
9767bedd0cfSJohannes Berg {
9777bedd0cfSJohannes Berg 	int err;
9787bedd0cfSJohannes Berg 
9797bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
9807bedd0cfSJohannes Berg 	if (err)
9817bedd0cfSJohannes Berg 		return err;
9827bedd0cfSJohannes Berg 
9834d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
98434e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
985f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
986f0706e82SJiri Benc 
987b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
988b24b8a24SPavel Emelyanov 		    (unsigned long)local);
9897bedd0cfSJohannes Berg 	return 0;
990f0706e82SJiri Benc }
991f0706e82SJiri Benc 
992f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
993f0706e82SJiri Benc {
994a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
9957bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
996f0706e82SJiri Benc }
997f0706e82SJiri Benc 
998051007d9SJohannes Berg 
999e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1000f0706e82SJiri Benc {
1001b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1002f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1003d778207bSJohannes Berg 	LIST_HEAD(free_list);
100444213b5eSJohannes Berg 	int ret = 0;
1005f0706e82SJiri Benc 
1006d0709a65SJohannes Berg 	might_sleep();
1007d0709a65SJohannes Berg 
1008e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1009e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1010e716251dSJohannes Berg 
101134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
101234e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1013e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1014e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1015d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1016d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
101734316837SJohannes Berg 			ret++;
101834316837SJohannes Berg 		}
101934e89507SJohannes Berg 	}
1020d778207bSJohannes Berg 
1021d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1022d778207bSJohannes Berg 		synchronize_net();
1023d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1024d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1025d778207bSJohannes Berg 	}
102634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
102744213b5eSJohannes Berg 
1028051007d9SJohannes Berg 	return ret;
1029051007d9SJohannes Berg }
1030051007d9SJohannes Berg 
103124723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
103224723d1bSJohannes Berg 			  unsigned long exp_time)
103324723d1bSJohannes Berg {
103424723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
103524723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
103624723d1bSJohannes Berg 
103734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1038e46a2cf9SMohammed Shafi Shajakhan 
1039e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1040ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1041ec2b774eSMarek Lindner 			continue;
1042ec2b774eSMarek Lindner 
104324723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1044eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1045bdcbd8e0SJohannes Berg 				sta->sta.addr);
10463f52b7e3SMarco Porsch 
10473f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10483f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10493f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10503f52b7e3SMarco Porsch 
105134e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
105224723d1bSJohannes Berg 		}
1053e46a2cf9SMohammed Shafi Shajakhan 	}
1054e46a2cf9SMohammed Shafi Shajakhan 
105534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
105624723d1bSJohannes Berg }
105717741cdcSJohannes Berg 
1058686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1059686b9cb9SBen Greear 						   const u8 *addr,
1060686b9cb9SBen Greear 						   const u8 *localaddr)
106117741cdcSJohannes Berg {
10627bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
10637bedd0cfSJohannes Berg 	struct sta_info *sta;
10647bedd0cfSJohannes Berg 	struct rhash_head *tmp;
10657bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
10667bedd0cfSJohannes Berg 
10677bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
106817741cdcSJohannes Berg 
1069686b9cb9SBen Greear 	/*
1070686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1071686b9cb9SBen Greear 	 * ... first in list.
1072686b9cb9SBen Greear 	 */
10737bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1074686b9cb9SBen Greear 		if (localaddr &&
1075b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1076686b9cb9SBen Greear 			continue;
1077f7c65594SJohannes Berg 		if (!sta->uploaded)
1078f7c65594SJohannes Berg 			return NULL;
107917741cdcSJohannes Berg 		return &sta->sta;
1080f7c65594SJohannes Berg 	}
1081f7c65594SJohannes Berg 
1082abe60632SJohannes Berg 	return NULL;
108317741cdcSJohannes Berg }
1084686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10855ed176e1SJohannes Berg 
10865ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10875ed176e1SJohannes Berg 					 const u8 *addr)
10885ed176e1SJohannes Berg {
1089f7c65594SJohannes Berg 	struct sta_info *sta;
10905ed176e1SJohannes Berg 
10915ed176e1SJohannes Berg 	if (!vif)
10925ed176e1SJohannes Berg 		return NULL;
10935ed176e1SJohannes Berg 
1094f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1095f7c65594SJohannes Berg 	if (!sta)
1096f7c65594SJohannes Berg 		return NULL;
10975ed176e1SJohannes Berg 
1098f7c65594SJohannes Berg 	if (!sta->uploaded)
1099f7c65594SJohannes Berg 		return NULL;
1100f7c65594SJohannes Berg 
1101f7c65594SJohannes Berg 	return &sta->sta;
11025ed176e1SJohannes Berg }
110317741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1104af818581SJohannes Berg 
1105e3685e03SJohannes Berg /* powersave support code */
1106e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
110750a9432dSJohannes Berg {
1108608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1109e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1110e3685e03SJohannes Berg 	struct sk_buff_head pending;
1111*ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1112e3685e03SJohannes Berg 	unsigned long flags;
1113d012a605SMarco Porsch 	struct ps_data *ps;
1114d012a605SMarco Porsch 
11153918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11163918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11173918edb0SFelix Fietkau 				     u.ap);
11183918edb0SFelix Fietkau 
11193918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1120d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11213f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11223f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1123d012a605SMarco Porsch 	else
1124d012a605SMarco Porsch 		return;
112550a9432dSJohannes Berg 
1126c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
112747086fc5SJohannes Berg 
11285a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1129948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1130*ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1131948d887dSJohannes Berg 
1132d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
113312375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1134af818581SJohannes Berg 
1135*ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1136*ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1137*ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1138*ba8c3d6fSFelix Fietkau 
1139*ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1140*ba8c3d6fSFelix Fietkau 				continue;
1141*ba8c3d6fSFelix Fietkau 
1142*ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1143*ba8c3d6fSFelix Fietkau 		}
1144*ba8c3d6fSFelix Fietkau 	}
1145*ba8c3d6fSFelix Fietkau 
1146948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1147af818581SJohannes Berg 
11481d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11491d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1150af818581SJohannes Berg 	/* Send all buffered frames to the station */
1151948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1152948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1153948d887dSJohannes Berg 
1154987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1155948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1156987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1157948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1158948d887dSJohannes Berg 		filtered += tmp - count;
1159948d887dSJohannes Berg 		count = tmp;
1160948d887dSJohannes Berg 
1161987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1162948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1163987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1164948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1165948d887dSJohannes Berg 		buffered += tmp - count;
1166948d887dSJohannes Berg 	}
1167948d887dSJohannes Berg 
1168e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11695ac2e350SJohannes Berg 
11705ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11715ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11725ac2e350SJohannes Berg 
11735ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
11745ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
11755ac2e350SJohannes Berg 	 */
11765ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
11775ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
11781d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1179948d887dSJohannes Berg 
1180e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1181e3685e03SJohannes Berg 
1182687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1183062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1184062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1185687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1186687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1187687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1188687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1189687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1190687da132SEmmanuel Grumbach 		       sta->sta.addr);
1191687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1192687da132SEmmanuel Grumbach 					   sta->sta.addr,
1193687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1194687da132SEmmanuel Grumbach 	}
1195687da132SEmmanuel Grumbach 
1196af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1197af818581SJohannes Berg 
1198c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1199c868cb35SJohannes Berg 
1200bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1201bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1202bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1203af818581SJohannes Berg }
1204af818581SJohannes Berg 
1205ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1206ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1207b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1208b77cf4f8SJohannes Berg 					 bool call_driver)
1209ce662b44SJohannes Berg {
1210ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1211ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1212ce662b44SJohannes Berg 	struct sk_buff *skb;
1213ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1214ce662b44SJohannes Berg 	__le16 fc;
1215a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1216ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
121755de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1218ce662b44SJohannes Berg 
1219ce662b44SJohannes Berg 	if (qos) {
1220ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1221ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1222ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1223ce662b44SJohannes Berg 	} else {
1224ce662b44SJohannes Berg 		size -= 2;
1225ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1226ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1227ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1228ce662b44SJohannes Berg 	}
1229ce662b44SJohannes Berg 
1230ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1231ce662b44SJohannes Berg 	if (!skb)
1232ce662b44SJohannes Berg 		return;
1233ce662b44SJohannes Berg 
1234ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1235ce662b44SJohannes Berg 
1236ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1237ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1238ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1239ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1240ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1241ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1242864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1243ce662b44SJohannes Berg 
1244ce662b44SJohannes Berg 	skb->priority = tid;
1245ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
124659b66255SJohannes Berg 	if (qos) {
1247ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1248ce662b44SJohannes Berg 
124940b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1250ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1251ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1252ce662b44SJohannes Berg 	}
1253ce662b44SJohannes Berg 
1254ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1255ce662b44SJohannes Berg 
1256ce662b44SJohannes Berg 	/*
1257ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1258ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1259deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1260deeaee19SJohannes Berg 	 * ends the poll/service period.
1261ce662b44SJohannes Berg 	 */
126202f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1263deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1264ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1265ce662b44SJohannes Berg 
12666b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
12676b127c71SSujith Manoharan 
1268b77cf4f8SJohannes Berg 	if (call_driver)
1269b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1270b77cf4f8SJohannes Berg 					  reason, false);
127140b96408SJohannes Berg 
127289afe614SJohannes Berg 	skb->dev = sdata->dev;
127389afe614SJohannes Berg 
127455de908aSJohannes Berg 	rcu_read_lock();
127555de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
127655de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
127755de908aSJohannes Berg 		rcu_read_unlock();
127855de908aSJohannes Berg 		kfree_skb(skb);
127955de908aSJohannes Berg 		return;
128055de908aSJohannes Berg 	}
128155de908aSJohannes Berg 
128273c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
12837c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
128455de908aSJohannes Berg 	rcu_read_unlock();
1285ce662b44SJohannes Berg }
1286ce662b44SJohannes Berg 
12870a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
12880a1cb809SJohannes Berg {
12890a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
12900a1cb809SJohannes Berg 	if (tids & 0xF8)
12910a1cb809SJohannes Berg 		return fls(tids) - 1;
12920a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
12930a1cb809SJohannes Berg 	if (tids & BIT(0))
12940a1cb809SJohannes Berg 		return 0;
12950a1cb809SJohannes Berg 	return fls(tids) - 1;
12960a1cb809SJohannes Berg }
12970a1cb809SJohannes Berg 
129847086fc5SJohannes Berg static void
129947086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
130047086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
130147086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1302af818581SJohannes Berg {
1303af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1304af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1305948d887dSJohannes Berg 	bool more_data = false;
1306948d887dSJohannes Berg 	int ac;
13074049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
130847086fc5SJohannes Berg 	struct sk_buff_head frames;
130947086fc5SJohannes Berg 
1310deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1311c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1312deeaee19SJohannes Berg 
131347086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1314af818581SJohannes Berg 
1315f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1316948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13174049e09aSJohannes Berg 		unsigned long tids;
13184049e09aSJohannes Berg 
131947086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1320948d887dSJohannes Berg 			continue;
1321948d887dSJohannes Berg 
13224049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13234049e09aSJohannes Berg 
1324f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1325f9f760b4SJohannes Berg 		 * release from hardware queues
1326f9f760b4SJohannes Berg 		 */
1327*ba8c3d6fSFelix Fietkau 		if (skb_queue_empty(&frames)) {
1328f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1329*ba8c3d6fSFelix Fietkau 			driver_release_tids |= sta->txq_buffered_tids & tids;
1330*ba8c3d6fSFelix Fietkau 		}
1331f9f760b4SJohannes Berg 
13324049e09aSJohannes Berg 		if (driver_release_tids) {
1333f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1334f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1335f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1336f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1337f9f760b4SJohannes Berg 			 */
1338f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1339f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1340f9f760b4SJohannes Berg 				more_data = true;
1341f9f760b4SJohannes Berg 				driver_release_tids =
1342f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1343f9f760b4SJohannes Berg 						driver_release_tids));
1344f9f760b4SJohannes Berg 				break;
1345f9f760b4SJohannes Berg 			}
13464049e09aSJohannes Berg 		} else {
134747086fc5SJohannes Berg 			struct sk_buff *skb;
134847086fc5SJohannes Berg 
134947086fc5SJohannes Berg 			while (n_frames > 0) {
1350948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1351948d887dSJohannes Berg 				if (!skb) {
135247086fc5SJohannes Berg 					skb = skb_dequeue(
135347086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1354af818581SJohannes Berg 					if (skb)
1355af818581SJohannes Berg 						local->total_ps_buffered--;
1356af818581SJohannes Berg 				}
135747086fc5SJohannes Berg 				if (!skb)
135847086fc5SJohannes Berg 					break;
135947086fc5SJohannes Berg 				n_frames--;
136047086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
136147086fc5SJohannes Berg 			}
1362948d887dSJohannes Berg 		}
1363948d887dSJohannes Berg 
1364f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1365f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1366f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13674049e09aSJohannes Berg 		 */
1368948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1369948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1370948d887dSJohannes Berg 			more_data = true;
1371948d887dSJohannes Berg 			break;
1372948d887dSJohannes Berg 		}
1373948d887dSJohannes Berg 	}
1374af818581SJohannes Berg 
1375f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1376ce662b44SJohannes Berg 		int tid;
13774049e09aSJohannes Berg 
1378ce662b44SJohannes Berg 		/*
1379ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1380ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1381ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1382ce662b44SJohannes Berg 		 *
1383ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1384ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1385ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1386ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1387ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1388ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1389ce662b44SJohannes Berg 		 *
1390ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1391ce662b44SJohannes Berg 		 */
1392ce662b44SJohannes Berg 
1393ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1394ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1395ce662b44SJohannes Berg 
1396b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1397f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
139847086fc5SJohannes Berg 		struct sk_buff_head pending;
139947086fc5SJohannes Berg 		struct sk_buff *skb;
140040b96408SJohannes Berg 		int num = 0;
140140b96408SJohannes Berg 		u16 tids = 0;
1402b77cf4f8SJohannes Berg 		bool need_null = false;
140347086fc5SJohannes Berg 
140447086fc5SJohannes Berg 		skb_queue_head_init(&pending);
140547086fc5SJohannes Berg 
140647086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1407af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
140847086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
140940b96408SJohannes Berg 			u8 *qoshdr = NULL;
141040b96408SJohannes Berg 
141140b96408SJohannes Berg 			num++;
1412af818581SJohannes Berg 
1413af818581SJohannes Berg 			/*
141447086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
141547086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
141647086fc5SJohannes Berg 			 * exchange.
1417af818581SJohannes Berg 			 */
14186b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
14196b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1420af818581SJohannes Berg 
142147086fc5SJohannes Berg 			/*
142247086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
142347086fc5SJohannes Berg 			 * more buffered frames for this STA
142447086fc5SJohannes Berg 			 */
142524b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
142647086fc5SJohannes Berg 				hdr->frame_control |=
142747086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
142824b9c373SJanusz.Dziedzic@tieto.com 			else
142924b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
143024b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1431af818581SJohannes Berg 
143240b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
143340b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
143440b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
143540b96408SJohannes Berg 
1436b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1437b77cf4f8SJohannes Berg 
1438b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1439b77cf4f8SJohannes Berg 
1440b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1441b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1442b77cf4f8SJohannes Berg 				continue;
1443b77cf4f8SJohannes Berg 
1444b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1445b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1446b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1447b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1448b77cf4f8SJohannes Berg 				break;
1449b77cf4f8SJohannes Berg 			}
1450b77cf4f8SJohannes Berg 
1451b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1452b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1453b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1454b77cf4f8SJohannes Berg 			 * and be done.
1455b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1456b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1457b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1458b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1459b77cf4f8SJohannes Berg 			 *
1460b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1461b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1462b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1463b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1464b77cf4f8SJohannes Berg 			 *
1465b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1466b77cf4f8SJohannes Berg 			 */
1467b77cf4f8SJohannes Berg 			if (qoshdr) {
146840b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1469deeaee19SJohannes Berg 
147047086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
147147086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1472b77cf4f8SJohannes Berg 			} else {
1473b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1474b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1475b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1476b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1477b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1478b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1479b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1480b77cf4f8SJohannes Berg 				 */
1481b77cf4f8SJohannes Berg 				hdr->frame_control |=
1482b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1483b77cf4f8SJohannes Berg 				need_null = true;
1484b77cf4f8SJohannes Berg 				num++;
148552a3f20cSMarco Porsch 			}
1486b77cf4f8SJohannes Berg 			break;
148747086fc5SJohannes Berg 		}
148847086fc5SJohannes Berg 
148940b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
149040b96408SJohannes Berg 					  reason, more_data);
149140b96408SJohannes Berg 
149247086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1493af818581SJohannes Berg 
1494b77cf4f8SJohannes Berg 		if (need_null)
1495b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1496b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1497b77cf4f8SJohannes Berg 				reason, false);
1498b77cf4f8SJohannes Berg 
1499c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1500af818581SJohannes Berg 	} else {
1501*ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1502*ba8c3d6fSFelix Fietkau 		int tid;
1503*ba8c3d6fSFelix Fietkau 
1504af818581SJohannes Berg 		/*
15054049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
15064049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1507f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1508f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1509f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1510f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1511f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1512f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1513af818581SJohannes Berg 		 */
15144049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
151547086fc5SJohannes Berg 					    n_frames, reason, more_data);
15164049e09aSJohannes Berg 
15174049e09aSJohannes Berg 		/*
15184049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
15194049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1520f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
15214049e09aSJohannes Berg 		 * release function.
1522f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1523*ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1524*ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
15254049e09aSJohannes Berg 		 */
1526*ba8c3d6fSFelix Fietkau 
1527*ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1528*ba8c3d6fSFelix Fietkau 			return;
1529*ba8c3d6fSFelix Fietkau 
1530*ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1531*ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1532*ba8c3d6fSFelix Fietkau 
1533*ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1534*ba8c3d6fSFelix Fietkau 				continue;
1535*ba8c3d6fSFelix Fietkau 
1536*ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1537*ba8c3d6fSFelix Fietkau 			break;
1538*ba8c3d6fSFelix Fietkau 		}
1539af818581SJohannes Berg 	}
1540af818581SJohannes Berg }
1541af818581SJohannes Berg 
1542af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1543af818581SJohannes Berg {
154447086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1545af818581SJohannes Berg 
1546af818581SJohannes Berg 	/*
154747086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
154847086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
154947086fc5SJohannes Berg 	 * only from the non-enabled ones.
1550af818581SJohannes Berg 	 */
155147086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
155247086fc5SJohannes Berg 		ignore_for_response = 0;
1553af818581SJohannes Berg 
155447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
155547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1556af818581SJohannes Berg }
155747086fc5SJohannes Berg 
155847086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
155947086fc5SJohannes Berg {
156047086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
156147086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
156247086fc5SJohannes Berg 
156347086fc5SJohannes Berg 	/*
156447086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
156547086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
156647086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
156747086fc5SJohannes Berg 	 * actually getting called.
156847086fc5SJohannes Berg 	 */
156947086fc5SJohannes Berg 	if (!delivery_enabled)
157047086fc5SJohannes Berg 		return;
157147086fc5SJohannes Berg 
157247086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
157347086fc5SJohannes Berg 	case 1:
157447086fc5SJohannes Berg 		n_frames = 2;
157547086fc5SJohannes Berg 		break;
157647086fc5SJohannes Berg 	case 2:
157747086fc5SJohannes Berg 		n_frames = 4;
157847086fc5SJohannes Berg 		break;
157947086fc5SJohannes Berg 	case 3:
158047086fc5SJohannes Berg 		n_frames = 6;
158147086fc5SJohannes Berg 		break;
158247086fc5SJohannes Berg 	case 0:
158347086fc5SJohannes Berg 		/* XXX: what is a good value? */
158413a8098aSAndrei Otcheretianski 		n_frames = 128;
158547086fc5SJohannes Berg 		break;
158647086fc5SJohannes Berg 	}
158747086fc5SJohannes Berg 
158847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
158947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1590af818581SJohannes Berg }
1591af818581SJohannes Berg 
1592af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1593af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1594af818581SJohannes Berg {
1595af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1596af818581SJohannes Berg 
1597b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1598b5878a2dSJohannes Berg 
15995ac2e350SJohannes Berg 	if (block) {
1600c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
16015ac2e350SJohannes Berg 		return;
16025ac2e350SJohannes Berg 	}
16035ac2e350SJohannes Berg 
16045ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
16055ac2e350SJohannes Berg 		return;
16065ac2e350SJohannes Berg 
16075ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
16085ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
16095ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16105ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16115ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
16125ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
16135ac2e350SJohannes Berg 		/* must be asleep in this case */
16145ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16155ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16165ac2e350SJohannes Berg 	} else {
16175ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16185ac2e350SJohannes Berg 	}
1619af818581SJohannes Berg }
1620af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1621dcf55fb5SFelix Fietkau 
1622e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
162337fbd908SJohannes Berg {
162437fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
162537fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
162637fbd908SJohannes Berg 
162737fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
162837fbd908SJohannes Berg 
162937fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
163037fbd908SJohannes Berg }
1631e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
163237fbd908SJohannes Berg 
1633042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1634042ec453SJohannes Berg 				u8 tid, bool buffered)
1635dcf55fb5SFelix Fietkau {
1636dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1637dcf55fb5SFelix Fietkau 
16385a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1639042ec453SJohannes Berg 		return;
1640042ec453SJohannes Berg 
16411b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
16421b000789SJohannes Berg 
1643948d887dSJohannes Berg 	if (buffered)
1644948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1645948d887dSJohannes Berg 	else
1646948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1647948d887dSJohannes Berg 
1648c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1649dcf55fb5SFelix Fietkau }
1650042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1651d9a7ddb0SJohannes Berg 
165283d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1653d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1654d9a7ddb0SJohannes Berg {
16558bf11d8dSJohannes Berg 	might_sleep();
1656d9a7ddb0SJohannes Berg 
1657d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1658d9a7ddb0SJohannes Berg 		return 0;
1659d9a7ddb0SJohannes Berg 
1660f09603a2SJohannes Berg 	/* check allowed transitions first */
1661f09603a2SJohannes Berg 
1662d9a7ddb0SJohannes Berg 	switch (new_state) {
1663d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1664f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1665d9a7ddb0SJohannes Berg 			return -EINVAL;
1666d9a7ddb0SJohannes Berg 		break;
1667d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1668f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1669f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1670d9a7ddb0SJohannes Berg 			return -EINVAL;
1671d9a7ddb0SJohannes Berg 		break;
1672d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1673f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1674f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1675d9a7ddb0SJohannes Berg 			return -EINVAL;
1676d9a7ddb0SJohannes Berg 		break;
1677d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1678f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1679d9a7ddb0SJohannes Berg 			return -EINVAL;
1680d9a7ddb0SJohannes Berg 		break;
1681d9a7ddb0SJohannes Berg 	default:
1682d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1683d9a7ddb0SJohannes Berg 		return -EINVAL;
1684d9a7ddb0SJohannes Berg 	}
1685d9a7ddb0SJohannes Berg 
1686bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1687bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1688f09603a2SJohannes Berg 
1689f09603a2SJohannes Berg 	/*
1690f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1691f09603a2SJohannes Berg 	 * fail the transition
1692f09603a2SJohannes Berg 	 */
1693f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1694f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1695f09603a2SJohannes Berg 					sta->sta_state, new_state);
1696f09603a2SJohannes Berg 		if (err)
1697f09603a2SJohannes Berg 			return err;
1698f09603a2SJohannes Berg 	}
1699f09603a2SJohannes Berg 
1700f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1701f09603a2SJohannes Berg 
1702f09603a2SJohannes Berg 	switch (new_state) {
1703f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1704f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1705f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1706f09603a2SJohannes Berg 		break;
1707f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1708f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1709f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1710f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1711f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1712f09603a2SJohannes Berg 		break;
1713f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1714f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1715f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1716f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
17177e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17187e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17197e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17207e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1721f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1722f09603a2SJohannes Berg 		}
1723f09603a2SJohannes Berg 		break;
1724f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1725f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
17267e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17277e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17287e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17297e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1730f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1731f09603a2SJohannes Berg 		}
1732f09603a2SJohannes Berg 		break;
1733f09603a2SJohannes Berg 	default:
1734f09603a2SJohannes Berg 		break;
1735f09603a2SJohannes Berg 	}
1736f09603a2SJohannes Berg 
1737d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1738d9a7ddb0SJohannes Berg 
1739d9a7ddb0SJohannes Berg 	return 0;
1740d9a7ddb0SJohannes Berg }
1741687da132SEmmanuel Grumbach 
1742687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1743687da132SEmmanuel Grumbach {
1744687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1745687da132SEmmanuel Grumbach 	u8 rx_streams;
1746687da132SEmmanuel Grumbach 
1747687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1748687da132SEmmanuel Grumbach 		return 1;
1749687da132SEmmanuel Grumbach 
1750687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1751687da132SEmmanuel Grumbach 		int i;
1752687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1753687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1754687da132SEmmanuel Grumbach 
1755687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1756687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1757687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1758687da132SEmmanuel Grumbach 				return i + 1;
1759687da132SEmmanuel Grumbach 	}
1760687da132SEmmanuel Grumbach 
1761687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1762687da132SEmmanuel Grumbach 		rx_streams = 4;
1763687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1764687da132SEmmanuel Grumbach 		rx_streams = 3;
1765687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1766687da132SEmmanuel Grumbach 		rx_streams = 2;
1767687da132SEmmanuel Grumbach 	else
1768687da132SEmmanuel Grumbach 		rx_streams = 1;
1769687da132SEmmanuel Grumbach 
1770687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1771687da132SEmmanuel Grumbach 		return rx_streams;
1772687da132SEmmanuel Grumbach 
1773687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1774687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1775687da132SEmmanuel Grumbach }
1776b7ffbd7eSJohannes Berg 
1777b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1778b7ffbd7eSJohannes Berg {
1779b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1780b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
17819a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1782b7ffbd7eSJohannes Berg 	struct timespec uptime;
1783b7ffbd7eSJohannes Berg 	u32 thr = 0;
1784b7ffbd7eSJohannes Berg 	int i, ac;
1785b7ffbd7eSJohannes Berg 
17869a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
17879a244409SJohn W. Linville 		ref = local->rate_ctrl;
17889a244409SJohn W. Linville 
1789b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1790b7ffbd7eSJohannes Berg 
1791225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1792225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1793225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1794225b8189SJohannes Berg 	 */
1795225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1796225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1797225b8189SJohannes Berg 
17982b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
17992b9a7e1bSJohannes Berg 
1800319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1801319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1802319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1803319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1804319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1805319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BEACON_LOSS);
1806b7ffbd7eSJohannes Berg 
180718171520SThomas Gleixner 	ktime_get_ts(&uptime);
1808b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1809b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
18102b9a7e1bSJohannes Berg 
1811319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1812319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1813b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
18142b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1815b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1816319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1817b7ffbd7eSJohannes Berg 	}
18182b9a7e1bSJohannes Berg 
1819319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
18202b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
18212b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
18222b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1823319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
18242b9a7e1bSJohannes Berg 	}
18252b9a7e1bSJohannes Berg 
1826319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1827319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1828b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1829319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
18302b9a7e1bSJohannes Berg 	}
18312b9a7e1bSJohannes Berg 
1832319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1833b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1834319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
18352b9a7e1bSJohannes Berg 	}
18362b9a7e1bSJohannes Berg 
1837319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1838b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1839319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
18402b9a7e1bSJohannes Berg 	}
18412b9a7e1bSJohannes Berg 
1842319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1843b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1844319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
18452b9a7e1bSJohannes Berg 	}
18462b9a7e1bSJohannes Berg 
1847b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1848b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1849b7ffbd7eSJohannes Berg 
1850225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1851225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1852225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1853225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1854225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1855225b8189SJohannes Berg 	}
1856225b8189SJohannes Berg 
1857b7ffbd7eSJohannes Berg 	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1858b7ffbd7eSJohannes Berg 	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1859319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1860b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1861319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1862b7ffbd7eSJohannes Berg 		}
18632b9a7e1bSJohannes Berg 
1864319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
18652b9a7e1bSJohannes Berg 			sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1866319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
18672b9a7e1bSJohannes Berg 		}
18682b9a7e1bSJohannes Berg 	}
18692b9a7e1bSJohannes Berg 
18702b9a7e1bSJohannes Berg 	if (sta->chains &&
1871319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1872319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1873319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1874319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1875b7ffbd7eSJohannes Berg 
1876b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1877b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1878b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1879b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1880b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1881b7ffbd7eSJohannes Berg 		}
1882b7ffbd7eSJohannes Berg 	}
1883b7ffbd7eSJohannes Berg 
1884319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1885b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1886319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
18872b9a7e1bSJohannes Berg 	}
18882b9a7e1bSJohannes Berg 
1889319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1890b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1891319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
18922b9a7e1bSJohannes Berg 	}
1893b7ffbd7eSJohannes Berg 
189479c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
189579c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
189679c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
189779c892b8SJohannes Berg 
189879c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
189979c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
190079c892b8SJohannes Berg 			tidstats->rx_msdu = sta->rx_msdu[i];
190179c892b8SJohannes Berg 		}
190279c892b8SJohannes Berg 
190379c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
190479c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
190579c892b8SJohannes Berg 			tidstats->tx_msdu = sta->tx_msdu[i];
190679c892b8SJohannes Berg 		}
190779c892b8SJohannes Berg 
190879c892b8SJohannes Berg 		if (!(tidstats->filled &
190979c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
191079c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
191179c892b8SJohannes Berg 			tidstats->filled |=
191279c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
191379c892b8SJohannes Berg 			tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
191479c892b8SJohannes Berg 		}
191579c892b8SJohannes Berg 
191679c892b8SJohannes Berg 		if (!(tidstats->filled &
191779c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
191879c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
191979c892b8SJohannes Berg 			tidstats->filled |=
192079c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
192179c892b8SJohannes Berg 			tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
192279c892b8SJohannes Berg 		}
192379c892b8SJohannes Berg 	}
192479c892b8SJohannes Berg 
1925b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1926b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1927319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1928319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
1929319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
1930319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
1931319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
1932319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
1933b7ffbd7eSJohannes Berg 
1934b7ffbd7eSJohannes Berg 		sinfo->llid = sta->llid;
1935b7ffbd7eSJohannes Berg 		sinfo->plid = sta->plid;
1936b7ffbd7eSJohannes Berg 		sinfo->plink_state = sta->plink_state;
1937b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1938319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1939b7ffbd7eSJohannes Berg 			sinfo->t_offset = sta->t_offset;
1940b7ffbd7eSJohannes Berg 		}
1941b7ffbd7eSJohannes Berg 		sinfo->local_pm = sta->local_pm;
1942b7ffbd7eSJohannes Berg 		sinfo->peer_pm = sta->peer_pm;
1943b7ffbd7eSJohannes Berg 		sinfo->nonpeer_pm = sta->nonpeer_pm;
1944b7ffbd7eSJohannes Berg #endif
1945b7ffbd7eSJohannes Berg 	}
1946b7ffbd7eSJohannes Berg 
1947b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1948b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1949b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1950b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1951b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1952b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1953b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1954785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1955b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1956b7ffbd7eSJohannes Berg 
1957b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1958b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1959b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1960b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1961b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1962b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1963b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1964b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1965b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1966b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1967b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1968b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1969a74a8c84SJohannes Berg 	if (sta->sta.wme)
1970b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1971b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
1972b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1973b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
1974b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1975b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
1976b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1977b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1978b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1979b7ffbd7eSJohannes Berg 
1980b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
1981b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
1982b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1983b7ffbd7eSJohannes Berg 	else
1984b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
1985b7ffbd7eSJohannes Berg 
1986b7ffbd7eSJohannes Berg 	if (thr != 0) {
1987319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
1988b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
1989b7ffbd7eSJohannes Berg 	}
1990b7ffbd7eSJohannes Berg }
1991