xref: /openbmc/linux/net/mac80211/sta_info.c (revision 225b818982403120ce1f5e7d4b3e5245e0399775)
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 {
232ad38bfc9SMatti Gottlieb 	int i;
233ad38bfc9SMatti Gottlieb 
234889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2354b7679a5SJohannes Berg 		rate_control_free_sta(sta);
23693e5deb1SJohannes Berg 
237ad38bfc9SMatti Gottlieb 	if (sta->tx_lat) {
238ad38bfc9SMatti Gottlieb 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
239ad38bfc9SMatti Gottlieb 			kfree(sta->tx_lat[i].bins);
240ad38bfc9SMatti Gottlieb 		kfree(sta->tx_lat);
241ad38bfc9SMatti Gottlieb 	}
242ad38bfc9SMatti Gottlieb 
243bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
24493e5deb1SJohannes Berg 
24553d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
24693e5deb1SJohannes Berg 	kfree(sta);
24793e5deb1SJohannes Berg }
24893e5deb1SJohannes Berg 
2494d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
250d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
251d0709a65SJohannes Berg 			      struct sta_info *sta)
252f0706e82SJiri Benc {
2534d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
25417741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
255cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
256f0706e82SJiri Benc }
257f0706e82SJiri Benc 
2585ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
259af818581SJohannes Berg {
260af818581SJohannes Berg 	struct sta_info *sta;
261af818581SJohannes Berg 
2625ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
263af818581SJohannes Berg 
264af818581SJohannes Berg 	if (sta->dead)
265af818581SJohannes Berg 		return;
266af818581SJohannes Berg 
26754420473SHelmut Schaa 	local_bh_disable();
2685ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
269af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2705ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
271af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2725ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
27347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
274ce662b44SJohannes Berg 	local_bh_enable();
275af818581SJohannes Berg }
276af818581SJohannes Berg 
277af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
278af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
279af65cd96SJohannes Berg {
280af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
281af65cd96SJohannes Berg 		return 0;
282af65cd96SJohannes Berg 
283889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
284af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
285af65cd96SJohannes Berg 						     &sta->sta, gfp);
286889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
287af65cd96SJohannes Berg 		return -ENOMEM;
288af65cd96SJohannes Berg 
289af65cd96SJohannes Berg 	return 0;
290af65cd96SJohannes Berg }
291af65cd96SJohannes Berg 
29273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
29356544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
294f0706e82SJiri Benc {
295d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
296f0706e82SJiri Benc 	struct sta_info *sta;
297ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
298ad38bfc9SMatti Gottlieb 	struct ieee80211_tx_latency_bin_ranges *tx_latency;
29916c5f15cSRon Rindjunsky 	int i;
300f0706e82SJiri Benc 
30117741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
302f0706e82SJiri Benc 	if (!sta)
30373651ee6SJohannes Berg 		return NULL;
304f0706e82SJiri Benc 
305ef04a297SJohannes Berg 	rcu_read_lock();
306ef04a297SJohannes Berg 	tx_latency = rcu_dereference(local->tx_latency);
307ef04a297SJohannes Berg 	/* init stations Tx latency statistics && TID bins */
308ef04a297SJohannes Berg 	if (tx_latency) {
309ef04a297SJohannes Berg 		sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
310ef04a297SJohannes Berg 				      sizeof(struct ieee80211_tx_latency_stat),
311ef04a297SJohannes Berg 				      GFP_ATOMIC);
312ef04a297SJohannes Berg 		if (!sta->tx_lat) {
313ef04a297SJohannes Berg 			rcu_read_unlock();
314ef04a297SJohannes Berg 			goto free;
315ef04a297SJohannes Berg 		}
316ef04a297SJohannes Berg 
317ef04a297SJohannes Berg 		if (tx_latency->n_ranges) {
318ef04a297SJohannes Berg 			for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
319ef04a297SJohannes Berg 				/* size of bins is size of the ranges +1 */
320ef04a297SJohannes Berg 				sta->tx_lat[i].bin_count =
321ef04a297SJohannes Berg 					tx_latency->n_ranges + 1;
322ef04a297SJohannes Berg 				sta->tx_lat[i].bins =
323ef04a297SJohannes Berg 					kcalloc(sta->tx_lat[i].bin_count,
324ef04a297SJohannes Berg 						sizeof(u32), GFP_ATOMIC);
325ef04a297SJohannes Berg 				if (!sta->tx_lat[i].bins) {
326ef04a297SJohannes Berg 					rcu_read_unlock();
327ef04a297SJohannes Berg 					goto free;
328ef04a297SJohannes Berg 				}
329ef04a297SJohannes Berg 			}
330ef04a297SJohannes Berg 		}
331ef04a297SJohannes Berg 	}
332ef04a297SJohannes Berg 	rcu_read_unlock();
333ef04a297SJohannes Berg 
33407346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3351d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3365ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
33767c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
338a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
33987f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
34087f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
34187f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
34287f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3436c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
34487f59c70SThomas Pedersen #endif
34507346f81SJohannes Berg 
34617741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
347d0709a65SJohannes Berg 	sta->local = local;
348d0709a65SJohannes Berg 	sta->sdata = sdata;
3498bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
350f0706e82SJiri Benc 
35171ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35271ec375cSJohannes Berg 
353b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
354b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
355b6da911bSLiad Kaufman 
35618171520SThomas Gleixner 	ktime_get_ts(&uptime);
357ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
358541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
359ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
360ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
361541a45a1SBruno Randolf 
362ef04a297SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp))
363ef04a297SJohannes Berg 		goto free;
364f0706e82SJiri Benc 
3655a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
366a622ab72SJohannes Berg 		/*
367a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
368a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
369a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
370a622ab72SJohannes Berg 		 */
37116c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
37216c5f15cSRon Rindjunsky 	}
373948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
374948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
375948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
376948d887dSJohannes Berg 	}
37773651ee6SJohannes Berg 
3785a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3794be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
380cccaec98SSenthil Balasubramanian 
381af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
382687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
383687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
384687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
385687da132SEmmanuel Grumbach 			local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
386687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
387687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
388687da132SEmmanuel Grumbach 		/*
389687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
390687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
391687da132SEmmanuel Grumbach 		 */
392687da132SEmmanuel Grumbach 		switch (smps) {
393687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
394687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
395687da132SEmmanuel Grumbach 			break;
396687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
397687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
398687da132SEmmanuel Grumbach 			break;
399687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
400687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
401687da132SEmmanuel Grumbach 			break;
402687da132SEmmanuel Grumbach 		default:
403687da132SEmmanuel Grumbach 			WARN_ON(1);
404687da132SEmmanuel Grumbach 		}
405687da132SEmmanuel Grumbach 	}
406af0ed69bSJohannes Berg 
407bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
40873651ee6SJohannes Berg 	return sta;
409ef04a297SJohannes Berg 
410ef04a297SJohannes Berg free:
411ef04a297SJohannes Berg 	if (sta->tx_lat) {
412ef04a297SJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
413ef04a297SJohannes Berg 			kfree(sta->tx_lat[i].bins);
414ef04a297SJohannes Berg 		kfree(sta->tx_lat);
415ef04a297SJohannes Berg 	}
416ef04a297SJohannes Berg 	kfree(sta);
417ef04a297SJohannes Berg 	return NULL;
41873651ee6SJohannes Berg }
41973651ee6SJohannes Berg 
4208c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
42134e89507SJohannes Berg {
42234e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42334e89507SJohannes Berg 
42403e4497eSJohannes Berg 	/*
42503e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
42603e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
42703e4497eSJohannes Berg 	 * and another CPU turns off the net device.
42803e4497eSJohannes Berg 	 */
4298c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4308c71df7aSGuy Eilam 		return -ENETDOWN;
43103e4497eSJohannes Berg 
432b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4338c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4348c71df7aSGuy Eilam 		return -EINVAL;
4358c71df7aSGuy Eilam 
4368c71df7aSGuy Eilam 	return 0;
43793e5deb1SJohannes Berg }
43844213b5eSJohannes Berg 
439f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
440f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
441f09603a2SJohannes Berg 				     struct sta_info *sta)
442f09603a2SJohannes Berg {
443f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
444f09603a2SJohannes Berg 	int err = 0;
445f09603a2SJohannes Berg 
446f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
447f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
448f09603a2SJohannes Berg 		if (err)
449f09603a2SJohannes Berg 			break;
450f09603a2SJohannes Berg 	}
451f09603a2SJohannes Berg 
452f09603a2SJohannes Berg 	if (!err) {
453a4ec45a4SJohannes Berg 		/*
454a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
455a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
456a4ec45a4SJohannes Berg 		 */
457a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
458f09603a2SJohannes Berg 			sta->uploaded = true;
459f09603a2SJohannes Berg 		return 0;
460f09603a2SJohannes Berg 	}
461f09603a2SJohannes Berg 
462f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
463bdcbd8e0SJohannes Berg 		sdata_info(sdata,
464bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
465bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
466f09603a2SJohannes Berg 		err = 0;
467f09603a2SJohannes Berg 	}
468f09603a2SJohannes Berg 
469f09603a2SJohannes Berg 	/* unwind on error */
470f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
471f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
472f09603a2SJohannes Berg 
473f09603a2SJohannes Berg 	return err;
474f09603a2SJohannes Berg }
475f09603a2SJohannes Berg 
47634e89507SJohannes Berg /*
4778c71df7aSGuy Eilam  * should be called with sta_mtx locked
4788c71df7aSGuy Eilam  * this function replaces the mutex lock
4798c71df7aSGuy Eilam  * with a RCU lock
4808c71df7aSGuy Eilam  */
4814d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4828c71df7aSGuy Eilam {
4838c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4848c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4857852e361SJohannes Berg 	struct station_info sinfo;
4868c71df7aSGuy Eilam 	int err = 0;
4878c71df7aSGuy Eilam 
4888c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4898c71df7aSGuy Eilam 
4907852e361SJohannes Berg 	/* check if STA exists already */
4917852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4924d33960bSJohannes Berg 		err = -EEXIST;
4934d33960bSJohannes Berg 		goto out_err;
49434e89507SJohannes Berg 	}
49534e89507SJohannes Berg 
4964d33960bSJohannes Berg 	local->num_sta++;
4974d33960bSJohannes Berg 	local->sta_generation++;
4984d33960bSJohannes Berg 	smp_mb();
4994d33960bSJohannes Berg 
5005108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5015108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5025108ca82SJohannes Berg 
5034d33960bSJohannes Berg 	/* make the station visible */
5044d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5054d33960bSJohannes Berg 
5062bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
50783d5cc01SJohannes Berg 
5085108ca82SJohannes Berg 	/* notify driver */
5095108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5105108ca82SJohannes Berg 	if (err)
5115108ca82SJohannes Berg 		goto out_remove;
5125108ca82SJohannes Berg 
51383d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5145108ca82SJohannes Berg 	/* accept BA sessions now */
5155108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5164d33960bSJohannes Berg 
51721f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
5184d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5194d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5204d33960bSJohannes Berg 
5214d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5224d33960bSJohannes Berg 	sinfo.filled = 0;
5234d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5244d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
525d0709a65SJohannes Berg 
526bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
527f0706e82SJiri Benc 
52834e89507SJohannes Berg 	/* move reference to rcu-protected */
52934e89507SJohannes Berg 	rcu_read_lock();
53034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
531e9f207f0SJiri Benc 
53273651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
53373651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
53473651ee6SJohannes Berg 
53573651ee6SJohannes Berg 	return 0;
5365108ca82SJohannes Berg  out_remove:
5375108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5385108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5395108ca82SJohannes Berg 	local->num_sta--;
5405108ca82SJohannes Berg 	synchronize_net();
5415108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5424d33960bSJohannes Berg  out_err:
5434d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5444d33960bSJohannes Berg 	rcu_read_lock();
5454d33960bSJohannes Berg 	return err;
5468c71df7aSGuy Eilam }
5478c71df7aSGuy Eilam 
5488c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5498c71df7aSGuy Eilam {
5508c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
551308f7fcfSZhao, Gang 	int err;
5528c71df7aSGuy Eilam 
5534d33960bSJohannes Berg 	might_sleep();
5544d33960bSJohannes Berg 
5558c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5568c71df7aSGuy Eilam 	if (err) {
5578c71df7aSGuy Eilam 		rcu_read_lock();
5588c71df7aSGuy Eilam 		goto out_free;
5598c71df7aSGuy Eilam 	}
5608c71df7aSGuy Eilam 
561004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
562004c872eSJohannes Berg 
5634d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5648c71df7aSGuy Eilam 	if (err)
565004c872eSJohannes Berg 		goto out_free;
566d0709a65SJohannes Berg 
567004c872eSJohannes Berg 	return 0;
56893e5deb1SJohannes Berg  out_free:
569d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
57093e5deb1SJohannes Berg 	return err;
571f0706e82SJiri Benc }
572f0706e82SJiri Benc 
57334e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
57434e89507SJohannes Berg {
57534e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
57634e89507SJohannes Berg 
57734e89507SJohannes Berg 	rcu_read_unlock();
57834e89507SJohannes Berg 
57934e89507SJohannes Berg 	return err;
58034e89507SJohannes Berg }
58134e89507SJohannes Berg 
582d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
583004c872eSJohannes Berg {
584004c872eSJohannes Berg 	/*
585004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
586004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
587004c872eSJohannes Berg 	 */
588d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
589004c872eSJohannes Berg }
590004c872eSJohannes Berg 
591d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
592004c872eSJohannes Berg {
593004c872eSJohannes Berg 	/*
594004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
595004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
596004c872eSJohannes Berg 	 */
597d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
598004c872eSJohannes Berg }
599004c872eSJohannes Berg 
6003d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6013d5839b6SIlan Peer {
6023d5839b6SIlan Peer 	/*
6033d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6043d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6053d5839b6SIlan Peer 	 */
6063d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6073d5839b6SIlan Peer }
6083d5839b6SIlan Peer 
609948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
610004c872eSJohannes Berg {
611948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
612948d887dSJohannes Berg 	switch (ac) {
613948d887dSJohannes Berg 	case IEEE80211_AC_VO:
614948d887dSJohannes Berg 		return BIT(6) | BIT(7);
615948d887dSJohannes Berg 	case IEEE80211_AC_VI:
616948d887dSJohannes Berg 		return BIT(4) | BIT(5);
617948d887dSJohannes Berg 	case IEEE80211_AC_BE:
618948d887dSJohannes Berg 		return BIT(0) | BIT(3);
619948d887dSJohannes Berg 	case IEEE80211_AC_BK:
620948d887dSJohannes Berg 		return BIT(1) | BIT(2);
621948d887dSJohannes Berg 	default:
622948d887dSJohannes Berg 		WARN_ON(1);
623948d887dSJohannes Berg 		return 0;
624d0709a65SJohannes Berg 	}
625004c872eSJohannes Berg }
626004c872eSJohannes Berg 
6279b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
628004c872eSJohannes Berg {
629c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
630d012a605SMarco Porsch 	struct ps_data *ps;
631948d887dSJohannes Berg 	bool indicate_tim = false;
632948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
633948d887dSJohannes Berg 	int ac;
634d012a605SMarco Porsch 	u16 id;
635004c872eSJohannes Berg 
636d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
637d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
638c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
639c868cb35SJohannes Berg 			return;
6403e122be0SJohannes Berg 
641d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
642d012a605SMarco Porsch 		id = sta->sta.aid;
6433f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6443f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6453f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
646204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6476f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6483f52b7e3SMarco Porsch #endif
649d012a605SMarco Porsch 	} else {
650d012a605SMarco Porsch 		return;
651d012a605SMarco Porsch 	}
652d012a605SMarco Porsch 
653c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
654c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
655c868cb35SJohannes Berg 		return;
656004c872eSJohannes Berg 
657c868cb35SJohannes Berg 	if (sta->dead)
658c868cb35SJohannes Berg 		goto done;
6593e122be0SJohannes Berg 
660948d887dSJohannes Berg 	/*
661948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
662948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
663948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
664948d887dSJohannes Berg 	 * non-enabled ones.
665948d887dSJohannes Berg 	 */
666948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
667948d887dSJohannes Berg 		ignore_for_tim = 0;
668948d887dSJohannes Berg 
6699b7a86f3SJohannes Berg 	if (ignore_pending)
6709b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
6719b7a86f3SJohannes Berg 
672948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
673948d887dSJohannes Berg 		unsigned long tids;
674948d887dSJohannes Berg 
675948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
676948d887dSJohannes Berg 			continue;
677948d887dSJohannes Berg 
678948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
679948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
680948d887dSJohannes Berg 		if (indicate_tim)
681948d887dSJohannes Berg 			break;
682948d887dSJohannes Berg 
683948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
684948d887dSJohannes Berg 
685948d887dSJohannes Berg 		indicate_tim |=
686948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
687004c872eSJohannes Berg 	}
688004c872eSJohannes Berg 
689c868cb35SJohannes Berg  done:
69065f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
691004c872eSJohannes Berg 
6923d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6933d5839b6SIlan Peer 		goto out_unlock;
6943d5839b6SIlan Peer 
695948d887dSJohannes Berg 	if (indicate_tim)
696d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
697c868cb35SJohannes Berg 	else
698d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6993e122be0SJohannes Berg 
7009b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
701c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
702948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
703c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
704004c872eSJohannes Berg 	}
705004c872eSJohannes Berg 
7063d5839b6SIlan Peer out_unlock:
70765f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
708004c872eSJohannes Berg }
709004c872eSJohannes Berg 
7109b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
7119b7a86f3SJohannes Berg {
7129b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
7139b7a86f3SJohannes Berg }
7149b7a86f3SJohannes Berg 
715cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
716f0706e82SJiri Benc {
717e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
718f0706e82SJiri Benc 	int timeout;
719f0706e82SJiri Benc 
720f0706e82SJiri Benc 	if (!skb)
721cd0b8d89SJohannes Berg 		return false;
722f0706e82SJiri Benc 
723e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
724f0706e82SJiri Benc 
725f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
72657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
72757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
72857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
729f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
730f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
731e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
732f0706e82SJiri Benc }
733f0706e82SJiri Benc 
734f0706e82SJiri Benc 
735948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
736948d887dSJohannes Berg 						struct sta_info *sta, int ac)
737f0706e82SJiri Benc {
738f0706e82SJiri Benc 	unsigned long flags;
739f0706e82SJiri Benc 	struct sk_buff *skb;
740f0706e82SJiri Benc 
74160750397SJohannes Berg 	/*
74260750397SJohannes Berg 	 * First check for frames that should expire on the filtered
74360750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
74460750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
74560750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
74660750397SJohannes Berg 	 * total_ps_buffered counter.
74760750397SJohannes Berg 	 */
748f0706e82SJiri Benc 	for (;;) {
749948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
750948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
75157c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
752948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
753836341a7SJohannes Berg 		else
754f0706e82SJiri Benc 			skb = NULL;
755948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
756f0706e82SJiri Benc 
75760750397SJohannes Berg 		/*
75860750397SJohannes Berg 		 * Frames are queued in order, so if this one
75960750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
76060750397SJohannes Berg 		 * we actually reached the end of the queue we
76160750397SJohannes Berg 		 * also need to stop, of course.
76260750397SJohannes Berg 		 */
76360750397SJohannes Berg 		if (!skb)
76460750397SJohannes Berg 			break;
765d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
76660750397SJohannes Berg 	}
76760750397SJohannes Berg 
76860750397SJohannes Berg 	/*
76960750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
77060750397SJohannes Berg 	 * only find something if the filtered queue was emptied
77160750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
77260750397SJohannes Berg 	 * buffered frames.
77360750397SJohannes Berg 	 */
774f0706e82SJiri Benc 	for (;;) {
775948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
776948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
777f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
778948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
779f0706e82SJiri Benc 		else
780f0706e82SJiri Benc 			skb = NULL;
781948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
782f0706e82SJiri Benc 
78360750397SJohannes Berg 		/*
78460750397SJohannes Berg 		 * frames are queued in order, so if this one
78560750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
78660750397SJohannes Berg 		 * the queue) we can stop testing
78760750397SJohannes Berg 		 */
788836341a7SJohannes Berg 		if (!skb)
789836341a7SJohannes Berg 			break;
790836341a7SJohannes Berg 
791f0706e82SJiri Benc 		local->total_ps_buffered--;
792bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
793bdcbd8e0SJohannes Berg 		       sta->sta.addr);
794d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
795f0706e82SJiri Benc 	}
7963393a608SJuuso Oikarinen 
79760750397SJohannes Berg 	/*
79860750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
79960750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
80060750397SJohannes Berg 	 * frames.
80160750397SJohannes Berg 	 */
80260750397SJohannes Berg 	sta_info_recalc_tim(sta);
80360750397SJohannes Berg 
80460750397SJohannes Berg 	/*
80560750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
80660750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
80760750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
80860750397SJohannes Berg 	 */
809948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
810948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
811948d887dSJohannes Berg }
812948d887dSJohannes Berg 
813948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
814948d887dSJohannes Berg 					     struct sta_info *sta)
815948d887dSJohannes Berg {
816948d887dSJohannes Berg 	bool have_buffered = false;
817948d887dSJohannes Berg 	int ac;
818948d887dSJohannes Berg 
8193f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8203f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8213f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
822948d887dSJohannes Berg 		return false;
823948d887dSJohannes Berg 
824948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
825948d887dSJohannes Berg 		have_buffered |=
826948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
827948d887dSJohannes Berg 
828948d887dSJohannes Berg 	return have_buffered;
829f0706e82SJiri Benc }
830f0706e82SJiri Benc 
831d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
83234e89507SJohannes Berg {
83334e89507SJohannes Berg 	struct ieee80211_local *local;
83434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8356d10e46bSJohannes Berg 	int ret;
83634e89507SJohannes Berg 
83734e89507SJohannes Berg 	might_sleep();
83834e89507SJohannes Berg 
83934e89507SJohannes Berg 	if (!sta)
84034e89507SJohannes Berg 		return -ENOENT;
84134e89507SJohannes Berg 
84234e89507SJohannes Berg 	local = sta->local;
84334e89507SJohannes Berg 	sdata = sta->sdata;
84434e89507SJohannes Berg 
84583d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
84683d5cc01SJohannes Berg 
847098a6070SJohannes Berg 	/*
848098a6070SJohannes Berg 	 * Before removing the station from the driver and
849098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
850098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
851098a6070SJohannes Berg 	 * will be sufficient.
852098a6070SJohannes Berg 	 */
853c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
854c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
855098a6070SJohannes Berg 
85634e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
857b01711beSJohannes Berg 	if (WARN_ON(ret))
85834e89507SJohannes Berg 		return ret;
85934e89507SJohannes Berg 
860a7a6bdd0SArik Nemtsov 	/*
861a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
862a7a6bdd0SArik Nemtsov 	 * removal.
863a7a6bdd0SArik Nemtsov 	 */
864a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
865a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
866a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
867a7a6bdd0SArik Nemtsov 	}
868a7a6bdd0SArik Nemtsov 
869794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8704d33960bSJohannes Berg 
8716a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8726a9d1b91SJohannes Berg 
873a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
874a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
875a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
876a710c816SJohannes Berg 
877d778207bSJohannes Berg 	return 0;
878d778207bSJohannes Berg }
879d778207bSJohannes Berg 
880d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
881d778207bSJohannes Berg {
882d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
883d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8846f7a8d26SJohannes Berg 	struct station_info sinfo = {};
885d778207bSJohannes Berg 	int ret;
886d778207bSJohannes Berg 
887d778207bSJohannes Berg 	/*
888d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
889d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
890d778207bSJohannes Berg 	 */
891d778207bSJohannes Berg 
892d778207bSJohannes Berg 	might_sleep();
893d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
894d778207bSJohannes Berg 
895c8782078SJohannes Berg 	/* now keys can no longer be reached */
8966d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
89734e89507SJohannes Berg 
8989b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
8999b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
9009b7a86f3SJohannes Berg 
90134e89507SJohannes Berg 	sta->dead = true;
90234e89507SJohannes Berg 
90334e89507SJohannes Berg 	local->num_sta--;
90434e89507SJohannes Berg 	local->sta_generation++;
90534e89507SJohannes Berg 
90683d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
907f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
908f09603a2SJohannes Berg 		if (ret) {
90983d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
91083d5cc01SJohannes Berg 			break;
91183d5cc01SJohannes Berg 		}
91283d5cc01SJohannes Berg 	}
913d9a7ddb0SJohannes Berg 
914f09603a2SJohannes Berg 	if (sta->uploaded) {
915f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
916f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
917f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
918f09603a2SJohannes Berg 	}
91934e89507SJohannes Berg 
920bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
921bdcbd8e0SJohannes Berg 
9226f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
9236f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
924ec15e68bSJouni Malinen 
92534e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
92634e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
92721f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
92834e89507SJohannes Berg 
929d34ba216SJohannes Berg 	cleanup_single_sta(sta);
930d778207bSJohannes Berg }
931d778207bSJohannes Berg 
932d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
933d778207bSJohannes Berg {
934d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
935d778207bSJohannes Berg 
936d778207bSJohannes Berg 	if (err)
937d778207bSJohannes Berg 		return err;
938d778207bSJohannes Berg 
939d778207bSJohannes Berg 	synchronize_net();
940d778207bSJohannes Berg 
941d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
94234e89507SJohannes Berg 
94334e89507SJohannes Berg 	return 0;
94434e89507SJohannes Berg }
94534e89507SJohannes Berg 
94634e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
94734e89507SJohannes Berg {
94834e89507SJohannes Berg 	struct sta_info *sta;
94934e89507SJohannes Berg 	int ret;
95034e89507SJohannes Berg 
95134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9527852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
95334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
95434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
95534e89507SJohannes Berg 
95634e89507SJohannes Berg 	return ret;
95734e89507SJohannes Berg }
95834e89507SJohannes Berg 
95934e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
96034e89507SJohannes Berg 			      const u8 *addr)
96134e89507SJohannes Berg {
96234e89507SJohannes Berg 	struct sta_info *sta;
96334e89507SJohannes Berg 	int ret;
96434e89507SJohannes Berg 
96534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9667852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
96734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
96834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
96934e89507SJohannes Berg 
97034e89507SJohannes Berg 	return ret;
97134e89507SJohannes Berg }
972f0706e82SJiri Benc 
973f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
974f0706e82SJiri Benc {
975f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
976f0706e82SJiri Benc 	struct sta_info *sta;
9773393a608SJuuso Oikarinen 	bool timer_needed = false;
978f0706e82SJiri Benc 
979d0709a65SJohannes Berg 	rcu_read_lock();
980d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9813393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9823393a608SJuuso Oikarinen 			timer_needed = true;
983d0709a65SJohannes Berg 	rcu_read_unlock();
984f0706e82SJiri Benc 
9855bb644a0SJohannes Berg 	if (local->quiescing)
9865bb644a0SJohannes Berg 		return;
9875bb644a0SJohannes Berg 
9883393a608SJuuso Oikarinen 	if (!timer_needed)
9893393a608SJuuso Oikarinen 		return;
9903393a608SJuuso Oikarinen 
99126d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
99226d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
993f0706e82SJiri Benc }
994f0706e82SJiri Benc 
995f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
996f0706e82SJiri Benc {
9974d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
99834e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
999f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1000f0706e82SJiri Benc 
1001b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1002b24b8a24SPavel Emelyanov 		    (unsigned long)local);
1003f0706e82SJiri Benc }
1004f0706e82SJiri Benc 
1005f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1006f0706e82SJiri Benc {
1007a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
1008f0706e82SJiri Benc }
1009f0706e82SJiri Benc 
1010051007d9SJohannes Berg 
1011e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1012f0706e82SJiri Benc {
1013b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1014f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1015d778207bSJohannes Berg 	LIST_HEAD(free_list);
101644213b5eSJohannes Berg 	int ret = 0;
1017f0706e82SJiri Benc 
1018d0709a65SJohannes Berg 	might_sleep();
1019d0709a65SJohannes Berg 
1020e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1021e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1022e716251dSJohannes Berg 
102334e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
102434e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1025e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1026e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1027d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1028d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
102934316837SJohannes Berg 			ret++;
103034316837SJohannes Berg 		}
103134e89507SJohannes Berg 	}
1032d778207bSJohannes Berg 
1033d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1034d778207bSJohannes Berg 		synchronize_net();
1035d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1036d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1037d778207bSJohannes Berg 	}
103834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
103944213b5eSJohannes Berg 
1040051007d9SJohannes Berg 	return ret;
1041051007d9SJohannes Berg }
1042051007d9SJohannes Berg 
104324723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
104424723d1bSJohannes Berg 			  unsigned long exp_time)
104524723d1bSJohannes Berg {
104624723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
104724723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
104824723d1bSJohannes Berg 
104934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1050e46a2cf9SMohammed Shafi Shajakhan 
1051e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1052ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1053ec2b774eSMarek Lindner 			continue;
1054ec2b774eSMarek Lindner 
105524723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1056eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1057bdcbd8e0SJohannes Berg 				sta->sta.addr);
10583f52b7e3SMarco Porsch 
10593f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10603f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10613f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10623f52b7e3SMarco Porsch 
106334e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
106424723d1bSJohannes Berg 		}
1065e46a2cf9SMohammed Shafi Shajakhan 	}
1066e46a2cf9SMohammed Shafi Shajakhan 
106734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
106824723d1bSJohannes Berg }
106917741cdcSJohannes Berg 
1070686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1071686b9cb9SBen Greear 					       const u8 *addr,
1072686b9cb9SBen Greear 					       const u8 *localaddr)
107317741cdcSJohannes Berg {
1074abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
107517741cdcSJohannes Berg 
1076686b9cb9SBen Greear 	/*
1077686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1078686b9cb9SBen Greear 	 * ... first in list.
1079686b9cb9SBen Greear 	 */
1080f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1081686b9cb9SBen Greear 		if (localaddr &&
1082b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1083686b9cb9SBen Greear 			continue;
1084f7c65594SJohannes Berg 		if (!sta->uploaded)
1085f7c65594SJohannes Berg 			return NULL;
108617741cdcSJohannes Berg 		return &sta->sta;
1087f7c65594SJohannes Berg 	}
1088f7c65594SJohannes Berg 
1089abe60632SJohannes Berg 	return NULL;
109017741cdcSJohannes Berg }
1091686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10925ed176e1SJohannes Berg 
10935ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10945ed176e1SJohannes Berg 					 const u8 *addr)
10955ed176e1SJohannes Berg {
1096f7c65594SJohannes Berg 	struct sta_info *sta;
10975ed176e1SJohannes Berg 
10985ed176e1SJohannes Berg 	if (!vif)
10995ed176e1SJohannes Berg 		return NULL;
11005ed176e1SJohannes Berg 
1101f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1102f7c65594SJohannes Berg 	if (!sta)
1103f7c65594SJohannes Berg 		return NULL;
11045ed176e1SJohannes Berg 
1105f7c65594SJohannes Berg 	if (!sta->uploaded)
1106f7c65594SJohannes Berg 		return NULL;
1107f7c65594SJohannes Berg 
1108f7c65594SJohannes Berg 	return &sta->sta;
11095ed176e1SJohannes Berg }
111017741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1111af818581SJohannes Berg 
1112e3685e03SJohannes Berg /* powersave support code */
1113e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
111450a9432dSJohannes Berg {
1115608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1116e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1117e3685e03SJohannes Berg 	struct sk_buff_head pending;
1118e3685e03SJohannes Berg 	int filtered = 0, buffered = 0, ac;
1119e3685e03SJohannes Berg 	unsigned long flags;
1120d012a605SMarco Porsch 	struct ps_data *ps;
1121d012a605SMarco Porsch 
11223918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11233918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11243918edb0SFelix Fietkau 				     u.ap);
11253918edb0SFelix Fietkau 
11263918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1127d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11283f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11293f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1130d012a605SMarco Porsch 	else
1131d012a605SMarco Porsch 		return;
113250a9432dSJohannes Berg 
1133c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
113447086fc5SJohannes Berg 
11355a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1136948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1137948d887dSJohannes Berg 
1138d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
113912375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1140af818581SJohannes Berg 
1141948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1142af818581SJohannes Berg 
11431d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11441d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1145af818581SJohannes Berg 	/* Send all buffered frames to the station */
1146948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1147948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1148948d887dSJohannes Berg 
1149987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1150948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1151987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1152948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1153948d887dSJohannes Berg 		filtered += tmp - count;
1154948d887dSJohannes Berg 		count = tmp;
1155948d887dSJohannes Berg 
1156987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1157948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1158987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1159948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1160948d887dSJohannes Berg 		buffered += tmp - count;
1161948d887dSJohannes Berg 	}
1162948d887dSJohannes Berg 
1163e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11645ac2e350SJohannes Berg 
11655ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11665ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11675ac2e350SJohannes Berg 
11685ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
11695ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
11705ac2e350SJohannes Berg 	 */
11715ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
11725ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
11731d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1174948d887dSJohannes Berg 
1175e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1176e3685e03SJohannes Berg 
1177687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1178062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1179062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1180687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1181687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1182687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1183687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1184687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1185687da132SEmmanuel Grumbach 		       sta->sta.addr);
1186687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1187687da132SEmmanuel Grumbach 					   sta->sta.addr,
1188687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1189687da132SEmmanuel Grumbach 	}
1190687da132SEmmanuel Grumbach 
1191af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1192af818581SJohannes Berg 
1193c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1194c868cb35SJohannes Berg 
1195bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1196bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1197bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1198af818581SJohannes Berg }
1199af818581SJohannes Berg 
1200ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1201ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1202b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1203b77cf4f8SJohannes Berg 					 bool call_driver)
1204ce662b44SJohannes Berg {
1205ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1206ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1207ce662b44SJohannes Berg 	struct sk_buff *skb;
1208ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1209ce662b44SJohannes Berg 	__le16 fc;
1210a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1211ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
121255de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1213ce662b44SJohannes Berg 
1214ce662b44SJohannes Berg 	if (qos) {
1215ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1216ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1217ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1218ce662b44SJohannes Berg 	} else {
1219ce662b44SJohannes Berg 		size -= 2;
1220ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1221ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1222ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1223ce662b44SJohannes Berg 	}
1224ce662b44SJohannes Berg 
1225ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1226ce662b44SJohannes Berg 	if (!skb)
1227ce662b44SJohannes Berg 		return;
1228ce662b44SJohannes Berg 
1229ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1230ce662b44SJohannes Berg 
1231ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1232ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1233ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1234ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1235ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1236ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1237864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1238ce662b44SJohannes Berg 
1239ce662b44SJohannes Berg 	skb->priority = tid;
1240ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
124159b66255SJohannes Berg 	if (qos) {
1242ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1243ce662b44SJohannes Berg 
124440b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1245ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1246ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1247ce662b44SJohannes Berg 	}
1248ce662b44SJohannes Berg 
1249ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1250ce662b44SJohannes Berg 
1251ce662b44SJohannes Berg 	/*
1252ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1253ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1254deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1255deeaee19SJohannes Berg 	 * ends the poll/service period.
1256ce662b44SJohannes Berg 	 */
125702f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1258deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1259ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1260ce662b44SJohannes Berg 
12616b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
12626b127c71SSujith Manoharan 
1263b77cf4f8SJohannes Berg 	if (call_driver)
1264b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1265b77cf4f8SJohannes Berg 					  reason, false);
126640b96408SJohannes Berg 
126789afe614SJohannes Berg 	skb->dev = sdata->dev;
126889afe614SJohannes Berg 
126955de908aSJohannes Berg 	rcu_read_lock();
127055de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
127155de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
127255de908aSJohannes Berg 		rcu_read_unlock();
127355de908aSJohannes Berg 		kfree_skb(skb);
127455de908aSJohannes Berg 		return;
127555de908aSJohannes Berg 	}
127655de908aSJohannes Berg 
127773c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
127873c4e195SJohannes Berg 	ieee80211_xmit(sdata, skb);
127955de908aSJohannes Berg 	rcu_read_unlock();
1280ce662b44SJohannes Berg }
1281ce662b44SJohannes Berg 
12820a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
12830a1cb809SJohannes Berg {
12840a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
12850a1cb809SJohannes Berg 	if (tids & 0xF8)
12860a1cb809SJohannes Berg 		return fls(tids) - 1;
12870a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
12880a1cb809SJohannes Berg 	if (tids & BIT(0))
12890a1cb809SJohannes Berg 		return 0;
12900a1cb809SJohannes Berg 	return fls(tids) - 1;
12910a1cb809SJohannes Berg }
12920a1cb809SJohannes Berg 
129347086fc5SJohannes Berg static void
129447086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
129547086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
129647086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1297af818581SJohannes Berg {
1298af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1299af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1300948d887dSJohannes Berg 	bool more_data = false;
1301948d887dSJohannes Berg 	int ac;
13024049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
130347086fc5SJohannes Berg 	struct sk_buff_head frames;
130447086fc5SJohannes Berg 
1305deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1306c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1307deeaee19SJohannes Berg 
130847086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1309af818581SJohannes Berg 
1310f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1311948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13124049e09aSJohannes Berg 		unsigned long tids;
13134049e09aSJohannes Berg 
131447086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1315948d887dSJohannes Berg 			continue;
1316948d887dSJohannes Berg 
13174049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13184049e09aSJohannes Berg 
1319f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1320f9f760b4SJohannes Berg 		 * release from hardware queues
1321f9f760b4SJohannes Berg 		 */
1322f9f760b4SJohannes Berg 		if (skb_queue_empty(&frames))
1323f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1324f9f760b4SJohannes Berg 
13254049e09aSJohannes Berg 		if (driver_release_tids) {
1326f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1327f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1328f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1329f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1330f9f760b4SJohannes Berg 			 */
1331f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1332f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1333f9f760b4SJohannes Berg 				more_data = true;
1334f9f760b4SJohannes Berg 				driver_release_tids =
1335f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1336f9f760b4SJohannes Berg 						driver_release_tids));
1337f9f760b4SJohannes Berg 				break;
1338f9f760b4SJohannes Berg 			}
13394049e09aSJohannes Berg 		} else {
134047086fc5SJohannes Berg 			struct sk_buff *skb;
134147086fc5SJohannes Berg 
134247086fc5SJohannes Berg 			while (n_frames > 0) {
1343948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1344948d887dSJohannes Berg 				if (!skb) {
134547086fc5SJohannes Berg 					skb = skb_dequeue(
134647086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1347af818581SJohannes Berg 					if (skb)
1348af818581SJohannes Berg 						local->total_ps_buffered--;
1349af818581SJohannes Berg 				}
135047086fc5SJohannes Berg 				if (!skb)
135147086fc5SJohannes Berg 					break;
135247086fc5SJohannes Berg 				n_frames--;
135347086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
135447086fc5SJohannes Berg 			}
1355948d887dSJohannes Berg 		}
1356948d887dSJohannes Berg 
1357f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1358f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1359f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13604049e09aSJohannes Berg 		 */
1361948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1362948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1363948d887dSJohannes Berg 			more_data = true;
1364948d887dSJohannes Berg 			break;
1365948d887dSJohannes Berg 		}
1366948d887dSJohannes Berg 	}
1367af818581SJohannes Berg 
1368f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1369ce662b44SJohannes Berg 		int tid;
13704049e09aSJohannes Berg 
1371ce662b44SJohannes Berg 		/*
1372ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1373ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1374ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1375ce662b44SJohannes Berg 		 *
1376ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1377ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1378ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1379ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1380ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1381ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1382ce662b44SJohannes Berg 		 *
1383ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1384ce662b44SJohannes Berg 		 */
1385ce662b44SJohannes Berg 
1386ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1387ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1388ce662b44SJohannes Berg 
1389b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1390f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
139147086fc5SJohannes Berg 		struct sk_buff_head pending;
139247086fc5SJohannes Berg 		struct sk_buff *skb;
139340b96408SJohannes Berg 		int num = 0;
139440b96408SJohannes Berg 		u16 tids = 0;
1395b77cf4f8SJohannes Berg 		bool need_null = false;
139647086fc5SJohannes Berg 
139747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
139847086fc5SJohannes Berg 
139947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1400af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
140147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
140240b96408SJohannes Berg 			u8 *qoshdr = NULL;
140340b96408SJohannes Berg 
140440b96408SJohannes Berg 			num++;
1405af818581SJohannes Berg 
1406af818581SJohannes Berg 			/*
140747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
140847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
140947086fc5SJohannes Berg 			 * exchange.
1410af818581SJohannes Berg 			 */
14116b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
14126b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1413af818581SJohannes Berg 
141447086fc5SJohannes Berg 			/*
141547086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
141647086fc5SJohannes Berg 			 * more buffered frames for this STA
141747086fc5SJohannes Berg 			 */
141824b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
141947086fc5SJohannes Berg 				hdr->frame_control |=
142047086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
142124b9c373SJanusz.Dziedzic@tieto.com 			else
142224b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
142324b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1424af818581SJohannes Berg 
142540b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
142640b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
142740b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
142840b96408SJohannes Berg 
1429b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1430b77cf4f8SJohannes Berg 
1431b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1432b77cf4f8SJohannes Berg 
1433b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1434b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1435b77cf4f8SJohannes Berg 				continue;
1436b77cf4f8SJohannes Berg 
1437b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1438b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1439b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1440b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1441b77cf4f8SJohannes Berg 				break;
1442b77cf4f8SJohannes Berg 			}
1443b77cf4f8SJohannes Berg 
1444b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1445b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1446b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1447b77cf4f8SJohannes Berg 			 * and be done.
1448b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1449b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1450b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1451b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1452b77cf4f8SJohannes Berg 			 *
1453b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1454b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1455b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1456b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1457b77cf4f8SJohannes Berg 			 *
1458b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1459b77cf4f8SJohannes Berg 			 */
1460b77cf4f8SJohannes Berg 			if (qoshdr) {
146140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1462deeaee19SJohannes Berg 
146347086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
146447086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1465b77cf4f8SJohannes Berg 			} else {
1466b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1467b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1468b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1469b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1470b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1471b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1472b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1473b77cf4f8SJohannes Berg 				 */
1474b77cf4f8SJohannes Berg 				hdr->frame_control |=
1475b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1476b77cf4f8SJohannes Berg 				need_null = true;
1477b77cf4f8SJohannes Berg 				num++;
147852a3f20cSMarco Porsch 			}
1479b77cf4f8SJohannes Berg 			break;
148047086fc5SJohannes Berg 		}
148147086fc5SJohannes Berg 
148240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
148340b96408SJohannes Berg 					  reason, more_data);
148440b96408SJohannes Berg 
148547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1486af818581SJohannes Berg 
1487b77cf4f8SJohannes Berg 		if (need_null)
1488b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1489b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1490b77cf4f8SJohannes Berg 				reason, false);
1491b77cf4f8SJohannes Berg 
1492c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1493af818581SJohannes Berg 	} else {
1494af818581SJohannes Berg 		/*
14954049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
14964049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1497f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1498f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1499f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1500f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1501f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1502f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1503af818581SJohannes Berg 		 */
15044049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
150547086fc5SJohannes Berg 					    n_frames, reason, more_data);
15064049e09aSJohannes Berg 
15074049e09aSJohannes Berg 		/*
15084049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
15094049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1510f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
15114049e09aSJohannes Berg 		 * release function.
1512f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
15134049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
15144049e09aSJohannes Berg 		 */
1515af818581SJohannes Berg 	}
1516af818581SJohannes Berg }
1517af818581SJohannes Berg 
1518af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1519af818581SJohannes Berg {
152047086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1521af818581SJohannes Berg 
1522af818581SJohannes Berg 	/*
152347086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
152447086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
152547086fc5SJohannes Berg 	 * only from the non-enabled ones.
1526af818581SJohannes Berg 	 */
152747086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
152847086fc5SJohannes Berg 		ignore_for_response = 0;
1529af818581SJohannes Berg 
153047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
153147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1532af818581SJohannes Berg }
153347086fc5SJohannes Berg 
153447086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
153547086fc5SJohannes Berg {
153647086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
153747086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
153847086fc5SJohannes Berg 
153947086fc5SJohannes Berg 	/*
154047086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
154147086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
154247086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
154347086fc5SJohannes Berg 	 * actually getting called.
154447086fc5SJohannes Berg 	 */
154547086fc5SJohannes Berg 	if (!delivery_enabled)
154647086fc5SJohannes Berg 		return;
154747086fc5SJohannes Berg 
154847086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
154947086fc5SJohannes Berg 	case 1:
155047086fc5SJohannes Berg 		n_frames = 2;
155147086fc5SJohannes Berg 		break;
155247086fc5SJohannes Berg 	case 2:
155347086fc5SJohannes Berg 		n_frames = 4;
155447086fc5SJohannes Berg 		break;
155547086fc5SJohannes Berg 	case 3:
155647086fc5SJohannes Berg 		n_frames = 6;
155747086fc5SJohannes Berg 		break;
155847086fc5SJohannes Berg 	case 0:
155947086fc5SJohannes Berg 		/* XXX: what is a good value? */
156013a8098aSAndrei Otcheretianski 		n_frames = 128;
156147086fc5SJohannes Berg 		break;
156247086fc5SJohannes Berg 	}
156347086fc5SJohannes Berg 
156447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
156547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1566af818581SJohannes Berg }
1567af818581SJohannes Berg 
1568af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1569af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1570af818581SJohannes Berg {
1571af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1572af818581SJohannes Berg 
1573b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1574b5878a2dSJohannes Berg 
15755ac2e350SJohannes Berg 	if (block) {
1576c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
15775ac2e350SJohannes Berg 		return;
15785ac2e350SJohannes Berg 	}
15795ac2e350SJohannes Berg 
15805ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
15815ac2e350SJohannes Berg 		return;
15825ac2e350SJohannes Berg 
15835ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
15845ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
15855ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15865ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15875ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
15885ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
15895ac2e350SJohannes Berg 		/* must be asleep in this case */
15905ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15915ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15925ac2e350SJohannes Berg 	} else {
15935ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15945ac2e350SJohannes Berg 	}
1595af818581SJohannes Berg }
1596af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1597dcf55fb5SFelix Fietkau 
1598e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
159937fbd908SJohannes Berg {
160037fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
160137fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
160237fbd908SJohannes Berg 
160337fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
160437fbd908SJohannes Berg 
160537fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
160637fbd908SJohannes Berg }
1607e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
160837fbd908SJohannes Berg 
1609042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1610042ec453SJohannes Berg 				u8 tid, bool buffered)
1611dcf55fb5SFelix Fietkau {
1612dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1613dcf55fb5SFelix Fietkau 
16145a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1615042ec453SJohannes Berg 		return;
1616042ec453SJohannes Berg 
16171b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
16181b000789SJohannes Berg 
1619948d887dSJohannes Berg 	if (buffered)
1620948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1621948d887dSJohannes Berg 	else
1622948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1623948d887dSJohannes Berg 
1624c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1625dcf55fb5SFelix Fietkau }
1626042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1627d9a7ddb0SJohannes Berg 
162883d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1629d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1630d9a7ddb0SJohannes Berg {
16318bf11d8dSJohannes Berg 	might_sleep();
1632d9a7ddb0SJohannes Berg 
1633d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1634d9a7ddb0SJohannes Berg 		return 0;
1635d9a7ddb0SJohannes Berg 
1636f09603a2SJohannes Berg 	/* check allowed transitions first */
1637f09603a2SJohannes Berg 
1638d9a7ddb0SJohannes Berg 	switch (new_state) {
1639d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1640f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1641d9a7ddb0SJohannes Berg 			return -EINVAL;
1642d9a7ddb0SJohannes Berg 		break;
1643d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1644f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1645f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1646d9a7ddb0SJohannes Berg 			return -EINVAL;
1647d9a7ddb0SJohannes Berg 		break;
1648d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1649f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1650f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1651d9a7ddb0SJohannes Berg 			return -EINVAL;
1652d9a7ddb0SJohannes Berg 		break;
1653d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1654f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1655d9a7ddb0SJohannes Berg 			return -EINVAL;
1656d9a7ddb0SJohannes Berg 		break;
1657d9a7ddb0SJohannes Berg 	default:
1658d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1659d9a7ddb0SJohannes Berg 		return -EINVAL;
1660d9a7ddb0SJohannes Berg 	}
1661d9a7ddb0SJohannes Berg 
1662bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1663bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1664f09603a2SJohannes Berg 
1665f09603a2SJohannes Berg 	/*
1666f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1667f09603a2SJohannes Berg 	 * fail the transition
1668f09603a2SJohannes Berg 	 */
1669f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1670f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1671f09603a2SJohannes Berg 					sta->sta_state, new_state);
1672f09603a2SJohannes Berg 		if (err)
1673f09603a2SJohannes Berg 			return err;
1674f09603a2SJohannes Berg 	}
1675f09603a2SJohannes Berg 
1676f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1677f09603a2SJohannes Berg 
1678f09603a2SJohannes Berg 	switch (new_state) {
1679f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1680f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1681f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1682f09603a2SJohannes Berg 		break;
1683f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1684f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1685f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1686f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1687f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1688f09603a2SJohannes Berg 		break;
1689f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1690f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1691f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1692f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
16937e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16947e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16957e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16967e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1697f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1698f09603a2SJohannes Berg 		}
1699f09603a2SJohannes Berg 		break;
1700f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1701f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
17027e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
17037e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
17047e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
17057e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1706f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1707f09603a2SJohannes Berg 		}
1708f09603a2SJohannes Berg 		break;
1709f09603a2SJohannes Berg 	default:
1710f09603a2SJohannes Berg 		break;
1711f09603a2SJohannes Berg 	}
1712f09603a2SJohannes Berg 
1713d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1714d9a7ddb0SJohannes Berg 
1715d9a7ddb0SJohannes Berg 	return 0;
1716d9a7ddb0SJohannes Berg }
1717687da132SEmmanuel Grumbach 
1718687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1719687da132SEmmanuel Grumbach {
1720687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1721687da132SEmmanuel Grumbach 	u8 rx_streams;
1722687da132SEmmanuel Grumbach 
1723687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1724687da132SEmmanuel Grumbach 		return 1;
1725687da132SEmmanuel Grumbach 
1726687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1727687da132SEmmanuel Grumbach 		int i;
1728687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1729687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1730687da132SEmmanuel Grumbach 
1731687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1732687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1733687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1734687da132SEmmanuel Grumbach 				return i + 1;
1735687da132SEmmanuel Grumbach 	}
1736687da132SEmmanuel Grumbach 
1737687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1738687da132SEmmanuel Grumbach 		rx_streams = 4;
1739687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1740687da132SEmmanuel Grumbach 		rx_streams = 3;
1741687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1742687da132SEmmanuel Grumbach 		rx_streams = 2;
1743687da132SEmmanuel Grumbach 	else
1744687da132SEmmanuel Grumbach 		rx_streams = 1;
1745687da132SEmmanuel Grumbach 
1746687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1747687da132SEmmanuel Grumbach 		return rx_streams;
1748687da132SEmmanuel Grumbach 
1749687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1750687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1751687da132SEmmanuel Grumbach }
1752b7ffbd7eSJohannes Berg 
1753b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1754b7ffbd7eSJohannes Berg {
1755b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1756b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
17579a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1758b7ffbd7eSJohannes Berg 	struct timespec uptime;
1759b7ffbd7eSJohannes Berg 	u32 thr = 0;
1760b7ffbd7eSJohannes Berg 	int i, ac;
1761b7ffbd7eSJohannes Berg 
17629a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
17639a244409SJohn W. Linville 		ref = local->rate_ctrl;
17649a244409SJohn W. Linville 
1765b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1766b7ffbd7eSJohannes Berg 
1767*225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
1768*225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
1769*225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
1770*225b8189SJohannes Berg 	 */
1771*225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1772*225b8189SJohannes Berg 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1773*225b8189SJohannes Berg 
17742b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
17752b9a7e1bSJohannes Berg 
1776319090bfSJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1777319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1778319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1779319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1780319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_RX_DROP_MISC) |
1781319090bfSJohannes Berg 			 BIT(NL80211_STA_INFO_BEACON_LOSS);
1782b7ffbd7eSJohannes Berg 
178318171520SThomas Gleixner 	ktime_get_ts(&uptime);
1784b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1785b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
17862b9a7e1bSJohannes Berg 
1787319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1788319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1789b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
17902b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1791b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1792319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1793b7ffbd7eSJohannes Berg 	}
17942b9a7e1bSJohannes Berg 
1795319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
17962b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
17972b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
17982b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1799319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
18002b9a7e1bSJohannes Berg 	}
18012b9a7e1bSJohannes Berg 
1802319090bfSJohannes Berg 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1803319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1804b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1805319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
18062b9a7e1bSJohannes Berg 	}
18072b9a7e1bSJohannes Berg 
1808319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1809b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1810319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
18112b9a7e1bSJohannes Berg 	}
18122b9a7e1bSJohannes Berg 
1813319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1814b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1815319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
18162b9a7e1bSJohannes Berg 	}
18172b9a7e1bSJohannes Berg 
1818319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1819b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1820319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
18212b9a7e1bSJohannes Berg 	}
18222b9a7e1bSJohannes Berg 
1823b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1824b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1825b7ffbd7eSJohannes Berg 
1826*225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1827*225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1828*225b8189SJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1829*225b8189SJohannes Berg 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1830*225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
1831*225b8189SJohannes Berg 	}
1832*225b8189SJohannes Berg 
1833b7ffbd7eSJohannes Berg 	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1834b7ffbd7eSJohannes Berg 	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1835319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
1836b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1837319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1838b7ffbd7eSJohannes Berg 		}
18392b9a7e1bSJohannes Berg 
1840319090bfSJohannes Berg 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
18412b9a7e1bSJohannes Berg 			sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1842319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
18432b9a7e1bSJohannes Berg 		}
18442b9a7e1bSJohannes Berg 	}
18452b9a7e1bSJohannes Berg 
18462b9a7e1bSJohannes Berg 	if (sta->chains &&
1847319090bfSJohannes Berg 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1848319090bfSJohannes Berg 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
1849319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
1850319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
1851b7ffbd7eSJohannes Berg 
1852b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1853b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1854b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1855b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1856b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1857b7ffbd7eSJohannes Berg 		}
1858b7ffbd7eSJohannes Berg 	}
1859b7ffbd7eSJohannes Berg 
1860319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
1861b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1862319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
18632b9a7e1bSJohannes Berg 	}
18642b9a7e1bSJohannes Berg 
1865319090bfSJohannes Berg 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
1866b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1867319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
18682b9a7e1bSJohannes Berg 	}
1869b7ffbd7eSJohannes Berg 
187079c892b8SJohannes Berg 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
187179c892b8SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
187279c892b8SJohannes Berg 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
187379c892b8SJohannes Berg 
187479c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
187579c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
187679c892b8SJohannes Berg 			tidstats->rx_msdu = sta->rx_msdu[i];
187779c892b8SJohannes Berg 		}
187879c892b8SJohannes Berg 
187979c892b8SJohannes Berg 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
188079c892b8SJohannes Berg 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
188179c892b8SJohannes Berg 			tidstats->tx_msdu = sta->tx_msdu[i];
188279c892b8SJohannes Berg 		}
188379c892b8SJohannes Berg 
188479c892b8SJohannes Berg 		if (!(tidstats->filled &
188579c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
188679c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
188779c892b8SJohannes Berg 			tidstats->filled |=
188879c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
188979c892b8SJohannes Berg 			tidstats->tx_msdu_retries = sta->tx_msdu_retries[i];
189079c892b8SJohannes Berg 		}
189179c892b8SJohannes Berg 
189279c892b8SJohannes Berg 		if (!(tidstats->filled &
189379c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
189479c892b8SJohannes Berg 		    local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
189579c892b8SJohannes Berg 			tidstats->filled |=
189679c892b8SJohannes Berg 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
189779c892b8SJohannes Berg 			tidstats->tx_msdu_failed = sta->tx_msdu_failed[i];
189879c892b8SJohannes Berg 		}
189979c892b8SJohannes Berg 	}
190079c892b8SJohannes Berg 
1901b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1902b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1903319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
1904319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLID) |
1905319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
1906319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
1907319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_PEER_PM) |
1908319090bfSJohannes Berg 				 BIT(NL80211_STA_INFO_NONPEER_PM);
1909b7ffbd7eSJohannes Berg 
1910b7ffbd7eSJohannes Berg 		sinfo->llid = sta->llid;
1911b7ffbd7eSJohannes Berg 		sinfo->plid = sta->plid;
1912b7ffbd7eSJohannes Berg 		sinfo->plink_state = sta->plink_state;
1913b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1914319090bfSJohannes Berg 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
1915b7ffbd7eSJohannes Berg 			sinfo->t_offset = sta->t_offset;
1916b7ffbd7eSJohannes Berg 		}
1917b7ffbd7eSJohannes Berg 		sinfo->local_pm = sta->local_pm;
1918b7ffbd7eSJohannes Berg 		sinfo->peer_pm = sta->peer_pm;
1919b7ffbd7eSJohannes Berg 		sinfo->nonpeer_pm = sta->nonpeer_pm;
1920b7ffbd7eSJohannes Berg #endif
1921b7ffbd7eSJohannes Berg 	}
1922b7ffbd7eSJohannes Berg 
1923b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1924b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1925b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1926b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1927b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1928b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1929b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1930785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1931b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1932b7ffbd7eSJohannes Berg 
1933b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1934b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1935b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1936b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1937b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1938b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1939b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1940b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1941b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1942b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1943b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1944b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1945a74a8c84SJohannes Berg 	if (sta->sta.wme)
1946b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1947b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
1948b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1949b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
1950b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1951b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
1952b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1953b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1954b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1955b7ffbd7eSJohannes Berg 
1956b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
1957b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
1958b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1959b7ffbd7eSJohannes Berg 	else
1960b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
1961b7ffbd7eSJohannes Berg 
1962b7ffbd7eSJohannes Berg 	if (thr != 0) {
1963319090bfSJohannes Berg 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
1964b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
1965b7ffbd7eSJohannes Berg 	}
1966b7ffbd7eSJohannes Berg }
1967