xref: /openbmc/linux/net/mac80211/sta_info.c (revision a7201a6c5ea0157e96d17240d185a538bfdb7038)
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
50ead2510SEmmanuel Grumbach  * Copyright (C) 2015 Intel Deutschland GmbH
6f0706e82SJiri Benc  *
7f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
8f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
9f0706e82SJiri Benc  * published by the Free Software Foundation.
10f0706e82SJiri Benc  */
11f0706e82SJiri Benc 
12f0706e82SJiri Benc #include <linux/module.h>
13f0706e82SJiri Benc #include <linux/init.h>
14888d04dfSFelix Fietkau #include <linux/etherdevice.h>
15f0706e82SJiri Benc #include <linux/netdevice.h>
16f0706e82SJiri Benc #include <linux/types.h>
17f0706e82SJiri Benc #include <linux/slab.h>
18f0706e82SJiri Benc #include <linux/skbuff.h>
19f0706e82SJiri Benc #include <linux/if_arp.h>
200d174406SJohannes Berg #include <linux/timer.h>
21d0709a65SJohannes Berg #include <linux/rtnetlink.h>
22f0706e82SJiri Benc 
23f0706e82SJiri Benc #include <net/mac80211.h>
24f0706e82SJiri Benc #include "ieee80211_i.h"
2524487981SJohannes Berg #include "driver-ops.h"
262c8dccc7SJohannes Berg #include "rate.h"
27f0706e82SJiri Benc #include "sta_info.h"
28e9f207f0SJiri Benc #include "debugfs_sta.h"
29ee385855SLuis Carlos Cobo #include "mesh.h"
30ce662b44SJohannes Berg #include "wme.h"
31f0706e82SJiri Benc 
32d0709a65SJohannes Berg /**
33d0709a65SJohannes Berg  * DOC: STA information lifetime rules
34d0709a65SJohannes Berg  *
35d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
36d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
37d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
38d0709a65SJohannes Berg  *
3934e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
4034e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4134e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4234e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4334e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4434e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4534e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4634e89507SJohannes Berg  * encryption keys.
4793e5deb1SJohannes Berg  *
4893e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4993e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
50d0709a65SJohannes Berg  *
5134e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
527e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
537e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5425985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5534e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5625985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5734e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
587e189a12SLuis R. Rodriguez  *
5934e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
6034e89507SJohannes Berg  * calls are available.
61d0709a65SJohannes Berg  *
6234e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6334e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6434e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6534e89507SJohannes Berg  * freed before they are done using it.
66d0709a65SJohannes Berg  */
67f0706e82SJiri Benc 
687bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
697bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
70caf22d31SJohannes Berg 	.automatic_shrinking = true,
717bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
72ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
737bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
747bedd0cfSJohannes Berg 	.hashfn = sta_addr_hash,
75ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
767bedd0cfSJohannes Berg };
777bedd0cfSJohannes Berg 
784d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
79be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
80f0706e82SJiri Benc 			     struct sta_info *sta)
81f0706e82SJiri Benc {
827bedd0cfSJohannes Berg 	return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
837bedd0cfSJohannes Berg 				      sta_rht_params);
84f0706e82SJiri Benc }
85f0706e82SJiri Benc 
865108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
87b22cfcfcSEliad Peller {
88b22cfcfcSEliad Peller 	int ac, i;
89b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
90b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
91b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
92d012a605SMarco Porsch 	struct ps_data *ps;
93b22cfcfcSEliad Peller 
94e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
955ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
965ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
97d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
98d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
99d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1003f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1013f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
102d012a605SMarco Porsch 		else
103d012a605SMarco Porsch 			return;
104b22cfcfcSEliad Peller 
105b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
106e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1075ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
108b22cfcfcSEliad Peller 
109d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
110b22cfcfcSEliad Peller 	}
111b22cfcfcSEliad Peller 
112ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
113ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
114ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
115ba8c3d6fSFelix Fietkau 			int n = skb_queue_len(&txqi->queue);
116ba8c3d6fSFelix Fietkau 
117ba8c3d6fSFelix Fietkau 			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
118ba8c3d6fSFelix Fietkau 			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
119ba8c3d6fSFelix Fietkau 		}
120ba8c3d6fSFelix Fietkau 	}
121ba8c3d6fSFelix Fietkau 
122b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
123b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1241f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1251f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
126b22cfcfcSEliad Peller 	}
127b22cfcfcSEliad Peller 
12845b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12945b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
130b22cfcfcSEliad Peller 
1315ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
132b22cfcfcSEliad Peller 
133b22cfcfcSEliad Peller 	/*
134b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
135b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
136b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
137b22cfcfcSEliad Peller 	 * directly by station destruction.
138b22cfcfcSEliad Peller 	 */
1395a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
140661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
141b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
142b22cfcfcSEliad Peller 		if (!tid_tx)
143b22cfcfcSEliad Peller 			continue;
1441f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
145b22cfcfcSEliad Peller 		kfree(tid_tx);
146b22cfcfcSEliad Peller 	}
1475108ca82SJohannes Berg }
148b22cfcfcSEliad Peller 
1495108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1505108ca82SJohannes Berg {
1515108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1525108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1535108ca82SJohannes Berg 
1545108ca82SJohannes Berg 	__cleanup_single_sta(sta);
155b22cfcfcSEliad Peller 	sta_info_free(local, sta);
156b22cfcfcSEliad Peller }
157b22cfcfcSEliad Peller 
158d0709a65SJohannes Berg /* protected by RCU */
159abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
160abe60632SJohannes Berg 			      const u8 *addr)
16143ba7e95SJohannes Berg {
162abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16360f4b626SJohannes Berg 	struct sta_info *sta;
16460f4b626SJohannes Berg 	struct rhash_head *tmp;
16560f4b626SJohannes Berg 	const struct bucket_table *tbl;
16643ba7e95SJohannes Berg 
16760f4b626SJohannes Berg 	rcu_read_lock();
16860f4b626SJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
16960f4b626SJohannes Berg 
17060f4b626SJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
17160f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17260f4b626SJohannes Berg 			rcu_read_unlock();
17360f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17460f4b626SJohannes Berg 			 * another rcu read section or the mutex
17560f4b626SJohannes Berg 			 */
17660f4b626SJohannes Berg 			return sta;
17760f4b626SJohannes Berg 		}
17860f4b626SJohannes Berg 	}
17960f4b626SJohannes Berg 	rcu_read_unlock();
18060f4b626SJohannes Berg 	return NULL;
18143ba7e95SJohannes Berg }
18243ba7e95SJohannes Berg 
1830e5ded5aSFelix Fietkau /*
1840e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1850e5ded5aSFelix Fietkau  * or from one of its vlans
1860e5ded5aSFelix Fietkau  */
1870e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1880e5ded5aSFelix Fietkau 				  const u8 *addr)
1890e5ded5aSFelix Fietkau {
1900e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1910e5ded5aSFelix Fietkau 	struct sta_info *sta;
1927bedd0cfSJohannes Berg 	struct rhash_head *tmp;
1937bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
1940e5ded5aSFelix Fietkau 
1957bedd0cfSJohannes Berg 	rcu_read_lock();
1967bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1977bedd0cfSJohannes Berg 
1987bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1997bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2007bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2017bedd0cfSJohannes Berg 			rcu_read_unlock();
2027bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2037bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2047bedd0cfSJohannes Berg 			 */
2050e5ded5aSFelix Fietkau 			return sta;
2060e5ded5aSFelix Fietkau 		}
2077bedd0cfSJohannes Berg 	}
2087bedd0cfSJohannes Berg 	rcu_read_unlock();
2097bedd0cfSJohannes Berg 	return NULL;
2107bedd0cfSJohannes Berg }
2110e5ded5aSFelix Fietkau 
2123b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2133b53fde8SJohannes Berg 				     int idx)
214ee385855SLuis Carlos Cobo {
2153b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
216ee385855SLuis Carlos Cobo 	struct sta_info *sta;
217ee385855SLuis Carlos Cobo 	int i = 0;
218ee385855SLuis Carlos Cobo 
219d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2203b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2212a8ca29aSLuis Carlos Cobo 			continue;
222ee385855SLuis Carlos Cobo 		if (i < idx) {
223ee385855SLuis Carlos Cobo 			++i;
224ee385855SLuis Carlos Cobo 			continue;
225ee385855SLuis Carlos Cobo 		}
2262a8ca29aSLuis Carlos Cobo 		return sta;
227ee385855SLuis Carlos Cobo 	}
228ee385855SLuis Carlos Cobo 
229ee385855SLuis Carlos Cobo 	return NULL;
230ee385855SLuis Carlos Cobo }
231f0706e82SJiri Benc 
23293e5deb1SJohannes Berg /**
233d9a7ddb0SJohannes Berg  * sta_info_free - free STA
23493e5deb1SJohannes Berg  *
2356ef307bcSRandy Dunlap  * @local: pointer to the global information
23693e5deb1SJohannes Berg  * @sta: STA info to free
23793e5deb1SJohannes Berg  *
23893e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
239d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
240d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
241d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
24293e5deb1SJohannes Berg  */
243d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
24493e5deb1SJohannes Berg {
245889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2464b7679a5SJohannes Berg 		rate_control_free_sta(sta);
24793e5deb1SJohannes Berg 
248bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
24993e5deb1SJohannes Berg 
250ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
251ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
25253d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
253433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
254433f5bc1SJohannes Berg 	kfree(sta->mesh);
255433f5bc1SJohannes Berg #endif
25693e5deb1SJohannes Berg 	kfree(sta);
25793e5deb1SJohannes Berg }
25893e5deb1SJohannes Berg 
2594d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
260d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
261d0709a65SJohannes Berg 			      struct sta_info *sta)
262f0706e82SJiri Benc {
2637bedd0cfSJohannes Berg 	rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
2647bedd0cfSJohannes Berg 			       sta_rht_params);
265f0706e82SJiri Benc }
266f0706e82SJiri Benc 
2675ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
268af818581SJohannes Berg {
269af818581SJohannes Berg 	struct sta_info *sta;
270af818581SJohannes Berg 
2715ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
272af818581SJohannes Berg 
273af818581SJohannes Berg 	if (sta->dead)
274af818581SJohannes Berg 		return;
275af818581SJohannes Berg 
27654420473SHelmut Schaa 	local_bh_disable();
2775ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
278af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2795ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
280af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2815ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
28247086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
283ce662b44SJohannes Berg 	local_bh_enable();
284af818581SJohannes Berg }
285af818581SJohannes Berg 
286af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
287af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
288af65cd96SJohannes Berg {
28930686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
290af65cd96SJohannes Berg 		return 0;
291af65cd96SJohannes Berg 
292889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
293af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29435c347acSJohannes Berg 						     sta, gfp);
295889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
296af65cd96SJohannes Berg 		return -ENOMEM;
297af65cd96SJohannes Berg 
298af65cd96SJohannes Berg 	return 0;
299af65cd96SJohannes Berg }
300af65cd96SJohannes Berg 
30173651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30256544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
303f0706e82SJiri Benc {
304d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
305ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
306f0706e82SJiri Benc 	struct sta_info *sta;
30716c5f15cSRon Rindjunsky 	int i;
308f0706e82SJiri Benc 
309ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
310f0706e82SJiri Benc 	if (!sta)
31173651ee6SJohannes Berg 		return NULL;
312f0706e82SJiri Benc 
31307346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3141d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3155ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
31667c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
317a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
31887f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
319433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
320433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
321433f5bc1SJohannes Berg 		if (!sta->mesh)
322433f5bc1SJohannes Berg 			goto free;
323433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
32487f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
32587f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
326433f5bc1SJohannes Berg 			init_timer(&sta->mesh->plink_timer);
327433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
328433f5bc1SJohannes Berg 	}
32987f59c70SThomas Pedersen #endif
33007346f81SJohannes Berg 
331ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
33217741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
333d0709a65SJohannes Berg 	sta->local = local;
334d0709a65SJohannes Berg 	sta->sdata = sdata;
335e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
336f0706e82SJiri Benc 
33771ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
33871ec375cSJohannes Berg 
339b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
340b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
341b6da911bSLiad Kaufman 
34284b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
343e5a9f8d0SJohannes Berg 	ewma_signal_init(&sta->rx_stats.avg_signal);
344e5a9f8d0SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++)
345e5a9f8d0SJohannes Berg 		ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]);
346541a45a1SBruno Randolf 
347ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
348ba8c3d6fSFelix Fietkau 		void *txq_data;
349ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
350ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
351ba8c3d6fSFelix Fietkau 
352ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
353ba8c3d6fSFelix Fietkau 		if (!txq_data)
354ba8c3d6fSFelix Fietkau 			goto free;
355ba8c3d6fSFelix Fietkau 
356ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
357ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
358ba8c3d6fSFelix Fietkau 
359ba8c3d6fSFelix Fietkau 			ieee80211_init_tx_queue(sdata, sta, txq, i);
360abfbc3afSJohannes Berg 		}
361ba8c3d6fSFelix Fietkau 	}
362ba8c3d6fSFelix Fietkau 
363ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
364ba8c3d6fSFelix Fietkau 		goto free_txq;
365f0706e82SJiri Benc 
3665a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
367a622ab72SJohannes Berg 		/*
368a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
369a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
370a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
371a622ab72SJohannes Berg 		 */
37216c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
37316c5f15cSRon Rindjunsky 	}
374948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
375948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
376948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
377948d887dSJohannes Berg 	}
37873651ee6SJohannes Berg 
3795a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3804be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
381cccaec98SSenthil Balasubramanian 
382af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
383687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
384687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
385687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
386ba8c3d6fSFelix Fietkau 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
387687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
388687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
389687da132SEmmanuel Grumbach 		/*
390687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
391687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
392687da132SEmmanuel Grumbach 		 */
393687da132SEmmanuel Grumbach 		switch (smps) {
394687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
395687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
396687da132SEmmanuel Grumbach 			break;
397687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
398687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
399687da132SEmmanuel Grumbach 			break;
400687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
401687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
402687da132SEmmanuel Grumbach 			break;
403687da132SEmmanuel Grumbach 		default:
404687da132SEmmanuel Grumbach 			WARN_ON(1);
405687da132SEmmanuel Grumbach 		}
406687da132SEmmanuel Grumbach 	}
407af0ed69bSJohannes Berg 
408bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
409ef04a297SJohannes Berg 
410abfbc3afSJohannes Berg 	return sta;
411ba8c3d6fSFelix Fietkau 
412ba8c3d6fSFelix Fietkau free_txq:
413ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
414ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
415ba8c3d6fSFelix Fietkau free:
416433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
417433f5bc1SJohannes Berg 	kfree(sta->mesh);
418433f5bc1SJohannes Berg #endif
419ba8c3d6fSFelix Fietkau 	kfree(sta);
420ba8c3d6fSFelix Fietkau 	return NULL;
42173651ee6SJohannes Berg }
42273651ee6SJohannes Berg 
4238c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
42434e89507SJohannes Berg {
42534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42634e89507SJohannes Berg 
42703e4497eSJohannes Berg 	/*
42803e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
42903e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
43003e4497eSJohannes Berg 	 * and another CPU turns off the net device.
43103e4497eSJohannes Berg 	 */
4328c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4338c71df7aSGuy Eilam 		return -ENETDOWN;
43403e4497eSJohannes Berg 
435b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4368c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4378c71df7aSGuy Eilam 		return -EINVAL;
4388c71df7aSGuy Eilam 
43931104891SJohannes Berg 	/* Strictly speaking this isn't necessary as we hold the mutex, but
44031104891SJohannes Berg 	 * the rhashtable code can't really deal with that distinction. We
44131104891SJohannes Berg 	 * do require the mutex for correctness though.
44231104891SJohannes Berg 	 */
44331104891SJohannes Berg 	rcu_read_lock();
44431104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
44531104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
44631104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
44731104891SJohannes Berg 		rcu_read_unlock();
44831104891SJohannes Berg 		return -ENOTUNIQ;
44931104891SJohannes Berg 	}
45031104891SJohannes Berg 	rcu_read_unlock();
45131104891SJohannes Berg 
4528c71df7aSGuy Eilam 	return 0;
45393e5deb1SJohannes Berg }
45444213b5eSJohannes Berg 
455f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
456f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
457f09603a2SJohannes Berg 				     struct sta_info *sta)
458f09603a2SJohannes Berg {
459f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
460f09603a2SJohannes Berg 	int err = 0;
461f09603a2SJohannes Berg 
462f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
463f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
464f09603a2SJohannes Berg 		if (err)
465f09603a2SJohannes Berg 			break;
466f09603a2SJohannes Berg 	}
467f09603a2SJohannes Berg 
468f09603a2SJohannes Berg 	if (!err) {
469a4ec45a4SJohannes Berg 		/*
470a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
471a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
472a4ec45a4SJohannes Berg 		 */
473a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
474f09603a2SJohannes Berg 			sta->uploaded = true;
475f09603a2SJohannes Berg 		return 0;
476f09603a2SJohannes Berg 	}
477f09603a2SJohannes Berg 
478f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
479bdcbd8e0SJohannes Berg 		sdata_info(sdata,
480bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
481bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
482f09603a2SJohannes Berg 		err = 0;
483f09603a2SJohannes Berg 	}
484f09603a2SJohannes Berg 
485f09603a2SJohannes Berg 	/* unwind on error */
486f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
487f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
488f09603a2SJohannes Berg 
489f09603a2SJohannes Berg 	return err;
490f09603a2SJohannes Berg }
491f09603a2SJohannes Berg 
49234e89507SJohannes Berg /*
4938c71df7aSGuy Eilam  * should be called with sta_mtx locked
4948c71df7aSGuy Eilam  * this function replaces the mutex lock
4958c71df7aSGuy Eilam  * with a RCU lock
4968c71df7aSGuy Eilam  */
4974d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4988c71df7aSGuy Eilam {
4998c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5008c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5017852e361SJohannes Berg 	struct station_info sinfo;
5028c71df7aSGuy Eilam 	int err = 0;
5038c71df7aSGuy Eilam 
5048c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
5058c71df7aSGuy Eilam 
5067852e361SJohannes Berg 	/* check if STA exists already */
5077852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
5084d33960bSJohannes Berg 		err = -EEXIST;
5094d33960bSJohannes Berg 		goto out_err;
51034e89507SJohannes Berg 	}
51134e89507SJohannes Berg 
5124d33960bSJohannes Berg 	local->num_sta++;
5134d33960bSJohannes Berg 	local->sta_generation++;
5144d33960bSJohannes Berg 	smp_mb();
5154d33960bSJohannes Berg 
5165108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5175108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5185108ca82SJohannes Berg 
5194d33960bSJohannes Berg 	/* make the station visible */
5204d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5214d33960bSJohannes Berg 
5222bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
52383d5cc01SJohannes Berg 
5245108ca82SJohannes Berg 	/* notify driver */
5255108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5265108ca82SJohannes Berg 	if (err)
5275108ca82SJohannes Berg 		goto out_remove;
5285108ca82SJohannes Berg 
52983d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5305108ca82SJohannes Berg 	/* accept BA sessions now */
5315108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5324d33960bSJohannes Berg 
5334d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5344d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5354d33960bSJohannes Berg 
5364d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5374d33960bSJohannes Berg 	sinfo.filled = 0;
5384d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5394d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
540d0709a65SJohannes Berg 
541bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
542f0706e82SJiri Benc 
54334e89507SJohannes Berg 	/* move reference to rcu-protected */
54434e89507SJohannes Berg 	rcu_read_lock();
54534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
546e9f207f0SJiri Benc 
54773651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
54873651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
54973651ee6SJohannes Berg 
55073651ee6SJohannes Berg 	return 0;
5515108ca82SJohannes Berg  out_remove:
5525108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5535108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5545108ca82SJohannes Berg 	local->num_sta--;
5555108ca82SJohannes Berg 	synchronize_net();
5565108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5574d33960bSJohannes Berg  out_err:
5584d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5594d33960bSJohannes Berg 	rcu_read_lock();
5604d33960bSJohannes Berg 	return err;
5618c71df7aSGuy Eilam }
5628c71df7aSGuy Eilam 
5638c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5648c71df7aSGuy Eilam {
5658c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
566308f7fcfSZhao, Gang 	int err;
5678c71df7aSGuy Eilam 
5684d33960bSJohannes Berg 	might_sleep();
5694d33960bSJohannes Berg 
57031104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
57131104891SJohannes Berg 
5728c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5738c71df7aSGuy Eilam 	if (err) {
57431104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
5758c71df7aSGuy Eilam 		rcu_read_lock();
5768c71df7aSGuy Eilam 		goto out_free;
5778c71df7aSGuy Eilam 	}
5788c71df7aSGuy Eilam 
5794d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5808c71df7aSGuy Eilam 	if (err)
581004c872eSJohannes Berg 		goto out_free;
582d0709a65SJohannes Berg 
583004c872eSJohannes Berg 	return 0;
58493e5deb1SJohannes Berg  out_free:
585d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
58693e5deb1SJohannes Berg 	return err;
587f0706e82SJiri Benc }
588f0706e82SJiri Benc 
58934e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
59034e89507SJohannes Berg {
59134e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
59234e89507SJohannes Berg 
59334e89507SJohannes Berg 	rcu_read_unlock();
59434e89507SJohannes Berg 
59534e89507SJohannes Berg 	return err;
59634e89507SJohannes Berg }
59734e89507SJohannes Berg 
598d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
599004c872eSJohannes Berg {
600004c872eSJohannes Berg 	/*
601004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
602004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
603004c872eSJohannes Berg 	 */
604d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
605004c872eSJohannes Berg }
606004c872eSJohannes Berg 
607d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
608004c872eSJohannes Berg {
609004c872eSJohannes Berg 	/*
610004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
611004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
612004c872eSJohannes Berg 	 */
613d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
614004c872eSJohannes Berg }
615004c872eSJohannes Berg 
6163d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6173d5839b6SIlan Peer {
6183d5839b6SIlan Peer 	/*
6193d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6203d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6213d5839b6SIlan Peer 	 */
6223d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6233d5839b6SIlan Peer }
6243d5839b6SIlan Peer 
625948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
626004c872eSJohannes Berg {
627948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
628948d887dSJohannes Berg 	switch (ac) {
629948d887dSJohannes Berg 	case IEEE80211_AC_VO:
630948d887dSJohannes Berg 		return BIT(6) | BIT(7);
631948d887dSJohannes Berg 	case IEEE80211_AC_VI:
632948d887dSJohannes Berg 		return BIT(4) | BIT(5);
633948d887dSJohannes Berg 	case IEEE80211_AC_BE:
634948d887dSJohannes Berg 		return BIT(0) | BIT(3);
635948d887dSJohannes Berg 	case IEEE80211_AC_BK:
636948d887dSJohannes Berg 		return BIT(1) | BIT(2);
637948d887dSJohannes Berg 	default:
638948d887dSJohannes Berg 		WARN_ON(1);
639948d887dSJohannes Berg 		return 0;
640d0709a65SJohannes Berg 	}
641004c872eSJohannes Berg }
642004c872eSJohannes Berg 
6439b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
644004c872eSJohannes Berg {
645c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
646d012a605SMarco Porsch 	struct ps_data *ps;
647948d887dSJohannes Berg 	bool indicate_tim = false;
648948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
649948d887dSJohannes Berg 	int ac;
650a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
651004c872eSJohannes Berg 
652d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
653d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
654c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
655c868cb35SJohannes Berg 			return;
6563e122be0SJohannes Berg 
657d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
6583f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6593f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6603f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
6613f52b7e3SMarco Porsch #endif
662d012a605SMarco Porsch 	} else {
663d012a605SMarco Porsch 		return;
664d012a605SMarco Porsch 	}
665d012a605SMarco Porsch 
666c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
66730686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
668c868cb35SJohannes Berg 		return;
669004c872eSJohannes Berg 
670c868cb35SJohannes Berg 	if (sta->dead)
671c868cb35SJohannes Berg 		goto done;
6723e122be0SJohannes Berg 
673948d887dSJohannes Berg 	/*
674948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
675948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
676948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
677948d887dSJohannes Berg 	 * non-enabled ones.
678948d887dSJohannes Berg 	 */
679948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
680948d887dSJohannes Berg 		ignore_for_tim = 0;
681948d887dSJohannes Berg 
6829b7a86f3SJohannes Berg 	if (ignore_pending)
6839b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6849b7a86f3SJohannes Berg 
685948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
686948d887dSJohannes Berg 		unsigned long tids;
687948d887dSJohannes Berg 
688948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
689948d887dSJohannes Berg 			continue;
690948d887dSJohannes Berg 
691948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
692948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
693948d887dSJohannes Berg 		if (indicate_tim)
694948d887dSJohannes Berg 			break;
695948d887dSJohannes Berg 
696948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
697948d887dSJohannes Berg 
698948d887dSJohannes Berg 		indicate_tim |=
699948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
700ba8c3d6fSFelix Fietkau 		indicate_tim |=
701ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
702004c872eSJohannes Berg 	}
703004c872eSJohannes Berg 
704c868cb35SJohannes Berg  done:
70565f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
706004c872eSJohannes Berg 
7073d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
7083d5839b6SIlan Peer 		goto out_unlock;
7093d5839b6SIlan Peer 
710948d887dSJohannes Berg 	if (indicate_tim)
711d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
712c868cb35SJohannes Berg 	else
713d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7143e122be0SJohannes Berg 
7159b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
716c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
717948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
718c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
719004c872eSJohannes Berg 	}
720004c872eSJohannes Berg 
7213d5839b6SIlan Peer out_unlock:
72265f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
723004c872eSJohannes Berg }
724004c872eSJohannes Berg 
7259b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7269b7a86f3SJohannes Berg {
7279b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7289b7a86f3SJohannes Berg }
7299b7a86f3SJohannes Berg 
730cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
731f0706e82SJiri Benc {
732e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
733f0706e82SJiri Benc 	int timeout;
734f0706e82SJiri Benc 
735f0706e82SJiri Benc 	if (!skb)
736cd0b8d89SJohannes Berg 		return false;
737f0706e82SJiri Benc 
738e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
739f0706e82SJiri Benc 
740f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
74157c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
74257c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
74357c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
744f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
745f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
746e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
747f0706e82SJiri Benc }
748f0706e82SJiri Benc 
749f0706e82SJiri Benc 
750948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
751948d887dSJohannes Berg 						struct sta_info *sta, int ac)
752f0706e82SJiri Benc {
753f0706e82SJiri Benc 	unsigned long flags;
754f0706e82SJiri Benc 	struct sk_buff *skb;
755f0706e82SJiri Benc 
75660750397SJohannes Berg 	/*
75760750397SJohannes Berg 	 * First check for frames that should expire on the filtered
75860750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
75960750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
76060750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
76160750397SJohannes Berg 	 * total_ps_buffered counter.
76260750397SJohannes Berg 	 */
763f0706e82SJiri Benc 	for (;;) {
764948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
765948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
76657c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
767948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
768836341a7SJohannes Berg 		else
769f0706e82SJiri Benc 			skb = NULL;
770948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
771f0706e82SJiri Benc 
77260750397SJohannes Berg 		/*
77360750397SJohannes Berg 		 * Frames are queued in order, so if this one
77460750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
77560750397SJohannes Berg 		 * we actually reached the end of the queue we
77660750397SJohannes Berg 		 * also need to stop, of course.
77760750397SJohannes Berg 		 */
77860750397SJohannes Berg 		if (!skb)
77960750397SJohannes Berg 			break;
780d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
78160750397SJohannes Berg 	}
78260750397SJohannes Berg 
78360750397SJohannes Berg 	/*
78460750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
78560750397SJohannes Berg 	 * only find something if the filtered queue was emptied
78660750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
78760750397SJohannes Berg 	 * buffered frames.
78860750397SJohannes Berg 	 */
789f0706e82SJiri Benc 	for (;;) {
790948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
791948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
792f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
793948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
794f0706e82SJiri Benc 		else
795f0706e82SJiri Benc 			skb = NULL;
796948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
797f0706e82SJiri Benc 
79860750397SJohannes Berg 		/*
79960750397SJohannes Berg 		 * frames are queued in order, so if this one
80060750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
80160750397SJohannes Berg 		 * the queue) we can stop testing
80260750397SJohannes Berg 		 */
803836341a7SJohannes Berg 		if (!skb)
804836341a7SJohannes Berg 			break;
805836341a7SJohannes Berg 
806f0706e82SJiri Benc 		local->total_ps_buffered--;
807bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
808bdcbd8e0SJohannes Berg 		       sta->sta.addr);
809d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
810f0706e82SJiri Benc 	}
8113393a608SJuuso Oikarinen 
81260750397SJohannes Berg 	/*
81360750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
81460750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
81560750397SJohannes Berg 	 * frames.
81660750397SJohannes Berg 	 */
81760750397SJohannes Berg 	sta_info_recalc_tim(sta);
81860750397SJohannes Berg 
81960750397SJohannes Berg 	/*
82060750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
82160750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
82260750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
82360750397SJohannes Berg 	 */
824948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
825948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
826948d887dSJohannes Berg }
827948d887dSJohannes Berg 
828948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
829948d887dSJohannes Berg 					     struct sta_info *sta)
830948d887dSJohannes Berg {
831948d887dSJohannes Berg 	bool have_buffered = false;
832948d887dSJohannes Berg 	int ac;
833948d887dSJohannes Berg 
8343f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8353f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8363f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
837948d887dSJohannes Berg 		return false;
838948d887dSJohannes Berg 
839948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
840948d887dSJohannes Berg 		have_buffered |=
841948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
842948d887dSJohannes Berg 
843948d887dSJohannes Berg 	return have_buffered;
844f0706e82SJiri Benc }
845f0706e82SJiri Benc 
846d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
84734e89507SJohannes Berg {
84834e89507SJohannes Berg 	struct ieee80211_local *local;
84934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8506d10e46bSJohannes Berg 	int ret;
85134e89507SJohannes Berg 
85234e89507SJohannes Berg 	might_sleep();
85334e89507SJohannes Berg 
85434e89507SJohannes Berg 	if (!sta)
85534e89507SJohannes Berg 		return -ENOENT;
85634e89507SJohannes Berg 
85734e89507SJohannes Berg 	local = sta->local;
85834e89507SJohannes Berg 	sdata = sta->sdata;
85934e89507SJohannes Berg 
86083d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
86183d5cc01SJohannes Berg 
862098a6070SJohannes Berg 	/*
863098a6070SJohannes Berg 	 * Before removing the station from the driver and
864098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
865098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
866098a6070SJohannes Berg 	 * will be sufficient.
867098a6070SJohannes Berg 	 */
868c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
869c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
870098a6070SJohannes Berg 
87134e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
872b01711beSJohannes Berg 	if (WARN_ON(ret))
87334e89507SJohannes Berg 		return ret;
87434e89507SJohannes Berg 
875a7a6bdd0SArik Nemtsov 	/*
876a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
877a7a6bdd0SArik Nemtsov 	 * removal.
878a7a6bdd0SArik Nemtsov 	 */
879a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
880a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
881a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
882a7a6bdd0SArik Nemtsov 	}
883a7a6bdd0SArik Nemtsov 
884794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
885ef044763SEliad Peller 	sta->removed = true;
8864d33960bSJohannes Berg 
8876a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8886a9d1b91SJohannes Berg 
889a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
890a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
891a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
892a710c816SJohannes Berg 
893d778207bSJohannes Berg 	return 0;
894d778207bSJohannes Berg }
895d778207bSJohannes Berg 
896d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
897d778207bSJohannes Berg {
898d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
899d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
9006f7a8d26SJohannes Berg 	struct station_info sinfo = {};
901d778207bSJohannes Berg 	int ret;
902d778207bSJohannes Berg 
903d778207bSJohannes Berg 	/*
904d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
905d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
906d778207bSJohannes Berg 	 */
907d778207bSJohannes Berg 
908d778207bSJohannes Berg 	might_sleep();
909d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
910d778207bSJohannes Berg 
911c8782078SJohannes Berg 	/* now keys can no longer be reached */
9126d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
91334e89507SJohannes Berg 
9149b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9159b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9169b7a86f3SJohannes Berg 
91734e89507SJohannes Berg 	sta->dead = true;
91834e89507SJohannes Berg 
91934e89507SJohannes Berg 	local->num_sta--;
92034e89507SJohannes Berg 	local->sta_generation++;
92134e89507SJohannes Berg 
92283d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
923f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
924f09603a2SJohannes Berg 		if (ret) {
92583d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
92683d5cc01SJohannes Berg 			break;
92783d5cc01SJohannes Berg 		}
92883d5cc01SJohannes Berg 	}
929d9a7ddb0SJohannes Berg 
930f09603a2SJohannes Berg 	if (sta->uploaded) {
931f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
932f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
933f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
934f09603a2SJohannes Berg 	}
93534e89507SJohannes Berg 
936bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
937bdcbd8e0SJohannes Berg 
9386f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
9396f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
940ec15e68bSJouni Malinen 
94134e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
94234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
94334e89507SJohannes Berg 
944d34ba216SJohannes Berg 	cleanup_single_sta(sta);
945d778207bSJohannes Berg }
946d778207bSJohannes Berg 
947d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
948d778207bSJohannes Berg {
949d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
950d778207bSJohannes Berg 
951d778207bSJohannes Berg 	if (err)
952d778207bSJohannes Berg 		return err;
953d778207bSJohannes Berg 
954d778207bSJohannes Berg 	synchronize_net();
955d778207bSJohannes Berg 
956d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
95734e89507SJohannes Berg 
95834e89507SJohannes Berg 	return 0;
95934e89507SJohannes Berg }
96034e89507SJohannes Berg 
96134e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
96234e89507SJohannes Berg {
96334e89507SJohannes Berg 	struct sta_info *sta;
96434e89507SJohannes Berg 	int ret;
96534e89507SJohannes Berg 
96634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9677852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
96834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
96934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
97034e89507SJohannes Berg 
97134e89507SJohannes Berg 	return ret;
97234e89507SJohannes Berg }
97334e89507SJohannes Berg 
97434e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
97534e89507SJohannes Berg 			      const u8 *addr)
97634e89507SJohannes Berg {
97734e89507SJohannes Berg 	struct sta_info *sta;
97834e89507SJohannes Berg 	int ret;
97934e89507SJohannes Berg 
98034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9817852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
98234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
98334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
98434e89507SJohannes Berg 
98534e89507SJohannes Berg 	return ret;
98634e89507SJohannes Berg }
987f0706e82SJiri Benc 
988f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
989f0706e82SJiri Benc {
990f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
991f0706e82SJiri Benc 	struct sta_info *sta;
9923393a608SJuuso Oikarinen 	bool timer_needed = false;
993f0706e82SJiri Benc 
994d0709a65SJohannes Berg 	rcu_read_lock();
995d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9963393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9973393a608SJuuso Oikarinen 			timer_needed = true;
998d0709a65SJohannes Berg 	rcu_read_unlock();
999f0706e82SJiri Benc 
10005bb644a0SJohannes Berg 	if (local->quiescing)
10015bb644a0SJohannes Berg 		return;
10025bb644a0SJohannes Berg 
10033393a608SJuuso Oikarinen 	if (!timer_needed)
10043393a608SJuuso Oikarinen 		return;
10053393a608SJuuso Oikarinen 
100626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
100726d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1008f0706e82SJiri Benc }
1009f0706e82SJiri Benc 
10107bedd0cfSJohannes Berg u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1011f0706e82SJiri Benc {
10127bedd0cfSJohannes Berg 	return jhash(key, ETH_ALEN, seed);
10137bedd0cfSJohannes Berg }
10147bedd0cfSJohannes Berg 
10157bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10167bedd0cfSJohannes Berg {
10177bedd0cfSJohannes Berg 	int err;
10187bedd0cfSJohannes Berg 
10197bedd0cfSJohannes Berg 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
10207bedd0cfSJohannes Berg 	if (err)
10217bedd0cfSJohannes Berg 		return err;
10227bedd0cfSJohannes Berg 
10234d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
102434e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1025f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1026f0706e82SJiri Benc 
1027b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1028b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10297bedd0cfSJohannes Berg 	return 0;
1030f0706e82SJiri Benc }
1031f0706e82SJiri Benc 
1032f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1033f0706e82SJiri Benc {
1034a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
10357bedd0cfSJohannes Berg 	rhashtable_destroy(&local->sta_hash);
1036f0706e82SJiri Benc }
1037f0706e82SJiri Benc 
1038051007d9SJohannes Berg 
1039e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1040f0706e82SJiri Benc {
1041b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1042f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1043d778207bSJohannes Berg 	LIST_HEAD(free_list);
104444213b5eSJohannes Berg 	int ret = 0;
1045f0706e82SJiri Benc 
1046d0709a65SJohannes Berg 	might_sleep();
1047d0709a65SJohannes Berg 
1048e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1049e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1050e716251dSJohannes Berg 
105134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
105234e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1053e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1054e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1055d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1056d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
105734316837SJohannes Berg 			ret++;
105834316837SJohannes Berg 		}
105934e89507SJohannes Berg 	}
1060d778207bSJohannes Berg 
1061d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1062d778207bSJohannes Berg 		synchronize_net();
1063d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1064d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1065d778207bSJohannes Berg 	}
106634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
106744213b5eSJohannes Berg 
1068051007d9SJohannes Berg 	return ret;
1069051007d9SJohannes Berg }
1070051007d9SJohannes Berg 
107124723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
107224723d1bSJohannes Berg 			  unsigned long exp_time)
107324723d1bSJohannes Berg {
107424723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
107524723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
107624723d1bSJohannes Berg 
107734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1078e46a2cf9SMohammed Shafi Shajakhan 
1079e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1080ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1081ec2b774eSMarek Lindner 			continue;
1082ec2b774eSMarek Lindner 
1083e5a9f8d0SJohannes Berg 		if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
1084eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1085bdcbd8e0SJohannes Berg 				sta->sta.addr);
10863f52b7e3SMarco Porsch 
10873f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10883f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10893f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10903f52b7e3SMarco Porsch 
109134e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
109224723d1bSJohannes Berg 		}
1093e46a2cf9SMohammed Shafi Shajakhan 	}
1094e46a2cf9SMohammed Shafi Shajakhan 
109534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
109624723d1bSJohannes Berg }
109717741cdcSJohannes Berg 
1098686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1099686b9cb9SBen Greear 						   const u8 *addr,
1100686b9cb9SBen Greear 						   const u8 *localaddr)
110117741cdcSJohannes Berg {
11027bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
11037bedd0cfSJohannes Berg 	struct sta_info *sta;
11047bedd0cfSJohannes Berg 	struct rhash_head *tmp;
11057bedd0cfSJohannes Berg 	const struct bucket_table *tbl;
11067bedd0cfSJohannes Berg 
11077bedd0cfSJohannes Berg 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
110817741cdcSJohannes Berg 
1109686b9cb9SBen Greear 	/*
1110686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1111686b9cb9SBen Greear 	 * ... first in list.
1112686b9cb9SBen Greear 	 */
11137bedd0cfSJohannes Berg 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1114686b9cb9SBen Greear 		if (localaddr &&
1115b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1116686b9cb9SBen Greear 			continue;
1117f7c65594SJohannes Berg 		if (!sta->uploaded)
1118f7c65594SJohannes Berg 			return NULL;
111917741cdcSJohannes Berg 		return &sta->sta;
1120f7c65594SJohannes Berg 	}
1121f7c65594SJohannes Berg 
1122abe60632SJohannes Berg 	return NULL;
112317741cdcSJohannes Berg }
1124686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11255ed176e1SJohannes Berg 
11265ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11275ed176e1SJohannes Berg 					 const u8 *addr)
11285ed176e1SJohannes Berg {
1129f7c65594SJohannes Berg 	struct sta_info *sta;
11305ed176e1SJohannes Berg 
11315ed176e1SJohannes Berg 	if (!vif)
11325ed176e1SJohannes Berg 		return NULL;
11335ed176e1SJohannes Berg 
1134f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1135f7c65594SJohannes Berg 	if (!sta)
1136f7c65594SJohannes Berg 		return NULL;
11375ed176e1SJohannes Berg 
1138f7c65594SJohannes Berg 	if (!sta->uploaded)
1139f7c65594SJohannes Berg 		return NULL;
1140f7c65594SJohannes Berg 
1141f7c65594SJohannes Berg 	return &sta->sta;
11425ed176e1SJohannes Berg }
114317741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1144af818581SJohannes Berg 
1145e3685e03SJohannes Berg /* powersave support code */
1146e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
114750a9432dSJohannes Berg {
1148608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1149e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1150e3685e03SJohannes Berg 	struct sk_buff_head pending;
1151ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1152e3685e03SJohannes Berg 	unsigned long flags;
1153d012a605SMarco Porsch 	struct ps_data *ps;
1154d012a605SMarco Porsch 
11553918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11563918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11573918edb0SFelix Fietkau 				     u.ap);
11583918edb0SFelix Fietkau 
11593918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1160d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11613f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11623f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1163d012a605SMarco Porsch 	else
1164d012a605SMarco Porsch 		return;
116550a9432dSJohannes Berg 
1166c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
116747086fc5SJohannes Berg 
11685a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1169948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1170ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1171948d887dSJohannes Berg 
117230686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
117312375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1174af818581SJohannes Berg 
1175ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1176ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1177ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1178ba8c3d6fSFelix Fietkau 
1179ba8c3d6fSFelix Fietkau 			if (!skb_queue_len(&txqi->queue))
1180ba8c3d6fSFelix Fietkau 				continue;
1181ba8c3d6fSFelix Fietkau 
1182ba8c3d6fSFelix Fietkau 			drv_wake_tx_queue(local, txqi);
1183ba8c3d6fSFelix Fietkau 		}
1184ba8c3d6fSFelix Fietkau 	}
1185ba8c3d6fSFelix Fietkau 
1186948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1187af818581SJohannes Berg 
11881d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11891d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1190af818581SJohannes Berg 	/* Send all buffered frames to the station */
1191948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1192948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1193948d887dSJohannes Berg 
1194987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1195948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1196987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1197948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1198948d887dSJohannes Berg 		filtered += tmp - count;
1199948d887dSJohannes Berg 		count = tmp;
1200948d887dSJohannes Berg 
1201987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1202948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1203987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1204948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1205948d887dSJohannes Berg 		buffered += tmp - count;
1206948d887dSJohannes Berg 	}
1207948d887dSJohannes Berg 
1208e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
12095ac2e350SJohannes Berg 
12105ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
12115ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
12125ac2e350SJohannes Berg 
12135ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12145ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12155ac2e350SJohannes Berg 	 */
12165ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12175ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12181d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1219948d887dSJohannes Berg 
1220e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1221e3685e03SJohannes Berg 
1222687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1223062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1224062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1225687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1226687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1227687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1228687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1229687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1230687da132SEmmanuel Grumbach 		       sta->sta.addr);
1231687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1232687da132SEmmanuel Grumbach 					   sta->sta.addr,
1233687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1234687da132SEmmanuel Grumbach 	}
1235687da132SEmmanuel Grumbach 
1236af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1237af818581SJohannes Berg 
1238c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1239c868cb35SJohannes Berg 
1240bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1241bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1242bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
124317c18bf8SJohannes Berg 
124417c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1245af818581SJohannes Berg }
1246af818581SJohannes Berg 
12470ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1248b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
12490ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1250ce662b44SJohannes Berg {
12510ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1252ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1253ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1254ce662b44SJohannes Berg 	struct sk_buff *skb;
1255ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1256ce662b44SJohannes Berg 	__le16 fc;
1257a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1258ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
125955de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1260ce662b44SJohannes Berg 
1261ce662b44SJohannes Berg 	if (qos) {
1262ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1263ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1264ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1265ce662b44SJohannes Berg 	} else {
1266ce662b44SJohannes Berg 		size -= 2;
1267ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1268ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1269ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1270ce662b44SJohannes Berg 	}
1271ce662b44SJohannes Berg 
1272ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1273ce662b44SJohannes Berg 	if (!skb)
1274ce662b44SJohannes Berg 		return;
1275ce662b44SJohannes Berg 
1276ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1277ce662b44SJohannes Berg 
1278ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1279ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1280ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1281ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1282ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1283ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1284864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1285ce662b44SJohannes Berg 
1286ce662b44SJohannes Berg 	skb->priority = tid;
1287ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
128859b66255SJohannes Berg 	if (qos) {
1289ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1290ce662b44SJohannes Berg 
12910ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1292ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1293ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
12940ead2510SEmmanuel Grumbach 			if (more_data)
12950ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
12960ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
12970ead2510SEmmanuel Grumbach 		}
1298ce662b44SJohannes Berg 	}
1299ce662b44SJohannes Berg 
1300ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1301ce662b44SJohannes Berg 
1302ce662b44SJohannes Berg 	/*
1303ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1304ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1305deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1306deeaee19SJohannes Berg 	 * ends the poll/service period.
1307ce662b44SJohannes Berg 	 */
130802f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1309deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1310ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1311ce662b44SJohannes Berg 
13126b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
13136b127c71SSujith Manoharan 
1314b77cf4f8SJohannes Berg 	if (call_driver)
1315b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1316b77cf4f8SJohannes Berg 					  reason, false);
131740b96408SJohannes Berg 
131889afe614SJohannes Berg 	skb->dev = sdata->dev;
131989afe614SJohannes Berg 
132055de908aSJohannes Berg 	rcu_read_lock();
132155de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
132255de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
132355de908aSJohannes Berg 		rcu_read_unlock();
132455de908aSJohannes Berg 		kfree_skb(skb);
132555de908aSJohannes Berg 		return;
132655de908aSJohannes Berg 	}
132755de908aSJohannes Berg 
132873c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13297c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
133055de908aSJohannes Berg 	rcu_read_unlock();
1331ce662b44SJohannes Berg }
1332ce662b44SJohannes Berg 
13330a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13340a1cb809SJohannes Berg {
13350a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13360a1cb809SJohannes Berg 	if (tids & 0xF8)
13370a1cb809SJohannes Berg 		return fls(tids) - 1;
13380a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13390a1cb809SJohannes Berg 	if (tids & BIT(0))
13400a1cb809SJohannes Berg 		return 0;
13410a1cb809SJohannes Berg 	return fls(tids) - 1;
13420a1cb809SJohannes Berg }
13430a1cb809SJohannes Berg 
13440ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
13450ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
13460ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
13470ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
13480ead2510SEmmanuel Grumbach  */
13490ead2510SEmmanuel Grumbach static bool
13500ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
13510ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
13520ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
13530ead2510SEmmanuel Grumbach {
13540ead2510SEmmanuel Grumbach 	int ac;
13550ead2510SEmmanuel Grumbach 
13560ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
13570ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
13580ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
13590ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
13600ead2510SEmmanuel Grumbach 	 */
13610ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
13620ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
13630ead2510SEmmanuel Grumbach 		return true;
13640ead2510SEmmanuel Grumbach 
13650ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13660ead2510SEmmanuel Grumbach 		if (ignored_acs & BIT(ac))
13670ead2510SEmmanuel Grumbach 			continue;
13680ead2510SEmmanuel Grumbach 
13690ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
13700ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
13710ead2510SEmmanuel Grumbach 			return true;
13720ead2510SEmmanuel Grumbach 	}
13730ead2510SEmmanuel Grumbach 
13740ead2510SEmmanuel Grumbach 	return false;
13750ead2510SEmmanuel Grumbach }
13760ead2510SEmmanuel Grumbach 
137747086fc5SJohannes Berg static void
13780ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
13790ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
13800ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
13810ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1382af818581SJohannes Berg {
1383af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1384af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1385948d887dSJohannes Berg 	int ac;
1386af818581SJohannes Berg 
1387f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1388948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13894049e09aSJohannes Berg 		unsigned long tids;
13904049e09aSJohannes Berg 
139147086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1392948d887dSJohannes Berg 			continue;
1393948d887dSJohannes Berg 
13944049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13954049e09aSJohannes Berg 
1396f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1397f9f760b4SJohannes Berg 		 * release from hardware queues
1398f9f760b4SJohannes Berg 		 */
13990ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
14000ead2510SEmmanuel Grumbach 			*driver_release_tids |=
14010ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
14020ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1403ba8c3d6fSFelix Fietkau 		}
1404f9f760b4SJohannes Berg 
14050ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
140647086fc5SJohannes Berg 			struct sk_buff *skb;
140747086fc5SJohannes Berg 
140847086fc5SJohannes Berg 			while (n_frames > 0) {
1409948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1410948d887dSJohannes Berg 				if (!skb) {
141147086fc5SJohannes Berg 					skb = skb_dequeue(
141247086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1413af818581SJohannes Berg 					if (skb)
1414af818581SJohannes Berg 						local->total_ps_buffered--;
1415af818581SJohannes Berg 				}
141647086fc5SJohannes Berg 				if (!skb)
141747086fc5SJohannes Berg 					break;
141847086fc5SJohannes Berg 				n_frames--;
14190ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
142047086fc5SJohannes Berg 			}
1421948d887dSJohannes Berg 		}
1422948d887dSJohannes Berg 
14230ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
14240ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
14250ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
14264049e09aSJohannes Berg 		 */
1427948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14280ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1429948d887dSJohannes Berg 			break;
1430948d887dSJohannes Berg 	}
1431948d887dSJohannes Berg }
1432af818581SJohannes Berg 
14330ead2510SEmmanuel Grumbach static void
14340ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
14350ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
14360ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
14370ead2510SEmmanuel Grumbach {
14380ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
14390ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
14400ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
14410ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
14420ead2510SEmmanuel Grumbach 	bool more_data;
14430ead2510SEmmanuel Grumbach 
14440ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
14450ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
14460ead2510SEmmanuel Grumbach 
14470ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
14480ead2510SEmmanuel Grumbach 
14490ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
14500ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
14510ead2510SEmmanuel Grumbach 
14520ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
14530ead2510SEmmanuel Grumbach 
14540ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL)
14550ead2510SEmmanuel Grumbach 		driver_release_tids =
14560ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
14570ead2510SEmmanuel Grumbach 
1458f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1459ce662b44SJohannes Berg 		int tid;
14604049e09aSJohannes Berg 
1461ce662b44SJohannes Berg 		/*
1462ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1463ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1464ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1465ce662b44SJohannes Berg 		 *
1466ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1467ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1468ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1469ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1470ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1471ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1472ce662b44SJohannes Berg 		 *
1473ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1474ce662b44SJohannes Berg 		 */
1475ce662b44SJohannes Berg 
1476ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1477ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1478ce662b44SJohannes Berg 
14790ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1480f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
148147086fc5SJohannes Berg 		struct sk_buff_head pending;
148247086fc5SJohannes Berg 		struct sk_buff *skb;
148340b96408SJohannes Berg 		int num = 0;
148440b96408SJohannes Berg 		u16 tids = 0;
1485b77cf4f8SJohannes Berg 		bool need_null = false;
148647086fc5SJohannes Berg 
148747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
148847086fc5SJohannes Berg 
148947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1490af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
149147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
149240b96408SJohannes Berg 			u8 *qoshdr = NULL;
149340b96408SJohannes Berg 
149440b96408SJohannes Berg 			num++;
1495af818581SJohannes Berg 
1496af818581SJohannes Berg 			/*
149747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
149847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
149947086fc5SJohannes Berg 			 * exchange.
1500af818581SJohannes Berg 			 */
15016b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
15026b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1503af818581SJohannes Berg 
150447086fc5SJohannes Berg 			/*
150547086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
150647086fc5SJohannes Berg 			 * more buffered frames for this STA
150747086fc5SJohannes Berg 			 */
150824b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
150947086fc5SJohannes Berg 				hdr->frame_control |=
151047086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
151124b9c373SJanusz.Dziedzic@tieto.com 			else
151224b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
151324b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1514af818581SJohannes Berg 
151540b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
151640b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
151740b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
151840b96408SJohannes Berg 
1519b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1520b77cf4f8SJohannes Berg 
1521b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1522b77cf4f8SJohannes Berg 
1523b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1524b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1525b77cf4f8SJohannes Berg 				continue;
1526b77cf4f8SJohannes Berg 
1527b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1528b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1529b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1530b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1531b77cf4f8SJohannes Berg 				break;
1532b77cf4f8SJohannes Berg 			}
1533b77cf4f8SJohannes Berg 
1534b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1535b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1536b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1537b77cf4f8SJohannes Berg 			 * and be done.
1538b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1539b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1540b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1541b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1542b77cf4f8SJohannes Berg 			 *
1543b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1544b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1545b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1546b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1547b77cf4f8SJohannes Berg 			 *
1548b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1549b77cf4f8SJohannes Berg 			 */
1550b77cf4f8SJohannes Berg 			if (qoshdr) {
155140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1552deeaee19SJohannes Berg 
155347086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
155447086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1555b77cf4f8SJohannes Berg 			} else {
1556b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1557b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1558b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1559b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1560b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1561b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1562b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1563b77cf4f8SJohannes Berg 				 */
1564b77cf4f8SJohannes Berg 				hdr->frame_control |=
1565b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1566b77cf4f8SJohannes Berg 				need_null = true;
1567b77cf4f8SJohannes Berg 				num++;
156852a3f20cSMarco Porsch 			}
1569b77cf4f8SJohannes Berg 			break;
157047086fc5SJohannes Berg 		}
157147086fc5SJohannes Berg 
157240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
157340b96408SJohannes Berg 					  reason, more_data);
157440b96408SJohannes Berg 
157547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1576af818581SJohannes Berg 
1577b77cf4f8SJohannes Berg 		if (need_null)
1578b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
15790ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
15800ead2510SEmmanuel Grumbach 				reason, false, false);
1581b77cf4f8SJohannes Berg 
1582c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1583af818581SJohannes Berg 	} else {
1584ba8c3d6fSFelix Fietkau 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1585ba8c3d6fSFelix Fietkau 		int tid;
1586ba8c3d6fSFelix Fietkau 
1587af818581SJohannes Berg 		/*
15884049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
15894049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1590f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1591f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1592f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1593f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1594f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1595f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1596af818581SJohannes Berg 		 */
15974049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
159847086fc5SJohannes Berg 					    n_frames, reason, more_data);
15994049e09aSJohannes Berg 
16004049e09aSJohannes Berg 		/*
16014049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
16024049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1603f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
16044049e09aSJohannes Berg 		 * release function.
1605f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1606ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1607ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
16084049e09aSJohannes Berg 		 */
1609ba8c3d6fSFelix Fietkau 
1610ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1611ba8c3d6fSFelix Fietkau 			return;
1612ba8c3d6fSFelix Fietkau 
1613ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1614ba8c3d6fSFelix Fietkau 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1615ba8c3d6fSFelix Fietkau 
1616ba8c3d6fSFelix Fietkau 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1617ba8c3d6fSFelix Fietkau 				continue;
1618ba8c3d6fSFelix Fietkau 
1619ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1620ba8c3d6fSFelix Fietkau 			break;
1621ba8c3d6fSFelix Fietkau 		}
1622af818581SJohannes Berg 	}
1623af818581SJohannes Berg }
1624af818581SJohannes Berg 
1625af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1626af818581SJohannes Berg {
162747086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1628af818581SJohannes Berg 
1629af818581SJohannes Berg 	/*
163047086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
163147086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
163247086fc5SJohannes Berg 	 * only from the non-enabled ones.
1633af818581SJohannes Berg 	 */
163447086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
163547086fc5SJohannes Berg 		ignore_for_response = 0;
1636af818581SJohannes Berg 
163747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
163847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1639af818581SJohannes Berg }
164047086fc5SJohannes Berg 
164147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
164247086fc5SJohannes Berg {
164347086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
164447086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
164547086fc5SJohannes Berg 
164647086fc5SJohannes Berg 	/*
164747086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
164847086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
164947086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
165047086fc5SJohannes Berg 	 * actually getting called.
165147086fc5SJohannes Berg 	 */
165247086fc5SJohannes Berg 	if (!delivery_enabled)
165347086fc5SJohannes Berg 		return;
165447086fc5SJohannes Berg 
165547086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
165647086fc5SJohannes Berg 	case 1:
165747086fc5SJohannes Berg 		n_frames = 2;
165847086fc5SJohannes Berg 		break;
165947086fc5SJohannes Berg 	case 2:
166047086fc5SJohannes Berg 		n_frames = 4;
166147086fc5SJohannes Berg 		break;
166247086fc5SJohannes Berg 	case 3:
166347086fc5SJohannes Berg 		n_frames = 6;
166447086fc5SJohannes Berg 		break;
166547086fc5SJohannes Berg 	case 0:
166647086fc5SJohannes Berg 		/* XXX: what is a good value? */
166713a8098aSAndrei Otcheretianski 		n_frames = 128;
166847086fc5SJohannes Berg 		break;
166947086fc5SJohannes Berg 	}
167047086fc5SJohannes Berg 
167147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
167247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1673af818581SJohannes Berg }
1674af818581SJohannes Berg 
1675af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1676af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1677af818581SJohannes Berg {
1678af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1679af818581SJohannes Berg 
1680b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1681b5878a2dSJohannes Berg 
16825ac2e350SJohannes Berg 	if (block) {
1683c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
168417c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
16855ac2e350SJohannes Berg 		return;
16865ac2e350SJohannes Berg 	}
16875ac2e350SJohannes Berg 
16885ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
16895ac2e350SJohannes Berg 		return;
16905ac2e350SJohannes Berg 
16915ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
16925ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
16935ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16945ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
16955ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
16965ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
16975ac2e350SJohannes Berg 		/* must be asleep in this case */
16985ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
16995ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17005ac2e350SJohannes Berg 	} else {
17015ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
170217c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
17035ac2e350SJohannes Berg 	}
1704af818581SJohannes Berg }
1705af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1706dcf55fb5SFelix Fietkau 
1707e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
170837fbd908SJohannes Berg {
170937fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
171037fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
171137fbd908SJohannes Berg 
171237fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
171337fbd908SJohannes Berg 
171437fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
171537fbd908SJohannes Berg }
1716e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
171737fbd908SJohannes Berg 
17180ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
17190ead2510SEmmanuel Grumbach {
17200ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
17210ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
17220ead2510SEmmanuel Grumbach 	bool more_data;
17230ead2510SEmmanuel Grumbach 
17240ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
17250ead2510SEmmanuel Grumbach 
17260ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
17270ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
17280ead2510SEmmanuel Grumbach 					       reason, 0);
17290ead2510SEmmanuel Grumbach 
17300ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
17310ead2510SEmmanuel Grumbach }
17320ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
17330ead2510SEmmanuel Grumbach 
1734042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1735042ec453SJohannes Berg 				u8 tid, bool buffered)
1736dcf55fb5SFelix Fietkau {
1737dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1738dcf55fb5SFelix Fietkau 
17395a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1740042ec453SJohannes Berg 		return;
1741042ec453SJohannes Berg 
17421b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
17431b000789SJohannes Berg 
1744948d887dSJohannes Berg 	if (buffered)
1745948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1746948d887dSJohannes Berg 	else
1747948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1748948d887dSJohannes Berg 
1749c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1750dcf55fb5SFelix Fietkau }
1751042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1752d9a7ddb0SJohannes Berg 
175383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1754d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1755d9a7ddb0SJohannes Berg {
17568bf11d8dSJohannes Berg 	might_sleep();
1757d9a7ddb0SJohannes Berg 
1758d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1759d9a7ddb0SJohannes Berg 		return 0;
1760d9a7ddb0SJohannes Berg 
1761f09603a2SJohannes Berg 	/* check allowed transitions first */
1762f09603a2SJohannes Berg 
1763d9a7ddb0SJohannes Berg 	switch (new_state) {
1764d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1765f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1766d9a7ddb0SJohannes Berg 			return -EINVAL;
1767d9a7ddb0SJohannes Berg 		break;
1768d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1769f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1770f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1771d9a7ddb0SJohannes Berg 			return -EINVAL;
1772d9a7ddb0SJohannes Berg 		break;
1773d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1774f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1775f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1776d9a7ddb0SJohannes Berg 			return -EINVAL;
1777d9a7ddb0SJohannes Berg 		break;
1778d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1779f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1780d9a7ddb0SJohannes Berg 			return -EINVAL;
1781d9a7ddb0SJohannes Berg 		break;
1782d9a7ddb0SJohannes Berg 	default:
1783d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1784d9a7ddb0SJohannes Berg 		return -EINVAL;
1785d9a7ddb0SJohannes Berg 	}
1786d9a7ddb0SJohannes Berg 
1787bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1788bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1789f09603a2SJohannes Berg 
1790f09603a2SJohannes Berg 	/*
1791f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1792f09603a2SJohannes Berg 	 * fail the transition
1793f09603a2SJohannes Berg 	 */
1794f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1795f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1796f09603a2SJohannes Berg 					sta->sta_state, new_state);
1797f09603a2SJohannes Berg 		if (err)
1798f09603a2SJohannes Berg 			return err;
1799f09603a2SJohannes Berg 	}
1800f09603a2SJohannes Berg 
1801f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1802f09603a2SJohannes Berg 
1803f09603a2SJohannes Berg 	switch (new_state) {
1804f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1805f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1806f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1807f09603a2SJohannes Berg 		break;
1808f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1809*a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
1810f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1811*a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1812f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1813*a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
1814*a7201a6cSIlan Peer 		}
1815f09603a2SJohannes Berg 		break;
1816f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1817f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1818f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1819*a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
1820f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
18217e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
18227e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
18237e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
18247e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1825f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
182617c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
1827f09603a2SJohannes Berg 		}
1828f09603a2SJohannes Berg 		break;
1829f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1830f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
18317e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
18327e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
18337e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
18347e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1835f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
183617c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
1837f09603a2SJohannes Berg 		}
1838f09603a2SJohannes Berg 		break;
1839f09603a2SJohannes Berg 	default:
1840f09603a2SJohannes Berg 		break;
1841f09603a2SJohannes Berg 	}
1842f09603a2SJohannes Berg 
1843d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1844d9a7ddb0SJohannes Berg 
1845d9a7ddb0SJohannes Berg 	return 0;
1846d9a7ddb0SJohannes Berg }
1847687da132SEmmanuel Grumbach 
1848687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1849687da132SEmmanuel Grumbach {
1850687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1851687da132SEmmanuel Grumbach 	u8 rx_streams;
1852687da132SEmmanuel Grumbach 
1853687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1854687da132SEmmanuel Grumbach 		return 1;
1855687da132SEmmanuel Grumbach 
1856687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1857687da132SEmmanuel Grumbach 		int i;
1858687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1859687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1860687da132SEmmanuel Grumbach 
1861687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1862687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1863687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1864687da132SEmmanuel Grumbach 				return i + 1;
1865687da132SEmmanuel Grumbach 	}
1866687da132SEmmanuel Grumbach 
1867687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1868687da132SEmmanuel Grumbach 		rx_streams = 4;
1869687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1870687da132SEmmanuel Grumbach 		rx_streams = 3;
1871687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1872687da132SEmmanuel Grumbach 		rx_streams = 2;
1873687da132SEmmanuel Grumbach 	else
1874687da132SEmmanuel Grumbach 		rx_streams = 1;
1875687da132SEmmanuel Grumbach 
1876687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1877687da132SEmmanuel Grumbach 		return rx_streams;
1878687da132SEmmanuel Grumbach 
1879687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1880687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1881687da132SEmmanuel Grumbach }
1882b7ffbd7eSJohannes Berg 
1883fbd6ff5cSJohannes Berg static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1884fbd6ff5cSJohannes Berg {
1885fbd6ff5cSJohannes Berg 	rinfo->flags = 0;
1886fbd6ff5cSJohannes Berg 
1887e5a9f8d0SJohannes Berg 	if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
1888fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
1889e5a9f8d0SJohannes Berg 		rinfo->mcs = sta->rx_stats.last_rate_idx;
1890e5a9f8d0SJohannes Berg 	} else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
1891fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
1892e5a9f8d0SJohannes Berg 		rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1893e5a9f8d0SJohannes Berg 		rinfo->mcs = sta->rx_stats.last_rate_idx;
1894fbd6ff5cSJohannes Berg 	} else {
1895fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
1896fbd6ff5cSJohannes Berg 		int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1897fbd6ff5cSJohannes Berg 		u16 brate;
1898fbd6ff5cSJohannes Berg 
1899fbd6ff5cSJohannes Berg 		sband = sta->local->hw.wiphy->bands[
1900fbd6ff5cSJohannes Berg 				ieee80211_get_sdata_band(sta->sdata)];
1901e5a9f8d0SJohannes Berg 		brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
1902fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1903fbd6ff5cSJohannes Berg 	}
1904fbd6ff5cSJohannes Berg 
1905e5a9f8d0SJohannes Berg 	if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
1906fbd6ff5cSJohannes Berg 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1907fbd6ff5cSJohannes Berg 
1908e5a9f8d0SJohannes Berg 	if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
1909fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_5;
1910e5a9f8d0SJohannes Berg 	else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
1911fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_10;
1912e5a9f8d0SJohannes Berg 	else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
1913fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_40;
1914e5a9f8d0SJohannes Berg 	else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
1915fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_80;
1916e5a9f8d0SJohannes Berg 	else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
1917fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_160;
1918fbd6ff5cSJohannes Berg 	else
1919fbd6ff5cSJohannes Berg 		rinfo->bw = RATE_INFO_BW_20;
1920fbd6ff5cSJohannes Berg }
1921fbd6ff5cSJohannes Berg 
1922b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1923b7ffbd7eSJohannes Berg {
1924b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1925b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
19269a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1927b7ffbd7eSJohannes Berg 	u32 thr = 0;
1928b7ffbd7eSJohannes Berg 	int i, ac;
1929b7ffbd7eSJohannes Berg 
19309a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
19319a244409SJohn W. Linville 		ref = local->rate_ctrl;
19329a244409SJohn W. Linville 
1933b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1934b7ffbd7eSJohannes Berg 
1935225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1936225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1937225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1938225b8189SJohannes Berg 	 */
1939225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1940225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1941225b8189SJohannes Berg 
19422b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
19432b9a7e1bSJohannes Berg 
1944319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1945319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1946319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1947319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1948976bd9efSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1949976bd9efSJohannes Berg 
1950976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1951976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1952976bd9efSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1953976bd9efSJohannes Berg 	}
1954b7ffbd7eSJohannes Berg 
195584b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
1956e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
1957e5a9f8d0SJohannes Berg 		jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
19582b9a7e1bSJohannes Berg 
1959319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1960319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1961b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
19622b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1963e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
1964319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1965b7ffbd7eSJohannes Berg 	}
19662b9a7e1bSJohannes Berg 
1967319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
19682b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
19692b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1970e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
1971319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
19722b9a7e1bSJohannes Berg 	}
19732b9a7e1bSJohannes Berg 
1974319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1975319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1976e5a9f8d0SJohannes Berg 		sinfo->rx_bytes = sta->rx_stats.bytes;
1977319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
19782b9a7e1bSJohannes Berg 	}
19792b9a7e1bSJohannes Berg 
1980319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1981e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
1982319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
19832b9a7e1bSJohannes Berg 	}
19842b9a7e1bSJohannes Berg 
1985319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1986e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
1987319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
19882b9a7e1bSJohannes Berg 	}
19892b9a7e1bSJohannes Berg 
1990319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1991e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
1992319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
19932b9a7e1bSJohannes Berg 	}
19942b9a7e1bSJohannes Berg 
1995e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
1996b7ffbd7eSJohannes Berg 
1997225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1998225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1999225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2000225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2001225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2002225b8189SJohannes Berg 	}
2003225b8189SJohannes Berg 
200430686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
200530686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2006319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2007e5a9f8d0SJohannes Berg 			sinfo->signal = (s8)sta->rx_stats.last_signal;
2008319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2009b7ffbd7eSJohannes Berg 		}
20102b9a7e1bSJohannes Berg 
2011319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
201240d9a38aSJohannes Berg 			sinfo->signal_avg =
2013e5a9f8d0SJohannes Berg 				-ewma_signal_read(&sta->rx_stats.avg_signal);
2014319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
20152b9a7e1bSJohannes Berg 		}
20162b9a7e1bSJohannes Berg 	}
20172b9a7e1bSJohannes Berg 
2018e5a9f8d0SJohannes Berg 	if (sta->rx_stats.chains &&
2019319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2020319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2021319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2022319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2023b7ffbd7eSJohannes Berg 
2024e5a9f8d0SJohannes Berg 		sinfo->chains = sta->rx_stats.chains;
2025b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2026e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2027e5a9f8d0SJohannes Berg 				sta->rx_stats.chain_signal_last[i];
2028b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2029e5a9f8d0SJohannes Berg 				-ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]);
2030b7ffbd7eSJohannes Berg 		}
2031b7ffbd7eSJohannes Berg 	}
2032b7ffbd7eSJohannes Berg 
2033319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2034e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2035e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2036319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
20372b9a7e1bSJohannes Berg 	}
20382b9a7e1bSJohannes Berg 
2039319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2040b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
2041319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
20422b9a7e1bSJohannes Berg 	}
2043b7ffbd7eSJohannes Berg 
204479c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
204579c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
204679c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
204779c892b8SJohannes Berg 
204879c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
204979c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2050e5a9f8d0SJohannes Berg 			tidstats->rx_msdu = sta->rx_stats.msdu[i];
205179c892b8SJohannes Berg 		}
205279c892b8SJohannes Berg 
205379c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
205479c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2055e5a9f8d0SJohannes Berg 			tidstats->tx_msdu = sta->tx_stats.msdu[i];
205679c892b8SJohannes Berg 		}
205779c892b8SJohannes Berg 
205879c892b8SJohannes Berg 		if (!(tidstats->filled &
205979c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
206030686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
206179c892b8SJohannes Berg 			tidstats->filled |=
206279c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2063e5a9f8d0SJohannes Berg 			tidstats->tx_msdu_retries =
2064e5a9f8d0SJohannes Berg 				sta->status_stats.msdu_retries[i];
206579c892b8SJohannes Berg 		}
206679c892b8SJohannes Berg 
206779c892b8SJohannes Berg 		if (!(tidstats->filled &
206879c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
206930686bf7SJohannes Berg 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
207079c892b8SJohannes Berg 			tidstats->filled |=
207179c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2072e5a9f8d0SJohannes Berg 			tidstats->tx_msdu_failed =
2073e5a9f8d0SJohannes Berg 				sta->status_stats.msdu_failed[i];
207479c892b8SJohannes Berg 		}
207579c892b8SJohannes Berg 	}
207679c892b8SJohannes Berg 
2077b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2078b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2079319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2080319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
2081319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
2082319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
2083319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
2084319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
2085b7ffbd7eSJohannes Berg 
2086433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2087433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2088433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2089b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2090319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2091433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2092b7ffbd7eSJohannes Berg 		}
2093433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2094433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2095433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2096b7ffbd7eSJohannes Berg #endif
2097b7ffbd7eSJohannes Berg 	}
2098b7ffbd7eSJohannes Berg 
2099b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2100b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2101b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2102b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2103b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2104b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2105b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2106785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2107b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2108b7ffbd7eSJohannes Berg 
2109b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2110b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2111b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2112b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2113b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2114b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2115b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2116b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2117b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2118b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2119b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2120b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2121a74a8c84SJohannes Berg 	if (sta->sta.wme)
2122b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2123b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2124b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2125b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2126b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2127b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2128b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2129b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2130b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2131b7ffbd7eSJohannes Berg 
2132b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2133b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2134b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2135b7ffbd7eSJohannes Berg 	else
2136b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
2137b7ffbd7eSJohannes Berg 
2138b7ffbd7eSJohannes Berg 	if (thr != 0) {
2139319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2140b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
2141b7ffbd7eSJohannes Berg 	}
2142b7ffbd7eSJohannes Berg }
2143