xref: /openbmc/linux/net/mac80211/sta_info.c (revision 2b9a7e1bac24df8ddb0713ad1e5807a7243bcab0)
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 		sta_info_recalc_tim(sta);
120b22cfcfcSEliad Peller 	}
121b22cfcfcSEliad Peller 
122b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
123b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1241f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1251f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
126b22cfcfcSEliad Peller 	}
127b22cfcfcSEliad Peller 
12845b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12945b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
130b22cfcfcSEliad Peller 
1315ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
132b22cfcfcSEliad Peller 
133b22cfcfcSEliad Peller 	/*
134b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
135b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
136b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
137b22cfcfcSEliad Peller 	 * directly by station destruction.
138b22cfcfcSEliad Peller 	 */
1395a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
140661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
141b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
142b22cfcfcSEliad Peller 		if (!tid_tx)
143b22cfcfcSEliad Peller 			continue;
1441f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
145b22cfcfcSEliad Peller 		kfree(tid_tx);
146b22cfcfcSEliad Peller 	}
1475108ca82SJohannes Berg }
148b22cfcfcSEliad Peller 
1495108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1505108ca82SJohannes Berg {
1515108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1525108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1535108ca82SJohannes Berg 
1545108ca82SJohannes Berg 	__cleanup_single_sta(sta);
155b22cfcfcSEliad Peller 	sta_info_free(local, sta);
156b22cfcfcSEliad Peller }
157b22cfcfcSEliad Peller 
158d0709a65SJohannes Berg /* protected by RCU */
159abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
160abe60632SJohannes Berg 			      const u8 *addr)
16143ba7e95SJohannes Berg {
162abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16343ba7e95SJohannes Berg 	struct sta_info *sta;
16443ba7e95SJohannes Berg 
1650379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1660379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
16743ba7e95SJohannes Berg 	while (sta) {
168abe60632SJohannes Berg 		if (sta->sdata == sdata &&
169b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
17043ba7e95SJohannes Berg 			break;
1710379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1720379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
17343ba7e95SJohannes Berg 	}
17443ba7e95SJohannes Berg 	return sta;
17543ba7e95SJohannes Berg }
17643ba7e95SJohannes Berg 
1770e5ded5aSFelix Fietkau /*
1780e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1790e5ded5aSFelix Fietkau  * or from one of its vlans
1800e5ded5aSFelix Fietkau  */
1810e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1820e5ded5aSFelix Fietkau 				  const u8 *addr)
1830e5ded5aSFelix Fietkau {
1840e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1850e5ded5aSFelix Fietkau 	struct sta_info *sta;
1860e5ded5aSFelix Fietkau 
1870379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1880379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1890e5ded5aSFelix Fietkau 	while (sta) {
1900e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
191a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
192b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
1930e5ded5aSFelix Fietkau 			break;
1940379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1950379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1960e5ded5aSFelix Fietkau 	}
1970e5ded5aSFelix Fietkau 	return sta;
1980e5ded5aSFelix Fietkau }
1990e5ded5aSFelix Fietkau 
2003b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2013b53fde8SJohannes Berg 				     int idx)
202ee385855SLuis Carlos Cobo {
2033b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
204ee385855SLuis Carlos Cobo 	struct sta_info *sta;
205ee385855SLuis Carlos Cobo 	int i = 0;
206ee385855SLuis Carlos Cobo 
207d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2083b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2092a8ca29aSLuis Carlos Cobo 			continue;
210ee385855SLuis Carlos Cobo 		if (i < idx) {
211ee385855SLuis Carlos Cobo 			++i;
212ee385855SLuis Carlos Cobo 			continue;
213ee385855SLuis Carlos Cobo 		}
2142a8ca29aSLuis Carlos Cobo 		return sta;
215ee385855SLuis Carlos Cobo 	}
216ee385855SLuis Carlos Cobo 
217ee385855SLuis Carlos Cobo 	return NULL;
218ee385855SLuis Carlos Cobo }
219f0706e82SJiri Benc 
22093e5deb1SJohannes Berg /**
221d9a7ddb0SJohannes Berg  * sta_info_free - free STA
22293e5deb1SJohannes Berg  *
2236ef307bcSRandy Dunlap  * @local: pointer to the global information
22493e5deb1SJohannes Berg  * @sta: STA info to free
22593e5deb1SJohannes Berg  *
22693e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
227d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
228d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
229d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
23093e5deb1SJohannes Berg  */
231d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
23293e5deb1SJohannes Berg {
233ad38bfc9SMatti Gottlieb 	int i;
234ad38bfc9SMatti Gottlieb 
235889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2364b7679a5SJohannes Berg 		rate_control_free_sta(sta);
23793e5deb1SJohannes Berg 
238ad38bfc9SMatti Gottlieb 	if (sta->tx_lat) {
239ad38bfc9SMatti Gottlieb 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
240ad38bfc9SMatti Gottlieb 			kfree(sta->tx_lat[i].bins);
241ad38bfc9SMatti Gottlieb 		kfree(sta->tx_lat);
242ad38bfc9SMatti Gottlieb 	}
243ad38bfc9SMatti Gottlieb 
244bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
24593e5deb1SJohannes Berg 
24653d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
24793e5deb1SJohannes Berg 	kfree(sta);
24893e5deb1SJohannes Berg }
24993e5deb1SJohannes Berg 
2504d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
251d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
252d0709a65SJohannes Berg 			      struct sta_info *sta)
253f0706e82SJiri Benc {
2544d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
25517741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
256cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
257f0706e82SJiri Benc }
258f0706e82SJiri Benc 
2595ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
260af818581SJohannes Berg {
261af818581SJohannes Berg 	struct sta_info *sta;
262af818581SJohannes Berg 
2635ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
264af818581SJohannes Berg 
265af818581SJohannes Berg 	if (sta->dead)
266af818581SJohannes Berg 		return;
267af818581SJohannes Berg 
26854420473SHelmut Schaa 	local_bh_disable();
2695ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
270af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2715ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
272af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2735ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
27447086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
275ce662b44SJohannes Berg 	local_bh_enable();
276af818581SJohannes Berg }
277af818581SJohannes Berg 
278af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
279af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
280af65cd96SJohannes Berg {
281af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
282af65cd96SJohannes Berg 		return 0;
283af65cd96SJohannes Berg 
284889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
285af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
286af65cd96SJohannes Berg 						     &sta->sta, gfp);
287889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
288af65cd96SJohannes Berg 		return -ENOMEM;
289af65cd96SJohannes Berg 
290af65cd96SJohannes Berg 	return 0;
291af65cd96SJohannes Berg }
292af65cd96SJohannes Berg 
29373651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
29456544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
295f0706e82SJiri Benc {
296d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
297f0706e82SJiri Benc 	struct sta_info *sta;
298ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
299ad38bfc9SMatti Gottlieb 	struct ieee80211_tx_latency_bin_ranges *tx_latency;
30016c5f15cSRon Rindjunsky 	int i;
301f0706e82SJiri Benc 
30217741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
303f0706e82SJiri Benc 	if (!sta)
30473651ee6SJohannes Berg 		return NULL;
305f0706e82SJiri Benc 
306ef04a297SJohannes Berg 	rcu_read_lock();
307ef04a297SJohannes Berg 	tx_latency = rcu_dereference(local->tx_latency);
308ef04a297SJohannes Berg 	/* init stations Tx latency statistics && TID bins */
309ef04a297SJohannes Berg 	if (tx_latency) {
310ef04a297SJohannes Berg 		sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
311ef04a297SJohannes Berg 				      sizeof(struct ieee80211_tx_latency_stat),
312ef04a297SJohannes Berg 				      GFP_ATOMIC);
313ef04a297SJohannes Berg 		if (!sta->tx_lat) {
314ef04a297SJohannes Berg 			rcu_read_unlock();
315ef04a297SJohannes Berg 			goto free;
316ef04a297SJohannes Berg 		}
317ef04a297SJohannes Berg 
318ef04a297SJohannes Berg 		if (tx_latency->n_ranges) {
319ef04a297SJohannes Berg 			for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
320ef04a297SJohannes Berg 				/* size of bins is size of the ranges +1 */
321ef04a297SJohannes Berg 				sta->tx_lat[i].bin_count =
322ef04a297SJohannes Berg 					tx_latency->n_ranges + 1;
323ef04a297SJohannes Berg 				sta->tx_lat[i].bins =
324ef04a297SJohannes Berg 					kcalloc(sta->tx_lat[i].bin_count,
325ef04a297SJohannes Berg 						sizeof(u32), GFP_ATOMIC);
326ef04a297SJohannes Berg 				if (!sta->tx_lat[i].bins) {
327ef04a297SJohannes Berg 					rcu_read_unlock();
328ef04a297SJohannes Berg 					goto free;
329ef04a297SJohannes Berg 				}
330ef04a297SJohannes Berg 			}
331ef04a297SJohannes Berg 		}
332ef04a297SJohannes Berg 	}
333ef04a297SJohannes Berg 	rcu_read_unlock();
334ef04a297SJohannes Berg 
33507346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3361d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3375ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
33867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
339a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
34087f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
34187f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
34287f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
34387f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3446c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
34587f59c70SThomas Pedersen #endif
34607346f81SJohannes Berg 
34717741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
348d0709a65SJohannes Berg 	sta->local = local;
349d0709a65SJohannes Berg 	sta->sdata = sdata;
3508bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
351f0706e82SJiri Benc 
35271ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35371ec375cSJohannes Berg 
354b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
355b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
356b6da911bSLiad Kaufman 
35718171520SThomas Gleixner 	ktime_get_ts(&uptime);
358ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
359541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
360ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
361ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
362541a45a1SBruno Randolf 
363ef04a297SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp))
364ef04a297SJohannes Berg 		goto free;
365f0706e82SJiri Benc 
3665a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
367a622ab72SJohannes Berg 		/*
368a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
369a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
370a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
371a622ab72SJohannes Berg 		 */
37216c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
37316c5f15cSRon Rindjunsky 	}
374948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
375948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
376948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
377948d887dSJohannes Berg 	}
37873651ee6SJohannes Berg 
3795a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3804be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
381cccaec98SSenthil Balasubramanian 
382af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
383687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
384687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
385687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
386687da132SEmmanuel Grumbach 			local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
387687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
388687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
389687da132SEmmanuel Grumbach 		/*
390687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
391687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
392687da132SEmmanuel Grumbach 		 */
393687da132SEmmanuel Grumbach 		switch (smps) {
394687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
395687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
396687da132SEmmanuel Grumbach 			break;
397687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
398687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
399687da132SEmmanuel Grumbach 			break;
400687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
401687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
402687da132SEmmanuel Grumbach 			break;
403687da132SEmmanuel Grumbach 		default:
404687da132SEmmanuel Grumbach 			WARN_ON(1);
405687da132SEmmanuel Grumbach 		}
406687da132SEmmanuel Grumbach 	}
407af0ed69bSJohannes Berg 
408bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
40973651ee6SJohannes Berg 	return sta;
410ef04a297SJohannes Berg 
411ef04a297SJohannes Berg free:
412ef04a297SJohannes Berg 	if (sta->tx_lat) {
413ef04a297SJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
414ef04a297SJohannes Berg 			kfree(sta->tx_lat[i].bins);
415ef04a297SJohannes Berg 		kfree(sta->tx_lat);
416ef04a297SJohannes Berg 	}
417ef04a297SJohannes Berg 	kfree(sta);
418ef04a297SJohannes Berg 	return NULL;
41973651ee6SJohannes Berg }
42073651ee6SJohannes Berg 
4218c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
42234e89507SJohannes Berg {
42334e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42434e89507SJohannes Berg 
42503e4497eSJohannes Berg 	/*
42603e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
42703e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
42803e4497eSJohannes Berg 	 * and another CPU turns off the net device.
42903e4497eSJohannes Berg 	 */
4308c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4318c71df7aSGuy Eilam 		return -ENETDOWN;
43203e4497eSJohannes Berg 
433b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4348c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4358c71df7aSGuy Eilam 		return -EINVAL;
4368c71df7aSGuy Eilam 
4378c71df7aSGuy Eilam 	return 0;
43893e5deb1SJohannes Berg }
43944213b5eSJohannes Berg 
440f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
441f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
442f09603a2SJohannes Berg 				     struct sta_info *sta)
443f09603a2SJohannes Berg {
444f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
445f09603a2SJohannes Berg 	int err = 0;
446f09603a2SJohannes Berg 
447f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
448f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
449f09603a2SJohannes Berg 		if (err)
450f09603a2SJohannes Berg 			break;
451f09603a2SJohannes Berg 	}
452f09603a2SJohannes Berg 
453f09603a2SJohannes Berg 	if (!err) {
454a4ec45a4SJohannes Berg 		/*
455a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
456a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
457a4ec45a4SJohannes Berg 		 */
458a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
459f09603a2SJohannes Berg 			sta->uploaded = true;
460f09603a2SJohannes Berg 		return 0;
461f09603a2SJohannes Berg 	}
462f09603a2SJohannes Berg 
463f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
464bdcbd8e0SJohannes Berg 		sdata_info(sdata,
465bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
466bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
467f09603a2SJohannes Berg 		err = 0;
468f09603a2SJohannes Berg 	}
469f09603a2SJohannes Berg 
470f09603a2SJohannes Berg 	/* unwind on error */
471f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
472f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
473f09603a2SJohannes Berg 
474f09603a2SJohannes Berg 	return err;
475f09603a2SJohannes Berg }
476f09603a2SJohannes Berg 
47734e89507SJohannes Berg /*
4788c71df7aSGuy Eilam  * should be called with sta_mtx locked
4798c71df7aSGuy Eilam  * this function replaces the mutex lock
4808c71df7aSGuy Eilam  * with a RCU lock
4818c71df7aSGuy Eilam  */
4824d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4838c71df7aSGuy Eilam {
4848c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4858c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4867852e361SJohannes Berg 	struct station_info sinfo;
4878c71df7aSGuy Eilam 	int err = 0;
4888c71df7aSGuy Eilam 
4898c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4908c71df7aSGuy Eilam 
4917852e361SJohannes Berg 	/* check if STA exists already */
4927852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4934d33960bSJohannes Berg 		err = -EEXIST;
4944d33960bSJohannes Berg 		goto out_err;
49534e89507SJohannes Berg 	}
49634e89507SJohannes Berg 
4974d33960bSJohannes Berg 	local->num_sta++;
4984d33960bSJohannes Berg 	local->sta_generation++;
4994d33960bSJohannes Berg 	smp_mb();
5004d33960bSJohannes Berg 
5015108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5025108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5035108ca82SJohannes Berg 
5044d33960bSJohannes Berg 	/* make the station visible */
5054d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5064d33960bSJohannes Berg 
5072bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
50883d5cc01SJohannes Berg 
5095108ca82SJohannes Berg 	/* notify driver */
5105108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5115108ca82SJohannes Berg 	if (err)
5125108ca82SJohannes Berg 		goto out_remove;
5135108ca82SJohannes Berg 
51483d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5155108ca82SJohannes Berg 	/* accept BA sessions now */
5165108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5174d33960bSJohannes Berg 
51821f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
5194d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5204d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5214d33960bSJohannes Berg 
5224d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5234d33960bSJohannes Berg 	sinfo.filled = 0;
5244d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5254d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
526d0709a65SJohannes Berg 
527bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
528f0706e82SJiri Benc 
52934e89507SJohannes Berg 	/* move reference to rcu-protected */
53034e89507SJohannes Berg 	rcu_read_lock();
53134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
532e9f207f0SJiri Benc 
53373651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
53473651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
53573651ee6SJohannes Berg 
53673651ee6SJohannes Berg 	return 0;
5375108ca82SJohannes Berg  out_remove:
5385108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5395108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5405108ca82SJohannes Berg 	local->num_sta--;
5415108ca82SJohannes Berg 	synchronize_net();
5425108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5434d33960bSJohannes Berg  out_err:
5444d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5454d33960bSJohannes Berg 	rcu_read_lock();
5464d33960bSJohannes Berg 	return err;
5478c71df7aSGuy Eilam }
5488c71df7aSGuy Eilam 
5498c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5508c71df7aSGuy Eilam {
5518c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
552308f7fcfSZhao, Gang 	int err;
5538c71df7aSGuy Eilam 
5544d33960bSJohannes Berg 	might_sleep();
5554d33960bSJohannes Berg 
5568c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5578c71df7aSGuy Eilam 	if (err) {
5588c71df7aSGuy Eilam 		rcu_read_lock();
5598c71df7aSGuy Eilam 		goto out_free;
5608c71df7aSGuy Eilam 	}
5618c71df7aSGuy Eilam 
562004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
563004c872eSJohannes Berg 
5644d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5658c71df7aSGuy Eilam 	if (err)
566004c872eSJohannes Berg 		goto out_free;
567d0709a65SJohannes Berg 
568004c872eSJohannes Berg 	return 0;
56993e5deb1SJohannes Berg  out_free:
570d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
57193e5deb1SJohannes Berg 	return err;
572f0706e82SJiri Benc }
573f0706e82SJiri Benc 
57434e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
57534e89507SJohannes Berg {
57634e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
57734e89507SJohannes Berg 
57834e89507SJohannes Berg 	rcu_read_unlock();
57934e89507SJohannes Berg 
58034e89507SJohannes Berg 	return err;
58134e89507SJohannes Berg }
58234e89507SJohannes Berg 
583d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
584004c872eSJohannes Berg {
585004c872eSJohannes Berg 	/*
586004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
587004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
588004c872eSJohannes Berg 	 */
589d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
590004c872eSJohannes Berg }
591004c872eSJohannes Berg 
592d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
593004c872eSJohannes Berg {
594004c872eSJohannes Berg 	/*
595004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
596004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
597004c872eSJohannes Berg 	 */
598d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
599004c872eSJohannes Berg }
600004c872eSJohannes Berg 
6013d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6023d5839b6SIlan Peer {
6033d5839b6SIlan Peer 	/*
6043d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6053d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6063d5839b6SIlan Peer 	 */
6073d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6083d5839b6SIlan Peer }
6093d5839b6SIlan Peer 
610948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
611004c872eSJohannes Berg {
612948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
613948d887dSJohannes Berg 	switch (ac) {
614948d887dSJohannes Berg 	case IEEE80211_AC_VO:
615948d887dSJohannes Berg 		return BIT(6) | BIT(7);
616948d887dSJohannes Berg 	case IEEE80211_AC_VI:
617948d887dSJohannes Berg 		return BIT(4) | BIT(5);
618948d887dSJohannes Berg 	case IEEE80211_AC_BE:
619948d887dSJohannes Berg 		return BIT(0) | BIT(3);
620948d887dSJohannes Berg 	case IEEE80211_AC_BK:
621948d887dSJohannes Berg 		return BIT(1) | BIT(2);
622948d887dSJohannes Berg 	default:
623948d887dSJohannes Berg 		WARN_ON(1);
624948d887dSJohannes Berg 		return 0;
625d0709a65SJohannes Berg 	}
626004c872eSJohannes Berg }
627004c872eSJohannes Berg 
628c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
629004c872eSJohannes Berg {
630c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
631d012a605SMarco Porsch 	struct ps_data *ps;
632948d887dSJohannes Berg 	bool indicate_tim = false;
633948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
634948d887dSJohannes Berg 	int ac;
635d012a605SMarco Porsch 	u16 id;
636004c872eSJohannes Berg 
637d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
638d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
639c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
640c868cb35SJohannes Berg 			return;
6413e122be0SJohannes Berg 
642d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
643d012a605SMarco Porsch 		id = sta->sta.aid;
6443f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6453f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6463f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
647204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6486f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6493f52b7e3SMarco Porsch #endif
650d012a605SMarco Porsch 	} else {
651d012a605SMarco Porsch 		return;
652d012a605SMarco Porsch 	}
653d012a605SMarco Porsch 
654c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
655c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
656c868cb35SJohannes Berg 		return;
657004c872eSJohannes Berg 
658c868cb35SJohannes Berg 	if (sta->dead)
659c868cb35SJohannes Berg 		goto done;
6603e122be0SJohannes Berg 
661948d887dSJohannes Berg 	/*
662948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
663948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
664948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
665948d887dSJohannes Berg 	 * non-enabled ones.
666948d887dSJohannes Berg 	 */
667948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
668948d887dSJohannes Berg 		ignore_for_tim = 0;
669948d887dSJohannes Berg 
670948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
671948d887dSJohannes Berg 		unsigned long tids;
672948d887dSJohannes Berg 
673948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
674948d887dSJohannes Berg 			continue;
675948d887dSJohannes Berg 
676948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
677948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
678948d887dSJohannes Berg 		if (indicate_tim)
679948d887dSJohannes Berg 			break;
680948d887dSJohannes Berg 
681948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
682948d887dSJohannes Berg 
683948d887dSJohannes Berg 		indicate_tim |=
684948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
685004c872eSJohannes Berg 	}
686004c872eSJohannes Berg 
687c868cb35SJohannes Berg  done:
68865f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
689004c872eSJohannes Berg 
6903d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6913d5839b6SIlan Peer 		goto out_unlock;
6923d5839b6SIlan Peer 
693948d887dSJohannes Berg 	if (indicate_tim)
694d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
695c868cb35SJohannes Berg 	else
696d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6973e122be0SJohannes Berg 
698c868cb35SJohannes Berg 	if (local->ops->set_tim) {
699c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
700948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
701c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
702004c872eSJohannes Berg 	}
703004c872eSJohannes Berg 
7043d5839b6SIlan Peer out_unlock:
70565f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
706004c872eSJohannes Berg }
707004c872eSJohannes Berg 
708cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
709f0706e82SJiri Benc {
710e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
711f0706e82SJiri Benc 	int timeout;
712f0706e82SJiri Benc 
713f0706e82SJiri Benc 	if (!skb)
714cd0b8d89SJohannes Berg 		return false;
715f0706e82SJiri Benc 
716e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
717f0706e82SJiri Benc 
718f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
71957c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
72057c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
72157c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
722f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
723f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
724e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
725f0706e82SJiri Benc }
726f0706e82SJiri Benc 
727f0706e82SJiri Benc 
728948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
729948d887dSJohannes Berg 						struct sta_info *sta, int ac)
730f0706e82SJiri Benc {
731f0706e82SJiri Benc 	unsigned long flags;
732f0706e82SJiri Benc 	struct sk_buff *skb;
733f0706e82SJiri Benc 
73460750397SJohannes Berg 	/*
73560750397SJohannes Berg 	 * First check for frames that should expire on the filtered
73660750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
73760750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
73860750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
73960750397SJohannes Berg 	 * total_ps_buffered counter.
74060750397SJohannes Berg 	 */
741f0706e82SJiri Benc 	for (;;) {
742948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
743948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
74457c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
745948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
746836341a7SJohannes Berg 		else
747f0706e82SJiri Benc 			skb = NULL;
748948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
749f0706e82SJiri Benc 
75060750397SJohannes Berg 		/*
75160750397SJohannes Berg 		 * Frames are queued in order, so if this one
75260750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
75360750397SJohannes Berg 		 * we actually reached the end of the queue we
75460750397SJohannes Berg 		 * also need to stop, of course.
75560750397SJohannes Berg 		 */
75660750397SJohannes Berg 		if (!skb)
75760750397SJohannes Berg 			break;
758d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
75960750397SJohannes Berg 	}
76060750397SJohannes Berg 
76160750397SJohannes Berg 	/*
76260750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
76360750397SJohannes Berg 	 * only find something if the filtered queue was emptied
76460750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
76560750397SJohannes Berg 	 * buffered frames.
76660750397SJohannes Berg 	 */
767f0706e82SJiri Benc 	for (;;) {
768948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
769948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
770f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
771948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
772f0706e82SJiri Benc 		else
773f0706e82SJiri Benc 			skb = NULL;
774948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
775f0706e82SJiri Benc 
77660750397SJohannes Berg 		/*
77760750397SJohannes Berg 		 * frames are queued in order, so if this one
77860750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
77960750397SJohannes Berg 		 * the queue) we can stop testing
78060750397SJohannes Berg 		 */
781836341a7SJohannes Berg 		if (!skb)
782836341a7SJohannes Berg 			break;
783836341a7SJohannes Berg 
784f0706e82SJiri Benc 		local->total_ps_buffered--;
785bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
786bdcbd8e0SJohannes Berg 		       sta->sta.addr);
787d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
788f0706e82SJiri Benc 	}
7893393a608SJuuso Oikarinen 
79060750397SJohannes Berg 	/*
79160750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
79260750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
79360750397SJohannes Berg 	 * frames.
79460750397SJohannes Berg 	 */
79560750397SJohannes Berg 	sta_info_recalc_tim(sta);
79660750397SJohannes Berg 
79760750397SJohannes Berg 	/*
79860750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
79960750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
80060750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
80160750397SJohannes Berg 	 */
802948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
803948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
804948d887dSJohannes Berg }
805948d887dSJohannes Berg 
806948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
807948d887dSJohannes Berg 					     struct sta_info *sta)
808948d887dSJohannes Berg {
809948d887dSJohannes Berg 	bool have_buffered = false;
810948d887dSJohannes Berg 	int ac;
811948d887dSJohannes Berg 
8123f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8133f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8143f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
815948d887dSJohannes Berg 		return false;
816948d887dSJohannes Berg 
817948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
818948d887dSJohannes Berg 		have_buffered |=
819948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
820948d887dSJohannes Berg 
821948d887dSJohannes Berg 	return have_buffered;
822f0706e82SJiri Benc }
823f0706e82SJiri Benc 
824d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
82534e89507SJohannes Berg {
82634e89507SJohannes Berg 	struct ieee80211_local *local;
82734e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8286d10e46bSJohannes Berg 	int ret;
82934e89507SJohannes Berg 
83034e89507SJohannes Berg 	might_sleep();
83134e89507SJohannes Berg 
83234e89507SJohannes Berg 	if (!sta)
83334e89507SJohannes Berg 		return -ENOENT;
83434e89507SJohannes Berg 
83534e89507SJohannes Berg 	local = sta->local;
83634e89507SJohannes Berg 	sdata = sta->sdata;
83734e89507SJohannes Berg 
83883d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
83983d5cc01SJohannes Berg 
840098a6070SJohannes Berg 	/*
841098a6070SJohannes Berg 	 * Before removing the station from the driver and
842098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
843098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
844098a6070SJohannes Berg 	 * will be sufficient.
845098a6070SJohannes Berg 	 */
846c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
847c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
848098a6070SJohannes Berg 
84934e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
850b01711beSJohannes Berg 	if (WARN_ON(ret))
85134e89507SJohannes Berg 		return ret;
85234e89507SJohannes Berg 
853a7a6bdd0SArik Nemtsov 	/*
854a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
855a7a6bdd0SArik Nemtsov 	 * removal.
856a7a6bdd0SArik Nemtsov 	 */
857a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
858a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
859a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
860a7a6bdd0SArik Nemtsov 	}
861a7a6bdd0SArik Nemtsov 
862794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8634d33960bSJohannes Berg 
8646a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8656a9d1b91SJohannes Berg 
866a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
867a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
868a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
869a710c816SJohannes Berg 
870d778207bSJohannes Berg 	return 0;
871d778207bSJohannes Berg }
872d778207bSJohannes Berg 
873d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
874d778207bSJohannes Berg {
875d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
876d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
8776f7a8d26SJohannes Berg 	struct station_info sinfo = {};
878d778207bSJohannes Berg 	int ret;
879d778207bSJohannes Berg 
880d778207bSJohannes Berg 	/*
881d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
882d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
883d778207bSJohannes Berg 	 */
884d778207bSJohannes Berg 
885d778207bSJohannes Berg 	might_sleep();
886d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
887d778207bSJohannes Berg 
888c8782078SJohannes Berg 	/* now keys can no longer be reached */
8896d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
89034e89507SJohannes Berg 
89134e89507SJohannes Berg 	sta->dead = true;
89234e89507SJohannes Berg 
89334e89507SJohannes Berg 	local->num_sta--;
89434e89507SJohannes Berg 	local->sta_generation++;
89534e89507SJohannes Berg 
89683d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
897f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
898f09603a2SJohannes Berg 		if (ret) {
89983d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
90083d5cc01SJohannes Berg 			break;
90183d5cc01SJohannes Berg 		}
90283d5cc01SJohannes Berg 	}
903d9a7ddb0SJohannes Berg 
904f09603a2SJohannes Berg 	if (sta->uploaded) {
905f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
906f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
907f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
908f09603a2SJohannes Berg 	}
90934e89507SJohannes Berg 
910bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
911bdcbd8e0SJohannes Berg 
9126f7a8d26SJohannes Berg 	sta_set_sinfo(sta, &sinfo);
9136f7a8d26SJohannes Berg 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
914ec15e68bSJouni Malinen 
91534e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
91634e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
91721f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
91834e89507SJohannes Berg 
919d34ba216SJohannes Berg 	cleanup_single_sta(sta);
920d778207bSJohannes Berg }
921d778207bSJohannes Berg 
922d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
923d778207bSJohannes Berg {
924d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
925d778207bSJohannes Berg 
926d778207bSJohannes Berg 	if (err)
927d778207bSJohannes Berg 		return err;
928d778207bSJohannes Berg 
929d778207bSJohannes Berg 	synchronize_net();
930d778207bSJohannes Berg 
931d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
93234e89507SJohannes Berg 
93334e89507SJohannes Berg 	return 0;
93434e89507SJohannes Berg }
93534e89507SJohannes Berg 
93634e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
93734e89507SJohannes Berg {
93834e89507SJohannes Berg 	struct sta_info *sta;
93934e89507SJohannes Berg 	int ret;
94034e89507SJohannes Berg 
94134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9427852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
94334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
94434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
94534e89507SJohannes Berg 
94634e89507SJohannes Berg 	return ret;
94734e89507SJohannes Berg }
94834e89507SJohannes Berg 
94934e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
95034e89507SJohannes Berg 			      const u8 *addr)
95134e89507SJohannes Berg {
95234e89507SJohannes Berg 	struct sta_info *sta;
95334e89507SJohannes Berg 	int ret;
95434e89507SJohannes Berg 
95534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9567852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
95734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
95834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
95934e89507SJohannes Berg 
96034e89507SJohannes Berg 	return ret;
96134e89507SJohannes Berg }
962f0706e82SJiri Benc 
963f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
964f0706e82SJiri Benc {
965f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
966f0706e82SJiri Benc 	struct sta_info *sta;
9673393a608SJuuso Oikarinen 	bool timer_needed = false;
968f0706e82SJiri Benc 
969d0709a65SJohannes Berg 	rcu_read_lock();
970d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9713393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9723393a608SJuuso Oikarinen 			timer_needed = true;
973d0709a65SJohannes Berg 	rcu_read_unlock();
974f0706e82SJiri Benc 
9755bb644a0SJohannes Berg 	if (local->quiescing)
9765bb644a0SJohannes Berg 		return;
9775bb644a0SJohannes Berg 
9783393a608SJuuso Oikarinen 	if (!timer_needed)
9793393a608SJuuso Oikarinen 		return;
9803393a608SJuuso Oikarinen 
98126d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
98226d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
983f0706e82SJiri Benc }
984f0706e82SJiri Benc 
985f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
986f0706e82SJiri Benc {
9874d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
98834e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
989f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
990f0706e82SJiri Benc 
991b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
992b24b8a24SPavel Emelyanov 		    (unsigned long)local);
993f0706e82SJiri Benc }
994f0706e82SJiri Benc 
995f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
996f0706e82SJiri Benc {
997a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
998f0706e82SJiri Benc }
999f0706e82SJiri Benc 
1000051007d9SJohannes Berg 
1001e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1002f0706e82SJiri Benc {
1003b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1004f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1005d778207bSJohannes Berg 	LIST_HEAD(free_list);
100644213b5eSJohannes Berg 	int ret = 0;
1007f0706e82SJiri Benc 
1008d0709a65SJohannes Berg 	might_sleep();
1009d0709a65SJohannes Berg 
1010e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1011e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1012e716251dSJohannes Berg 
101334e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
101434e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1015e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1016e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1017d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1018d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
101934316837SJohannes Berg 			ret++;
102034316837SJohannes Berg 		}
102134e89507SJohannes Berg 	}
1022d778207bSJohannes Berg 
1023d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1024d778207bSJohannes Berg 		synchronize_net();
1025d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1026d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1027d778207bSJohannes Berg 	}
102834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
102944213b5eSJohannes Berg 
1030051007d9SJohannes Berg 	return ret;
1031051007d9SJohannes Berg }
1032051007d9SJohannes Berg 
103324723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
103424723d1bSJohannes Berg 			  unsigned long exp_time)
103524723d1bSJohannes Berg {
103624723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
103724723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
103824723d1bSJohannes Berg 
103934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1040e46a2cf9SMohammed Shafi Shajakhan 
1041e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1042ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1043ec2b774eSMarek Lindner 			continue;
1044ec2b774eSMarek Lindner 
104524723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1046eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1047bdcbd8e0SJohannes Berg 				sta->sta.addr);
10483f52b7e3SMarco Porsch 
10493f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10503f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10513f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10523f52b7e3SMarco Porsch 
105334e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
105424723d1bSJohannes Berg 		}
1055e46a2cf9SMohammed Shafi Shajakhan 	}
1056e46a2cf9SMohammed Shafi Shajakhan 
105734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
105824723d1bSJohannes Berg }
105917741cdcSJohannes Berg 
1060686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1061686b9cb9SBen Greear 					       const u8 *addr,
1062686b9cb9SBen Greear 					       const u8 *localaddr)
106317741cdcSJohannes Berg {
1064abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
106517741cdcSJohannes Berg 
1066686b9cb9SBen Greear 	/*
1067686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1068686b9cb9SBen Greear 	 * ... first in list.
1069686b9cb9SBen Greear 	 */
1070f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1071686b9cb9SBen Greear 		if (localaddr &&
1072b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1073686b9cb9SBen Greear 			continue;
1074f7c65594SJohannes Berg 		if (!sta->uploaded)
1075f7c65594SJohannes Berg 			return NULL;
107617741cdcSJohannes Berg 		return &sta->sta;
1077f7c65594SJohannes Berg 	}
1078f7c65594SJohannes Berg 
1079abe60632SJohannes Berg 	return NULL;
108017741cdcSJohannes Berg }
1081686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10825ed176e1SJohannes Berg 
10835ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10845ed176e1SJohannes Berg 					 const u8 *addr)
10855ed176e1SJohannes Berg {
1086f7c65594SJohannes Berg 	struct sta_info *sta;
10875ed176e1SJohannes Berg 
10885ed176e1SJohannes Berg 	if (!vif)
10895ed176e1SJohannes Berg 		return NULL;
10905ed176e1SJohannes Berg 
1091f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1092f7c65594SJohannes Berg 	if (!sta)
1093f7c65594SJohannes Berg 		return NULL;
10945ed176e1SJohannes Berg 
1095f7c65594SJohannes Berg 	if (!sta->uploaded)
1096f7c65594SJohannes Berg 		return NULL;
1097f7c65594SJohannes Berg 
1098f7c65594SJohannes Berg 	return &sta->sta;
10995ed176e1SJohannes Berg }
110017741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1101af818581SJohannes Berg 
1102e3685e03SJohannes Berg /* powersave support code */
1103e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
110450a9432dSJohannes Berg {
1105608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1106e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1107e3685e03SJohannes Berg 	struct sk_buff_head pending;
1108e3685e03SJohannes Berg 	int filtered = 0, buffered = 0, ac;
1109e3685e03SJohannes Berg 	unsigned long flags;
1110d012a605SMarco Porsch 	struct ps_data *ps;
1111d012a605SMarco Porsch 
11123918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
11133918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11143918edb0SFelix Fietkau 				     u.ap);
11153918edb0SFelix Fietkau 
11163918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1117d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11183f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11193f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1120d012a605SMarco Porsch 	else
1121d012a605SMarco Porsch 		return;
112250a9432dSJohannes Berg 
1123c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
112447086fc5SJohannes Berg 
11255a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1126948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1127948d887dSJohannes Berg 
1128d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
112912375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1130af818581SJohannes Berg 
1131948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1132af818581SJohannes Berg 
11331d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11341d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1135af818581SJohannes Berg 	/* Send all buffered frames to the station */
1136948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1137948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1138948d887dSJohannes Berg 
1139987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1140948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1141987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1142948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1143948d887dSJohannes Berg 		filtered += tmp - count;
1144948d887dSJohannes Berg 		count = tmp;
1145948d887dSJohannes Berg 
1146987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1147948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1148987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1149948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1150948d887dSJohannes Berg 		buffered += tmp - count;
1151948d887dSJohannes Berg 	}
1152948d887dSJohannes Berg 
1153e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11545ac2e350SJohannes Berg 
11555ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11565ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11575ac2e350SJohannes Berg 
11585ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
11595ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
11605ac2e350SJohannes Berg 	 */
11615ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
11625ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
11631d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1164948d887dSJohannes Berg 
1165e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1166e3685e03SJohannes Berg 
1167687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1168062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1169062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1170687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1171687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1172687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1173687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1174687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1175687da132SEmmanuel Grumbach 		       sta->sta.addr);
1176687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1177687da132SEmmanuel Grumbach 					   sta->sta.addr,
1178687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1179687da132SEmmanuel Grumbach 	}
1180687da132SEmmanuel Grumbach 
1181af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1182af818581SJohannes Berg 
1183c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1184c868cb35SJohannes Berg 
1185bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1186bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1187bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1188af818581SJohannes Berg }
1189af818581SJohannes Berg 
1190ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1191ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1192b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1193b77cf4f8SJohannes Berg 					 bool call_driver)
1194ce662b44SJohannes Berg {
1195ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1196ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1197ce662b44SJohannes Berg 	struct sk_buff *skb;
1198ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1199ce662b44SJohannes Berg 	__le16 fc;
1200a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1201ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
120255de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1203ce662b44SJohannes Berg 
1204ce662b44SJohannes Berg 	if (qos) {
1205ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1206ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1207ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1208ce662b44SJohannes Berg 	} else {
1209ce662b44SJohannes Berg 		size -= 2;
1210ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1211ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1212ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1213ce662b44SJohannes Berg 	}
1214ce662b44SJohannes Berg 
1215ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1216ce662b44SJohannes Berg 	if (!skb)
1217ce662b44SJohannes Berg 		return;
1218ce662b44SJohannes Berg 
1219ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1220ce662b44SJohannes Berg 
1221ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1222ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1223ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1224ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1225ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1226ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1227864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1228ce662b44SJohannes Berg 
1229ce662b44SJohannes Berg 	skb->priority = tid;
1230ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
123159b66255SJohannes Berg 	if (qos) {
1232ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1233ce662b44SJohannes Berg 
123440b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1235ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1236ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1237ce662b44SJohannes Berg 	}
1238ce662b44SJohannes Berg 
1239ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1240ce662b44SJohannes Berg 
1241ce662b44SJohannes Berg 	/*
1242ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1243ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1244deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1245deeaee19SJohannes Berg 	 * ends the poll/service period.
1246ce662b44SJohannes Berg 	 */
124702f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1248deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1249ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1250ce662b44SJohannes Berg 
12516b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
12526b127c71SSujith Manoharan 
1253b77cf4f8SJohannes Berg 	if (call_driver)
1254b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1255b77cf4f8SJohannes Berg 					  reason, false);
125640b96408SJohannes Berg 
125789afe614SJohannes Berg 	skb->dev = sdata->dev;
125889afe614SJohannes Berg 
125955de908aSJohannes Berg 	rcu_read_lock();
126055de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
126155de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
126255de908aSJohannes Berg 		rcu_read_unlock();
126355de908aSJohannes Berg 		kfree_skb(skb);
126455de908aSJohannes Berg 		return;
126555de908aSJohannes Berg 	}
126655de908aSJohannes Berg 
126773c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
126873c4e195SJohannes Berg 	ieee80211_xmit(sdata, skb);
126955de908aSJohannes Berg 	rcu_read_unlock();
1270ce662b44SJohannes Berg }
1271ce662b44SJohannes Berg 
12720a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
12730a1cb809SJohannes Berg {
12740a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
12750a1cb809SJohannes Berg 	if (tids & 0xF8)
12760a1cb809SJohannes Berg 		return fls(tids) - 1;
12770a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
12780a1cb809SJohannes Berg 	if (tids & BIT(0))
12790a1cb809SJohannes Berg 		return 0;
12800a1cb809SJohannes Berg 	return fls(tids) - 1;
12810a1cb809SJohannes Berg }
12820a1cb809SJohannes Berg 
128347086fc5SJohannes Berg static void
128447086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
128547086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
128647086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1287af818581SJohannes Berg {
1288af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1289af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1290948d887dSJohannes Berg 	bool more_data = false;
1291948d887dSJohannes Berg 	int ac;
12924049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
129347086fc5SJohannes Berg 	struct sk_buff_head frames;
129447086fc5SJohannes Berg 
1295deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1296c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1297deeaee19SJohannes Berg 
129847086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1299af818581SJohannes Berg 
1300f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1301948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13024049e09aSJohannes Berg 		unsigned long tids;
13034049e09aSJohannes Berg 
130447086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1305948d887dSJohannes Berg 			continue;
1306948d887dSJohannes Berg 
13074049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
13084049e09aSJohannes Berg 
1309f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1310f9f760b4SJohannes Berg 		 * release from hardware queues
1311f9f760b4SJohannes Berg 		 */
1312f9f760b4SJohannes Berg 		if (skb_queue_empty(&frames))
1313f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1314f9f760b4SJohannes Berg 
13154049e09aSJohannes Berg 		if (driver_release_tids) {
1316f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1317f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1318f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1319f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1320f9f760b4SJohannes Berg 			 */
1321f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1322f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1323f9f760b4SJohannes Berg 				more_data = true;
1324f9f760b4SJohannes Berg 				driver_release_tids =
1325f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1326f9f760b4SJohannes Berg 						driver_release_tids));
1327f9f760b4SJohannes Berg 				break;
1328f9f760b4SJohannes Berg 			}
13294049e09aSJohannes Berg 		} else {
133047086fc5SJohannes Berg 			struct sk_buff *skb;
133147086fc5SJohannes Berg 
133247086fc5SJohannes Berg 			while (n_frames > 0) {
1333948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1334948d887dSJohannes Berg 				if (!skb) {
133547086fc5SJohannes Berg 					skb = skb_dequeue(
133647086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1337af818581SJohannes Berg 					if (skb)
1338af818581SJohannes Berg 						local->total_ps_buffered--;
1339af818581SJohannes Berg 				}
134047086fc5SJohannes Berg 				if (!skb)
134147086fc5SJohannes Berg 					break;
134247086fc5SJohannes Berg 				n_frames--;
134347086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
134447086fc5SJohannes Berg 			}
1345948d887dSJohannes Berg 		}
1346948d887dSJohannes Berg 
1347f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1348f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1349f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13504049e09aSJohannes Berg 		 */
1351948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1352948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1353948d887dSJohannes Berg 			more_data = true;
1354948d887dSJohannes Berg 			break;
1355948d887dSJohannes Berg 		}
1356948d887dSJohannes Berg 	}
1357af818581SJohannes Berg 
1358f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1359ce662b44SJohannes Berg 		int tid;
13604049e09aSJohannes Berg 
1361ce662b44SJohannes Berg 		/*
1362ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1363ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1364ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1365ce662b44SJohannes Berg 		 *
1366ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1367ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1368ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1369ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1370ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1371ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1372ce662b44SJohannes Berg 		 *
1373ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1374ce662b44SJohannes Berg 		 */
1375ce662b44SJohannes Berg 
1376ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1377ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1378ce662b44SJohannes Berg 
1379b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1380f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
138147086fc5SJohannes Berg 		struct sk_buff_head pending;
138247086fc5SJohannes Berg 		struct sk_buff *skb;
138340b96408SJohannes Berg 		int num = 0;
138440b96408SJohannes Berg 		u16 tids = 0;
1385b77cf4f8SJohannes Berg 		bool need_null = false;
138647086fc5SJohannes Berg 
138747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
138847086fc5SJohannes Berg 
138947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1390af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
139147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
139240b96408SJohannes Berg 			u8 *qoshdr = NULL;
139340b96408SJohannes Berg 
139440b96408SJohannes Berg 			num++;
1395af818581SJohannes Berg 
1396af818581SJohannes Berg 			/*
139747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
139847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
139947086fc5SJohannes Berg 			 * exchange.
1400af818581SJohannes Berg 			 */
14016b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
14026b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1403af818581SJohannes Berg 
140447086fc5SJohannes Berg 			/*
140547086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
140647086fc5SJohannes Berg 			 * more buffered frames for this STA
140747086fc5SJohannes Berg 			 */
140824b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
140947086fc5SJohannes Berg 				hdr->frame_control |=
141047086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
141124b9c373SJanusz.Dziedzic@tieto.com 			else
141224b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
141324b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1414af818581SJohannes Berg 
141540b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
141640b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
141740b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
141840b96408SJohannes Berg 
1419b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1420b77cf4f8SJohannes Berg 
1421b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1422b77cf4f8SJohannes Berg 
1423b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1424b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1425b77cf4f8SJohannes Berg 				continue;
1426b77cf4f8SJohannes Berg 
1427b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1428b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1429b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1430b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1431b77cf4f8SJohannes Berg 				break;
1432b77cf4f8SJohannes Berg 			}
1433b77cf4f8SJohannes Berg 
1434b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1435b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1436b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1437b77cf4f8SJohannes Berg 			 * and be done.
1438b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1439b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1440b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1441b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1442b77cf4f8SJohannes Berg 			 *
1443b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1444b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1445b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1446b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1447b77cf4f8SJohannes Berg 			 *
1448b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1449b77cf4f8SJohannes Berg 			 */
1450b77cf4f8SJohannes Berg 			if (qoshdr) {
145140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1452deeaee19SJohannes Berg 
145347086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
145447086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1455b77cf4f8SJohannes Berg 			} else {
1456b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1457b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1458b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1459b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1460b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1461b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1462b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1463b77cf4f8SJohannes Berg 				 */
1464b77cf4f8SJohannes Berg 				hdr->frame_control |=
1465b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1466b77cf4f8SJohannes Berg 				need_null = true;
1467b77cf4f8SJohannes Berg 				num++;
146852a3f20cSMarco Porsch 			}
1469b77cf4f8SJohannes Berg 			break;
147047086fc5SJohannes Berg 		}
147147086fc5SJohannes Berg 
147240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
147340b96408SJohannes Berg 					  reason, more_data);
147440b96408SJohannes Berg 
147547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1476af818581SJohannes Berg 
1477b77cf4f8SJohannes Berg 		if (need_null)
1478b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1479b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1480b77cf4f8SJohannes Berg 				reason, false);
1481b77cf4f8SJohannes Berg 
1482c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1483af818581SJohannes Berg 	} else {
1484af818581SJohannes Berg 		/*
14854049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
14864049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1487f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1488f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1489f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1490f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1491f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1492f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1493af818581SJohannes Berg 		 */
14944049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
149547086fc5SJohannes Berg 					    n_frames, reason, more_data);
14964049e09aSJohannes Berg 
14974049e09aSJohannes Berg 		/*
14984049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
14994049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1500f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
15014049e09aSJohannes Berg 		 * release function.
1502f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
15034049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
15044049e09aSJohannes Berg 		 */
1505af818581SJohannes Berg 	}
1506af818581SJohannes Berg }
1507af818581SJohannes Berg 
1508af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1509af818581SJohannes Berg {
151047086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1511af818581SJohannes Berg 
1512af818581SJohannes Berg 	/*
151347086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
151447086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
151547086fc5SJohannes Berg 	 * only from the non-enabled ones.
1516af818581SJohannes Berg 	 */
151747086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
151847086fc5SJohannes Berg 		ignore_for_response = 0;
1519af818581SJohannes Berg 
152047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
152147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1522af818581SJohannes Berg }
152347086fc5SJohannes Berg 
152447086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
152547086fc5SJohannes Berg {
152647086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
152747086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
152847086fc5SJohannes Berg 
152947086fc5SJohannes Berg 	/*
153047086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
153147086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
153247086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
153347086fc5SJohannes Berg 	 * actually getting called.
153447086fc5SJohannes Berg 	 */
153547086fc5SJohannes Berg 	if (!delivery_enabled)
153647086fc5SJohannes Berg 		return;
153747086fc5SJohannes Berg 
153847086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
153947086fc5SJohannes Berg 	case 1:
154047086fc5SJohannes Berg 		n_frames = 2;
154147086fc5SJohannes Berg 		break;
154247086fc5SJohannes Berg 	case 2:
154347086fc5SJohannes Berg 		n_frames = 4;
154447086fc5SJohannes Berg 		break;
154547086fc5SJohannes Berg 	case 3:
154647086fc5SJohannes Berg 		n_frames = 6;
154747086fc5SJohannes Berg 		break;
154847086fc5SJohannes Berg 	case 0:
154947086fc5SJohannes Berg 		/* XXX: what is a good value? */
155013a8098aSAndrei Otcheretianski 		n_frames = 128;
155147086fc5SJohannes Berg 		break;
155247086fc5SJohannes Berg 	}
155347086fc5SJohannes Berg 
155447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
155547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1556af818581SJohannes Berg }
1557af818581SJohannes Berg 
1558af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1559af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1560af818581SJohannes Berg {
1561af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1562af818581SJohannes Berg 
1563b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1564b5878a2dSJohannes Berg 
15655ac2e350SJohannes Berg 	if (block) {
1566c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
15675ac2e350SJohannes Berg 		return;
15685ac2e350SJohannes Berg 	}
15695ac2e350SJohannes Berg 
15705ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
15715ac2e350SJohannes Berg 		return;
15725ac2e350SJohannes Berg 
15735ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
15745ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
15755ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15765ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15775ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
15785ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
15795ac2e350SJohannes Berg 		/* must be asleep in this case */
15805ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15815ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15825ac2e350SJohannes Berg 	} else {
15835ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15845ac2e350SJohannes Berg 	}
1585af818581SJohannes Berg }
1586af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1587dcf55fb5SFelix Fietkau 
1588e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
158937fbd908SJohannes Berg {
159037fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
159137fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
159237fbd908SJohannes Berg 
159337fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
159437fbd908SJohannes Berg 
159537fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
159637fbd908SJohannes Berg }
1597e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
159837fbd908SJohannes Berg 
1599042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1600042ec453SJohannes Berg 				u8 tid, bool buffered)
1601dcf55fb5SFelix Fietkau {
1602dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1603dcf55fb5SFelix Fietkau 
16045a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1605042ec453SJohannes Berg 		return;
1606042ec453SJohannes Berg 
16071b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
16081b000789SJohannes Berg 
1609948d887dSJohannes Berg 	if (buffered)
1610948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1611948d887dSJohannes Berg 	else
1612948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1613948d887dSJohannes Berg 
1614c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1615dcf55fb5SFelix Fietkau }
1616042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1617d9a7ddb0SJohannes Berg 
161883d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1619d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1620d9a7ddb0SJohannes Berg {
16218bf11d8dSJohannes Berg 	might_sleep();
1622d9a7ddb0SJohannes Berg 
1623d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1624d9a7ddb0SJohannes Berg 		return 0;
1625d9a7ddb0SJohannes Berg 
1626f09603a2SJohannes Berg 	/* check allowed transitions first */
1627f09603a2SJohannes Berg 
1628d9a7ddb0SJohannes Berg 	switch (new_state) {
1629d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1630f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1631d9a7ddb0SJohannes Berg 			return -EINVAL;
1632d9a7ddb0SJohannes Berg 		break;
1633d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1634f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1635f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1636d9a7ddb0SJohannes Berg 			return -EINVAL;
1637d9a7ddb0SJohannes Berg 		break;
1638d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1639f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1640f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1641d9a7ddb0SJohannes Berg 			return -EINVAL;
1642d9a7ddb0SJohannes Berg 		break;
1643d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1644f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1645d9a7ddb0SJohannes Berg 			return -EINVAL;
1646d9a7ddb0SJohannes Berg 		break;
1647d9a7ddb0SJohannes Berg 	default:
1648d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1649d9a7ddb0SJohannes Berg 		return -EINVAL;
1650d9a7ddb0SJohannes Berg 	}
1651d9a7ddb0SJohannes Berg 
1652bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1653bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1654f09603a2SJohannes Berg 
1655f09603a2SJohannes Berg 	/*
1656f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1657f09603a2SJohannes Berg 	 * fail the transition
1658f09603a2SJohannes Berg 	 */
1659f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1660f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1661f09603a2SJohannes Berg 					sta->sta_state, new_state);
1662f09603a2SJohannes Berg 		if (err)
1663f09603a2SJohannes Berg 			return err;
1664f09603a2SJohannes Berg 	}
1665f09603a2SJohannes Berg 
1666f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1667f09603a2SJohannes Berg 
1668f09603a2SJohannes Berg 	switch (new_state) {
1669f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1670f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1671f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1672f09603a2SJohannes Berg 		break;
1673f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1674f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1675f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1676f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1677f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1678f09603a2SJohannes Berg 		break;
1679f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1680f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1681f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1682f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
16837e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16847e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16857e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16867e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1687f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1688f09603a2SJohannes Berg 		}
1689f09603a2SJohannes Berg 		break;
1690f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1691f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
16927e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16937e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16947e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16957e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1696f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1697f09603a2SJohannes Berg 		}
1698f09603a2SJohannes Berg 		break;
1699f09603a2SJohannes Berg 	default:
1700f09603a2SJohannes Berg 		break;
1701f09603a2SJohannes Berg 	}
1702f09603a2SJohannes Berg 
1703d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1704d9a7ddb0SJohannes Berg 
1705d9a7ddb0SJohannes Berg 	return 0;
1706d9a7ddb0SJohannes Berg }
1707687da132SEmmanuel Grumbach 
1708687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1709687da132SEmmanuel Grumbach {
1710687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1711687da132SEmmanuel Grumbach 	u8 rx_streams;
1712687da132SEmmanuel Grumbach 
1713687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1714687da132SEmmanuel Grumbach 		return 1;
1715687da132SEmmanuel Grumbach 
1716687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1717687da132SEmmanuel Grumbach 		int i;
1718687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1719687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1720687da132SEmmanuel Grumbach 
1721687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1722687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1723687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1724687da132SEmmanuel Grumbach 				return i + 1;
1725687da132SEmmanuel Grumbach 	}
1726687da132SEmmanuel Grumbach 
1727687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1728687da132SEmmanuel Grumbach 		rx_streams = 4;
1729687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1730687da132SEmmanuel Grumbach 		rx_streams = 3;
1731687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1732687da132SEmmanuel Grumbach 		rx_streams = 2;
1733687da132SEmmanuel Grumbach 	else
1734687da132SEmmanuel Grumbach 		rx_streams = 1;
1735687da132SEmmanuel Grumbach 
1736687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1737687da132SEmmanuel Grumbach 		return rx_streams;
1738687da132SEmmanuel Grumbach 
1739687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1740687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1741687da132SEmmanuel Grumbach }
1742b7ffbd7eSJohannes Berg 
1743b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1744b7ffbd7eSJohannes Berg {
1745b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1746b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
17479a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1748b7ffbd7eSJohannes Berg 	struct timespec uptime;
1749b7ffbd7eSJohannes Berg 	u32 thr = 0;
1750b7ffbd7eSJohannes Berg 	int i, ac;
1751b7ffbd7eSJohannes Berg 
17529a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
17539a244409SJohn W. Linville 		ref = local->rate_ctrl;
17549a244409SJohn W. Linville 
1755b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1756b7ffbd7eSJohannes Berg 
1757*2b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1758*2b9a7e1bSJohannes Berg 
1759*2b9a7e1bSJohannes Berg 	sinfo->filled |= STATION_INFO_INACTIVE_TIME |
1760*2b9a7e1bSJohannes Berg 			 STATION_INFO_STA_FLAGS |
1761b7ffbd7eSJohannes Berg 			 STATION_INFO_BSS_PARAM |
1762b7ffbd7eSJohannes Berg 			 STATION_INFO_CONNECTED_TIME |
1763*2b9a7e1bSJohannes Berg 			 STATION_INFO_RX_DROP_MISC |
1764b7ffbd7eSJohannes Berg 			 STATION_INFO_BEACON_LOSS_COUNT;
1765b7ffbd7eSJohannes Berg 
176618171520SThomas Gleixner 	ktime_get_ts(&uptime);
1767b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1768b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
1769*2b9a7e1bSJohannes Berg 
1770*2b9a7e1bSJohannes Berg 	if (!(sinfo->filled & (STATION_INFO_TX_BYTES64 |
1771*2b9a7e1bSJohannes Berg 			       STATION_INFO_TX_BYTES))) {
1772b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
1773*2b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1774b7ffbd7eSJohannes Berg 			sinfo->tx_bytes += sta->tx_bytes[ac];
1775*2b9a7e1bSJohannes Berg 		sinfo->filled |= STATION_INFO_TX_BYTES64;
1776b7ffbd7eSJohannes Berg 	}
1777*2b9a7e1bSJohannes Berg 
1778*2b9a7e1bSJohannes Berg 	if (!(sinfo->filled & STATION_INFO_TX_PACKETS)) {
1779*2b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
1780*2b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1781*2b9a7e1bSJohannes Berg 			sinfo->tx_packets += sta->tx_packets[ac];
1782*2b9a7e1bSJohannes Berg 		sinfo->filled |= STATION_INFO_TX_PACKETS;
1783*2b9a7e1bSJohannes Berg 	}
1784*2b9a7e1bSJohannes Berg 
1785*2b9a7e1bSJohannes Berg 	if (!(sinfo->filled & (STATION_INFO_RX_BYTES64 |
1786*2b9a7e1bSJohannes Berg 			       STATION_INFO_RX_BYTES))) {
1787b7ffbd7eSJohannes Berg 		sinfo->rx_bytes = sta->rx_bytes;
1788*2b9a7e1bSJohannes Berg 		sinfo->filled |= STATION_INFO_RX_BYTES64;
1789*2b9a7e1bSJohannes Berg 	}
1790*2b9a7e1bSJohannes Berg 
1791*2b9a7e1bSJohannes Berg 	if (!(sinfo->filled & STATION_INFO_RX_PACKETS)) {
1792b7ffbd7eSJohannes Berg 		sinfo->rx_packets = sta->rx_packets;
1793*2b9a7e1bSJohannes Berg 		sinfo->filled |= STATION_INFO_RX_PACKETS;
1794*2b9a7e1bSJohannes Berg 	}
1795*2b9a7e1bSJohannes Berg 
1796*2b9a7e1bSJohannes Berg 	if (!(sinfo->filled & STATION_INFO_TX_RETRIES)) {
1797b7ffbd7eSJohannes Berg 		sinfo->tx_retries = sta->tx_retry_count;
1798*2b9a7e1bSJohannes Berg 		sinfo->filled |= STATION_INFO_TX_RETRIES;
1799*2b9a7e1bSJohannes Berg 	}
1800*2b9a7e1bSJohannes Berg 
1801*2b9a7e1bSJohannes Berg 	if (!(sinfo->filled & STATION_INFO_TX_FAILED)) {
1802b7ffbd7eSJohannes Berg 		sinfo->tx_failed = sta->tx_retry_failed;
1803*2b9a7e1bSJohannes Berg 		sinfo->filled |= STATION_INFO_TX_FAILED;
1804*2b9a7e1bSJohannes Berg 	}
1805*2b9a7e1bSJohannes Berg 
1806b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1807b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1808b7ffbd7eSJohannes Berg 
1809b7ffbd7eSJohannes Berg 	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1810b7ffbd7eSJohannes Berg 	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1811*2b9a7e1bSJohannes Berg 		if (!(sinfo->filled & STATION_INFO_SIGNAL)) {
1812b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1813*2b9a7e1bSJohannes Berg 			sinfo->filled |= STATION_INFO_SIGNAL;
1814b7ffbd7eSJohannes Berg 		}
1815*2b9a7e1bSJohannes Berg 
1816*2b9a7e1bSJohannes Berg 		if (!(sinfo->filled & STATION_INFO_SIGNAL_AVG)) {
1817*2b9a7e1bSJohannes Berg 			sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1818*2b9a7e1bSJohannes Berg 			sinfo->filled |= STATION_INFO_SIGNAL_AVG;
1819*2b9a7e1bSJohannes Berg 		}
1820*2b9a7e1bSJohannes Berg 	}
1821*2b9a7e1bSJohannes Berg 
1822*2b9a7e1bSJohannes Berg 	if (sta->chains &&
1823*2b9a7e1bSJohannes Berg 	    !(sinfo->filled & (STATION_INFO_CHAIN_SIGNAL |
1824*2b9a7e1bSJohannes Berg 			       STATION_INFO_CHAIN_SIGNAL_AVG))) {
1825b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_CHAIN_SIGNAL |
1826b7ffbd7eSJohannes Berg 				 STATION_INFO_CHAIN_SIGNAL_AVG;
1827b7ffbd7eSJohannes Berg 
1828b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1829b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1830b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1831b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1832b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1833b7ffbd7eSJohannes Berg 		}
1834b7ffbd7eSJohannes Berg 	}
1835b7ffbd7eSJohannes Berg 
1836*2b9a7e1bSJohannes Berg 	if (!(sinfo->filled & STATION_INFO_TX_BITRATE)) {
1837b7ffbd7eSJohannes Berg 		sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1838*2b9a7e1bSJohannes Berg 		sinfo->filled |= STATION_INFO_TX_BITRATE;
1839*2b9a7e1bSJohannes Berg 	}
1840*2b9a7e1bSJohannes Berg 
1841*2b9a7e1bSJohannes Berg 	if (!(sinfo->filled & STATION_INFO_RX_BITRATE)) {
1842b7ffbd7eSJohannes Berg 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
1843*2b9a7e1bSJohannes Berg 		sinfo->filled |= STATION_INFO_RX_BITRATE;
1844*2b9a7e1bSJohannes Berg 	}
1845b7ffbd7eSJohannes Berg 
1846b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1847b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1848b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_LLID |
1849b7ffbd7eSJohannes Berg 				 STATION_INFO_PLID |
1850b7ffbd7eSJohannes Berg 				 STATION_INFO_PLINK_STATE |
1851b7ffbd7eSJohannes Berg 				 STATION_INFO_LOCAL_PM |
1852b7ffbd7eSJohannes Berg 				 STATION_INFO_PEER_PM |
1853b7ffbd7eSJohannes Berg 				 STATION_INFO_NONPEER_PM;
1854b7ffbd7eSJohannes Berg 
1855b7ffbd7eSJohannes Berg 		sinfo->llid = sta->llid;
1856b7ffbd7eSJohannes Berg 		sinfo->plid = sta->plid;
1857b7ffbd7eSJohannes Berg 		sinfo->plink_state = sta->plink_state;
1858b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1859b7ffbd7eSJohannes Berg 			sinfo->filled |= STATION_INFO_T_OFFSET;
1860b7ffbd7eSJohannes Berg 			sinfo->t_offset = sta->t_offset;
1861b7ffbd7eSJohannes Berg 		}
1862b7ffbd7eSJohannes Berg 		sinfo->local_pm = sta->local_pm;
1863b7ffbd7eSJohannes Berg 		sinfo->peer_pm = sta->peer_pm;
1864b7ffbd7eSJohannes Berg 		sinfo->nonpeer_pm = sta->nonpeer_pm;
1865b7ffbd7eSJohannes Berg #endif
1866b7ffbd7eSJohannes Berg 	}
1867b7ffbd7eSJohannes Berg 
1868b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1869b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1870b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1871b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1872b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1873b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1874b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1875785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1876b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1877b7ffbd7eSJohannes Berg 
1878b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1879b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1880b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1881b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1882b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1883b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1884b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1885b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1886b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1887b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1888b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1889b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1890a74a8c84SJohannes Berg 	if (sta->sta.wme)
1891b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1892b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
1893b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1894b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
1895b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1896b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
1897b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1898b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1899b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1900b7ffbd7eSJohannes Berg 
1901b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
1902b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
1903b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1904b7ffbd7eSJohannes Berg 	else
1905b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
1906b7ffbd7eSJohannes Berg 
1907b7ffbd7eSJohannes Berg 	if (thr != 0) {
1908b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_EXPECTED_THROUGHPUT;
1909b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
1910b7ffbd7eSJohannes Berg 	}
1911b7ffbd7eSJohannes Berg }
1912