xref: /openbmc/linux/net/mac80211/sta_info.c (revision dcba665b1f4a5e986f22ac4230d536341d3ea5da)
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
5*dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 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,
74ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
757bedd0cfSJohannes Berg };
767bedd0cfSJohannes Berg 
774d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
78be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
79f0706e82SJiri Benc 			     struct sta_info *sta)
80f0706e82SJiri Benc {
8183e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
827bedd0cfSJohannes Berg 			       sta_rht_params);
83f0706e82SJiri Benc }
84f0706e82SJiri Benc 
855108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
86b22cfcfcSEliad Peller {
87b22cfcfcSEliad Peller 	int ac, i;
88b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
89b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
90b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
91fa962b92SMichal Kazior 	struct fq *fq = &local->fq;
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 
116fa962b92SMichal Kazior 			spin_lock_bh(&fq->lock);
117fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
118fa962b92SMichal Kazior 			spin_unlock_bh(&fq->lock);
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 
15883e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
15983e7e4ceSHerbert Xu 					 const u8 *addr)
16083e7e4ceSHerbert Xu {
16183e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
16283e7e4ceSHerbert Xu }
16383e7e4ceSHerbert Xu 
164d0709a65SJohannes Berg /* protected by RCU */
165abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
166abe60632SJohannes Berg 			      const u8 *addr)
16743ba7e95SJohannes Berg {
168abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16983e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
17060f4b626SJohannes Berg 	struct sta_info *sta;
17143ba7e95SJohannes Berg 
17260f4b626SJohannes Berg 	rcu_read_lock();
17383e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
17460f4b626SJohannes Berg 		if (sta->sdata == sdata) {
17560f4b626SJohannes Berg 			rcu_read_unlock();
17660f4b626SJohannes Berg 			/* this is safe as the caller must already hold
17760f4b626SJohannes Berg 			 * another rcu read section or the mutex
17860f4b626SJohannes Berg 			 */
17960f4b626SJohannes Berg 			return sta;
18060f4b626SJohannes Berg 		}
18160f4b626SJohannes Berg 	}
18260f4b626SJohannes Berg 	rcu_read_unlock();
18360f4b626SJohannes Berg 	return NULL;
18443ba7e95SJohannes Berg }
18543ba7e95SJohannes Berg 
1860e5ded5aSFelix Fietkau /*
1870e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1880e5ded5aSFelix Fietkau  * or from one of its vlans
1890e5ded5aSFelix Fietkau  */
1900e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1910e5ded5aSFelix Fietkau 				  const u8 *addr)
1920e5ded5aSFelix Fietkau {
1930e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
19483e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
1950e5ded5aSFelix Fietkau 	struct sta_info *sta;
1960e5ded5aSFelix Fietkau 
1977bedd0cfSJohannes Berg 	rcu_read_lock();
19883e7e4ceSHerbert Xu 	for_each_sta_info(local, 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
256c9c5962bSJohannes Berg 	free_percpu(sta->pcpu_rx_stats);
25793e5deb1SJohannes Berg 	kfree(sta);
25893e5deb1SJohannes Berg }
25993e5deb1SJohannes Berg 
2604d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
26162b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
262d0709a65SJohannes Berg 			     struct sta_info *sta)
263f0706e82SJiri Benc {
26483e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
2657bedd0cfSJohannes Berg 			       sta_rht_params);
266f0706e82SJiri Benc }
267f0706e82SJiri Benc 
2685ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
269af818581SJohannes Berg {
270af818581SJohannes Berg 	struct sta_info *sta;
271af818581SJohannes Berg 
2725ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
273af818581SJohannes Berg 
274af818581SJohannes Berg 	if (sta->dead)
275af818581SJohannes Berg 		return;
276af818581SJohannes Berg 
27754420473SHelmut Schaa 	local_bh_disable();
2785ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
279af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2805ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
281af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2825ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
28347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
284ce662b44SJohannes Berg 	local_bh_enable();
285af818581SJohannes Berg }
286af818581SJohannes Berg 
287af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
288af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
289af65cd96SJohannes Berg {
29030686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
291af65cd96SJohannes Berg 		return 0;
292af65cd96SJohannes Berg 
293889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
294af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
29535c347acSJohannes Berg 						     sta, gfp);
296889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
297af65cd96SJohannes Berg 		return -ENOMEM;
298af65cd96SJohannes Berg 
299af65cd96SJohannes Berg 	return 0;
300af65cd96SJohannes Berg }
301af65cd96SJohannes Berg 
30273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30356544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
304f0706e82SJiri Benc {
305d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
306ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
307f0706e82SJiri Benc 	struct sta_info *sta;
30816c5f15cSRon Rindjunsky 	int i;
309f0706e82SJiri Benc 
310ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
311f0706e82SJiri Benc 	if (!sta)
31273651ee6SJohannes Berg 		return NULL;
313f0706e82SJiri Benc 
314c9c5962bSJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
315c9c5962bSJohannes Berg 		sta->pcpu_rx_stats =
316c9c5962bSJohannes Berg 			alloc_percpu(struct ieee80211_sta_rx_stats);
317c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
318c9c5962bSJohannes Berg 			goto free;
319c9c5962bSJohannes Berg 	}
320c9c5962bSJohannes Berg 
32107346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3221d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3235ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
32467c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
325a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
32687f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
327433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
328433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
329433f5bc1SJohannes Berg 		if (!sta->mesh)
330433f5bc1SJohannes Berg 			goto free;
331433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
33287f59c70SThomas Pedersen 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
33387f59c70SThomas Pedersen 		    !sdata->u.mesh.user_mpm)
334433f5bc1SJohannes Berg 			init_timer(&sta->mesh->plink_timer);
335433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
336433f5bc1SJohannes Berg 	}
33787f59c70SThomas Pedersen #endif
33807346f81SJohannes Berg 
339ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
34017741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
341480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
342480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
343480dd46bSMaxim Altshul 
344d0709a65SJohannes Berg 	sta->local = local;
345d0709a65SJohannes Berg 	sta->sdata = sdata;
346e5a9f8d0SJohannes Berg 	sta->rx_stats.last_rx = jiffies;
347f0706e82SJiri Benc 
3480f9c5a61SJohannes Berg 	u64_stats_init(&sta->rx_stats.syncp);
3490f9c5a61SJohannes Berg 
35071ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35171ec375cSJohannes Berg 
352b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
353b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
354b6da911bSLiad Kaufman 
35584b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
3560be6ed13SJohannes Berg 	ewma_signal_init(&sta->rx_stats_avg.signal);
3570be6ed13SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
3580be6ed13SJohannes Berg 		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
359541a45a1SBruno Randolf 
360ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
361ba8c3d6fSFelix Fietkau 		void *txq_data;
362ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
363ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
364ba8c3d6fSFelix Fietkau 
365ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
366ba8c3d6fSFelix Fietkau 		if (!txq_data)
367ba8c3d6fSFelix Fietkau 			goto free;
368ba8c3d6fSFelix Fietkau 
369ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
370ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
371ba8c3d6fSFelix Fietkau 
372fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
373abfbc3afSJohannes Berg 		}
374ba8c3d6fSFelix Fietkau 	}
375ba8c3d6fSFelix Fietkau 
376ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
377ba8c3d6fSFelix Fietkau 		goto free_txq;
378f0706e82SJiri Benc 
3795a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
380a622ab72SJohannes Berg 		/*
381a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
382a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
383a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
384a622ab72SJohannes Berg 		 */
38516c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
38616c5f15cSRon Rindjunsky 	}
387948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
388948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
389948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
390948d887dSJohannes Berg 	}
39173651ee6SJohannes Berg 
3925a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3934be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
394cccaec98SSenthil Balasubramanian 
395af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
396687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
397687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
398687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
399ba8c3d6fSFelix Fietkau 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
400687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
401687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
402687da132SEmmanuel Grumbach 		/*
403687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
404687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
405687da132SEmmanuel Grumbach 		 */
406687da132SEmmanuel Grumbach 		switch (smps) {
407687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
408687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
409687da132SEmmanuel Grumbach 			break;
410687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
411687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
412687da132SEmmanuel Grumbach 			break;
413687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
414687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
415687da132SEmmanuel Grumbach 			break;
416687da132SEmmanuel Grumbach 		default:
417687da132SEmmanuel Grumbach 			WARN_ON(1);
418687da132SEmmanuel Grumbach 		}
419687da132SEmmanuel Grumbach 	}
420af0ed69bSJohannes Berg 
4216e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
4226e0456b5SFelix Fietkau 
423bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
424ef04a297SJohannes Berg 
425abfbc3afSJohannes Berg 	return sta;
426ba8c3d6fSFelix Fietkau 
427ba8c3d6fSFelix Fietkau free_txq:
428ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
429ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
430ba8c3d6fSFelix Fietkau free:
431433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
432433f5bc1SJohannes Berg 	kfree(sta->mesh);
433433f5bc1SJohannes Berg #endif
434ba8c3d6fSFelix Fietkau 	kfree(sta);
435ba8c3d6fSFelix Fietkau 	return NULL;
43673651ee6SJohannes Berg }
43773651ee6SJohannes Berg 
4388c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
43934e89507SJohannes Berg {
44034e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
44134e89507SJohannes Berg 
44203e4497eSJohannes Berg 	/*
44303e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
44403e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
44503e4497eSJohannes Berg 	 * and another CPU turns off the net device.
44603e4497eSJohannes Berg 	 */
4478c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4488c71df7aSGuy Eilam 		return -ENETDOWN;
44903e4497eSJohannes Berg 
450b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4518c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4528c71df7aSGuy Eilam 		return -EINVAL;
4538c71df7aSGuy Eilam 
45483e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
45583e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
45683e7e4ceSHerbert Xu 	 * for correctness.
45731104891SJohannes Berg 	 */
45831104891SJohannes Berg 	rcu_read_lock();
45931104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
46031104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
46131104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
46231104891SJohannes Berg 		rcu_read_unlock();
46331104891SJohannes Berg 		return -ENOTUNIQ;
46431104891SJohannes Berg 	}
46531104891SJohannes Berg 	rcu_read_unlock();
46631104891SJohannes Berg 
4678c71df7aSGuy Eilam 	return 0;
46893e5deb1SJohannes Berg }
46944213b5eSJohannes Berg 
470f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
471f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
472f09603a2SJohannes Berg 				     struct sta_info *sta)
473f09603a2SJohannes Berg {
474f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
475f09603a2SJohannes Berg 	int err = 0;
476f09603a2SJohannes Berg 
477f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
478f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
479f09603a2SJohannes Berg 		if (err)
480f09603a2SJohannes Berg 			break;
481f09603a2SJohannes Berg 	}
482f09603a2SJohannes Berg 
483f09603a2SJohannes Berg 	if (!err) {
484a4ec45a4SJohannes Berg 		/*
485a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
486a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
487a4ec45a4SJohannes Berg 		 */
488a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
489f09603a2SJohannes Berg 			sta->uploaded = true;
490f09603a2SJohannes Berg 		return 0;
491f09603a2SJohannes Berg 	}
492f09603a2SJohannes Berg 
493f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
494bdcbd8e0SJohannes Berg 		sdata_info(sdata,
495bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
496bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
497f09603a2SJohannes Berg 		err = 0;
498f09603a2SJohannes Berg 	}
499f09603a2SJohannes Berg 
500f09603a2SJohannes Berg 	/* unwind on error */
501f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
502f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
503f09603a2SJohannes Berg 
504f09603a2SJohannes Berg 	return err;
505f09603a2SJohannes Berg }
506f09603a2SJohannes Berg 
50734e89507SJohannes Berg /*
5088c71df7aSGuy Eilam  * should be called with sta_mtx locked
5098c71df7aSGuy Eilam  * this function replaces the mutex lock
5108c71df7aSGuy Eilam  * with a RCU lock
5118c71df7aSGuy Eilam  */
5124d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
5138c71df7aSGuy Eilam {
5148c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5158c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5160c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
5178c71df7aSGuy Eilam 	int err = 0;
5188c71df7aSGuy Eilam 
5198c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
5208c71df7aSGuy Eilam 
5217852e361SJohannes Berg 	/* check if STA exists already */
5227852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
5234d33960bSJohannes Berg 		err = -EEXIST;
5244d33960bSJohannes Berg 		goto out_err;
52534e89507SJohannes Berg 	}
52634e89507SJohannes Berg 
5270c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
5280c2e3842SKoen Vandeputte 	if (!sinfo) {
5290c2e3842SKoen Vandeputte 		err = -ENOMEM;
5300c2e3842SKoen Vandeputte 		goto out_err;
5310c2e3842SKoen Vandeputte 	}
5320c2e3842SKoen Vandeputte 
5334d33960bSJohannes Berg 	local->num_sta++;
5344d33960bSJohannes Berg 	local->sta_generation++;
5354d33960bSJohannes Berg 	smp_mb();
5364d33960bSJohannes Berg 
5375108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5385108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5395108ca82SJohannes Berg 
5404d33960bSJohannes Berg 	/* make the station visible */
54162b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
54262b14b24SJohannes Berg 	if (err)
54362b14b24SJohannes Berg 		goto out_drop_sta;
5444d33960bSJohannes Berg 
5452bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
54683d5cc01SJohannes Berg 
5475108ca82SJohannes Berg 	/* notify driver */
5485108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5495108ca82SJohannes Berg 	if (err)
5505108ca82SJohannes Berg 		goto out_remove;
5515108ca82SJohannes Berg 
55283d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5535108ca82SJohannes Berg 	/* accept BA sessions now */
5545108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5554d33960bSJohannes Berg 
5564d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5574d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5584d33960bSJohannes Berg 
5590ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
5600ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
5610ef049dcSArnd Bergmann 	kfree(sinfo);
562d0709a65SJohannes Berg 
563bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
564f0706e82SJiri Benc 
56534e89507SJohannes Berg 	/* move reference to rcu-protected */
56634e89507SJohannes Berg 	rcu_read_lock();
56734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
568e9f207f0SJiri Benc 
56973651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
57073651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
57173651ee6SJohannes Berg 
57273651ee6SJohannes Berg 	return 0;
5735108ca82SJohannes Berg  out_remove:
5745108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5755108ca82SJohannes Berg 	list_del_rcu(&sta->list);
57662b14b24SJohannes Berg  out_drop_sta:
5775108ca82SJohannes Berg 	local->num_sta--;
5785108ca82SJohannes Berg 	synchronize_net();
5795108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5804d33960bSJohannes Berg  out_err:
5814d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
582ea32f065SSudip Mukherjee 	kfree(sinfo);
5834d33960bSJohannes Berg 	rcu_read_lock();
5844d33960bSJohannes Berg 	return err;
5858c71df7aSGuy Eilam }
5868c71df7aSGuy Eilam 
5878c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5888c71df7aSGuy Eilam {
5898c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
590308f7fcfSZhao, Gang 	int err;
5918c71df7aSGuy Eilam 
5924d33960bSJohannes Berg 	might_sleep();
5934d33960bSJohannes Berg 
59431104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
59531104891SJohannes Berg 
5968c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5978c71df7aSGuy Eilam 	if (err) {
59831104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
5998c71df7aSGuy Eilam 		rcu_read_lock();
6008c71df7aSGuy Eilam 		goto out_free;
6018c71df7aSGuy Eilam 	}
6028c71df7aSGuy Eilam 
6034d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
6048c71df7aSGuy Eilam 	if (err)
605004c872eSJohannes Berg 		goto out_free;
606d0709a65SJohannes Berg 
607004c872eSJohannes Berg 	return 0;
60893e5deb1SJohannes Berg  out_free:
609d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
61093e5deb1SJohannes Berg 	return err;
611f0706e82SJiri Benc }
612f0706e82SJiri Benc 
61334e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
61434e89507SJohannes Berg {
61534e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
61634e89507SJohannes Berg 
61734e89507SJohannes Berg 	rcu_read_unlock();
61834e89507SJohannes Berg 
61934e89507SJohannes Berg 	return err;
62034e89507SJohannes Berg }
62134e89507SJohannes Berg 
622d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
623004c872eSJohannes Berg {
624004c872eSJohannes Berg 	/*
625004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
626004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
627004c872eSJohannes Berg 	 */
628d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
629004c872eSJohannes Berg }
630004c872eSJohannes Berg 
631d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
632004c872eSJohannes Berg {
633004c872eSJohannes Berg 	/*
634004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
635004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
636004c872eSJohannes Berg 	 */
637d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
638004c872eSJohannes Berg }
639004c872eSJohannes Berg 
6403d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6413d5839b6SIlan Peer {
6423d5839b6SIlan Peer 	/*
6433d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6443d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6453d5839b6SIlan Peer 	 */
6463d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6473d5839b6SIlan Peer }
6483d5839b6SIlan Peer 
649948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
650004c872eSJohannes Berg {
651948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
652948d887dSJohannes Berg 	switch (ac) {
653948d887dSJohannes Berg 	case IEEE80211_AC_VO:
654948d887dSJohannes Berg 		return BIT(6) | BIT(7);
655948d887dSJohannes Berg 	case IEEE80211_AC_VI:
656948d887dSJohannes Berg 		return BIT(4) | BIT(5);
657948d887dSJohannes Berg 	case IEEE80211_AC_BE:
658948d887dSJohannes Berg 		return BIT(0) | BIT(3);
659948d887dSJohannes Berg 	case IEEE80211_AC_BK:
660948d887dSJohannes Berg 		return BIT(1) | BIT(2);
661948d887dSJohannes Berg 	default:
662948d887dSJohannes Berg 		WARN_ON(1);
663948d887dSJohannes Berg 		return 0;
664d0709a65SJohannes Berg 	}
665004c872eSJohannes Berg }
666004c872eSJohannes Berg 
6679b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
668004c872eSJohannes Berg {
669c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
670d012a605SMarco Porsch 	struct ps_data *ps;
671948d887dSJohannes Berg 	bool indicate_tim = false;
672948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
673948d887dSJohannes Berg 	int ac;
674a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
675004c872eSJohannes Berg 
676d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
677d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
678c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
679c868cb35SJohannes Berg 			return;
6803e122be0SJohannes Berg 
681d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
6823f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6833f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6843f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
6853f52b7e3SMarco Porsch #endif
686d012a605SMarco Porsch 	} else {
687d012a605SMarco Porsch 		return;
688d012a605SMarco Porsch 	}
689d012a605SMarco Porsch 
690c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
691d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
692c868cb35SJohannes Berg 		return;
693004c872eSJohannes Berg 
694c868cb35SJohannes Berg 	if (sta->dead)
695c868cb35SJohannes Berg 		goto done;
6963e122be0SJohannes Berg 
697948d887dSJohannes Berg 	/*
698948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
699948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
700948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
701948d887dSJohannes Berg 	 * non-enabled ones.
702948d887dSJohannes Berg 	 */
703948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
704948d887dSJohannes Berg 		ignore_for_tim = 0;
705948d887dSJohannes Berg 
7069b7a86f3SJohannes Berg 	if (ignore_pending)
7079b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
7089b7a86f3SJohannes Berg 
709948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
710948d887dSJohannes Berg 		unsigned long tids;
711948d887dSJohannes Berg 
712f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
713948d887dSJohannes Berg 			continue;
714948d887dSJohannes Berg 
715948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
716948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
717948d887dSJohannes Berg 		if (indicate_tim)
718948d887dSJohannes Berg 			break;
719948d887dSJohannes Berg 
720948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
721948d887dSJohannes Berg 
722948d887dSJohannes Berg 		indicate_tim |=
723948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
724ba8c3d6fSFelix Fietkau 		indicate_tim |=
725ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
726004c872eSJohannes Berg 	}
727004c872eSJohannes Berg 
728c868cb35SJohannes Berg  done:
72965f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
730004c872eSJohannes Berg 
7313d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
7323d5839b6SIlan Peer 		goto out_unlock;
7333d5839b6SIlan Peer 
734948d887dSJohannes Berg 	if (indicate_tim)
735d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
736c868cb35SJohannes Berg 	else
737d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7383e122be0SJohannes Berg 
7399b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
740c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
741948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
742c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
743004c872eSJohannes Berg 	}
744004c872eSJohannes Berg 
7453d5839b6SIlan Peer out_unlock:
74665f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
747004c872eSJohannes Berg }
748004c872eSJohannes Berg 
7499b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7509b7a86f3SJohannes Berg {
7519b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7529b7a86f3SJohannes Berg }
7539b7a86f3SJohannes Berg 
754cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
755f0706e82SJiri Benc {
756e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
757f0706e82SJiri Benc 	int timeout;
758f0706e82SJiri Benc 
759f0706e82SJiri Benc 	if (!skb)
760cd0b8d89SJohannes Berg 		return false;
761f0706e82SJiri Benc 
762e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
763f0706e82SJiri Benc 
764f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
76557c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
76657c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
76757c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
768f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
769f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
770e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
771f0706e82SJiri Benc }
772f0706e82SJiri Benc 
773f0706e82SJiri Benc 
774948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
775948d887dSJohannes Berg 						struct sta_info *sta, int ac)
776f0706e82SJiri Benc {
777f0706e82SJiri Benc 	unsigned long flags;
778f0706e82SJiri Benc 	struct sk_buff *skb;
779f0706e82SJiri Benc 
78060750397SJohannes Berg 	/*
78160750397SJohannes Berg 	 * First check for frames that should expire on the filtered
78260750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
78360750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
78460750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
78560750397SJohannes Berg 	 * total_ps_buffered counter.
78660750397SJohannes Berg 	 */
787f0706e82SJiri Benc 	for (;;) {
788948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
789948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
79057c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
791948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
792836341a7SJohannes Berg 		else
793f0706e82SJiri Benc 			skb = NULL;
794948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
795f0706e82SJiri Benc 
79660750397SJohannes Berg 		/*
79760750397SJohannes Berg 		 * Frames are queued in order, so if this one
79860750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
79960750397SJohannes Berg 		 * we actually reached the end of the queue we
80060750397SJohannes Berg 		 * also need to stop, of course.
80160750397SJohannes Berg 		 */
80260750397SJohannes Berg 		if (!skb)
80360750397SJohannes Berg 			break;
804d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
80560750397SJohannes Berg 	}
80660750397SJohannes Berg 
80760750397SJohannes Berg 	/*
80860750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
80960750397SJohannes Berg 	 * only find something if the filtered queue was emptied
81060750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
81160750397SJohannes Berg 	 * buffered frames.
81260750397SJohannes Berg 	 */
813f0706e82SJiri Benc 	for (;;) {
814948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
815948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
816f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
817948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
818f0706e82SJiri Benc 		else
819f0706e82SJiri Benc 			skb = NULL;
820948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
821f0706e82SJiri Benc 
82260750397SJohannes Berg 		/*
82360750397SJohannes Berg 		 * frames are queued in order, so if this one
82460750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
82560750397SJohannes Berg 		 * the queue) we can stop testing
82660750397SJohannes Berg 		 */
827836341a7SJohannes Berg 		if (!skb)
828836341a7SJohannes Berg 			break;
829836341a7SJohannes Berg 
830f0706e82SJiri Benc 		local->total_ps_buffered--;
831bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
832bdcbd8e0SJohannes Berg 		       sta->sta.addr);
833d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
834f0706e82SJiri Benc 	}
8353393a608SJuuso Oikarinen 
83660750397SJohannes Berg 	/*
83760750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
83860750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
83960750397SJohannes Berg 	 * frames.
84060750397SJohannes Berg 	 */
84160750397SJohannes Berg 	sta_info_recalc_tim(sta);
84260750397SJohannes Berg 
84360750397SJohannes Berg 	/*
84460750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
84560750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
84660750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
84760750397SJohannes Berg 	 */
848948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
849948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
850948d887dSJohannes Berg }
851948d887dSJohannes Berg 
852948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
853948d887dSJohannes Berg 					     struct sta_info *sta)
854948d887dSJohannes Berg {
855948d887dSJohannes Berg 	bool have_buffered = false;
856948d887dSJohannes Berg 	int ac;
857948d887dSJohannes Berg 
8583f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8593f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8603f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
861948d887dSJohannes Berg 		return false;
862948d887dSJohannes Berg 
863948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
864948d887dSJohannes Berg 		have_buffered |=
865948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
866948d887dSJohannes Berg 
867948d887dSJohannes Berg 	return have_buffered;
868f0706e82SJiri Benc }
869f0706e82SJiri Benc 
870d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
87134e89507SJohannes Berg {
87234e89507SJohannes Berg 	struct ieee80211_local *local;
87334e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8746d10e46bSJohannes Berg 	int ret;
87534e89507SJohannes Berg 
87634e89507SJohannes Berg 	might_sleep();
87734e89507SJohannes Berg 
87834e89507SJohannes Berg 	if (!sta)
87934e89507SJohannes Berg 		return -ENOENT;
88034e89507SJohannes Berg 
88134e89507SJohannes Berg 	local = sta->local;
88234e89507SJohannes Berg 	sdata = sta->sdata;
88334e89507SJohannes Berg 
88483d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
88583d5cc01SJohannes Berg 
886098a6070SJohannes Berg 	/*
887098a6070SJohannes Berg 	 * Before removing the station from the driver and
888098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
889098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
890098a6070SJohannes Berg 	 * will be sufficient.
891098a6070SJohannes Berg 	 */
892c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
893c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
894098a6070SJohannes Berg 
895f59374ebSSara Sharon 	/*
896f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
897f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
898f59374ebSSara Sharon 	 * all such frames to be processed.
899f59374ebSSara Sharon 	 */
900f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
901f59374ebSSara Sharon 
90234e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
903b01711beSJohannes Berg 	if (WARN_ON(ret))
90434e89507SJohannes Berg 		return ret;
90534e89507SJohannes Berg 
906a7a6bdd0SArik Nemtsov 	/*
907a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
908a7a6bdd0SArik Nemtsov 	 * removal.
909a7a6bdd0SArik Nemtsov 	 */
910a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
911a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
912a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
913a7a6bdd0SArik Nemtsov 	}
914a7a6bdd0SArik Nemtsov 
915794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
916ef044763SEliad Peller 	sta->removed = true;
9174d33960bSJohannes Berg 
9186a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
9196a9d1b91SJohannes Berg 
920a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
921a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
922a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
923a710c816SJohannes Berg 
924d778207bSJohannes Berg 	return 0;
925d778207bSJohannes Berg }
926d778207bSJohannes Berg 
927d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
928d778207bSJohannes Berg {
929d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
930d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
9310ef049dcSArnd Bergmann 	struct station_info *sinfo;
932d778207bSJohannes Berg 	int ret;
933d778207bSJohannes Berg 
934d778207bSJohannes Berg 	/*
935d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
936d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
937d778207bSJohannes Berg 	 */
938d778207bSJohannes Berg 
939d778207bSJohannes Berg 	might_sleep();
940d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
941d778207bSJohannes Berg 
942c8782078SJohannes Berg 	/* now keys can no longer be reached */
9436d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
94434e89507SJohannes Berg 
9459b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
9469b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9479b7a86f3SJohannes Berg 
94834e89507SJohannes Berg 	sta->dead = true;
94934e89507SJohannes Berg 
95034e89507SJohannes Berg 	local->num_sta--;
95134e89507SJohannes Berg 	local->sta_generation++;
95234e89507SJohannes Berg 
95383d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
954f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
955f09603a2SJohannes Berg 		if (ret) {
95683d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
95783d5cc01SJohannes Berg 			break;
95883d5cc01SJohannes Berg 		}
95983d5cc01SJohannes Berg 	}
960d9a7ddb0SJohannes Berg 
961f09603a2SJohannes Berg 	if (sta->uploaded) {
962f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
963f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
964f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
965f09603a2SJohannes Berg 	}
96634e89507SJohannes Berg 
967bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
968bdcbd8e0SJohannes Berg 
9690ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
9700ef049dcSArnd Bergmann 	if (sinfo)
9710ef049dcSArnd Bergmann 		sta_set_sinfo(sta, sinfo);
9720ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
9730ef049dcSArnd Bergmann 	kfree(sinfo);
974ec15e68bSJouni Malinen 
97534e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
97634e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
97734e89507SJohannes Berg 
978d34ba216SJohannes Berg 	cleanup_single_sta(sta);
979d778207bSJohannes Berg }
980d778207bSJohannes Berg 
981d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
982d778207bSJohannes Berg {
983d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
984d778207bSJohannes Berg 
985d778207bSJohannes Berg 	if (err)
986d778207bSJohannes Berg 		return err;
987d778207bSJohannes Berg 
988d778207bSJohannes Berg 	synchronize_net();
989d778207bSJohannes Berg 
990d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
99134e89507SJohannes Berg 
99234e89507SJohannes Berg 	return 0;
99334e89507SJohannes Berg }
99434e89507SJohannes Berg 
99534e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
99634e89507SJohannes Berg {
99734e89507SJohannes Berg 	struct sta_info *sta;
99834e89507SJohannes Berg 	int ret;
99934e89507SJohannes Berg 
100034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10017852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
100234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
100334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
100434e89507SJohannes Berg 
100534e89507SJohannes Berg 	return ret;
100634e89507SJohannes Berg }
100734e89507SJohannes Berg 
100834e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
100934e89507SJohannes Berg 			      const u8 *addr)
101034e89507SJohannes Berg {
101134e89507SJohannes Berg 	struct sta_info *sta;
101234e89507SJohannes Berg 	int ret;
101334e89507SJohannes Berg 
101434e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
10157852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
101634e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
101734e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
101834e89507SJohannes Berg 
101934e89507SJohannes Berg 	return ret;
102034e89507SJohannes Berg }
1021f0706e82SJiri Benc 
1022f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
1023f0706e82SJiri Benc {
1024f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
1025f0706e82SJiri Benc 	struct sta_info *sta;
10263393a608SJuuso Oikarinen 	bool timer_needed = false;
1027f0706e82SJiri Benc 
1028d0709a65SJohannes Berg 	rcu_read_lock();
1029d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
10303393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
10313393a608SJuuso Oikarinen 			timer_needed = true;
1032d0709a65SJohannes Berg 	rcu_read_unlock();
1033f0706e82SJiri Benc 
10345bb644a0SJohannes Berg 	if (local->quiescing)
10355bb644a0SJohannes Berg 		return;
10365bb644a0SJohannes Berg 
10373393a608SJuuso Oikarinen 	if (!timer_needed)
10383393a608SJuuso Oikarinen 		return;
10393393a608SJuuso Oikarinen 
104026d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
104126d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1042f0706e82SJiri Benc }
1043f0706e82SJiri Benc 
10447bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
10457bedd0cfSJohannes Berg {
10467bedd0cfSJohannes Berg 	int err;
10477bedd0cfSJohannes Berg 
104883e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
10497bedd0cfSJohannes Berg 	if (err)
10507bedd0cfSJohannes Berg 		return err;
10517bedd0cfSJohannes Berg 
10524d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
105334e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1054f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1055f0706e82SJiri Benc 
1056b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1057b24b8a24SPavel Emelyanov 		    (unsigned long)local);
10587bedd0cfSJohannes Berg 	return 0;
1059f0706e82SJiri Benc }
1060f0706e82SJiri Benc 
1061f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1062f0706e82SJiri Benc {
1063a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
106483e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1065f0706e82SJiri Benc }
1066f0706e82SJiri Benc 
1067051007d9SJohannes Berg 
1068e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1069f0706e82SJiri Benc {
1070b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1071f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1072d778207bSJohannes Berg 	LIST_HEAD(free_list);
107344213b5eSJohannes Berg 	int ret = 0;
1074f0706e82SJiri Benc 
1075d0709a65SJohannes Berg 	might_sleep();
1076d0709a65SJohannes Berg 
1077e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1078e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1079e716251dSJohannes Berg 
108034e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
108134e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1082e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1083e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1084d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1085d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
108634316837SJohannes Berg 			ret++;
108734316837SJohannes Berg 		}
108834e89507SJohannes Berg 	}
1089d778207bSJohannes Berg 
1090d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1091d778207bSJohannes Berg 		synchronize_net();
1092d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1093d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1094d778207bSJohannes Berg 	}
109534e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
109644213b5eSJohannes Berg 
1097051007d9SJohannes Berg 	return ret;
1098051007d9SJohannes Berg }
1099051007d9SJohannes Berg 
110024723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
110124723d1bSJohannes Berg 			  unsigned long exp_time)
110224723d1bSJohannes Berg {
110324723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
110424723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
110524723d1bSJohannes Berg 
110634e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1107e46a2cf9SMohammed Shafi Shajakhan 
1108e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1109b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1110b8da6b6aSJohannes Berg 
1111ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1112ec2b774eSMarek Lindner 			continue;
1113ec2b774eSMarek Lindner 
1114b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1115eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1116bdcbd8e0SJohannes Berg 				sta->sta.addr);
11173f52b7e3SMarco Porsch 
11183f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
11193f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
11203f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
11213f52b7e3SMarco Porsch 
112234e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
112324723d1bSJohannes Berg 		}
1124e46a2cf9SMohammed Shafi Shajakhan 	}
1125e46a2cf9SMohammed Shafi Shajakhan 
112634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
112724723d1bSJohannes Berg }
112817741cdcSJohannes Berg 
1129686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1130686b9cb9SBen Greear 						   const u8 *addr,
1131686b9cb9SBen Greear 						   const u8 *localaddr)
113217741cdcSJohannes Berg {
11337bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
113483e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
11357bedd0cfSJohannes Berg 	struct sta_info *sta;
113617741cdcSJohannes Berg 
1137686b9cb9SBen Greear 	/*
1138686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1139686b9cb9SBen Greear 	 * ... first in list.
1140686b9cb9SBen Greear 	 */
114183e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1142686b9cb9SBen Greear 		if (localaddr &&
1143b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1144686b9cb9SBen Greear 			continue;
1145f7c65594SJohannes Berg 		if (!sta->uploaded)
1146f7c65594SJohannes Berg 			return NULL;
114717741cdcSJohannes Berg 		return &sta->sta;
1148f7c65594SJohannes Berg 	}
1149f7c65594SJohannes Berg 
1150abe60632SJohannes Berg 	return NULL;
115117741cdcSJohannes Berg }
1152686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
11535ed176e1SJohannes Berg 
11545ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
11555ed176e1SJohannes Berg 					 const u8 *addr)
11565ed176e1SJohannes Berg {
1157f7c65594SJohannes Berg 	struct sta_info *sta;
11585ed176e1SJohannes Berg 
11595ed176e1SJohannes Berg 	if (!vif)
11605ed176e1SJohannes Berg 		return NULL;
11615ed176e1SJohannes Berg 
1162f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1163f7c65594SJohannes Berg 	if (!sta)
1164f7c65594SJohannes Berg 		return NULL;
11655ed176e1SJohannes Berg 
1166f7c65594SJohannes Berg 	if (!sta->uploaded)
1167f7c65594SJohannes Berg 		return NULL;
1168f7c65594SJohannes Berg 
1169f7c65594SJohannes Berg 	return &sta->sta;
11705ed176e1SJohannes Berg }
117117741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1172af818581SJohannes Berg 
1173e3685e03SJohannes Berg /* powersave support code */
1174e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
117550a9432dSJohannes Berg {
1176608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1177e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1178e3685e03SJohannes Berg 	struct sk_buff_head pending;
1179ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1180e3685e03SJohannes Berg 	unsigned long flags;
1181d012a605SMarco Porsch 	struct ps_data *ps;
1182d012a605SMarco Porsch 
11833918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11843918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11853918edb0SFelix Fietkau 				     u.ap);
11863918edb0SFelix Fietkau 
11873918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1188d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11893f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11903f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1191d012a605SMarco Porsch 	else
1192d012a605SMarco Porsch 		return;
119350a9432dSJohannes Berg 
1194c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
119547086fc5SJohannes Berg 
11965a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1197948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1198ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1199948d887dSJohannes Berg 
120030686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
120112375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1202af818581SJohannes Berg 
1203ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
1204ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1205bb42f2d1SToke Høiland-Jørgensen 			if (!txq_has_queue(sta->sta.txq[i]))
1206ba8c3d6fSFelix Fietkau 				continue;
1207ba8c3d6fSFelix Fietkau 
1208bb42f2d1SToke Høiland-Jørgensen 			drv_wake_tx_queue(local, to_txq_info(sta->sta.txq[i]));
1209ba8c3d6fSFelix Fietkau 		}
1210ba8c3d6fSFelix Fietkau 	}
1211ba8c3d6fSFelix Fietkau 
1212948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1213af818581SJohannes Berg 
12141d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
12151d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1216af818581SJohannes Berg 	/* Send all buffered frames to the station */
1217948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1218948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1219948d887dSJohannes Berg 
1220987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1221948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1222987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1223948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1224948d887dSJohannes Berg 		filtered += tmp - count;
1225948d887dSJohannes Berg 		count = tmp;
1226948d887dSJohannes Berg 
1227987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1228948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1229987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1230948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1231948d887dSJohannes Berg 		buffered += tmp - count;
1232948d887dSJohannes Berg 	}
1233948d887dSJohannes Berg 
1234e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
12355ac2e350SJohannes Berg 
12365ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
12375ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
12385ac2e350SJohannes Berg 
12395ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
12405ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
12415ac2e350SJohannes Berg 	 */
12425ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
12435ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
12441d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1245948d887dSJohannes Berg 
1246e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1247e3685e03SJohannes Berg 
1248687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1249062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1250062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1251687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1252687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1253687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1254687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1255687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1256687da132SEmmanuel Grumbach 		       sta->sta.addr);
1257687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1258687da132SEmmanuel Grumbach 					   sta->sta.addr,
1259687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1260687da132SEmmanuel Grumbach 	}
1261687da132SEmmanuel Grumbach 
1262af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1263af818581SJohannes Berg 
1264c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1265c868cb35SJohannes Berg 
1266bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
12672595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1268bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
126917c18bf8SJohannes Berg 
127017c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1271af818581SJohannes Berg }
1272af818581SJohannes Berg 
12730ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1274b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
12750ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1276ce662b44SJohannes Berg {
12770ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1278ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1279ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1280ce662b44SJohannes Berg 	struct sk_buff *skb;
1281ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1282ce662b44SJohannes Berg 	__le16 fc;
1283a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1284ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
128555de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1286ce662b44SJohannes Berg 
1287ce662b44SJohannes Berg 	if (qos) {
1288ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1289ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1290ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1291ce662b44SJohannes Berg 	} else {
1292ce662b44SJohannes Berg 		size -= 2;
1293ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1294ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1295ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1296ce662b44SJohannes Berg 	}
1297ce662b44SJohannes Berg 
1298ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1299ce662b44SJohannes Berg 	if (!skb)
1300ce662b44SJohannes Berg 		return;
1301ce662b44SJohannes Berg 
1302ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1303ce662b44SJohannes Berg 
1304ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1305ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1306ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1307ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1308ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1309ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1310864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1311ce662b44SJohannes Berg 
1312ce662b44SJohannes Berg 	skb->priority = tid;
1313ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
131459b66255SJohannes Berg 	if (qos) {
1315ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1316ce662b44SJohannes Berg 
13170ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1318ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1319ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
13200ead2510SEmmanuel Grumbach 			if (more_data)
13210ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
13220ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
13230ead2510SEmmanuel Grumbach 		}
1324ce662b44SJohannes Berg 	}
1325ce662b44SJohannes Berg 
1326ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1327ce662b44SJohannes Berg 
1328ce662b44SJohannes Berg 	/*
1329ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1330ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1331deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1332deeaee19SJohannes Berg 	 * ends the poll/service period.
1333ce662b44SJohannes Berg 	 */
133402f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1335deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1336ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1337ce662b44SJohannes Berg 
13386b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
13396b127c71SSujith Manoharan 
1340b77cf4f8SJohannes Berg 	if (call_driver)
1341b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1342b77cf4f8SJohannes Berg 					  reason, false);
134340b96408SJohannes Berg 
134489afe614SJohannes Berg 	skb->dev = sdata->dev;
134589afe614SJohannes Berg 
134655de908aSJohannes Berg 	rcu_read_lock();
134755de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
134855de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
134955de908aSJohannes Berg 		rcu_read_unlock();
135055de908aSJohannes Berg 		kfree_skb(skb);
135155de908aSJohannes Berg 		return;
135255de908aSJohannes Berg 	}
135355de908aSJohannes Berg 
135473c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
13557c10770fSJohannes Berg 	ieee80211_xmit(sdata, sta, skb);
135655de908aSJohannes Berg 	rcu_read_unlock();
1357ce662b44SJohannes Berg }
1358ce662b44SJohannes Berg 
13590a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
13600a1cb809SJohannes Berg {
13610a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
13620a1cb809SJohannes Berg 	if (tids & 0xF8)
13630a1cb809SJohannes Berg 		return fls(tids) - 1;
13640a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
13650a1cb809SJohannes Berg 	if (tids & BIT(0))
13660a1cb809SJohannes Berg 		return 0;
13670a1cb809SJohannes Berg 	return fls(tids) - 1;
13680a1cb809SJohannes Berg }
13690a1cb809SJohannes Berg 
13700ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
13710ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
13720ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
13730ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
13740ead2510SEmmanuel Grumbach  */
13750ead2510SEmmanuel Grumbach static bool
13760ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
13770ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
13780ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
13790ead2510SEmmanuel Grumbach {
13800ead2510SEmmanuel Grumbach 	int ac;
13810ead2510SEmmanuel Grumbach 
13820ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
13830ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
13840ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
13850ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
13860ead2510SEmmanuel Grumbach 	 */
13870ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
13880ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
13890ead2510SEmmanuel Grumbach 		return true;
13900ead2510SEmmanuel Grumbach 
13910ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1392f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
13930ead2510SEmmanuel Grumbach 			continue;
13940ead2510SEmmanuel Grumbach 
13950ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
13960ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
13970ead2510SEmmanuel Grumbach 			return true;
13980ead2510SEmmanuel Grumbach 	}
13990ead2510SEmmanuel Grumbach 
14000ead2510SEmmanuel Grumbach 	return false;
14010ead2510SEmmanuel Grumbach }
14020ead2510SEmmanuel Grumbach 
140347086fc5SJohannes Berg static void
14040ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
14050ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
14060ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
14070ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1408af818581SJohannes Berg {
1409af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1410af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1411948d887dSJohannes Berg 	int ac;
1412af818581SJohannes Berg 
1413f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1414948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
14154049e09aSJohannes Berg 		unsigned long tids;
14164049e09aSJohannes Berg 
1417f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1418948d887dSJohannes Berg 			continue;
1419948d887dSJohannes Berg 
14204049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
14214049e09aSJohannes Berg 
1422f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1423f9f760b4SJohannes Berg 		 * release from hardware queues
1424f9f760b4SJohannes Berg 		 */
14250ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
14260ead2510SEmmanuel Grumbach 			*driver_release_tids |=
14270ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
14280ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1429ba8c3d6fSFelix Fietkau 		}
1430f9f760b4SJohannes Berg 
14310ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
143247086fc5SJohannes Berg 			struct sk_buff *skb;
143347086fc5SJohannes Berg 
143447086fc5SJohannes Berg 			while (n_frames > 0) {
1435948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1436948d887dSJohannes Berg 				if (!skb) {
143747086fc5SJohannes Berg 					skb = skb_dequeue(
143847086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1439af818581SJohannes Berg 					if (skb)
1440af818581SJohannes Berg 						local->total_ps_buffered--;
1441af818581SJohannes Berg 				}
144247086fc5SJohannes Berg 				if (!skb)
144347086fc5SJohannes Berg 					break;
144447086fc5SJohannes Berg 				n_frames--;
14450ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
144647086fc5SJohannes Berg 			}
1447948d887dSJohannes Berg 		}
1448948d887dSJohannes Berg 
14490ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
14500ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
14510ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
14524049e09aSJohannes Berg 		 */
1453948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
14540ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1455948d887dSJohannes Berg 			break;
1456948d887dSJohannes Berg 	}
1457948d887dSJohannes Berg }
1458af818581SJohannes Berg 
14590ead2510SEmmanuel Grumbach static void
14600ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
14610ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
14620ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
14630ead2510SEmmanuel Grumbach {
14640ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
14650ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
14660ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
14670ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
14680ead2510SEmmanuel Grumbach 	bool more_data;
14690ead2510SEmmanuel Grumbach 
14700ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
14710ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
14720ead2510SEmmanuel Grumbach 
14730ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
14740ead2510SEmmanuel Grumbach 
14750ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
14760ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
14770ead2510SEmmanuel Grumbach 
14780ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
14790ead2510SEmmanuel Grumbach 
14801a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
14810ead2510SEmmanuel Grumbach 		driver_release_tids =
14820ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
14830ead2510SEmmanuel Grumbach 
1484f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1485f438ceb8SEmmanuel Grumbach 		int tid, ac;
14864049e09aSJohannes Berg 
1487ce662b44SJohannes Berg 		/*
1488ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1489ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1490ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1491ce662b44SJohannes Berg 		 *
1492ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1493ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1494ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1495ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1496ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1497ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1498ce662b44SJohannes Berg 		 *
1499ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1500ce662b44SJohannes Berg 		 */
1501ce662b44SJohannes Berg 
1502ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1503f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1504d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1505d7f84244SEmmanuel Grumbach 				break;
1506f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1507ce662b44SJohannes Berg 
15080ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1509f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
151047086fc5SJohannes Berg 		struct sk_buff_head pending;
151147086fc5SJohannes Berg 		struct sk_buff *skb;
151240b96408SJohannes Berg 		int num = 0;
151340b96408SJohannes Berg 		u16 tids = 0;
1514b77cf4f8SJohannes Berg 		bool need_null = false;
151547086fc5SJohannes Berg 
151647086fc5SJohannes Berg 		skb_queue_head_init(&pending);
151747086fc5SJohannes Berg 
151847086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1519af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
152047086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
152140b96408SJohannes Berg 			u8 *qoshdr = NULL;
152240b96408SJohannes Berg 
152340b96408SJohannes Berg 			num++;
1524af818581SJohannes Berg 
1525af818581SJohannes Berg 			/*
152647086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
152747086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
152847086fc5SJohannes Berg 			 * exchange.
1529af818581SJohannes Berg 			 */
15306b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
15316b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1532af818581SJohannes Berg 
153347086fc5SJohannes Berg 			/*
153447086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
153547086fc5SJohannes Berg 			 * more buffered frames for this STA
153647086fc5SJohannes Berg 			 */
153724b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
153847086fc5SJohannes Berg 				hdr->frame_control |=
153947086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
154024b9c373SJanusz.Dziedzic@tieto.com 			else
154124b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
154224b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1543af818581SJohannes Berg 
154440b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
154540b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
154640b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
154740b96408SJohannes Berg 
1548b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1549b77cf4f8SJohannes Berg 
1550b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1551b77cf4f8SJohannes Berg 
1552b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1553b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1554b77cf4f8SJohannes Berg 				continue;
1555b77cf4f8SJohannes Berg 
1556b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1557b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1558b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1559b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1560b77cf4f8SJohannes Berg 				break;
1561b77cf4f8SJohannes Berg 			}
1562b77cf4f8SJohannes Berg 
1563b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1564b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1565b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1566b77cf4f8SJohannes Berg 			 * and be done.
1567b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1568b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1569b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1570b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1571b77cf4f8SJohannes Berg 			 *
1572b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1573b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1574b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1575b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1576b77cf4f8SJohannes Berg 			 *
1577b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1578b77cf4f8SJohannes Berg 			 */
1579b77cf4f8SJohannes Berg 			if (qoshdr) {
158040b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1581deeaee19SJohannes Berg 
158247086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
158347086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1584b77cf4f8SJohannes Berg 			} else {
1585b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1586b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1587b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1588b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1589b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1590b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1591b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1592b77cf4f8SJohannes Berg 				 */
1593b77cf4f8SJohannes Berg 				hdr->frame_control |=
1594b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1595b77cf4f8SJohannes Berg 				need_null = true;
1596b77cf4f8SJohannes Berg 				num++;
159752a3f20cSMarco Porsch 			}
1598b77cf4f8SJohannes Berg 			break;
159947086fc5SJohannes Berg 		}
160047086fc5SJohannes Berg 
160140b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
160240b96408SJohannes Berg 					  reason, more_data);
160340b96408SJohannes Berg 
160447086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1605af818581SJohannes Berg 
1606b77cf4f8SJohannes Berg 		if (need_null)
1607b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
16080ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
16090ead2510SEmmanuel Grumbach 				reason, false, false);
1610b77cf4f8SJohannes Berg 
1611c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1612af818581SJohannes Berg 	} else {
1613ba8c3d6fSFelix Fietkau 		int tid;
1614ba8c3d6fSFelix Fietkau 
1615af818581SJohannes Berg 		/*
16164049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
16174049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1618f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1619f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1620f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1621f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1622f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1623f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1624af818581SJohannes Berg 		 */
16254049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
162647086fc5SJohannes Berg 					    n_frames, reason, more_data);
16274049e09aSJohannes Berg 
16284049e09aSJohannes Berg 		/*
16294049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
16304049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1631f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
16324049e09aSJohannes Berg 		 * release function.
1633f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1634ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1635ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
16364049e09aSJohannes Berg 		 */
1637ba8c3d6fSFelix Fietkau 
1638ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1639ba8c3d6fSFelix Fietkau 			return;
1640ba8c3d6fSFelix Fietkau 
1641ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
164283843c80SFelix Fietkau 			if (!(driver_release_tids & BIT(tid)) ||
16431e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1644ba8c3d6fSFelix Fietkau 				continue;
1645ba8c3d6fSFelix Fietkau 
1646ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1647ba8c3d6fSFelix Fietkau 			break;
1648ba8c3d6fSFelix Fietkau 		}
1649af818581SJohannes Berg 	}
1650af818581SJohannes Berg }
1651af818581SJohannes Berg 
1652af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1653af818581SJohannes Berg {
165447086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1655af818581SJohannes Berg 
1656af818581SJohannes Berg 	/*
165747086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
165847086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
165947086fc5SJohannes Berg 	 * only from the non-enabled ones.
1660af818581SJohannes Berg 	 */
166147086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
166247086fc5SJohannes Berg 		ignore_for_response = 0;
1663af818581SJohannes Berg 
166447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
166547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1666af818581SJohannes Berg }
166747086fc5SJohannes Berg 
166847086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
166947086fc5SJohannes Berg {
167047086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
167147086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
167247086fc5SJohannes Berg 
167347086fc5SJohannes Berg 	/*
167447086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
167547086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
167647086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
167747086fc5SJohannes Berg 	 * actually getting called.
167847086fc5SJohannes Berg 	 */
167947086fc5SJohannes Berg 	if (!delivery_enabled)
168047086fc5SJohannes Berg 		return;
168147086fc5SJohannes Berg 
168247086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
168347086fc5SJohannes Berg 	case 1:
168447086fc5SJohannes Berg 		n_frames = 2;
168547086fc5SJohannes Berg 		break;
168647086fc5SJohannes Berg 	case 2:
168747086fc5SJohannes Berg 		n_frames = 4;
168847086fc5SJohannes Berg 		break;
168947086fc5SJohannes Berg 	case 3:
169047086fc5SJohannes Berg 		n_frames = 6;
169147086fc5SJohannes Berg 		break;
169247086fc5SJohannes Berg 	case 0:
169347086fc5SJohannes Berg 		/* XXX: what is a good value? */
169413a8098aSAndrei Otcheretianski 		n_frames = 128;
169547086fc5SJohannes Berg 		break;
169647086fc5SJohannes Berg 	}
169747086fc5SJohannes Berg 
169847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
169947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1700af818581SJohannes Berg }
1701af818581SJohannes Berg 
1702af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1703af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1704af818581SJohannes Berg {
1705af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1706af818581SJohannes Berg 
1707b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1708b5878a2dSJohannes Berg 
17095ac2e350SJohannes Berg 	if (block) {
1710c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
171117c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
17125ac2e350SJohannes Berg 		return;
17135ac2e350SJohannes Berg 	}
17145ac2e350SJohannes Berg 
17155ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
17165ac2e350SJohannes Berg 		return;
17175ac2e350SJohannes Berg 
17185ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
17195ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
17205ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17215ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17225ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
17235ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
17245ac2e350SJohannes Berg 		/* must be asleep in this case */
17255ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
17265ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
17275ac2e350SJohannes Berg 	} else {
17285ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
172917c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
17305ac2e350SJohannes Berg 	}
1731af818581SJohannes Berg }
1732af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1733dcf55fb5SFelix Fietkau 
1734e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
173537fbd908SJohannes Berg {
173637fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
173737fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
173837fbd908SJohannes Berg 
173937fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
174037fbd908SJohannes Berg 
174137fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
174237fbd908SJohannes Berg }
1743e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
174437fbd908SJohannes Berg 
17450ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
17460ead2510SEmmanuel Grumbach {
17470ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
17480ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
17490ead2510SEmmanuel Grumbach 	bool more_data;
17500ead2510SEmmanuel Grumbach 
17510ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
17520ead2510SEmmanuel Grumbach 
17530ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
17540ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
17550ead2510SEmmanuel Grumbach 					       reason, 0);
17560ead2510SEmmanuel Grumbach 
17570ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
17580ead2510SEmmanuel Grumbach }
17590ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
17600ead2510SEmmanuel Grumbach 
1761042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1762042ec453SJohannes Berg 				u8 tid, bool buffered)
1763dcf55fb5SFelix Fietkau {
1764dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1765dcf55fb5SFelix Fietkau 
17665a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1767042ec453SJohannes Berg 		return;
1768042ec453SJohannes Berg 
17691b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
17701b000789SJohannes Berg 
1771948d887dSJohannes Berg 	if (buffered)
1772948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1773948d887dSJohannes Berg 	else
1774948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1775948d887dSJohannes Berg 
1776c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1777dcf55fb5SFelix Fietkau }
1778042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1779d9a7ddb0SJohannes Berg 
178052cfa1d6SAyala Beker static void
178152cfa1d6SAyala Beker ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
178252cfa1d6SAyala Beker {
178352cfa1d6SAyala Beker 	struct ieee80211_local *local = sdata->local;
178452cfa1d6SAyala Beker 	bool allow_p2p_go_ps = sdata->vif.p2p;
178552cfa1d6SAyala Beker 	struct sta_info *sta;
178652cfa1d6SAyala Beker 
178752cfa1d6SAyala Beker 	rcu_read_lock();
178852cfa1d6SAyala Beker 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
178952cfa1d6SAyala Beker 		if (sdata != sta->sdata ||
179052cfa1d6SAyala Beker 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
179152cfa1d6SAyala Beker 			continue;
179252cfa1d6SAyala Beker 		if (!sta->sta.support_p2p_ps) {
179352cfa1d6SAyala Beker 			allow_p2p_go_ps = false;
179452cfa1d6SAyala Beker 			break;
179552cfa1d6SAyala Beker 		}
179652cfa1d6SAyala Beker 	}
179752cfa1d6SAyala Beker 	rcu_read_unlock();
179852cfa1d6SAyala Beker 
179952cfa1d6SAyala Beker 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
180052cfa1d6SAyala Beker 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
180152cfa1d6SAyala Beker 		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
180252cfa1d6SAyala Beker 	}
180352cfa1d6SAyala Beker }
180452cfa1d6SAyala Beker 
180583d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1806d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1807d9a7ddb0SJohannes Berg {
18088bf11d8dSJohannes Berg 	might_sleep();
1809d9a7ddb0SJohannes Berg 
1810d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1811d9a7ddb0SJohannes Berg 		return 0;
1812d9a7ddb0SJohannes Berg 
1813f09603a2SJohannes Berg 	/* check allowed transitions first */
1814f09603a2SJohannes Berg 
1815d9a7ddb0SJohannes Berg 	switch (new_state) {
1816d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1817f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1818d9a7ddb0SJohannes Berg 			return -EINVAL;
1819d9a7ddb0SJohannes Berg 		break;
1820d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1821f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1822f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1823d9a7ddb0SJohannes Berg 			return -EINVAL;
1824d9a7ddb0SJohannes Berg 		break;
1825d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1826f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1827f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1828d9a7ddb0SJohannes Berg 			return -EINVAL;
1829d9a7ddb0SJohannes Berg 		break;
1830d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1831f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1832d9a7ddb0SJohannes Berg 			return -EINVAL;
1833d9a7ddb0SJohannes Berg 		break;
1834d9a7ddb0SJohannes Berg 	default:
1835d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1836d9a7ddb0SJohannes Berg 		return -EINVAL;
1837d9a7ddb0SJohannes Berg 	}
1838d9a7ddb0SJohannes Berg 
1839bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1840bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1841f09603a2SJohannes Berg 
1842f09603a2SJohannes Berg 	/*
1843f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1844f09603a2SJohannes Berg 	 * fail the transition
1845f09603a2SJohannes Berg 	 */
1846f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1847f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1848f09603a2SJohannes Berg 					sta->sta_state, new_state);
1849f09603a2SJohannes Berg 		if (err)
1850f09603a2SJohannes Berg 			return err;
1851f09603a2SJohannes Berg 	}
1852f09603a2SJohannes Berg 
1853f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1854f09603a2SJohannes Berg 
1855f09603a2SJohannes Berg 	switch (new_state) {
1856f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1857f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1858f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1859f09603a2SJohannes Berg 		break;
1860f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1861a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
1862f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1863a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1864f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1865a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
186652cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
186752cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1868a7201a6cSIlan Peer 		}
1869f09603a2SJohannes Berg 		break;
1870f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1871f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1872f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1873a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
187452cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
187552cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1876f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
187772f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
1878f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
187917c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
188049ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
1881f09603a2SJohannes Berg 		}
1882f09603a2SJohannes Berg 		break;
1883f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1884f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
188572f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
1886f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
188717c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
188849ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
1889f09603a2SJohannes Berg 		}
1890f09603a2SJohannes Berg 		break;
1891f09603a2SJohannes Berg 	default:
1892f09603a2SJohannes Berg 		break;
1893f09603a2SJohannes Berg 	}
1894f09603a2SJohannes Berg 
1895d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1896d9a7ddb0SJohannes Berg 
1897d9a7ddb0SJohannes Berg 	return 0;
1898d9a7ddb0SJohannes Berg }
1899687da132SEmmanuel Grumbach 
1900687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1901687da132SEmmanuel Grumbach {
1902687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1903687da132SEmmanuel Grumbach 	u8 rx_streams;
1904687da132SEmmanuel Grumbach 
1905687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1906687da132SEmmanuel Grumbach 		return 1;
1907687da132SEmmanuel Grumbach 
1908687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1909687da132SEmmanuel Grumbach 		int i;
1910687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1911687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1912687da132SEmmanuel Grumbach 
1913687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1914687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1915687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1916687da132SEmmanuel Grumbach 				return i + 1;
1917687da132SEmmanuel Grumbach 	}
1918687da132SEmmanuel Grumbach 
1919687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1920687da132SEmmanuel Grumbach 		rx_streams = 4;
1921687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1922687da132SEmmanuel Grumbach 		rx_streams = 3;
1923687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1924687da132SEmmanuel Grumbach 		rx_streams = 2;
1925687da132SEmmanuel Grumbach 	else
1926687da132SEmmanuel Grumbach 		rx_streams = 1;
1927687da132SEmmanuel Grumbach 
1928687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1929687da132SEmmanuel Grumbach 		return rx_streams;
1930687da132SEmmanuel Grumbach 
1931687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1932687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1933687da132SEmmanuel Grumbach }
1934b7ffbd7eSJohannes Berg 
1935c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
1936c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
1937c9c5962bSJohannes Berg {
1938c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
1939c9c5962bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1940c9c5962bSJohannes Berg 	int cpu;
1941c9c5962bSJohannes Berg 
1942c9c5962bSJohannes Berg 	if (!ieee80211_hw_check(&local->hw, USES_RSS))
1943c9c5962bSJohannes Berg 		return stats;
1944c9c5962bSJohannes Berg 
1945c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
1946c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
1947c9c5962bSJohannes Berg 
1948c9c5962bSJohannes Berg 		cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
1949c9c5962bSJohannes Berg 
1950c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
1951c9c5962bSJohannes Berg 			stats = cpustats;
1952c9c5962bSJohannes Berg 	}
1953c9c5962bSJohannes Berg 
1954c9c5962bSJohannes Berg 	return stats;
1955c9c5962bSJohannes Berg }
1956c9c5962bSJohannes Berg 
19574f6b1b3dSJohannes Berg static void sta_stats_decode_rate(struct ieee80211_local *local, u16 rate,
19584f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
1959fbd6ff5cSJohannes Berg {
1960*dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
1961fbd6ff5cSJohannes Berg 
1962*dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
19637f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
19644f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
1965*dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
1966*dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
1967*dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
1968*dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
19697f406cd1SJohannes Berg 		break;
19707f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
19714f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
1972*dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
1973*dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
1974*dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
19757f406cd1SJohannes Berg 		break;
19767f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
1977fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
1978fbd6ff5cSJohannes Berg 		u16 brate;
19794f6b1b3dSJohannes Berg 		unsigned int shift;
1980*dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
1981*dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
1982fbd6ff5cSJohannes Berg 
1983a17d93ffSBen Greear 		rinfo->flags = 0;
1984*dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
1985*dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
19864f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
19874f6b1b3dSJohannes Berg 			shift = 2;
19884f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
19894f6b1b3dSJohannes Berg 			shift = 1;
19904f6b1b3dSJohannes Berg 		else
19914f6b1b3dSJohannes Berg 			shift = 0;
1992fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
19937f406cd1SJohannes Berg 		break;
19947f406cd1SJohannes Berg 		}
1995fbd6ff5cSJohannes Berg 	}
19964f6b1b3dSJohannes Berg }
1997fbd6ff5cSJohannes Berg 
1998a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
19994f6b1b3dSJohannes Berg {
2000c9c5962bSJohannes Berg 	u16 rate = ACCESS_ONCE(sta_get_last_rx_stats(sta)->last_rate);
20014f6b1b3dSJohannes Berg 
20024f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2003a17d93ffSBen Greear 		return -EINVAL;
2004a17d93ffSBen Greear 
20054f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2006a17d93ffSBen Greear 	return 0;
2007fbd6ff5cSJohannes Berg }
2008fbd6ff5cSJohannes Berg 
20090f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
20100f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
20110f9c5a61SJohannes Berg 			     int tid)
20120f9c5a61SJohannes Berg {
20130f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
20140f9c5a61SJohannes Berg 
20150f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
20160f9c5a61SJohannes Berg 		unsigned int start;
20170f9c5a61SJohannes Berg 
20180f9c5a61SJohannes Berg 		do {
20190f9c5a61SJohannes Berg 			start = u64_stats_fetch_begin(&sta->rx_stats.syncp);
20200f9c5a61SJohannes Berg 			tidstats->rx_msdu = sta->rx_stats.msdu[tid];
20210f9c5a61SJohannes Berg 		} while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start));
20220f9c5a61SJohannes Berg 
20230f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
20240f9c5a61SJohannes Berg 	}
20250f9c5a61SJohannes Berg 
20260f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
20270f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
20280f9c5a61SJohannes Berg 		tidstats->tx_msdu = sta->tx_stats.msdu[tid];
20290f9c5a61SJohannes Berg 	}
20300f9c5a61SJohannes Berg 
20310f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
20320f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20330f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
20340f9c5a61SJohannes Berg 		tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
20350f9c5a61SJohannes Berg 	}
20360f9c5a61SJohannes Berg 
20370f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
20380f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
20390f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
20400f9c5a61SJohannes Berg 		tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
20410f9c5a61SJohannes Berg 	}
20420f9c5a61SJohannes Berg }
20430f9c5a61SJohannes Berg 
2044c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2045c9c5962bSJohannes Berg {
2046c9c5962bSJohannes Berg 	unsigned int start;
2047c9c5962bSJohannes Berg 	u64 value;
2048c9c5962bSJohannes Berg 
2049c9c5962bSJohannes Berg 	do {
2050c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2051c9c5962bSJohannes Berg 		value = rxstats->bytes;
2052c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2053c9c5962bSJohannes Berg 
2054c9c5962bSJohannes Berg 	return value;
2055c9c5962bSJohannes Berg }
2056c9c5962bSJohannes Berg 
2057b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
2058b7ffbd7eSJohannes Berg {
2059b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2060b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2061b7ffbd7eSJohannes Berg 	u32 thr = 0;
2062c9c5962bSJohannes Berg 	int i, ac, cpu;
2063c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2064c9c5962bSJohannes Berg 
2065c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2066b7ffbd7eSJohannes Berg 
2067b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2068b7ffbd7eSJohannes Berg 
2069225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2070225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2071225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2072225b8189SJohannes Berg 	 */
2073225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2074225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2075225b8189SJohannes Berg 
20762b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
20772b9a7e1bSJohannes Berg 
2078319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
2079319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
2080319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
2081319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
2082976bd9efSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC);
2083976bd9efSJohannes Berg 
2084976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2085976bd9efSJohannes Berg 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2086976bd9efSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
2087976bd9efSJohannes Berg 	}
2088b7ffbd7eSJohannes Berg 
208984b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2090e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2091b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
20922b9a7e1bSJohannes Berg 
2093319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
2094319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
2095b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
20962b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2097e5a9f8d0SJohannes Berg 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2098319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
2099b7ffbd7eSJohannes Berg 	}
21002b9a7e1bSJohannes Berg 
2101319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
21022b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
21032b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2104e5a9f8d0SJohannes Berg 			sinfo->tx_packets += sta->tx_stats.packets[ac];
2105319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
21062b9a7e1bSJohannes Berg 	}
21072b9a7e1bSJohannes Berg 
2108319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
2109319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
2110c9c5962bSJohannes Berg 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
21110f9c5a61SJohannes Berg 
2112c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2113c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2114c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2115c9c5962bSJohannes Berg 
2116c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2117c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2118c9c5962bSJohannes Berg 			}
2119c9c5962bSJohannes Berg 		}
2120c9c5962bSJohannes Berg 
2121319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
21222b9a7e1bSJohannes Berg 	}
21232b9a7e1bSJohannes Berg 
2124319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
2125e5a9f8d0SJohannes Berg 		sinfo->rx_packets = sta->rx_stats.packets;
2126c9c5962bSJohannes Berg 		if (sta->pcpu_rx_stats) {
2127c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2128c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2129c9c5962bSJohannes Berg 
2130c9c5962bSJohannes Berg 				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2131c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2132c9c5962bSJohannes Berg 			}
2133c9c5962bSJohannes Berg 		}
2134319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
21352b9a7e1bSJohannes Berg 	}
21362b9a7e1bSJohannes Berg 
2137319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
2138e5a9f8d0SJohannes Berg 		sinfo->tx_retries = sta->status_stats.retry_count;
2139319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
21402b9a7e1bSJohannes Berg 	}
21412b9a7e1bSJohannes Berg 
2142319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
2143e5a9f8d0SJohannes Berg 		sinfo->tx_failed = sta->status_stats.retry_failed;
2144319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
21452b9a7e1bSJohannes Berg 	}
21462b9a7e1bSJohannes Berg 
2147e5a9f8d0SJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2148c9c5962bSJohannes Berg 	if (sta->pcpu_rx_stats) {
2149c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2150c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2151c9c5962bSJohannes Berg 
2152c9c5962bSJohannes Berg 			cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2153c9c5962bSJohannes Berg 			sinfo->rx_packets += cpurxs->dropped;
2154c9c5962bSJohannes Berg 		}
2155c9c5962bSJohannes Berg 	}
2156b7ffbd7eSJohannes Berg 
2157225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2158225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2159225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
2160225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2161225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2162225b8189SJohannes Berg 	}
2163225b8189SJohannes Berg 
216430686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
216530686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2166319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2167c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2168319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2169b7ffbd7eSJohannes Berg 		}
21702b9a7e1bSJohannes Berg 
2171c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats &&
2172c9c5962bSJohannes Berg 		    !(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
217340d9a38aSJohannes Berg 			sinfo->signal_avg =
21740be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.signal);
2175319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
21762b9a7e1bSJohannes Berg 		}
21772b9a7e1bSJohannes Berg 	}
21782b9a7e1bSJohannes Berg 
2179c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2180c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2181c9c5962bSJohannes Berg 	 * pcpu statistics
2182c9c5962bSJohannes Berg 	 */
2183c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2184319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2185319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2186c9c5962bSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL);
2187c9c5962bSJohannes Berg 		if (!sta->pcpu_rx_stats)
2188c9c5962bSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2189b7ffbd7eSJohannes Berg 
2190c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2191c9c5962bSJohannes Berg 
2192b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2193e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2194c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2195b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
21960be6ed13SJohannes Berg 				-ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2197b7ffbd7eSJohannes Berg 		}
2198b7ffbd7eSJohannes Berg 	}
2199b7ffbd7eSJohannes Berg 
2200319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2201e5a9f8d0SJohannes Berg 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2202e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2203319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
22042b9a7e1bSJohannes Berg 	}
22052b9a7e1bSJohannes Berg 
2206319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2207a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2208319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
22092b9a7e1bSJohannes Berg 	}
2210b7ffbd7eSJohannes Berg 
221179c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
221279c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
221379c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
221479c892b8SJohannes Berg 
22150f9c5a61SJohannes Berg 		sta_set_tidstats(sta, tidstats, i);
221679c892b8SJohannes Berg 	}
221779c892b8SJohannes Berg 
2218b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2219b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2220319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2221319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
2222319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
2223319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
2224319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
2225319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
2226b7ffbd7eSJohannes Berg 
2227433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2228433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2229433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2230b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2231319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2232433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2233b7ffbd7eSJohannes Berg 		}
2234433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2235433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2236433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2237b7ffbd7eSJohannes Berg #endif
2238b7ffbd7eSJohannes Berg 	}
2239b7ffbd7eSJohannes Berg 
2240b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2241b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2242b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2243b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2244b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2245b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2246b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2247785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2248b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2249b7ffbd7eSJohannes Berg 
2250b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2251b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2252b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2253b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2254b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2255b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2256b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2257b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2258b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2259b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2260b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2261b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2262a74a8c84SJohannes Berg 	if (sta->sta.wme)
2263b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2264b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2265b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2266b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2267b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2268b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2269b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2270b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2271b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2272b7ffbd7eSJohannes Berg 
22733b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
22743b17fbf8SMaxim Altshul 
22753b17fbf8SMaxim Altshul 	if (thr != 0) {
22763b17fbf8SMaxim Altshul 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
22773b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
22783b17fbf8SMaxim Altshul 	}
22793b17fbf8SMaxim Altshul }
22803b17fbf8SMaxim Altshul 
22813b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
22823b17fbf8SMaxim Altshul {
22833b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
22843b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
22853b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
22863b17fbf8SMaxim Altshul 	u32 thr = 0;
22873b17fbf8SMaxim Altshul 
22883b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
22893b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
22903b17fbf8SMaxim Altshul 
2291b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2292b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2293b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2294b7ffbd7eSJohannes Berg 	else
22954fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2296b7ffbd7eSJohannes Berg 
22973b17fbf8SMaxim Altshul 	return thr;
2298b7ffbd7eSJohannes Berg }
2299b8da6b6aSJohannes Berg 
2300b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2301b8da6b6aSJohannes Berg {
2302c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2303c9c5962bSJohannes Berg 
2304c9c5962bSJohannes Berg 	if (time_after(stats->last_rx, sta->status_stats.last_ack))
2305c9c5962bSJohannes Berg 		return stats->last_rx;
2306b8da6b6aSJohannes Berg 	return sta->status_stats.last_ack;
2307b8da6b6aSJohannes Berg }
2308