xref: /openbmc/linux/net/mac80211/sta_info.c (revision abfbc3af57b1b92ff976ce7f1c776c169d14ed8a)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
5f0706e82SJiri Benc  *
6f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
7f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
8f0706e82SJiri Benc  * published by the Free Software Foundation.
9f0706e82SJiri Benc  */
10f0706e82SJiri Benc 
11f0706e82SJiri Benc #include <linux/module.h>
12f0706e82SJiri Benc #include <linux/init.h>
13888d04dfSFelix Fietkau #include <linux/etherdevice.h>
14f0706e82SJiri Benc #include <linux/netdevice.h>
15f0706e82SJiri Benc #include <linux/types.h>
16f0706e82SJiri Benc #include <linux/slab.h>
17f0706e82SJiri Benc #include <linux/skbuff.h>
18f0706e82SJiri Benc #include <linux/if_arp.h>
190d174406SJohannes Berg #include <linux/timer.h>
20d0709a65SJohannes Berg #include <linux/rtnetlink.h>
21f0706e82SJiri Benc 
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
674d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
68be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
69f0706e82SJiri Benc 			     struct sta_info *sta)
70f0706e82SJiri Benc {
71f0706e82SJiri Benc 	struct sta_info *s;
72f0706e82SJiri Benc 
7340b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
744d33960bSJohannes Berg 				      lockdep_is_held(&local->sta_mtx));
75f0706e82SJiri Benc 	if (!s)
76be8755e1SMichael Wu 		return -ENOENT;
77be8755e1SMichael Wu 	if (s == sta) {
78cf778b00SEric Dumazet 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
79d0709a65SJohannes Berg 				   s->hnext);
80be8755e1SMichael Wu 		return 0;
81f0706e82SJiri Benc 	}
82f0706e82SJiri Benc 
8340b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8440b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8540b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
864d33960bSJohannes Berg 					lockdep_is_held(&local->sta_mtx));
8740b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
88cf778b00SEric Dumazet 		rcu_assign_pointer(s->hnext, sta->hnext);
89be8755e1SMichael Wu 		return 0;
90f0706e82SJiri Benc 	}
91f0706e82SJiri Benc 
92be8755e1SMichael Wu 	return -ENOENT;
93f0706e82SJiri Benc }
94f0706e82SJiri Benc 
955108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
96b22cfcfcSEliad Peller {
97b22cfcfcSEliad Peller 	int ac, i;
98b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
99b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
100b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
101d012a605SMarco Porsch 	struct ps_data *ps;
102b22cfcfcSEliad Peller 
103e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1045ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1055ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
106d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
107d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
108d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1093f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1103f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
111d012a605SMarco Porsch 		else
112d012a605SMarco Porsch 			return;
113b22cfcfcSEliad Peller 
114b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
115e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1165ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
117b22cfcfcSEliad Peller 
118d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
119b22cfcfcSEliad Peller 	}
120b22cfcfcSEliad Peller 
121b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
122b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1231f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1241f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
125b22cfcfcSEliad Peller 	}
126b22cfcfcSEliad Peller 
12745b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12845b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
129b22cfcfcSEliad Peller 
1305ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
131b22cfcfcSEliad Peller 
132b22cfcfcSEliad Peller 	/*
133b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
134b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
135b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
136b22cfcfcSEliad Peller 	 * directly by station destruction.
137b22cfcfcSEliad Peller 	 */
1385a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
139661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
140b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
141b22cfcfcSEliad Peller 		if (!tid_tx)
142b22cfcfcSEliad Peller 			continue;
1431f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
144b22cfcfcSEliad Peller 		kfree(tid_tx);
145b22cfcfcSEliad Peller 	}
1465108ca82SJohannes Berg }
147b22cfcfcSEliad Peller 
1485108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1495108ca82SJohannes Berg {
1505108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1515108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1525108ca82SJohannes Berg 
1535108ca82SJohannes Berg 	__cleanup_single_sta(sta);
154b22cfcfcSEliad Peller 	sta_info_free(local, sta);
155b22cfcfcSEliad Peller }
156b22cfcfcSEliad Peller 
157d0709a65SJohannes Berg /* protected by RCU */
158abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
159abe60632SJohannes Berg 			      const u8 *addr)
16043ba7e95SJohannes Berg {
161abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16243ba7e95SJohannes Berg 	struct sta_info *sta;
16343ba7e95SJohannes Berg 
1640379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1650379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
16643ba7e95SJohannes Berg 	while (sta) {
167abe60632SJohannes Berg 		if (sta->sdata == sdata &&
168b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
16943ba7e95SJohannes Berg 			break;
1700379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1710379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
17243ba7e95SJohannes Berg 	}
17343ba7e95SJohannes Berg 	return sta;
17443ba7e95SJohannes Berg }
17543ba7e95SJohannes Berg 
1760e5ded5aSFelix Fietkau /*
1770e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1780e5ded5aSFelix Fietkau  * or from one of its vlans
1790e5ded5aSFelix Fietkau  */
1800e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1810e5ded5aSFelix Fietkau 				  const u8 *addr)
1820e5ded5aSFelix Fietkau {
1830e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1840e5ded5aSFelix Fietkau 	struct sta_info *sta;
1850e5ded5aSFelix Fietkau 
1860379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1870379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1880e5ded5aSFelix Fietkau 	while (sta) {
1890e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
190a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
191b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
1920e5ded5aSFelix Fietkau 			break;
1930379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1940379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1950e5ded5aSFelix Fietkau 	}
1960e5ded5aSFelix Fietkau 	return sta;
1970e5ded5aSFelix Fietkau }
1980e5ded5aSFelix Fietkau 
1993b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2003b53fde8SJohannes Berg 				     int idx)
201ee385855SLuis Carlos Cobo {
2023b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
203ee385855SLuis Carlos Cobo 	struct sta_info *sta;
204ee385855SLuis Carlos Cobo 	int i = 0;
205ee385855SLuis Carlos Cobo 
206d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2073b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2082a8ca29aSLuis Carlos Cobo 			continue;
209ee385855SLuis Carlos Cobo 		if (i < idx) {
210ee385855SLuis Carlos Cobo 			++i;
211ee385855SLuis Carlos Cobo 			continue;
212ee385855SLuis Carlos Cobo 		}
2132a8ca29aSLuis Carlos Cobo 		return sta;
214ee385855SLuis Carlos Cobo 	}
215ee385855SLuis Carlos Cobo 
216ee385855SLuis Carlos Cobo 	return NULL;
217ee385855SLuis Carlos Cobo }
218f0706e82SJiri Benc 
21993e5deb1SJohannes Berg /**
220d9a7ddb0SJohannes Berg  * sta_info_free - free STA
22193e5deb1SJohannes Berg  *
2226ef307bcSRandy Dunlap  * @local: pointer to the global information
22393e5deb1SJohannes Berg  * @sta: STA info to free
22493e5deb1SJohannes Berg  *
22593e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
226d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
227d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
228d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
22993e5deb1SJohannes Berg  */
230d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
23193e5deb1SJohannes Berg {
232889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2334b7679a5SJohannes Berg 		rate_control_free_sta(sta);
23493e5deb1SJohannes Berg 
235bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
23693e5deb1SJohannes Berg 
23753d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
23893e5deb1SJohannes Berg 	kfree(sta);
23993e5deb1SJohannes Berg }
24093e5deb1SJohannes Berg 
2414d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
242d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
243d0709a65SJohannes Berg 			      struct sta_info *sta)
244f0706e82SJiri Benc {
2454d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
24617741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
247cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
248f0706e82SJiri Benc }
249f0706e82SJiri Benc 
2505ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
251af818581SJohannes Berg {
252af818581SJohannes Berg 	struct sta_info *sta;
253af818581SJohannes Berg 
2545ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
255af818581SJohannes Berg 
256af818581SJohannes Berg 	if (sta->dead)
257af818581SJohannes Berg 		return;
258af818581SJohannes Berg 
25954420473SHelmut Schaa 	local_bh_disable();
2605ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
261af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2625ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
263af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2645ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
26547086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
266ce662b44SJohannes Berg 	local_bh_enable();
267af818581SJohannes Berg }
268af818581SJohannes Berg 
269af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
270af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
271af65cd96SJohannes Berg {
272af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
273af65cd96SJohannes Berg 		return 0;
274af65cd96SJohannes Berg 
275889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
276af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
277af65cd96SJohannes Berg 						     &sta->sta, gfp);
278889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
279af65cd96SJohannes Berg 		return -ENOMEM;
280af65cd96SJohannes Berg 
281af65cd96SJohannes Berg 	return 0;
282af65cd96SJohannes Berg }
283af65cd96SJohannes Berg 
28473651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
28556544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
286f0706e82SJiri Benc {
287d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
288f0706e82SJiri Benc 	struct sta_info *sta;
289ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
29016c5f15cSRon Rindjunsky 	int i;
291f0706e82SJiri Benc 
29217741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
293f0706e82SJiri Benc 	if (!sta)
29473651ee6SJohannes Berg 		return NULL;
295f0706e82SJiri Benc 
29607346f81SJohannes Berg 	spin_lock_init(&sta->lock);
2971d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
2985ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
29967c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
300a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
30187f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
30287f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
30387f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
30487f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3056c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
30687f59c70SThomas Pedersen #endif
30707346f81SJohannes Berg 
30817741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
309d0709a65SJohannes Berg 	sta->local = local;
310d0709a65SJohannes Berg 	sta->sdata = sdata;
3118bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
312f0706e82SJiri Benc 
31371ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
31471ec375cSJohannes Berg 
315b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
316b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
317b6da911bSLiad Kaufman 
31818171520SThomas Gleixner 	ktime_get_ts(&uptime);
319ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
320541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
321ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
322ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
323541a45a1SBruno Randolf 
324*abfbc3afSJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
325*abfbc3afSJohannes Berg 		kfree(sta);
326*abfbc3afSJohannes Berg 		return NULL;
327*abfbc3afSJohannes Berg 	}
328f0706e82SJiri Benc 
3295a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
330a622ab72SJohannes Berg 		/*
331a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
332a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
333a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
334a622ab72SJohannes Berg 		 */
33516c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
33616c5f15cSRon Rindjunsky 	}
337948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
338948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
339948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
340948d887dSJohannes Berg 	}
34173651ee6SJohannes Berg 
3425a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3434be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
344cccaec98SSenthil Balasubramanian 
345af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
346687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
347687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
348687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
349687da132SEmmanuel Grumbach 			local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
350687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
351687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
352687da132SEmmanuel Grumbach 		/*
353687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
354687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
355687da132SEmmanuel Grumbach 		 */
356687da132SEmmanuel Grumbach 		switch (smps) {
357687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
358687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
359687da132SEmmanuel Grumbach 			break;
360687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
361687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
362687da132SEmmanuel Grumbach 			break;
363687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
364687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
365687da132SEmmanuel Grumbach 			break;
366687da132SEmmanuel Grumbach 		default:
367687da132SEmmanuel Grumbach 			WARN_ON(1);
368687da132SEmmanuel Grumbach 		}
369687da132SEmmanuel Grumbach 	}
370af0ed69bSJohannes Berg 
371bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
372ef04a297SJohannes Berg 
373*abfbc3afSJohannes Berg 	return sta;
37473651ee6SJohannes Berg }
37573651ee6SJohannes Berg 
3768c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
37734e89507SJohannes Berg {
37834e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
37934e89507SJohannes Berg 
38003e4497eSJohannes Berg 	/*
38103e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
38203e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
38303e4497eSJohannes Berg 	 * and another CPU turns off the net device.
38403e4497eSJohannes Berg 	 */
3858c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
3868c71df7aSGuy Eilam 		return -ENETDOWN;
38703e4497eSJohannes Berg 
388b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
3898c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
3908c71df7aSGuy Eilam 		return -EINVAL;
3918c71df7aSGuy Eilam 
3928c71df7aSGuy Eilam 	return 0;
39393e5deb1SJohannes Berg }
39444213b5eSJohannes Berg 
395f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
396f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
397f09603a2SJohannes Berg 				     struct sta_info *sta)
398f09603a2SJohannes Berg {
399f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
400f09603a2SJohannes Berg 	int err = 0;
401f09603a2SJohannes Berg 
402f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
403f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
404f09603a2SJohannes Berg 		if (err)
405f09603a2SJohannes Berg 			break;
406f09603a2SJohannes Berg 	}
407f09603a2SJohannes Berg 
408f09603a2SJohannes Berg 	if (!err) {
409a4ec45a4SJohannes Berg 		/*
410a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
411a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
412a4ec45a4SJohannes Berg 		 */
413a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
414f09603a2SJohannes Berg 			sta->uploaded = true;
415f09603a2SJohannes Berg 		return 0;
416f09603a2SJohannes Berg 	}
417f09603a2SJohannes Berg 
418f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
419bdcbd8e0SJohannes Berg 		sdata_info(sdata,
420bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
421bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
422f09603a2SJohannes Berg 		err = 0;
423f09603a2SJohannes Berg 	}
424f09603a2SJohannes Berg 
425f09603a2SJohannes Berg 	/* unwind on error */
426f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
427f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
428f09603a2SJohannes Berg 
429f09603a2SJohannes Berg 	return err;
430f09603a2SJohannes Berg }
431f09603a2SJohannes Berg 
43234e89507SJohannes Berg /*
4338c71df7aSGuy Eilam  * should be called with sta_mtx locked
4348c71df7aSGuy Eilam  * this function replaces the mutex lock
4358c71df7aSGuy Eilam  * with a RCU lock
4368c71df7aSGuy Eilam  */
4374d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4388c71df7aSGuy Eilam {
4398c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4408c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4417852e361SJohannes Berg 	struct station_info sinfo;
4428c71df7aSGuy Eilam 	int err = 0;
4438c71df7aSGuy Eilam 
4448c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4458c71df7aSGuy Eilam 
4467852e361SJohannes Berg 	/* check if STA exists already */
4477852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4484d33960bSJohannes Berg 		err = -EEXIST;
4494d33960bSJohannes Berg 		goto out_err;
45034e89507SJohannes Berg 	}
45134e89507SJohannes Berg 
4524d33960bSJohannes Berg 	local->num_sta++;
4534d33960bSJohannes Berg 	local->sta_generation++;
4544d33960bSJohannes Berg 	smp_mb();
4554d33960bSJohannes Berg 
4565108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
4575108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4585108ca82SJohannes Berg 
4594d33960bSJohannes Berg 	/* make the station visible */
4604d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
4614d33960bSJohannes Berg 
4622bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
46383d5cc01SJohannes Berg 
4645108ca82SJohannes Berg 	/* notify driver */
4655108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
4665108ca82SJohannes Berg 	if (err)
4675108ca82SJohannes Berg 		goto out_remove;
4685108ca82SJohannes Berg 
46983d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
4705108ca82SJohannes Berg 	/* accept BA sessions now */
4715108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4724d33960bSJohannes Berg 
47321f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
4744d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
4754d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
4764d33960bSJohannes Berg 
4774d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
4784d33960bSJohannes Berg 	sinfo.filled = 0;
4794d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
4804d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
481d0709a65SJohannes Berg 
482bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
483f0706e82SJiri Benc 
48434e89507SJohannes Berg 	/* move reference to rcu-protected */
48534e89507SJohannes Berg 	rcu_read_lock();
48634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
487e9f207f0SJiri Benc 
48873651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
48973651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
49073651ee6SJohannes Berg 
49173651ee6SJohannes Berg 	return 0;
4925108ca82SJohannes Berg  out_remove:
4935108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
4945108ca82SJohannes Berg 	list_del_rcu(&sta->list);
4955108ca82SJohannes Berg 	local->num_sta--;
4965108ca82SJohannes Berg 	synchronize_net();
4975108ca82SJohannes Berg 	__cleanup_single_sta(sta);
4984d33960bSJohannes Berg  out_err:
4994d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5004d33960bSJohannes Berg 	rcu_read_lock();
5014d33960bSJohannes Berg 	return err;
5028c71df7aSGuy Eilam }
5038c71df7aSGuy Eilam 
5048c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5058c71df7aSGuy Eilam {
5068c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
507308f7fcfSZhao, Gang 	int err;
5088c71df7aSGuy Eilam 
5094d33960bSJohannes Berg 	might_sleep();
5104d33960bSJohannes Berg 
5118c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5128c71df7aSGuy Eilam 	if (err) {
5138c71df7aSGuy Eilam 		rcu_read_lock();
5148c71df7aSGuy Eilam 		goto out_free;
5158c71df7aSGuy Eilam 	}
5168c71df7aSGuy Eilam 
517004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
518004c872eSJohannes Berg 
5194d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5208c71df7aSGuy Eilam 	if (err)
521004c872eSJohannes Berg 		goto out_free;
522d0709a65SJohannes Berg 
523004c872eSJohannes Berg 	return 0;
52493e5deb1SJohannes Berg  out_free:
525d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
52693e5deb1SJohannes Berg 	return err;
527f0706e82SJiri Benc }
528f0706e82SJiri Benc 
52934e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
53034e89507SJohannes Berg {
53134e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
53234e89507SJohannes Berg 
53334e89507SJohannes Berg 	rcu_read_unlock();
53434e89507SJohannes Berg 
53534e89507SJohannes Berg 	return err;
53634e89507SJohannes Berg }
53734e89507SJohannes Berg 
538d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
539004c872eSJohannes Berg {
540004c872eSJohannes Berg 	/*
541004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
542004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
543004c872eSJohannes Berg 	 */
544d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
545004c872eSJohannes Berg }
546004c872eSJohannes Berg 
547d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
548004c872eSJohannes Berg {
549004c872eSJohannes Berg 	/*
550004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
551004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
552004c872eSJohannes Berg 	 */
553d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
554004c872eSJohannes Berg }
555004c872eSJohannes Berg 
5563d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
5573d5839b6SIlan Peer {
5583d5839b6SIlan Peer 	/*
5593d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
5603d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
5613d5839b6SIlan Peer 	 */
5623d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
5633d5839b6SIlan Peer }
5643d5839b6SIlan Peer 
565948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
566004c872eSJohannes Berg {
567948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
568948d887dSJohannes Berg 	switch (ac) {
569948d887dSJohannes Berg 	case IEEE80211_AC_VO:
570948d887dSJohannes Berg 		return BIT(6) | BIT(7);
571948d887dSJohannes Berg 	case IEEE80211_AC_VI:
572948d887dSJohannes Berg 		return BIT(4) | BIT(5);
573948d887dSJohannes Berg 	case IEEE80211_AC_BE:
574948d887dSJohannes Berg 		return BIT(0) | BIT(3);
575948d887dSJohannes Berg 	case IEEE80211_AC_BK:
576948d887dSJohannes Berg 		return BIT(1) | BIT(2);
577948d887dSJohannes Berg 	default:
578948d887dSJohannes Berg 		WARN_ON(1);
579948d887dSJohannes Berg 		return 0;
580d0709a65SJohannes Berg 	}
581004c872eSJohannes Berg }
582004c872eSJohannes Berg 
5839b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
584004c872eSJohannes Berg {
585c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
586d012a605SMarco Porsch 	struct ps_data *ps;
587948d887dSJohannes Berg 	bool indicate_tim = false;
588948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
589948d887dSJohannes Berg 	int ac;
590d012a605SMarco Porsch 	u16 id;
591004c872eSJohannes Berg 
592d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
593d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
594c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
595c868cb35SJohannes Berg 			return;
5963e122be0SJohannes Berg 
597d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
598d012a605SMarco Porsch 		id = sta->sta.aid;
5993f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6003f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6013f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
602204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6036f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6043f52b7e3SMarco Porsch #endif
605d012a605SMarco Porsch 	} else {
606d012a605SMarco Porsch 		return;
607d012a605SMarco Porsch 	}
608d012a605SMarco Porsch 
609c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
610c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
611c868cb35SJohannes Berg 		return;
612004c872eSJohannes Berg 
613c868cb35SJohannes Berg 	if (sta->dead)
614c868cb35SJohannes Berg 		goto done;
6153e122be0SJohannes Berg 
616948d887dSJohannes Berg 	/*
617948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
618948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
619948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
620948d887dSJohannes Berg 	 * non-enabled ones.
621948d887dSJohannes Berg 	 */
622948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
623948d887dSJohannes Berg 		ignore_for_tim = 0;
624948d887dSJohannes Berg 
6259b7a86f3SJohannes Berg 	if (ignore_pending)
6269b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6279b7a86f3SJohannes Berg 
628948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
629948d887dSJohannes Berg 		unsigned long tids;
630948d887dSJohannes Berg 
631948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
632948d887dSJohannes Berg 			continue;
633948d887dSJohannes Berg 
634948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
635948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
636948d887dSJohannes Berg 		if (indicate_tim)
637948d887dSJohannes Berg 			break;
638948d887dSJohannes Berg 
639948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
640948d887dSJohannes Berg 
641948d887dSJohannes Berg 		indicate_tim |=
642948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
643004c872eSJohannes Berg 	}
644004c872eSJohannes Berg 
645c868cb35SJohannes Berg  done:
64665f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
647004c872eSJohannes Berg 
6483d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6493d5839b6SIlan Peer 		goto out_unlock;
6503d5839b6SIlan Peer 
651948d887dSJohannes Berg 	if (indicate_tim)
652d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
653c868cb35SJohannes Berg 	else
654d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6553e122be0SJohannes Berg 
6569b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
657c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
658948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
659c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
660004c872eSJohannes Berg 	}
661004c872eSJohannes Berg 
6623d5839b6SIlan Peer out_unlock:
66365f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
664004c872eSJohannes Berg }
665004c872eSJohannes Berg 
6669b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
6679b7a86f3SJohannes Berg {
6689b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
6699b7a86f3SJohannes Berg }
6709b7a86f3SJohannes Berg 
671cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
672f0706e82SJiri Benc {
673e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
674f0706e82SJiri Benc 	int timeout;
675f0706e82SJiri Benc 
676f0706e82SJiri Benc 	if (!skb)
677cd0b8d89SJohannes Berg 		return false;
678f0706e82SJiri Benc 
679e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
680f0706e82SJiri Benc 
681f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
68257c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
68357c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
68457c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
685f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
686f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
687e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
688f0706e82SJiri Benc }
689f0706e82SJiri Benc 
690f0706e82SJiri Benc 
691948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
692948d887dSJohannes Berg 						struct sta_info *sta, int ac)
693f0706e82SJiri Benc {
694f0706e82SJiri Benc 	unsigned long flags;
695f0706e82SJiri Benc 	struct sk_buff *skb;
696f0706e82SJiri Benc 
69760750397SJohannes Berg 	/*
69860750397SJohannes Berg 	 * First check for frames that should expire on the filtered
69960750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
70060750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
70160750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
70260750397SJohannes Berg 	 * total_ps_buffered counter.
70360750397SJohannes Berg 	 */
704f0706e82SJiri Benc 	for (;;) {
705948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
706948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
70757c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
708948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
709836341a7SJohannes Berg 		else
710f0706e82SJiri Benc 			skb = NULL;
711948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
712f0706e82SJiri Benc 
71360750397SJohannes Berg 		/*
71460750397SJohannes Berg 		 * Frames are queued in order, so if this one
71560750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
71660750397SJohannes Berg 		 * we actually reached the end of the queue we
71760750397SJohannes Berg 		 * also need to stop, of course.
71860750397SJohannes Berg 		 */
71960750397SJohannes Berg 		if (!skb)
72060750397SJohannes Berg 			break;
721d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
72260750397SJohannes Berg 	}
72360750397SJohannes Berg 
72460750397SJohannes Berg 	/*
72560750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
72660750397SJohannes Berg 	 * only find something if the filtered queue was emptied
72760750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
72860750397SJohannes Berg 	 * buffered frames.
72960750397SJohannes Berg 	 */
730f0706e82SJiri Benc 	for (;;) {
731948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
732948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
733f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
734948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
735f0706e82SJiri Benc 		else
736f0706e82SJiri Benc 			skb = NULL;
737948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
738f0706e82SJiri Benc 
73960750397SJohannes Berg 		/*
74060750397SJohannes Berg 		 * frames are queued in order, so if this one
74160750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
74260750397SJohannes Berg 		 * the queue) we can stop testing
74360750397SJohannes Berg 		 */
744836341a7SJohannes Berg 		if (!skb)
745836341a7SJohannes Berg 			break;
746836341a7SJohannes Berg 
747f0706e82SJiri Benc 		local->total_ps_buffered--;
748bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
749bdcbd8e0SJohannes Berg 		       sta->sta.addr);
750d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
751f0706e82SJiri Benc 	}
7523393a608SJuuso Oikarinen 
75360750397SJohannes Berg 	/*
75460750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
75560750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
75660750397SJohannes Berg 	 * frames.
75760750397SJohannes Berg 	 */
75860750397SJohannes Berg 	sta_info_recalc_tim(sta);
75960750397SJohannes Berg 
76060750397SJohannes Berg 	/*
76160750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
76260750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
76360750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
76460750397SJohannes Berg 	 */
765948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
766948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
767948d887dSJohannes Berg }
768948d887dSJohannes Berg 
769948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
770948d887dSJohannes Berg 					     struct sta_info *sta)
771948d887dSJohannes Berg {
772948d887dSJohannes Berg 	bool have_buffered = false;
773948d887dSJohannes Berg 	int ac;
774948d887dSJohannes Berg 
7753f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
7763f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
7773f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
778948d887dSJohannes Berg 		return false;
779948d887dSJohannes Berg 
780948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
781948d887dSJohannes Berg 		have_buffered |=
782948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
783948d887dSJohannes Berg 
784948d887dSJohannes Berg 	return have_buffered;
785f0706e82SJiri Benc }
786f0706e82SJiri Benc 
787d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
78834e89507SJohannes Berg {
78934e89507SJohannes Berg 	struct ieee80211_local *local;
79034e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
7916d10e46bSJohannes Berg 	int ret;
79234e89507SJohannes Berg 
79334e89507SJohannes Berg 	might_sleep();
79434e89507SJohannes Berg 
79534e89507SJohannes Berg 	if (!sta)
79634e89507SJohannes Berg 		return -ENOENT;
79734e89507SJohannes Berg 
79834e89507SJohannes Berg 	local = sta->local;
79934e89507SJohannes Berg 	sdata = sta->sdata;
80034e89507SJohannes Berg 
80183d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
80283d5cc01SJohannes Berg 
803098a6070SJohannes Berg 	/*
804098a6070SJohannes Berg 	 * Before removing the station from the driver and
805098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
806098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
807098a6070SJohannes Berg 	 * will be sufficient.
808098a6070SJohannes Berg 	 */
809c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
810c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
811098a6070SJohannes Berg 
81234e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
813b01711beSJohannes Berg 	if (WARN_ON(ret))
81434e89507SJohannes Berg 		return ret;
81534e89507SJohannes Berg 
816a7a6bdd0SArik Nemtsov 	/*
817a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
818a7a6bdd0SArik Nemtsov 	 * removal.
819a7a6bdd0SArik Nemtsov 	 */
820a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
821a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
822a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
823a7a6bdd0SArik Nemtsov 	}
824a7a6bdd0SArik Nemtsov 
825794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8264d33960bSJohannes Berg 
8276a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8286a9d1b91SJohannes Berg 
829a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
830a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
831a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
832a710c816SJohannes Berg 
833d778207bSJohannes Berg 	return 0;
834d778207bSJohannes Berg }
835d778207bSJohannes Berg 
836d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
837d778207bSJohannes Berg {
838d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
839d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8406f7a8d26SJohannes Berg 	struct station_info sinfo = {};
841d778207bSJohannes Berg 	int ret;
842d778207bSJohannes Berg 
843d778207bSJohannes Berg 	/*
844d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
845d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
846d778207bSJohannes Berg 	 */
847d778207bSJohannes Berg 
848d778207bSJohannes Berg 	might_sleep();
849d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
850d778207bSJohannes Berg 
851c8782078SJohannes Berg 	/* now keys can no longer be reached */
8526d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
85334e89507SJohannes Berg 
8549b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
8559b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
8569b7a86f3SJohannes Berg 
85734e89507SJohannes Berg 	sta->dead = true;
85834e89507SJohannes Berg 
85934e89507SJohannes Berg 	local->num_sta--;
86034e89507SJohannes Berg 	local->sta_generation++;
86134e89507SJohannes Berg 
86283d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
863f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
864f09603a2SJohannes Berg 		if (ret) {
86583d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
86683d5cc01SJohannes Berg 			break;
86783d5cc01SJohannes Berg 		}
86883d5cc01SJohannes Berg 	}
869d9a7ddb0SJohannes Berg 
870f09603a2SJohannes Berg 	if (sta->uploaded) {
871f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
872f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
873f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
874f09603a2SJohannes Berg 	}
87534e89507SJohannes Berg 
876bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
877bdcbd8e0SJohannes Berg 
8786f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
8796f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
880ec15e68bSJouni Malinen 
88134e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
88234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
88321f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
88434e89507SJohannes Berg 
885d34ba216SJohannes Berg 	cleanup_single_sta(sta);
886d778207bSJohannes Berg }
887d778207bSJohannes Berg 
888d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
889d778207bSJohannes Berg {
890d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
891d778207bSJohannes Berg 
892d778207bSJohannes Berg 	if (err)
893d778207bSJohannes Berg 		return err;
894d778207bSJohannes Berg 
895d778207bSJohannes Berg 	synchronize_net();
896d778207bSJohannes Berg 
897d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
89834e89507SJohannes Berg 
89934e89507SJohannes Berg 	return 0;
90034e89507SJohannes Berg }
90134e89507SJohannes Berg 
90234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
90334e89507SJohannes Berg {
90434e89507SJohannes Berg 	struct sta_info *sta;
90534e89507SJohannes Berg 	int ret;
90634e89507SJohannes Berg 
90734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9087852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
90934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
91034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
91134e89507SJohannes Berg 
91234e89507SJohannes Berg 	return ret;
91334e89507SJohannes Berg }
91434e89507SJohannes Berg 
91534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
91634e89507SJohannes Berg 			      const u8 *addr)
91734e89507SJohannes Berg {
91834e89507SJohannes Berg 	struct sta_info *sta;
91934e89507SJohannes Berg 	int ret;
92034e89507SJohannes Berg 
92134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9227852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
92334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
92434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
92534e89507SJohannes Berg 
92634e89507SJohannes Berg 	return ret;
92734e89507SJohannes Berg }
928f0706e82SJiri Benc 
929f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
930f0706e82SJiri Benc {
931f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
932f0706e82SJiri Benc 	struct sta_info *sta;
9333393a608SJuuso Oikarinen 	bool timer_needed = false;
934f0706e82SJiri Benc 
935d0709a65SJohannes Berg 	rcu_read_lock();
936d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9373393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9383393a608SJuuso Oikarinen 			timer_needed = true;
939d0709a65SJohannes Berg 	rcu_read_unlock();
940f0706e82SJiri Benc 
9415bb644a0SJohannes Berg 	if (local->quiescing)
9425bb644a0SJohannes Berg 		return;
9435bb644a0SJohannes Berg 
9443393a608SJuuso Oikarinen 	if (!timer_needed)
9453393a608SJuuso Oikarinen 		return;
9463393a608SJuuso Oikarinen 
94726d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
94826d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
949f0706e82SJiri Benc }
950f0706e82SJiri Benc 
951f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
952f0706e82SJiri Benc {
9534d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
95434e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
955f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
956f0706e82SJiri Benc 
957b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
958b24b8a24SPavel Emelyanov 		    (unsigned long)local);
959f0706e82SJiri Benc }
960f0706e82SJiri Benc 
961f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
962f0706e82SJiri Benc {
963a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
964f0706e82SJiri Benc }
965f0706e82SJiri Benc 
966051007d9SJohannes Berg 
967e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
968f0706e82SJiri Benc {
969b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
970f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
971d778207bSJohannes Berg 	LIST_HEAD(free_list);
97244213b5eSJohannes Berg 	int ret = 0;
973f0706e82SJiri Benc 
974d0709a65SJohannes Berg 	might_sleep();
975d0709a65SJohannes Berg 
976e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
977e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
978e716251dSJohannes Berg 
97934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
98034e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
981e716251dSJohannes Berg 		if (sdata == sta->sdata ||
982e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
983d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
984d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
98534316837SJohannes Berg 			ret++;
98634316837SJohannes Berg 		}
98734e89507SJohannes Berg 	}
988d778207bSJohannes Berg 
989d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
990d778207bSJohannes Berg 		synchronize_net();
991d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
992d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
993d778207bSJohannes Berg 	}
99434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
99544213b5eSJohannes Berg 
996051007d9SJohannes Berg 	return ret;
997051007d9SJohannes Berg }
998051007d9SJohannes Berg 
99924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
100024723d1bSJohannes Berg 			  unsigned long exp_time)
100124723d1bSJohannes Berg {
100224723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
100324723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
100424723d1bSJohannes Berg 
100534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1006e46a2cf9SMohammed Shafi Shajakhan 
1007e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1008ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1009ec2b774eSMarek Lindner 			continue;
1010ec2b774eSMarek Lindner 
101124723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1012eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1013bdcbd8e0SJohannes Berg 				sta->sta.addr);
10143f52b7e3SMarco Porsch 
10153f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10163f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10173f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10183f52b7e3SMarco Porsch 
101934e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
102024723d1bSJohannes Berg 		}
1021e46a2cf9SMohammed Shafi Shajakhan 	}
1022e46a2cf9SMohammed Shafi Shajakhan 
102334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
102424723d1bSJohannes Berg }
102517741cdcSJohannes Berg 
1026686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1027686b9cb9SBen Greear 					       const u8 *addr,
1028686b9cb9SBen Greear 					       const u8 *localaddr)
102917741cdcSJohannes Berg {
1030abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
103117741cdcSJohannes Berg 
1032686b9cb9SBen Greear 	/*
1033686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1034686b9cb9SBen Greear 	 * ... first in list.
1035686b9cb9SBen Greear 	 */
1036f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1037686b9cb9SBen Greear 		if (localaddr &&
1038b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1039686b9cb9SBen Greear 			continue;
1040f7c65594SJohannes Berg 		if (!sta->uploaded)
1041f7c65594SJohannes Berg 			return NULL;
104217741cdcSJohannes Berg 		return &sta->sta;
1043f7c65594SJohannes Berg 	}
1044f7c65594SJohannes Berg 
1045abe60632SJohannes Berg 	return NULL;
104617741cdcSJohannes Berg }
1047686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10485ed176e1SJohannes Berg 
10495ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10505ed176e1SJohannes Berg 					 const u8 *addr)
10515ed176e1SJohannes Berg {
1052f7c65594SJohannes Berg 	struct sta_info *sta;
10535ed176e1SJohannes Berg 
10545ed176e1SJohannes Berg 	if (!vif)
10555ed176e1SJohannes Berg 		return NULL;
10565ed176e1SJohannes Berg 
1057f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1058f7c65594SJohannes Berg 	if (!sta)
1059f7c65594SJohannes Berg 		return NULL;
10605ed176e1SJohannes Berg 
1061f7c65594SJohannes Berg 	if (!sta->uploaded)
1062f7c65594SJohannes Berg 		return NULL;
1063f7c65594SJohannes Berg 
1064f7c65594SJohannes Berg 	return &sta->sta;
10655ed176e1SJohannes Berg }
106617741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1067af818581SJohannes Berg 
1068e3685e03SJohannes Berg /* powersave support code */
1069e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
107050a9432dSJohannes Berg {
1071608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1072e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1073e3685e03SJohannes Berg 	struct sk_buff_head pending;
1074e3685e03SJohannes Berg 	int filtered = 0, buffered = 0, ac;
1075e3685e03SJohannes Berg 	unsigned long flags;
1076d012a605SMarco Porsch 	struct ps_data *ps;
1077d012a605SMarco Porsch 
10783918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
10793918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
10803918edb0SFelix Fietkau 				     u.ap);
10813918edb0SFelix Fietkau 
10823918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1083d012a605SMarco Porsch 		ps = &sdata->bss->ps;
10843f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
10853f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1086d012a605SMarco Porsch 	else
1087d012a605SMarco Porsch 		return;
108850a9432dSJohannes Berg 
1089c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
109047086fc5SJohannes Berg 
10915a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1092948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1093948d887dSJohannes Berg 
1094d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
109512375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1096af818581SJohannes Berg 
1097948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1098af818581SJohannes Berg 
10991d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11001d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1101af818581SJohannes Berg 	/* Send all buffered frames to the station */
1102948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1103948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1104948d887dSJohannes Berg 
1105987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1106948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1107987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1108948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1109948d887dSJohannes Berg 		filtered += tmp - count;
1110948d887dSJohannes Berg 		count = tmp;
1111948d887dSJohannes Berg 
1112987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1113948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1114987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1115948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1116948d887dSJohannes Berg 		buffered += tmp - count;
1117948d887dSJohannes Berg 	}
1118948d887dSJohannes Berg 
1119e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11205ac2e350SJohannes Berg 
11215ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11225ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11235ac2e350SJohannes Berg 
11245ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
11255ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
11265ac2e350SJohannes Berg 	 */
11275ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
11285ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
11291d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1130948d887dSJohannes Berg 
1131e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1132e3685e03SJohannes Berg 
1133687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1134062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1135062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1136687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1137687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1138687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1139687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1140687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1141687da132SEmmanuel Grumbach 		       sta->sta.addr);
1142687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1143687da132SEmmanuel Grumbach 					   sta->sta.addr,
1144687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1145687da132SEmmanuel Grumbach 	}
1146687da132SEmmanuel Grumbach 
1147af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1148af818581SJohannes Berg 
1149c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1150c868cb35SJohannes Berg 
1151bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1152bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1153bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1154af818581SJohannes Berg }
1155af818581SJohannes Berg 
1156ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1157ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1158b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1159b77cf4f8SJohannes Berg 					 bool call_driver)
1160ce662b44SJohannes Berg {
1161ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1162ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1163ce662b44SJohannes Berg 	struct sk_buff *skb;
1164ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1165ce662b44SJohannes Berg 	__le16 fc;
1166a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1167ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
116855de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1169ce662b44SJohannes Berg 
1170ce662b44SJohannes Berg 	if (qos) {
1171ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1172ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1173ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1174ce662b44SJohannes Berg 	} else {
1175ce662b44SJohannes Berg 		size -= 2;
1176ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1177ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1178ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1179ce662b44SJohannes Berg 	}
1180ce662b44SJohannes Berg 
1181ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1182ce662b44SJohannes Berg 	if (!skb)
1183ce662b44SJohannes Berg 		return;
1184ce662b44SJohannes Berg 
1185ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1186ce662b44SJohannes Berg 
1187ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1188ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1189ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1190ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1191ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1192ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1193864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1194ce662b44SJohannes Berg 
1195ce662b44SJohannes Berg 	skb->priority = tid;
1196ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
119759b66255SJohannes Berg 	if (qos) {
1198ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1199ce662b44SJohannes Berg 
120040b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1201ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1202ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1203ce662b44SJohannes Berg 	}
1204ce662b44SJohannes Berg 
1205ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1206ce662b44SJohannes Berg 
1207ce662b44SJohannes Berg 	/*
1208ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1209ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1210deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1211deeaee19SJohannes Berg 	 * ends the poll/service period.
1212ce662b44SJohannes Berg 	 */
121302f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1214deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1215ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1216ce662b44SJohannes Berg 
12176b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
12186b127c71SSujith Manoharan 
1219b77cf4f8SJohannes Berg 	if (call_driver)
1220b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1221b77cf4f8SJohannes Berg 					  reason, false);
122240b96408SJohannes Berg 
122389afe614SJohannes Berg 	skb->dev = sdata->dev;
122489afe614SJohannes Berg 
122555de908aSJohannes Berg 	rcu_read_lock();
122655de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
122755de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
122855de908aSJohannes Berg 		rcu_read_unlock();
122955de908aSJohannes Berg 		kfree_skb(skb);
123055de908aSJohannes Berg 		return;
123155de908aSJohannes Berg 	}
123255de908aSJohannes Berg 
123373c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
123473c4e195SJohannes Berg 	ieee80211_xmit(sdata, skb);
123555de908aSJohannes Berg 	rcu_read_unlock();
1236ce662b44SJohannes Berg }
1237ce662b44SJohannes Berg 
12380a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
12390a1cb809SJohannes Berg {
12400a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
12410a1cb809SJohannes Berg 	if (tids & 0xF8)
12420a1cb809SJohannes Berg 		return fls(tids) - 1;
12430a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
12440a1cb809SJohannes Berg 	if (tids & BIT(0))
12450a1cb809SJohannes Berg 		return 0;
12460a1cb809SJohannes Berg 	return fls(tids) - 1;
12470a1cb809SJohannes Berg }
12480a1cb809SJohannes Berg 
124947086fc5SJohannes Berg static void
125047086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
125147086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
125247086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1253af818581SJohannes Berg {
1254af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1255af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1256948d887dSJohannes Berg 	bool more_data = false;
1257948d887dSJohannes Berg 	int ac;
12584049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
125947086fc5SJohannes Berg 	struct sk_buff_head frames;
126047086fc5SJohannes Berg 
1261deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1262c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1263deeaee19SJohannes Berg 
126447086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1265af818581SJohannes Berg 
1266f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1267948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
12684049e09aSJohannes Berg 		unsigned long tids;
12694049e09aSJohannes Berg 
127047086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1271948d887dSJohannes Berg 			continue;
1272948d887dSJohannes Berg 
12734049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
12744049e09aSJohannes Berg 
1275f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1276f9f760b4SJohannes Berg 		 * release from hardware queues
1277f9f760b4SJohannes Berg 		 */
1278f9f760b4SJohannes Berg 		if (skb_queue_empty(&frames))
1279f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1280f9f760b4SJohannes Berg 
12814049e09aSJohannes Berg 		if (driver_release_tids) {
1282f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1283f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1284f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1285f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1286f9f760b4SJohannes Berg 			 */
1287f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1288f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1289f9f760b4SJohannes Berg 				more_data = true;
1290f9f760b4SJohannes Berg 				driver_release_tids =
1291f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1292f9f760b4SJohannes Berg 						driver_release_tids));
1293f9f760b4SJohannes Berg 				break;
1294f9f760b4SJohannes Berg 			}
12954049e09aSJohannes Berg 		} else {
129647086fc5SJohannes Berg 			struct sk_buff *skb;
129747086fc5SJohannes Berg 
129847086fc5SJohannes Berg 			while (n_frames > 0) {
1299948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1300948d887dSJohannes Berg 				if (!skb) {
130147086fc5SJohannes Berg 					skb = skb_dequeue(
130247086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1303af818581SJohannes Berg 					if (skb)
1304af818581SJohannes Berg 						local->total_ps_buffered--;
1305af818581SJohannes Berg 				}
130647086fc5SJohannes Berg 				if (!skb)
130747086fc5SJohannes Berg 					break;
130847086fc5SJohannes Berg 				n_frames--;
130947086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
131047086fc5SJohannes Berg 			}
1311948d887dSJohannes Berg 		}
1312948d887dSJohannes Berg 
1313f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1314f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1315f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13164049e09aSJohannes Berg 		 */
1317948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1318948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1319948d887dSJohannes Berg 			more_data = true;
1320948d887dSJohannes Berg 			break;
1321948d887dSJohannes Berg 		}
1322948d887dSJohannes Berg 	}
1323af818581SJohannes Berg 
1324f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1325ce662b44SJohannes Berg 		int tid;
13264049e09aSJohannes Berg 
1327ce662b44SJohannes Berg 		/*
1328ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1329ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1330ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1331ce662b44SJohannes Berg 		 *
1332ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1333ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1334ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1335ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1336ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1337ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1338ce662b44SJohannes Berg 		 *
1339ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1340ce662b44SJohannes Berg 		 */
1341ce662b44SJohannes Berg 
1342ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1343ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1344ce662b44SJohannes Berg 
1345b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1346f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
134747086fc5SJohannes Berg 		struct sk_buff_head pending;
134847086fc5SJohannes Berg 		struct sk_buff *skb;
134940b96408SJohannes Berg 		int num = 0;
135040b96408SJohannes Berg 		u16 tids = 0;
1351b77cf4f8SJohannes Berg 		bool need_null = false;
135247086fc5SJohannes Berg 
135347086fc5SJohannes Berg 		skb_queue_head_init(&pending);
135447086fc5SJohannes Berg 
135547086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1356af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
135747086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
135840b96408SJohannes Berg 			u8 *qoshdr = NULL;
135940b96408SJohannes Berg 
136040b96408SJohannes Berg 			num++;
1361af818581SJohannes Berg 
1362af818581SJohannes Berg 			/*
136347086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
136447086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
136547086fc5SJohannes Berg 			 * exchange.
1366af818581SJohannes Berg 			 */
13676b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
13686b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1369af818581SJohannes Berg 
137047086fc5SJohannes Berg 			/*
137147086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
137247086fc5SJohannes Berg 			 * more buffered frames for this STA
137347086fc5SJohannes Berg 			 */
137424b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
137547086fc5SJohannes Berg 				hdr->frame_control |=
137647086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
137724b9c373SJanusz.Dziedzic@tieto.com 			else
137824b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
137924b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1380af818581SJohannes Berg 
138140b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
138240b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
138340b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
138440b96408SJohannes Berg 
1385b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1386b77cf4f8SJohannes Berg 
1387b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1388b77cf4f8SJohannes Berg 
1389b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1390b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1391b77cf4f8SJohannes Berg 				continue;
1392b77cf4f8SJohannes Berg 
1393b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1394b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1395b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1396b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1397b77cf4f8SJohannes Berg 				break;
1398b77cf4f8SJohannes Berg 			}
1399b77cf4f8SJohannes Berg 
1400b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1401b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1402b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1403b77cf4f8SJohannes Berg 			 * and be done.
1404b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1405b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1406b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1407b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1408b77cf4f8SJohannes Berg 			 *
1409b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1410b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1411b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1412b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1413b77cf4f8SJohannes Berg 			 *
1414b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1415b77cf4f8SJohannes Berg 			 */
1416b77cf4f8SJohannes Berg 			if (qoshdr) {
141740b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1418deeaee19SJohannes Berg 
141947086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
142047086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1421b77cf4f8SJohannes Berg 			} else {
1422b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1423b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1424b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1425b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1426b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1427b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1428b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1429b77cf4f8SJohannes Berg 				 */
1430b77cf4f8SJohannes Berg 				hdr->frame_control |=
1431b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1432b77cf4f8SJohannes Berg 				need_null = true;
1433b77cf4f8SJohannes Berg 				num++;
143452a3f20cSMarco Porsch 			}
1435b77cf4f8SJohannes Berg 			break;
143647086fc5SJohannes Berg 		}
143747086fc5SJohannes Berg 
143840b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
143940b96408SJohannes Berg 					  reason, more_data);
144040b96408SJohannes Berg 
144147086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1442af818581SJohannes Berg 
1443b77cf4f8SJohannes Berg 		if (need_null)
1444b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1445b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1446b77cf4f8SJohannes Berg 				reason, false);
1447b77cf4f8SJohannes Berg 
1448c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1449af818581SJohannes Berg 	} else {
1450af818581SJohannes Berg 		/*
14514049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
14524049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1453f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1454f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1455f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1456f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1457f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1458f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1459af818581SJohannes Berg 		 */
14604049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
146147086fc5SJohannes Berg 					    n_frames, reason, more_data);
14624049e09aSJohannes Berg 
14634049e09aSJohannes Berg 		/*
14644049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
14654049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1466f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
14674049e09aSJohannes Berg 		 * release function.
1468f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
14694049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
14704049e09aSJohannes Berg 		 */
1471af818581SJohannes Berg 	}
1472af818581SJohannes Berg }
1473af818581SJohannes Berg 
1474af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1475af818581SJohannes Berg {
147647086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1477af818581SJohannes Berg 
1478af818581SJohannes Berg 	/*
147947086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
148047086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
148147086fc5SJohannes Berg 	 * only from the non-enabled ones.
1482af818581SJohannes Berg 	 */
148347086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
148447086fc5SJohannes Berg 		ignore_for_response = 0;
1485af818581SJohannes Berg 
148647086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
148747086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1488af818581SJohannes Berg }
148947086fc5SJohannes Berg 
149047086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
149147086fc5SJohannes Berg {
149247086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
149347086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
149447086fc5SJohannes Berg 
149547086fc5SJohannes Berg 	/*
149647086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
149747086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
149847086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
149947086fc5SJohannes Berg 	 * actually getting called.
150047086fc5SJohannes Berg 	 */
150147086fc5SJohannes Berg 	if (!delivery_enabled)
150247086fc5SJohannes Berg 		return;
150347086fc5SJohannes Berg 
150447086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
150547086fc5SJohannes Berg 	case 1:
150647086fc5SJohannes Berg 		n_frames = 2;
150747086fc5SJohannes Berg 		break;
150847086fc5SJohannes Berg 	case 2:
150947086fc5SJohannes Berg 		n_frames = 4;
151047086fc5SJohannes Berg 		break;
151147086fc5SJohannes Berg 	case 3:
151247086fc5SJohannes Berg 		n_frames = 6;
151347086fc5SJohannes Berg 		break;
151447086fc5SJohannes Berg 	case 0:
151547086fc5SJohannes Berg 		/* XXX: what is a good value? */
151613a8098aSAndrei Otcheretianski 		n_frames = 128;
151747086fc5SJohannes Berg 		break;
151847086fc5SJohannes Berg 	}
151947086fc5SJohannes Berg 
152047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
152147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1522af818581SJohannes Berg }
1523af818581SJohannes Berg 
1524af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1525af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1526af818581SJohannes Berg {
1527af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1528af818581SJohannes Berg 
1529b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1530b5878a2dSJohannes Berg 
15315ac2e350SJohannes Berg 	if (block) {
1532c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
15335ac2e350SJohannes Berg 		return;
15345ac2e350SJohannes Berg 	}
15355ac2e350SJohannes Berg 
15365ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
15375ac2e350SJohannes Berg 		return;
15385ac2e350SJohannes Berg 
15395ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
15405ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
15415ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15425ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15435ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
15445ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
15455ac2e350SJohannes Berg 		/* must be asleep in this case */
15465ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15475ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15485ac2e350SJohannes Berg 	} else {
15495ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15505ac2e350SJohannes Berg 	}
1551af818581SJohannes Berg }
1552af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1553dcf55fb5SFelix Fietkau 
1554e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
155537fbd908SJohannes Berg {
155637fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
155737fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
155837fbd908SJohannes Berg 
155937fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
156037fbd908SJohannes Berg 
156137fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
156237fbd908SJohannes Berg }
1563e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
156437fbd908SJohannes Berg 
1565042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1566042ec453SJohannes Berg 				u8 tid, bool buffered)
1567dcf55fb5SFelix Fietkau {
1568dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1569dcf55fb5SFelix Fietkau 
15705a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1571042ec453SJohannes Berg 		return;
1572042ec453SJohannes Berg 
15731b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
15741b000789SJohannes Berg 
1575948d887dSJohannes Berg 	if (buffered)
1576948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1577948d887dSJohannes Berg 	else
1578948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1579948d887dSJohannes Berg 
1580c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1581dcf55fb5SFelix Fietkau }
1582042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1583d9a7ddb0SJohannes Berg 
158483d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1585d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1586d9a7ddb0SJohannes Berg {
15878bf11d8dSJohannes Berg 	might_sleep();
1588d9a7ddb0SJohannes Berg 
1589d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1590d9a7ddb0SJohannes Berg 		return 0;
1591d9a7ddb0SJohannes Berg 
1592f09603a2SJohannes Berg 	/* check allowed transitions first */
1593f09603a2SJohannes Berg 
1594d9a7ddb0SJohannes Berg 	switch (new_state) {
1595d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1596f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1597d9a7ddb0SJohannes Berg 			return -EINVAL;
1598d9a7ddb0SJohannes Berg 		break;
1599d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1600f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1601f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1602d9a7ddb0SJohannes Berg 			return -EINVAL;
1603d9a7ddb0SJohannes Berg 		break;
1604d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1605f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1606f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1607d9a7ddb0SJohannes Berg 			return -EINVAL;
1608d9a7ddb0SJohannes Berg 		break;
1609d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1610f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1611d9a7ddb0SJohannes Berg 			return -EINVAL;
1612d9a7ddb0SJohannes Berg 		break;
1613d9a7ddb0SJohannes Berg 	default:
1614d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1615d9a7ddb0SJohannes Berg 		return -EINVAL;
1616d9a7ddb0SJohannes Berg 	}
1617d9a7ddb0SJohannes Berg 
1618bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1619bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1620f09603a2SJohannes Berg 
1621f09603a2SJohannes Berg 	/*
1622f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1623f09603a2SJohannes Berg 	 * fail the transition
1624f09603a2SJohannes Berg 	 */
1625f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1626f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1627f09603a2SJohannes Berg 					sta->sta_state, new_state);
1628f09603a2SJohannes Berg 		if (err)
1629f09603a2SJohannes Berg 			return err;
1630f09603a2SJohannes Berg 	}
1631f09603a2SJohannes Berg 
1632f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1633f09603a2SJohannes Berg 
1634f09603a2SJohannes Berg 	switch (new_state) {
1635f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1636f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1637f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1638f09603a2SJohannes Berg 		break;
1639f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1640f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1641f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1642f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1643f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1644f09603a2SJohannes Berg 		break;
1645f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1646f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1647f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1648f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
16497e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16507e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16517e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16527e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1653f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1654f09603a2SJohannes Berg 		}
1655f09603a2SJohannes Berg 		break;
1656f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1657f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
16587e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16597e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16607e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16617e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1662f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1663f09603a2SJohannes Berg 		}
1664f09603a2SJohannes Berg 		break;
1665f09603a2SJohannes Berg 	default:
1666f09603a2SJohannes Berg 		break;
1667f09603a2SJohannes Berg 	}
1668f09603a2SJohannes Berg 
1669d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1670d9a7ddb0SJohannes Berg 
1671d9a7ddb0SJohannes Berg 	return 0;
1672d9a7ddb0SJohannes Berg }
1673687da132SEmmanuel Grumbach 
1674687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1675687da132SEmmanuel Grumbach {
1676687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1677687da132SEmmanuel Grumbach 	u8 rx_streams;
1678687da132SEmmanuel Grumbach 
1679687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1680687da132SEmmanuel Grumbach 		return 1;
1681687da132SEmmanuel Grumbach 
1682687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1683687da132SEmmanuel Grumbach 		int i;
1684687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1685687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1686687da132SEmmanuel Grumbach 
1687687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1688687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1689687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1690687da132SEmmanuel Grumbach 				return i + 1;
1691687da132SEmmanuel Grumbach 	}
1692687da132SEmmanuel Grumbach 
1693687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1694687da132SEmmanuel Grumbach 		rx_streams = 4;
1695687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1696687da132SEmmanuel Grumbach 		rx_streams = 3;
1697687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1698687da132SEmmanuel Grumbach 		rx_streams = 2;
1699687da132SEmmanuel Grumbach 	else
1700687da132SEmmanuel Grumbach 		rx_streams = 1;
1701687da132SEmmanuel Grumbach 
1702687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1703687da132SEmmanuel Grumbach 		return rx_streams;
1704687da132SEmmanuel Grumbach 
1705687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1706687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1707687da132SEmmanuel Grumbach }
1708b7ffbd7eSJohannes Berg 
1709b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1710b7ffbd7eSJohannes Berg {
1711b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1712b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
17139a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1714b7ffbd7eSJohannes Berg 	struct timespec uptime;
1715b7ffbd7eSJohannes Berg 	u32 thr = 0;
1716b7ffbd7eSJohannes Berg 	int i, ac;
1717b7ffbd7eSJohannes Berg 
17189a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
17199a244409SJohn W. Linville 		ref = local->rate_ctrl;
17209a244409SJohn W. Linville 
1721b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1722b7ffbd7eSJohannes Berg 
1723225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1724225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1725225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1726225b8189SJohannes Berg 	 */
1727225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1728225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1729225b8189SJohannes Berg 
17302b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
17312b9a7e1bSJohannes Berg 
1732319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1733319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1734319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1735319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1736319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1737319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BEACON_LOSS);
1738b7ffbd7eSJohannes Berg 
173918171520SThomas Gleixner 	ktime_get_ts(&uptime);
1740b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1741b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
17422b9a7e1bSJohannes Berg 
1743319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1744319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1745b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
17462b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1747b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1748319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1749b7ffbd7eSJohannes Berg 	}
17502b9a7e1bSJohannes Berg 
1751319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
17522b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
17532b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
17542b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1755319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
17562b9a7e1bSJohannes Berg 	}
17572b9a7e1bSJohannes Berg 
1758319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1759319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1760b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1761319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
17622b9a7e1bSJohannes Berg 	}
17632b9a7e1bSJohannes Berg 
1764319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1765b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1766319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
17672b9a7e1bSJohannes Berg 	}
17682b9a7e1bSJohannes Berg 
1769319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1770b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1771319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
17722b9a7e1bSJohannes Berg 	}
17732b9a7e1bSJohannes Berg 
1774319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1775b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1776319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
17772b9a7e1bSJohannes Berg 	}
17782b9a7e1bSJohannes Berg 
1779b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1780b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1781b7ffbd7eSJohannes Berg 
1782225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1783225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1784225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1785225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1786225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1787225b8189SJohannes Berg 	}
1788225b8189SJohannes Berg 
1789b7ffbd7eSJohannes Berg 	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1790b7ffbd7eSJohannes Berg 	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1791319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1792b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1793319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1794b7ffbd7eSJohannes Berg 		}
17952b9a7e1bSJohannes Berg 
1796319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
17972b9a7e1bSJohannes Berg 			sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1798319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
17992b9a7e1bSJohannes Berg 		}
18002b9a7e1bSJohannes Berg 	}
18012b9a7e1bSJohannes Berg 
18022b9a7e1bSJohannes Berg 	if (sta->chains &&
1803319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1804319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1805319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1806319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1807b7ffbd7eSJohannes Berg 
1808b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1809b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1810b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1811b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1812b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1813b7ffbd7eSJohannes Berg 		}
1814b7ffbd7eSJohannes Berg 	}
1815b7ffbd7eSJohannes Berg 
1816319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1817b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1818319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
18192b9a7e1bSJohannes Berg 	}
18202b9a7e1bSJohannes Berg 
1821319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1822b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1823319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
18242b9a7e1bSJohannes Berg 	}
1825b7ffbd7eSJohannes Berg 
182679c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
182779c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
182879c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
182979c892b8SJohannes Berg 
183079c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
183179c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
183279c892b8SJohannes Berg 			tidstats->rx_msdu = sta->rx_msdu[i];
183379c892b8SJohannes Berg 		}
183479c892b8SJohannes Berg 
183579c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
183679c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
183779c892b8SJohannes Berg 			tidstats->tx_msdu = sta->tx_msdu[i];
183879c892b8SJohannes Berg 		}
183979c892b8SJohannes Berg 
184079c892b8SJohannes Berg 		if (!(tidstats->filled &
184179c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
184279c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
184379c892b8SJohannes Berg 			tidstats->filled |=
184479c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
184579c892b8SJohannes Berg 			tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
184679c892b8SJohannes Berg 		}
184779c892b8SJohannes Berg 
184879c892b8SJohannes Berg 		if (!(tidstats->filled &
184979c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
185079c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
185179c892b8SJohannes Berg 			tidstats->filled |=
185279c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
185379c892b8SJohannes Berg 			tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
185479c892b8SJohannes Berg 		}
185579c892b8SJohannes Berg 	}
185679c892b8SJohannes Berg 
1857b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1858b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1859319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1860319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
1861319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
1862319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
1863319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
1864319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
1865b7ffbd7eSJohannes Berg 
1866b7ffbd7eSJohannes Berg 		sinfo->llid = sta->llid;
1867b7ffbd7eSJohannes Berg 		sinfo->plid = sta->plid;
1868b7ffbd7eSJohannes Berg 		sinfo->plink_state = sta->plink_state;
1869b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1870319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1871b7ffbd7eSJohannes Berg 			sinfo->t_offset = sta->t_offset;
1872b7ffbd7eSJohannes Berg 		}
1873b7ffbd7eSJohannes Berg 		sinfo->local_pm = sta->local_pm;
1874b7ffbd7eSJohannes Berg 		sinfo->peer_pm = sta->peer_pm;
1875b7ffbd7eSJohannes Berg 		sinfo->nonpeer_pm = sta->nonpeer_pm;
1876b7ffbd7eSJohannes Berg #endif
1877b7ffbd7eSJohannes Berg 	}
1878b7ffbd7eSJohannes Berg 
1879b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1880b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1881b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1882b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1883b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1884b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1885b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1886785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1887b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1888b7ffbd7eSJohannes Berg 
1889b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1890b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1891b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1892b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1893b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1894b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1895b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1896b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1897b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1898b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1899b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1900b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1901a74a8c84SJohannes Berg 	if (sta->sta.wme)
1902b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1903b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
1904b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1905b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
1906b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1907b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
1908b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1909b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1910b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1911b7ffbd7eSJohannes Berg 
1912b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
1913b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
1914b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1915b7ffbd7eSJohannes Berg 	else
1916b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
1917b7ffbd7eSJohannes Berg 
1918b7ffbd7eSJohannes Berg 	if (thr != 0) {
1919319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
1920b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
1921b7ffbd7eSJohannes Berg 	}
1922b7ffbd7eSJohannes Berg }
1923