xref: /openbmc/linux/net/mac80211/sta_info.c (revision 2bad7748b332d7218377287248eb66c25de033b2)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
5f0706e82SJiri Benc  *
6f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
7f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
8f0706e82SJiri Benc  * published by the Free Software Foundation.
9f0706e82SJiri Benc  */
10f0706e82SJiri Benc 
11f0706e82SJiri Benc #include <linux/module.h>
12f0706e82SJiri Benc #include <linux/init.h>
13888d04dfSFelix Fietkau #include <linux/etherdevice.h>
14f0706e82SJiri Benc #include <linux/netdevice.h>
15f0706e82SJiri Benc #include <linux/types.h>
16f0706e82SJiri Benc #include <linux/slab.h>
17f0706e82SJiri Benc #include <linux/skbuff.h>
18f0706e82SJiri Benc #include <linux/if_arp.h>
190d174406SJohannes Berg #include <linux/timer.h>
20d0709a65SJohannes Berg #include <linux/rtnetlink.h>
21f0706e82SJiri Benc 
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
674d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
68be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
69f0706e82SJiri Benc 			     struct sta_info *sta)
70f0706e82SJiri Benc {
71f0706e82SJiri Benc 	struct sta_info *s;
72f0706e82SJiri Benc 
7340b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
744d33960bSJohannes Berg 				      lockdep_is_held(&local->sta_mtx));
75f0706e82SJiri Benc 	if (!s)
76be8755e1SMichael Wu 		return -ENOENT;
77be8755e1SMichael Wu 	if (s == sta) {
78cf778b00SEric Dumazet 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
79d0709a65SJohannes Berg 				   s->hnext);
80be8755e1SMichael Wu 		return 0;
81f0706e82SJiri Benc 	}
82f0706e82SJiri Benc 
8340b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8440b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8540b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
864d33960bSJohannes Berg 					lockdep_is_held(&local->sta_mtx));
8740b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
88cf778b00SEric Dumazet 		rcu_assign_pointer(s->hnext, sta->hnext);
89be8755e1SMichael Wu 		return 0;
90f0706e82SJiri Benc 	}
91f0706e82SJiri Benc 
92be8755e1SMichael Wu 	return -ENOENT;
93f0706e82SJiri Benc }
94f0706e82SJiri Benc 
955108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
96b22cfcfcSEliad Peller {
97b22cfcfcSEliad Peller 	int ac, i;
98b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
99b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
100b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
101d012a605SMarco Porsch 	struct ps_data *ps;
102b22cfcfcSEliad Peller 
103e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1045ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1055ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
106d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
107d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
108d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1093f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1103f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
111d012a605SMarco Porsch 		else
112d012a605SMarco Porsch 			return;
113b22cfcfcSEliad Peller 
114b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
115e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1165ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
117b22cfcfcSEliad Peller 
118d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
119b22cfcfcSEliad Peller 		sta_info_recalc_tim(sta);
120b22cfcfcSEliad Peller 	}
121b22cfcfcSEliad Peller 
122b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
123b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1241f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1251f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
126b22cfcfcSEliad Peller 	}
127b22cfcfcSEliad Peller 
12845b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12945b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
130b22cfcfcSEliad Peller 
1315ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
132b22cfcfcSEliad Peller 
133b22cfcfcSEliad Peller 	/*
134b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
135b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
136b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
137b22cfcfcSEliad Peller 	 * directly by station destruction.
138b22cfcfcSEliad Peller 	 */
1395a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
140661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
141b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
142b22cfcfcSEliad Peller 		if (!tid_tx)
143b22cfcfcSEliad Peller 			continue;
1441f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
145b22cfcfcSEliad Peller 		kfree(tid_tx);
146b22cfcfcSEliad Peller 	}
1475108ca82SJohannes Berg }
148b22cfcfcSEliad Peller 
1495108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1505108ca82SJohannes Berg {
1515108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1525108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1535108ca82SJohannes Berg 
1545108ca82SJohannes Berg 	__cleanup_single_sta(sta);
155b22cfcfcSEliad Peller 	sta_info_free(local, sta);
156b22cfcfcSEliad Peller }
157b22cfcfcSEliad Peller 
158d0709a65SJohannes Berg /* protected by RCU */
159abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
160abe60632SJohannes Berg 			      const u8 *addr)
16143ba7e95SJohannes Berg {
162abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16343ba7e95SJohannes Berg 	struct sta_info *sta;
16443ba7e95SJohannes Berg 
1650379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1660379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
16743ba7e95SJohannes Berg 	while (sta) {
168abe60632SJohannes Berg 		if (sta->sdata == sdata &&
169b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
17043ba7e95SJohannes Berg 			break;
1710379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1720379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
17343ba7e95SJohannes Berg 	}
17443ba7e95SJohannes Berg 	return sta;
17543ba7e95SJohannes Berg }
17643ba7e95SJohannes Berg 
1770e5ded5aSFelix Fietkau /*
1780e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1790e5ded5aSFelix Fietkau  * or from one of its vlans
1800e5ded5aSFelix Fietkau  */
1810e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1820e5ded5aSFelix Fietkau 				  const u8 *addr)
1830e5ded5aSFelix Fietkau {
1840e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1850e5ded5aSFelix Fietkau 	struct sta_info *sta;
1860e5ded5aSFelix Fietkau 
1870379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1880379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1890e5ded5aSFelix Fietkau 	while (sta) {
1900e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
191a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
192b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
1930e5ded5aSFelix Fietkau 			break;
1940379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1950379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1960e5ded5aSFelix Fietkau 	}
1970e5ded5aSFelix Fietkau 	return sta;
1980e5ded5aSFelix Fietkau }
1990e5ded5aSFelix Fietkau 
2003b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2013b53fde8SJohannes Berg 				     int idx)
202ee385855SLuis Carlos Cobo {
2033b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
204ee385855SLuis Carlos Cobo 	struct sta_info *sta;
205ee385855SLuis Carlos Cobo 	int i = 0;
206ee385855SLuis Carlos Cobo 
207d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2083b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2092a8ca29aSLuis Carlos Cobo 			continue;
210ee385855SLuis Carlos Cobo 		if (i < idx) {
211ee385855SLuis Carlos Cobo 			++i;
212ee385855SLuis Carlos Cobo 			continue;
213ee385855SLuis Carlos Cobo 		}
2142a8ca29aSLuis Carlos Cobo 		return sta;
215ee385855SLuis Carlos Cobo 	}
216ee385855SLuis Carlos Cobo 
217ee385855SLuis Carlos Cobo 	return NULL;
218ee385855SLuis Carlos Cobo }
219f0706e82SJiri Benc 
22093e5deb1SJohannes Berg /**
221d9a7ddb0SJohannes Berg  * sta_info_free - free STA
22293e5deb1SJohannes Berg  *
2236ef307bcSRandy Dunlap  * @local: pointer to the global information
22493e5deb1SJohannes Berg  * @sta: STA info to free
22593e5deb1SJohannes Berg  *
22693e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
227d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
228d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
229d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
23093e5deb1SJohannes Berg  */
231d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
23293e5deb1SJohannes Berg {
233ad38bfc9SMatti Gottlieb 	int i;
234ad38bfc9SMatti Gottlieb 
235889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2364b7679a5SJohannes Berg 		rate_control_free_sta(sta);
23793e5deb1SJohannes Berg 
238ad38bfc9SMatti Gottlieb 	if (sta->tx_lat) {
239ad38bfc9SMatti Gottlieb 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
240ad38bfc9SMatti Gottlieb 			kfree(sta->tx_lat[i].bins);
241ad38bfc9SMatti Gottlieb 		kfree(sta->tx_lat);
242ad38bfc9SMatti Gottlieb 	}
243ad38bfc9SMatti Gottlieb 
244bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
24593e5deb1SJohannes Berg 
24653d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
24793e5deb1SJohannes Berg 	kfree(sta);
24893e5deb1SJohannes Berg }
24993e5deb1SJohannes Berg 
2504d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
251d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
252d0709a65SJohannes Berg 			      struct sta_info *sta)
253f0706e82SJiri Benc {
2544d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
25517741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
256cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
257f0706e82SJiri Benc }
258f0706e82SJiri Benc 
2595ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
260af818581SJohannes Berg {
261af818581SJohannes Berg 	struct sta_info *sta;
262af818581SJohannes Berg 
2635ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
264af818581SJohannes Berg 
265af818581SJohannes Berg 	if (sta->dead)
266af818581SJohannes Berg 		return;
267af818581SJohannes Berg 
26854420473SHelmut Schaa 	local_bh_disable();
2695ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
270af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
2715ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
272af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
2735ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
27447086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
275ce662b44SJohannes Berg 	local_bh_enable();
276af818581SJohannes Berg }
277af818581SJohannes Berg 
278af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
279af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
280af65cd96SJohannes Berg {
281af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
282af65cd96SJohannes Berg 		return 0;
283af65cd96SJohannes Berg 
284889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
285af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
286af65cd96SJohannes Berg 						     &sta->sta, gfp);
287889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
288af65cd96SJohannes Berg 		return -ENOMEM;
289af65cd96SJohannes Berg 
290af65cd96SJohannes Berg 	return 0;
291af65cd96SJohannes Berg }
292af65cd96SJohannes Berg 
29373651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
29456544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
295f0706e82SJiri Benc {
296d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
297f0706e82SJiri Benc 	struct sta_info *sta;
298ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
299ad38bfc9SMatti Gottlieb 	struct ieee80211_tx_latency_bin_ranges *tx_latency;
30016c5f15cSRon Rindjunsky 	int i;
301f0706e82SJiri Benc 
30217741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
303f0706e82SJiri Benc 	if (!sta)
30473651ee6SJohannes Berg 		return NULL;
305f0706e82SJiri Benc 
306ef04a297SJohannes Berg 	rcu_read_lock();
307ef04a297SJohannes Berg 	tx_latency = rcu_dereference(local->tx_latency);
308ef04a297SJohannes Berg 	/* init stations Tx latency statistics && TID bins */
309ef04a297SJohannes Berg 	if (tx_latency) {
310ef04a297SJohannes Berg 		sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
311ef04a297SJohannes Berg 				      sizeof(struct ieee80211_tx_latency_stat),
312ef04a297SJohannes Berg 				      GFP_ATOMIC);
313ef04a297SJohannes Berg 		if (!sta->tx_lat) {
314ef04a297SJohannes Berg 			rcu_read_unlock();
315ef04a297SJohannes Berg 			goto free;
316ef04a297SJohannes Berg 		}
317ef04a297SJohannes Berg 
318ef04a297SJohannes Berg 		if (tx_latency->n_ranges) {
319ef04a297SJohannes Berg 			for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
320ef04a297SJohannes Berg 				/* size of bins is size of the ranges +1 */
321ef04a297SJohannes Berg 				sta->tx_lat[i].bin_count =
322ef04a297SJohannes Berg 					tx_latency->n_ranges + 1;
323ef04a297SJohannes Berg 				sta->tx_lat[i].bins =
324ef04a297SJohannes Berg 					kcalloc(sta->tx_lat[i].bin_count,
325ef04a297SJohannes Berg 						sizeof(u32), GFP_ATOMIC);
326ef04a297SJohannes Berg 				if (!sta->tx_lat[i].bins) {
327ef04a297SJohannes Berg 					rcu_read_unlock();
328ef04a297SJohannes Berg 					goto free;
329ef04a297SJohannes Berg 				}
330ef04a297SJohannes Berg 			}
331ef04a297SJohannes Berg 		}
332ef04a297SJohannes Berg 	}
333ef04a297SJohannes Berg 	rcu_read_unlock();
334ef04a297SJohannes Berg 
33507346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3361d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
3375ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
33867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
339a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
34087f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
34187f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
34287f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
34387f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3446c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
34587f59c70SThomas Pedersen #endif
34607346f81SJohannes Berg 
34717741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
348d0709a65SJohannes Berg 	sta->local = local;
349d0709a65SJohannes Berg 	sta->sdata = sdata;
3508bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
351f0706e82SJiri Benc 
35271ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35371ec375cSJohannes Berg 
35418171520SThomas Gleixner 	ktime_get_ts(&uptime);
355ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
356541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
357ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
358ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
359541a45a1SBruno Randolf 
360ef04a297SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp))
361ef04a297SJohannes Berg 		goto free;
362f0706e82SJiri Benc 
3635a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
364a622ab72SJohannes Berg 		/*
365a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
366a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
367a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
368a622ab72SJohannes Berg 		 */
36916c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
37016c5f15cSRon Rindjunsky 	}
371948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
372948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
373948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
374948d887dSJohannes Berg 	}
37573651ee6SJohannes Berg 
3765a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3774be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
378cccaec98SSenthil Balasubramanian 
379af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
380687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
381687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
382687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
383687da132SEmmanuel Grumbach 			local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
384687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
385687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
386687da132SEmmanuel Grumbach 		/*
387687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
388687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
389687da132SEmmanuel Grumbach 		 */
390687da132SEmmanuel Grumbach 		switch (smps) {
391687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
392687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
393687da132SEmmanuel Grumbach 			break;
394687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
395687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
396687da132SEmmanuel Grumbach 			break;
397687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
398687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
399687da132SEmmanuel Grumbach 			break;
400687da132SEmmanuel Grumbach 		default:
401687da132SEmmanuel Grumbach 			WARN_ON(1);
402687da132SEmmanuel Grumbach 		}
403687da132SEmmanuel Grumbach 	}
404af0ed69bSJohannes Berg 
405bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
40673651ee6SJohannes Berg 	return sta;
407ef04a297SJohannes Berg 
408ef04a297SJohannes Berg free:
409ef04a297SJohannes Berg 	if (sta->tx_lat) {
410ef04a297SJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
411ef04a297SJohannes Berg 			kfree(sta->tx_lat[i].bins);
412ef04a297SJohannes Berg 		kfree(sta->tx_lat);
413ef04a297SJohannes Berg 	}
414ef04a297SJohannes Berg 	kfree(sta);
415ef04a297SJohannes Berg 	return NULL;
41673651ee6SJohannes Berg }
41773651ee6SJohannes Berg 
4188c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
41934e89507SJohannes Berg {
42034e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42134e89507SJohannes Berg 
42203e4497eSJohannes Berg 	/*
42303e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
42403e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
42503e4497eSJohannes Berg 	 * and another CPU turns off the net device.
42603e4497eSJohannes Berg 	 */
4278c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4288c71df7aSGuy Eilam 		return -ENETDOWN;
42903e4497eSJohannes Berg 
430b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4318c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4328c71df7aSGuy Eilam 		return -EINVAL;
4338c71df7aSGuy Eilam 
4348c71df7aSGuy Eilam 	return 0;
43593e5deb1SJohannes Berg }
43644213b5eSJohannes Berg 
437f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
438f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
439f09603a2SJohannes Berg 				     struct sta_info *sta)
440f09603a2SJohannes Berg {
441f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
442f09603a2SJohannes Berg 	int err = 0;
443f09603a2SJohannes Berg 
444f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
445f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
446f09603a2SJohannes Berg 		if (err)
447f09603a2SJohannes Berg 			break;
448f09603a2SJohannes Berg 	}
449f09603a2SJohannes Berg 
450f09603a2SJohannes Berg 	if (!err) {
451a4ec45a4SJohannes Berg 		/*
452a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
453a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
454a4ec45a4SJohannes Berg 		 */
455a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
456f09603a2SJohannes Berg 			sta->uploaded = true;
457f09603a2SJohannes Berg 		return 0;
458f09603a2SJohannes Berg 	}
459f09603a2SJohannes Berg 
460f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
461bdcbd8e0SJohannes Berg 		sdata_info(sdata,
462bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
463bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
464f09603a2SJohannes Berg 		err = 0;
465f09603a2SJohannes Berg 	}
466f09603a2SJohannes Berg 
467f09603a2SJohannes Berg 	/* unwind on error */
468f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
469f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
470f09603a2SJohannes Berg 
471f09603a2SJohannes Berg 	return err;
472f09603a2SJohannes Berg }
473f09603a2SJohannes Berg 
47434e89507SJohannes Berg /*
4758c71df7aSGuy Eilam  * should be called with sta_mtx locked
4768c71df7aSGuy Eilam  * this function replaces the mutex lock
4778c71df7aSGuy Eilam  * with a RCU lock
4788c71df7aSGuy Eilam  */
4794d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4808c71df7aSGuy Eilam {
4818c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4828c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4837852e361SJohannes Berg 	struct station_info sinfo;
4848c71df7aSGuy Eilam 	int err = 0;
4858c71df7aSGuy Eilam 
4868c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4878c71df7aSGuy Eilam 
4887852e361SJohannes Berg 	/* check if STA exists already */
4897852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4904d33960bSJohannes Berg 		err = -EEXIST;
4914d33960bSJohannes Berg 		goto out_err;
49234e89507SJohannes Berg 	}
49334e89507SJohannes Berg 
4944d33960bSJohannes Berg 	local->num_sta++;
4954d33960bSJohannes Berg 	local->sta_generation++;
4964d33960bSJohannes Berg 	smp_mb();
4974d33960bSJohannes Berg 
4985108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
4995108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5005108ca82SJohannes Berg 
5014d33960bSJohannes Berg 	/* make the station visible */
5024d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5034d33960bSJohannes Berg 
504*2bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
50583d5cc01SJohannes Berg 
5065108ca82SJohannes Berg 	/* notify driver */
5075108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5085108ca82SJohannes Berg 	if (err)
5095108ca82SJohannes Berg 		goto out_remove;
5105108ca82SJohannes Berg 
51183d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5125108ca82SJohannes Berg 	/* accept BA sessions now */
5135108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5144d33960bSJohannes Berg 
51521f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
5164d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5174d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5184d33960bSJohannes Berg 
5194d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5204d33960bSJohannes Berg 	sinfo.filled = 0;
5214d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5224d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
523d0709a65SJohannes Berg 
524bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
525f0706e82SJiri Benc 
52634e89507SJohannes Berg 	/* move reference to rcu-protected */
52734e89507SJohannes Berg 	rcu_read_lock();
52834e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
529e9f207f0SJiri Benc 
53073651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
53173651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
53273651ee6SJohannes Berg 
53373651ee6SJohannes Berg 	return 0;
5345108ca82SJohannes Berg  out_remove:
5355108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5365108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5375108ca82SJohannes Berg 	local->num_sta--;
5385108ca82SJohannes Berg 	synchronize_net();
5395108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5404d33960bSJohannes Berg  out_err:
5414d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5424d33960bSJohannes Berg 	rcu_read_lock();
5434d33960bSJohannes Berg 	return err;
5448c71df7aSGuy Eilam }
5458c71df7aSGuy Eilam 
5468c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5478c71df7aSGuy Eilam {
5488c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
549308f7fcfSZhao, Gang 	int err;
5508c71df7aSGuy Eilam 
5514d33960bSJohannes Berg 	might_sleep();
5524d33960bSJohannes Berg 
5538c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5548c71df7aSGuy Eilam 	if (err) {
5558c71df7aSGuy Eilam 		rcu_read_lock();
5568c71df7aSGuy Eilam 		goto out_free;
5578c71df7aSGuy Eilam 	}
5588c71df7aSGuy Eilam 
559004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
560004c872eSJohannes Berg 
5614d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5628c71df7aSGuy Eilam 	if (err)
563004c872eSJohannes Berg 		goto out_free;
564d0709a65SJohannes Berg 
565004c872eSJohannes Berg 	return 0;
56693e5deb1SJohannes Berg  out_free:
567d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
56893e5deb1SJohannes Berg 	return err;
569f0706e82SJiri Benc }
570f0706e82SJiri Benc 
57134e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
57234e89507SJohannes Berg {
57334e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
57434e89507SJohannes Berg 
57534e89507SJohannes Berg 	rcu_read_unlock();
57634e89507SJohannes Berg 
57734e89507SJohannes Berg 	return err;
57834e89507SJohannes Berg }
57934e89507SJohannes Berg 
580d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
581004c872eSJohannes Berg {
582004c872eSJohannes Berg 	/*
583004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
584004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
585004c872eSJohannes Berg 	 */
586d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
587004c872eSJohannes Berg }
588004c872eSJohannes Berg 
589d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
590004c872eSJohannes Berg {
591004c872eSJohannes Berg 	/*
592004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
593004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
594004c872eSJohannes Berg 	 */
595d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
596004c872eSJohannes Berg }
597004c872eSJohannes Berg 
5983d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
5993d5839b6SIlan Peer {
6003d5839b6SIlan Peer 	/*
6013d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6023d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6033d5839b6SIlan Peer 	 */
6043d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6053d5839b6SIlan Peer }
6063d5839b6SIlan Peer 
607948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
608004c872eSJohannes Berg {
609948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
610948d887dSJohannes Berg 	switch (ac) {
611948d887dSJohannes Berg 	case IEEE80211_AC_VO:
612948d887dSJohannes Berg 		return BIT(6) | BIT(7);
613948d887dSJohannes Berg 	case IEEE80211_AC_VI:
614948d887dSJohannes Berg 		return BIT(4) | BIT(5);
615948d887dSJohannes Berg 	case IEEE80211_AC_BE:
616948d887dSJohannes Berg 		return BIT(0) | BIT(3);
617948d887dSJohannes Berg 	case IEEE80211_AC_BK:
618948d887dSJohannes Berg 		return BIT(1) | BIT(2);
619948d887dSJohannes Berg 	default:
620948d887dSJohannes Berg 		WARN_ON(1);
621948d887dSJohannes Berg 		return 0;
622d0709a65SJohannes Berg 	}
623004c872eSJohannes Berg }
624004c872eSJohannes Berg 
625c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
626004c872eSJohannes Berg {
627c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
628d012a605SMarco Porsch 	struct ps_data *ps;
629948d887dSJohannes Berg 	bool indicate_tim = false;
630948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
631948d887dSJohannes Berg 	int ac;
632d012a605SMarco Porsch 	u16 id;
633004c872eSJohannes Berg 
634d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
635d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
636c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
637c868cb35SJohannes Berg 			return;
6383e122be0SJohannes Berg 
639d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
640d012a605SMarco Porsch 		id = sta->sta.aid;
6413f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6423f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6433f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
644204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6456f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6463f52b7e3SMarco Porsch #endif
647d012a605SMarco Porsch 	} else {
648d012a605SMarco Porsch 		return;
649d012a605SMarco Porsch 	}
650d012a605SMarco Porsch 
651c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
652c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
653c868cb35SJohannes Berg 		return;
654004c872eSJohannes Berg 
655c868cb35SJohannes Berg 	if (sta->dead)
656c868cb35SJohannes Berg 		goto done;
6573e122be0SJohannes Berg 
658948d887dSJohannes Berg 	/*
659948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
660948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
661948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
662948d887dSJohannes Berg 	 * non-enabled ones.
663948d887dSJohannes Berg 	 */
664948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
665948d887dSJohannes Berg 		ignore_for_tim = 0;
666948d887dSJohannes Berg 
667948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
668948d887dSJohannes Berg 		unsigned long tids;
669948d887dSJohannes Berg 
670948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
671948d887dSJohannes Berg 			continue;
672948d887dSJohannes Berg 
673948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
674948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
675948d887dSJohannes Berg 		if (indicate_tim)
676948d887dSJohannes Berg 			break;
677948d887dSJohannes Berg 
678948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
679948d887dSJohannes Berg 
680948d887dSJohannes Berg 		indicate_tim |=
681948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
682004c872eSJohannes Berg 	}
683004c872eSJohannes Berg 
684c868cb35SJohannes Berg  done:
68565f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
686004c872eSJohannes Berg 
6873d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6883d5839b6SIlan Peer 		goto out_unlock;
6893d5839b6SIlan Peer 
690948d887dSJohannes Berg 	if (indicate_tim)
691d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
692c868cb35SJohannes Berg 	else
693d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6943e122be0SJohannes Berg 
695c868cb35SJohannes Berg 	if (local->ops->set_tim) {
696c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
697948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
698c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
699004c872eSJohannes Berg 	}
700004c872eSJohannes Berg 
7013d5839b6SIlan Peer out_unlock:
70265f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
703004c872eSJohannes Berg }
704004c872eSJohannes Berg 
705cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
706f0706e82SJiri Benc {
707e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
708f0706e82SJiri Benc 	int timeout;
709f0706e82SJiri Benc 
710f0706e82SJiri Benc 	if (!skb)
711cd0b8d89SJohannes Berg 		return false;
712f0706e82SJiri Benc 
713e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
714f0706e82SJiri Benc 
715f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
71657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
71757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
71857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
719f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
720f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
721e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
722f0706e82SJiri Benc }
723f0706e82SJiri Benc 
724f0706e82SJiri Benc 
725948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
726948d887dSJohannes Berg 						struct sta_info *sta, int ac)
727f0706e82SJiri Benc {
728f0706e82SJiri Benc 	unsigned long flags;
729f0706e82SJiri Benc 	struct sk_buff *skb;
730f0706e82SJiri Benc 
73160750397SJohannes Berg 	/*
73260750397SJohannes Berg 	 * First check for frames that should expire on the filtered
73360750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
73460750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
73560750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
73660750397SJohannes Berg 	 * total_ps_buffered counter.
73760750397SJohannes Berg 	 */
738f0706e82SJiri Benc 	for (;;) {
739948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
740948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
74157c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
742948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
743836341a7SJohannes Berg 		else
744f0706e82SJiri Benc 			skb = NULL;
745948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
746f0706e82SJiri Benc 
74760750397SJohannes Berg 		/*
74860750397SJohannes Berg 		 * Frames are queued in order, so if this one
74960750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
75060750397SJohannes Berg 		 * we actually reached the end of the queue we
75160750397SJohannes Berg 		 * also need to stop, of course.
75260750397SJohannes Berg 		 */
75360750397SJohannes Berg 		if (!skb)
75460750397SJohannes Berg 			break;
755d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
75660750397SJohannes Berg 	}
75760750397SJohannes Berg 
75860750397SJohannes Berg 	/*
75960750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
76060750397SJohannes Berg 	 * only find something if the filtered queue was emptied
76160750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
76260750397SJohannes Berg 	 * buffered frames.
76360750397SJohannes Berg 	 */
764f0706e82SJiri Benc 	for (;;) {
765948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
766948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
767f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
768948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
769f0706e82SJiri Benc 		else
770f0706e82SJiri Benc 			skb = NULL;
771948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
772f0706e82SJiri Benc 
77360750397SJohannes Berg 		/*
77460750397SJohannes Berg 		 * frames are queued in order, so if this one
77560750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
77660750397SJohannes Berg 		 * the queue) we can stop testing
77760750397SJohannes Berg 		 */
778836341a7SJohannes Berg 		if (!skb)
779836341a7SJohannes Berg 			break;
780836341a7SJohannes Berg 
781f0706e82SJiri Benc 		local->total_ps_buffered--;
782bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
783bdcbd8e0SJohannes Berg 		       sta->sta.addr);
784d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
785f0706e82SJiri Benc 	}
7863393a608SJuuso Oikarinen 
78760750397SJohannes Berg 	/*
78860750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
78960750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
79060750397SJohannes Berg 	 * frames.
79160750397SJohannes Berg 	 */
79260750397SJohannes Berg 	sta_info_recalc_tim(sta);
79360750397SJohannes Berg 
79460750397SJohannes Berg 	/*
79560750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
79660750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
79760750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
79860750397SJohannes Berg 	 */
799948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
800948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
801948d887dSJohannes Berg }
802948d887dSJohannes Berg 
803948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
804948d887dSJohannes Berg 					     struct sta_info *sta)
805948d887dSJohannes Berg {
806948d887dSJohannes Berg 	bool have_buffered = false;
807948d887dSJohannes Berg 	int ac;
808948d887dSJohannes Berg 
8093f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8103f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8113f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
812948d887dSJohannes Berg 		return false;
813948d887dSJohannes Berg 
814948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
815948d887dSJohannes Berg 		have_buffered |=
816948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
817948d887dSJohannes Berg 
818948d887dSJohannes Berg 	return have_buffered;
819f0706e82SJiri Benc }
820f0706e82SJiri Benc 
821d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
82234e89507SJohannes Berg {
82334e89507SJohannes Berg 	struct ieee80211_local *local;
82434e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8256d10e46bSJohannes Berg 	int ret;
82634e89507SJohannes Berg 
82734e89507SJohannes Berg 	might_sleep();
82834e89507SJohannes Berg 
82934e89507SJohannes Berg 	if (!sta)
83034e89507SJohannes Berg 		return -ENOENT;
83134e89507SJohannes Berg 
83234e89507SJohannes Berg 	local = sta->local;
83334e89507SJohannes Berg 	sdata = sta->sdata;
83434e89507SJohannes Berg 
83583d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
83683d5cc01SJohannes Berg 
837098a6070SJohannes Berg 	/*
838098a6070SJohannes Berg 	 * Before removing the station from the driver and
839098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
840098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
841098a6070SJohannes Berg 	 * will be sufficient.
842098a6070SJohannes Berg 	 */
843c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
844c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
845098a6070SJohannes Berg 
84634e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
847b01711beSJohannes Berg 	if (WARN_ON(ret))
84834e89507SJohannes Berg 		return ret;
84934e89507SJohannes Berg 
850794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8514d33960bSJohannes Berg 
8526a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8536a9d1b91SJohannes Berg 
854a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
855a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
856a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
857a710c816SJohannes Berg 
858d778207bSJohannes Berg 	return 0;
859d778207bSJohannes Berg }
860d778207bSJohannes Berg 
861d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
862d778207bSJohannes Berg {
863d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
864d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
865d778207bSJohannes Berg 	int ret;
866d778207bSJohannes Berg 
867d778207bSJohannes Berg 	/*
868d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
869d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
870d778207bSJohannes Berg 	 */
871d778207bSJohannes Berg 
872d778207bSJohannes Berg 	might_sleep();
873d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
874d778207bSJohannes Berg 
875c8782078SJohannes Berg 	/* now keys can no longer be reached */
8766d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
87734e89507SJohannes Berg 
87834e89507SJohannes Berg 	sta->dead = true;
87934e89507SJohannes Berg 
88034e89507SJohannes Berg 	local->num_sta--;
88134e89507SJohannes Berg 	local->sta_generation++;
88234e89507SJohannes Berg 
88383d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
884f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
885f09603a2SJohannes Berg 		if (ret) {
88683d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
88783d5cc01SJohannes Berg 			break;
88883d5cc01SJohannes Berg 		}
88983d5cc01SJohannes Berg 	}
890d9a7ddb0SJohannes Berg 
891f09603a2SJohannes Berg 	if (sta->uploaded) {
892f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
893f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
894f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
895f09603a2SJohannes Berg 	}
89634e89507SJohannes Berg 
897bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
898bdcbd8e0SJohannes Berg 
899ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
900ec15e68bSJouni Malinen 
90134e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
90234e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
90321f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
90434e89507SJohannes Berg 
905d34ba216SJohannes Berg 	cleanup_single_sta(sta);
906d778207bSJohannes Berg }
907d778207bSJohannes Berg 
908d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
909d778207bSJohannes Berg {
910d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
911d778207bSJohannes Berg 
912d778207bSJohannes Berg 	if (err)
913d778207bSJohannes Berg 		return err;
914d778207bSJohannes Berg 
915d778207bSJohannes Berg 	synchronize_net();
916d778207bSJohannes Berg 
917d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
91834e89507SJohannes Berg 
91934e89507SJohannes Berg 	return 0;
92034e89507SJohannes Berg }
92134e89507SJohannes Berg 
92234e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
92334e89507SJohannes Berg {
92434e89507SJohannes Berg 	struct sta_info *sta;
92534e89507SJohannes Berg 	int ret;
92634e89507SJohannes Berg 
92734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9287852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
92934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
93034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
93134e89507SJohannes Berg 
93234e89507SJohannes Berg 	return ret;
93334e89507SJohannes Berg }
93434e89507SJohannes Berg 
93534e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
93634e89507SJohannes Berg 			      const u8 *addr)
93734e89507SJohannes Berg {
93834e89507SJohannes Berg 	struct sta_info *sta;
93934e89507SJohannes Berg 	int ret;
94034e89507SJohannes Berg 
94134e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9427852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
94334e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
94434e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
94534e89507SJohannes Berg 
94634e89507SJohannes Berg 	return ret;
94734e89507SJohannes Berg }
948f0706e82SJiri Benc 
949f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
950f0706e82SJiri Benc {
951f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
952f0706e82SJiri Benc 	struct sta_info *sta;
9533393a608SJuuso Oikarinen 	bool timer_needed = false;
954f0706e82SJiri Benc 
955d0709a65SJohannes Berg 	rcu_read_lock();
956d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9573393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9583393a608SJuuso Oikarinen 			timer_needed = true;
959d0709a65SJohannes Berg 	rcu_read_unlock();
960f0706e82SJiri Benc 
9615bb644a0SJohannes Berg 	if (local->quiescing)
9625bb644a0SJohannes Berg 		return;
9635bb644a0SJohannes Berg 
9643393a608SJuuso Oikarinen 	if (!timer_needed)
9653393a608SJuuso Oikarinen 		return;
9663393a608SJuuso Oikarinen 
96726d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
96826d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
969f0706e82SJiri Benc }
970f0706e82SJiri Benc 
971f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
972f0706e82SJiri Benc {
9734d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
97434e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
975f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
976f0706e82SJiri Benc 
977b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
978b24b8a24SPavel Emelyanov 		    (unsigned long)local);
979f0706e82SJiri Benc }
980f0706e82SJiri Benc 
981f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
982f0706e82SJiri Benc {
983a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
984f0706e82SJiri Benc }
985f0706e82SJiri Benc 
986051007d9SJohannes Berg 
987e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
988f0706e82SJiri Benc {
989b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
990f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
991d778207bSJohannes Berg 	LIST_HEAD(free_list);
99244213b5eSJohannes Berg 	int ret = 0;
993f0706e82SJiri Benc 
994d0709a65SJohannes Berg 	might_sleep();
995d0709a65SJohannes Berg 
996e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
997e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
998e716251dSJohannes Berg 
99934e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
100034e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1001e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1002e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1003d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1004d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
100534316837SJohannes Berg 			ret++;
100634316837SJohannes Berg 		}
100734e89507SJohannes Berg 	}
1008d778207bSJohannes Berg 
1009d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1010d778207bSJohannes Berg 		synchronize_net();
1011d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1012d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1013d778207bSJohannes Berg 	}
101434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
101544213b5eSJohannes Berg 
1016051007d9SJohannes Berg 	return ret;
1017051007d9SJohannes Berg }
1018051007d9SJohannes Berg 
101924723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
102024723d1bSJohannes Berg 			  unsigned long exp_time)
102124723d1bSJohannes Berg {
102224723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
102324723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
102424723d1bSJohannes Berg 
102534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1026e46a2cf9SMohammed Shafi Shajakhan 
1027e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1028ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1029ec2b774eSMarek Lindner 			continue;
1030ec2b774eSMarek Lindner 
103124723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1032eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1033bdcbd8e0SJohannes Berg 				sta->sta.addr);
10343f52b7e3SMarco Porsch 
10353f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10363f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10373f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10383f52b7e3SMarco Porsch 
103934e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
104024723d1bSJohannes Berg 		}
1041e46a2cf9SMohammed Shafi Shajakhan 	}
1042e46a2cf9SMohammed Shafi Shajakhan 
104334e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
104424723d1bSJohannes Berg }
104517741cdcSJohannes Berg 
1046686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1047686b9cb9SBen Greear 					       const u8 *addr,
1048686b9cb9SBen Greear 					       const u8 *localaddr)
104917741cdcSJohannes Berg {
1050abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
105117741cdcSJohannes Berg 
1052686b9cb9SBen Greear 	/*
1053686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1054686b9cb9SBen Greear 	 * ... first in list.
1055686b9cb9SBen Greear 	 */
1056f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1057686b9cb9SBen Greear 		if (localaddr &&
1058b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1059686b9cb9SBen Greear 			continue;
1060f7c65594SJohannes Berg 		if (!sta->uploaded)
1061f7c65594SJohannes Berg 			return NULL;
106217741cdcSJohannes Berg 		return &sta->sta;
1063f7c65594SJohannes Berg 	}
1064f7c65594SJohannes Berg 
1065abe60632SJohannes Berg 	return NULL;
106617741cdcSJohannes Berg }
1067686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10685ed176e1SJohannes Berg 
10695ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10705ed176e1SJohannes Berg 					 const u8 *addr)
10715ed176e1SJohannes Berg {
1072f7c65594SJohannes Berg 	struct sta_info *sta;
10735ed176e1SJohannes Berg 
10745ed176e1SJohannes Berg 	if (!vif)
10755ed176e1SJohannes Berg 		return NULL;
10765ed176e1SJohannes Berg 
1077f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1078f7c65594SJohannes Berg 	if (!sta)
1079f7c65594SJohannes Berg 		return NULL;
10805ed176e1SJohannes Berg 
1081f7c65594SJohannes Berg 	if (!sta->uploaded)
1082f7c65594SJohannes Berg 		return NULL;
1083f7c65594SJohannes Berg 
1084f7c65594SJohannes Berg 	return &sta->sta;
10855ed176e1SJohannes Berg }
108617741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1087af818581SJohannes Berg 
1088e3685e03SJohannes Berg /* powersave support code */
1089e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
109050a9432dSJohannes Berg {
1091608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1092e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1093e3685e03SJohannes Berg 	struct sk_buff_head pending;
1094e3685e03SJohannes Berg 	int filtered = 0, buffered = 0, ac;
1095e3685e03SJohannes Berg 	unsigned long flags;
1096d012a605SMarco Porsch 	struct ps_data *ps;
1097d012a605SMarco Porsch 
10983918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
10993918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
11003918edb0SFelix Fietkau 				     u.ap);
11013918edb0SFelix Fietkau 
11023918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1103d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11043f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11053f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1106d012a605SMarco Porsch 	else
1107d012a605SMarco Porsch 		return;
110850a9432dSJohannes Berg 
1109c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
111047086fc5SJohannes Berg 
11115a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1112948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1113948d887dSJohannes Berg 
1114d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
111512375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1116af818581SJohannes Berg 
1117948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1118af818581SJohannes Berg 
11191d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11201d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1121af818581SJohannes Berg 	/* Send all buffered frames to the station */
1122948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1123948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1124948d887dSJohannes Berg 
1125987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1126948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1127987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1128948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1129948d887dSJohannes Berg 		filtered += tmp - count;
1130948d887dSJohannes Berg 		count = tmp;
1131948d887dSJohannes Berg 
1132987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1133948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1134987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1135948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1136948d887dSJohannes Berg 		buffered += tmp - count;
1137948d887dSJohannes Berg 	}
1138948d887dSJohannes Berg 
1139e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
11405ac2e350SJohannes Berg 
11415ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
11425ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
11435ac2e350SJohannes Berg 
11445ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
11455ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
11465ac2e350SJohannes Berg 	 */
11475ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
11485ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
11491d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1150948d887dSJohannes Berg 
1151e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1152e3685e03SJohannes Berg 
1153687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1154062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1155062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1156687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1157687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1158687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1159687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1160687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1161687da132SEmmanuel Grumbach 		       sta->sta.addr);
1162687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1163687da132SEmmanuel Grumbach 					   sta->sta.addr,
1164687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1165687da132SEmmanuel Grumbach 	}
1166687da132SEmmanuel Grumbach 
1167af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1168af818581SJohannes Berg 
1169c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1170c868cb35SJohannes Berg 
1171bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1172bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1173bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1174af818581SJohannes Berg }
1175af818581SJohannes Berg 
1176ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1177ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1178b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1179b77cf4f8SJohannes Berg 					 bool call_driver)
1180ce662b44SJohannes Berg {
1181ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1182ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1183ce662b44SJohannes Berg 	struct sk_buff *skb;
1184ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1185ce662b44SJohannes Berg 	__le16 fc;
1186a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1187ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
118855de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1189ce662b44SJohannes Berg 
1190ce662b44SJohannes Berg 	if (qos) {
1191ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1192ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1193ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1194ce662b44SJohannes Berg 	} else {
1195ce662b44SJohannes Berg 		size -= 2;
1196ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1197ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1198ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1199ce662b44SJohannes Berg 	}
1200ce662b44SJohannes Berg 
1201ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1202ce662b44SJohannes Berg 	if (!skb)
1203ce662b44SJohannes Berg 		return;
1204ce662b44SJohannes Berg 
1205ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1206ce662b44SJohannes Berg 
1207ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1208ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1209ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1210ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1211ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1212ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1213864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1214ce662b44SJohannes Berg 
1215ce662b44SJohannes Berg 	skb->priority = tid;
1216ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
121759b66255SJohannes Berg 	if (qos) {
1218ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1219ce662b44SJohannes Berg 
122040b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1221ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1222ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1223ce662b44SJohannes Berg 	}
1224ce662b44SJohannes Berg 
1225ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1226ce662b44SJohannes Berg 
1227ce662b44SJohannes Berg 	/*
1228ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1229ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1230deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1231deeaee19SJohannes Berg 	 * ends the poll/service period.
1232ce662b44SJohannes Berg 	 */
123302f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1234d6d23de2SFelix Fietkau 		       IEEE80211_TX_CTL_PS_RESPONSE |
1235deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1236ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1237ce662b44SJohannes Berg 
1238b77cf4f8SJohannes Berg 	if (call_driver)
1239b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1240b77cf4f8SJohannes Berg 					  reason, false);
124140b96408SJohannes Berg 
124289afe614SJohannes Berg 	skb->dev = sdata->dev;
124389afe614SJohannes Berg 
124455de908aSJohannes Berg 	rcu_read_lock();
124555de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
124655de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
124755de908aSJohannes Berg 		rcu_read_unlock();
124855de908aSJohannes Berg 		kfree_skb(skb);
124955de908aSJohannes Berg 		return;
125055de908aSJohannes Berg 	}
125155de908aSJohannes Berg 
12524bf88530SJohannes Berg 	ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
125355de908aSJohannes Berg 	rcu_read_unlock();
1254ce662b44SJohannes Berg }
1255ce662b44SJohannes Berg 
12560a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
12570a1cb809SJohannes Berg {
12580a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
12590a1cb809SJohannes Berg 	if (tids & 0xF8)
12600a1cb809SJohannes Berg 		return fls(tids) - 1;
12610a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
12620a1cb809SJohannes Berg 	if (tids & BIT(0))
12630a1cb809SJohannes Berg 		return 0;
12640a1cb809SJohannes Berg 	return fls(tids) - 1;
12650a1cb809SJohannes Berg }
12660a1cb809SJohannes Berg 
126747086fc5SJohannes Berg static void
126847086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
126947086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
127047086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1271af818581SJohannes Berg {
1272af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1273af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1274948d887dSJohannes Berg 	bool more_data = false;
1275948d887dSJohannes Berg 	int ac;
12764049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
127747086fc5SJohannes Berg 	struct sk_buff_head frames;
127847086fc5SJohannes Berg 
1279deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1280c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1281deeaee19SJohannes Berg 
128247086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1283af818581SJohannes Berg 
1284f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1285948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
12864049e09aSJohannes Berg 		unsigned long tids;
12874049e09aSJohannes Berg 
128847086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1289948d887dSJohannes Berg 			continue;
1290948d887dSJohannes Berg 
12914049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
12924049e09aSJohannes Berg 
1293f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1294f9f760b4SJohannes Berg 		 * release from hardware queues
1295f9f760b4SJohannes Berg 		 */
1296f9f760b4SJohannes Berg 		if (skb_queue_empty(&frames))
1297f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1298f9f760b4SJohannes Berg 
12994049e09aSJohannes Berg 		if (driver_release_tids) {
1300f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1301f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1302f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1303f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1304f9f760b4SJohannes Berg 			 */
1305f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1306f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1307f9f760b4SJohannes Berg 				more_data = true;
1308f9f760b4SJohannes Berg 				driver_release_tids =
1309f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1310f9f760b4SJohannes Berg 						driver_release_tids));
1311f9f760b4SJohannes Berg 				break;
1312f9f760b4SJohannes Berg 			}
13134049e09aSJohannes Berg 		} else {
131447086fc5SJohannes Berg 			struct sk_buff *skb;
131547086fc5SJohannes Berg 
131647086fc5SJohannes Berg 			while (n_frames > 0) {
1317948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1318948d887dSJohannes Berg 				if (!skb) {
131947086fc5SJohannes Berg 					skb = skb_dequeue(
132047086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1321af818581SJohannes Berg 					if (skb)
1322af818581SJohannes Berg 						local->total_ps_buffered--;
1323af818581SJohannes Berg 				}
132447086fc5SJohannes Berg 				if (!skb)
132547086fc5SJohannes Berg 					break;
132647086fc5SJohannes Berg 				n_frames--;
132747086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
132847086fc5SJohannes Berg 			}
1329948d887dSJohannes Berg 		}
1330948d887dSJohannes Berg 
1331f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1332f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1333f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13344049e09aSJohannes Berg 		 */
1335948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1336948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1337948d887dSJohannes Berg 			more_data = true;
1338948d887dSJohannes Berg 			break;
1339948d887dSJohannes Berg 		}
1340948d887dSJohannes Berg 	}
1341af818581SJohannes Berg 
1342f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1343ce662b44SJohannes Berg 		int tid;
13444049e09aSJohannes Berg 
1345ce662b44SJohannes Berg 		/*
1346ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1347ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1348ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1349ce662b44SJohannes Berg 		 *
1350ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1351ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1352ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1353ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1354ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1355ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1356ce662b44SJohannes Berg 		 *
1357ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1358ce662b44SJohannes Berg 		 */
1359ce662b44SJohannes Berg 
1360ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1361ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1362ce662b44SJohannes Berg 
1363b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1364f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
136547086fc5SJohannes Berg 		struct sk_buff_head pending;
136647086fc5SJohannes Berg 		struct sk_buff *skb;
136740b96408SJohannes Berg 		int num = 0;
136840b96408SJohannes Berg 		u16 tids = 0;
1369b77cf4f8SJohannes Berg 		bool need_null = false;
137047086fc5SJohannes Berg 
137147086fc5SJohannes Berg 		skb_queue_head_init(&pending);
137247086fc5SJohannes Berg 
137347086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1374af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
137547086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
137640b96408SJohannes Berg 			u8 *qoshdr = NULL;
137740b96408SJohannes Berg 
137840b96408SJohannes Berg 			num++;
1379af818581SJohannes Berg 
1380af818581SJohannes Berg 			/*
138147086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
138247086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
138347086fc5SJohannes Berg 			 * exchange.
1384af818581SJohannes Berg 			 */
1385d6d23de2SFelix Fietkau 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1386d6d23de2SFelix Fietkau 				       IEEE80211_TX_CTL_PS_RESPONSE;
1387af818581SJohannes Berg 
138847086fc5SJohannes Berg 			/*
138947086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
139047086fc5SJohannes Berg 			 * more buffered frames for this STA
139147086fc5SJohannes Berg 			 */
139224b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
139347086fc5SJohannes Berg 				hdr->frame_control |=
139447086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
139524b9c373SJanusz.Dziedzic@tieto.com 			else
139624b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
139724b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1398af818581SJohannes Berg 
139940b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
140040b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
140140b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
140240b96408SJohannes Berg 
1403b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1404b77cf4f8SJohannes Berg 
1405b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1406b77cf4f8SJohannes Berg 
1407b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1408b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1409b77cf4f8SJohannes Berg 				continue;
1410b77cf4f8SJohannes Berg 
1411b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1412b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1413b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1414b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1415b77cf4f8SJohannes Berg 				break;
1416b77cf4f8SJohannes Berg 			}
1417b77cf4f8SJohannes Berg 
1418b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1419b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1420b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1421b77cf4f8SJohannes Berg 			 * and be done.
1422b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1423b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1424b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1425b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1426b77cf4f8SJohannes Berg 			 *
1427b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1428b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1429b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1430b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1431b77cf4f8SJohannes Berg 			 *
1432b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1433b77cf4f8SJohannes Berg 			 */
1434b77cf4f8SJohannes Berg 			if (qoshdr) {
143540b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1436deeaee19SJohannes Berg 
143747086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
143847086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1439b77cf4f8SJohannes Berg 			} else {
1440b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1441b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1442b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1443b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1444b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1445b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1446b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1447b77cf4f8SJohannes Berg 				 */
1448b77cf4f8SJohannes Berg 				hdr->frame_control |=
1449b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1450b77cf4f8SJohannes Berg 				need_null = true;
1451b77cf4f8SJohannes Berg 				num++;
145252a3f20cSMarco Porsch 			}
1453b77cf4f8SJohannes Berg 			break;
145447086fc5SJohannes Berg 		}
145547086fc5SJohannes Berg 
145640b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
145740b96408SJohannes Berg 					  reason, more_data);
145840b96408SJohannes Berg 
145947086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1460af818581SJohannes Berg 
1461b77cf4f8SJohannes Berg 		if (need_null)
1462b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1463b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1464b77cf4f8SJohannes Berg 				reason, false);
1465b77cf4f8SJohannes Berg 
1466c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1467af818581SJohannes Berg 	} else {
1468af818581SJohannes Berg 		/*
14694049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
14704049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1471f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1472f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1473f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1474f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1475f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1476f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1477af818581SJohannes Berg 		 */
14784049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
147947086fc5SJohannes Berg 					    n_frames, reason, more_data);
14804049e09aSJohannes Berg 
14814049e09aSJohannes Berg 		/*
14824049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
14834049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1484f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
14854049e09aSJohannes Berg 		 * release function.
1486f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
14874049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
14884049e09aSJohannes Berg 		 */
1489af818581SJohannes Berg 	}
1490af818581SJohannes Berg }
1491af818581SJohannes Berg 
1492af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1493af818581SJohannes Berg {
149447086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1495af818581SJohannes Berg 
1496af818581SJohannes Berg 	/*
149747086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
149847086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
149947086fc5SJohannes Berg 	 * only from the non-enabled ones.
1500af818581SJohannes Berg 	 */
150147086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
150247086fc5SJohannes Berg 		ignore_for_response = 0;
1503af818581SJohannes Berg 
150447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
150547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1506af818581SJohannes Berg }
150747086fc5SJohannes Berg 
150847086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
150947086fc5SJohannes Berg {
151047086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
151147086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
151247086fc5SJohannes Berg 
151347086fc5SJohannes Berg 	/*
151447086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
151547086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
151647086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
151747086fc5SJohannes Berg 	 * actually getting called.
151847086fc5SJohannes Berg 	 */
151947086fc5SJohannes Berg 	if (!delivery_enabled)
152047086fc5SJohannes Berg 		return;
152147086fc5SJohannes Berg 
152247086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
152347086fc5SJohannes Berg 	case 1:
152447086fc5SJohannes Berg 		n_frames = 2;
152547086fc5SJohannes Berg 		break;
152647086fc5SJohannes Berg 	case 2:
152747086fc5SJohannes Berg 		n_frames = 4;
152847086fc5SJohannes Berg 		break;
152947086fc5SJohannes Berg 	case 3:
153047086fc5SJohannes Berg 		n_frames = 6;
153147086fc5SJohannes Berg 		break;
153247086fc5SJohannes Berg 	case 0:
153347086fc5SJohannes Berg 		/* XXX: what is a good value? */
153447086fc5SJohannes Berg 		n_frames = 8;
153547086fc5SJohannes Berg 		break;
153647086fc5SJohannes Berg 	}
153747086fc5SJohannes Berg 
153847086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
153947086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1540af818581SJohannes Berg }
1541af818581SJohannes Berg 
1542af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1543af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1544af818581SJohannes Berg {
1545af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1546af818581SJohannes Berg 
1547b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1548b5878a2dSJohannes Berg 
15495ac2e350SJohannes Berg 	if (block) {
1550c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
15515ac2e350SJohannes Berg 		return;
15525ac2e350SJohannes Berg 	}
15535ac2e350SJohannes Berg 
15545ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
15555ac2e350SJohannes Berg 		return;
15565ac2e350SJohannes Berg 
15575ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
15585ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
15595ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15605ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15615ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
15625ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
15635ac2e350SJohannes Berg 		/* must be asleep in this case */
15645ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15655ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
15665ac2e350SJohannes Berg 	} else {
15675ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
15685ac2e350SJohannes Berg 	}
1569af818581SJohannes Berg }
1570af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1571dcf55fb5SFelix Fietkau 
1572e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
157337fbd908SJohannes Berg {
157437fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
157537fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
157637fbd908SJohannes Berg 
157737fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
157837fbd908SJohannes Berg 
157937fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
158037fbd908SJohannes Berg }
1581e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
158237fbd908SJohannes Berg 
1583042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1584042ec453SJohannes Berg 				u8 tid, bool buffered)
1585dcf55fb5SFelix Fietkau {
1586dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1587dcf55fb5SFelix Fietkau 
15885a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1589042ec453SJohannes Berg 		return;
1590042ec453SJohannes Berg 
15911b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
15921b000789SJohannes Berg 
1593948d887dSJohannes Berg 	if (buffered)
1594948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1595948d887dSJohannes Berg 	else
1596948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1597948d887dSJohannes Berg 
1598c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1599dcf55fb5SFelix Fietkau }
1600042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1601d9a7ddb0SJohannes Berg 
160283d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1603d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1604d9a7ddb0SJohannes Berg {
16058bf11d8dSJohannes Berg 	might_sleep();
1606d9a7ddb0SJohannes Berg 
1607d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1608d9a7ddb0SJohannes Berg 		return 0;
1609d9a7ddb0SJohannes Berg 
1610f09603a2SJohannes Berg 	/* check allowed transitions first */
1611f09603a2SJohannes Berg 
1612d9a7ddb0SJohannes Berg 	switch (new_state) {
1613d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1614f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1615d9a7ddb0SJohannes Berg 			return -EINVAL;
1616d9a7ddb0SJohannes Berg 		break;
1617d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1618f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1619f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1620d9a7ddb0SJohannes Berg 			return -EINVAL;
1621d9a7ddb0SJohannes Berg 		break;
1622d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1623f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1624f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1625d9a7ddb0SJohannes Berg 			return -EINVAL;
1626d9a7ddb0SJohannes Berg 		break;
1627d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1628f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1629d9a7ddb0SJohannes Berg 			return -EINVAL;
1630d9a7ddb0SJohannes Berg 		break;
1631d9a7ddb0SJohannes Berg 	default:
1632d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1633d9a7ddb0SJohannes Berg 		return -EINVAL;
1634d9a7ddb0SJohannes Berg 	}
1635d9a7ddb0SJohannes Berg 
1636bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1637bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1638f09603a2SJohannes Berg 
1639f09603a2SJohannes Berg 	/*
1640f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1641f09603a2SJohannes Berg 	 * fail the transition
1642f09603a2SJohannes Berg 	 */
1643f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1644f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1645f09603a2SJohannes Berg 					sta->sta_state, new_state);
1646f09603a2SJohannes Berg 		if (err)
1647f09603a2SJohannes Berg 			return err;
1648f09603a2SJohannes Berg 	}
1649f09603a2SJohannes Berg 
1650f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1651f09603a2SJohannes Berg 
1652f09603a2SJohannes Berg 	switch (new_state) {
1653f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1654f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1655f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1656f09603a2SJohannes Berg 		break;
1657f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1658f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1659f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1660f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1661f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1662f09603a2SJohannes Berg 		break;
1663f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1664f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1665f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1666f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
16677e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16687e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16697e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16707e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1671f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1672f09603a2SJohannes Berg 		}
1673f09603a2SJohannes Berg 		break;
1674f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1675f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
16767e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16777e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16787e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16797e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1680f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1681f09603a2SJohannes Berg 		}
1682f09603a2SJohannes Berg 		break;
1683f09603a2SJohannes Berg 	default:
1684f09603a2SJohannes Berg 		break;
1685f09603a2SJohannes Berg 	}
1686f09603a2SJohannes Berg 
1687d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1688d9a7ddb0SJohannes Berg 
1689d9a7ddb0SJohannes Berg 	return 0;
1690d9a7ddb0SJohannes Berg }
1691687da132SEmmanuel Grumbach 
1692687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1693687da132SEmmanuel Grumbach {
1694687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1695687da132SEmmanuel Grumbach 	u8 rx_streams;
1696687da132SEmmanuel Grumbach 
1697687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1698687da132SEmmanuel Grumbach 		return 1;
1699687da132SEmmanuel Grumbach 
1700687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1701687da132SEmmanuel Grumbach 		int i;
1702687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1703687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1704687da132SEmmanuel Grumbach 
1705687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1706687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1707687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1708687da132SEmmanuel Grumbach 				return i + 1;
1709687da132SEmmanuel Grumbach 	}
1710687da132SEmmanuel Grumbach 
1711687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1712687da132SEmmanuel Grumbach 		rx_streams = 4;
1713687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1714687da132SEmmanuel Grumbach 		rx_streams = 3;
1715687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1716687da132SEmmanuel Grumbach 		rx_streams = 2;
1717687da132SEmmanuel Grumbach 	else
1718687da132SEmmanuel Grumbach 		rx_streams = 1;
1719687da132SEmmanuel Grumbach 
1720687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1721687da132SEmmanuel Grumbach 		return rx_streams;
1722687da132SEmmanuel Grumbach 
1723687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1724687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1725687da132SEmmanuel Grumbach }
1726b7ffbd7eSJohannes Berg 
1727b7ffbd7eSJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1728b7ffbd7eSJohannes Berg {
1729b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1730b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
17319a244409SJohn W. Linville 	struct rate_control_ref *ref = NULL;
1732b7ffbd7eSJohannes Berg 	struct timespec uptime;
1733b7ffbd7eSJohannes Berg 	u64 packets = 0;
1734b7ffbd7eSJohannes Berg 	u32 thr = 0;
1735b7ffbd7eSJohannes Berg 	int i, ac;
1736b7ffbd7eSJohannes Berg 
17379a244409SJohn W. Linville 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
17389a244409SJohn W. Linville 		ref = local->rate_ctrl;
17399a244409SJohn W. Linville 
1740b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
1741b7ffbd7eSJohannes Berg 
1742b7ffbd7eSJohannes Berg 	sinfo->filled = STATION_INFO_INACTIVE_TIME |
1743b7ffbd7eSJohannes Berg 			STATION_INFO_RX_BYTES64 |
1744b7ffbd7eSJohannes Berg 			STATION_INFO_TX_BYTES64 |
1745b7ffbd7eSJohannes Berg 			STATION_INFO_RX_PACKETS |
1746b7ffbd7eSJohannes Berg 			STATION_INFO_TX_PACKETS |
1747b7ffbd7eSJohannes Berg 			STATION_INFO_TX_RETRIES |
1748b7ffbd7eSJohannes Berg 			STATION_INFO_TX_FAILED |
1749b7ffbd7eSJohannes Berg 			STATION_INFO_TX_BITRATE |
1750b7ffbd7eSJohannes Berg 			STATION_INFO_RX_BITRATE |
1751b7ffbd7eSJohannes Berg 			STATION_INFO_RX_DROP_MISC |
1752b7ffbd7eSJohannes Berg 			STATION_INFO_BSS_PARAM |
1753b7ffbd7eSJohannes Berg 			STATION_INFO_CONNECTED_TIME |
1754b7ffbd7eSJohannes Berg 			STATION_INFO_STA_FLAGS |
1755b7ffbd7eSJohannes Berg 			STATION_INFO_BEACON_LOSS_COUNT;
1756b7ffbd7eSJohannes Berg 
175718171520SThomas Gleixner 	ktime_get_ts(&uptime);
1758b7ffbd7eSJohannes Berg 	sinfo->connected_time = uptime.tv_sec - sta->last_connected;
1759b7ffbd7eSJohannes Berg 
1760b7ffbd7eSJohannes Berg 	sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
1761b7ffbd7eSJohannes Berg 	sinfo->tx_bytes = 0;
1762b7ffbd7eSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1763b7ffbd7eSJohannes Berg 		sinfo->tx_bytes += sta->tx_bytes[ac];
1764b7ffbd7eSJohannes Berg 		packets += sta->tx_packets[ac];
1765b7ffbd7eSJohannes Berg 	}
1766b7ffbd7eSJohannes Berg 	sinfo->tx_packets = packets;
1767b7ffbd7eSJohannes Berg 	sinfo->rx_bytes = sta->rx_bytes;
1768b7ffbd7eSJohannes Berg 	sinfo->rx_packets = sta->rx_packets;
1769b7ffbd7eSJohannes Berg 	sinfo->tx_retries = sta->tx_retry_count;
1770b7ffbd7eSJohannes Berg 	sinfo->tx_failed = sta->tx_retry_failed;
1771b7ffbd7eSJohannes Berg 	sinfo->rx_dropped_misc = sta->rx_dropped;
1772b7ffbd7eSJohannes Berg 	sinfo->beacon_loss_count = sta->beacon_loss_count;
1773b7ffbd7eSJohannes Berg 
1774b7ffbd7eSJohannes Berg 	if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
1775b7ffbd7eSJohannes Berg 	    (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
1776b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
1777b7ffbd7eSJohannes Berg 		if (!local->ops->get_rssi ||
1778b7ffbd7eSJohannes Berg 		    drv_get_rssi(local, sdata, &sta->sta, &sinfo->signal))
1779b7ffbd7eSJohannes Berg 			sinfo->signal = (s8)sta->last_signal;
1780b7ffbd7eSJohannes Berg 		sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
1781b7ffbd7eSJohannes Berg 	}
1782b7ffbd7eSJohannes Berg 	if (sta->chains) {
1783b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_CHAIN_SIGNAL |
1784b7ffbd7eSJohannes Berg 				 STATION_INFO_CHAIN_SIGNAL_AVG;
1785b7ffbd7eSJohannes Berg 
1786b7ffbd7eSJohannes Berg 		sinfo->chains = sta->chains;
1787b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
1788b7ffbd7eSJohannes Berg 			sinfo->chain_signal[i] = sta->chain_signal_last[i];
1789b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
1790b7ffbd7eSJohannes Berg 				(s8) -ewma_read(&sta->chain_signal_avg[i]);
1791b7ffbd7eSJohannes Berg 		}
1792b7ffbd7eSJohannes Berg 	}
1793b7ffbd7eSJohannes Berg 
1794b7ffbd7eSJohannes Berg 	sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
1795b7ffbd7eSJohannes Berg 	sta_set_rate_info_rx(sta, &sinfo->rxrate);
1796b7ffbd7eSJohannes Berg 
1797b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
1798b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
1799b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_LLID |
1800b7ffbd7eSJohannes Berg 				 STATION_INFO_PLID |
1801b7ffbd7eSJohannes Berg 				 STATION_INFO_PLINK_STATE |
1802b7ffbd7eSJohannes Berg 				 STATION_INFO_LOCAL_PM |
1803b7ffbd7eSJohannes Berg 				 STATION_INFO_PEER_PM |
1804b7ffbd7eSJohannes Berg 				 STATION_INFO_NONPEER_PM;
1805b7ffbd7eSJohannes Berg 
1806b7ffbd7eSJohannes Berg 		sinfo->llid = sta->llid;
1807b7ffbd7eSJohannes Berg 		sinfo->plid = sta->plid;
1808b7ffbd7eSJohannes Berg 		sinfo->plink_state = sta->plink_state;
1809b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
1810b7ffbd7eSJohannes Berg 			sinfo->filled |= STATION_INFO_T_OFFSET;
1811b7ffbd7eSJohannes Berg 			sinfo->t_offset = sta->t_offset;
1812b7ffbd7eSJohannes Berg 		}
1813b7ffbd7eSJohannes Berg 		sinfo->local_pm = sta->local_pm;
1814b7ffbd7eSJohannes Berg 		sinfo->peer_pm = sta->peer_pm;
1815b7ffbd7eSJohannes Berg 		sinfo->nonpeer_pm = sta->nonpeer_pm;
1816b7ffbd7eSJohannes Berg #endif
1817b7ffbd7eSJohannes Berg 	}
1818b7ffbd7eSJohannes Berg 
1819b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
1820b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
1821b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
1822b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
1823b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
1824b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
1825b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
1826785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
1827b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
1828b7ffbd7eSJohannes Berg 
1829b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
1830b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
1831b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
1832b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
1833b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
1834b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
1835b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
1836b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
1837b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1838b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
1839b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
1840b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
1841a74a8c84SJohannes Berg 	if (sta->sta.wme)
1842b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
1843b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
1844b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
1845b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
1846b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
1847b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
1848b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
1849b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
1850b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
1851b7ffbd7eSJohannes Berg 
1852b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
1853b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
1854b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
1855b7ffbd7eSJohannes Berg 	else
1856b7ffbd7eSJohannes Berg 		thr = drv_get_expected_throughput(local, &sta->sta);
1857b7ffbd7eSJohannes Berg 
1858b7ffbd7eSJohannes Berg 	if (thr != 0) {
1859b7ffbd7eSJohannes Berg 		sinfo->filled |= STATION_INFO_EXPECTED_THROUGHPUT;
1860b7ffbd7eSJohannes Berg 		sinfo->expected_throughput = thr;
1861b7ffbd7eSJohannes Berg 	}
1862b7ffbd7eSJohannes Berg }
1863