xref: /openbmc/linux/net/mac80211/sta_info.c (revision b7ffbd7ef6751f6cde73082346e365738daf00d2)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4f0706e82SJiri Benc  *
5f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
6f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
7f0706e82SJiri Benc  * published by the Free Software Foundation.
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21f0706e82SJiri Benc #include <net/mac80211.h>
22f0706e82SJiri Benc #include "ieee80211_i.h"
2324487981SJohannes Berg #include "driver-ops.h"
242c8dccc7SJohannes Berg #include "rate.h"
25f0706e82SJiri Benc #include "sta_info.h"
26e9f207f0SJiri Benc #include "debugfs_sta.h"
27ee385855SLuis Carlos Cobo #include "mesh.h"
28ce662b44SJohannes Berg #include "wme.h"
29f0706e82SJiri Benc 
30d0709a65SJohannes Berg /**
31d0709a65SJohannes Berg  * DOC: STA information lifetime rules
32d0709a65SJohannes Berg  *
33d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
34d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
35d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
36d0709a65SJohannes Berg  *
3734e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3834e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
3934e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4034e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4134e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4234e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4334e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4434e89507SJohannes Berg  * encryption keys.
4593e5deb1SJohannes Berg  *
4693e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4793e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
48d0709a65SJohannes Berg  *
4934e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
507e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
517e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5225985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5334e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5425985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5534e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
567e189a12SLuis R. Rodriguez  *
5734e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5834e89507SJohannes Berg  * calls are available.
59d0709a65SJohannes Berg  *
6034e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6134e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6234e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6334e89507SJohannes Berg  * freed before they are done using it.
64d0709a65SJohannes Berg  */
65f0706e82SJiri Benc 
664d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
67be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
68f0706e82SJiri Benc 			     struct sta_info *sta)
69f0706e82SJiri Benc {
70f0706e82SJiri Benc 	struct sta_info *s;
71f0706e82SJiri Benc 
7240b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
734d33960bSJohannes Berg 				      lockdep_is_held(&local->sta_mtx));
74f0706e82SJiri Benc 	if (!s)
75be8755e1SMichael Wu 		return -ENOENT;
76be8755e1SMichael Wu 	if (s == sta) {
77cf778b00SEric Dumazet 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
78d0709a65SJohannes Berg 				   s->hnext);
79be8755e1SMichael Wu 		return 0;
80f0706e82SJiri Benc 	}
81f0706e82SJiri Benc 
8240b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8340b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8440b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
854d33960bSJohannes Berg 					lockdep_is_held(&local->sta_mtx));
8640b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
87cf778b00SEric Dumazet 		rcu_assign_pointer(s->hnext, sta->hnext);
88be8755e1SMichael Wu 		return 0;
89f0706e82SJiri Benc 	}
90f0706e82SJiri Benc 
91be8755e1SMichael Wu 	return -ENOENT;
92f0706e82SJiri Benc }
93f0706e82SJiri Benc 
945108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
95b22cfcfcSEliad Peller {
96b22cfcfcSEliad Peller 	int ac, i;
97b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
98b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
99b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
100d012a605SMarco Porsch 	struct ps_data *ps;
101b22cfcfcSEliad Peller 
102e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1035ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1045ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
105d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
106d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
107d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1083f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1093f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
110d012a605SMarco Porsch 		else
111d012a605SMarco Porsch 			return;
112b22cfcfcSEliad Peller 
113b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
114e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1155ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
116b22cfcfcSEliad Peller 
117d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
118b22cfcfcSEliad Peller 		sta_info_recalc_tim(sta);
119b22cfcfcSEliad Peller 	}
120b22cfcfcSEliad Peller 
121b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
122b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1231f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1241f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
125b22cfcfcSEliad Peller 	}
126b22cfcfcSEliad Peller 
12745b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12845b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
129b22cfcfcSEliad Peller 
1305ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
131b22cfcfcSEliad Peller 
132b22cfcfcSEliad Peller 	/*
133b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
134b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
135b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
136b22cfcfcSEliad Peller 	 * directly by station destruction.
137b22cfcfcSEliad Peller 	 */
1385a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
139661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
140b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
141b22cfcfcSEliad Peller 		if (!tid_tx)
142b22cfcfcSEliad Peller 			continue;
1431f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
144b22cfcfcSEliad Peller 		kfree(tid_tx);
145b22cfcfcSEliad Peller 	}
1465108ca82SJohannes Berg }
147b22cfcfcSEliad Peller 
1485108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1495108ca82SJohannes Berg {
1505108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1515108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1525108ca82SJohannes Berg 
1535108ca82SJohannes Berg 	__cleanup_single_sta(sta);
154b22cfcfcSEliad Peller 	sta_info_free(local, sta);
155b22cfcfcSEliad Peller }
156b22cfcfcSEliad Peller 
157d0709a65SJohannes Berg /* protected by RCU */
158abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
159abe60632SJohannes Berg 			      const u8 *addr)
16043ba7e95SJohannes Berg {
161abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16243ba7e95SJohannes Berg 	struct sta_info *sta;
16343ba7e95SJohannes Berg 
1640379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1650379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
16643ba7e95SJohannes Berg 	while (sta) {
167abe60632SJohannes Berg 		if (sta->sdata == sdata &&
168b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
16943ba7e95SJohannes Berg 			break;
1700379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1710379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
17243ba7e95SJohannes Berg 	}
17343ba7e95SJohannes Berg 	return sta;
17443ba7e95SJohannes Berg }
17543ba7e95SJohannes Berg 
1760e5ded5aSFelix Fietkau /*
1770e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1780e5ded5aSFelix Fietkau  * or from one of its vlans
1790e5ded5aSFelix Fietkau  */
1800e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1810e5ded5aSFelix Fietkau 				  const u8 *addr)
1820e5ded5aSFelix Fietkau {
1830e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1840e5ded5aSFelix Fietkau 	struct sta_info *sta;
1850e5ded5aSFelix Fietkau 
1860379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1870379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1880e5ded5aSFelix Fietkau 	while (sta) {
1890e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
190a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
191b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
1920e5ded5aSFelix Fietkau 			break;
1930379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1940379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1950e5ded5aSFelix Fietkau 	}
1960e5ded5aSFelix Fietkau 	return sta;
1970e5ded5aSFelix Fietkau }
1980e5ded5aSFelix Fietkau 
1993b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2003b53fde8SJohannes Berg 				     int idx)
201ee385855SLuis Carlos Cobo {
2023b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
203ee385855SLuis Carlos Cobo 	struct sta_info *sta;
204ee385855SLuis Carlos Cobo 	int i = 0;
205ee385855SLuis Carlos Cobo 
206d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2073b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2082a8ca29aSLuis Carlos Cobo 			continue;
209ee385855SLuis Carlos Cobo 		if (i < idx) {
210ee385855SLuis Carlos Cobo 			++i;
211ee385855SLuis Carlos Cobo 			continue;
212ee385855SLuis Carlos Cobo 		}
2132a8ca29aSLuis Carlos Cobo 		return sta;
214ee385855SLuis Carlos Cobo 	}
215ee385855SLuis Carlos Cobo 
216ee385855SLuis Carlos Cobo 	return NULL;
217ee385855SLuis Carlos Cobo }
218f0706e82SJiri Benc 
21993e5deb1SJohannes Berg /**
220d9a7ddb0SJohannes Berg  * sta_info_free - free STA
22193e5deb1SJohannes Berg  *
2226ef307bcSRandy Dunlap  * @local: pointer to the global information
22393e5deb1SJohannes Berg  * @sta: STA info to free
22493e5deb1SJohannes Berg  *
22593e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
226d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
227d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
228d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
22993e5deb1SJohannes Berg  */
230d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
23193e5deb1SJohannes Berg {
232ad38bfc9SMatti Gottlieb 	int i;
233ad38bfc9SMatti Gottlieb 
234889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2354b7679a5SJohannes Berg 		rate_control_free_sta(sta);
23693e5deb1SJohannes Berg 
237ad38bfc9SMatti Gottlieb 	if (sta->tx_lat) {
238ad38bfc9SMatti Gottlieb 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
239ad38bfc9SMatti Gottlieb 			kfree(sta->tx_lat[i].bins);
240ad38bfc9SMatti Gottlieb 		kfree(sta->tx_lat);
241ad38bfc9SMatti Gottlieb 	}
242ad38bfc9SMatti Gottlieb 
243bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
24493e5deb1SJohannes Berg 
24553d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
24693e5deb1SJohannes Berg 	kfree(sta);
24793e5deb1SJohannes Berg }
24893e5deb1SJohannes Berg 
2494d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
250d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
251d0709a65SJohannes Berg 			      struct sta_info *sta)
252f0706e82SJiri Benc {
2534d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
25417741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
255cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
256f0706e82SJiri Benc }
257f0706e82SJiri Benc 
2585ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
259af818581SJohannes Berg {
260af818581SJohannes Berg 	struct sta_info *sta;
261af818581SJohannes Berg 
2625ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
263af818581SJohannes Berg 
264af818581SJohannes Berg 	if (sta->dead)
265af818581SJohannes Berg 		return;
266af818581SJohannes Berg 
26754420473SHelmut Schaa 	local_bh_disable();
2685ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
269af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2705ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
271af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2725ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
27347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
274ce662b44SJohannes Berg 	local_bh_enable();
275af818581SJohannes Berg }
276af818581SJohannes Berg 
277af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
278af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
279af65cd96SJohannes Berg {
280af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
281af65cd96SJohannes Berg 		return 0;
282af65cd96SJohannes Berg 
283889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
284af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
285af65cd96SJohannes Berg 						     &sta->sta, gfp);
286889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
287af65cd96SJohannes Berg 		return -ENOMEM;
288af65cd96SJohannes Berg 
289af65cd96SJohannes Berg 	return 0;
290af65cd96SJohannes Berg }
291af65cd96SJohannes Berg 
29273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
29356544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
294f0706e82SJiri Benc {
295d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
296f0706e82SJiri Benc 	struct sta_info *sta;
297ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
298ad38bfc9SMatti Gottlieb 	struct ieee80211_tx_latency_bin_ranges *tx_latency;
29916c5f15cSRon Rindjunsky 	int i;
300f0706e82SJiri Benc 
30117741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
302f0706e82SJiri Benc 	if (!sta)
30373651ee6SJohannes Berg 		return NULL;
304f0706e82SJiri Benc 
305ef04a297SJohannes Berg 	rcu_read_lock();
306ef04a297SJohannes Berg 	tx_latency = rcu_dereference(local->tx_latency);
307ef04a297SJohannes Berg 	/* init stations Tx latency statistics && TID bins */
308ef04a297SJohannes Berg 	if (tx_latency) {
309ef04a297SJohannes Berg 		sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
310ef04a297SJohannes Berg 				      sizeof(struct ieee80211_tx_latency_stat),
311ef04a297SJohannes Berg 				      GFP_ATOMIC);
312ef04a297SJohannes Berg 		if (!sta->tx_lat) {
313ef04a297SJohannes Berg 			rcu_read_unlock();
314ef04a297SJohannes Berg 			goto free;
315ef04a297SJohannes Berg 		}
316ef04a297SJohannes Berg 
317ef04a297SJohannes Berg 		if (tx_latency->n_ranges) {
318ef04a297SJohannes Berg 			for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
319ef04a297SJohannes Berg 				/* size of bins is size of the ranges +1 */
320ef04a297SJohannes Berg 				sta->tx_lat[i].bin_count =
321ef04a297SJohannes Berg 					tx_latency->n_ranges + 1;
322ef04a297SJohannes Berg 				sta->tx_lat[i].bins =
323ef04a297SJohannes Berg 					kcalloc(sta->tx_lat[i].bin_count,
324ef04a297SJohannes Berg 						sizeof(u32), GFP_ATOMIC);
325ef04a297SJohannes Berg 				if (!sta->tx_lat[i].bins) {
326ef04a297SJohannes Berg 					rcu_read_unlock();
327ef04a297SJohannes Berg 					goto free;
328ef04a297SJohannes Berg 				}
329ef04a297SJohannes Berg 			}
330ef04a297SJohannes Berg 		}
331ef04a297SJohannes Berg 	}
332ef04a297SJohannes Berg 	rcu_read_unlock();
333ef04a297SJohannes Berg 
33407346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3351d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3365ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
33767c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
338a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
33987f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
34087f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
34187f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
34287f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3436c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
34487f59c70SThomas Pedersen #endif
34507346f81SJohannes Berg 
34617741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
347d0709a65SJohannes Berg 	sta->local = local;
348d0709a65SJohannes Berg 	sta->sdata = sdata;
3498bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
350f0706e82SJiri Benc 
35171ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35271ec375cSJohannes Berg 
353ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
354ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
355541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
356ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
357ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
358541a45a1SBruno Randolf 
359ef04a297SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp))
360ef04a297SJohannes Berg 		goto free;
361f0706e82SJiri Benc 
3625a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
363a622ab72SJohannes Berg 		/*
364a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
365a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
366a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
367a622ab72SJohannes Berg 		 */
36816c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
36916c5f15cSRon Rindjunsky 	}
370948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
371948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
372948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
373948d887dSJohannes Berg 	}
37473651ee6SJohannes Berg 
3755a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3764be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
377cccaec98SSenthil Balasubramanian 
378af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
379687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
380687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
381687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
382687da132SEmmanuel Grumbach 			local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
383687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
384687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
385687da132SEmmanuel Grumbach 		/*
386687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
387687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
388687da132SEmmanuel Grumbach 		 */
389687da132SEmmanuel Grumbach 		switch (smps) {
390687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
391687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
392687da132SEmmanuel Grumbach 			break;
393687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
394687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
395687da132SEmmanuel Grumbach 			break;
396687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
397687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
398687da132SEmmanuel Grumbach 			break;
399687da132SEmmanuel Grumbach 		default:
400687da132SEmmanuel Grumbach 			WARN_ON(1);
401687da132SEmmanuel Grumbach 		}
402687da132SEmmanuel Grumbach 	}
403af0ed69bSJohannes Berg 
404bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
40573651ee6SJohannes Berg 	return sta;
406ef04a297SJohannes Berg 
407ef04a297SJohannes Berg free:
408ef04a297SJohannes Berg 	if (sta->tx_lat) {
409ef04a297SJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
410ef04a297SJohannes Berg 			kfree(sta->tx_lat[i].bins);
411ef04a297SJohannes Berg 		kfree(sta->tx_lat);
412ef04a297SJohannes Berg 	}
413ef04a297SJohannes Berg 	kfree(sta);
414ef04a297SJohannes Berg 	return NULL;
41573651ee6SJohannes Berg }
41673651ee6SJohannes Berg 
4178c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
41834e89507SJohannes Berg {
41934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42034e89507SJohannes Berg 
42103e4497eSJohannes Berg 	/*
42203e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
42303e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
42403e4497eSJohannes Berg 	 * and another CPU turns off the net device.
42503e4497eSJohannes Berg 	 */
4268c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4278c71df7aSGuy Eilam 		return -ENETDOWN;
42803e4497eSJohannes Berg 
429b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4308c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4318c71df7aSGuy Eilam 		return -EINVAL;
4328c71df7aSGuy Eilam 
4338c71df7aSGuy Eilam 	return 0;
43493e5deb1SJohannes Berg }
43544213b5eSJohannes Berg 
436f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
437f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
438f09603a2SJohannes Berg 				     struct sta_info *sta)
439f09603a2SJohannes Berg {
440f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
441f09603a2SJohannes Berg 	int err = 0;
442f09603a2SJohannes Berg 
443f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
444f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
445f09603a2SJohannes Berg 		if (err)
446f09603a2SJohannes Berg 			break;
447f09603a2SJohannes Berg 	}
448f09603a2SJohannes Berg 
449f09603a2SJohannes Berg 	if (!err) {
450a4ec45a4SJohannes Berg 		/*
451a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
452a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
453a4ec45a4SJohannes Berg 		 */
454a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
455f09603a2SJohannes Berg 			sta->uploaded = true;
456f09603a2SJohannes Berg 		return 0;
457f09603a2SJohannes Berg 	}
458f09603a2SJohannes Berg 
459f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
460bdcbd8e0SJohannes Berg 		sdata_info(sdata,
461bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
462bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
463f09603a2SJohannes Berg 		err = 0;
464f09603a2SJohannes Berg 	}
465f09603a2SJohannes Berg 
466f09603a2SJohannes Berg 	/* unwind on error */
467f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
468f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
469f09603a2SJohannes Berg 
470f09603a2SJohannes Berg 	return err;
471f09603a2SJohannes Berg }
472f09603a2SJohannes Berg 
47334e89507SJohannes Berg /*
4748c71df7aSGuy Eilam  * should be called with sta_mtx locked
4758c71df7aSGuy Eilam  * this function replaces the mutex lock
4768c71df7aSGuy Eilam  * with a RCU lock
4778c71df7aSGuy Eilam  */
4784d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4798c71df7aSGuy Eilam {
4808c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4818c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4827852e361SJohannes Berg 	struct station_info sinfo;
4838c71df7aSGuy Eilam 	int err = 0;
4848c71df7aSGuy Eilam 
4858c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4868c71df7aSGuy Eilam 
4877852e361SJohannes Berg 	/* check if STA exists already */
4887852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4894d33960bSJohannes Berg 		err = -EEXIST;
4904d33960bSJohannes Berg 		goto out_err;
49134e89507SJohannes Berg 	}
49234e89507SJohannes Berg 
4934d33960bSJohannes Berg 	local->num_sta++;
4944d33960bSJohannes Berg 	local->sta_generation++;
4954d33960bSJohannes Berg 	smp_mb();
4964d33960bSJohannes Berg 
4975108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
4985108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4995108ca82SJohannes Berg 
5004d33960bSJohannes Berg 	/* make the station visible */
5014d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5024d33960bSJohannes Berg 
503794454ceSArik Nemtsov 	list_add_rcu(&sta->list, &local->sta_list);
50483d5cc01SJohannes Berg 
5055108ca82SJohannes Berg 	/* notify driver */
5065108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5075108ca82SJohannes Berg 	if (err)
5085108ca82SJohannes Berg 		goto out_remove;
5095108ca82SJohannes Berg 
51083d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5115108ca82SJohannes Berg 	/* accept BA sessions now */
5125108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5134d33960bSJohannes Berg 
51421f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
5154d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5164d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5174d33960bSJohannes Berg 
5184d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5194d33960bSJohannes Berg 	sinfo.filled = 0;
5204d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5214d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
522d0709a65SJohannes Berg 
523bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
524f0706e82SJiri Benc 
52534e89507SJohannes Berg 	/* move reference to rcu-protected */
52634e89507SJohannes Berg 	rcu_read_lock();
52734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
528e9f207f0SJiri Benc 
52973651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
53073651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
53173651ee6SJohannes Berg 
53273651ee6SJohannes Berg 	return 0;
5335108ca82SJohannes Berg  out_remove:
5345108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5355108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5365108ca82SJohannes Berg 	local->num_sta--;
5375108ca82SJohannes Berg 	synchronize_net();
5385108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5394d33960bSJohannes Berg  out_err:
5404d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5414d33960bSJohannes Berg 	rcu_read_lock();
5424d33960bSJohannes Berg 	return err;
5438c71df7aSGuy Eilam }
5448c71df7aSGuy Eilam 
5458c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5468c71df7aSGuy Eilam {
5478c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
548308f7fcfSZhao, Gang 	int err;
5498c71df7aSGuy Eilam 
5504d33960bSJohannes Berg 	might_sleep();
5514d33960bSJohannes Berg 
5528c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5538c71df7aSGuy Eilam 	if (err) {
5548c71df7aSGuy Eilam 		rcu_read_lock();
5558c71df7aSGuy Eilam 		goto out_free;
5568c71df7aSGuy Eilam 	}
5578c71df7aSGuy Eilam 
558004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
559004c872eSJohannes Berg 
5604d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5618c71df7aSGuy Eilam 	if (err)
562004c872eSJohannes Berg 		goto out_free;
563d0709a65SJohannes Berg 
564004c872eSJohannes Berg 	return 0;
56593e5deb1SJohannes Berg  out_free:
566d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
56793e5deb1SJohannes Berg 	return err;
568f0706e82SJiri Benc }
569f0706e82SJiri Benc 
57034e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
57134e89507SJohannes Berg {
57234e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
57334e89507SJohannes Berg 
57434e89507SJohannes Berg 	rcu_read_unlock();
57534e89507SJohannes Berg 
57634e89507SJohannes Berg 	return err;
57734e89507SJohannes Berg }
57834e89507SJohannes Berg 
579d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
580004c872eSJohannes Berg {
581004c872eSJohannes Berg 	/*
582004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
583004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
584004c872eSJohannes Berg 	 */
585d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
586004c872eSJohannes Berg }
587004c872eSJohannes Berg 
588d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
589004c872eSJohannes Berg {
590004c872eSJohannes Berg 	/*
591004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
592004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
593004c872eSJohannes Berg 	 */
594d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
595004c872eSJohannes Berg }
596004c872eSJohannes Berg 
5973d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
5983d5839b6SIlan Peer {
5993d5839b6SIlan Peer 	/*
6003d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6013d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6023d5839b6SIlan Peer 	 */
6033d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6043d5839b6SIlan Peer }
6053d5839b6SIlan Peer 
606948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
607004c872eSJohannes Berg {
608948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
609948d887dSJohannes Berg 	switch (ac) {
610948d887dSJohannes Berg 	case IEEE80211_AC_VO:
611948d887dSJohannes Berg 		return BIT(6) | BIT(7);
612948d887dSJohannes Berg 	case IEEE80211_AC_VI:
613948d887dSJohannes Berg 		return BIT(4) | BIT(5);
614948d887dSJohannes Berg 	case IEEE80211_AC_BE:
615948d887dSJohannes Berg 		return BIT(0) | BIT(3);
616948d887dSJohannes Berg 	case IEEE80211_AC_BK:
617948d887dSJohannes Berg 		return BIT(1) | BIT(2);
618948d887dSJohannes Berg 	default:
619948d887dSJohannes Berg 		WARN_ON(1);
620948d887dSJohannes Berg 		return 0;
621d0709a65SJohannes Berg 	}
622004c872eSJohannes Berg }
623004c872eSJohannes Berg 
624c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
625004c872eSJohannes Berg {
626c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
627d012a605SMarco Porsch 	struct ps_data *ps;
628948d887dSJohannes Berg 	bool indicate_tim = false;
629948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
630948d887dSJohannes Berg 	int ac;
631d012a605SMarco Porsch 	u16 id;
632004c872eSJohannes Berg 
633d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
634d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
635c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
636c868cb35SJohannes Berg 			return;
6373e122be0SJohannes Berg 
638d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
639d012a605SMarco Porsch 		id = sta->sta.aid;
6403f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6413f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6423f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
643204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6446f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6453f52b7e3SMarco Porsch #endif
646d012a605SMarco Porsch 	} else {
647d012a605SMarco Porsch 		return;
648d012a605SMarco Porsch 	}
649d012a605SMarco Porsch 
650c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
651c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
652c868cb35SJohannes Berg 		return;
653004c872eSJohannes Berg 
654c868cb35SJohannes Berg 	if (sta->dead)
655c868cb35SJohannes Berg 		goto done;
6563e122be0SJohannes Berg 
657948d887dSJohannes Berg 	/*
658948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
659948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
660948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
661948d887dSJohannes Berg 	 * non-enabled ones.
662948d887dSJohannes Berg 	 */
663948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
664948d887dSJohannes Berg 		ignore_for_tim = 0;
665948d887dSJohannes Berg 
666948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
667948d887dSJohannes Berg 		unsigned long tids;
668948d887dSJohannes Berg 
669948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
670948d887dSJohannes Berg 			continue;
671948d887dSJohannes Berg 
672948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
673948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
674948d887dSJohannes Berg 		if (indicate_tim)
675948d887dSJohannes Berg 			break;
676948d887dSJohannes Berg 
677948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
678948d887dSJohannes Berg 
679948d887dSJohannes Berg 		indicate_tim |=
680948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
681004c872eSJohannes Berg 	}
682004c872eSJohannes Berg 
683c868cb35SJohannes Berg  done:
68465f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
685004c872eSJohannes Berg 
6863d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6873d5839b6SIlan Peer 		goto out_unlock;
6883d5839b6SIlan Peer 
689948d887dSJohannes Berg 	if (indicate_tim)
690d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
691c868cb35SJohannes Berg 	else
692d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6933e122be0SJohannes Berg 
694c868cb35SJohannes Berg 	if (local->ops->set_tim) {
695c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
696948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
697c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
698004c872eSJohannes Berg 	}
699004c872eSJohannes Berg 
7003d5839b6SIlan Peer out_unlock:
70165f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
702004c872eSJohannes Berg }
703004c872eSJohannes Berg 
704cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
705f0706e82SJiri Benc {
706e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
707f0706e82SJiri Benc 	int timeout;
708f0706e82SJiri Benc 
709f0706e82SJiri Benc 	if (!skb)
710cd0b8d89SJohannes Berg 		return false;
711f0706e82SJiri Benc 
712e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
713f0706e82SJiri Benc 
714f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
71557c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
71657c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
71757c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
718f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
719f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
720e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
721f0706e82SJiri Benc }
722f0706e82SJiri Benc 
723f0706e82SJiri Benc 
724948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
725948d887dSJohannes Berg 						struct sta_info *sta, int ac)
726f0706e82SJiri Benc {
727f0706e82SJiri Benc 	unsigned long flags;
728f0706e82SJiri Benc 	struct sk_buff *skb;
729f0706e82SJiri Benc 
73060750397SJohannes Berg 	/*
73160750397SJohannes Berg 	 * First check for frames that should expire on the filtered
73260750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
73360750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
73460750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
73560750397SJohannes Berg 	 * total_ps_buffered counter.
73660750397SJohannes Berg 	 */
737f0706e82SJiri Benc 	for (;;) {
738948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
739948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
74057c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
741948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
742836341a7SJohannes Berg 		else
743f0706e82SJiri Benc 			skb = NULL;
744948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
745f0706e82SJiri Benc 
74660750397SJohannes Berg 		/*
74760750397SJohannes Berg 		 * Frames are queued in order, so if this one
74860750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
74960750397SJohannes Berg 		 * we actually reached the end of the queue we
75060750397SJohannes Berg 		 * also need to stop, of course.
75160750397SJohannes Berg 		 */
75260750397SJohannes Berg 		if (!skb)
75360750397SJohannes Berg 			break;
754d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
75560750397SJohannes Berg 	}
75660750397SJohannes Berg 
75760750397SJohannes Berg 	/*
75860750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
75960750397SJohannes Berg 	 * only find something if the filtered queue was emptied
76060750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
76160750397SJohannes Berg 	 * buffered frames.
76260750397SJohannes Berg 	 */
763f0706e82SJiri Benc 	for (;;) {
764948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
765948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
766f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
767948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
768f0706e82SJiri Benc 		else
769f0706e82SJiri Benc 			skb = NULL;
770948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
771f0706e82SJiri Benc 
77260750397SJohannes Berg 		/*
77360750397SJohannes Berg 		 * frames are queued in order, so if this one
77460750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
77560750397SJohannes Berg 		 * the queue) we can stop testing
77660750397SJohannes Berg 		 */
777836341a7SJohannes Berg 		if (!skb)
778836341a7SJohannes Berg 			break;
779836341a7SJohannes Berg 
780f0706e82SJiri Benc 		local->total_ps_buffered--;
781bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
782bdcbd8e0SJohannes Berg 		       sta->sta.addr);
783d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
784f0706e82SJiri Benc 	}
7853393a608SJuuso Oikarinen 
78660750397SJohannes Berg 	/*
78760750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
78860750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
78960750397SJohannes Berg 	 * frames.
79060750397SJohannes Berg 	 */
79160750397SJohannes Berg 	sta_info_recalc_tim(sta);
79260750397SJohannes Berg 
79360750397SJohannes Berg 	/*
79460750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
79560750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
79660750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
79760750397SJohannes Berg 	 */
798948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
799948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
800948d887dSJohannes Berg }
801948d887dSJohannes Berg 
802948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
803948d887dSJohannes Berg 					     struct sta_info *sta)
804948d887dSJohannes Berg {
805948d887dSJohannes Berg 	bool have_buffered = false;
806948d887dSJohannes Berg 	int ac;
807948d887dSJohannes Berg 
8083f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8093f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8103f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
811948d887dSJohannes Berg 		return false;
812948d887dSJohannes Berg 
813948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
814948d887dSJohannes Berg 		have_buffered |=
815948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
816948d887dSJohannes Berg 
817948d887dSJohannes Berg 	return have_buffered;
818f0706e82SJiri Benc }
819f0706e82SJiri Benc 
820d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
82134e89507SJohannes Berg {
82234e89507SJohannes Berg 	struct ieee80211_local *local;
82334e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8246d10e46bSJohannes Berg 	int ret;
82534e89507SJohannes Berg 
82634e89507SJohannes Berg 	might_sleep();
82734e89507SJohannes Berg 
82834e89507SJohannes Berg 	if (!sta)
82934e89507SJohannes Berg 		return -ENOENT;
83034e89507SJohannes Berg 
83134e89507SJohannes Berg 	local = sta->local;
83234e89507SJohannes Berg 	sdata = sta->sdata;
83334e89507SJohannes Berg 
83483d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
83583d5cc01SJohannes Berg 
836098a6070SJohannes Berg 	/*
837098a6070SJohannes Berg 	 * Before removing the station from the driver and
838098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
839098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
840098a6070SJohannes Berg 	 * will be sufficient.
841098a6070SJohannes Berg 	 */
842c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
843c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
844098a6070SJohannes Berg 
84534e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
846b01711beSJohannes Berg 	if (WARN_ON(ret))
84734e89507SJohannes Berg 		return ret;
84834e89507SJohannes Berg 
849794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8504d33960bSJohannes Berg 
8516a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8526a9d1b91SJohannes Berg 
853a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
854a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
855a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
856a710c816SJohannes Berg 
857d778207bSJohannes Berg 	return 0;
858d778207bSJohannes Berg }
859d778207bSJohannes Berg 
860d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
861d778207bSJohannes Berg {
862d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
863d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
864d778207bSJohannes Berg 	int ret;
865d778207bSJohannes Berg 
866d778207bSJohannes Berg 	/*
867d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
868d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
869d778207bSJohannes Berg 	 */
870d778207bSJohannes Berg 
871d778207bSJohannes Berg 	might_sleep();
872d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
873d778207bSJohannes Berg 
874c8782078SJohannes Berg 	/* now keys can no longer be reached */
8756d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
87634e89507SJohannes Berg 
87734e89507SJohannes Berg 	sta->dead = true;
87834e89507SJohannes Berg 
87934e89507SJohannes Berg 	local->num_sta--;
88034e89507SJohannes Berg 	local->sta_generation++;
88134e89507SJohannes Berg 
88283d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
883f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
884f09603a2SJohannes Berg 		if (ret) {
88583d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
88683d5cc01SJohannes Berg 			break;
88783d5cc01SJohannes Berg 		}
88883d5cc01SJohannes Berg 	}
889d9a7ddb0SJohannes Berg 
890f09603a2SJohannes Berg 	if (sta->uploaded) {
891f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
892f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
893f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
894f09603a2SJohannes Berg 	}
89534e89507SJohannes Berg 
896bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
897bdcbd8e0SJohannes Berg 
898ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
899ec15e68bSJouni Malinen 
90034e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
90134e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
90221f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
90334e89507SJohannes Berg 
904d34ba216SJohannes Berg 	cleanup_single_sta(sta);
905d778207bSJohannes Berg }
906d778207bSJohannes Berg 
907d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
908d778207bSJohannes Berg {
909d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
910d778207bSJohannes Berg 
911d778207bSJohannes Berg 	if (err)
912d778207bSJohannes Berg 		return err;
913d778207bSJohannes Berg 
914d778207bSJohannes Berg 	synchronize_net();
915d778207bSJohannes Berg 
916d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
91734e89507SJohannes Berg 
91834e89507SJohannes Berg 	return 0;
91934e89507SJohannes Berg }
92034e89507SJohannes Berg 
92134e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
92234e89507SJohannes Berg {
92334e89507SJohannes Berg 	struct sta_info *sta;
92434e89507SJohannes Berg 	int ret;
92534e89507SJohannes Berg 
92634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9277852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
92834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
92934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
93034e89507SJohannes Berg 
93134e89507SJohannes Berg 	return ret;
93234e89507SJohannes Berg }
93334e89507SJohannes Berg 
93434e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
93534e89507SJohannes Berg 			      const u8 *addr)
93634e89507SJohannes Berg {
93734e89507SJohannes Berg 	struct sta_info *sta;
93834e89507SJohannes Berg 	int ret;
93934e89507SJohannes Berg 
94034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9417852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
94234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
94334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
94434e89507SJohannes Berg 
94534e89507SJohannes Berg 	return ret;
94634e89507SJohannes Berg }
947f0706e82SJiri Benc 
948f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
949f0706e82SJiri Benc {
950f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
951f0706e82SJiri Benc 	struct sta_info *sta;
9523393a608SJuuso Oikarinen 	bool timer_needed = false;
953f0706e82SJiri Benc 
954d0709a65SJohannes Berg 	rcu_read_lock();
955d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9563393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9573393a608SJuuso Oikarinen 			timer_needed = true;
958d0709a65SJohannes Berg 	rcu_read_unlock();
959f0706e82SJiri Benc 
9605bb644a0SJohannes Berg 	if (local->quiescing)
9615bb644a0SJohannes Berg 		return;
9625bb644a0SJohannes Berg 
9633393a608SJuuso Oikarinen 	if (!timer_needed)
9643393a608SJuuso Oikarinen 		return;
9653393a608SJuuso Oikarinen 
96626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
96726d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
968f0706e82SJiri Benc }
969f0706e82SJiri Benc 
970f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
971f0706e82SJiri Benc {
9724d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
97334e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
974f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
975f0706e82SJiri Benc 
976b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
977b24b8a24SPavel Emelyanov 		    (unsigned long)local);
978f0706e82SJiri Benc }
979f0706e82SJiri Benc 
980f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
981f0706e82SJiri Benc {
982a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
983f0706e82SJiri Benc }
984f0706e82SJiri Benc 
985051007d9SJohannes Berg 
986e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
987f0706e82SJiri Benc {
988b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
989f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
990d778207bSJohannes Berg 	LIST_HEAD(free_list);
99144213b5eSJohannes Berg 	int ret = 0;
992f0706e82SJiri Benc 
993d0709a65SJohannes Berg 	might_sleep();
994d0709a65SJohannes Berg 
995e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
996e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
997e716251dSJohannes Berg 
99834e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
99934e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1000e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1001e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1002d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1003d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
100434316837SJohannes Berg 			ret++;
100534316837SJohannes Berg 		}
100634e89507SJohannes Berg 	}
1007d778207bSJohannes Berg 
1008d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1009d778207bSJohannes Berg 		synchronize_net();
1010d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1011d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1012d778207bSJohannes Berg 	}
101334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
101444213b5eSJohannes Berg 
1015051007d9SJohannes Berg 	return ret;
1016051007d9SJohannes Berg }
1017051007d9SJohannes Berg 
101824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
101924723d1bSJohannes Berg 			  unsigned long exp_time)
102024723d1bSJohannes Berg {
102124723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
102224723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
102324723d1bSJohannes Berg 
102434e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1025e46a2cf9SMohammed Shafi Shajakhan 
1026e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1027ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1028ec2b774eSMarek Lindner 			continue;
1029ec2b774eSMarek Lindner 
103024723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1031eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1032bdcbd8e0SJohannes Berg 				sta->sta.addr);
10333f52b7e3SMarco Porsch 
10343f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10353f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10363f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10373f52b7e3SMarco Porsch 
103834e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
103924723d1bSJohannes Berg 		}
1040e46a2cf9SMohammed Shafi Shajakhan 	}
1041e46a2cf9SMohammed Shafi Shajakhan 
104234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
104324723d1bSJohannes Berg }
104417741cdcSJohannes Berg 
1045686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1046686b9cb9SBen Greear 					       const u8 *addr,
1047686b9cb9SBen Greear 					       const u8 *localaddr)
104817741cdcSJohannes Berg {
1049abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
105017741cdcSJohannes Berg 
1051686b9cb9SBen Greear 	/*
1052686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1053686b9cb9SBen Greear 	 * ... first in list.
1054686b9cb9SBen Greear 	 */
1055f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1056686b9cb9SBen Greear 		if (localaddr &&
1057b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1058686b9cb9SBen Greear 			continue;
1059f7c65594SJohannes Berg 		if (!sta->uploaded)
1060f7c65594SJohannes Berg 			return NULL;
106117741cdcSJohannes Berg 		return &sta->sta;
1062f7c65594SJohannes Berg 	}
1063f7c65594SJohannes Berg 
1064abe60632SJohannes Berg 	return NULL;
106517741cdcSJohannes Berg }
1066686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10675ed176e1SJohannes Berg 
10685ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10695ed176e1SJohannes Berg 					 const u8 *addr)
10705ed176e1SJohannes Berg {
1071f7c65594SJohannes Berg 	struct sta_info *sta;
10725ed176e1SJohannes Berg 
10735ed176e1SJohannes Berg 	if (!vif)
10745ed176e1SJohannes Berg 		return NULL;
10755ed176e1SJohannes Berg 
1076f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1077f7c65594SJohannes Berg 	if (!sta)
1078f7c65594SJohannes Berg 		return NULL;
10795ed176e1SJohannes Berg 
1080f7c65594SJohannes Berg 	if (!sta->uploaded)
1081f7c65594SJohannes Berg 		return NULL;
1082f7c65594SJohannes Berg 
1083f7c65594SJohannes Berg 	return &sta->sta;
10845ed176e1SJohannes Berg }
108517741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1086af818581SJohannes Berg 
1087e3685e03SJohannes Berg /* powersave support code */
1088e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
108950a9432dSJohannes Berg {
1090608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1091e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1092e3685e03SJohannes Berg 	struct sk_buff_head pending;
1093e3685e03SJohannes Berg 	int filtered = 0, buffered = 0, ac;
1094e3685e03SJohannes Berg 	unsigned long flags;
1095d012a605SMarco Porsch 	struct ps_data *ps;
1096d012a605SMarco Porsch 
1097d012a605SMarco Porsch 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
1098d012a605SMarco Porsch 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1099d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11003f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11013f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1102d012a605SMarco Porsch 	else
1103d012a605SMarco Porsch 		return;
110450a9432dSJohannes Berg 
1105c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
110647086fc5SJohannes Berg 
11075a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1108948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1109948d887dSJohannes Berg 
1110d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
111112375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1112af818581SJohannes Berg 
1113948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1114af818581SJohannes Berg 
11151d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11161d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1117af818581SJohannes Berg 	/* Send all buffered frames to the station */
1118948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1119948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1120948d887dSJohannes Berg 
1121987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1122948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1123987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1124948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1125948d887dSJohannes Berg 		filtered += tmp - count;
1126948d887dSJohannes Berg 		count = tmp;
1127948d887dSJohannes Berg 
1128987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1129948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1130987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1131948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1132948d887dSJohannes Berg 		buffered += tmp - count;
1133948d887dSJohannes Berg 	}
1134948d887dSJohannes Berg 
1135e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11365ac2e350SJohannes Berg 
11375ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11385ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11395ac2e350SJohannes Berg 
11405ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
11415ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
11425ac2e350SJohannes Berg 	 */
11435ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
11445ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
11451d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1146948d887dSJohannes Berg 
1147e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1148e3685e03SJohannes Berg 
1149687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1150062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1151062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1152687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1153687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1154687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1155687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1156687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1157687da132SEmmanuel Grumbach 		       sta->sta.addr);
1158687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1159687da132SEmmanuel Grumbach 					   sta->sta.addr,
1160687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1161687da132SEmmanuel Grumbach 	}
1162687da132SEmmanuel Grumbach 
1163af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1164af818581SJohannes Berg 
1165c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1166c868cb35SJohannes Berg 
1167bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1168bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1169bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1170af818581SJohannes Berg }
1171af818581SJohannes Berg 
1172ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1173ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1174b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1175b77cf4f8SJohannes Berg 					 bool call_driver)
1176ce662b44SJohannes Berg {
1177ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1178ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1179ce662b44SJohannes Berg 	struct sk_buff *skb;
1180ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1181ce662b44SJohannes Berg 	__le16 fc;
1182c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1183ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
118455de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1185ce662b44SJohannes Berg 
1186ce662b44SJohannes Berg 	if (qos) {
1187ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1188ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1189ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1190ce662b44SJohannes Berg 	} else {
1191ce662b44SJohannes Berg 		size -= 2;
1192ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1193ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1194ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1195ce662b44SJohannes Berg 	}
1196ce662b44SJohannes Berg 
1197ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1198ce662b44SJohannes Berg 	if (!skb)
1199ce662b44SJohannes Berg 		return;
1200ce662b44SJohannes Berg 
1201ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1202ce662b44SJohannes Berg 
1203ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1204ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1205ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1206ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1207ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1208ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1209864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1210ce662b44SJohannes Berg 
1211ce662b44SJohannes Berg 	skb->priority = tid;
1212ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
121359b66255SJohannes Berg 	if (qos) {
1214ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1215ce662b44SJohannes Berg 
121640b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1217ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1218ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1219ce662b44SJohannes Berg 	}
1220ce662b44SJohannes Berg 
1221ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1222ce662b44SJohannes Berg 
1223ce662b44SJohannes Berg 	/*
1224ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1225ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1226deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1227deeaee19SJohannes Berg 	 * ends the poll/service period.
1228ce662b44SJohannes Berg 	 */
122902f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1230d6d23de2SFelix Fietkau 		       IEEE80211_TX_CTL_PS_RESPONSE |
1231deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1232ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1233ce662b44SJohannes Berg 
1234b77cf4f8SJohannes Berg 	if (call_driver)
1235b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1236b77cf4f8SJohannes Berg 					  reason, false);
123740b96408SJohannes Berg 
123889afe614SJohannes Berg 	skb->dev = sdata->dev;
123989afe614SJohannes Berg 
124055de908aSJohannes Berg 	rcu_read_lock();
124155de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
124255de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
124355de908aSJohannes Berg 		rcu_read_unlock();
124455de908aSJohannes Berg 		kfree_skb(skb);
124555de908aSJohannes Berg 		return;
124655de908aSJohannes Berg 	}
124755de908aSJohannes Berg 
12484bf88530SJohannes Berg 	ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
124955de908aSJohannes Berg 	rcu_read_unlock();
1250ce662b44SJohannes Berg }
1251ce662b44SJohannes Berg 
12520a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
12530a1cb809SJohannes Berg {
12540a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
12550a1cb809SJohannes Berg 	if (tids & 0xF8)
12560a1cb809SJohannes Berg 		return fls(tids) - 1;
12570a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
12580a1cb809SJohannes Berg 	if (tids & BIT(0))
12590a1cb809SJohannes Berg 		return 0;
12600a1cb809SJohannes Berg 	return fls(tids) - 1;
12610a1cb809SJohannes Berg }
12620a1cb809SJohannes Berg 
126347086fc5SJohannes Berg static void
126447086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
126547086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
126647086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1267af818581SJohannes Berg {
1268af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1269af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1270948d887dSJohannes Berg 	bool more_data = false;
1271948d887dSJohannes Berg 	int ac;
12724049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
127347086fc5SJohannes Berg 	struct sk_buff_head frames;
127447086fc5SJohannes Berg 
1275deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1276c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1277deeaee19SJohannes Berg 
127847086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1279af818581SJohannes Berg 
1280f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1281948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
12824049e09aSJohannes Berg 		unsigned long tids;
12834049e09aSJohannes Berg 
128447086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1285948d887dSJohannes Berg 			continue;
1286948d887dSJohannes Berg 
12874049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
12884049e09aSJohannes Berg 
1289f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1290f9f760b4SJohannes Berg 		 * release from hardware queues
1291f9f760b4SJohannes Berg 		 */
1292f9f760b4SJohannes Berg 		if (skb_queue_empty(&frames))
1293f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1294f9f760b4SJohannes Berg 
12954049e09aSJohannes Berg 		if (driver_release_tids) {
1296f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1297f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1298f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1299f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1300f9f760b4SJohannes Berg 			 */
1301f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1302f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1303f9f760b4SJohannes Berg 				more_data = true;
1304f9f760b4SJohannes Berg 				driver_release_tids =
1305f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1306f9f760b4SJohannes Berg 						driver_release_tids));
1307f9f760b4SJohannes Berg 				break;
1308f9f760b4SJohannes Berg 			}
13094049e09aSJohannes Berg 		} else {
131047086fc5SJohannes Berg 			struct sk_buff *skb;
131147086fc5SJohannes Berg 
131247086fc5SJohannes Berg 			while (n_frames > 0) {
1313948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1314948d887dSJohannes Berg 				if (!skb) {
131547086fc5SJohannes Berg 					skb = skb_dequeue(
131647086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1317af818581SJohannes Berg 					if (skb)
1318af818581SJohannes Berg 						local->total_ps_buffered--;
1319af818581SJohannes Berg 				}
132047086fc5SJohannes Berg 				if (!skb)
132147086fc5SJohannes Berg 					break;
132247086fc5SJohannes Berg 				n_frames--;
132347086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
132447086fc5SJohannes Berg 			}
1325948d887dSJohannes Berg 		}
1326948d887dSJohannes Berg 
1327f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1328f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1329f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13304049e09aSJohannes Berg 		 */
1331948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1332948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1333948d887dSJohannes Berg 			more_data = true;
1334948d887dSJohannes Berg 			break;
1335948d887dSJohannes Berg 		}
1336948d887dSJohannes Berg 	}
1337af818581SJohannes Berg 
1338f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1339ce662b44SJohannes Berg 		int tid;
13404049e09aSJohannes Berg 
1341ce662b44SJohannes Berg 		/*
1342ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1343ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1344ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1345ce662b44SJohannes Berg 		 *
1346ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1347ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1348ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1349ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1350ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1351ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1352ce662b44SJohannes Berg 		 *
1353ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1354ce662b44SJohannes Berg 		 */
1355ce662b44SJohannes Berg 
1356ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1357ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1358ce662b44SJohannes Berg 
1359b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1360f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
136147086fc5SJohannes Berg 		struct sk_buff_head pending;
136247086fc5SJohannes Berg 		struct sk_buff *skb;
136340b96408SJohannes Berg 		int num = 0;
136440b96408SJohannes Berg 		u16 tids = 0;
1365b77cf4f8SJohannes Berg 		bool need_null = false;
136647086fc5SJohannes Berg 
136747086fc5SJohannes Berg 		skb_queue_head_init(&pending);
136847086fc5SJohannes Berg 
136947086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1370af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
137147086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
137240b96408SJohannes Berg 			u8 *qoshdr = NULL;
137340b96408SJohannes Berg 
137440b96408SJohannes Berg 			num++;
1375af818581SJohannes Berg 
1376af818581SJohannes Berg 			/*
137747086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
137847086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
137947086fc5SJohannes Berg 			 * exchange.
1380af818581SJohannes Berg 			 */
1381d6d23de2SFelix Fietkau 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1382d6d23de2SFelix Fietkau 				       IEEE80211_TX_CTL_PS_RESPONSE;
1383af818581SJohannes Berg 
138447086fc5SJohannes Berg 			/*
138547086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
138647086fc5SJohannes Berg 			 * more buffered frames for this STA
138747086fc5SJohannes Berg 			 */
138824b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
138947086fc5SJohannes Berg 				hdr->frame_control |=
139047086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
139124b9c373SJanusz.Dziedzic@tieto.com 			else
139224b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
139324b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1394af818581SJohannes Berg 
139540b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
139640b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
139740b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
139840b96408SJohannes Berg 
1399b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1400b77cf4f8SJohannes Berg 
1401b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1402b77cf4f8SJohannes Berg 
1403b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1404b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1405b77cf4f8SJohannes Berg 				continue;
1406b77cf4f8SJohannes Berg 
1407b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1408b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1409b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1410b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1411b77cf4f8SJohannes Berg 				break;
1412b77cf4f8SJohannes Berg 			}
1413b77cf4f8SJohannes Berg 
1414b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1415b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1416b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1417b77cf4f8SJohannes Berg 			 * and be done.
1418b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1419b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1420b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1421b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1422b77cf4f8SJohannes Berg 			 *
1423b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1424b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1425b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1426b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1427b77cf4f8SJohannes Berg 			 *
1428b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1429b77cf4f8SJohannes Berg 			 */
1430b77cf4f8SJohannes Berg 			if (qoshdr) {
143140b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1432deeaee19SJohannes Berg 
143347086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
143447086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1435b77cf4f8SJohannes Berg 			} else {
1436b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1437b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1438b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1439b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1440b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1441b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1442b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1443b77cf4f8SJohannes Berg 				 */
1444b77cf4f8SJohannes Berg 				hdr->frame_control |=
1445b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1446b77cf4f8SJohannes Berg 				need_null = true;
1447b77cf4f8SJohannes Berg 				num++;
144852a3f20cSMarco Porsch 			}
1449b77cf4f8SJohannes Berg 			break;
145047086fc5SJohannes Berg 		}
145147086fc5SJohannes Berg 
145240b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
145340b96408SJohannes Berg 					  reason, more_data);
145440b96408SJohannes Berg 
145547086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1456af818581SJohannes Berg 
1457b77cf4f8SJohannes Berg 		if (need_null)
1458b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1459b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1460b77cf4f8SJohannes Berg 				reason, false);
1461b77cf4f8SJohannes Berg 
1462c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1463af818581SJohannes Berg 	} else {
1464af818581SJohannes Berg 		/*
14654049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
14664049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1467f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1468f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1469f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1470f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1471f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1472f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1473af818581SJohannes Berg 		 */
14744049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
147547086fc5SJohannes Berg 					    n_frames, reason, more_data);
14764049e09aSJohannes Berg 
14774049e09aSJohannes Berg 		/*
14784049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
14794049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1480f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
14814049e09aSJohannes Berg 		 * release function.
1482f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
14834049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
14844049e09aSJohannes Berg 		 */
1485af818581SJohannes Berg 	}
1486af818581SJohannes Berg }
1487af818581SJohannes Berg 
1488af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1489af818581SJohannes Berg {
149047086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1491af818581SJohannes Berg 
1492af818581SJohannes Berg 	/*
149347086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
149447086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
149547086fc5SJohannes Berg 	 * only from the non-enabled ones.
1496af818581SJohannes Berg 	 */
149747086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
149847086fc5SJohannes Berg 		ignore_for_response = 0;
1499af818581SJohannes Berg 
150047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
150147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1502af818581SJohannes Berg }
150347086fc5SJohannes Berg 
150447086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
150547086fc5SJohannes Berg {
150647086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
150747086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
150847086fc5SJohannes Berg 
150947086fc5SJohannes Berg 	/*
151047086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
151147086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
151247086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
151347086fc5SJohannes Berg 	 * actually getting called.
151447086fc5SJohannes Berg 	 */
151547086fc5SJohannes Berg 	if (!delivery_enabled)
151647086fc5SJohannes Berg 		return;
151747086fc5SJohannes Berg 
151847086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
151947086fc5SJohannes Berg 	case 1:
152047086fc5SJohannes Berg 		n_frames = 2;
152147086fc5SJohannes Berg 		break;
152247086fc5SJohannes Berg 	case 2:
152347086fc5SJohannes Berg 		n_frames = 4;
152447086fc5SJohannes Berg 		break;
152547086fc5SJohannes Berg 	case 3:
152647086fc5SJohannes Berg 		n_frames = 6;
152747086fc5SJohannes Berg 		break;
152847086fc5SJohannes Berg 	case 0:
152947086fc5SJohannes Berg 		/* XXX: what is a good value? */
153047086fc5SJohannes Berg 		n_frames = 8;
153147086fc5SJohannes Berg 		break;
153247086fc5SJohannes Berg 	}
153347086fc5SJohannes Berg 
153447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
153547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1536af818581SJohannes Berg }
1537af818581SJohannes Berg 
1538af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1539af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1540af818581SJohannes Berg {
1541af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1542af818581SJohannes Berg 
1543b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1544b5878a2dSJohannes Berg 
15455ac2e350SJohannes Berg 	if (block) {
1546c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
15475ac2e350SJohannes Berg 		return;
15485ac2e350SJohannes Berg 	}
15495ac2e350SJohannes Berg 
15505ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
15515ac2e350SJohannes Berg 		return;
15525ac2e350SJohannes Berg 
15535ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
15545ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
15555ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15565ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15575ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
15585ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
15595ac2e350SJohannes Berg 		/* must be asleep in this case */
15605ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15615ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15625ac2e350SJohannes Berg 	} else {
15635ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15645ac2e350SJohannes Berg 	}
1565af818581SJohannes Berg }
1566af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1567dcf55fb5SFelix Fietkau 
1568e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
156937fbd908SJohannes Berg {
157037fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
157137fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
157237fbd908SJohannes Berg 
157337fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
157437fbd908SJohannes Berg 
157537fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
157637fbd908SJohannes Berg }
1577e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
157837fbd908SJohannes Berg 
1579042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1580042ec453SJohannes Berg 				u8 tid, bool buffered)
1581dcf55fb5SFelix Fietkau {
1582dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1583dcf55fb5SFelix Fietkau 
15845a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1585042ec453SJohannes Berg 		return;
1586042ec453SJohannes Berg 
15871b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
15881b000789SJohannes Berg 
1589948d887dSJohannes Berg 	if (buffered)
1590948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1591948d887dSJohannes Berg 	else
1592948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1593948d887dSJohannes Berg 
1594c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1595dcf55fb5SFelix Fietkau }
1596042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1597d9a7ddb0SJohannes Berg 
159883d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1599d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1600d9a7ddb0SJohannes Berg {
16018bf11d8dSJohannes Berg 	might_sleep();
1602d9a7ddb0SJohannes Berg 
1603d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1604d9a7ddb0SJohannes Berg 		return 0;
1605d9a7ddb0SJohannes Berg 
1606f09603a2SJohannes Berg 	/* check allowed transitions first */
1607f09603a2SJohannes Berg 
1608d9a7ddb0SJohannes Berg 	switch (new_state) {
1609d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1610f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1611d9a7ddb0SJohannes Berg 			return -EINVAL;
1612d9a7ddb0SJohannes Berg 		break;
1613d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1614f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1615f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1616d9a7ddb0SJohannes Berg 			return -EINVAL;
1617d9a7ddb0SJohannes Berg 		break;
1618d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1619f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1620f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1621d9a7ddb0SJohannes Berg 			return -EINVAL;
1622d9a7ddb0SJohannes Berg 		break;
1623d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1624f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1625d9a7ddb0SJohannes Berg 			return -EINVAL;
1626d9a7ddb0SJohannes Berg 		break;
1627d9a7ddb0SJohannes Berg 	default:
1628d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1629d9a7ddb0SJohannes Berg 		return -EINVAL;
1630d9a7ddb0SJohannes Berg 	}
1631d9a7ddb0SJohannes Berg 
1632bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1633bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1634f09603a2SJohannes Berg 
1635f09603a2SJohannes Berg 	/*
1636f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1637f09603a2SJohannes Berg 	 * fail the transition
1638f09603a2SJohannes Berg 	 */
1639f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1640f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1641f09603a2SJohannes Berg 					sta->sta_state, new_state);
1642f09603a2SJohannes Berg 		if (err)
1643f09603a2SJohannes Berg 			return err;
1644f09603a2SJohannes Berg 	}
1645f09603a2SJohannes Berg 
1646f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1647f09603a2SJohannes Berg 
1648f09603a2SJohannes Berg 	switch (new_state) {
1649f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1650f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1651f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1652f09603a2SJohannes Berg 		break;
1653f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1654f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1655f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1656f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1657f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1658f09603a2SJohannes Berg 		break;
1659f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1660f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1661f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1662f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
16637e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16647e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16657e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16667e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1667f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1668f09603a2SJohannes Berg 		}
1669f09603a2SJohannes Berg 		break;
1670f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1671f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
16727e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16737e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16747e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16757e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1676f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1677f09603a2SJohannes Berg 		}
1678f09603a2SJohannes Berg 		break;
1679f09603a2SJohannes Berg 	default:
1680f09603a2SJohannes Berg 		break;
1681f09603a2SJohannes Berg 	}
1682f09603a2SJohannes Berg 
1683d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1684d9a7ddb0SJohannes Berg 
1685d9a7ddb0SJohannes Berg 	return 0;
1686d9a7ddb0SJohannes Berg }
1687687da132SEmmanuel Grumbach 
1688687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1689687da132SEmmanuel Grumbach {
1690687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1691687da132SEmmanuel Grumbach 	u8 rx_streams;
1692687da132SEmmanuel Grumbach 
1693687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1694687da132SEmmanuel Grumbach 		return 1;
1695687da132SEmmanuel Grumbach 
1696687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1697687da132SEmmanuel Grumbach 		int i;
1698687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1699687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1700687da132SEmmanuel Grumbach 
1701687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1702687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1703687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1704687da132SEmmanuel Grumbach 				return i + 1;
1705687da132SEmmanuel Grumbach 	}
1706687da132SEmmanuel Grumbach 
1707687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1708687da132SEmmanuel Grumbach 		rx_streams = 4;
1709687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1710687da132SEmmanuel Grumbach 		rx_streams = 3;
1711687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1712687da132SEmmanuel Grumbach 		rx_streams = 2;
1713687da132SEmmanuel Grumbach 	else
1714687da132SEmmanuel Grumbach 		rx_streams = 1;
1715687da132SEmmanuel Grumbach 
1716687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1717687da132SEmmanuel Grumbach 		return rx_streams;
1718687da132SEmmanuel Grumbach 
1719687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1720687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1721687da132SEmmanuel Grumbach }
1722*b7ffbd7eSJohannes Berg 
1723*b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1724*b7ffbd7eSJohannes Berg {
1725*b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1726*b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1727*b7ffbd7eSJohannes Berg 	struct rate_control_ref *ref = local->rate_ctrl;
1728*b7ffbd7eSJohannes Berg 	struct timespec uptime;
1729*b7ffbd7eSJohannes Berg 	u64 packets = 0;
1730*b7ffbd7eSJohannes Berg 	u32 thr = 0;
1731*b7ffbd7eSJohannes Berg 	int i, ac;
1732*b7ffbd7eSJohannes Berg 
1733*b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1734*b7ffbd7eSJohannes Berg 
1735*b7ffbd7eSJohannes Berg 	sinfo->filled = STATION_INFO_INACTIVE_TIME |
1736*b7ffbd7eSJohannes Berg 			STATION_INFO_RX_BYTES64 |
1737*b7ffbd7eSJohannes Berg 			STATION_INFO_TX_BYTES64 |
1738*b7ffbd7eSJohannes Berg 			STATION_INFO_RX_PACKETS |
1739*b7ffbd7eSJohannes Berg 			STATION_INFO_TX_PACKETS |
1740*b7ffbd7eSJohannes Berg 			STATION_INFO_TX_RETRIES |
1741*b7ffbd7eSJohannes Berg 			STATION_INFO_TX_FAILED |
1742*b7ffbd7eSJohannes Berg 			STATION_INFO_TX_BITRATE |
1743*b7ffbd7eSJohannes Berg 			STATION_INFO_RX_BITRATE |
1744*b7ffbd7eSJohannes Berg 			STATION_INFO_RX_DROP_MISC |
1745*b7ffbd7eSJohannes Berg 			STATION_INFO_BSS_PARAM |
1746*b7ffbd7eSJohannes Berg 			STATION_INFO_CONNECTED_TIME |
1747*b7ffbd7eSJohannes Berg 			STATION_INFO_STA_FLAGS |
1748*b7ffbd7eSJohannes Berg 			STATION_INFO_BEACON_LOSS_COUNT;
1749*b7ffbd7eSJohannes Berg 
1750*b7ffbd7eSJohannes Berg 	do_posix_clock_monotonic_gettime(&uptime);
1751*b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1752*b7ffbd7eSJohannes Berg 
1753*b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
1754*b7ffbd7eSJohannes Berg 	sinfo->tx_bytes = 0;
1755*b7ffbd7eSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1756*b7ffbd7eSJohannes Berg 		sinfo->tx_bytes += sta->tx_bytes[ac];
1757*b7ffbd7eSJohannes Berg 		packets += sta->tx_packets[ac];
1758*b7ffbd7eSJohannes Berg 	}
1759*b7ffbd7eSJohannes Berg 	sinfo->tx_packets = packets;
1760*b7ffbd7eSJohannes Berg 	sinfo->rx_bytes = sta->rx_bytes;
1761*b7ffbd7eSJohannes Berg 	sinfo->rx_packets = sta->rx_packets;
1762*b7ffbd7eSJohannes Berg 	sinfo->tx_retries = sta->tx_retry_count;
1763*b7ffbd7eSJohannes Berg 	sinfo->tx_failed = sta->tx_retry_failed;
1764*b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1765*b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1766*b7ffbd7eSJohannes Berg 
1767*b7ffbd7eSJohannes Berg 	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1768*b7ffbd7eSJohannes Berg 	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1769*b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
1770*b7ffbd7eSJohannes Berg 		if (!local->ops->get_rssi ||
1771*b7ffbd7eSJohannes Berg 		    drv_get_rssi(local, sdata, &sta->sta, &sinfo->signal))
1772*b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1773*b7ffbd7eSJohannes Berg 		sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1774*b7ffbd7eSJohannes Berg 	}
1775*b7ffbd7eSJohannes Berg 	if (sta->chains) {
1776*b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_CHAIN_SIGNAL |
1777*b7ffbd7eSJohannes Berg 				 STATION_INFO_CHAIN_SIGNAL_AVG;
1778*b7ffbd7eSJohannes Berg 
1779*b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1780*b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1781*b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1782*b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1783*b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1784*b7ffbd7eSJohannes Berg 		}
1785*b7ffbd7eSJohannes Berg 	}
1786*b7ffbd7eSJohannes Berg 
1787*b7ffbd7eSJohannes Berg 	sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1788*b7ffbd7eSJohannes Berg 	sta_set_rate_info_rx(sta, &sinfo->rxrate);
1789*b7ffbd7eSJohannes Berg 
1790*b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1791*b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1792*b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_LLID |
1793*b7ffbd7eSJohannes Berg 				 STATION_INFO_PLID |
1794*b7ffbd7eSJohannes Berg 				 STATION_INFO_PLINK_STATE |
1795*b7ffbd7eSJohannes Berg 				 STATION_INFO_LOCAL_PM |
1796*b7ffbd7eSJohannes Berg 				 STATION_INFO_PEER_PM |
1797*b7ffbd7eSJohannes Berg 				 STATION_INFO_NONPEER_PM;
1798*b7ffbd7eSJohannes Berg 
1799*b7ffbd7eSJohannes Berg 		sinfo->llid = sta->llid;
1800*b7ffbd7eSJohannes Berg 		sinfo->plid = sta->plid;
1801*b7ffbd7eSJohannes Berg 		sinfo->plink_state = sta->plink_state;
1802*b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1803*b7ffbd7eSJohannes Berg 			sinfo->filled |= STATION_INFO_T_OFFSET;
1804*b7ffbd7eSJohannes Berg 			sinfo->t_offset = sta->t_offset;
1805*b7ffbd7eSJohannes Berg 		}
1806*b7ffbd7eSJohannes Berg 		sinfo->local_pm = sta->local_pm;
1807*b7ffbd7eSJohannes Berg 		sinfo->peer_pm = sta->peer_pm;
1808*b7ffbd7eSJohannes Berg 		sinfo->nonpeer_pm = sta->nonpeer_pm;
1809*b7ffbd7eSJohannes Berg #endif
1810*b7ffbd7eSJohannes Berg 	}
1811*b7ffbd7eSJohannes Berg 
1812*b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1813*b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1814*b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1815*b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1816*b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1817*b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1818*b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1819*b7ffbd7eSJohannes Berg 	sinfo->bss_param.dtim_period = sdata->local->hw.conf.ps_dtim_period;
1820*b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1821*b7ffbd7eSJohannes Berg 
1822*b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1823*b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1824*b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1825*b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1826*b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1827*b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1828*b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1829*b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1830*b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1831*b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1832*b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1833*b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1834*b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_WME))
1835*b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1836*b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
1837*b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1838*b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
1839*b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1840*b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
1841*b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1842*b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1843*b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1844*b7ffbd7eSJohannes Berg 
1845*b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
1846*b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
1847*b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1848*b7ffbd7eSJohannes Berg 	else
1849*b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
1850*b7ffbd7eSJohannes Berg 
1851*b7ffbd7eSJohannes Berg 	if (thr != 0) {
1852*b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_EXPECTED_THROUGHPUT;
1853*b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
1854*b7ffbd7eSJohannes Berg 	}
1855*b7ffbd7eSJohannes Berg }
1856