xref: /openbmc/linux/net/mac80211/sta_info.c (revision 40b96408831f038b1a6b45e8b22cd050f82a3896)
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>
12f0706e82SJiri Benc #include <linux/netdevice.h>
13f0706e82SJiri Benc #include <linux/types.h>
14f0706e82SJiri Benc #include <linux/slab.h>
15f0706e82SJiri Benc #include <linux/skbuff.h>
16f0706e82SJiri Benc #include <linux/if_arp.h>
170d174406SJohannes Berg #include <linux/timer.h>
18d0709a65SJohannes Berg #include <linux/rtnetlink.h>
19f0706e82SJiri Benc 
20f0706e82SJiri Benc #include <net/mac80211.h>
21f0706e82SJiri Benc #include "ieee80211_i.h"
2224487981SJohannes Berg #include "driver-ops.h"
232c8dccc7SJohannes Berg #include "rate.h"
24f0706e82SJiri Benc #include "sta_info.h"
25e9f207f0SJiri Benc #include "debugfs_sta.h"
26ee385855SLuis Carlos Cobo #include "mesh.h"
27ce662b44SJohannes Berg #include "wme.h"
28f0706e82SJiri Benc 
29d0709a65SJohannes Berg /**
30d0709a65SJohannes Berg  * DOC: STA information lifetime rules
31d0709a65SJohannes Berg  *
32d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
33d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
34d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
35d0709a65SJohannes Berg  *
3634e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3734e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
3834e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
3934e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4034e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4134e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4234e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4334e89507SJohannes Berg  * encryption keys.
4493e5deb1SJohannes Berg  *
4593e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4693e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
47d0709a65SJohannes Berg  *
4834e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
497e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
507e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5125985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5234e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5325985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5434e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
557e189a12SLuis R. Rodriguez  *
5634e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5734e89507SJohannes Berg  * calls are available.
58d0709a65SJohannes Berg  *
5934e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6034e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6134e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6234e89507SJohannes Berg  * freed before they are done using it.
63d0709a65SJohannes Berg  */
64f0706e82SJiri Benc 
65f0706e82SJiri Benc /* Caller must hold local->sta_lock */
66be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
67f0706e82SJiri Benc 			     struct sta_info *sta)
68f0706e82SJiri Benc {
69f0706e82SJiri Benc 	struct sta_info *s;
70f0706e82SJiri Benc 
7140b275b6SJohannes Berg 	s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
7240b275b6SJohannes Berg 				      lockdep_is_held(&local->sta_lock));
73f0706e82SJiri Benc 	if (!s)
74be8755e1SMichael Wu 		return -ENOENT;
75be8755e1SMichael Wu 	if (s == sta) {
7617741cdcSJohannes Berg 		rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
77d0709a65SJohannes Berg 				   s->hnext);
78be8755e1SMichael Wu 		return 0;
79f0706e82SJiri Benc 	}
80f0706e82SJiri Benc 
8140b275b6SJohannes Berg 	while (rcu_access_pointer(s->hnext) &&
8240b275b6SJohannes Berg 	       rcu_access_pointer(s->hnext) != sta)
8340b275b6SJohannes Berg 		s = rcu_dereference_protected(s->hnext,
8440b275b6SJohannes Berg 					lockdep_is_held(&local->sta_lock));
8540b275b6SJohannes Berg 	if (rcu_access_pointer(s->hnext)) {
86d0709a65SJohannes Berg 		rcu_assign_pointer(s->hnext, sta->hnext);
87be8755e1SMichael Wu 		return 0;
88f0706e82SJiri Benc 	}
89f0706e82SJiri Benc 
90be8755e1SMichael Wu 	return -ENOENT;
91f0706e82SJiri Benc }
92f0706e82SJiri Benc 
93d0709a65SJohannes Berg /* protected by RCU */
94abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
95abe60632SJohannes Berg 			      const u8 *addr)
9643ba7e95SJohannes Berg {
97abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
9843ba7e95SJohannes Berg 	struct sta_info *sta;
9943ba7e95SJohannes Berg 
1000379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1010379185bSJohannes Berg 				    lockdep_is_held(&local->sta_lock) ||
1020379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
10343ba7e95SJohannes Berg 	while (sta) {
1042a33bee2SGuy Eilam 		if (sta->sdata == sdata && !sta->dummy &&
1052a33bee2SGuy Eilam 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
1062a33bee2SGuy Eilam 			break;
1072a33bee2SGuy Eilam 		sta = rcu_dereference_check(sta->hnext,
1082a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_lock) ||
1092a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_mtx));
1102a33bee2SGuy Eilam 	}
1112a33bee2SGuy Eilam 	return sta;
1122a33bee2SGuy Eilam }
1132a33bee2SGuy Eilam 
1142a33bee2SGuy Eilam /* get a station info entry even if it is a dummy station*/
1152a33bee2SGuy Eilam struct sta_info *sta_info_get_rx(struct ieee80211_sub_if_data *sdata,
1162a33bee2SGuy Eilam 			      const u8 *addr)
1172a33bee2SGuy Eilam {
1182a33bee2SGuy Eilam 	struct ieee80211_local *local = sdata->local;
1192a33bee2SGuy Eilam 	struct sta_info *sta;
1202a33bee2SGuy Eilam 
1212a33bee2SGuy Eilam 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1222a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_lock) ||
1232a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_mtx));
1242a33bee2SGuy Eilam 	while (sta) {
125abe60632SJohannes Berg 		if (sta->sdata == sdata &&
126abe60632SJohannes Berg 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
12743ba7e95SJohannes Berg 			break;
1280379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1290379185bSJohannes Berg 					    lockdep_is_held(&local->sta_lock) ||
1300379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
13143ba7e95SJohannes Berg 	}
13243ba7e95SJohannes Berg 	return sta;
13343ba7e95SJohannes Berg }
13443ba7e95SJohannes Berg 
1350e5ded5aSFelix Fietkau /*
1360e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
1370e5ded5aSFelix Fietkau  * or from one of its vlans
1380e5ded5aSFelix Fietkau  */
1390e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1400e5ded5aSFelix Fietkau 				  const u8 *addr)
1410e5ded5aSFelix Fietkau {
1420e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
1430e5ded5aSFelix Fietkau 	struct sta_info *sta;
1440e5ded5aSFelix Fietkau 
1450379185bSJohannes Berg 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1460379185bSJohannes Berg 				    lockdep_is_held(&local->sta_lock) ||
1470379185bSJohannes Berg 				    lockdep_is_held(&local->sta_mtx));
1480e5ded5aSFelix Fietkau 	while (sta) {
1490e5ded5aSFelix Fietkau 		if ((sta->sdata == sdata ||
150a2c1e3daSJohannes Berg 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
1512a33bee2SGuy Eilam 		    !sta->dummy &&
1522a33bee2SGuy Eilam 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
1532a33bee2SGuy Eilam 			break;
1542a33bee2SGuy Eilam 		sta = rcu_dereference_check(sta->hnext,
1552a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_lock) ||
1562a33bee2SGuy Eilam 					    lockdep_is_held(&local->sta_mtx));
1572a33bee2SGuy Eilam 	}
1582a33bee2SGuy Eilam 	return sta;
1592a33bee2SGuy Eilam }
1602a33bee2SGuy Eilam 
1612a33bee2SGuy Eilam /*
1622a33bee2SGuy Eilam  * Get sta info either from the specified interface
1632a33bee2SGuy Eilam  * or from one of its vlans (including dummy stations)
1642a33bee2SGuy Eilam  */
1652a33bee2SGuy Eilam struct sta_info *sta_info_get_bss_rx(struct ieee80211_sub_if_data *sdata,
1662a33bee2SGuy Eilam 				  const u8 *addr)
1672a33bee2SGuy Eilam {
1682a33bee2SGuy Eilam 	struct ieee80211_local *local = sdata->local;
1692a33bee2SGuy Eilam 	struct sta_info *sta;
1702a33bee2SGuy Eilam 
1712a33bee2SGuy Eilam 	sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
1722a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_lock) ||
1732a33bee2SGuy Eilam 				    lockdep_is_held(&local->sta_mtx));
1742a33bee2SGuy Eilam 	while (sta) {
1752a33bee2SGuy Eilam 		if ((sta->sdata == sdata ||
1762a33bee2SGuy Eilam 		     (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
1770e5ded5aSFelix Fietkau 		    memcmp(sta->sta.addr, addr, ETH_ALEN) == 0)
1780e5ded5aSFelix Fietkau 			break;
1790379185bSJohannes Berg 		sta = rcu_dereference_check(sta->hnext,
1800379185bSJohannes Berg 					    lockdep_is_held(&local->sta_lock) ||
1810379185bSJohannes Berg 					    lockdep_is_held(&local->sta_mtx));
1820e5ded5aSFelix Fietkau 	}
1830e5ded5aSFelix Fietkau 	return sta;
1840e5ded5aSFelix Fietkau }
1850e5ded5aSFelix Fietkau 
1863b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
1873b53fde8SJohannes Berg 				     int idx)
188ee385855SLuis Carlos Cobo {
1893b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
190ee385855SLuis Carlos Cobo 	struct sta_info *sta;
191ee385855SLuis Carlos Cobo 	int i = 0;
192ee385855SLuis Carlos Cobo 
193d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
1943b53fde8SJohannes Berg 		if (sdata != sta->sdata)
1952a8ca29aSLuis Carlos Cobo 			continue;
196ee385855SLuis Carlos Cobo 		if (i < idx) {
197ee385855SLuis Carlos Cobo 			++i;
198ee385855SLuis Carlos Cobo 			continue;
199ee385855SLuis Carlos Cobo 		}
2002a8ca29aSLuis Carlos Cobo 		return sta;
201ee385855SLuis Carlos Cobo 	}
202ee385855SLuis Carlos Cobo 
203ee385855SLuis Carlos Cobo 	return NULL;
204ee385855SLuis Carlos Cobo }
205f0706e82SJiri Benc 
20693e5deb1SJohannes Berg /**
20793e5deb1SJohannes Berg  * __sta_info_free - internal STA free helper
20893e5deb1SJohannes Berg  *
2096ef307bcSRandy Dunlap  * @local: pointer to the global information
21093e5deb1SJohannes Berg  * @sta: STA info to free
21193e5deb1SJohannes Berg  *
21293e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
21393e5deb1SJohannes Berg  * that may happen before sta_info_insert().
21493e5deb1SJohannes Berg  */
21593e5deb1SJohannes Berg static void __sta_info_free(struct ieee80211_local *local,
21693e5deb1SJohannes Berg 			    struct sta_info *sta)
21793e5deb1SJohannes Berg {
218af65cd96SJohannes Berg 	if (sta->rate_ctrl) {
2194b7679a5SJohannes Berg 		rate_control_free_sta(sta);
22093e5deb1SJohannes Berg 		rate_control_put(sta->rate_ctrl);
221af65cd96SJohannes Berg 	}
22293e5deb1SJohannes Berg 
22393e5deb1SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2240fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Destroyed STA %pM\n", sta->sta.addr);
22593e5deb1SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
22693e5deb1SJohannes Berg 
22793e5deb1SJohannes Berg 	kfree(sta);
22893e5deb1SJohannes Berg }
22993e5deb1SJohannes Berg 
230d0709a65SJohannes Berg /* Caller must hold local->sta_lock */
231d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local,
232d0709a65SJohannes Berg 			      struct sta_info *sta)
233f0706e82SJiri Benc {
23417741cdcSJohannes Berg 	sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
23517741cdcSJohannes Berg 	rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
236f0706e82SJiri Benc }
237f0706e82SJiri Benc 
238af818581SJohannes Berg static void sta_unblock(struct work_struct *wk)
239af818581SJohannes Berg {
240af818581SJohannes Berg 	struct sta_info *sta;
241af818581SJohannes Berg 
242af818581SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_unblock_wk);
243af818581SJohannes Berg 
244af818581SJohannes Berg 	if (sta->dead)
245af818581SJohannes Berg 		return;
246af818581SJohannes Berg 
247c2c98fdeSJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
248af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
249c2c98fdeSJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
250c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
251ce662b44SJohannes Berg 
252ce662b44SJohannes Berg 		local_bh_disable();
253af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
254ce662b44SJohannes Berg 		local_bh_enable();
255c2c98fdeSJohannes Berg 	} else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
256c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
257ce662b44SJohannes Berg 
258ce662b44SJohannes Berg 		local_bh_disable();
25947086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
260ce662b44SJohannes Berg 		local_bh_enable();
26150a9432dSJohannes Berg 	} else
262c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
263af818581SJohannes Berg }
264af818581SJohannes Berg 
265af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
266af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
267af65cd96SJohannes Berg {
268af65cd96SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
269af65cd96SJohannes Berg 		return 0;
270af65cd96SJohannes Berg 
271af65cd96SJohannes Berg 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
272af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
273af65cd96SJohannes Berg 						     &sta->sta, gfp);
274af65cd96SJohannes Berg 	if (!sta->rate_ctrl_priv) {
275af65cd96SJohannes Berg 		rate_control_put(sta->rate_ctrl);
276af65cd96SJohannes Berg 		return -ENOMEM;
277af65cd96SJohannes Berg 	}
278af65cd96SJohannes Berg 
279af65cd96SJohannes Berg 	return 0;
280af65cd96SJohannes Berg }
281af65cd96SJohannes Berg 
28273651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
28373651ee6SJohannes Berg 				u8 *addr, gfp_t gfp)
284f0706e82SJiri Benc {
285d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
286f0706e82SJiri Benc 	struct sta_info *sta;
287ebe27c91SMohammed Shafi Shajakhan 	struct timespec uptime;
28816c5f15cSRon Rindjunsky 	int i;
289f0706e82SJiri Benc 
29017741cdcSJohannes Berg 	sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
291f0706e82SJiri Benc 	if (!sta)
29273651ee6SJohannes Berg 		return NULL;
293f0706e82SJiri Benc 
29407346f81SJohannes Berg 	spin_lock_init(&sta->lock);
295af818581SJohannes Berg 	INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
29667c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
297a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
29807346f81SJohannes Berg 
29917741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
300d0709a65SJohannes Berg 	sta->local = local;
301d0709a65SJohannes Berg 	sta->sdata = sdata;
3028bc8aecdSFelix Fietkau 	sta->last_rx = jiffies;
303f0706e82SJiri Benc 
304ebe27c91SMohammed Shafi Shajakhan 	do_posix_clock_monotonic_gettime(&uptime);
305ebe27c91SMohammed Shafi Shajakhan 	sta->last_connected = uptime.tv_sec;
306541a45a1SBruno Randolf 	ewma_init(&sta->avg_signal, 1024, 8);
307541a45a1SBruno Randolf 
308af65cd96SJohannes Berg 	if (sta_prepare_rate_control(local, sta, gfp)) {
309f0706e82SJiri Benc 		kfree(sta);
31073651ee6SJohannes Berg 		return NULL;
311f0706e82SJiri Benc 	}
312f0706e82SJiri Benc 
31316c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
314a622ab72SJohannes Berg 		/*
315a622ab72SJohannes Berg 		 * timer_to_tid must be initialized with identity mapping
316a622ab72SJohannes Berg 		 * to enable session_timer's data differentiation. See
317a622ab72SJohannes Berg 		 * sta_rx_agg_session_timer_expired for usage.
318a622ab72SJohannes Berg 		 */
31916c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
32016c5f15cSRon Rindjunsky 	}
321948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
322948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
323948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
324948d887dSJohannes Berg 	}
32573651ee6SJohannes Berg 
326cccaec98SSenthil Balasubramanian 	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
3274be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
328cccaec98SSenthil Balasubramanian 
32973651ee6SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
3300fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Allocated STA %pM\n", sta->sta.addr);
33173651ee6SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
33273651ee6SJohannes Berg 
33303e4497eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
33457cf8043SJavier Cardona 	sta->plink_state = NL80211_PLINK_LISTEN;
33503e4497eSJohannes Berg 	init_timer(&sta->plink_timer);
33603e4497eSJohannes Berg #endif
33703e4497eSJohannes Berg 
33873651ee6SJohannes Berg 	return sta;
33973651ee6SJohannes Berg }
34073651ee6SJohannes Berg 
3412a33bee2SGuy Eilam static int sta_info_finish_insert(struct sta_info *sta,
3422a33bee2SGuy Eilam 				bool async, bool dummy_reinsert)
34373651ee6SJohannes Berg {
34473651ee6SJohannes Berg 	struct ieee80211_local *local = sta->local;
34573651ee6SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
34698b62183SJohannes Berg 	struct station_info sinfo;
34773651ee6SJohannes Berg 	unsigned long flags;
34893e5deb1SJohannes Berg 	int err = 0;
34973651ee6SJohannes Berg 
35046a5ebafSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
35134e89507SJohannes Berg 
3522a33bee2SGuy Eilam 	if (!sta->dummy || dummy_reinsert) {
35334e89507SJohannes Berg 		/* notify driver */
35434e89507SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
35534e89507SJohannes Berg 			sdata = container_of(sdata->bss,
35634e89507SJohannes Berg 					     struct ieee80211_sub_if_data,
35734e89507SJohannes Berg 					     u.ap);
35834e89507SJohannes Berg 		err = drv_sta_add(local, sdata, &sta->sta);
35934e89507SJohannes Berg 		if (err) {
36034e89507SJohannes Berg 			if (!async)
36134e89507SJohannes Berg 				return err;
3622a33bee2SGuy Eilam 			printk(KERN_DEBUG "%s: failed to add IBSS STA %pM to "
3632a33bee2SGuy Eilam 					  "driver (%d) - keeping it anyway.\n",
36434e89507SJohannes Berg 			       sdata->name, sta->sta.addr, err);
36534e89507SJohannes Berg 		} else {
36634e89507SJohannes Berg 			sta->uploaded = true;
36734e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
36834e89507SJohannes Berg 			if (async)
3690fb9a9ecSJoe Perches 				wiphy_debug(local->hw.wiphy,
3700fb9a9ecSJoe Perches 					    "Finished adding IBSS STA %pM\n",
3710fb9a9ecSJoe Perches 					    sta->sta.addr);
37234e89507SJohannes Berg #endif
37334e89507SJohannes Berg 		}
37434e89507SJohannes Berg 
37534e89507SJohannes Berg 		sdata = sta->sdata;
3762a33bee2SGuy Eilam 	}
37734e89507SJohannes Berg 
3782a33bee2SGuy Eilam 	if (!dummy_reinsert) {
37934e89507SJohannes Berg 		if (!async) {
38034e89507SJohannes Berg 			local->num_sta++;
38134e89507SJohannes Berg 			local->sta_generation++;
38234e89507SJohannes Berg 			smp_mb();
38334e89507SJohannes Berg 
38434e89507SJohannes Berg 			/* make the station visible */
38534e89507SJohannes Berg 			spin_lock_irqsave(&local->sta_lock, flags);
38634e89507SJohannes Berg 			sta_info_hash_add(local, sta);
38734e89507SJohannes Berg 			spin_unlock_irqrestore(&local->sta_lock, flags);
38834e89507SJohannes Berg 		}
38934e89507SJohannes Berg 
39034e89507SJohannes Berg 		list_add(&sta->list, &local->sta_list);
3912a33bee2SGuy Eilam 	} else {
3922a33bee2SGuy Eilam 		sta->dummy = false;
3932a33bee2SGuy Eilam 	}
39434e89507SJohannes Berg 
3952a33bee2SGuy Eilam 	if (!sta->dummy) {
39634e89507SJohannes Berg 		ieee80211_sta_debugfs_add(sta);
39734e89507SJohannes Berg 		rate_control_add_sta_debugfs(sta);
39834e89507SJohannes Berg 
399f612cedfSJouni Malinen 		memset(&sinfo, 0, sizeof(sinfo));
40034e89507SJohannes Berg 		sinfo.filled = 0;
40134e89507SJohannes Berg 		sinfo.generation = local->sta_generation;
40234e89507SJohannes Berg 		cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
4032a33bee2SGuy Eilam 	}
40434e89507SJohannes Berg 
40534e89507SJohannes Berg 	return 0;
40634e89507SJohannes Berg }
40734e89507SJohannes Berg 
40834e89507SJohannes Berg static void sta_info_finish_pending(struct ieee80211_local *local)
40934e89507SJohannes Berg {
41034e89507SJohannes Berg 	struct sta_info *sta;
41134e89507SJohannes Berg 	unsigned long flags;
41234e89507SJohannes Berg 
41334e89507SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
41434e89507SJohannes Berg 	while (!list_empty(&local->sta_pending_list)) {
41534e89507SJohannes Berg 		sta = list_first_entry(&local->sta_pending_list,
41634e89507SJohannes Berg 				       struct sta_info, list);
41734e89507SJohannes Berg 		list_del(&sta->list);
41834e89507SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
41934e89507SJohannes Berg 
4202a33bee2SGuy Eilam 		sta_info_finish_insert(sta, true, false);
42134e89507SJohannes Berg 
42234e89507SJohannes Berg 		spin_lock_irqsave(&local->sta_lock, flags);
42334e89507SJohannes Berg 	}
42434e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
42534e89507SJohannes Berg }
42634e89507SJohannes Berg 
42734e89507SJohannes Berg static void sta_info_finish_work(struct work_struct *work)
42834e89507SJohannes Berg {
42934e89507SJohannes Berg 	struct ieee80211_local *local =
43034e89507SJohannes Berg 		container_of(work, struct ieee80211_local, sta_finish_work);
43134e89507SJohannes Berg 
43234e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
43334e89507SJohannes Berg 	sta_info_finish_pending(local);
43434e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
43534e89507SJohannes Berg }
43634e89507SJohannes Berg 
4378c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
43834e89507SJohannes Berg {
43934e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
44034e89507SJohannes Berg 
44103e4497eSJohannes Berg 	/*
44203e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
44303e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
44403e4497eSJohannes Berg 	 * and another CPU turns off the net device.
44503e4497eSJohannes Berg 	 */
4468c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
4478c71df7aSGuy Eilam 		return -ENETDOWN;
44803e4497eSJohannes Berg 
44947846c9bSJohannes Berg 	if (WARN_ON(compare_ether_addr(sta->sta.addr, sdata->vif.addr) == 0 ||
4508c71df7aSGuy Eilam 		    is_multicast_ether_addr(sta->sta.addr)))
4518c71df7aSGuy Eilam 		return -EINVAL;
4528c71df7aSGuy Eilam 
4538c71df7aSGuy Eilam 	return 0;
45493e5deb1SJohannes Berg }
45544213b5eSJohannes Berg 
4568c71df7aSGuy Eilam static int sta_info_insert_ibss(struct sta_info *sta) __acquires(RCU)
4578c71df7aSGuy Eilam {
4588c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4598c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4608c71df7aSGuy Eilam 	unsigned long flags;
4618c71df7aSGuy Eilam 
462d0709a65SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
46343ba7e95SJohannes Berg 	/* check if STA exists already */
4642a33bee2SGuy Eilam 	if (sta_info_get_bss_rx(sdata, sta->sta.addr)) {
465d0709a65SJohannes Berg 		spin_unlock_irqrestore(&local->sta_lock, flags);
46634e89507SJohannes Berg 		rcu_read_lock();
4678c71df7aSGuy Eilam 		return -EEXIST;
46843ba7e95SJohannes Berg 	}
46934e89507SJohannes Berg 
470f0706e82SJiri Benc 	local->num_sta++;
47134e89507SJohannes Berg 	local->sta_generation++;
47234e89507SJohannes Berg 	smp_mb();
473f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
47432bfd35dSJohannes Berg 
47534e89507SJohannes Berg 	list_add_tail(&sta->list, &local->sta_pending_list);
47632bfd35dSJohannes Berg 
47734e89507SJohannes Berg 	rcu_read_lock();
47834e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
47934e89507SJohannes Berg 
48034e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
4810fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Added IBSS STA %pM\n",
4820fb9a9ecSJoe Perches 			sta->sta.addr);
48334e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
48434e89507SJohannes Berg 
48534e89507SJohannes Berg 	ieee80211_queue_work(&local->hw, &local->sta_finish_work);
48634e89507SJohannes Berg 
48734e89507SJohannes Berg 	return 0;
48834e89507SJohannes Berg }
48934e89507SJohannes Berg 
49034e89507SJohannes Berg /*
4918c71df7aSGuy Eilam  * should be called with sta_mtx locked
4928c71df7aSGuy Eilam  * this function replaces the mutex lock
4938c71df7aSGuy Eilam  * with a RCU lock
4948c71df7aSGuy Eilam  */
4958c71df7aSGuy Eilam static int sta_info_insert_non_ibss(struct sta_info *sta) __acquires(RCU)
4968c71df7aSGuy Eilam {
4978c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
4988c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
4998c71df7aSGuy Eilam 	unsigned long flags;
5002a33bee2SGuy Eilam 	struct sta_info *exist_sta;
5012a33bee2SGuy Eilam 	bool dummy_reinsert = false;
5028c71df7aSGuy Eilam 	int err = 0;
5038c71df7aSGuy Eilam 
5048c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
5058c71df7aSGuy Eilam 
5068c71df7aSGuy Eilam 	/*
50734e89507SJohannes Berg 	 * On first glance, this will look racy, because the code
5088c71df7aSGuy Eilam 	 * in this function, which inserts a station with sleeping,
50934e89507SJohannes Berg 	 * unlocks the sta_lock between checking existence in the
51034e89507SJohannes Berg 	 * hash table and inserting into it.
51134e89507SJohannes Berg 	 *
51234e89507SJohannes Berg 	 * However, it is not racy against itself because it keeps
5138c71df7aSGuy Eilam 	 * the mutex locked.
51434e89507SJohannes Berg 	 */
51534e89507SJohannes Berg 
51634e89507SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
5172a33bee2SGuy Eilam 	/*
5182a33bee2SGuy Eilam 	 * check if STA exists already.
5192a33bee2SGuy Eilam 	 * only accept a scenario of a second call to sta_info_insert_non_ibss
5202a33bee2SGuy Eilam 	 * with a dummy station entry that was inserted earlier
5212a33bee2SGuy Eilam 	 * in that case - assume that the dummy station flag should
5222a33bee2SGuy Eilam 	 * be removed.
5232a33bee2SGuy Eilam 	 */
5242a33bee2SGuy Eilam 	exist_sta = sta_info_get_bss_rx(sdata, sta->sta.addr);
5252a33bee2SGuy Eilam 	if (exist_sta) {
5262a33bee2SGuy Eilam 		if (exist_sta == sta && sta->dummy) {
5272a33bee2SGuy Eilam 			dummy_reinsert = true;
5282a33bee2SGuy Eilam 		} else {
52934e89507SJohannes Berg 			spin_unlock_irqrestore(&local->sta_lock, flags);
53038a679a5SJouni Malinen 			mutex_unlock(&local->sta_mtx);
53134e89507SJohannes Berg 			rcu_read_lock();
5328c71df7aSGuy Eilam 			return -EEXIST;
53334e89507SJohannes Berg 		}
5342a33bee2SGuy Eilam 	}
53534e89507SJohannes Berg 
53634e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
53734e89507SJohannes Berg 
5382a33bee2SGuy Eilam 	err = sta_info_finish_insert(sta, false, dummy_reinsert);
53934e89507SJohannes Berg 	if (err) {
54034e89507SJohannes Berg 		mutex_unlock(&local->sta_mtx);
54134e89507SJohannes Berg 		rcu_read_lock();
5428c71df7aSGuy Eilam 		return err;
54332bfd35dSJohannes Berg 	}
544d0709a65SJohannes Berg 
545f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
5462a33bee2SGuy Eilam 	wiphy_debug(local->hw.wiphy, "Inserted %sSTA %pM\n",
5472a33bee2SGuy Eilam 			sta->dummy ? "dummy " : "", sta->sta.addr);
548f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
549f0706e82SJiri Benc 
55034e89507SJohannes Berg 	/* move reference to rcu-protected */
55134e89507SJohannes Berg 	rcu_read_lock();
55234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
553e9f207f0SJiri Benc 
55473651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
55573651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
55673651ee6SJohannes Berg 
55773651ee6SJohannes Berg 	return 0;
5588c71df7aSGuy Eilam }
5598c71df7aSGuy Eilam 
5608c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
5618c71df7aSGuy Eilam {
5628c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
5638c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
5648c71df7aSGuy Eilam 	int err = 0;
5658c71df7aSGuy Eilam 
5668c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
5678c71df7aSGuy Eilam 	if (err) {
5688c71df7aSGuy Eilam 		rcu_read_lock();
5698c71df7aSGuy Eilam 		goto out_free;
5708c71df7aSGuy Eilam 	}
5718c71df7aSGuy Eilam 
5728c71df7aSGuy Eilam 	/*
5738c71df7aSGuy Eilam 	 * In ad-hoc mode, we sometimes need to insert stations
5748c71df7aSGuy Eilam 	 * from tasklet context from the RX path. To avoid races,
5758c71df7aSGuy Eilam 	 * always do so in that case -- see the comment below.
5768c71df7aSGuy Eilam 	 */
5778c71df7aSGuy Eilam 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
5788c71df7aSGuy Eilam 		err = sta_info_insert_ibss(sta);
5798c71df7aSGuy Eilam 		if (err)
5808c71df7aSGuy Eilam 			goto out_free;
5818c71df7aSGuy Eilam 
5828c71df7aSGuy Eilam 		return 0;
5838c71df7aSGuy Eilam 	}
5848c71df7aSGuy Eilam 
5858c71df7aSGuy Eilam 	/*
5868c71df7aSGuy Eilam 	 * It might seem that the function called below is in race against
5878c71df7aSGuy Eilam 	 * the function call above that atomically inserts the station... That,
5888c71df7aSGuy Eilam 	 * however, is not true because the above code can only
5898c71df7aSGuy Eilam 	 * be invoked for IBSS interfaces, and the below code will
5908c71df7aSGuy Eilam 	 * not be -- and the two do not race against each other as
5918c71df7aSGuy Eilam 	 * the hash table also keys off the interface.
5928c71df7aSGuy Eilam 	 */
5938c71df7aSGuy Eilam 
5948c71df7aSGuy Eilam 	might_sleep();
5958c71df7aSGuy Eilam 
5968c71df7aSGuy Eilam 	mutex_lock(&local->sta_mtx);
5978c71df7aSGuy Eilam 
5988c71df7aSGuy Eilam 	err = sta_info_insert_non_ibss(sta);
5998c71df7aSGuy Eilam 	if (err)
6008c71df7aSGuy Eilam 		goto out_free;
6018c71df7aSGuy Eilam 
6028c71df7aSGuy Eilam 	return 0;
60393e5deb1SJohannes Berg  out_free:
60493e5deb1SJohannes Berg 	BUG_ON(!err);
60593e5deb1SJohannes Berg 	__sta_info_free(local, sta);
60693e5deb1SJohannes Berg 	return err;
607f0706e82SJiri Benc }
608f0706e82SJiri Benc 
60934e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
61034e89507SJohannes Berg {
61134e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
61234e89507SJohannes Berg 
61334e89507SJohannes Berg 	rcu_read_unlock();
61434e89507SJohannes Berg 
61534e89507SJohannes Berg 	return err;
61634e89507SJohannes Berg }
61734e89507SJohannes Berg 
6182a33bee2SGuy Eilam /* Caller must hold sta->local->sta_mtx */
6192a33bee2SGuy Eilam int sta_info_reinsert(struct sta_info *sta)
6202a33bee2SGuy Eilam {
6212a33bee2SGuy Eilam 	struct ieee80211_local *local = sta->local;
6222a33bee2SGuy Eilam 	int err = 0;
6232a33bee2SGuy Eilam 
6242a33bee2SGuy Eilam 	err = sta_info_insert_check(sta);
6252a33bee2SGuy Eilam 	if (err) {
6262a33bee2SGuy Eilam 		mutex_unlock(&local->sta_mtx);
6272a33bee2SGuy Eilam 		return err;
6282a33bee2SGuy Eilam 	}
6292a33bee2SGuy Eilam 
6302a33bee2SGuy Eilam 	might_sleep();
6312a33bee2SGuy Eilam 
6322a33bee2SGuy Eilam 	err = sta_info_insert_non_ibss(sta);
6332a33bee2SGuy Eilam 	rcu_read_unlock();
6342a33bee2SGuy Eilam 	return err;
6352a33bee2SGuy Eilam }
6362a33bee2SGuy Eilam 
637004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
638004c872eSJohannes Berg {
639004c872eSJohannes Berg 	/*
640004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
641004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
642004c872eSJohannes Berg 	 */
643004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
644004c872eSJohannes Berg }
645004c872eSJohannes Berg 
646004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
647004c872eSJohannes Berg {
648004c872eSJohannes Berg 	/*
649004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
650004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
651004c872eSJohannes Berg 	 */
652004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
653004c872eSJohannes Berg }
654004c872eSJohannes Berg 
655948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
656948d887dSJohannes Berg {
657948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
658948d887dSJohannes Berg 	switch (ac) {
659948d887dSJohannes Berg 	case IEEE80211_AC_VO:
660948d887dSJohannes Berg 		return BIT(6) | BIT(7);
661948d887dSJohannes Berg 	case IEEE80211_AC_VI:
662948d887dSJohannes Berg 		return BIT(4) | BIT(5);
663948d887dSJohannes Berg 	case IEEE80211_AC_BE:
664948d887dSJohannes Berg 		return BIT(0) | BIT(3);
665948d887dSJohannes Berg 	case IEEE80211_AC_BK:
666948d887dSJohannes Berg 		return BIT(1) | BIT(2);
667948d887dSJohannes Berg 	default:
668948d887dSJohannes Berg 		WARN_ON(1);
669948d887dSJohannes Berg 		return 0;
670948d887dSJohannes Berg 	}
671948d887dSJohannes Berg }
672948d887dSJohannes Berg 
673c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
674004c872eSJohannes Berg {
675c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
676c868cb35SJohannes Berg 	struct ieee80211_if_ap *bss = sta->sdata->bss;
677d0709a65SJohannes Berg 	unsigned long flags;
678948d887dSJohannes Berg 	bool indicate_tim = false;
679948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
680948d887dSJohannes Berg 	int ac;
681004c872eSJohannes Berg 
682c868cb35SJohannes Berg 	if (WARN_ON_ONCE(!sta->sdata->bss))
683c868cb35SJohannes Berg 		return;
6843e122be0SJohannes Berg 
685c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
686c868cb35SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
687c868cb35SJohannes Berg 		return;
688004c872eSJohannes Berg 
689c868cb35SJohannes Berg 	if (sta->dead)
690c868cb35SJohannes Berg 		goto done;
6913e122be0SJohannes Berg 
692948d887dSJohannes Berg 	/*
693948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
694948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
695948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
696948d887dSJohannes Berg 	 * non-enabled ones.
697948d887dSJohannes Berg 	 */
698948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
699948d887dSJohannes Berg 		ignore_for_tim = 0;
700948d887dSJohannes Berg 
701948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
702948d887dSJohannes Berg 		unsigned long tids;
703948d887dSJohannes Berg 
704948d887dSJohannes Berg 		if (ignore_for_tim & BIT(ac))
705948d887dSJohannes Berg 			continue;
706948d887dSJohannes Berg 
707948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
708948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
709948d887dSJohannes Berg 		if (indicate_tim)
710948d887dSJohannes Berg 			break;
711948d887dSJohannes Berg 
712948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
713948d887dSJohannes Berg 
714948d887dSJohannes Berg 		indicate_tim |=
715948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
716948d887dSJohannes Berg 	}
717c868cb35SJohannes Berg 
718c868cb35SJohannes Berg  done:
719c868cb35SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
720c868cb35SJohannes Berg 
721948d887dSJohannes Berg 	if (indicate_tim)
722c868cb35SJohannes Berg 		__bss_tim_set(bss, sta->sta.aid);
723c868cb35SJohannes Berg 	else
72417741cdcSJohannes Berg 		__bss_tim_clear(bss, sta->sta.aid);
7253e122be0SJohannes Berg 
726c868cb35SJohannes Berg 	if (local->ops->set_tim) {
727c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
728948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
729c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
730004c872eSJohannes Berg 	}
731004c872eSJohannes Berg 
732c868cb35SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
733004c872eSJohannes Berg }
734004c872eSJohannes Berg 
735cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
736f0706e82SJiri Benc {
737e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
738f0706e82SJiri Benc 	int timeout;
739f0706e82SJiri Benc 
740f0706e82SJiri Benc 	if (!skb)
741cd0b8d89SJohannes Berg 		return false;
742f0706e82SJiri Benc 
743e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
744f0706e82SJiri Benc 
745f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
74657c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
74757c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
74857c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
749f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
750f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
751e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
752f0706e82SJiri Benc }
753f0706e82SJiri Benc 
754f0706e82SJiri Benc 
755948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
756948d887dSJohannes Berg 						struct sta_info *sta, int ac)
757f0706e82SJiri Benc {
758f0706e82SJiri Benc 	unsigned long flags;
759f0706e82SJiri Benc 	struct sk_buff *skb;
760f0706e82SJiri Benc 
76160750397SJohannes Berg 	/*
76260750397SJohannes Berg 	 * First check for frames that should expire on the filtered
76360750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
76460750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
76560750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
76660750397SJohannes Berg 	 * total_ps_buffered counter.
76760750397SJohannes Berg 	 */
76860750397SJohannes Berg 	for (;;) {
769948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
770948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
77160750397SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
772948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
77360750397SJohannes Berg 		else
77460750397SJohannes Berg 			skb = NULL;
775948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
77660750397SJohannes Berg 
77760750397SJohannes Berg 		/*
77860750397SJohannes Berg 		 * Frames are queued in order, so if this one
77960750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
78060750397SJohannes Berg 		 * we actually reached the end of the queue we
78160750397SJohannes Berg 		 * also need to stop, of course.
78260750397SJohannes Berg 		 */
78360750397SJohannes Berg 		if (!skb)
78460750397SJohannes Berg 			break;
78560750397SJohannes Berg 		dev_kfree_skb(skb);
78660750397SJohannes Berg 	}
78760750397SJohannes Berg 
78860750397SJohannes Berg 	/*
78960750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
79060750397SJohannes Berg 	 * only find something if the filtered queue was emptied
79160750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
79260750397SJohannes Berg 	 * buffered frames.
79360750397SJohannes Berg 	 */
794f0706e82SJiri Benc 	for (;;) {
795948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
796948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
79757c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
798948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
799836341a7SJohannes Berg 		else
800f0706e82SJiri Benc 			skb = NULL;
801948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
802f0706e82SJiri Benc 
80360750397SJohannes Berg 		/*
80460750397SJohannes Berg 		 * frames are queued in order, so if this one
80560750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
80660750397SJohannes Berg 		 * the queue) we can stop testing
80760750397SJohannes Berg 		 */
808836341a7SJohannes Berg 		if (!skb)
809836341a7SJohannes Berg 			break;
810836341a7SJohannes Berg 
811f0706e82SJiri Benc 		local->total_ps_buffered--;
812f4ea83ddSJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
8130c68ae26SJohannes Berg 		printk(KERN_DEBUG "Buffered frame expired (STA %pM)\n",
8140c68ae26SJohannes Berg 		       sta->sta.addr);
815f4ea83ddSJohannes Berg #endif
816f0706e82SJiri Benc 		dev_kfree_skb(skb);
817f0706e82SJiri Benc 	}
8183393a608SJuuso Oikarinen 
81960750397SJohannes Berg 	/*
82060750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
82160750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
82260750397SJohannes Berg 	 * frames.
82360750397SJohannes Berg 	 */
82460750397SJohannes Berg 	sta_info_recalc_tim(sta);
82560750397SJohannes Berg 
82660750397SJohannes Berg 	/*
82760750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
82860750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
82960750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
83060750397SJohannes Berg 	 */
831948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
832948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
833948d887dSJohannes Berg }
834948d887dSJohannes Berg 
835948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
836948d887dSJohannes Berg 					     struct sta_info *sta)
837948d887dSJohannes Berg {
838948d887dSJohannes Berg 	bool have_buffered = false;
839948d887dSJohannes Berg 	int ac;
840948d887dSJohannes Berg 
841948d887dSJohannes Berg 	/* This is only necessary for stations on BSS interfaces */
842948d887dSJohannes Berg 	if (!sta->sdata->bss)
843948d887dSJohannes Berg 		return false;
844948d887dSJohannes Berg 
845948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
846948d887dSJohannes Berg 		have_buffered |=
847948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
848948d887dSJohannes Berg 
849948d887dSJohannes Berg 	return have_buffered;
850f0706e82SJiri Benc }
851f0706e82SJiri Benc 
85234e89507SJohannes Berg static int __must_check __sta_info_destroy(struct sta_info *sta)
85334e89507SJohannes Berg {
85434e89507SJohannes Berg 	struct ieee80211_local *local;
85534e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
85634e89507SJohannes Berg 	unsigned long flags;
857948d887dSJohannes Berg 	int ret, i, ac;
85834e89507SJohannes Berg 
85934e89507SJohannes Berg 	might_sleep();
86034e89507SJohannes Berg 
86134e89507SJohannes Berg 	if (!sta)
86234e89507SJohannes Berg 		return -ENOENT;
86334e89507SJohannes Berg 
86434e89507SJohannes Berg 	local = sta->local;
86534e89507SJohannes Berg 	sdata = sta->sdata;
86634e89507SJohannes Berg 
867098a6070SJohannes Berg 	/*
868098a6070SJohannes Berg 	 * Before removing the station from the driver and
869098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
870098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
871098a6070SJohannes Berg 	 * will be sufficient.
872098a6070SJohannes Berg 	 */
873c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
87453f73c09SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, true);
875098a6070SJohannes Berg 
87634e89507SJohannes Berg 	spin_lock_irqsave(&local->sta_lock, flags);
87734e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
87834e89507SJohannes Berg 	/* this might still be the pending list ... which is fine */
87934e89507SJohannes Berg 	if (!ret)
88034e89507SJohannes Berg 		list_del(&sta->list);
88134e89507SJohannes Berg 	spin_unlock_irqrestore(&local->sta_lock, flags);
88234e89507SJohannes Berg 	if (ret)
88334e89507SJohannes Berg 		return ret;
88434e89507SJohannes Berg 
8858cb23153SJohannes Berg 	mutex_lock(&local->key_mtx);
886e31b8213SJohannes Berg 	for (i = 0; i < NUM_DEFAULT_KEYS; i++)
88740b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
888e31b8213SJohannes Berg 	if (sta->ptk)
88940b275b6SJohannes Berg 		__ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
8908cb23153SJohannes Berg 	mutex_unlock(&local->key_mtx);
89134e89507SJohannes Berg 
89234e89507SJohannes Berg 	sta->dead = true;
89334e89507SJohannes Berg 
894c2c98fdeSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
895c2c98fdeSJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
89634e89507SJohannes Berg 		BUG_ON(!sdata->bss);
89734e89507SJohannes Berg 
898c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_STA);
899c2c98fdeSJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
900c2c98fdeSJohannes Berg 
90134e89507SJohannes Berg 		atomic_dec(&sdata->bss->num_sta_ps);
902c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
90334e89507SJohannes Berg 	}
90434e89507SJohannes Berg 
90534e89507SJohannes Berg 	local->num_sta--;
90634e89507SJohannes Berg 	local->sta_generation++;
90734e89507SJohannes Berg 
90834e89507SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
90934e89507SJohannes Berg 		rcu_assign_pointer(sdata->u.vlan.sta, NULL);
91034e89507SJohannes Berg 
91134e89507SJohannes Berg 	if (sta->uploaded) {
91234e89507SJohannes Berg 		if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
91334e89507SJohannes Berg 			sdata = container_of(sdata->bss,
91434e89507SJohannes Berg 					     struct ieee80211_sub_if_data,
91534e89507SJohannes Berg 					     u.ap);
91634e89507SJohannes Berg 		drv_sta_remove(local, sdata, &sta->sta);
91734e89507SJohannes Berg 		sdata = sta->sdata;
91834e89507SJohannes Berg 	}
91934e89507SJohannes Berg 
920e64b3795SJohannes Berg 	/*
921e64b3795SJohannes Berg 	 * At this point, after we wait for an RCU grace period,
922e64b3795SJohannes Berg 	 * neither mac80211 nor the driver can reference this
923e64b3795SJohannes Berg 	 * sta struct any more except by still existing timers
924e64b3795SJohannes Berg 	 * associated with this station that we clean up below.
925e64b3795SJohannes Berg 	 */
926e64b3795SJohannes Berg 	synchronize_rcu();
927e64b3795SJohannes Berg 
928948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
929948d887dSJohannes Berg 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
930948d887dSJohannes Berg 		__skb_queue_purge(&sta->ps_tx_buf[ac]);
931948d887dSJohannes Berg 		__skb_queue_purge(&sta->tx_filtered[ac]);
932948d887dSJohannes Berg 	}
933c868cb35SJohannes Berg 
93434e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
935e64b3795SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
93634e89507SJohannes Berg 		mesh_accept_plinks_update(sdata);
93734e89507SJohannes Berg #endif
93834e89507SJohannes Berg 
93934e89507SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
9400fb9a9ecSJoe Perches 	wiphy_debug(local->hw.wiphy, "Removed STA %pM\n", sta->sta.addr);
94134e89507SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
94234e89507SJohannes Berg 	cancel_work_sync(&sta->drv_unblock_wk);
94334e89507SJohannes Berg 
944ec15e68bSJouni Malinen 	cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
945ec15e68bSJouni Malinen 
94634e89507SJohannes Berg 	rate_control_remove_sta_debugfs(sta);
94734e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
94834e89507SJohannes Berg 
94934e89507SJohannes Berg #ifdef CONFIG_MAC80211_MESH
95034e89507SJohannes Berg 	if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
95134e89507SJohannes Berg 		mesh_plink_deactivate(sta);
95234e89507SJohannes Berg 		del_timer_sync(&sta->plink_timer);
95334e89507SJohannes Berg 	}
95434e89507SJohannes Berg #endif
95534e89507SJohannes Berg 
95634e89507SJohannes Berg 	__sta_info_free(local, sta);
95734e89507SJohannes Berg 
95834e89507SJohannes Berg 	return 0;
95934e89507SJohannes Berg }
96034e89507SJohannes Berg 
96134e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
96234e89507SJohannes Berg {
96334e89507SJohannes Berg 	struct sta_info *sta;
96434e89507SJohannes Berg 	int ret;
96534e89507SJohannes Berg 
96634e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9672a33bee2SGuy Eilam 	sta = sta_info_get_rx(sdata, addr);
96834e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
96934e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
97034e89507SJohannes Berg 
97134e89507SJohannes Berg 	return ret;
97234e89507SJohannes Berg }
97334e89507SJohannes Berg 
97434e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
97534e89507SJohannes Berg 			      const u8 *addr)
97634e89507SJohannes Berg {
97734e89507SJohannes Berg 	struct sta_info *sta;
97834e89507SJohannes Berg 	int ret;
97934e89507SJohannes Berg 
98034e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
9812a33bee2SGuy Eilam 	sta = sta_info_get_bss_rx(sdata, addr);
98234e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
98334e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
98434e89507SJohannes Berg 
98534e89507SJohannes Berg 	return ret;
98634e89507SJohannes Berg }
987f0706e82SJiri Benc 
988f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
989f0706e82SJiri Benc {
990f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
991f0706e82SJiri Benc 	struct sta_info *sta;
9923393a608SJuuso Oikarinen 	bool timer_needed = false;
993f0706e82SJiri Benc 
994d0709a65SJohannes Berg 	rcu_read_lock();
995d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
9963393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
9973393a608SJuuso Oikarinen 			timer_needed = true;
998d0709a65SJohannes Berg 	rcu_read_unlock();
999f0706e82SJiri Benc 
10005bb644a0SJohannes Berg 	if (local->quiescing)
10015bb644a0SJohannes Berg 		return;
10025bb644a0SJohannes Berg 
10033393a608SJuuso Oikarinen 	if (!timer_needed)
10043393a608SJuuso Oikarinen 		return;
10053393a608SJuuso Oikarinen 
100626d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
100726d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1008f0706e82SJiri Benc }
1009f0706e82SJiri Benc 
1010f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
1011f0706e82SJiri Benc {
1012d0709a65SJohannes Berg 	spin_lock_init(&local->sta_lock);
101334e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1014f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
101534e89507SJohannes Berg 	INIT_LIST_HEAD(&local->sta_pending_list);
101634e89507SJohannes Berg 	INIT_WORK(&local->sta_finish_work, sta_info_finish_work);
1017f0706e82SJiri Benc 
1018b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1019b24b8a24SPavel Emelyanov 		    (unsigned long)local);
1020f0706e82SJiri Benc }
1021f0706e82SJiri Benc 
1022f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1023f0706e82SJiri Benc {
1024f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
1025be8755e1SMichael Wu 	sta_info_flush(local, NULL);
1026f0706e82SJiri Benc }
1027f0706e82SJiri Benc 
1028f0706e82SJiri Benc /**
1029f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
103044213b5eSJohannes Berg  *
103144213b5eSJohannes Berg  * Returns the number of removed STA entries.
103244213b5eSJohannes Berg  *
1033f0706e82SJiri Benc  * @local: local interface data
1034d0709a65SJohannes Berg  * @sdata: matching rule for the net device (sta->dev) or %NULL to match all STAs
1035f0706e82SJiri Benc  */
103644213b5eSJohannes Berg int sta_info_flush(struct ieee80211_local *local,
1037d0709a65SJohannes Berg 		   struct ieee80211_sub_if_data *sdata)
1038f0706e82SJiri Benc {
1039f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
104044213b5eSJohannes Berg 	int ret = 0;
1041f0706e82SJiri Benc 
1042d0709a65SJohannes Berg 	might_sleep();
1043d0709a65SJohannes Berg 
104434e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1045d0709a65SJohannes Berg 
104634e89507SJohannes Berg 	sta_info_finish_pending(local);
104734e89507SJohannes Berg 
104834e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
104934e89507SJohannes Berg 		if (!sdata || sdata == sta->sdata)
105034e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
105134e89507SJohannes Berg 	}
105234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
105344213b5eSJohannes Berg 
105444213b5eSJohannes Berg 	return ret;
1055f0706e82SJiri Benc }
1056dc6676b7SJohannes Berg 
105724723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
105824723d1bSJohannes Berg 			  unsigned long exp_time)
105924723d1bSJohannes Berg {
106024723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
106124723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
106224723d1bSJohannes Berg 
106334e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
106424723d1bSJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
106524723d1bSJohannes Berg 		if (time_after(jiffies, sta->last_rx + exp_time)) {
106624723d1bSJohannes Berg #ifdef CONFIG_MAC80211_IBSS_DEBUG
10670c68ae26SJohannes Berg 			printk(KERN_DEBUG "%s: expiring inactive STA %pM\n",
106847846c9bSJohannes Berg 			       sdata->name, sta->sta.addr);
106924723d1bSJohannes Berg #endif
107034e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
107124723d1bSJohannes Berg 		}
107234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
107324723d1bSJohannes Berg }
107417741cdcSJohannes Berg 
1075686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1076686b9cb9SBen Greear 					       const u8 *addr,
1077686b9cb9SBen Greear 					       const u8 *localaddr)
107817741cdcSJohannes Berg {
1079abe60632SJohannes Berg 	struct sta_info *sta, *nxt;
108017741cdcSJohannes Berg 
1081686b9cb9SBen Greear 	/*
1082686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1083686b9cb9SBen Greear 	 * ... first in list.
1084686b9cb9SBen Greear 	 */
1085f7c65594SJohannes Berg 	for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1086686b9cb9SBen Greear 		if (localaddr &&
1087686b9cb9SBen Greear 		    compare_ether_addr(sta->sdata->vif.addr, localaddr) != 0)
1088686b9cb9SBen Greear 			continue;
1089f7c65594SJohannes Berg 		if (!sta->uploaded)
1090f7c65594SJohannes Berg 			return NULL;
109117741cdcSJohannes Berg 		return &sta->sta;
1092f7c65594SJohannes Berg 	}
1093f7c65594SJohannes Berg 
1094abe60632SJohannes Berg 	return NULL;
109517741cdcSJohannes Berg }
1096686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
10975ed176e1SJohannes Berg 
10985ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
10995ed176e1SJohannes Berg 					 const u8 *addr)
11005ed176e1SJohannes Berg {
1101f7c65594SJohannes Berg 	struct sta_info *sta;
11025ed176e1SJohannes Berg 
11035ed176e1SJohannes Berg 	if (!vif)
11045ed176e1SJohannes Berg 		return NULL;
11055ed176e1SJohannes Berg 
1106f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1107f7c65594SJohannes Berg 	if (!sta)
1108f7c65594SJohannes Berg 		return NULL;
11095ed176e1SJohannes Berg 
1110f7c65594SJohannes Berg 	if (!sta->uploaded)
1111f7c65594SJohannes Berg 		return NULL;
1112f7c65594SJohannes Berg 
1113f7c65594SJohannes Berg 	return &sta->sta;
11145ed176e1SJohannes Berg }
111517741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1116af818581SJohannes Berg 
111750a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta)
111850a9432dSJohannes Berg {
111950a9432dSJohannes Berg 	struct sta_info *sta = _sta;
112050a9432dSJohannes Berg 
1121c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1122c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_STA);
112350a9432dSJohannes Berg }
112450a9432dSJohannes Berg 
1125af818581SJohannes Berg /* powersave support code */
1126af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1127af818581SJohannes Berg {
1128af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1129af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1130948d887dSJohannes Berg 	struct sk_buff_head pending;
1131948d887dSJohannes Berg 	int filtered = 0, buffered = 0, ac;
1132af818581SJohannes Berg 
1133c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
113447086fc5SJohannes Berg 
1135948d887dSJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(STA_TID_NUM) > 1);
1136948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1137948d887dSJohannes Berg 
1138d057e5a3SArik Nemtsov 	if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
113912375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1140af818581SJohannes Berg 
1141948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1142948d887dSJohannes Berg 
1143af818581SJohannes Berg 	/* Send all buffered frames to the station */
1144948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1145948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1146948d887dSJohannes Berg 
1147948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1148948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1149948d887dSJohannes Berg 		filtered += tmp - count;
1150948d887dSJohannes Berg 		count = tmp;
1151948d887dSJohannes Berg 
1152948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1153948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1154948d887dSJohannes Berg 		buffered += tmp - count;
1155948d887dSJohannes Berg 	}
1156948d887dSJohannes Berg 
1157948d887dSJohannes Berg 	ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
1158948d887dSJohannes Berg 
1159af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1160af818581SJohannes Berg 
1161c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1162c868cb35SJohannes Berg 
1163af818581SJohannes Berg #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
1164af818581SJohannes Berg 	printk(KERN_DEBUG "%s: STA %pM aid %d sending %d filtered/%d PS frames "
116547846c9bSJohannes Berg 	       "since STA not sleeping anymore\n", sdata->name,
1166948d887dSJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1167af818581SJohannes Berg #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
1168af818581SJohannes Berg }
1169af818581SJohannes Berg 
1170ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1171ce662b44SJohannes Berg 					 struct sta_info *sta, int tid,
1172*40b96408SJohannes Berg 					 enum ieee80211_frame_release_type reason)
1173ce662b44SJohannes Berg {
1174ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1175ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1176ce662b44SJohannes Berg 	struct sk_buff *skb;
1177ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1178ce662b44SJohannes Berg 	__le16 fc;
1179c2c98fdeSJohannes Berg 	bool qos = test_sta_flag(sta, WLAN_STA_WME);
1180ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
1181ce662b44SJohannes Berg 
1182ce662b44SJohannes Berg 	if (qos) {
1183ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1184ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1185ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1186ce662b44SJohannes Berg 	} else {
1187ce662b44SJohannes Berg 		size -= 2;
1188ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1189ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1190ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1191ce662b44SJohannes Berg 	}
1192ce662b44SJohannes Berg 
1193ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1194ce662b44SJohannes Berg 	if (!skb)
1195ce662b44SJohannes Berg 		return;
1196ce662b44SJohannes Berg 
1197ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1198ce662b44SJohannes Berg 
1199ce662b44SJohannes Berg 	nullfunc = (void *) skb_put(skb, size);
1200ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1201ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1202ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1203ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1204ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1205ce662b44SJohannes Berg 
1206ce662b44SJohannes Berg 	if (qos) {
1207ce662b44SJohannes Berg 		skb->priority = tid;
1208ce662b44SJohannes Berg 
1209ce662b44SJohannes Berg 		skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1210ce662b44SJohannes Berg 
1211ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1212ce662b44SJohannes Berg 
1213*40b96408SJohannes Berg 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1214ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1215ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1216ce662b44SJohannes Berg 	}
1217ce662b44SJohannes Berg 
1218ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1219ce662b44SJohannes Berg 
1220ce662b44SJohannes Berg 	/*
1221ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1222ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1223deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1224deeaee19SJohannes Berg 	 * ends the poll/service period.
1225ce662b44SJohannes Berg 	 */
1226deeaee19SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE |
1227deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1228ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1229ce662b44SJohannes Berg 
1230*40b96408SJohannes Berg 	drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false);
1231*40b96408SJohannes Berg 
1232ce662b44SJohannes Berg 	ieee80211_xmit(sdata, skb);
1233ce662b44SJohannes Berg }
1234ce662b44SJohannes Berg 
123547086fc5SJohannes Berg static void
123647086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta,
123747086fc5SJohannes Berg 				  int n_frames, u8 ignored_acs,
123847086fc5SJohannes Berg 				  enum ieee80211_frame_release_type reason)
1239af818581SJohannes Berg {
1240af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1241af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
12424049e09aSJohannes Berg 	bool found = false;
1243948d887dSJohannes Berg 	bool more_data = false;
1244948d887dSJohannes Berg 	int ac;
12454049e09aSJohannes Berg 	unsigned long driver_release_tids = 0;
124647086fc5SJohannes Berg 	struct sk_buff_head frames;
124747086fc5SJohannes Berg 
1248deeaee19SJohannes Berg 	/* Service or PS-Poll period starts */
1249c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_SP);
1250deeaee19SJohannes Berg 
125147086fc5SJohannes Berg 	__skb_queue_head_init(&frames);
1252af818581SJohannes Berg 
1253948d887dSJohannes Berg 	/*
125447086fc5SJohannes Berg 	 * Get response frame(s) and more data bit for it.
1255948d887dSJohannes Berg 	 */
1256948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
12574049e09aSJohannes Berg 		unsigned long tids;
12584049e09aSJohannes Berg 
125947086fc5SJohannes Berg 		if (ignored_acs & BIT(ac))
1260948d887dSJohannes Berg 			continue;
1261948d887dSJohannes Berg 
12624049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
12634049e09aSJohannes Berg 
12644049e09aSJohannes Berg 		if (!found) {
12654049e09aSJohannes Berg 			driver_release_tids = sta->driver_buffered_tids & tids;
12664049e09aSJohannes Berg 			if (driver_release_tids) {
12674049e09aSJohannes Berg 				found = true;
12684049e09aSJohannes Berg 			} else {
126947086fc5SJohannes Berg 				struct sk_buff *skb;
127047086fc5SJohannes Berg 
127147086fc5SJohannes Berg 				while (n_frames > 0) {
1272948d887dSJohannes Berg 					skb = skb_dequeue(&sta->tx_filtered[ac]);
1273948d887dSJohannes Berg 					if (!skb) {
127447086fc5SJohannes Berg 						skb = skb_dequeue(
127547086fc5SJohannes Berg 							&sta->ps_tx_buf[ac]);
1276af818581SJohannes Berg 						if (skb)
1277af818581SJohannes Berg 							local->total_ps_buffered--;
1278af818581SJohannes Berg 					}
127947086fc5SJohannes Berg 					if (!skb)
128047086fc5SJohannes Berg 						break;
128147086fc5SJohannes Berg 					n_frames--;
12824049e09aSJohannes Berg 					found = true;
128347086fc5SJohannes Berg 					__skb_queue_tail(&frames, skb);
128447086fc5SJohannes Berg 				}
1285948d887dSJohannes Berg 			}
1286948d887dSJohannes Berg 
12874049e09aSJohannes Berg 			/*
12884049e09aSJohannes Berg 			 * If the driver has data on more than one TID then
12894049e09aSJohannes Berg 			 * certainly there's more data if we release just a
12904049e09aSJohannes Berg 			 * single frame now (from a single TID).
12914049e09aSJohannes Berg 			 */
129247086fc5SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
129347086fc5SJohannes Berg 			    hweight16(driver_release_tids) > 1) {
12944049e09aSJohannes Berg 				more_data = true;
12954049e09aSJohannes Berg 				driver_release_tids =
12964049e09aSJohannes Berg 					BIT(ffs(driver_release_tids) - 1);
12974049e09aSJohannes Berg 				break;
12984049e09aSJohannes Berg 			}
12994049e09aSJohannes Berg 		}
1300948d887dSJohannes Berg 
1301948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1302948d887dSJohannes Berg 		    !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1303948d887dSJohannes Berg 			more_data = true;
1304948d887dSJohannes Berg 			break;
1305948d887dSJohannes Berg 		}
1306948d887dSJohannes Berg 	}
1307af818581SJohannes Berg 
13084049e09aSJohannes Berg 	if (!found) {
1309ce662b44SJohannes Berg 		int tid;
13104049e09aSJohannes Berg 
1311ce662b44SJohannes Berg 		/*
1312ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1313ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1314ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1315ce662b44SJohannes Berg 		 *
1316ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1317ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1318ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1319ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1320ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1321ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1322ce662b44SJohannes Berg 		 *
1323ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1324ce662b44SJohannes Berg 		 */
1325ce662b44SJohannes Berg 
1326ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1327ce662b44SJohannes Berg 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1328ce662b44SJohannes Berg 
1329*40b96408SJohannes Berg 		ieee80211_send_null_response(sdata, sta, tid, reason);
13304049e09aSJohannes Berg 		return;
13314049e09aSJohannes Berg 	}
13324049e09aSJohannes Berg 
133347086fc5SJohannes Berg 	if (!driver_release_tids) {
133447086fc5SJohannes Berg 		struct sk_buff_head pending;
133547086fc5SJohannes Berg 		struct sk_buff *skb;
1336*40b96408SJohannes Berg 		int num = 0;
1337*40b96408SJohannes Berg 		u16 tids = 0;
133847086fc5SJohannes Berg 
133947086fc5SJohannes Berg 		skb_queue_head_init(&pending);
134047086fc5SJohannes Berg 
134147086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1342af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
134347086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
1344*40b96408SJohannes Berg 			u8 *qoshdr = NULL;
1345*40b96408SJohannes Berg 
1346*40b96408SJohannes Berg 			num++;
1347af818581SJohannes Berg 
1348af818581SJohannes Berg 			/*
134947086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
135047086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
135147086fc5SJohannes Berg 			 * exchange.
1352af818581SJohannes Berg 			 */
135347086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_CTL_POLL_RESPONSE;
1354af818581SJohannes Berg 
135547086fc5SJohannes Berg 			/*
135647086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
135747086fc5SJohannes Berg 			 * more buffered frames for this STA
135847086fc5SJohannes Berg 			 */
1359948d887dSJohannes Berg 			if (!more_data)
136047086fc5SJohannes Berg 				hdr->frame_control &=
136147086fc5SJohannes Berg 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1362af818581SJohannes Berg 			else
136347086fc5SJohannes Berg 				hdr->frame_control |=
136447086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1365af818581SJohannes Berg 
1366*40b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
1367*40b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
1368*40b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
1369*40b96408SJohannes Berg 
137047086fc5SJohannes Berg 			/* set EOSP for the frame */
1371*40b96408SJohannes Berg 			if (reason == IEEE80211_FRAME_RELEASE_UAPSD &&
1372*40b96408SJohannes Berg 			    qoshdr && skb_queue_empty(&frames))
1373*40b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1374deeaee19SJohannes Berg 
137547086fc5SJohannes Berg 			info->flags |= IEEE80211_TX_STATUS_EOSP |
137647086fc5SJohannes Berg 				       IEEE80211_TX_CTL_REQ_TX_STATUS;
137747086fc5SJohannes Berg 
1378*40b96408SJohannes Berg 			if (qoshdr)
1379*40b96408SJohannes Berg 				tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK);
1380*40b96408SJohannes Berg 			else
1381*40b96408SJohannes Berg 				tids |= BIT(0);
1382*40b96408SJohannes Berg 
138347086fc5SJohannes Berg 			__skb_queue_tail(&pending, skb);
138447086fc5SJohannes Berg 		}
138547086fc5SJohannes Berg 
1386*40b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
1387*40b96408SJohannes Berg 					  reason, more_data);
1388*40b96408SJohannes Berg 
138947086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1390af818581SJohannes Berg 
1391c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1392af818581SJohannes Berg 	} else {
1393af818581SJohannes Berg 		/*
13944049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
13954049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
13964049e09aSJohannes Berg 		 * Note that, as per the comment above, it'll also have to see
13974049e09aSJohannes Berg 		 * if there is more than just one frame on the specific TID that
13984049e09aSJohannes Berg 		 * we're releasing from, and it needs to set the more-data bit
13994049e09aSJohannes Berg 		 * accordingly if we tell it that there's no more data. If we do
14004049e09aSJohannes Berg 		 * tell it there's more data, then of course the more-data bit
14014049e09aSJohannes Berg 		 * needs to be set anyway.
1402af818581SJohannes Berg 		 */
14034049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
140447086fc5SJohannes Berg 					    n_frames, reason, more_data);
14054049e09aSJohannes Berg 
14064049e09aSJohannes Berg 		/*
14074049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
14084049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
14094049e09aSJohannes Berg 		 * that the TID became empty before returning here from the
14104049e09aSJohannes Berg 		 * release function.
14114049e09aSJohannes Berg 		 * Either way, however, when the driver tells us that the TID
14124049e09aSJohannes Berg 		 * became empty we'll do the TIM recalculation.
14134049e09aSJohannes Berg 		 */
1414af818581SJohannes Berg 	}
1415af818581SJohannes Berg }
1416af818581SJohannes Berg 
141747086fc5SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
141847086fc5SJohannes Berg {
141947086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
142047086fc5SJohannes Berg 
142147086fc5SJohannes Berg 	/*
142247086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
142347086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
142447086fc5SJohannes Berg 	 * only from the non-enabled ones.
142547086fc5SJohannes Berg 	 */
142647086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
142747086fc5SJohannes Berg 		ignore_for_response = 0;
142847086fc5SJohannes Berg 
142947086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
143047086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
143147086fc5SJohannes Berg }
143247086fc5SJohannes Berg 
143347086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
143447086fc5SJohannes Berg {
143547086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
143647086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
143747086fc5SJohannes Berg 
143847086fc5SJohannes Berg 	/*
143947086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
144047086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
144147086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
144247086fc5SJohannes Berg 	 * actually getting called.
144347086fc5SJohannes Berg 	 */
144447086fc5SJohannes Berg 	if (!delivery_enabled)
144547086fc5SJohannes Berg 		return;
144647086fc5SJohannes Berg 
144747086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
144847086fc5SJohannes Berg 	case 1:
144947086fc5SJohannes Berg 		n_frames = 2;
145047086fc5SJohannes Berg 		break;
145147086fc5SJohannes Berg 	case 2:
145247086fc5SJohannes Berg 		n_frames = 4;
145347086fc5SJohannes Berg 		break;
145447086fc5SJohannes Berg 	case 3:
145547086fc5SJohannes Berg 		n_frames = 6;
145647086fc5SJohannes Berg 		break;
145747086fc5SJohannes Berg 	case 0:
145847086fc5SJohannes Berg 		/* XXX: what is a good value? */
145947086fc5SJohannes Berg 		n_frames = 8;
146047086fc5SJohannes Berg 		break;
146147086fc5SJohannes Berg 	}
146247086fc5SJohannes Berg 
146347086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
146447086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
146547086fc5SJohannes Berg }
146647086fc5SJohannes Berg 
1467af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1468af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1469af818581SJohannes Berg {
1470af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1471af818581SJohannes Berg 
1472b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1473b5878a2dSJohannes Berg 
1474af818581SJohannes Berg 	if (block)
1475c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1476c2c98fdeSJohannes Berg 	else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1477af818581SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1478af818581SJohannes Berg }
1479af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1480dcf55fb5SFelix Fietkau 
1481042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1482042ec453SJohannes Berg 				u8 tid, bool buffered)
1483dcf55fb5SFelix Fietkau {
1484dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1485dcf55fb5SFelix Fietkau 
1486948d887dSJohannes Berg 	if (WARN_ON(tid >= STA_TID_NUM))
1487042ec453SJohannes Berg 		return;
1488042ec453SJohannes Berg 
1489948d887dSJohannes Berg 	if (buffered)
1490948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
1491948d887dSJohannes Berg 	else
1492948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
1493948d887dSJohannes Berg 
1494c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1495dcf55fb5SFelix Fietkau }
1496042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1497