xref: /openbmc/linux/net/mac80211/sta_info.c (revision 35c347ac53040daba955fa06fcd5f909bee85017)
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 
109ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
110ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
111ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
112ba8c3d6fSFelix Fietkau 			int n = skb_queue_len(&txqi->queue);
113ba8c3d6fSFelix Fietkau 
114ba8c3d6fSFelix Fietkau 			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
115ba8c3d6fSFelix Fietkau 			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
116ba8c3d6fSFelix Fietkau 		}
117ba8c3d6fSFelix Fietkau 	}
118ba8c3d6fSFelix 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 
231ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
232ba8c3d6fSFelix 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,
272*35c347acSJohannes Berg 						     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;
283ba8c3d6fSFelix 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 
288ba8c3d6fSFelix 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
29848bf6bedSBob Copeland 	spin_lock_init(&sta->plink_lock);
29987f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
30087f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
30187f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3026c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
30387f59c70SThomas Pedersen #endif
30407346f81SJohannes Berg 
30517741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
306d0709a65SJohannes Berg 	sta->local = local;
307d0709a65SJohannes Berg 	sta->sdata = sdata;
3088bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
309f0706e82SJiri Benc 
31071ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
31171ec375cSJohannes Berg 
312b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
313b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
314b6da911bSLiad Kaufman 
31518171520SThomas Gleixner 	ktime_get_ts(&uptime);
316ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
317541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
318ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
319ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
320541a45a1SBruno Randolf 
321ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
322ba8c3d6fSFelix Fietkau 		void *txq_data;
323ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
324ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
325ba8c3d6fSFelix Fietkau 
326ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
327ba8c3d6fSFelix Fietkau 		if (!txq_data)
328ba8c3d6fSFelix Fietkau 			goto free;
329ba8c3d6fSFelix Fietkau 
330ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
331ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
332ba8c3d6fSFelix Fietkau 
333ba8c3d6fSFelix Fietkau 			ieee80211_init_tx_queue(sdata, sta, txq, i);
334abfbc3afSJohannes Berg 		}
335ba8c3d6fSFelix Fietkau 	}
336ba8c3d6fSFelix Fietkau 
337ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
338ba8c3d6fSFelix Fietkau 		goto free_txq;
339f0706e82SJiri Benc 
3405a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
341a622ab72SJohannes Berg 		/*
342a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
343a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
344a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
345a622ab72SJohannes Berg 		 */
34616c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
34716c5f15cSRon Rindjunsky 	}
348948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
349948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
350948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
351948d887dSJohannes Berg 	}
35273651ee6SJohannes Berg 
3535a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3544be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
355cccaec98SSenthil Balasubramanian 
356af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
357687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
358687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
359687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
360ba8c3d6fSFelix Fietkau 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
361687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
362687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
363687da132SEmmanuel Grumbach 		/*
364687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
365687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
366687da132SEmmanuel Grumbach 		 */
367687da132SEmmanuel Grumbach 		switch (smps) {
368687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
369687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
370687da132SEmmanuel Grumbach 			break;
371687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
372687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
373687da132SEmmanuel Grumbach 			break;
374687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
375687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
376687da132SEmmanuel Grumbach 			break;
377687da132SEmmanuel Grumbach 		default:
378687da132SEmmanuel Grumbach 			WARN_ON(1);
379687da132SEmmanuel Grumbach 		}
380687da132SEmmanuel Grumbach 	}
381af0ed69bSJohannes Berg 
382bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
383ef04a297SJohannes Berg 
384abfbc3afSJohannes Berg 	return sta;
385ba8c3d6fSFelix Fietkau 
386ba8c3d6fSFelix Fietkau free_txq:
387ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
388ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
389ba8c3d6fSFelix Fietkau free:
390ba8c3d6fSFelix Fietkau 	kfree(sta);
391ba8c3d6fSFelix Fietkau 	return NULL;
39273651ee6SJohannes Berg }
39373651ee6SJohannes Berg 
3948c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
39534e89507SJohannes Berg {
39634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
39734e89507SJohannes Berg 
39803e4497eSJohannes Berg 	/*
39903e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
40003e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
40103e4497eSJohannes Berg 	 * and another CPU turns off the net device.
40203e4497eSJohannes Berg 	 */
4038c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4048c71df7aSGuy Eilam 		return -ENETDOWN;
40503e4497eSJohannes Berg 
406b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4078c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4088c71df7aSGuy Eilam 		return -EINVAL;
4098c71df7aSGuy Eilam 
4108c71df7aSGuy Eilam 	return 0;
41193e5deb1SJohannes Berg }
41244213b5eSJohannes Berg 
413f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
414f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
415f09603a2SJohannes Berg 				     struct sta_info *sta)
416f09603a2SJohannes Berg {
417f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
418f09603a2SJohannes Berg 	int err = 0;
419f09603a2SJohannes Berg 
420f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
421f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
422f09603a2SJohannes Berg 		if (err)
423f09603a2SJohannes Berg 			break;
424f09603a2SJohannes Berg 	}
425f09603a2SJohannes Berg 
426f09603a2SJohannes Berg 	if (!err) {
427a4ec45a4SJohannes Berg 		/*
428a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
429a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
430a4ec45a4SJohannes Berg 		 */
431a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
432f09603a2SJohannes Berg 			sta->uploaded = true;
433f09603a2SJohannes Berg 		return 0;
434f09603a2SJohannes Berg 	}
435f09603a2SJohannes Berg 
436f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
437bdcbd8e0SJohannes Berg 		sdata_info(sdata,
438bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
439bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
440f09603a2SJohannes Berg 		err = 0;
441f09603a2SJohannes Berg 	}
442f09603a2SJohannes Berg 
443f09603a2SJohannes Berg 	/* unwind on error */
444f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
445f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
446f09603a2SJohannes Berg 
447f09603a2SJohannes Berg 	return err;
448f09603a2SJohannes Berg }
449f09603a2SJohannes Berg 
45034e89507SJohannes Berg /*
4518c71df7aSGuy Eilam  * should be called with sta_mtx locked
4528c71df7aSGuy Eilam  * this function replaces the mutex lock
4538c71df7aSGuy Eilam  * with a RCU lock
4548c71df7aSGuy Eilam  */
4554d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4568c71df7aSGuy Eilam {
4578c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4588c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4597852e361SJohannes Berg 	struct station_info sinfo;
4608c71df7aSGuy Eilam 	int err = 0;
4618c71df7aSGuy Eilam 
4628c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4638c71df7aSGuy Eilam 
4647852e361SJohannes Berg 	/* check if STA exists already */
4657852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4664d33960bSJohannes Berg 		err = -EEXIST;
4674d33960bSJohannes Berg 		goto out_err;
46834e89507SJohannes Berg 	}
46934e89507SJohannes Berg 
4704d33960bSJohannes Berg 	local->num_sta++;
4714d33960bSJohannes Berg 	local->sta_generation++;
4724d33960bSJohannes Berg 	smp_mb();
4734d33960bSJohannes Berg 
4745108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
4755108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4765108ca82SJohannes Berg 
4774d33960bSJohannes Berg 	/* make the station visible */
4784d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
4794d33960bSJohannes Berg 
4802bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
48183d5cc01SJohannes Berg 
4825108ca82SJohannes Berg 	/* notify driver */
4835108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
4845108ca82SJohannes Berg 	if (err)
4855108ca82SJohannes Berg 		goto out_remove;
4865108ca82SJohannes Berg 
48783d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
4885108ca82SJohannes Berg 	/* accept BA sessions now */
4895108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4904d33960bSJohannes Berg 
49121f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
4924d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
4934d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
4944d33960bSJohannes Berg 
4954d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
4964d33960bSJohannes Berg 	sinfo.filled = 0;
4974d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
4984d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
499d0709a65SJohannes Berg 
500bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
501f0706e82SJiri Benc 
50234e89507SJohannes Berg 	/* move reference to rcu-protected */
50334e89507SJohannes Berg 	rcu_read_lock();
50434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
505e9f207f0SJiri Benc 
50673651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
50773651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
50873651ee6SJohannes Berg 
50973651ee6SJohannes Berg 	return 0;
5105108ca82SJohannes Berg  out_remove:
5115108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5125108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5135108ca82SJohannes Berg 	local->num_sta--;
5145108ca82SJohannes Berg 	synchronize_net();
5155108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5164d33960bSJohannes Berg  out_err:
5174d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5184d33960bSJohannes Berg 	rcu_read_lock();
5194d33960bSJohannes Berg 	return err;
5208c71df7aSGuy Eilam }
5218c71df7aSGuy Eilam 
5228c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5238c71df7aSGuy Eilam {
5248c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
525308f7fcfSZhao, Gang 	int err;
5268c71df7aSGuy Eilam 
5274d33960bSJohannes Berg 	might_sleep();
5284d33960bSJohannes Berg 
5298c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5308c71df7aSGuy Eilam 	if (err) {
5318c71df7aSGuy Eilam 		rcu_read_lock();
5328c71df7aSGuy Eilam 		goto out_free;
5338c71df7aSGuy Eilam 	}
5348c71df7aSGuy Eilam 
535004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
536004c872eSJohannes Berg 
5374d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5388c71df7aSGuy Eilam 	if (err)
539004c872eSJohannes Berg 		goto out_free;
540d0709a65SJohannes Berg 
541004c872eSJohannes Berg 	return 0;
54293e5deb1SJohannes Berg  out_free:
543d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
54493e5deb1SJohannes Berg 	return err;
545f0706e82SJiri Benc }
546f0706e82SJiri Benc 
54734e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
54834e89507SJohannes Berg {
54934e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
55034e89507SJohannes Berg 
55134e89507SJohannes Berg 	rcu_read_unlock();
55234e89507SJohannes Berg 
55334e89507SJohannes Berg 	return err;
55434e89507SJohannes Berg }
55534e89507SJohannes Berg 
556d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
557004c872eSJohannes Berg {
558004c872eSJohannes Berg 	/*
559004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
560004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
561004c872eSJohannes Berg 	 */
562d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
563004c872eSJohannes Berg }
564004c872eSJohannes Berg 
565d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
566004c872eSJohannes Berg {
567004c872eSJohannes Berg 	/*
568004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
569004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
570004c872eSJohannes Berg 	 */
571d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
572004c872eSJohannes Berg }
573004c872eSJohannes Berg 
5743d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
5753d5839b6SIlan Peer {
5763d5839b6SIlan Peer 	/*
5773d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
5783d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
5793d5839b6SIlan Peer 	 */
5803d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
5813d5839b6SIlan Peer }
5823d5839b6SIlan Peer 
583948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
584004c872eSJohannes Berg {
585948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
586948d887dSJohannes Berg 	switch (ac) {
587948d887dSJohannes Berg 	case IEEE80211_AC_VO:
588948d887dSJohannes Berg 		return BIT(6) | BIT(7);
589948d887dSJohannes Berg 	case IEEE80211_AC_VI:
590948d887dSJohannes Berg 		return BIT(4) | BIT(5);
591948d887dSJohannes Berg 	case IEEE80211_AC_BE:
592948d887dSJohannes Berg 		return BIT(0) | BIT(3);
593948d887dSJohannes Berg 	case IEEE80211_AC_BK:
594948d887dSJohannes Berg 		return BIT(1) | BIT(2);
595948d887dSJohannes Berg 	default:
596948d887dSJohannes Berg 		WARN_ON(1);
597948d887dSJohannes Berg 		return 0;
598d0709a65SJohannes Berg 	}
599004c872eSJohannes Berg }
600004c872eSJohannes Berg 
6019b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
602004c872eSJohannes Berg {
603c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
604d012a605SMarco Porsch 	struct ps_data *ps;
605948d887dSJohannes Berg 	bool indicate_tim = false;
606948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
607948d887dSJohannes Berg 	int ac;
608d012a605SMarco Porsch 	u16 id;
609004c872eSJohannes Berg 
610d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
611d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
612c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
613c868cb35SJohannes Berg 			return;
6143e122be0SJohannes Berg 
615d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
616d012a605SMarco Porsch 		id = sta->sta.aid;
6173f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6183f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6193f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
620204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6216f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6223f52b7e3SMarco Porsch #endif
623d012a605SMarco Porsch 	} else {
624d012a605SMarco Porsch 		return;
625d012a605SMarco Porsch 	}
626d012a605SMarco Porsch 
627c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
628c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
629c868cb35SJohannes Berg 		return;
630004c872eSJohannes Berg 
631c868cb35SJohannes Berg 	if (sta->dead)
632c868cb35SJohannes Berg 		goto done;
6333e122be0SJohannes Berg 
634948d887dSJohannes Berg 	/*
635948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
636948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
637948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
638948d887dSJohannes Berg 	 * non-enabled ones.
639948d887dSJohannes Berg 	 */
640948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
641948d887dSJohannes Berg 		ignore_for_tim = 0;
642948d887dSJohannes Berg 
6439b7a86f3SJohannes Berg 	if (ignore_pending)
6449b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6459b7a86f3SJohannes Berg 
646948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
647948d887dSJohannes Berg 		unsigned long tids;
648948d887dSJohannes Berg 
649948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
650948d887dSJohannes Berg 			continue;
651948d887dSJohannes Berg 
652948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
653948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
654948d887dSJohannes Berg 		if (indicate_tim)
655948d887dSJohannes Berg 			break;
656948d887dSJohannes Berg 
657948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
658948d887dSJohannes Berg 
659948d887dSJohannes Berg 		indicate_tim |=
660948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
661ba8c3d6fSFelix Fietkau 		indicate_tim |=
662ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
663004c872eSJohannes Berg 	}
664004c872eSJohannes Berg 
665c868cb35SJohannes Berg  done:
66665f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
667004c872eSJohannes Berg 
6683d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6693d5839b6SIlan Peer 		goto out_unlock;
6703d5839b6SIlan Peer 
671948d887dSJohannes Berg 	if (indicate_tim)
672d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
673c868cb35SJohannes Berg 	else
674d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6753e122be0SJohannes Berg 
6769b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
677c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
678948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
679c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
680004c872eSJohannes Berg 	}
681004c872eSJohannes Berg 
6823d5839b6SIlan Peer out_unlock:
68365f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
684004c872eSJohannes Berg }
685004c872eSJohannes Berg 
6869b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
6879b7a86f3SJohannes Berg {
6889b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
6899b7a86f3SJohannes Berg }
6909b7a86f3SJohannes Berg 
691cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
692f0706e82SJiri Benc {
693e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
694f0706e82SJiri Benc 	int timeout;
695f0706e82SJiri Benc 
696f0706e82SJiri Benc 	if (!skb)
697cd0b8d89SJohannes Berg 		return false;
698f0706e82SJiri Benc 
699e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
700f0706e82SJiri Benc 
701f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
70257c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
70357c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
70457c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
705f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
706f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
707e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
708f0706e82SJiri Benc }
709f0706e82SJiri Benc 
710f0706e82SJiri Benc 
711948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
712948d887dSJohannes Berg 						struct sta_info *sta, int ac)
713f0706e82SJiri Benc {
714f0706e82SJiri Benc 	unsigned long flags;
715f0706e82SJiri Benc 	struct sk_buff *skb;
716f0706e82SJiri Benc 
71760750397SJohannes Berg 	/*
71860750397SJohannes Berg 	 * First check for frames that should expire on the filtered
71960750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
72060750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
72160750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
72260750397SJohannes Berg 	 * total_ps_buffered counter.
72360750397SJohannes Berg 	 */
724f0706e82SJiri Benc 	for (;;) {
725948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
726948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
72757c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
728948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
729836341a7SJohannes Berg 		else
730f0706e82SJiri Benc 			skb = NULL;
731948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
732f0706e82SJiri Benc 
73360750397SJohannes Berg 		/*
73460750397SJohannes Berg 		 * Frames are queued in order, so if this one
73560750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
73660750397SJohannes Berg 		 * we actually reached the end of the queue we
73760750397SJohannes Berg 		 * also need to stop, of course.
73860750397SJohannes Berg 		 */
73960750397SJohannes Berg 		if (!skb)
74060750397SJohannes Berg 			break;
741d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
74260750397SJohannes Berg 	}
74360750397SJohannes Berg 
74460750397SJohannes Berg 	/*
74560750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
74660750397SJohannes Berg 	 * only find something if the filtered queue was emptied
74760750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
74860750397SJohannes Berg 	 * buffered frames.
74960750397SJohannes Berg 	 */
750f0706e82SJiri Benc 	for (;;) {
751948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
752948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
753f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
754948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
755f0706e82SJiri Benc 		else
756f0706e82SJiri Benc 			skb = NULL;
757948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
758f0706e82SJiri Benc 
75960750397SJohannes Berg 		/*
76060750397SJohannes Berg 		 * frames are queued in order, so if this one
76160750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
76260750397SJohannes Berg 		 * the queue) we can stop testing
76360750397SJohannes Berg 		 */
764836341a7SJohannes Berg 		if (!skb)
765836341a7SJohannes Berg 			break;
766836341a7SJohannes Berg 
767f0706e82SJiri Benc 		local->total_ps_buffered--;
768bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
769bdcbd8e0SJohannes Berg 		       sta->sta.addr);
770d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
771f0706e82SJiri Benc 	}
7723393a608SJuuso Oikarinen 
77360750397SJohannes Berg 	/*
77460750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
77560750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
77660750397SJohannes Berg 	 * frames.
77760750397SJohannes Berg 	 */
77860750397SJohannes Berg 	sta_info_recalc_tim(sta);
77960750397SJohannes Berg 
78060750397SJohannes Berg 	/*
78160750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
78260750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
78360750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
78460750397SJohannes Berg 	 */
785948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
786948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
787948d887dSJohannes Berg }
788948d887dSJohannes Berg 
789948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
790948d887dSJohannes Berg 					     struct sta_info *sta)
791948d887dSJohannes Berg {
792948d887dSJohannes Berg 	bool have_buffered = false;
793948d887dSJohannes Berg 	int ac;
794948d887dSJohannes Berg 
7953f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
7963f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
7973f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
798948d887dSJohannes Berg 		return false;
799948d887dSJohannes Berg 
800948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
801948d887dSJohannes Berg 		have_buffered |=
802948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
803948d887dSJohannes Berg 
804948d887dSJohannes Berg 	return have_buffered;
805f0706e82SJiri Benc }
806f0706e82SJiri Benc 
807d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
80834e89507SJohannes Berg {
80934e89507SJohannes Berg 	struct ieee80211_local *local;
81034e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8116d10e46bSJohannes Berg 	int ret;
81234e89507SJohannes Berg 
81334e89507SJohannes Berg 	might_sleep();
81434e89507SJohannes Berg 
81534e89507SJohannes Berg 	if (!sta)
81634e89507SJohannes Berg 		return -ENOENT;
81734e89507SJohannes Berg 
81834e89507SJohannes Berg 	local = sta->local;
81934e89507SJohannes Berg 	sdata = sta->sdata;
82034e89507SJohannes Berg 
82183d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
82283d5cc01SJohannes Berg 
823098a6070SJohannes Berg 	/*
824098a6070SJohannes Berg 	 * Before removing the station from the driver and
825098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
826098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
827098a6070SJohannes Berg 	 * will be sufficient.
828098a6070SJohannes Berg 	 */
829c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
830c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
831098a6070SJohannes Berg 
83234e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
833b01711beSJohannes Berg 	if (WARN_ON(ret))
83434e89507SJohannes Berg 		return ret;
83534e89507SJohannes Berg 
836a7a6bdd0SArik Nemtsov 	/*
837a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
838a7a6bdd0SArik Nemtsov 	 * removal.
839a7a6bdd0SArik Nemtsov 	 */
840a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
841a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
842a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
843a7a6bdd0SArik Nemtsov 	}
844a7a6bdd0SArik Nemtsov 
845794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8464d33960bSJohannes Berg 
8476a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8486a9d1b91SJohannes Berg 
849a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
850a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
851a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
852a710c816SJohannes Berg 
853d778207bSJohannes Berg 	return 0;
854d778207bSJohannes Berg }
855d778207bSJohannes Berg 
856d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
857d778207bSJohannes Berg {
858d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
859d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8606f7a8d26SJohannes Berg 	struct station_info sinfo = {};
861d778207bSJohannes Berg 	int ret;
862d778207bSJohannes Berg 
863d778207bSJohannes Berg 	/*
864d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
865d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
866d778207bSJohannes Berg 	 */
867d778207bSJohannes Berg 
868d778207bSJohannes Berg 	might_sleep();
869d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
870d778207bSJohannes Berg 
871c8782078SJohannes Berg 	/* now keys can no longer be reached */
8726d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
87334e89507SJohannes Berg 
8749b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
8759b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
8769b7a86f3SJohannes Berg 
87734e89507SJohannes Berg 	sta->dead = true;
87834e89507SJohannes Berg 
87934e89507SJohannes Berg 	local->num_sta--;
88034e89507SJohannes Berg 	local->sta_generation++;
88134e89507SJohannes Berg 
88283d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
883f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
884f09603a2SJohannes Berg 		if (ret) {
88583d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
88683d5cc01SJohannes Berg 			break;
88783d5cc01SJohannes Berg 		}
88883d5cc01SJohannes Berg 	}
889d9a7ddb0SJohannes Berg 
890f09603a2SJohannes Berg 	if (sta->uploaded) {
891f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
892f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
893f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
894f09603a2SJohannes Berg 	}
89534e89507SJohannes Berg 
896bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
897bdcbd8e0SJohannes Berg 
8986f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
8996f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
900ec15e68bSJouni Malinen 
90134e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
90234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
90321f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
90434e89507SJohannes Berg 
905d34ba216SJohannes Berg 	cleanup_single_sta(sta);
906d778207bSJohannes Berg }
907d778207bSJohannes Berg 
908d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
909d778207bSJohannes Berg {
910d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
911d778207bSJohannes Berg 
912d778207bSJohannes Berg 	if (err)
913d778207bSJohannes Berg 		return err;
914d778207bSJohannes Berg 
915d778207bSJohannes Berg 	synchronize_net();
916d778207bSJohannes Berg 
917d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
91834e89507SJohannes Berg 
91934e89507SJohannes Berg 	return 0;
92034e89507SJohannes Berg }
92134e89507SJohannes Berg 
92234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
92334e89507SJohannes Berg {
92434e89507SJohannes Berg 	struct sta_info *sta;
92534e89507SJohannes Berg 	int ret;
92634e89507SJohannes Berg 
92734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9287852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
92934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
93034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
93134e89507SJohannes Berg 
93234e89507SJohannes Berg 	return ret;
93334e89507SJohannes Berg }
93434e89507SJohannes Berg 
93534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
93634e89507SJohannes Berg 			      const u8 *addr)
93734e89507SJohannes Berg {
93834e89507SJohannes Berg 	struct sta_info *sta;
93934e89507SJohannes Berg 	int ret;
94034e89507SJohannes Berg 
94134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9427852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
94334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
94434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
94534e89507SJohannes Berg 
94634e89507SJohannes Berg 	return ret;
94734e89507SJohannes Berg }
948f0706e82SJiri Benc 
949f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
950f0706e82SJiri Benc {
951f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
952f0706e82SJiri Benc 	struct sta_info *sta;
9533393a608SJuuso Oikarinen 	bool timer_needed = false;
954f0706e82SJiri Benc 
955d0709a65SJohannes Berg 	rcu_read_lock();
956d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9573393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9583393a608SJuuso Oikarinen 			timer_needed = true;
959d0709a65SJohannes Berg 	rcu_read_unlock();
960f0706e82SJiri Benc 
9615bb644a0SJohannes Berg 	if (local->quiescing)
9625bb644a0SJohannes Berg 		return;
9635bb644a0SJohannes Berg 
9643393a608SJuuso Oikarinen 	if (!timer_needed)
9653393a608SJuuso Oikarinen 		return;
9663393a608SJuuso Oikarinen 
96726d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
96826d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
969f0706e82SJiri Benc }
970f0706e82SJiri Benc 
9717bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
972f0706e82SJiri Benc {
9737bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
9747bedd0cfSJohannes Berg }
9757bedd0cfSJohannes Berg 
9767bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
9777bedd0cfSJohannes Berg {
9787bedd0cfSJohannes Berg 	int err;
9797bedd0cfSJohannes Berg 
9807bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
9817bedd0cfSJohannes Berg 	if (err)
9827bedd0cfSJohannes Berg 		return err;
9837bedd0cfSJohannes Berg 
9844d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
98534e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
986f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
987f0706e82SJiri Benc 
988b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
989b24b8a24SPavel Emelyanov 		    (unsigned long)local);
9907bedd0cfSJohannes Berg 	return 0;
991f0706e82SJiri Benc }
992f0706e82SJiri Benc 
993f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
994f0706e82SJiri Benc {
995a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
9967bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
997f0706e82SJiri Benc }
998f0706e82SJiri Benc 
999051007d9SJohannes Berg 
1000e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1001f0706e82SJiri Benc {
1002b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1003f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1004d778207bSJohannes Berg 	LIST_HEAD(free_list);
100544213b5eSJohannes Berg 	int ret = 0;
1006f0706e82SJiri Benc 
1007d0709a65SJohannes Berg 	might_sleep();
1008d0709a65SJohannes Berg 
1009e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1010e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1011e716251dSJohannes Berg 
101234e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
101334e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1014e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1015e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1016d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1017d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
101834316837SJohannes Berg 			ret++;
101934316837SJohannes Berg 		}
102034e89507SJohannes Berg 	}
1021d778207bSJohannes Berg 
1022d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1023d778207bSJohannes Berg 		synchronize_net();
1024d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1025d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1026d778207bSJohannes Berg 	}
102734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
102844213b5eSJohannes Berg 
1029051007d9SJohannes Berg 	return ret;
1030051007d9SJohannes Berg }
1031051007d9SJohannes Berg 
103224723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
103324723d1bSJohannes Berg 			  unsigned long exp_time)
103424723d1bSJohannes Berg {
103524723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
103624723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
103724723d1bSJohannes Berg 
103834e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1039e46a2cf9SMohammed Shafi Shajakhan 
1040e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1041ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1042ec2b774eSMarek Lindner 			continue;
1043ec2b774eSMarek Lindner 
104424723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1045eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1046bdcbd8e0SJohannes Berg 				sta->sta.addr);
10473f52b7e3SMarco Porsch 
10483f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10493f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10503f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10513f52b7e3SMarco Porsch 
105234e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
105324723d1bSJohannes Berg 		}
1054e46a2cf9SMohammed Shafi Shajakhan 	}
1055e46a2cf9SMohammed Shafi Shajakhan 
105634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
105724723d1bSJohannes Berg }
105817741cdcSJohannes Berg 
1059686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1060686b9cb9SBen Greear 						   const u8 *addr,
1061686b9cb9SBen Greear 						   const u8 *localaddr)
106217741cdcSJohannes Berg {
10637bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
10647bedd0cfSJohannes Berg 	struct sta_info *sta;
10657bedd0cfSJohannes Berg 	struct rhash_head *tmp;
10667bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
10677bedd0cfSJohannes Berg 
10687bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
106917741cdcSJohannes Berg 
1070686b9cb9SBen Greear 	/*
1071686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1072686b9cb9SBen Greear 	 * ... first in list.
1073686b9cb9SBen Greear 	 */
10747bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1075686b9cb9SBen Greear 		if (localaddr &&
1076b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1077686b9cb9SBen Greear 			continue;
1078f7c65594SJohannes Berg 		if (!sta->uploaded)
1079f7c65594SJohannes Berg 			return NULL;
108017741cdcSJohannes Berg 		return &sta->sta;
1081f7c65594SJohannes Berg 	}
1082f7c65594SJohannes Berg 
1083abe60632SJohannes Berg 	return NULL;
108417741cdcSJohannes Berg }
1085686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10865ed176e1SJohannes Berg 
10875ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10885ed176e1SJohannes Berg 					 const u8 *addr)
10895ed176e1SJohannes Berg {
1090f7c65594SJohannes Berg 	struct sta_info *sta;
10915ed176e1SJohannes Berg 
10925ed176e1SJohannes Berg 	if (!vif)
10935ed176e1SJohannes Berg 		return NULL;
10945ed176e1SJohannes Berg 
1095f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1096f7c65594SJohannes Berg 	if (!sta)
1097f7c65594SJohannes Berg 		return NULL;
10985ed176e1SJohannes Berg 
1099f7c65594SJohannes Berg 	if (!sta->uploaded)
1100f7c65594SJohannes Berg 		return NULL;
1101f7c65594SJohannes Berg 
1102f7c65594SJohannes Berg 	return &sta->sta;
11035ed176e1SJohannes Berg }
110417741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1105af818581SJohannes Berg 
1106e3685e03SJohannes Berg /* powersave support code */
1107e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
110850a9432dSJohannes Berg {
1109608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1110e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1111e3685e03SJohannes Berg 	struct sk_buff_head pending;
1112ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1113e3685e03SJohannes Berg 	unsigned long flags;
1114d012a605SMarco Porsch 	struct ps_data *ps;
1115d012a605SMarco Porsch 
11163918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11173918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11183918edb0SFelix Fietkau 				     u.ap);
11193918edb0SFelix Fietkau 
11203918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1121d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11223f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11233f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1124d012a605SMarco Porsch 	else
1125d012a605SMarco Porsch 		return;
112650a9432dSJohannes Berg 
1127c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
112847086fc5SJohannes Berg 
11295a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1130948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1131ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1132948d887dSJohannes Berg 
1133d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
113412375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1135af818581SJohannes Berg 
1136ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1137ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1138ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1139ba8c3d6fSFelix Fietkau 
1140ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1141ba8c3d6fSFelix Fietkau 				continue;
1142ba8c3d6fSFelix Fietkau 
1143ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1144ba8c3d6fSFelix Fietkau 		}
1145ba8c3d6fSFelix Fietkau 	}
1146ba8c3d6fSFelix Fietkau 
1147948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1148af818581SJohannes Berg 
11491d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11501d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1151af818581SJohannes Berg 	/* Send all buffered frames to the station */
1152948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1153948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1154948d887dSJohannes Berg 
1155987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1156948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1157987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1158948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1159948d887dSJohannes Berg 		filtered += tmp - count;
1160948d887dSJohannes Berg 		count = tmp;
1161948d887dSJohannes Berg 
1162987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1163948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1164987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1165948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1166948d887dSJohannes Berg 		buffered += tmp - count;
1167948d887dSJohannes Berg 	}
1168948d887dSJohannes Berg 
1169e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11705ac2e350SJohannes Berg 
11715ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11725ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11735ac2e350SJohannes Berg 
11745ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
11755ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
11765ac2e350SJohannes Berg 	 */
11775ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
11785ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
11791d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1180948d887dSJohannes Berg 
1181e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1182e3685e03SJohannes Berg 
1183687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1184062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1185062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1186687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1187687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1188687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1189687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1190687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1191687da132SEmmanuel Grumbach 		       sta->sta.addr);
1192687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1193687da132SEmmanuel Grumbach 					   sta->sta.addr,
1194687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1195687da132SEmmanuel Grumbach 	}
1196687da132SEmmanuel Grumbach 
1197af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1198af818581SJohannes Berg 
1199c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1200c868cb35SJohannes Berg 
1201bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1202bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1203bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1204af818581SJohannes Berg }
1205af818581SJohannes Berg 
1206ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1207ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1208b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1209b77cf4f8SJohannes Berg 					 bool call_driver)
1210ce662b44SJohannes Berg {
1211ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1212ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1213ce662b44SJohannes Berg 	struct sk_buff *skb;
1214ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1215ce662b44SJohannes Berg 	__le16 fc;
1216a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1217ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
121855de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1219ce662b44SJohannes Berg 
1220ce662b44SJohannes Berg 	if (qos) {
1221ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1222ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1223ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1224ce662b44SJohannes Berg 	} else {
1225ce662b44SJohannes Berg 		size -= 2;
1226ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1227ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1228ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1229ce662b44SJohannes Berg 	}
1230ce662b44SJohannes Berg 
1231ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1232ce662b44SJohannes Berg 	if (!skb)
1233ce662b44SJohannes Berg 		return;
1234ce662b44SJohannes Berg 
1235ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1236ce662b44SJohannes Berg 
1237ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1238ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1239ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1240ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1241ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1242ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1243864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1244ce662b44SJohannes Berg 
1245ce662b44SJohannes Berg 	skb->priority = tid;
1246ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
124759b66255SJohannes Berg 	if (qos) {
1248ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1249ce662b44SJohannes Berg 
125040b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1251ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1252ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1253ce662b44SJohannes Berg 	}
1254ce662b44SJohannes Berg 
1255ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1256ce662b44SJohannes Berg 
1257ce662b44SJohannes Berg 	/*
1258ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1259ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1260deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1261deeaee19SJohannes Berg 	 * ends the poll/service period.
1262ce662b44SJohannes Berg 	 */
126302f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1264deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1265ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1266ce662b44SJohannes Berg 
12676b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
12686b127c71SSujith Manoharan 
1269b77cf4f8SJohannes Berg 	if (call_driver)
1270b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1271b77cf4f8SJohannes Berg 					  reason, false);
127240b96408SJohannes Berg 
127389afe614SJohannes Berg 	skb->dev = sdata->dev;
127489afe614SJohannes Berg 
127555de908aSJohannes Berg 	rcu_read_lock();
127655de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
127755de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
127855de908aSJohannes Berg 		rcu_read_unlock();
127955de908aSJohannes Berg 		kfree_skb(skb);
128055de908aSJohannes Berg 		return;
128155de908aSJohannes Berg 	}
128255de908aSJohannes Berg 
128373c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
12847c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
128555de908aSJohannes Berg 	rcu_read_unlock();
1286ce662b44SJohannes Berg }
1287ce662b44SJohannes Berg 
12880a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
12890a1cb809SJohannes Berg {
12900a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
12910a1cb809SJohannes Berg 	if (tids & 0xF8)
12920a1cb809SJohannes Berg 		return fls(tids) - 1;
12930a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
12940a1cb809SJohannes Berg 	if (tids & BIT(0))
12950a1cb809SJohannes Berg 		return 0;
12960a1cb809SJohannes Berg 	return fls(tids) - 1;
12970a1cb809SJohannes Berg }
12980a1cb809SJohannes Berg 
129947086fc5SJohannes Berg static void
130047086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
130147086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
130247086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1303af818581SJohannes Berg {
1304af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1305af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1306948d887dSJohannes Berg 	bool more_data = false;
1307948d887dSJohannes Berg 	int ac;
13084049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
130947086fc5SJohannes Berg 	struct sk_buff_head frames;
131047086fc5SJohannes Berg 
1311deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1312c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1313deeaee19SJohannes Berg 
131447086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1315af818581SJohannes Berg 
1316f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1317948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13184049e09aSJohannes Berg 		unsigned long tids;
13194049e09aSJohannes Berg 
132047086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1321948d887dSJohannes Berg 			continue;
1322948d887dSJohannes Berg 
13234049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13244049e09aSJohannes Berg 
1325f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1326f9f760b4SJohannes Berg 		 * release from hardware queues
1327f9f760b4SJohannes Berg 		 */
1328ba8c3d6fSFelix Fietkau 		if (skb_queue_empty(&frames)) {
1329f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1330ba8c3d6fSFelix Fietkau 			driver_release_tids |= sta->txq_buffered_tids & tids;
1331ba8c3d6fSFelix Fietkau 		}
1332f9f760b4SJohannes Berg 
13334049e09aSJohannes Berg 		if (driver_release_tids) {
1334f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1335f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1336f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1337f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1338f9f760b4SJohannes Berg 			 */
1339f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1340f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1341f9f760b4SJohannes Berg 				more_data = true;
1342f9f760b4SJohannes Berg 				driver_release_tids =
1343f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1344f9f760b4SJohannes Berg 						driver_release_tids));
1345f9f760b4SJohannes Berg 				break;
1346f9f760b4SJohannes Berg 			}
13474049e09aSJohannes Berg 		} else {
134847086fc5SJohannes Berg 			struct sk_buff *skb;
134947086fc5SJohannes Berg 
135047086fc5SJohannes Berg 			while (n_frames > 0) {
1351948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1352948d887dSJohannes Berg 				if (!skb) {
135347086fc5SJohannes Berg 					skb = skb_dequeue(
135447086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1355af818581SJohannes Berg 					if (skb)
1356af818581SJohannes Berg 						local->total_ps_buffered--;
1357af818581SJohannes Berg 				}
135847086fc5SJohannes Berg 				if (!skb)
135947086fc5SJohannes Berg 					break;
136047086fc5SJohannes Berg 				n_frames--;
136147086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
136247086fc5SJohannes Berg 			}
1363948d887dSJohannes Berg 		}
1364948d887dSJohannes Berg 
1365f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1366f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1367f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13684049e09aSJohannes Berg 		 */
1369948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1370948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1371948d887dSJohannes Berg 			more_data = true;
1372948d887dSJohannes Berg 			break;
1373948d887dSJohannes Berg 		}
1374948d887dSJohannes Berg 	}
1375af818581SJohannes Berg 
1376f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1377ce662b44SJohannes Berg 		int tid;
13784049e09aSJohannes Berg 
1379ce662b44SJohannes Berg 		/*
1380ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1381ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1382ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1383ce662b44SJohannes Berg 		 *
1384ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1385ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1386ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1387ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1388ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1389ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1390ce662b44SJohannes Berg 		 *
1391ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1392ce662b44SJohannes Berg 		 */
1393ce662b44SJohannes Berg 
1394ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1395ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1396ce662b44SJohannes Berg 
1397b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1398f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
139947086fc5SJohannes Berg 		struct sk_buff_head pending;
140047086fc5SJohannes Berg 		struct sk_buff *skb;
140140b96408SJohannes Berg 		int num = 0;
140240b96408SJohannes Berg 		u16 tids = 0;
1403b77cf4f8SJohannes Berg 		bool need_null = false;
140447086fc5SJohannes Berg 
140547086fc5SJohannes Berg 		skb_queue_head_init(&pending);
140647086fc5SJohannes Berg 
140747086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1408af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
140947086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
141040b96408SJohannes Berg 			u8 *qoshdr = NULL;
141140b96408SJohannes Berg 
141240b96408SJohannes Berg 			num++;
1413af818581SJohannes Berg 
1414af818581SJohannes Berg 			/*
141547086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
141647086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
141747086fc5SJohannes Berg 			 * exchange.
1418af818581SJohannes Berg 			 */
14196b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
14206b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1421af818581SJohannes Berg 
142247086fc5SJohannes Berg 			/*
142347086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
142447086fc5SJohannes Berg 			 * more buffered frames for this STA
142547086fc5SJohannes Berg 			 */
142624b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
142747086fc5SJohannes Berg 				hdr->frame_control |=
142847086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
142924b9c373SJanusz.Dziedzic@tieto.com 			else
143024b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
143124b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1432af818581SJohannes Berg 
143340b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
143440b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
143540b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
143640b96408SJohannes Berg 
1437b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1438b77cf4f8SJohannes Berg 
1439b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1440b77cf4f8SJohannes Berg 
1441b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1442b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1443b77cf4f8SJohannes Berg 				continue;
1444b77cf4f8SJohannes Berg 
1445b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1446b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1447b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1448b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1449b77cf4f8SJohannes Berg 				break;
1450b77cf4f8SJohannes Berg 			}
1451b77cf4f8SJohannes Berg 
1452b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1453b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1454b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1455b77cf4f8SJohannes Berg 			 * and be done.
1456b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1457b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1458b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1459b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1460b77cf4f8SJohannes Berg 			 *
1461b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1462b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1463b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1464b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1465b77cf4f8SJohannes Berg 			 *
1466b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1467b77cf4f8SJohannes Berg 			 */
1468b77cf4f8SJohannes Berg 			if (qoshdr) {
146940b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1470deeaee19SJohannes Berg 
147147086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
147247086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1473b77cf4f8SJohannes Berg 			} else {
1474b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1475b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1476b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1477b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1478b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1479b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1480b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1481b77cf4f8SJohannes Berg 				 */
1482b77cf4f8SJohannes Berg 				hdr->frame_control |=
1483b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1484b77cf4f8SJohannes Berg 				need_null = true;
1485b77cf4f8SJohannes Berg 				num++;
148652a3f20cSMarco Porsch 			}
1487b77cf4f8SJohannes Berg 			break;
148847086fc5SJohannes Berg 		}
148947086fc5SJohannes Berg 
149040b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
149140b96408SJohannes Berg 					  reason, more_data);
149240b96408SJohannes Berg 
149347086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1494af818581SJohannes Berg 
1495b77cf4f8SJohannes Berg 		if (need_null)
1496b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1497b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1498b77cf4f8SJohannes Berg 				reason, false);
1499b77cf4f8SJohannes Berg 
1500c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1501af818581SJohannes Berg 	} else {
1502ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1503ba8c3d6fSFelix Fietkau 		int tid;
1504ba8c3d6fSFelix Fietkau 
1505af818581SJohannes Berg 		/*
15064049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
15074049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1508f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1509f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1510f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1511f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1512f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1513f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1514af818581SJohannes Berg 		 */
15154049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
151647086fc5SJohannes Berg 					    n_frames, reason, more_data);
15174049e09aSJohannes Berg 
15184049e09aSJohannes Berg 		/*
15194049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
15204049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1521f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
15224049e09aSJohannes Berg 		 * release function.
1523f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1524ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1525ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
15264049e09aSJohannes Berg 		 */
1527ba8c3d6fSFelix Fietkau 
1528ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1529ba8c3d6fSFelix Fietkau 			return;
1530ba8c3d6fSFelix Fietkau 
1531ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1532ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1533ba8c3d6fSFelix Fietkau 
1534ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1535ba8c3d6fSFelix Fietkau 				continue;
1536ba8c3d6fSFelix Fietkau 
1537ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1538ba8c3d6fSFelix Fietkau 			break;
1539ba8c3d6fSFelix Fietkau 		}
1540af818581SJohannes Berg 	}
1541af818581SJohannes Berg }
1542af818581SJohannes Berg 
1543af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1544af818581SJohannes Berg {
154547086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1546af818581SJohannes Berg 
1547af818581SJohannes Berg 	/*
154847086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
154947086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
155047086fc5SJohannes Berg 	 * only from the non-enabled ones.
1551af818581SJohannes Berg 	 */
155247086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
155347086fc5SJohannes Berg 		ignore_for_response = 0;
1554af818581SJohannes Berg 
155547086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
155647086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1557af818581SJohannes Berg }
155847086fc5SJohannes Berg 
155947086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
156047086fc5SJohannes Berg {
156147086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
156247086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
156347086fc5SJohannes Berg 
156447086fc5SJohannes Berg 	/*
156547086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
156647086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
156747086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
156847086fc5SJohannes Berg 	 * actually getting called.
156947086fc5SJohannes Berg 	 */
157047086fc5SJohannes Berg 	if (!delivery_enabled)
157147086fc5SJohannes Berg 		return;
157247086fc5SJohannes Berg 
157347086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
157447086fc5SJohannes Berg 	case 1:
157547086fc5SJohannes Berg 		n_frames = 2;
157647086fc5SJohannes Berg 		break;
157747086fc5SJohannes Berg 	case 2:
157847086fc5SJohannes Berg 		n_frames = 4;
157947086fc5SJohannes Berg 		break;
158047086fc5SJohannes Berg 	case 3:
158147086fc5SJohannes Berg 		n_frames = 6;
158247086fc5SJohannes Berg 		break;
158347086fc5SJohannes Berg 	case 0:
158447086fc5SJohannes Berg 		/* XXX: what is a good value? */
158513a8098aSAndrei Otcheretianski 		n_frames = 128;
158647086fc5SJohannes Berg 		break;
158747086fc5SJohannes Berg 	}
158847086fc5SJohannes Berg 
158947086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
159047086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1591af818581SJohannes Berg }
1592af818581SJohannes Berg 
1593af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1594af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1595af818581SJohannes Berg {
1596af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1597af818581SJohannes Berg 
1598b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1599b5878a2dSJohannes Berg 
16005ac2e350SJohannes Berg 	if (block) {
1601c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
16025ac2e350SJohannes Berg 		return;
16035ac2e350SJohannes Berg 	}
16045ac2e350SJohannes Berg 
16055ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
16065ac2e350SJohannes Berg 		return;
16075ac2e350SJohannes Berg 
16085ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
16095ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
16105ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16115ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16125ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
16135ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
16145ac2e350SJohannes Berg 		/* must be asleep in this case */
16155ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16165ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16175ac2e350SJohannes Berg 	} else {
16185ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16195ac2e350SJohannes Berg 	}
1620af818581SJohannes Berg }
1621af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1622dcf55fb5SFelix Fietkau 
1623e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
162437fbd908SJohannes Berg {
162537fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
162637fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
162737fbd908SJohannes Berg 
162837fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
162937fbd908SJohannes Berg 
163037fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
163137fbd908SJohannes Berg }
1632e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
163337fbd908SJohannes Berg 
1634042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1635042ec453SJohannes Berg 				u8 tid, bool buffered)
1636dcf55fb5SFelix Fietkau {
1637dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1638dcf55fb5SFelix Fietkau 
16395a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1640042ec453SJohannes Berg 		return;
1641042ec453SJohannes Berg 
16421b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
16431b000789SJohannes Berg 
1644948d887dSJohannes Berg 	if (buffered)
1645948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1646948d887dSJohannes Berg 	else
1647948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1648948d887dSJohannes Berg 
1649c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1650dcf55fb5SFelix Fietkau }
1651042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1652d9a7ddb0SJohannes Berg 
165383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1654d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1655d9a7ddb0SJohannes Berg {
16568bf11d8dSJohannes Berg 	might_sleep();
1657d9a7ddb0SJohannes Berg 
1658d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1659d9a7ddb0SJohannes Berg 		return 0;
1660d9a7ddb0SJohannes Berg 
1661f09603a2SJohannes Berg 	/* check allowed transitions first */
1662f09603a2SJohannes Berg 
1663d9a7ddb0SJohannes Berg 	switch (new_state) {
1664d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1665f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1666d9a7ddb0SJohannes Berg 			return -EINVAL;
1667d9a7ddb0SJohannes Berg 		break;
1668d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1669f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1670f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1671d9a7ddb0SJohannes Berg 			return -EINVAL;
1672d9a7ddb0SJohannes Berg 		break;
1673d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1674f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1675f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1676d9a7ddb0SJohannes Berg 			return -EINVAL;
1677d9a7ddb0SJohannes Berg 		break;
1678d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1679f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1680d9a7ddb0SJohannes Berg 			return -EINVAL;
1681d9a7ddb0SJohannes Berg 		break;
1682d9a7ddb0SJohannes Berg 	default:
1683d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1684d9a7ddb0SJohannes Berg 		return -EINVAL;
1685d9a7ddb0SJohannes Berg 	}
1686d9a7ddb0SJohannes Berg 
1687bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1688bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1689f09603a2SJohannes Berg 
1690f09603a2SJohannes Berg 	/*
1691f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1692f09603a2SJohannes Berg 	 * fail the transition
1693f09603a2SJohannes Berg 	 */
1694f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1695f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1696f09603a2SJohannes Berg 					sta->sta_state, new_state);
1697f09603a2SJohannes Berg 		if (err)
1698f09603a2SJohannes Berg 			return err;
1699f09603a2SJohannes Berg 	}
1700f09603a2SJohannes Berg 
1701f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1702f09603a2SJohannes Berg 
1703f09603a2SJohannes Berg 	switch (new_state) {
1704f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1705f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1706f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1707f09603a2SJohannes Berg 		break;
1708f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1709f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1710f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1711f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1712f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1713f09603a2SJohannes Berg 		break;
1714f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1715f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1716f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1717f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
17187e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17197e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17207e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17217e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1722f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1723f09603a2SJohannes Berg 		}
1724f09603a2SJohannes Berg 		break;
1725f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1726f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
17277e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17287e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17297e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17307e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1731f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1732f09603a2SJohannes Berg 		}
1733f09603a2SJohannes Berg 		break;
1734f09603a2SJohannes Berg 	default:
1735f09603a2SJohannes Berg 		break;
1736f09603a2SJohannes Berg 	}
1737f09603a2SJohannes Berg 
1738d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1739d9a7ddb0SJohannes Berg 
1740d9a7ddb0SJohannes Berg 	return 0;
1741d9a7ddb0SJohannes Berg }
1742687da132SEmmanuel Grumbach 
1743687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1744687da132SEmmanuel Grumbach {
1745687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1746687da132SEmmanuel Grumbach 	u8 rx_streams;
1747687da132SEmmanuel Grumbach 
1748687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1749687da132SEmmanuel Grumbach 		return 1;
1750687da132SEmmanuel Grumbach 
1751687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1752687da132SEmmanuel Grumbach 		int i;
1753687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1754687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1755687da132SEmmanuel Grumbach 
1756687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1757687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1758687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1759687da132SEmmanuel Grumbach 				return i + 1;
1760687da132SEmmanuel Grumbach 	}
1761687da132SEmmanuel Grumbach 
1762687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1763687da132SEmmanuel Grumbach 		rx_streams = 4;
1764687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1765687da132SEmmanuel Grumbach 		rx_streams = 3;
1766687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1767687da132SEmmanuel Grumbach 		rx_streams = 2;
1768687da132SEmmanuel Grumbach 	else
1769687da132SEmmanuel Grumbach 		rx_streams = 1;
1770687da132SEmmanuel Grumbach 
1771687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1772687da132SEmmanuel Grumbach 		return rx_streams;
1773687da132SEmmanuel Grumbach 
1774687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1775687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1776687da132SEmmanuel Grumbach }
1777b7ffbd7eSJohannes Berg 
1778b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1779b7ffbd7eSJohannes Berg {
1780b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1781b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
17829a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1783b7ffbd7eSJohannes Berg 	struct timespec uptime;
1784b7ffbd7eSJohannes Berg 	u32 thr = 0;
1785b7ffbd7eSJohannes Berg 	int i, ac;
1786b7ffbd7eSJohannes Berg 
17879a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
17889a244409SJohn W. Linville 		ref = local->rate_ctrl;
17899a244409SJohn W. Linville 
1790b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1791b7ffbd7eSJohannes Berg 
1792225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1793225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1794225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1795225b8189SJohannes Berg 	 */
1796225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1797225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1798225b8189SJohannes Berg 
17992b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
18002b9a7e1bSJohannes Berg 
1801319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1802319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1803319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1804319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1805319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1806319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BEACON_LOSS);
1807b7ffbd7eSJohannes Berg 
180818171520SThomas Gleixner 	ktime_get_ts(&uptime);
1809b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1810b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
18112b9a7e1bSJohannes Berg 
1812319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1813319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1814b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
18152b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1816b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1817319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1818b7ffbd7eSJohannes Berg 	}
18192b9a7e1bSJohannes Berg 
1820319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
18212b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
18222b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
18232b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1824319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
18252b9a7e1bSJohannes Berg 	}
18262b9a7e1bSJohannes Berg 
1827319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1828319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1829b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1830319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
18312b9a7e1bSJohannes Berg 	}
18322b9a7e1bSJohannes Berg 
1833319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1834b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1835319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
18362b9a7e1bSJohannes Berg 	}
18372b9a7e1bSJohannes Berg 
1838319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1839b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1840319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
18412b9a7e1bSJohannes Berg 	}
18422b9a7e1bSJohannes Berg 
1843319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1844b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1845319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
18462b9a7e1bSJohannes Berg 	}
18472b9a7e1bSJohannes Berg 
1848b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1849b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1850b7ffbd7eSJohannes Berg 
1851225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1852225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1853225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1854225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1855225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1856225b8189SJohannes Berg 	}
1857225b8189SJohannes Berg 
1858b7ffbd7eSJohannes Berg 	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1859b7ffbd7eSJohannes Berg 	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1860319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1861b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1862319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1863b7ffbd7eSJohannes Berg 		}
18642b9a7e1bSJohannes Berg 
1865319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
18662b9a7e1bSJohannes Berg 			sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1867319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
18682b9a7e1bSJohannes Berg 		}
18692b9a7e1bSJohannes Berg 	}
18702b9a7e1bSJohannes Berg 
18712b9a7e1bSJohannes Berg 	if (sta->chains &&
1872319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1873319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1874319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1875319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1876b7ffbd7eSJohannes Berg 
1877b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1878b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1879b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1880b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1881b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1882b7ffbd7eSJohannes Berg 		}
1883b7ffbd7eSJohannes Berg 	}
1884b7ffbd7eSJohannes Berg 
1885319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1886b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1887319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
18882b9a7e1bSJohannes Berg 	}
18892b9a7e1bSJohannes Berg 
1890319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1891b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1892319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
18932b9a7e1bSJohannes Berg 	}
1894b7ffbd7eSJohannes Berg 
189579c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
189679c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
189779c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
189879c892b8SJohannes Berg 
189979c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
190079c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
190179c892b8SJohannes Berg 			tidstats->rx_msdu = sta->rx_msdu[i];
190279c892b8SJohannes Berg 		}
190379c892b8SJohannes Berg 
190479c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
190579c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
190679c892b8SJohannes Berg 			tidstats->tx_msdu = sta->tx_msdu[i];
190779c892b8SJohannes Berg 		}
190879c892b8SJohannes Berg 
190979c892b8SJohannes Berg 		if (!(tidstats->filled &
191079c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
191179c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
191279c892b8SJohannes Berg 			tidstats->filled |=
191379c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
191479c892b8SJohannes Berg 			tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
191579c892b8SJohannes Berg 		}
191679c892b8SJohannes Berg 
191779c892b8SJohannes Berg 		if (!(tidstats->filled &
191879c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
191979c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
192079c892b8SJohannes Berg 			tidstats->filled |=
192179c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
192279c892b8SJohannes Berg 			tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
192379c892b8SJohannes Berg 		}
192479c892b8SJohannes Berg 	}
192579c892b8SJohannes Berg 
1926b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1927b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1928319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1929319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
1930319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
1931319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
1932319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
1933319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
1934b7ffbd7eSJohannes Berg 
1935b7ffbd7eSJohannes Berg 		sinfo->llid = sta->llid;
1936b7ffbd7eSJohannes Berg 		sinfo->plid = sta->plid;
1937b7ffbd7eSJohannes Berg 		sinfo->plink_state = sta->plink_state;
1938b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1939319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1940b7ffbd7eSJohannes Berg 			sinfo->t_offset = sta->t_offset;
1941b7ffbd7eSJohannes Berg 		}
1942b7ffbd7eSJohannes Berg 		sinfo->local_pm = sta->local_pm;
1943b7ffbd7eSJohannes Berg 		sinfo->peer_pm = sta->peer_pm;
1944b7ffbd7eSJohannes Berg 		sinfo->nonpeer_pm = sta->nonpeer_pm;
1945b7ffbd7eSJohannes Berg #endif
1946b7ffbd7eSJohannes Berg 	}
1947b7ffbd7eSJohannes Berg 
1948b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1949b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1950b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1951b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1952b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1953b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1954b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1955785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1956b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1957b7ffbd7eSJohannes Berg 
1958b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1959b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1960b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1961b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1962b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1963b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1964b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1965b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1966b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1967b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1968b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1969b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1970a74a8c84SJohannes Berg 	if (sta->sta.wme)
1971b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1972b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
1973b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1974b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
1975b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1976b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
1977b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1978b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1979b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1980b7ffbd7eSJohannes Berg 
1981b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
1982b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
1983b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1984b7ffbd7eSJohannes Berg 	else
1985b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
1986b7ffbd7eSJohannes Berg 
1987b7ffbd7eSJohannes Berg 	if (thr != 0) {
1988319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
1989b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
1990b7ffbd7eSJohannes Berg 	}
1991b7ffbd7eSJohannes Berg }
1992