xref: /openbmc/linux/net/mac80211/sta_info.c (revision e716251d776ce92eb5169522f565ada3deed2a2a)
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 
9497f97b1fSJohannes 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 
102b22cfcfcSEliad Peller 	if (test_sta_flag(sta, WLAN_STA_PS_STA)) {
103d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
104d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
105d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1063f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1073f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
108d012a605SMarco Porsch 		else
109d012a605SMarco Porsch 			return;
110b22cfcfcSEliad Peller 
111b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
112b22cfcfcSEliad Peller 
113d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
114b22cfcfcSEliad Peller 		sta_info_recalc_tim(sta);
115b22cfcfcSEliad Peller 	}
116b22cfcfcSEliad Peller 
117b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
118b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1191f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1201f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
121b22cfcfcSEliad Peller 	}
122b22cfcfcSEliad Peller 
12345b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
12445b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
125b22cfcfcSEliad Peller 
126b22cfcfcSEliad Peller 	cancel_work_sync(&sta->drv_unblock_wk);
127b22cfcfcSEliad Peller 
128b22cfcfcSEliad Peller 	/*
129b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
130b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
131b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
132b22cfcfcSEliad Peller 	 * directly by station destruction.
133b22cfcfcSEliad Peller 	 */
1345a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
135661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
136b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
137b22cfcfcSEliad Peller 		if (!tid_tx)
138b22cfcfcSEliad Peller 			continue;
1391f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
140b22cfcfcSEliad Peller 		kfree(tid_tx);
141b22cfcfcSEliad Peller 	}
142b22cfcfcSEliad Peller 
143b22cfcfcSEliad Peller 	sta_info_free(local, sta);
144b22cfcfcSEliad Peller }
145b22cfcfcSEliad Peller 
146d0709a65SJohannes Berg /* protected by RCU */
147abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
148abe60632SJohannes Berg 			      const u8 *addr)
14943ba7e95SJohannes Berg {
150abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
15143ba7e95SJohannes Berg 	struct sta_info *sta;
15243ba7e95SJohannes Berg 
1530379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1540379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
15543ba7e95SJohannes Berg 	while (sta) {
156abe60632SJohannes Berg 		if (sta->sdata == sdata &&
157b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
15843ba7e95SJohannes Berg 			break;
1590379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1600379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
16143ba7e95SJohannes Berg 	}
16243ba7e95SJohannes Berg 	return sta;
16343ba7e95SJohannes Berg }
16443ba7e95SJohannes Berg 
1650e5ded5aSFelix Fietkau /*
1660e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1670e5ded5aSFelix Fietkau  * or from one of its vlans
1680e5ded5aSFelix Fietkau  */
1690e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1700e5ded5aSFelix Fietkau 				  const u8 *addr)
1710e5ded5aSFelix Fietkau {
1720e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1730e5ded5aSFelix Fietkau 	struct sta_info *sta;
1740e5ded5aSFelix Fietkau 
1750379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1760379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1770e5ded5aSFelix Fietkau 	while (sta) {
1780e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
179a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
180b203ca39SJoe Perches 		    ether_addr_equal(sta->sta.addr, addr))
1810e5ded5aSFelix Fietkau 			break;
1820379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1830379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1840e5ded5aSFelix Fietkau 	}
1850e5ded5aSFelix Fietkau 	return sta;
1860e5ded5aSFelix Fietkau }
1870e5ded5aSFelix Fietkau 
1883b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1893b53fde8SJohannes Berg 				     int idx)
190ee385855SLuis Carlos Cobo {
1913b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
192ee385855SLuis Carlos Cobo 	struct sta_info *sta;
193ee385855SLuis Carlos Cobo 	int i = 0;
194ee385855SLuis Carlos Cobo 
195d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1963b53fde8SJohannes Berg 		if (sdata != sta->sdata)
1972a8ca29aSLuis Carlos Cobo 			continue;
198ee385855SLuis Carlos Cobo 		if (i < idx) {
199ee385855SLuis Carlos Cobo 			++i;
200ee385855SLuis Carlos Cobo 			continue;
201ee385855SLuis Carlos Cobo 		}
2022a8ca29aSLuis Carlos Cobo 		return sta;
203ee385855SLuis Carlos Cobo 	}
204ee385855SLuis Carlos Cobo 
205ee385855SLuis Carlos Cobo 	return NULL;
206ee385855SLuis Carlos Cobo }
207f0706e82SJiri Benc 
20893e5deb1SJohannes Berg /**
209d9a7ddb0SJohannes Berg  * sta_info_free - free STA
21093e5deb1SJohannes Berg  *
2116ef307bcSRandy Dunlap  * @local: pointer to the global information
21293e5deb1SJohannes Berg  * @sta: STA info to free
21393e5deb1SJohannes Berg  *
21493e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
215d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
216d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
217d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
21893e5deb1SJohannes Berg  */
219d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
22093e5deb1SJohannes Berg {
221ad38bfc9SMatti Gottlieb 	int i;
222ad38bfc9SMatti Gottlieb 
223889cbb91SJohannes Berg 	if (sta->rate_ctrl)
2244b7679a5SJohannes Berg 		rate_control_free_sta(sta);
22593e5deb1SJohannes Berg 
226ad38bfc9SMatti Gottlieb 	if (sta->tx_lat) {
227ad38bfc9SMatti Gottlieb 		for (i = 0; i < IEEE80211_NUM_TIDS; i++)
228ad38bfc9SMatti Gottlieb 			kfree(sta->tx_lat[i].bins);
229ad38bfc9SMatti Gottlieb 		kfree(sta->tx_lat);
230ad38bfc9SMatti Gottlieb 	}
231ad38bfc9SMatti Gottlieb 
232bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
23393e5deb1SJohannes Berg 
23493e5deb1SJohannes Berg 	kfree(sta);
23593e5deb1SJohannes Berg }
23693e5deb1SJohannes Berg 
2374d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
238d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
239d0709a65SJohannes Berg 			      struct sta_info *sta)
240f0706e82SJiri Benc {
2414d33960bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
24217741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
243cf778b00SEric Dumazet 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
244f0706e82SJiri Benc }
245f0706e82SJiri Benc 
246af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
247af818581SJohannes Berg {
248af818581SJohannes Berg 	struct sta_info *sta;
249af818581SJohannes Berg 
250af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
251af818581SJohannes Berg 
252af818581SJohannes Berg 	if (sta->dead)
253af818581SJohannes Berg 		return;
254af818581SJohannes Berg 
25554420473SHelmut Schaa 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
25654420473SHelmut Schaa 		local_bh_disable();
257af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
25854420473SHelmut Schaa 		local_bh_enable();
25954420473SHelmut Schaa 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
260c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
261ce662b44SJohannes Berg 
262ce662b44SJohannes Berg 		local_bh_disable();
263af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
264ce662b44SJohannes Berg 		local_bh_enable();
265c2c98fdeSJohannes Berg 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
266c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
267ce662b44SJohannes Berg 
268ce662b44SJohannes Berg 		local_bh_disable();
26947086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
270ce662b44SJohannes Berg 		local_bh_enable();
27150a9432dSJohannes Berg 	} else
272c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
273af818581SJohannes Berg }
274af818581SJohannes Berg 
275af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
276af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
277af65cd96SJohannes Berg {
278af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
279af65cd96SJohannes Berg 		return 0;
280af65cd96SJohannes Berg 
281889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
282af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
283af65cd96SJohannes Berg 						     &sta->sta, gfp);
284889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
285af65cd96SJohannes Berg 		return -ENOMEM;
286af65cd96SJohannes Berg 
287af65cd96SJohannes Berg 	return 0;
288af65cd96SJohannes Berg }
289af65cd96SJohannes Berg 
29073651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
29156544160SJohannes Berg 				const u8 *addr, gfp_t gfp)
292f0706e82SJiri Benc {
293d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
294f0706e82SJiri Benc 	struct sta_info *sta;
295ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
296ad38bfc9SMatti Gottlieb 	struct ieee80211_tx_latency_bin_ranges *tx_latency;
29716c5f15cSRon Rindjunsky 	int i;
298f0706e82SJiri Benc 
29917741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
300f0706e82SJiri Benc 	if (!sta)
30173651ee6SJohannes Berg 		return NULL;
302f0706e82SJiri Benc 
30307346f81SJohannes Berg 	spin_lock_init(&sta->lock);
304af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
30567c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
306a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
30787f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
30887f59c70SThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif) &&
30987f59c70SThomas Pedersen 	    !sdata->u.mesh.user_mpm)
31087f59c70SThomas Pedersen 		init_timer(&sta->plink_timer);
3116c7c4cbfSThomas Pedersen 	sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
31287f59c70SThomas Pedersen #endif
31307346f81SJohannes Berg 
31417741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
315d0709a65SJohannes Berg 	sta->local = local;
316d0709a65SJohannes Berg 	sta->sdata = sdata;
3178bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
318f0706e82SJiri Benc 
31971ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
32071ec375cSJohannes Berg 
321ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
322ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
323541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
324ef0621e8SFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
325ef0621e8SFelix Fietkau 		ewma_init(&sta->chain_signal_avg[i], 1024, 8);
326541a45a1SBruno Randolf 
327af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
328f0706e82SJiri Benc 		kfree(sta);
32973651ee6SJohannes Berg 		return NULL;
330f0706e82SJiri Benc 	}
331f0706e82SJiri Benc 
3325a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
333a622ab72SJohannes Berg 		/*
334a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
335a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
336a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
337a622ab72SJohannes Berg 		 */
33816c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
33916c5f15cSRon Rindjunsky 	}
340948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
341948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
342948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
343948d887dSJohannes Berg 	}
34473651ee6SJohannes Berg 
3455a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
3464be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
347cccaec98SSenthil Balasubramanian 
348af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
349687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
350687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
351687da132SEmmanuel Grumbach 		struct ieee80211_supported_band *sband =
352687da132SEmmanuel Grumbach 			local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
353687da132SEmmanuel Grumbach 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
354687da132SEmmanuel Grumbach 				IEEE80211_HT_CAP_SM_PS_SHIFT;
355687da132SEmmanuel Grumbach 		/*
356687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
357687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
358687da132SEmmanuel Grumbach 		 */
359687da132SEmmanuel Grumbach 		switch (smps) {
360687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
361687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
362687da132SEmmanuel Grumbach 			break;
363687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
364687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
365687da132SEmmanuel Grumbach 			break;
366687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
367687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
368687da132SEmmanuel Grumbach 			break;
369687da132SEmmanuel Grumbach 		default:
370687da132SEmmanuel Grumbach 			WARN_ON(1);
371687da132SEmmanuel Grumbach 		}
372687da132SEmmanuel Grumbach 	}
373af0ed69bSJohannes Berg 
374ad38bfc9SMatti Gottlieb 	rcu_read_lock();
375ad38bfc9SMatti Gottlieb 
376ad38bfc9SMatti Gottlieb 	tx_latency = rcu_dereference(local->tx_latency);
377ad38bfc9SMatti Gottlieb 	/* init stations Tx latency statistics && TID bins */
378ad38bfc9SMatti Gottlieb 	if (tx_latency)
379ad38bfc9SMatti Gottlieb 		sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
380ad38bfc9SMatti Gottlieb 				      sizeof(struct ieee80211_tx_latency_stat),
381ad38bfc9SMatti Gottlieb 				      GFP_ATOMIC);
382ad38bfc9SMatti Gottlieb 
383ad38bfc9SMatti Gottlieb 	/*
384ad38bfc9SMatti Gottlieb 	 * if Tx latency and bins are enabled and the previous allocation
385ad38bfc9SMatti Gottlieb 	 * succeeded
386ad38bfc9SMatti Gottlieb 	 */
387ad38bfc9SMatti Gottlieb 	if (tx_latency && tx_latency->n_ranges && sta->tx_lat)
388ad38bfc9SMatti Gottlieb 		for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
389ad38bfc9SMatti Gottlieb 			/* size of bins is size of the ranges +1 */
390ad38bfc9SMatti Gottlieb 			sta->tx_lat[i].bin_count =
391ad38bfc9SMatti Gottlieb 				tx_latency->n_ranges + 1;
392ad38bfc9SMatti Gottlieb 			sta->tx_lat[i].bins  = kcalloc(sta->tx_lat[i].bin_count,
393ad38bfc9SMatti Gottlieb 						       sizeof(u32),
394ad38bfc9SMatti Gottlieb 						       GFP_ATOMIC);
395ad38bfc9SMatti Gottlieb 		}
396ad38bfc9SMatti Gottlieb 
397ad38bfc9SMatti Gottlieb 	rcu_read_unlock();
398ad38bfc9SMatti Gottlieb 
399bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
40073651ee6SJohannes Berg 
40173651ee6SJohannes Berg 	return sta;
40273651ee6SJohannes Berg }
40373651ee6SJohannes Berg 
4048c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
40534e89507SJohannes Berg {
40634e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
40734e89507SJohannes Berg 
40803e4497eSJohannes Berg 	/*
40903e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
41003e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
41103e4497eSJohannes Berg 	 * and another CPU turns off the net device.
41203e4497eSJohannes Berg 	 */
4138c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4148c71df7aSGuy Eilam 		return -ENETDOWN;
41503e4497eSJohannes Berg 
416b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
4178c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4188c71df7aSGuy Eilam 		return -EINVAL;
4198c71df7aSGuy Eilam 
4208c71df7aSGuy Eilam 	return 0;
42193e5deb1SJohannes Berg }
42244213b5eSJohannes Berg 
423f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
424f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
425f09603a2SJohannes Berg 				     struct sta_info *sta)
426f09603a2SJohannes Berg {
427f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
428f09603a2SJohannes Berg 	int err = 0;
429f09603a2SJohannes Berg 
430f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
431f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
432f09603a2SJohannes Berg 		if (err)
433f09603a2SJohannes Berg 			break;
434f09603a2SJohannes Berg 	}
435f09603a2SJohannes Berg 
436f09603a2SJohannes Berg 	if (!err) {
437a4ec45a4SJohannes Berg 		/*
438a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
439a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
440a4ec45a4SJohannes Berg 		 */
441a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
442f09603a2SJohannes Berg 			sta->uploaded = true;
443f09603a2SJohannes Berg 		return 0;
444f09603a2SJohannes Berg 	}
445f09603a2SJohannes Berg 
446f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
447bdcbd8e0SJohannes Berg 		sdata_info(sdata,
448bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
449bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
450f09603a2SJohannes Berg 		err = 0;
451f09603a2SJohannes Berg 	}
452f09603a2SJohannes Berg 
453f09603a2SJohannes Berg 	/* unwind on error */
454f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
455f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
456f09603a2SJohannes Berg 
457f09603a2SJohannes Berg 	return err;
458f09603a2SJohannes Berg }
459f09603a2SJohannes Berg 
46034e89507SJohannes Berg /*
4618c71df7aSGuy Eilam  * should be called with sta_mtx locked
4628c71df7aSGuy Eilam  * this function replaces the mutex lock
4638c71df7aSGuy Eilam  * with a RCU lock
4648c71df7aSGuy Eilam  */
4654d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
4668c71df7aSGuy Eilam {
4678c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4688c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4697852e361SJohannes Berg 	struct station_info sinfo;
4708c71df7aSGuy Eilam 	int err = 0;
4718c71df7aSGuy Eilam 
4728c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
4738c71df7aSGuy Eilam 
4747852e361SJohannes Berg 	/* check if STA exists already */
4757852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
4764d33960bSJohannes Berg 		err = -EEXIST;
4774d33960bSJohannes Berg 		goto out_err;
47834e89507SJohannes Berg 	}
47934e89507SJohannes Berg 
4804d33960bSJohannes Berg 	/* notify driver */
481f09603a2SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
482f09603a2SJohannes Berg 	if (err)
483f09603a2SJohannes Berg 		goto out_err;
4844d33960bSJohannes Berg 
4854d33960bSJohannes Berg 	local->num_sta++;
4864d33960bSJohannes Berg 	local->sta_generation++;
4874d33960bSJohannes Berg 	smp_mb();
4884d33960bSJohannes Berg 
4894d33960bSJohannes Berg 	/* make the station visible */
4904d33960bSJohannes Berg 	sta_info_hash_add(local, sta);
4914d33960bSJohannes Berg 
492794454ceSArik Nemtsov 	list_add_rcu(&sta->list, &local->sta_list);
49383d5cc01SJohannes Berg 
49483d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
4954d33960bSJohannes Berg 
49621f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
4974d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
4984d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
4994d33960bSJohannes Berg 
5004d33960bSJohannes Berg 	memset(&sinfo, 0, sizeof(sinfo));
5014d33960bSJohannes Berg 	sinfo.filled = 0;
5024d33960bSJohannes Berg 	sinfo.generation = local->sta_generation;
5034d33960bSJohannes Berg 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
504d0709a65SJohannes Berg 
505bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
506f0706e82SJiri Benc 
50734e89507SJohannes Berg 	/* move reference to rcu-protected */
50834e89507SJohannes Berg 	rcu_read_lock();
50934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
510e9f207f0SJiri Benc 
51173651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
51273651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
51373651ee6SJohannes Berg 
51473651ee6SJohannes Berg 	return 0;
5154d33960bSJohannes Berg  out_err:
5164d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
5174d33960bSJohannes Berg 	rcu_read_lock();
5184d33960bSJohannes Berg 	return err;
5198c71df7aSGuy Eilam }
5208c71df7aSGuy Eilam 
5218c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5228c71df7aSGuy Eilam {
5238c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5248c71df7aSGuy Eilam 	int err = 0;
5258c71df7aSGuy Eilam 
5264d33960bSJohannes Berg 	might_sleep();
5274d33960bSJohannes Berg 
5288c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5298c71df7aSGuy Eilam 	if (err) {
5308c71df7aSGuy Eilam 		rcu_read_lock();
5318c71df7aSGuy Eilam 		goto out_free;
5328c71df7aSGuy Eilam 	}
5338c71df7aSGuy Eilam 
534004c872eSJohannes Berg 	mutex_lock(&local->sta_mtx);
535004c872eSJohannes Berg 
5364d33960bSJohannes Berg 	err = sta_info_insert_finish(sta);
5378c71df7aSGuy Eilam 	if (err)
538004c872eSJohannes Berg 		goto out_free;
539d0709a65SJohannes Berg 
540004c872eSJohannes Berg 	return 0;
54193e5deb1SJohannes Berg  out_free:
54293e5deb1SJohannes Berg 	BUG_ON(!err);
543d9a7ddb0SJohannes Berg 	sta_info_free(local, sta);
54493e5deb1SJohannes Berg 	return err;
545f0706e82SJiri Benc }
546f0706e82SJiri Benc 
54734e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
54834e89507SJohannes Berg {
54934e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
55034e89507SJohannes Berg 
55134e89507SJohannes Berg 	rcu_read_unlock();
55234e89507SJohannes Berg 
55334e89507SJohannes Berg 	return err;
55434e89507SJohannes Berg }
55534e89507SJohannes Berg 
556d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
557004c872eSJohannes Berg {
558004c872eSJohannes Berg 	/*
559004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
560004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
561004c872eSJohannes Berg 	 */
562d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
563004c872eSJohannes Berg }
564004c872eSJohannes Berg 
565d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
566004c872eSJohannes Berg {
567004c872eSJohannes Berg 	/*
568004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
569004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
570004c872eSJohannes Berg 	 */
571d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
572004c872eSJohannes Berg }
573004c872eSJohannes Berg 
5743d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
5753d5839b6SIlan Peer {
5763d5839b6SIlan Peer 	/*
5773d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
5783d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
5793d5839b6SIlan Peer 	 */
5803d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
5813d5839b6SIlan Peer }
5823d5839b6SIlan Peer 
583948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
584004c872eSJohannes Berg {
585948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
586948d887dSJohannes Berg 	switch (ac) {
587948d887dSJohannes Berg 	case IEEE80211_AC_VO:
588948d887dSJohannes Berg 		return BIT(6) | BIT(7);
589948d887dSJohannes Berg 	case IEEE80211_AC_VI:
590948d887dSJohannes Berg 		return BIT(4) | BIT(5);
591948d887dSJohannes Berg 	case IEEE80211_AC_BE:
592948d887dSJohannes Berg 		return BIT(0) | BIT(3);
593948d887dSJohannes Berg 	case IEEE80211_AC_BK:
594948d887dSJohannes Berg 		return BIT(1) | BIT(2);
595948d887dSJohannes Berg 	default:
596948d887dSJohannes Berg 		WARN_ON(1);
597948d887dSJohannes Berg 		return 0;
598d0709a65SJohannes Berg 	}
599004c872eSJohannes Berg }
600004c872eSJohannes Berg 
601c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
602004c872eSJohannes Berg {
603c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
604d012a605SMarco Porsch 	struct ps_data *ps;
605948d887dSJohannes Berg 	bool indicate_tim = false;
606948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
607948d887dSJohannes Berg 	int ac;
608d012a605SMarco Porsch 	u16 id;
609004c872eSJohannes Berg 
610d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
611d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
612c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
613c868cb35SJohannes Berg 			return;
6143e122be0SJohannes Berg 
615d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
616d012a605SMarco Porsch 		id = sta->sta.aid;
6173f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
6183f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
6193f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
620204d1304SThomas Pedersen 		/* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
6216f101ef0SChun-Yeow Yeoh 		id = sta->plid % (IEEE80211_MAX_AID + 1);
6223f52b7e3SMarco Porsch #endif
623d012a605SMarco Porsch 	} else {
624d012a605SMarco Porsch 		return;
625d012a605SMarco Porsch 	}
626d012a605SMarco Porsch 
627c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
628c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
629c868cb35SJohannes Berg 		return;
630004c872eSJohannes Berg 
631c868cb35SJohannes Berg 	if (sta->dead)
632c868cb35SJohannes Berg 		goto done;
6333e122be0SJohannes Berg 
634948d887dSJohannes Berg 	/*
635948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
636948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
637948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
638948d887dSJohannes Berg 	 * non-enabled ones.
639948d887dSJohannes Berg 	 */
640948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
641948d887dSJohannes Berg 		ignore_for_tim = 0;
642948d887dSJohannes Berg 
643948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
644948d887dSJohannes Berg 		unsigned long tids;
645948d887dSJohannes Berg 
646948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
647948d887dSJohannes Berg 			continue;
648948d887dSJohannes Berg 
649948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
650948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
651948d887dSJohannes Berg 		if (indicate_tim)
652948d887dSJohannes Berg 			break;
653948d887dSJohannes Berg 
654948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
655948d887dSJohannes Berg 
656948d887dSJohannes Berg 		indicate_tim |=
657948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
658004c872eSJohannes Berg 	}
659004c872eSJohannes Berg 
660c868cb35SJohannes Berg  done:
66165f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
662004c872eSJohannes Berg 
6633d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
6643d5839b6SIlan Peer 		goto out_unlock;
6653d5839b6SIlan Peer 
666948d887dSJohannes Berg 	if (indicate_tim)
667d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
668c868cb35SJohannes Berg 	else
669d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
6703e122be0SJohannes Berg 
671c868cb35SJohannes Berg 	if (local->ops->set_tim) {
672c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
673948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
674c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
675004c872eSJohannes Berg 	}
676004c872eSJohannes Berg 
6773d5839b6SIlan Peer out_unlock:
67865f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
679004c872eSJohannes Berg }
680004c872eSJohannes Berg 
681cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
682f0706e82SJiri Benc {
683e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
684f0706e82SJiri Benc 	int timeout;
685f0706e82SJiri Benc 
686f0706e82SJiri Benc 	if (!skb)
687cd0b8d89SJohannes Berg 		return false;
688f0706e82SJiri Benc 
689e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
690f0706e82SJiri Benc 
691f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
69257c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
69357c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
69457c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
695f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
696f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
697e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
698f0706e82SJiri Benc }
699f0706e82SJiri Benc 
700f0706e82SJiri Benc 
701948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
702948d887dSJohannes Berg 						struct sta_info *sta, int ac)
703f0706e82SJiri Benc {
704f0706e82SJiri Benc 	unsigned long flags;
705f0706e82SJiri Benc 	struct sk_buff *skb;
706f0706e82SJiri Benc 
70760750397SJohannes Berg 	/*
70860750397SJohannes Berg 	 * First check for frames that should expire on the filtered
70960750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
71060750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
71160750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
71260750397SJohannes Berg 	 * total_ps_buffered counter.
71360750397SJohannes Berg 	 */
714f0706e82SJiri Benc 	for (;;) {
715948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
716948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
71757c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
718948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
719836341a7SJohannes Berg 		else
720f0706e82SJiri Benc 			skb = NULL;
721948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
722f0706e82SJiri Benc 
72360750397SJohannes Berg 		/*
72460750397SJohannes Berg 		 * Frames are queued in order, so if this one
72560750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
72660750397SJohannes Berg 		 * we actually reached the end of the queue we
72760750397SJohannes Berg 		 * also need to stop, of course.
72860750397SJohannes Berg 		 */
72960750397SJohannes Berg 		if (!skb)
73060750397SJohannes Berg 			break;
731d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
73260750397SJohannes Berg 	}
73360750397SJohannes Berg 
73460750397SJohannes Berg 	/*
73560750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
73660750397SJohannes Berg 	 * only find something if the filtered queue was emptied
73760750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
73860750397SJohannes Berg 	 * buffered frames.
73960750397SJohannes Berg 	 */
740f0706e82SJiri Benc 	for (;;) {
741948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
742948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
743f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
744948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
745f0706e82SJiri Benc 		else
746f0706e82SJiri Benc 			skb = NULL;
747948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
748f0706e82SJiri Benc 
74960750397SJohannes Berg 		/*
75060750397SJohannes Berg 		 * frames are queued in order, so if this one
75160750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
75260750397SJohannes Berg 		 * the queue) we can stop testing
75360750397SJohannes Berg 		 */
754836341a7SJohannes Berg 		if (!skb)
755836341a7SJohannes Berg 			break;
756836341a7SJohannes Berg 
757f0706e82SJiri Benc 		local->total_ps_buffered--;
758bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
759bdcbd8e0SJohannes Berg 		       sta->sta.addr);
760d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
761f0706e82SJiri Benc 	}
7623393a608SJuuso Oikarinen 
76360750397SJohannes Berg 	/*
76460750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
76560750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
76660750397SJohannes Berg 	 * frames.
76760750397SJohannes Berg 	 */
76860750397SJohannes Berg 	sta_info_recalc_tim(sta);
76960750397SJohannes Berg 
77060750397SJohannes Berg 	/*
77160750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
77260750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
77360750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
77460750397SJohannes Berg 	 */
775948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
776948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
777948d887dSJohannes Berg }
778948d887dSJohannes Berg 
779948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
780948d887dSJohannes Berg 					     struct sta_info *sta)
781948d887dSJohannes Berg {
782948d887dSJohannes Berg 	bool have_buffered = false;
783948d887dSJohannes Berg 	int ac;
784948d887dSJohannes Berg 
7853f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
7863f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
7873f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
788948d887dSJohannes Berg 		return false;
789948d887dSJohannes Berg 
790948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
791948d887dSJohannes Berg 		have_buffered |=
792948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
793948d887dSJohannes Berg 
794948d887dSJohannes Berg 	return have_buffered;
795f0706e82SJiri Benc }
796f0706e82SJiri Benc 
797d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
79834e89507SJohannes Berg {
79934e89507SJohannes Berg 	struct ieee80211_local *local;
80034e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
8016d10e46bSJohannes Berg 	int ret;
80234e89507SJohannes Berg 
80334e89507SJohannes Berg 	might_sleep();
80434e89507SJohannes Berg 
80534e89507SJohannes Berg 	if (!sta)
80634e89507SJohannes Berg 		return -ENOENT;
80734e89507SJohannes Berg 
80834e89507SJohannes Berg 	local = sta->local;
80934e89507SJohannes Berg 	sdata = sta->sdata;
81034e89507SJohannes Berg 
81183d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
81283d5cc01SJohannes Berg 
813098a6070SJohannes Berg 	/*
814098a6070SJohannes Berg 	 * Before removing the station from the driver and
815098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
816098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
817098a6070SJohannes Berg 	 * will be sufficient.
818098a6070SJohannes Berg 	 */
819c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
820c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
821098a6070SJohannes Berg 
82234e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
823b01711beSJohannes Berg 	if (WARN_ON(ret))
82434e89507SJohannes Berg 		return ret;
82534e89507SJohannes Berg 
826794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
8274d33960bSJohannes Berg 
8286a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
8296a9d1b91SJohannes Berg 
830a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
831a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
832a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
833a710c816SJohannes Berg 
834d778207bSJohannes Berg 	return 0;
835d778207bSJohannes Berg }
836d778207bSJohannes Berg 
837d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
838d778207bSJohannes Berg {
839d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
840d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
841d778207bSJohannes Berg 	int ret;
842d778207bSJohannes Berg 
843d778207bSJohannes Berg 	/*
844d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
845d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
846d778207bSJohannes Berg 	 */
847d778207bSJohannes Berg 
848d778207bSJohannes Berg 	might_sleep();
849d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
850d778207bSJohannes Berg 
851c8782078SJohannes Berg 	/* now keys can no longer be reached */
8526d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
85334e89507SJohannes Berg 
85434e89507SJohannes Berg 	sta->dead = true;
85534e89507SJohannes Berg 
85634e89507SJohannes Berg 	local->num_sta--;
85734e89507SJohannes Berg 	local->sta_generation++;
85834e89507SJohannes Berg 
85983d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
860f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
861f09603a2SJohannes Berg 		if (ret) {
86283d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
86383d5cc01SJohannes Berg 			break;
86483d5cc01SJohannes Berg 		}
86583d5cc01SJohannes Berg 	}
866d9a7ddb0SJohannes Berg 
867f09603a2SJohannes Berg 	if (sta->uploaded) {
868f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
869f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
870f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
871f09603a2SJohannes Berg 	}
87234e89507SJohannes Berg 
873bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
874bdcbd8e0SJohannes Berg 
875ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
876ec15e68bSJouni Malinen 
87734e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
87834e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
87921f659bfSEliad Peller 	ieee80211_recalc_min_chandef(sdata);
88034e89507SJohannes Berg 
881d34ba216SJohannes Berg 	cleanup_single_sta(sta);
882d778207bSJohannes Berg }
883d778207bSJohannes Berg 
884d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
885d778207bSJohannes Berg {
886d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
887d778207bSJohannes Berg 
888d778207bSJohannes Berg 	if (err)
889d778207bSJohannes Berg 		return err;
890d778207bSJohannes Berg 
891d778207bSJohannes Berg 	synchronize_net();
892d778207bSJohannes Berg 
893d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
89434e89507SJohannes Berg 
89534e89507SJohannes Berg 	return 0;
89634e89507SJohannes Berg }
89734e89507SJohannes Berg 
89834e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
89934e89507SJohannes Berg {
90034e89507SJohannes Berg 	struct sta_info *sta;
90134e89507SJohannes Berg 	int ret;
90234e89507SJohannes Berg 
90334e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9047852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
90534e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
90634e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
90734e89507SJohannes Berg 
90834e89507SJohannes Berg 	return ret;
90934e89507SJohannes Berg }
91034e89507SJohannes Berg 
91134e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
91234e89507SJohannes Berg 			      const u8 *addr)
91334e89507SJohannes Berg {
91434e89507SJohannes Berg 	struct sta_info *sta;
91534e89507SJohannes Berg 	int ret;
91634e89507SJohannes Berg 
91734e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9187852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
91934e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
92034e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
92134e89507SJohannes Berg 
92234e89507SJohannes Berg 	return ret;
92334e89507SJohannes Berg }
924f0706e82SJiri Benc 
925f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
926f0706e82SJiri Benc {
927f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
928f0706e82SJiri Benc 	struct sta_info *sta;
9293393a608SJuuso Oikarinen 	bool timer_needed = false;
930f0706e82SJiri Benc 
931d0709a65SJohannes Berg 	rcu_read_lock();
932d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9333393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9343393a608SJuuso Oikarinen 			timer_needed = true;
935d0709a65SJohannes Berg 	rcu_read_unlock();
936f0706e82SJiri Benc 
9375bb644a0SJohannes Berg 	if (local->quiescing)
9385bb644a0SJohannes Berg 		return;
9395bb644a0SJohannes Berg 
9403393a608SJuuso Oikarinen 	if (!timer_needed)
9413393a608SJuuso Oikarinen 		return;
9423393a608SJuuso Oikarinen 
94326d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
94426d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
945f0706e82SJiri Benc }
946f0706e82SJiri Benc 
947f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
948f0706e82SJiri Benc {
9494d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
95034e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
951f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
952f0706e82SJiri Benc 
953b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
954b24b8a24SPavel Emelyanov 		    (unsigned long)local);
955f0706e82SJiri Benc }
956f0706e82SJiri Benc 
957f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
958f0706e82SJiri Benc {
959a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
960f0706e82SJiri Benc }
961f0706e82SJiri Benc 
962051007d9SJohannes Berg 
963*e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
964f0706e82SJiri Benc {
965b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
966f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
967d778207bSJohannes Berg 	LIST_HEAD(free_list);
96844213b5eSJohannes Berg 	int ret = 0;
969f0706e82SJiri Benc 
970d0709a65SJohannes Berg 	might_sleep();
971d0709a65SJohannes Berg 
972*e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
973*e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
974*e716251dSJohannes Berg 
97534e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
97634e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
977*e716251dSJohannes Berg 		if (sdata == sta->sdata ||
978*e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
979d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
980d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
98134316837SJohannes Berg 			ret++;
98234316837SJohannes Berg 		}
98334e89507SJohannes Berg 	}
984d778207bSJohannes Berg 
985d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
986d778207bSJohannes Berg 		synchronize_net();
987d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
988d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
989d778207bSJohannes Berg 	}
99034e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
99144213b5eSJohannes Berg 
992051007d9SJohannes Berg 	return ret;
993051007d9SJohannes Berg }
994051007d9SJohannes Berg 
99524723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
99624723d1bSJohannes Berg 			  unsigned long exp_time)
99724723d1bSJohannes Berg {
99824723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
99924723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
100024723d1bSJohannes Berg 
100134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1002e46a2cf9SMohammed Shafi Shajakhan 
1003e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1004ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1005ec2b774eSMarek Lindner 			continue;
1006ec2b774eSMarek Lindner 
100724723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
1008eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1009bdcbd8e0SJohannes Berg 				sta->sta.addr);
10103f52b7e3SMarco Porsch 
10113f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
10123f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
10133f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
10143f52b7e3SMarco Porsch 
101534e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
101624723d1bSJohannes Berg 		}
1017e46a2cf9SMohammed Shafi Shajakhan 	}
1018e46a2cf9SMohammed Shafi Shajakhan 
101934e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
102024723d1bSJohannes Berg }
102117741cdcSJohannes Berg 
1022686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1023686b9cb9SBen Greear 					       const u8 *addr,
1024686b9cb9SBen Greear 					       const u8 *localaddr)
102517741cdcSJohannes Berg {
1026abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
102717741cdcSJohannes Berg 
1028686b9cb9SBen Greear 	/*
1029686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1030686b9cb9SBen Greear 	 * ... first in list.
1031686b9cb9SBen Greear 	 */
1032f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1033686b9cb9SBen Greear 		if (localaddr &&
1034b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1035686b9cb9SBen Greear 			continue;
1036f7c65594SJohannes Berg 		if (!sta->uploaded)
1037f7c65594SJohannes Berg 			return NULL;
103817741cdcSJohannes Berg 		return &sta->sta;
1039f7c65594SJohannes Berg 	}
1040f7c65594SJohannes Berg 
1041abe60632SJohannes Berg 	return NULL;
104217741cdcSJohannes Berg }
1043686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10445ed176e1SJohannes Berg 
10455ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10465ed176e1SJohannes Berg 					 const u8 *addr)
10475ed176e1SJohannes Berg {
1048f7c65594SJohannes Berg 	struct sta_info *sta;
10495ed176e1SJohannes Berg 
10505ed176e1SJohannes Berg 	if (!vif)
10515ed176e1SJohannes Berg 		return NULL;
10525ed176e1SJohannes Berg 
1053f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1054f7c65594SJohannes Berg 	if (!sta)
1055f7c65594SJohannes Berg 		return NULL;
10565ed176e1SJohannes Berg 
1057f7c65594SJohannes Berg 	if (!sta->uploaded)
1058f7c65594SJohannes Berg 		return NULL;
1059f7c65594SJohannes Berg 
1060f7c65594SJohannes Berg 	return &sta->sta;
10615ed176e1SJohannes Berg }
106217741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1063af818581SJohannes Berg 
106450a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta)
106550a9432dSJohannes Berg {
106650a9432dSJohannes Berg 	struct sta_info *sta = _sta;
1067608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1068d012a605SMarco Porsch 	struct ps_data *ps;
1069d012a605SMarco Porsch 
1070d012a605SMarco Porsch 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
1071d012a605SMarco Porsch 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1072d012a605SMarco Porsch 		ps = &sdata->bss->ps;
10733f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
10743f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1075d012a605SMarco Porsch 	else
1076d012a605SMarco Porsch 		return;
107750a9432dSJohannes Berg 
1078c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1079608383bfSHelmut Schaa 	if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA))
1080d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
108150a9432dSJohannes Berg }
108250a9432dSJohannes Berg 
1083af818581SJohannes Berg /* powersave support code */
1084af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1085af818581SJohannes Berg {
1086af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1087af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1088948d887dSJohannes Berg 	struct sk_buff_head pending;
1089948d887dSJohannes Berg 	int filtered = 0, buffered = 0, ac;
1090987c285cSArik Nemtsov 	unsigned long flags;
1091af818581SJohannes Berg 
1092c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
109347086fc5SJohannes Berg 
10945a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1095948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1096948d887dSJohannes Berg 
1097d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
109812375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1099af818581SJohannes Berg 
1100948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1101af818581SJohannes Berg 
1102af818581SJohannes Berg 	/* Send all buffered frames to the station */
1103948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1104948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1105948d887dSJohannes Berg 
1106987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1107948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1108987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1109948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1110948d887dSJohannes Berg 		filtered += tmp - count;
1111948d887dSJohannes Berg 		count = tmp;
1112948d887dSJohannes Berg 
1113987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1114948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1115987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1116948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1117948d887dSJohannes Berg 		buffered += tmp - count;
1118948d887dSJohannes Berg 	}
1119948d887dSJohannes Berg 
1120948d887dSJohannes Berg 	ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
1121948d887dSJohannes Berg 
1122687da132SEmmanuel Grumbach 	/* This station just woke up and isn't aware of our SMPS state */
1123687da132SEmmanuel Grumbach 	if (!ieee80211_smps_is_restrictive(sta->known_smps_mode,
1124687da132SEmmanuel Grumbach 					   sdata->smps_mode) &&
1125687da132SEmmanuel Grumbach 	    sta->known_smps_mode != sdata->bss->req_smps &&
1126687da132SEmmanuel Grumbach 	    sta_info_tx_streams(sta) != 1) {
1127687da132SEmmanuel Grumbach 		ht_dbg(sdata,
1128687da132SEmmanuel Grumbach 		       "%pM just woke up and MIMO capable - update SMPS\n",
1129687da132SEmmanuel Grumbach 		       sta->sta.addr);
1130687da132SEmmanuel Grumbach 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1131687da132SEmmanuel Grumbach 					   sta->sta.addr,
1132687da132SEmmanuel Grumbach 					   sdata->vif.bss_conf.bssid);
1133687da132SEmmanuel Grumbach 	}
1134687da132SEmmanuel Grumbach 
1135af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1136af818581SJohannes Berg 
1137c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1138c868cb35SJohannes Berg 
1139bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
1140bdcbd8e0SJohannes Berg 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1141bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1142af818581SJohannes Berg }
1143af818581SJohannes Berg 
1144ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1145ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
114640b96408SJohannes Berg 					 enum ieee80211_frame_release_type reason)
1147ce662b44SJohannes Berg {
1148ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1149ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1150ce662b44SJohannes Berg 	struct sk_buff *skb;
1151ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1152ce662b44SJohannes Berg 	__le16 fc;
1153c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1154ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
115555de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1156ce662b44SJohannes Berg 
1157ce662b44SJohannes Berg 	if (qos) {
1158ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1159ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1160ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1161ce662b44SJohannes Berg 	} else {
1162ce662b44SJohannes Berg 		size -= 2;
1163ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1164ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1165ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1166ce662b44SJohannes Berg 	}
1167ce662b44SJohannes Berg 
1168ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1169ce662b44SJohannes Berg 	if (!skb)
1170ce662b44SJohannes Berg 		return;
1171ce662b44SJohannes Berg 
1172ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1173ce662b44SJohannes Berg 
1174ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1175ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1176ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1177ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1178ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1179ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1180ce662b44SJohannes Berg 
1181ce662b44SJohannes Berg 	skb->priority = tid;
1182ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
118359b66255SJohannes Berg 	if (qos) {
1184ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1185ce662b44SJohannes Berg 
118640b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1187ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1188ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1189ce662b44SJohannes Berg 	}
1190ce662b44SJohannes Berg 
1191ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1192ce662b44SJohannes Berg 
1193ce662b44SJohannes Berg 	/*
1194ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1195ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1196deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1197deeaee19SJohannes Berg 	 * ends the poll/service period.
1198ce662b44SJohannes Berg 	 */
119902f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1200d6d23de2SFelix Fietkau 		       IEEE80211_TX_CTL_PS_RESPONSE |
1201deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1202ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1203ce662b44SJohannes Berg 
120440b96408SJohannes Berg 	drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
120540b96408SJohannes Berg 
120689afe614SJohannes Berg 	skb->dev = sdata->dev;
120789afe614SJohannes Berg 
120855de908aSJohannes Berg 	rcu_read_lock();
120955de908aSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
121055de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
121155de908aSJohannes Berg 		rcu_read_unlock();
121255de908aSJohannes Berg 		kfree_skb(skb);
121355de908aSJohannes Berg 		return;
121455de908aSJohannes Berg 	}
121555de908aSJohannes Berg 
12164bf88530SJohannes Berg 	ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
121755de908aSJohannes Berg 	rcu_read_unlock();
1218ce662b44SJohannes Berg }
1219ce662b44SJohannes Berg 
122047086fc5SJohannes Berg static void
122147086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
122247086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
122347086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1224af818581SJohannes Berg {
1225af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1226af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
12274049e09aSJohannes Berg 	bool found = false;
1228948d887dSJohannes Berg 	bool more_data = false;
1229948d887dSJohannes Berg 	int ac;
12304049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
123147086fc5SJohannes Berg 	struct sk_buff_head frames;
123247086fc5SJohannes Berg 
1233deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1234c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1235deeaee19SJohannes Berg 
123647086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1237af818581SJohannes Berg 
1238948d887dSJohannes Berg 	/*
123947086fc5SJohannes Berg 	 * Get response frame(s) and more data bit for it.
1240948d887dSJohannes Berg 	 */
1241948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
12424049e09aSJohannes Berg 		unsigned long tids;
12434049e09aSJohannes Berg 
124447086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1245948d887dSJohannes Berg 			continue;
1246948d887dSJohannes Berg 
12474049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
12484049e09aSJohannes Berg 
12494049e09aSJohannes Berg 		if (!found) {
12504049e09aSJohannes Berg 			driver_release_tids = sta->driver_buffered_tids & tids;
12514049e09aSJohannes Berg 			if (driver_release_tids) {
12524049e09aSJohannes Berg 				found = true;
12534049e09aSJohannes Berg 			} else {
125447086fc5SJohannes Berg 				struct sk_buff *skb;
125547086fc5SJohannes Berg 
125647086fc5SJohannes Berg 				while (n_frames > 0) {
1257948d887dSJohannes Berg 					skb = skb_dequeue(&sta->tx_filtered[ac]);
1258948d887dSJohannes Berg 					if (!skb) {
125947086fc5SJohannes Berg 						skb = skb_dequeue(
126047086fc5SJohannes Berg 							&sta->ps_tx_buf[ac]);
1261af818581SJohannes Berg 						if (skb)
1262af818581SJohannes Berg 							local->total_ps_buffered--;
1263af818581SJohannes Berg 					}
126447086fc5SJohannes Berg 					if (!skb)
126547086fc5SJohannes Berg 						break;
126647086fc5SJohannes Berg 					n_frames--;
12674049e09aSJohannes Berg 					found = true;
126847086fc5SJohannes Berg 					__skb_queue_tail(&frames, skb);
126947086fc5SJohannes Berg 				}
1270948d887dSJohannes Berg 			}
1271948d887dSJohannes Berg 
12724049e09aSJohannes Berg 			/*
12734049e09aSJohannes Berg 			 * If the driver has data on more than one TID then
12744049e09aSJohannes Berg 			 * certainly there's more data if we release just a
12754049e09aSJohannes Berg 			 * single frame now (from a single TID).
12764049e09aSJohannes Berg 			 */
127747086fc5SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
127847086fc5SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
12794049e09aSJohannes Berg 				more_data = true;
12804049e09aSJohannes Berg 				driver_release_tids =
12814049e09aSJohannes Berg 					BIT(ffs(driver_release_tids) - 1);
12824049e09aSJohannes Berg 				break;
12834049e09aSJohannes Berg 			}
12844049e09aSJohannes Berg 		}
1285948d887dSJohannes Berg 
1286948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1287948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1288948d887dSJohannes Berg 			more_data = true;
1289948d887dSJohannes Berg 			break;
1290948d887dSJohannes Berg 		}
1291948d887dSJohannes Berg 	}
1292af818581SJohannes Berg 
12934049e09aSJohannes Berg 	if (!found) {
1294ce662b44SJohannes Berg 		int tid;
12954049e09aSJohannes Berg 
1296ce662b44SJohannes Berg 		/*
1297ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1298ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1299ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1300ce662b44SJohannes Berg 		 *
1301ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1302ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1303ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1304ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1305ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1306ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1307ce662b44SJohannes Berg 		 *
1308ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1309ce662b44SJohannes Berg 		 */
1310ce662b44SJohannes Berg 
1311ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1312ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1313ce662b44SJohannes Berg 
131440b96408SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason);
13154049e09aSJohannes Berg 		return;
13164049e09aSJohannes Berg 	}
13174049e09aSJohannes Berg 
131847086fc5SJohannes Berg 	if (!driver_release_tids) {
131947086fc5SJohannes Berg 		struct sk_buff_head pending;
132047086fc5SJohannes Berg 		struct sk_buff *skb;
132140b96408SJohannes Berg 		int num = 0;
132240b96408SJohannes Berg 		u16 tids = 0;
132347086fc5SJohannes Berg 
132447086fc5SJohannes Berg 		skb_queue_head_init(&pending);
132547086fc5SJohannes Berg 
132647086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1327af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
132847086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
132940b96408SJohannes Berg 			u8 *qoshdr = NULL;
133040b96408SJohannes Berg 
133140b96408SJohannes Berg 			num++;
1332af818581SJohannes Berg 
1333af818581SJohannes Berg 			/*
133447086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
133547086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
133647086fc5SJohannes Berg 			 * exchange.
1337af818581SJohannes Berg 			 */
1338d6d23de2SFelix Fietkau 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1339d6d23de2SFelix Fietkau 				       IEEE80211_TX_CTL_PS_RESPONSE;
1340af818581SJohannes Berg 
134147086fc5SJohannes Berg 			/*
134247086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
134347086fc5SJohannes Berg 			 * more buffered frames for this STA
134447086fc5SJohannes Berg 			 */
134524b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
134647086fc5SJohannes Berg 				hdr->frame_control |=
134747086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
134824b9c373SJanusz.Dziedzic@tieto.com 			else
134924b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
135024b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1351af818581SJohannes Berg 
135240b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
135340b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
135440b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
135540b96408SJohannes Berg 
135652a3f20cSMarco Porsch 			/* end service period after last frame */
135752a3f20cSMarco Porsch 			if (skb_queue_empty(&frames)) {
135840b96408SJohannes Berg 				if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
135952a3f20cSMarco Porsch 				    qoshdr)
136040b96408SJohannes Berg 					*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1361deeaee19SJohannes Berg 
136247086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
136347086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
136452a3f20cSMarco Porsch 			}
136547086fc5SJohannes Berg 
136640b96408SJohannes Berg 			if (qoshdr)
136740b96408SJohannes Berg 				tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
136840b96408SJohannes Berg 			else
136940b96408SJohannes Berg 				tids |= BIT(0);
137040b96408SJohannes Berg 
137147086fc5SJohannes Berg 			__skb_queue_tail(&pending, skb);
137247086fc5SJohannes Berg 		}
137347086fc5SJohannes Berg 
137440b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
137540b96408SJohannes Berg 					  reason, more_data);
137640b96408SJohannes Berg 
137747086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1378af818581SJohannes Berg 
1379c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1380af818581SJohannes Berg 	} else {
1381af818581SJohannes Berg 		/*
13824049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
13834049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
13844049e09aSJohannes Berg 		 * Note that, as per the comment above, it'll also have to see
13854049e09aSJohannes Berg 		 * if there is more than just one frame on the specific TID that
13864049e09aSJohannes Berg 		 * we're releasing from, and it needs to set the more-data bit
13874049e09aSJohannes Berg 		 * accordingly if we tell it that there's no more data. If we do
13884049e09aSJohannes Berg 		 * tell it there's more data, then of course the more-data bit
13894049e09aSJohannes Berg 		 * needs to be set anyway.
1390af818581SJohannes Berg 		 */
13914049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
139247086fc5SJohannes Berg 					    n_frames, reason, more_data);
13934049e09aSJohannes Berg 
13944049e09aSJohannes Berg 		/*
13954049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
13964049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
13974049e09aSJohannes Berg 		 * that the TID became empty before returning here from the
13984049e09aSJohannes Berg 		 * release function.
13994049e09aSJohannes Berg 		 * Either way, however, when the driver tells us that the TID
14004049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
14014049e09aSJohannes Berg 		 */
1402af818581SJohannes Berg 	}
1403af818581SJohannes Berg }
1404af818581SJohannes Berg 
1405af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1406af818581SJohannes Berg {
140747086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1408af818581SJohannes Berg 
1409af818581SJohannes Berg 	/*
141047086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
141147086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
141247086fc5SJohannes Berg 	 * only from the non-enabled ones.
1413af818581SJohannes Berg 	 */
141447086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
141547086fc5SJohannes Berg 		ignore_for_response = 0;
1416af818581SJohannes Berg 
141747086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
141847086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1419af818581SJohannes Berg }
142047086fc5SJohannes Berg 
142147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
142247086fc5SJohannes Berg {
142347086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
142447086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
142547086fc5SJohannes Berg 
142647086fc5SJohannes Berg 	/*
142747086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
142847086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
142947086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
143047086fc5SJohannes Berg 	 * actually getting called.
143147086fc5SJohannes Berg 	 */
143247086fc5SJohannes Berg 	if (!delivery_enabled)
143347086fc5SJohannes Berg 		return;
143447086fc5SJohannes Berg 
143547086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
143647086fc5SJohannes Berg 	case 1:
143747086fc5SJohannes Berg 		n_frames = 2;
143847086fc5SJohannes Berg 		break;
143947086fc5SJohannes Berg 	case 2:
144047086fc5SJohannes Berg 		n_frames = 4;
144147086fc5SJohannes Berg 		break;
144247086fc5SJohannes Berg 	case 3:
144347086fc5SJohannes Berg 		n_frames = 6;
144447086fc5SJohannes Berg 		break;
144547086fc5SJohannes Berg 	case 0:
144647086fc5SJohannes Berg 		/* XXX: what is a good value? */
144747086fc5SJohannes Berg 		n_frames = 8;
144847086fc5SJohannes Berg 		break;
144947086fc5SJohannes Berg 	}
145047086fc5SJohannes Berg 
145147086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
145247086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1453af818581SJohannes Berg }
1454af818581SJohannes Berg 
1455af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1456af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1457af818581SJohannes Berg {
1458af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1459af818581SJohannes Berg 
1460b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1461b5878a2dSJohannes Berg 
1462af818581SJohannes Berg 	if (block)
1463c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1464c2c98fdeSJohannes Berg 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1465af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1466af818581SJohannes Berg }
1467af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1468dcf55fb5SFelix Fietkau 
1469e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
147037fbd908SJohannes Berg {
147137fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
147237fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
147337fbd908SJohannes Berg 
147437fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
147537fbd908SJohannes Berg 
147637fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
147737fbd908SJohannes Berg }
1478e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
147937fbd908SJohannes Berg 
1480042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1481042ec453SJohannes Berg 				u8 tid, bool buffered)
1482dcf55fb5SFelix Fietkau {
1483dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1484dcf55fb5SFelix Fietkau 
14855a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1486042ec453SJohannes Berg 		return;
1487042ec453SJohannes Berg 
1488948d887dSJohannes Berg 	if (buffered)
1489948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1490948d887dSJohannes Berg 	else
1491948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1492948d887dSJohannes Berg 
1493c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1494dcf55fb5SFelix Fietkau }
1495042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1496d9a7ddb0SJohannes Berg 
149783d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
1498d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
1499d9a7ddb0SJohannes Berg {
15008bf11d8dSJohannes Berg 	might_sleep();
1501d9a7ddb0SJohannes Berg 
1502d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
1503d9a7ddb0SJohannes Berg 		return 0;
1504d9a7ddb0SJohannes Berg 
1505f09603a2SJohannes Berg 	/* check allowed transitions first */
1506f09603a2SJohannes Berg 
1507d9a7ddb0SJohannes Berg 	switch (new_state) {
1508d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
1509f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
1510d9a7ddb0SJohannes Berg 			return -EINVAL;
1511d9a7ddb0SJohannes Berg 		break;
1512d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
1513f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
1514f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
1515d9a7ddb0SJohannes Berg 			return -EINVAL;
1516d9a7ddb0SJohannes Berg 		break;
1517d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
1518f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1519f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1520d9a7ddb0SJohannes Berg 			return -EINVAL;
1521d9a7ddb0SJohannes Berg 		break;
1522d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1523f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1524d9a7ddb0SJohannes Berg 			return -EINVAL;
1525d9a7ddb0SJohannes Berg 		break;
1526d9a7ddb0SJohannes Berg 	default:
1527d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
1528d9a7ddb0SJohannes Berg 		return -EINVAL;
1529d9a7ddb0SJohannes Berg 	}
1530d9a7ddb0SJohannes Berg 
1531bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1532bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
1533f09603a2SJohannes Berg 
1534f09603a2SJohannes Berg 	/*
1535f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
1536f09603a2SJohannes Berg 	 * fail the transition
1537f09603a2SJohannes Berg 	 */
1538f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1539f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1540f09603a2SJohannes Berg 					sta->sta_state, new_state);
1541f09603a2SJohannes Berg 		if (err)
1542f09603a2SJohannes Berg 			return err;
1543f09603a2SJohannes Berg 	}
1544f09603a2SJohannes Berg 
1545f09603a2SJohannes Berg 	/* reflect the change in all state variables */
1546f09603a2SJohannes Berg 
1547f09603a2SJohannes Berg 	switch (new_state) {
1548f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
1549f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
1550f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1551f09603a2SJohannes Berg 		break;
1552f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
1553f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_NONE)
1554f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1555f09603a2SJohannes Berg 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1556f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1557f09603a2SJohannes Berg 		break;
1558f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
1559f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1560f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1561f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
15627e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
15637e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
15647e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
15657e3ed02cSFelix Fietkau 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1566f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1567f09603a2SJohannes Berg 		}
1568f09603a2SJohannes Berg 		break;
1569f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
1570f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
15717e3ed02cSFelix Fietkau 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
15727e3ed02cSFelix Fietkau 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
15737e3ed02cSFelix Fietkau 			     !sta->sdata->u.vlan.sta))
15747e3ed02cSFelix Fietkau 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1575f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1576f09603a2SJohannes Berg 		}
1577f09603a2SJohannes Berg 		break;
1578f09603a2SJohannes Berg 	default:
1579f09603a2SJohannes Berg 		break;
1580f09603a2SJohannes Berg 	}
1581f09603a2SJohannes Berg 
1582d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
1583d9a7ddb0SJohannes Berg 
1584d9a7ddb0SJohannes Berg 	return 0;
1585d9a7ddb0SJohannes Berg }
1586687da132SEmmanuel Grumbach 
1587687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta)
1588687da132SEmmanuel Grumbach {
1589687da132SEmmanuel Grumbach 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1590687da132SEmmanuel Grumbach 	u8 rx_streams;
1591687da132SEmmanuel Grumbach 
1592687da132SEmmanuel Grumbach 	if (!sta->sta.ht_cap.ht_supported)
1593687da132SEmmanuel Grumbach 		return 1;
1594687da132SEmmanuel Grumbach 
1595687da132SEmmanuel Grumbach 	if (sta->sta.vht_cap.vht_supported) {
1596687da132SEmmanuel Grumbach 		int i;
1597687da132SEmmanuel Grumbach 		u16 tx_mcs_map =
1598687da132SEmmanuel Grumbach 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1599687da132SEmmanuel Grumbach 
1600687da132SEmmanuel Grumbach 		for (i = 7; i >= 0; i--)
1601687da132SEmmanuel Grumbach 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1602687da132SEmmanuel Grumbach 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1603687da132SEmmanuel Grumbach 				return i + 1;
1604687da132SEmmanuel Grumbach 	}
1605687da132SEmmanuel Grumbach 
1606687da132SEmmanuel Grumbach 	if (ht_cap->mcs.rx_mask[3])
1607687da132SEmmanuel Grumbach 		rx_streams = 4;
1608687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[2])
1609687da132SEmmanuel Grumbach 		rx_streams = 3;
1610687da132SEmmanuel Grumbach 	else if (ht_cap->mcs.rx_mask[1])
1611687da132SEmmanuel Grumbach 		rx_streams = 2;
1612687da132SEmmanuel Grumbach 	else
1613687da132SEmmanuel Grumbach 		rx_streams = 1;
1614687da132SEmmanuel Grumbach 
1615687da132SEmmanuel Grumbach 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1616687da132SEmmanuel Grumbach 		return rx_streams;
1617687da132SEmmanuel Grumbach 
1618687da132SEmmanuel Grumbach 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1619687da132SEmmanuel Grumbach 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1620687da132SEmmanuel Grumbach }
1621