xref: /openbmc/linux/net/mac80211/sta_info.c (revision 062f1d6de01df545ca0df366c6133f0fa164bff6)
1f0706e82SJiri Benc /*
2f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
3f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4f0706e82SJiri Benc  *
5f0706e82SJiri Benc  * This program is free software; you can redistribute it and/or modify
6f0706e82SJiri Benc  * it under the terms of the GNU General Public License version 2 as
7f0706e82SJiri Benc  * published by the Free Software Foundation.
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21f0706e82SJiri Benc #include <net/mac80211.h>
22f0706e82SJiri Benc #include "ieee80211_i.h"
2324487981SJohannes Berg #include "driver-ops.h"
242c8dccc7SJohannes Berg #include "rate.h"
25f0706e82SJiri Benc #include "sta_info.h"
26e9f207f0SJiri Benc #include "debugfs_sta.h"
27ee385855SLuis Carlos Cobo #include "mesh.h"
28ce662b44SJohannes Berg #include "wme.h"
29f0706e82SJiri Benc 
30d0709a65SJohannes Berg /**
31d0709a65SJohannes Berg  * DOC: STA information lifetime rules
32d0709a65SJohannes Berg  *
33d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
34d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
35d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
36d0709a65SJohannes Berg  *
3734e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3834e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
3934e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4034e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4134e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4234e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4334e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4434e89507SJohannes Berg  * encryption keys.
4593e5deb1SJohannes Berg  *
4693e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4793e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
48d0709a65SJohannes Berg  *
4934e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
507e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
517e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5225985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5334e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5425985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5534e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
567e189a12SLuis R. Rodriguez  *
5734e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5834e89507SJohannes Berg  * calls are available.
59d0709a65SJohannes Berg  *
6034e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6134e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6234e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6334e89507SJohannes Berg  * freed before they are done using it.
64d0709a65SJohannes Berg  */
65f0706e82SJiri Benc 
664d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
67be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
68f0706e82SJiri Benc 			     struct sta_info *sta)
69f0706e82SJiri Benc {
70f0706e82SJiri Benc 	struct sta_info *s;
71f0706e82SJiri Benc 
7240b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
734d33960bSJohannes Berg 				      lockdep_is_held(&local->sta_mtx));
74f0706e82SJiri Benc 	if (!s)
75be8755e1SMichael Wu 		return -ENOENT;
76be8755e1SMichael Wu 	if (s == sta) {
77cf778b00SEric Dumazet 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
78d0709a65SJohannes Berg 				   s->hnext);
79be8755e1SMichael Wu 		return 0;
80f0706e82SJiri Benc 	}
81f0706e82SJiri Benc 
8240b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8340b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8440b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
854d33960bSJohannes Berg 					lockdep_is_held(&local->sta_mtx));
8640b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
87cf778b00SEric Dumazet 		rcu_assign_pointer(s->hnext, sta->hnext);
88be8755e1SMichael Wu 		return 0;
89f0706e82SJiri Benc 	}
90f0706e82SJiri Benc 
91be8755e1SMichael Wu 	return -ENOENT;
92f0706e82SJiri Benc }
93f0706e82SJiri Benc 
945108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
95b22cfcfcSEliad Peller {
96b22cfcfcSEliad Peller 	int ac, i;
97b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
98b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
99b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
100d012a605SMarco Porsch 	struct ps_data *ps;
101b22cfcfcSEliad Peller 
102e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
103e3685e03SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
104d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
105d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
106d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1073f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1083f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
109d012a605SMarco Porsch 		else
110d012a605SMarco Porsch 			return;
111b22cfcfcSEliad Peller 
112b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
113e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
114b22cfcfcSEliad Peller 
115d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
116b22cfcfcSEliad Peller 		sta_info_recalc_tim(sta);
117b22cfcfcSEliad Peller 	}
118b22cfcfcSEliad Peller 
119b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
120b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1211f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1221f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
123b22cfcfcSEliad Peller 	}
124b22cfcfcSEliad Peller 
12545b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12645b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
127b22cfcfcSEliad Peller 
128b22cfcfcSEliad Peller 	cancel_work_sync(&sta->drv_unblock_wk);
129b22cfcfcSEliad Peller 
130b22cfcfcSEliad Peller 	/*
131b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
132b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
133b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
134b22cfcfcSEliad Peller 	 * directly by station destruction.
135b22cfcfcSEliad Peller 	 */
1365a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
137661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
138b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
139b22cfcfcSEliad Peller 		if (!tid_tx)
140b22cfcfcSEliad Peller 			continue;
1411f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
142b22cfcfcSEliad Peller 		kfree(tid_tx);
143b22cfcfcSEliad Peller 	}
1445108ca82SJohannes Berg }
145b22cfcfcSEliad Peller 
1465108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1475108ca82SJohannes Berg {
1485108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1495108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1505108ca82SJohannes Berg 
1515108ca82SJohannes Berg 	__cleanup_single_sta(sta);
152b22cfcfcSEliad Peller 	sta_info_free(local, sta);
153b22cfcfcSEliad Peller }
154b22cfcfcSEliad Peller 
155d0709a65SJohannes Berg /* protected by RCU */
156abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
157abe60632SJohannes Berg 			      const u8 *addr)
15843ba7e95SJohannes Berg {
159abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
16043ba7e95SJohannes Berg 	struct sta_info *sta;
16143ba7e95SJohannes Berg 
1620379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1630379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
16443ba7e95SJohannes Berg 	while (sta) {
165abe60632SJohannes Berg 		if (sta->sdata == sdata &&
166b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
16743ba7e95SJohannes Berg 			break;
1680379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1690379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
17043ba7e95SJohannes Berg 	}
17143ba7e95SJohannes Berg 	return sta;
17243ba7e95SJohannes Berg }
17343ba7e95SJohannes Berg 
1740e5ded5aSFelix Fietkau /*
1750e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1760e5ded5aSFelix Fietkau  * or from one of its vlans
1770e5ded5aSFelix Fietkau  */
1780e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1790e5ded5aSFelix Fietkau 				  const u8 *addr)
1800e5ded5aSFelix Fietkau {
1810e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1820e5ded5aSFelix Fietkau 	struct sta_info *sta;
1830e5ded5aSFelix Fietkau 
1840379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1850379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1860e5ded5aSFelix Fietkau 	while (sta) {
1870e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
188a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
189b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
1900e5ded5aSFelix Fietkau 			break;
1910379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1920379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1930e5ded5aSFelix Fietkau 	}
1940e5ded5aSFelix Fietkau 	return sta;
1950e5ded5aSFelix Fietkau }
1960e5ded5aSFelix Fietkau 
1973b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1983b53fde8SJohannes Berg 				     int idx)
199ee385855SLuis Carlos Cobo {
2003b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
201ee385855SLuis Carlos Cobo 	struct sta_info *sta;
202ee385855SLuis Carlos Cobo 	int i = 0;
203ee385855SLuis Carlos Cobo 
204d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
2053b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2062a8ca29aSLuis Carlos Cobo 			continue;
207ee385855SLuis Carlos Cobo 		if (i < idx) {
208ee385855SLuis Carlos Cobo 			++i;
209ee385855SLuis Carlos Cobo 			continue;
210ee385855SLuis Carlos Cobo 		}
2112a8ca29aSLuis Carlos Cobo 		return sta;
212ee385855SLuis Carlos Cobo 	}
213ee385855SLuis Carlos Cobo 
214ee385855SLuis Carlos Cobo 	return NULL;
215ee385855SLuis Carlos Cobo }
216f0706e82SJiri Benc 
21793e5deb1SJohannes Berg /**
218d9a7ddb0SJohannes Berg  * sta_info_free - free STA
21993e5deb1SJohannes Berg  *
2206ef307bcSRandy Dunlap  * @local: pointer to the global information
22193e5deb1SJohannes Berg  * @sta: STA info to free
22293e5deb1SJohannes Berg  *
22393e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
224d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
225d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
226d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
22793e5deb1SJohannes Berg  */
228d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
22993e5deb1SJohannes Berg {
230ad38bfc9SMatti Gottlieb 	int i;
231ad38bfc9SMatti Gottlieb 
232889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2334b7679a5SJohannes Berg 		rate_control_free_sta(sta);
23493e5deb1SJohannes Berg 
235ad38bfc9SMatti Gottlieb 	if (sta->tx_lat) {
236ad38bfc9SMatti Gottlieb 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
237ad38bfc9SMatti Gottlieb 			kfree(sta->tx_lat[i].bins);
238ad38bfc9SMatti Gottlieb 		kfree(sta->tx_lat);
239ad38bfc9SMatti Gottlieb 	}
240ad38bfc9SMatti Gottlieb 
241bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
24293e5deb1SJohannes Berg 
24393e5deb1SJohannes Berg 	kfree(sta);
24493e5deb1SJohannes Berg }
24593e5deb1SJohannes Berg 
2464d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
247d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
248d0709a65SJohannes Berg 			      struct sta_info *sta)
249f0706e82SJiri Benc {
2504d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
25117741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
252cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
253f0706e82SJiri Benc }
254f0706e82SJiri Benc 
255af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
256af818581SJohannes Berg {
257af818581SJohannes Berg 	struct sta_info *sta;
258af818581SJohannes Berg 
259af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
260af818581SJohannes Berg 
261af818581SJohannes Berg 	if (sta->dead)
262af818581SJohannes Berg 		return;
263af818581SJohannes Berg 
26454420473SHelmut Schaa 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
26554420473SHelmut Schaa 		local_bh_disable();
266af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
26754420473SHelmut Schaa 		local_bh_enable();
26854420473SHelmut Schaa 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
269c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
270ce662b44SJohannes Berg 
271ce662b44SJohannes Berg 		local_bh_disable();
272af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
273ce662b44SJohannes Berg 		local_bh_enable();
274c2c98fdeSJohannes Berg 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
275c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
276ce662b44SJohannes Berg 
277ce662b44SJohannes Berg 		local_bh_disable();
27847086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
279ce662b44SJohannes Berg 		local_bh_enable();
28050a9432dSJohannes Berg 	} else
281c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
282af818581SJohannes Berg }
283af818581SJohannes Berg 
284af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
285af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
286af65cd96SJohannes Berg {
287af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
288af65cd96SJohannes Berg 		return 0;
289af65cd96SJohannes Berg 
290889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
291af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
292af65cd96SJohannes Berg 						     &sta->sta, gfp);
293889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
294af65cd96SJohannes Berg 		return -ENOMEM;
295af65cd96SJohannes Berg 
296af65cd96SJohannes Berg 	return 0;
297af65cd96SJohannes Berg }
298af65cd96SJohannes Berg 
29973651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
30056544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
301f0706e82SJiri Benc {
302d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
303f0706e82SJiri Benc 	struct sta_info *sta;
304ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
305ad38bfc9SMatti Gottlieb 	struct ieee80211_tx_latency_bin_ranges *tx_latency;
30616c5f15cSRon Rindjunsky 	int i;
307f0706e82SJiri Benc 
30817741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
309f0706e82SJiri Benc 	if (!sta)
31073651ee6SJohannes Berg 		return NULL;
311f0706e82SJiri Benc 
312ef04a297SJohannes Berg 	rcu_read_lock();
313ef04a297SJohannes Berg 	tx_latency = rcu_dereference(local->tx_latency);
314ef04a297SJohannes Berg 	/* init stations Tx latency statistics && TID bins */
315ef04a297SJohannes Berg 	if (tx_latency) {
316ef04a297SJohannes Berg 		sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
317ef04a297SJohannes Berg 				      sizeof(struct ieee80211_tx_latency_stat),
318ef04a297SJohannes Berg 				      GFP_ATOMIC);
319ef04a297SJohannes Berg 		if (!sta->tx_lat) {
320ef04a297SJohannes Berg 			rcu_read_unlock();
321ef04a297SJohannes Berg 			goto free;
322ef04a297SJohannes Berg 		}
323ef04a297SJohannes Berg 
324ef04a297SJohannes Berg 		if (tx_latency->n_ranges) {
325ef04a297SJohannes Berg 			for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
326ef04a297SJohannes Berg 				/* size of bins is size of the ranges +1 */
327ef04a297SJohannes Berg 				sta->tx_lat[i].bin_count =
328ef04a297SJohannes Berg 					tx_latency->n_ranges + 1;
329ef04a297SJohannes Berg 				sta->tx_lat[i].bins =
330ef04a297SJohannes Berg 					kcalloc(sta->tx_lat[i].bin_count,
331ef04a297SJohannes Berg 						sizeof(u32), GFP_ATOMIC);
332ef04a297SJohannes Berg 				if (!sta->tx_lat[i].bins) {
333ef04a297SJohannes Berg 					rcu_read_unlock();
334ef04a297SJohannes Berg 					goto free;
335ef04a297SJohannes Berg 				}
336ef04a297SJohannes Berg 			}
337ef04a297SJohannes Berg 		}
338ef04a297SJohannes Berg 	}
339ef04a297SJohannes Berg 	rcu_read_unlock();
340ef04a297SJohannes Berg 
34107346f81SJohannes Berg 	spin_lock_init(&sta->lock);
3421d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
343af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
34467c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
345a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
34687f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
34787f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
34887f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
34987f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3506c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
35187f59c70SThomas Pedersen #endif
35207346f81SJohannes Berg 
35317741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
354d0709a65SJohannes Berg 	sta->local = local;
355d0709a65SJohannes Berg 	sta->sdata = sdata;
3568bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
357f0706e82SJiri Benc 
35871ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
35971ec375cSJohannes Berg 
360ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
361ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
362541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
363ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
364ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
365541a45a1SBruno Randolf 
366ef04a297SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp))
367ef04a297SJohannes Berg 		goto free;
368f0706e82SJiri Benc 
3695a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
370a622ab72SJohannes Berg 		/*
371a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
372a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
373a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
374a622ab72SJohannes Berg 		 */
37516c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
37616c5f15cSRon Rindjunsky 	}
377948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
378948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
379948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
380948d887dSJohannes Berg 	}
38173651ee6SJohannes Berg 
3825a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3834be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
384cccaec98SSenthil Balasubramanian 
385af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
386687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
387687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
388687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
389687da132SEmmanuel Grumbach 			local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
390687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
391687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
392687da132SEmmanuel Grumbach 		/*
393687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
394687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
395687da132SEmmanuel Grumbach 		 */
396687da132SEmmanuel Grumbach 		switch (smps) {
397687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
398687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
399687da132SEmmanuel Grumbach 			break;
400687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
401687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
402687da132SEmmanuel Grumbach 			break;
403687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
404687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
405687da132SEmmanuel Grumbach 			break;
406687da132SEmmanuel Grumbach 		default:
407687da132SEmmanuel Grumbach 			WARN_ON(1);
408687da132SEmmanuel Grumbach 		}
409687da132SEmmanuel Grumbach 	}
410af0ed69bSJohannes Berg 
411bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
41273651ee6SJohannes Berg 	return sta;
413ef04a297SJohannes Berg 
414ef04a297SJohannes Berg free:
415ef04a297SJohannes Berg 	if (sta->tx_lat) {
416ef04a297SJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
417ef04a297SJohannes Berg 			kfree(sta->tx_lat[i].bins);
418ef04a297SJohannes Berg 		kfree(sta->tx_lat);
419ef04a297SJohannes Berg 	}
420ef04a297SJohannes Berg 	kfree(sta);
421ef04a297SJohannes Berg 	return NULL;
42273651ee6SJohannes Berg }
42373651ee6SJohannes Berg 
4248c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
42534e89507SJohannes Berg {
42634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
42734e89507SJohannes Berg 
42803e4497eSJohannes Berg 	/*
42903e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
43003e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
43103e4497eSJohannes Berg 	 * and another CPU turns off the net device.
43203e4497eSJohannes Berg 	 */
4338c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4348c71df7aSGuy Eilam 		return -ENETDOWN;
43503e4497eSJohannes Berg 
436b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4378c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4388c71df7aSGuy Eilam 		return -EINVAL;
4398c71df7aSGuy Eilam 
4408c71df7aSGuy Eilam 	return 0;
44193e5deb1SJohannes Berg }
44244213b5eSJohannes Berg 
443f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
444f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
445f09603a2SJohannes Berg 				     struct sta_info *sta)
446f09603a2SJohannes Berg {
447f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
448f09603a2SJohannes Berg 	int err = 0;
449f09603a2SJohannes Berg 
450f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
451f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
452f09603a2SJohannes Berg 		if (err)
453f09603a2SJohannes Berg 			break;
454f09603a2SJohannes Berg 	}
455f09603a2SJohannes Berg 
456f09603a2SJohannes Berg 	if (!err) {
457a4ec45a4SJohannes Berg 		/*
458a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
459a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
460a4ec45a4SJohannes Berg 		 */
461a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
462f09603a2SJohannes Berg 			sta->uploaded = true;
463f09603a2SJohannes Berg 		return 0;
464f09603a2SJohannes Berg 	}
465f09603a2SJohannes Berg 
466f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
467bdcbd8e0SJohannes Berg 		sdata_info(sdata,
468bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
469bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
470f09603a2SJohannes Berg 		err = 0;
471f09603a2SJohannes Berg 	}
472f09603a2SJohannes Berg 
473f09603a2SJohannes Berg 	/* unwind on error */
474f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
475f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
476f09603a2SJohannes Berg 
477f09603a2SJohannes Berg 	return err;
478f09603a2SJohannes Berg }
479f09603a2SJohannes Berg 
48034e89507SJohannes Berg /*
4818c71df7aSGuy Eilam  * should be called with sta_mtx locked
4828c71df7aSGuy Eilam  * this function replaces the mutex lock
4838c71df7aSGuy Eilam  * with a RCU lock
4848c71df7aSGuy Eilam  */
4854d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4868c71df7aSGuy Eilam {
4878c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4888c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4897852e361SJohannes Berg 	struct station_info sinfo;
4908c71df7aSGuy Eilam 	int err = 0;
4918c71df7aSGuy Eilam 
4928c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4938c71df7aSGuy Eilam 
4947852e361SJohannes Berg 	/* check if STA exists already */
4957852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4964d33960bSJohannes Berg 		err = -EEXIST;
4974d33960bSJohannes Berg 		goto out_err;
49834e89507SJohannes Berg 	}
49934e89507SJohannes Berg 
5004d33960bSJohannes Berg 	local->num_sta++;
5014d33960bSJohannes Berg 	local->sta_generation++;
5024d33960bSJohannes Berg 	smp_mb();
5034d33960bSJohannes Berg 
5045108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
5055108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5065108ca82SJohannes Berg 
5074d33960bSJohannes Berg 	/* make the station visible */
5084d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
5094d33960bSJohannes Berg 
510794454ceSArik Nemtsov 	list_add_rcu(&sta->list, &local->sta_list);
51183d5cc01SJohannes Berg 
5125108ca82SJohannes Berg 	/* notify driver */
5135108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
5145108ca82SJohannes Berg 	if (err)
5155108ca82SJohannes Berg 		goto out_remove;
5165108ca82SJohannes Berg 
51783d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
5185108ca82SJohannes Berg 	/* accept BA sessions now */
5195108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5204d33960bSJohannes Berg 
52121f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
5224d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
5234d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
5244d33960bSJohannes Berg 
5254d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5264d33960bSJohannes Berg 	sinfo.filled = 0;
5274d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5284d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
529d0709a65SJohannes Berg 
530bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
531f0706e82SJiri Benc 
53234e89507SJohannes Berg 	/* move reference to rcu-protected */
53334e89507SJohannes Berg 	rcu_read_lock();
53434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
535e9f207f0SJiri Benc 
53673651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
53773651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
53873651ee6SJohannes Berg 
53973651ee6SJohannes Berg 	return 0;
5405108ca82SJohannes Berg  out_remove:
5415108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
5425108ca82SJohannes Berg 	list_del_rcu(&sta->list);
5435108ca82SJohannes Berg 	local->num_sta--;
5445108ca82SJohannes Berg 	synchronize_net();
5455108ca82SJohannes Berg 	__cleanup_single_sta(sta);
5464d33960bSJohannes Berg  out_err:
5474d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5484d33960bSJohannes Berg 	rcu_read_lock();
5494d33960bSJohannes Berg 	return err;
5508c71df7aSGuy Eilam }
5518c71df7aSGuy Eilam 
5528c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5538c71df7aSGuy Eilam {
5548c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5558c71df7aSGuy Eilam 	int err = 0;
5568c71df7aSGuy Eilam 
5574d33960bSJohannes Berg 	might_sleep();
5584d33960bSJohannes Berg 
5598c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5608c71df7aSGuy Eilam 	if (err) {
5618c71df7aSGuy Eilam 		rcu_read_lock();
5628c71df7aSGuy Eilam 		goto out_free;
5638c71df7aSGuy Eilam 	}
5648c71df7aSGuy Eilam 
565004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
566004c872eSJohannes Berg 
5674d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5688c71df7aSGuy Eilam 	if (err)
569004c872eSJohannes Berg 		goto out_free;
570d0709a65SJohannes Berg 
571004c872eSJohannes Berg 	return 0;
57293e5deb1SJohannes Berg  out_free:
57393e5deb1SJohannes Berg 	BUG_ON(!err);
574d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
57593e5deb1SJohannes Berg 	return err;
576f0706e82SJiri Benc }
577f0706e82SJiri Benc 
57834e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
57934e89507SJohannes Berg {
58034e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
58134e89507SJohannes Berg 
58234e89507SJohannes Berg 	rcu_read_unlock();
58334e89507SJohannes Berg 
58434e89507SJohannes Berg 	return err;
58534e89507SJohannes Berg }
58634e89507SJohannes Berg 
587d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
588004c872eSJohannes Berg {
589004c872eSJohannes Berg 	/*
590004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
591004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
592004c872eSJohannes Berg 	 */
593d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
594004c872eSJohannes Berg }
595004c872eSJohannes Berg 
596d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
597004c872eSJohannes Berg {
598004c872eSJohannes Berg 	/*
599004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
600004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
601004c872eSJohannes Berg 	 */
602d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
603004c872eSJohannes Berg }
604004c872eSJohannes Berg 
6053d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
6063d5839b6SIlan Peer {
6073d5839b6SIlan Peer 	/*
6083d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
6093d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
6103d5839b6SIlan Peer 	 */
6113d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
6123d5839b6SIlan Peer }
6133d5839b6SIlan Peer 
614948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
615004c872eSJohannes Berg {
616948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
617948d887dSJohannes Berg 	switch (ac) {
618948d887dSJohannes Berg 	case IEEE80211_AC_VO:
619948d887dSJohannes Berg 		return BIT(6) | BIT(7);
620948d887dSJohannes Berg 	case IEEE80211_AC_VI:
621948d887dSJohannes Berg 		return BIT(4) | BIT(5);
622948d887dSJohannes Berg 	case IEEE80211_AC_BE:
623948d887dSJohannes Berg 		return BIT(0) | BIT(3);
624948d887dSJohannes Berg 	case IEEE80211_AC_BK:
625948d887dSJohannes Berg 		return BIT(1) | BIT(2);
626948d887dSJohannes Berg 	default:
627948d887dSJohannes Berg 		WARN_ON(1);
628948d887dSJohannes Berg 		return 0;
629d0709a65SJohannes Berg 	}
630004c872eSJohannes Berg }
631004c872eSJohannes Berg 
632c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
633004c872eSJohannes Berg {
634c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
635d012a605SMarco Porsch 	struct ps_data *ps;
636948d887dSJohannes Berg 	bool indicate_tim = false;
637948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
638948d887dSJohannes Berg 	int ac;
639d012a605SMarco Porsch 	u16 id;
640004c872eSJohannes Berg 
641d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
642d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
643c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
644c868cb35SJohannes Berg 			return;
6453e122be0SJohannes Berg 
646d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
647d012a605SMarco Porsch 		id = sta->sta.aid;
6483f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6493f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6503f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
651204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6526f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6533f52b7e3SMarco Porsch #endif
654d012a605SMarco Porsch 	} else {
655d012a605SMarco Porsch 		return;
656d012a605SMarco Porsch 	}
657d012a605SMarco Porsch 
658c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
659c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
660c868cb35SJohannes Berg 		return;
661004c872eSJohannes Berg 
662c868cb35SJohannes Berg 	if (sta->dead)
663c868cb35SJohannes Berg 		goto done;
6643e122be0SJohannes Berg 
665948d887dSJohannes Berg 	/*
666948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
667948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
668948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
669948d887dSJohannes Berg 	 * non-enabled ones.
670948d887dSJohannes Berg 	 */
671948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
672948d887dSJohannes Berg 		ignore_for_tim = 0;
673948d887dSJohannes Berg 
674948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
675948d887dSJohannes Berg 		unsigned long tids;
676948d887dSJohannes Berg 
677948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
678948d887dSJohannes Berg 			continue;
679948d887dSJohannes Berg 
680948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
681948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
682948d887dSJohannes Berg 		if (indicate_tim)
683948d887dSJohannes Berg 			break;
684948d887dSJohannes Berg 
685948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
686948d887dSJohannes Berg 
687948d887dSJohannes Berg 		indicate_tim |=
688948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
689004c872eSJohannes Berg 	}
690004c872eSJohannes Berg 
691c868cb35SJohannes Berg  done:
69265f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
693004c872eSJohannes Berg 
6943d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6953d5839b6SIlan Peer 		goto out_unlock;
6963d5839b6SIlan Peer 
697948d887dSJohannes Berg 	if (indicate_tim)
698d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
699c868cb35SJohannes Berg 	else
700d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
7013e122be0SJohannes Berg 
702c868cb35SJohannes Berg 	if (local->ops->set_tim) {
703c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
704948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
705c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
706004c872eSJohannes Berg 	}
707004c872eSJohannes Berg 
7083d5839b6SIlan Peer out_unlock:
70965f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
710004c872eSJohannes Berg }
711004c872eSJohannes Berg 
712cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
713f0706e82SJiri Benc {
714e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
715f0706e82SJiri Benc 	int timeout;
716f0706e82SJiri Benc 
717f0706e82SJiri Benc 	if (!skb)
718cd0b8d89SJohannes Berg 		return false;
719f0706e82SJiri Benc 
720e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
721f0706e82SJiri Benc 
722f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
72357c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
72457c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
72557c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
726f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
727f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
728e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
729f0706e82SJiri Benc }
730f0706e82SJiri Benc 
731f0706e82SJiri Benc 
732948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
733948d887dSJohannes Berg 						struct sta_info *sta, int ac)
734f0706e82SJiri Benc {
735f0706e82SJiri Benc 	unsigned long flags;
736f0706e82SJiri Benc 	struct sk_buff *skb;
737f0706e82SJiri Benc 
73860750397SJohannes Berg 	/*
73960750397SJohannes Berg 	 * First check for frames that should expire on the filtered
74060750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
74160750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
74260750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
74360750397SJohannes Berg 	 * total_ps_buffered counter.
74460750397SJohannes Berg 	 */
745f0706e82SJiri Benc 	for (;;) {
746948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
747948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
74857c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
749948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
750836341a7SJohannes Berg 		else
751f0706e82SJiri Benc 			skb = NULL;
752948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
753f0706e82SJiri Benc 
75460750397SJohannes Berg 		/*
75560750397SJohannes Berg 		 * Frames are queued in order, so if this one
75660750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
75760750397SJohannes Berg 		 * we actually reached the end of the queue we
75860750397SJohannes Berg 		 * also need to stop, of course.
75960750397SJohannes Berg 		 */
76060750397SJohannes Berg 		if (!skb)
76160750397SJohannes Berg 			break;
762d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
76360750397SJohannes Berg 	}
76460750397SJohannes Berg 
76560750397SJohannes Berg 	/*
76660750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
76760750397SJohannes Berg 	 * only find something if the filtered queue was emptied
76860750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
76960750397SJohannes Berg 	 * buffered frames.
77060750397SJohannes Berg 	 */
771f0706e82SJiri Benc 	for (;;) {
772948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
773948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
774f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
775948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
776f0706e82SJiri Benc 		else
777f0706e82SJiri Benc 			skb = NULL;
778948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
779f0706e82SJiri Benc 
78060750397SJohannes Berg 		/*
78160750397SJohannes Berg 		 * frames are queued in order, so if this one
78260750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
78360750397SJohannes Berg 		 * the queue) we can stop testing
78460750397SJohannes Berg 		 */
785836341a7SJohannes Berg 		if (!skb)
786836341a7SJohannes Berg 			break;
787836341a7SJohannes Berg 
788f0706e82SJiri Benc 		local->total_ps_buffered--;
789bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
790bdcbd8e0SJohannes Berg 		       sta->sta.addr);
791d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
792f0706e82SJiri Benc 	}
7933393a608SJuuso Oikarinen 
79460750397SJohannes Berg 	/*
79560750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
79660750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
79760750397SJohannes Berg 	 * frames.
79860750397SJohannes Berg 	 */
79960750397SJohannes Berg 	sta_info_recalc_tim(sta);
80060750397SJohannes Berg 
80160750397SJohannes Berg 	/*
80260750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
80360750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
80460750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
80560750397SJohannes Berg 	 */
806948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
807948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
808948d887dSJohannes Berg }
809948d887dSJohannes Berg 
810948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
811948d887dSJohannes Berg 					     struct sta_info *sta)
812948d887dSJohannes Berg {
813948d887dSJohannes Berg 	bool have_buffered = false;
814948d887dSJohannes Berg 	int ac;
815948d887dSJohannes Berg 
8163f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
8173f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
8183f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
819948d887dSJohannes Berg 		return false;
820948d887dSJohannes Berg 
821948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
822948d887dSJohannes Berg 		have_buffered |=
823948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
824948d887dSJohannes Berg 
825948d887dSJohannes Berg 	return have_buffered;
826f0706e82SJiri Benc }
827f0706e82SJiri Benc 
828d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
82934e89507SJohannes Berg {
83034e89507SJohannes Berg 	struct ieee80211_local *local;
83134e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8326d10e46bSJohannes Berg 	int ret;
83334e89507SJohannes Berg 
83434e89507SJohannes Berg 	might_sleep();
83534e89507SJohannes Berg 
83634e89507SJohannes Berg 	if (!sta)
83734e89507SJohannes Berg 		return -ENOENT;
83834e89507SJohannes Berg 
83934e89507SJohannes Berg 	local = sta->local;
84034e89507SJohannes Berg 	sdata = sta->sdata;
84134e89507SJohannes Berg 
84283d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
84383d5cc01SJohannes Berg 
844098a6070SJohannes Berg 	/*
845098a6070SJohannes Berg 	 * Before removing the station from the driver and
846098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
847098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
848098a6070SJohannes Berg 	 * will be sufficient.
849098a6070SJohannes Berg 	 */
850c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
851c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
852098a6070SJohannes Berg 
85334e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
854b01711beSJohannes Berg 	if (WARN_ON(ret))
85534e89507SJohannes Berg 		return ret;
85634e89507SJohannes Berg 
857794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8584d33960bSJohannes Berg 
8596a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8606a9d1b91SJohannes Berg 
861a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
862a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
863a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
864a710c816SJohannes Berg 
865d778207bSJohannes Berg 	return 0;
866d778207bSJohannes Berg }
867d778207bSJohannes Berg 
868d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
869d778207bSJohannes Berg {
870d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
871d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
872d778207bSJohannes Berg 	int ret;
873d778207bSJohannes Berg 
874d778207bSJohannes Berg 	/*
875d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
876d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
877d778207bSJohannes Berg 	 */
878d778207bSJohannes Berg 
879d778207bSJohannes Berg 	might_sleep();
880d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
881d778207bSJohannes Berg 
882c8782078SJohannes Berg 	/* now keys can no longer be reached */
8836d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
88434e89507SJohannes Berg 
88534e89507SJohannes Berg 	sta->dead = true;
88634e89507SJohannes Berg 
88734e89507SJohannes Berg 	local->num_sta--;
88834e89507SJohannes Berg 	local->sta_generation++;
88934e89507SJohannes Berg 
89083d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
891f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
892f09603a2SJohannes Berg 		if (ret) {
89383d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
89483d5cc01SJohannes Berg 			break;
89583d5cc01SJohannes Berg 		}
89683d5cc01SJohannes Berg 	}
897d9a7ddb0SJohannes Berg 
898f09603a2SJohannes Berg 	if (sta->uploaded) {
899f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
900f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
901f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
902f09603a2SJohannes Berg 	}
90334e89507SJohannes Berg 
904bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
905bdcbd8e0SJohannes Berg 
906ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
907ec15e68bSJouni Malinen 
90834e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
90934e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
91021f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
91134e89507SJohannes Berg 
912d34ba216SJohannes Berg 	cleanup_single_sta(sta);
913d778207bSJohannes Berg }
914d778207bSJohannes Berg 
915d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
916d778207bSJohannes Berg {
917d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
918d778207bSJohannes Berg 
919d778207bSJohannes Berg 	if (err)
920d778207bSJohannes Berg 		return err;
921d778207bSJohannes Berg 
922d778207bSJohannes Berg 	synchronize_net();
923d778207bSJohannes Berg 
924d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
92534e89507SJohannes Berg 
92634e89507SJohannes Berg 	return 0;
92734e89507SJohannes Berg }
92834e89507SJohannes Berg 
92934e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
93034e89507SJohannes Berg {
93134e89507SJohannes Berg 	struct sta_info *sta;
93234e89507SJohannes Berg 	int ret;
93334e89507SJohannes Berg 
93434e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9357852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
93634e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
93734e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
93834e89507SJohannes Berg 
93934e89507SJohannes Berg 	return ret;
94034e89507SJohannes Berg }
94134e89507SJohannes Berg 
94234e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
94334e89507SJohannes Berg 			      const u8 *addr)
94434e89507SJohannes Berg {
94534e89507SJohannes Berg 	struct sta_info *sta;
94634e89507SJohannes Berg 	int ret;
94734e89507SJohannes Berg 
94834e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9497852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
95034e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
95134e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
95234e89507SJohannes Berg 
95334e89507SJohannes Berg 	return ret;
95434e89507SJohannes Berg }
955f0706e82SJiri Benc 
956f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
957f0706e82SJiri Benc {
958f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
959f0706e82SJiri Benc 	struct sta_info *sta;
9603393a608SJuuso Oikarinen 	bool timer_needed = false;
961f0706e82SJiri Benc 
962d0709a65SJohannes Berg 	rcu_read_lock();
963d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9643393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9653393a608SJuuso Oikarinen 			timer_needed = true;
966d0709a65SJohannes Berg 	rcu_read_unlock();
967f0706e82SJiri Benc 
9685bb644a0SJohannes Berg 	if (local->quiescing)
9695bb644a0SJohannes Berg 		return;
9705bb644a0SJohannes Berg 
9713393a608SJuuso Oikarinen 	if (!timer_needed)
9723393a608SJuuso Oikarinen 		return;
9733393a608SJuuso Oikarinen 
97426d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
97526d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
976f0706e82SJiri Benc }
977f0706e82SJiri Benc 
978f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
979f0706e82SJiri Benc {
9804d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
98134e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
982f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
983f0706e82SJiri Benc 
984b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
985b24b8a24SPavel Emelyanov 		    (unsigned long)local);
986f0706e82SJiri Benc }
987f0706e82SJiri Benc 
988f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
989f0706e82SJiri Benc {
990a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
991f0706e82SJiri Benc }
992f0706e82SJiri Benc 
993051007d9SJohannes Berg 
994e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
995f0706e82SJiri Benc {
996b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
997f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
998d778207bSJohannes Berg 	LIST_HEAD(free_list);
99944213b5eSJohannes Berg 	int ret = 0;
1000f0706e82SJiri Benc 
1001d0709a65SJohannes Berg 	might_sleep();
1002d0709a65SJohannes Berg 
1003e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1004e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1005e716251dSJohannes Berg 
100634e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
100734e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1008e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1009e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1010d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1011d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
101234316837SJohannes Berg 			ret++;
101334316837SJohannes Berg 		}
101434e89507SJohannes Berg 	}
1015d778207bSJohannes Berg 
1016d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1017d778207bSJohannes Berg 		synchronize_net();
1018d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1019d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1020d778207bSJohannes Berg 	}
102134e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
102244213b5eSJohannes Berg 
1023051007d9SJohannes Berg 	return ret;
1024051007d9SJohannes Berg }
1025051007d9SJohannes Berg 
102624723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
102724723d1bSJohannes Berg 			  unsigned long exp_time)
102824723d1bSJohannes Berg {
102924723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
103024723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
103124723d1bSJohannes Berg 
103234e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1033e46a2cf9SMohammed Shafi Shajakhan 
1034e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1035ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1036ec2b774eSMarek Lindner 			continue;
1037ec2b774eSMarek Lindner 
103824723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1039eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1040bdcbd8e0SJohannes Berg 				sta->sta.addr);
10413f52b7e3SMarco Porsch 
10423f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10433f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10443f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10453f52b7e3SMarco Porsch 
104634e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
104724723d1bSJohannes Berg 		}
1048e46a2cf9SMohammed Shafi Shajakhan 	}
1049e46a2cf9SMohammed Shafi Shajakhan 
105034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
105124723d1bSJohannes Berg }
105217741cdcSJohannes Berg 
1053686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1054686b9cb9SBen Greear 					       const u8 *addr,
1055686b9cb9SBen Greear 					       const u8 *localaddr)
105617741cdcSJohannes Berg {
1057abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
105817741cdcSJohannes Berg 
1059686b9cb9SBen Greear 	/*
1060686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1061686b9cb9SBen Greear 	 * ... first in list.
1062686b9cb9SBen Greear 	 */
1063f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1064686b9cb9SBen Greear 		if (localaddr &&
1065b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1066686b9cb9SBen Greear 			continue;
1067f7c65594SJohannes Berg 		if (!sta->uploaded)
1068f7c65594SJohannes Berg 			return NULL;
106917741cdcSJohannes Berg 		return &sta->sta;
1070f7c65594SJohannes Berg 	}
1071f7c65594SJohannes Berg 
1072abe60632SJohannes Berg 	return NULL;
107317741cdcSJohannes Berg }
1074686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10755ed176e1SJohannes Berg 
10765ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10775ed176e1SJohannes Berg 					 const u8 *addr)
10785ed176e1SJohannes Berg {
1079f7c65594SJohannes Berg 	struct sta_info *sta;
10805ed176e1SJohannes Berg 
10815ed176e1SJohannes Berg 	if (!vif)
10825ed176e1SJohannes Berg 		return NULL;
10835ed176e1SJohannes Berg 
1084f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1085f7c65594SJohannes Berg 	if (!sta)
1086f7c65594SJohannes Berg 		return NULL;
10875ed176e1SJohannes Berg 
1088f7c65594SJohannes Berg 	if (!sta->uploaded)
1089f7c65594SJohannes Berg 		return NULL;
1090f7c65594SJohannes Berg 
1091f7c65594SJohannes Berg 	return &sta->sta;
10925ed176e1SJohannes Berg }
109317741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1094af818581SJohannes Berg 
1095e3685e03SJohannes Berg /* powersave support code */
1096e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
109750a9432dSJohannes Berg {
1098608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1099e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1100e3685e03SJohannes Berg 	struct sk_buff_head pending;
1101e3685e03SJohannes Berg 	int filtered = 0, buffered = 0, ac;
1102e3685e03SJohannes Berg 	unsigned long flags;
1103d012a605SMarco Porsch 	struct ps_data *ps;
1104d012a605SMarco Porsch 
1105d012a605SMarco Porsch 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
1106d012a605SMarco Porsch 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1107d012a605SMarco Porsch 		ps = &sdata->bss->ps;
11083f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
11093f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1110d012a605SMarco Porsch 	else
1111d012a605SMarco Porsch 		return;
111250a9432dSJohannes Berg 
1113c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
111447086fc5SJohannes Berg 
11155a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1116948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1117948d887dSJohannes Berg 
1118d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
111912375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1120af818581SJohannes Berg 
1121948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1122af818581SJohannes Berg 
11231d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
11241d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1125af818581SJohannes Berg 	/* Send all buffered frames to the station */
1126948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1127948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1128948d887dSJohannes Berg 
1129987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1130948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1131987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1132948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1133948d887dSJohannes Berg 		filtered += tmp - count;
1134948d887dSJohannes Berg 		count = tmp;
1135948d887dSJohannes Berg 
1136987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1137948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1138987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1139948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1140948d887dSJohannes Berg 		buffered += tmp - count;
1141948d887dSJohannes Berg 	}
1142948d887dSJohannes Berg 
1143e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
1144e3685e03SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1145e3685e03SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_STA);
11461d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1147948d887dSJohannes Berg 
1148e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1149e3685e03SJohannes Berg 
1150687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1151*062f1d6dSChun-Yeow Yeoh 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1152*062f1d6dSChun-Yeow Yeoh 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1153687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1154687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1155687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1156687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1157687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1158687da132SEmmanuel Grumbach 		       sta->sta.addr);
1159687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1160687da132SEmmanuel Grumbach 					   sta->sta.addr,
1161687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1162687da132SEmmanuel Grumbach 	}
1163687da132SEmmanuel Grumbach 
1164af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1165af818581SJohannes Berg 
1166c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1167c868cb35SJohannes Berg 
1168bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1169bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1170bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1171af818581SJohannes Berg }
1172af818581SJohannes Berg 
1173ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1174ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1175b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
1176b77cf4f8SJohannes Berg 					 bool call_driver)
1177ce662b44SJohannes Berg {
1178ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1179ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1180ce662b44SJohannes Berg 	struct sk_buff *skb;
1181ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1182ce662b44SJohannes Berg 	__le16 fc;
1183c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1184ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
118555de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1186ce662b44SJohannes Berg 
1187ce662b44SJohannes Berg 	if (qos) {
1188ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1189ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1190ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1191ce662b44SJohannes Berg 	} else {
1192ce662b44SJohannes Berg 		size -= 2;
1193ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1194ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1195ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1196ce662b44SJohannes Berg 	}
1197ce662b44SJohannes Berg 
1198ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1199ce662b44SJohannes Berg 	if (!skb)
1200ce662b44SJohannes Berg 		return;
1201ce662b44SJohannes Berg 
1202ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1203ce662b44SJohannes Berg 
1204ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1205ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1206ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1207ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1208ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1209ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1210864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1211ce662b44SJohannes Berg 
1212ce662b44SJohannes Berg 	skb->priority = tid;
1213ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
121459b66255SJohannes Berg 	if (qos) {
1215ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1216ce662b44SJohannes Berg 
121740b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1218ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1219ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1220ce662b44SJohannes Berg 	}
1221ce662b44SJohannes Berg 
1222ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1223ce662b44SJohannes Berg 
1224ce662b44SJohannes Berg 	/*
1225ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1226ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1227deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1228deeaee19SJohannes Berg 	 * ends the poll/service period.
1229ce662b44SJohannes Berg 	 */
123002f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1231d6d23de2SFelix Fietkau 		       IEEE80211_TX_CTL_PS_RESPONSE |
1232deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1233ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1234ce662b44SJohannes Berg 
1235b77cf4f8SJohannes Berg 	if (call_driver)
1236b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1237b77cf4f8SJohannes Berg 					  reason, false);
123840b96408SJohannes Berg 
123989afe614SJohannes Berg 	skb->dev = sdata->dev;
124089afe614SJohannes Berg 
124155de908aSJohannes Berg 	rcu_read_lock();
124255de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
124355de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
124455de908aSJohannes Berg 		rcu_read_unlock();
124555de908aSJohannes Berg 		kfree_skb(skb);
124655de908aSJohannes Berg 		return;
124755de908aSJohannes Berg 	}
124855de908aSJohannes Berg 
12494bf88530SJohannes Berg 	ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
125055de908aSJohannes Berg 	rcu_read_unlock();
1251ce662b44SJohannes Berg }
1252ce662b44SJohannes Berg 
12530a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
12540a1cb809SJohannes Berg {
12550a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
12560a1cb809SJohannes Berg 	if (tids & 0xF8)
12570a1cb809SJohannes Berg 		return fls(tids) - 1;
12580a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
12590a1cb809SJohannes Berg 	if (tids & BIT(0))
12600a1cb809SJohannes Berg 		return 0;
12610a1cb809SJohannes Berg 	return fls(tids) - 1;
12620a1cb809SJohannes Berg }
12630a1cb809SJohannes Berg 
126447086fc5SJohannes Berg static void
126547086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
126647086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
126747086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1268af818581SJohannes Berg {
1269af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1270af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1271948d887dSJohannes Berg 	bool more_data = false;
1272948d887dSJohannes Berg 	int ac;
12734049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
127447086fc5SJohannes Berg 	struct sk_buff_head frames;
127547086fc5SJohannes Berg 
1276deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1277c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1278deeaee19SJohannes Berg 
127947086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1280af818581SJohannes Berg 
1281f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1282948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
12834049e09aSJohannes Berg 		unsigned long tids;
12844049e09aSJohannes Berg 
128547086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1286948d887dSJohannes Berg 			continue;
1287948d887dSJohannes Berg 
12884049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
12894049e09aSJohannes Berg 
1290f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1291f9f760b4SJohannes Berg 		 * release from hardware queues
1292f9f760b4SJohannes Berg 		 */
1293f9f760b4SJohannes Berg 		if (skb_queue_empty(&frames))
1294f9f760b4SJohannes Berg 			driver_release_tids |= sta->driver_buffered_tids & tids;
1295f9f760b4SJohannes Berg 
12964049e09aSJohannes Berg 		if (driver_release_tids) {
1297f9f760b4SJohannes Berg 			/* If the driver has data on more than one TID then
1298f9f760b4SJohannes Berg 			 * certainly there's more data if we release just a
1299f9f760b4SJohannes Berg 			 * single frame now (from a single TID). This will
1300f9f760b4SJohannes Berg 			 * only happen for PS-Poll.
1301f9f760b4SJohannes Berg 			 */
1302f9f760b4SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1303f9f760b4SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
1304f9f760b4SJohannes Berg 				more_data = true;
1305f9f760b4SJohannes Berg 				driver_release_tids =
1306f9f760b4SJohannes Berg 					BIT(find_highest_prio_tid(
1307f9f760b4SJohannes Berg 						driver_release_tids));
1308f9f760b4SJohannes Berg 				break;
1309f9f760b4SJohannes Berg 			}
13104049e09aSJohannes Berg 		} else {
131147086fc5SJohannes Berg 			struct sk_buff *skb;
131247086fc5SJohannes Berg 
131347086fc5SJohannes Berg 			while (n_frames > 0) {
1314948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1315948d887dSJohannes Berg 				if (!skb) {
131647086fc5SJohannes Berg 					skb = skb_dequeue(
131747086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1318af818581SJohannes Berg 					if (skb)
1319af818581SJohannes Berg 						local->total_ps_buffered--;
1320af818581SJohannes Berg 				}
132147086fc5SJohannes Berg 				if (!skb)
132247086fc5SJohannes Berg 					break;
132347086fc5SJohannes Berg 				n_frames--;
132447086fc5SJohannes Berg 				__skb_queue_tail(&frames, skb);
132547086fc5SJohannes Berg 			}
1326948d887dSJohannes Berg 		}
1327948d887dSJohannes Berg 
1328f9f760b4SJohannes Berg 		/* If we have more frames buffered on this AC, then set the
1329f9f760b4SJohannes Berg 		 * more-data bit and abort the loop since we can't send more
1330f9f760b4SJohannes Berg 		 * data from other ACs before the buffered frames from this.
13314049e09aSJohannes Berg 		 */
1332948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1333948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1334948d887dSJohannes Berg 			more_data = true;
1335948d887dSJohannes Berg 			break;
1336948d887dSJohannes Berg 		}
1337948d887dSJohannes Berg 	}
1338af818581SJohannes Berg 
1339f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1340ce662b44SJohannes Berg 		int tid;
13414049e09aSJohannes Berg 
1342ce662b44SJohannes Berg 		/*
1343ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1344ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1345ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1346ce662b44SJohannes Berg 		 *
1347ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1348ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1349ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1350ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1351ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1352ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1353ce662b44SJohannes Berg 		 *
1354ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1355ce662b44SJohannes Berg 		 */
1356ce662b44SJohannes Berg 
1357ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1358ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1359ce662b44SJohannes Berg 
1360b77cf4f8SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason, true);
1361f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
136247086fc5SJohannes Berg 		struct sk_buff_head pending;
136347086fc5SJohannes Berg 		struct sk_buff *skb;
136440b96408SJohannes Berg 		int num = 0;
136540b96408SJohannes Berg 		u16 tids = 0;
1366b77cf4f8SJohannes Berg 		bool need_null = false;
136747086fc5SJohannes Berg 
136847086fc5SJohannes Berg 		skb_queue_head_init(&pending);
136947086fc5SJohannes Berg 
137047086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1371af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
137247086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
137340b96408SJohannes Berg 			u8 *qoshdr = NULL;
137440b96408SJohannes Berg 
137540b96408SJohannes Berg 			num++;
1376af818581SJohannes Berg 
1377af818581SJohannes Berg 			/*
137847086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
137947086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
138047086fc5SJohannes Berg 			 * exchange.
1381af818581SJohannes Berg 			 */
1382d6d23de2SFelix Fietkau 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1383d6d23de2SFelix Fietkau 				       IEEE80211_TX_CTL_PS_RESPONSE;
1384af818581SJohannes Berg 
138547086fc5SJohannes Berg 			/*
138647086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
138747086fc5SJohannes Berg 			 * more buffered frames for this STA
138847086fc5SJohannes Berg 			 */
138924b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
139047086fc5SJohannes Berg 				hdr->frame_control |=
139147086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
139224b9c373SJanusz.Dziedzic@tieto.com 			else
139324b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
139424b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1395af818581SJohannes Berg 
139640b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
139740b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
139840b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
139940b96408SJohannes Berg 
1400b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1401b77cf4f8SJohannes Berg 
1402b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1403b77cf4f8SJohannes Berg 
1404b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1405b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1406b77cf4f8SJohannes Berg 				continue;
1407b77cf4f8SJohannes Berg 
1408b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1409b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1410b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1411b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1412b77cf4f8SJohannes Berg 				break;
1413b77cf4f8SJohannes Berg 			}
1414b77cf4f8SJohannes Berg 
1415b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1416b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1417b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1418b77cf4f8SJohannes Berg 			 * and be done.
1419b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1420b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1421b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1422b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1423b77cf4f8SJohannes Berg 			 *
1424b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1425b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1426b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1427b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1428b77cf4f8SJohannes Berg 			 *
1429b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1430b77cf4f8SJohannes Berg 			 */
1431b77cf4f8SJohannes Berg 			if (qoshdr) {
143240b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1433deeaee19SJohannes Berg 
143447086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
143547086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1436b77cf4f8SJohannes Berg 			} else {
1437b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1438b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1439b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1440b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1441b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1442b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1443b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1444b77cf4f8SJohannes Berg 				 */
1445b77cf4f8SJohannes Berg 				hdr->frame_control |=
1446b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1447b77cf4f8SJohannes Berg 				need_null = true;
1448b77cf4f8SJohannes Berg 				num++;
144952a3f20cSMarco Porsch 			}
1450b77cf4f8SJohannes Berg 			break;
145147086fc5SJohannes Berg 		}
145247086fc5SJohannes Berg 
145340b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
145440b96408SJohannes Berg 					  reason, more_data);
145540b96408SJohannes Berg 
145647086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1457af818581SJohannes Berg 
1458b77cf4f8SJohannes Berg 		if (need_null)
1459b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
1460b77cf4f8SJohannes Berg 				sdata, sta, find_highest_prio_tid(tids),
1461b77cf4f8SJohannes Berg 				reason, false);
1462b77cf4f8SJohannes Berg 
1463c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1464af818581SJohannes Berg 	} else {
1465af818581SJohannes Berg 		/*
14664049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
14674049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1468f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1469f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1470f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1471f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1472f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1473f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1474af818581SJohannes Berg 		 */
14754049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
147647086fc5SJohannes Berg 					    n_frames, reason, more_data);
14774049e09aSJohannes Berg 
14784049e09aSJohannes Berg 		/*
14794049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
14804049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1481f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
14824049e09aSJohannes Berg 		 * release function.
1483f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
14844049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
14854049e09aSJohannes Berg 		 */
1486af818581SJohannes Berg 	}
1487af818581SJohannes Berg }
1488af818581SJohannes Berg 
1489af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1490af818581SJohannes Berg {
149147086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1492af818581SJohannes Berg 
1493af818581SJohannes Berg 	/*
149447086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
149547086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
149647086fc5SJohannes Berg 	 * only from the non-enabled ones.
1497af818581SJohannes Berg 	 */
149847086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
149947086fc5SJohannes Berg 		ignore_for_response = 0;
1500af818581SJohannes Berg 
150147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
150247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1503af818581SJohannes Berg }
150447086fc5SJohannes Berg 
150547086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
150647086fc5SJohannes Berg {
150747086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
150847086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
150947086fc5SJohannes Berg 
151047086fc5SJohannes Berg 	/*
151147086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
151247086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
151347086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
151447086fc5SJohannes Berg 	 * actually getting called.
151547086fc5SJohannes Berg 	 */
151647086fc5SJohannes Berg 	if (!delivery_enabled)
151747086fc5SJohannes Berg 		return;
151847086fc5SJohannes Berg 
151947086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
152047086fc5SJohannes Berg 	case 1:
152147086fc5SJohannes Berg 		n_frames = 2;
152247086fc5SJohannes Berg 		break;
152347086fc5SJohannes Berg 	case 2:
152447086fc5SJohannes Berg 		n_frames = 4;
152547086fc5SJohannes Berg 		break;
152647086fc5SJohannes Berg 	case 3:
152747086fc5SJohannes Berg 		n_frames = 6;
152847086fc5SJohannes Berg 		break;
152947086fc5SJohannes Berg 	case 0:
153047086fc5SJohannes Berg 		/* XXX: what is a good value? */
153147086fc5SJohannes Berg 		n_frames = 8;
153247086fc5SJohannes Berg 		break;
153347086fc5SJohannes Berg 	}
153447086fc5SJohannes Berg 
153547086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
153647086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1537af818581SJohannes Berg }
1538af818581SJohannes Berg 
1539af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1540af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1541af818581SJohannes Berg {
1542af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1543af818581SJohannes Berg 
1544b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1545b5878a2dSJohannes Berg 
1546af818581SJohannes Berg 	if (block)
1547c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1548c2c98fdeSJohannes Berg 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1549af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1550af818581SJohannes Berg }
1551af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1552dcf55fb5SFelix Fietkau 
1553e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
155437fbd908SJohannes Berg {
155537fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
155637fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
155737fbd908SJohannes Berg 
155837fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
155937fbd908SJohannes Berg 
156037fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
156137fbd908SJohannes Berg }
1562e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
156337fbd908SJohannes Berg 
1564042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1565042ec453SJohannes Berg 				u8 tid, bool buffered)
1566dcf55fb5SFelix Fietkau {
1567dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1568dcf55fb5SFelix Fietkau 
15695a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1570042ec453SJohannes Berg 		return;
1571042ec453SJohannes Berg 
15721b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
15731b000789SJohannes Berg 
1574948d887dSJohannes Berg 	if (buffered)
1575948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1576948d887dSJohannes Berg 	else
1577948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1578948d887dSJohannes Berg 
1579c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1580dcf55fb5SFelix Fietkau }
1581042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1582d9a7ddb0SJohannes Berg 
158383d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1584d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1585d9a7ddb0SJohannes Berg {
15868bf11d8dSJohannes Berg 	might_sleep();
1587d9a7ddb0SJohannes Berg 
1588d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1589d9a7ddb0SJohannes Berg 		return 0;
1590d9a7ddb0SJohannes Berg 
1591f09603a2SJohannes Berg 	/* check allowed transitions first */
1592f09603a2SJohannes Berg 
1593d9a7ddb0SJohannes Berg 	switch (new_state) {
1594d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1595f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1596d9a7ddb0SJohannes Berg 			return -EINVAL;
1597d9a7ddb0SJohannes Berg 		break;
1598d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1599f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1600f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1601d9a7ddb0SJohannes Berg 			return -EINVAL;
1602d9a7ddb0SJohannes Berg 		break;
1603d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1604f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1605f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1606d9a7ddb0SJohannes Berg 			return -EINVAL;
1607d9a7ddb0SJohannes Berg 		break;
1608d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1609f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1610d9a7ddb0SJohannes Berg 			return -EINVAL;
1611d9a7ddb0SJohannes Berg 		break;
1612d9a7ddb0SJohannes Berg 	default:
1613d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1614d9a7ddb0SJohannes Berg 		return -EINVAL;
1615d9a7ddb0SJohannes Berg 	}
1616d9a7ddb0SJohannes Berg 
1617bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1618bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1619f09603a2SJohannes Berg 
1620f09603a2SJohannes Berg 	/*
1621f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1622f09603a2SJohannes Berg 	 * fail the transition
1623f09603a2SJohannes Berg 	 */
1624f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1625f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1626f09603a2SJohannes Berg 					sta->sta_state, new_state);
1627f09603a2SJohannes Berg 		if (err)
1628f09603a2SJohannes Berg 			return err;
1629f09603a2SJohannes Berg 	}
1630f09603a2SJohannes Berg 
1631f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1632f09603a2SJohannes Berg 
1633f09603a2SJohannes Berg 	switch (new_state) {
1634f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1635f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1636f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1637f09603a2SJohannes Berg 		break;
1638f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1639f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1640f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1641f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1642f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1643f09603a2SJohannes Berg 		break;
1644f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1645f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1646f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1647f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
16487e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16497e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16507e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16517e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1652f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1653f09603a2SJohannes Berg 		}
1654f09603a2SJohannes Berg 		break;
1655f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1656f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
16577e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
16587e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
16597e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
16607e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1661f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1662f09603a2SJohannes Berg 		}
1663f09603a2SJohannes Berg 		break;
1664f09603a2SJohannes Berg 	default:
1665f09603a2SJohannes Berg 		break;
1666f09603a2SJohannes Berg 	}
1667f09603a2SJohannes Berg 
1668d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1669d9a7ddb0SJohannes Berg 
1670d9a7ddb0SJohannes Berg 	return 0;
1671d9a7ddb0SJohannes Berg }
1672687da132SEmmanuel Grumbach 
1673687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1674687da132SEmmanuel Grumbach {
1675687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1676687da132SEmmanuel Grumbach 	u8 rx_streams;
1677687da132SEmmanuel Grumbach 
1678687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1679687da132SEmmanuel Grumbach 		return 1;
1680687da132SEmmanuel Grumbach 
1681687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1682687da132SEmmanuel Grumbach 		int i;
1683687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1684687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1685687da132SEmmanuel Grumbach 
1686687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1687687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1688687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1689687da132SEmmanuel Grumbach 				return i + 1;
1690687da132SEmmanuel Grumbach 	}
1691687da132SEmmanuel Grumbach 
1692687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1693687da132SEmmanuel Grumbach 		rx_streams = 4;
1694687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1695687da132SEmmanuel Grumbach 		rx_streams = 3;
1696687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1697687da132SEmmanuel Grumbach 		rx_streams = 2;
1698687da132SEmmanuel Grumbach 	else
1699687da132SEmmanuel Grumbach 		rx_streams = 1;
1700687da132SEmmanuel Grumbach 
1701687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1702687da132SEmmanuel Grumbach 		return rx_streams;
1703687da132SEmmanuel Grumbach 
1704687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1705687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1706687da132SEmmanuel Grumbach }
1707