xref: /openbmc/linux/net/mac80211/sta_info.c (revision fe3bf0f59e97193f8619707f5d9458ce71a4f8d8)
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>
18f0706e82SJiri Benc 
19f0706e82SJiri Benc #include <net/mac80211.h>
20f0706e82SJiri Benc #include "ieee80211_i.h"
21f0706e82SJiri Benc #include "ieee80211_rate.h"
22f0706e82SJiri Benc #include "sta_info.h"
23e9f207f0SJiri Benc #include "debugfs_sta.h"
24f0706e82SJiri Benc 
25f0706e82SJiri Benc /* Caller must hold local->sta_lock */
26f0706e82SJiri Benc static void sta_info_hash_add(struct ieee80211_local *local,
27f0706e82SJiri Benc 			      struct sta_info *sta)
28f0706e82SJiri Benc {
29f0706e82SJiri Benc 	sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
30f0706e82SJiri Benc 	local->sta_hash[STA_HASH(sta->addr)] = sta;
31f0706e82SJiri Benc }
32f0706e82SJiri Benc 
33f0706e82SJiri Benc 
34f0706e82SJiri Benc /* Caller must hold local->sta_lock */
35be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
36f0706e82SJiri Benc 			     struct sta_info *sta)
37f0706e82SJiri Benc {
38f0706e82SJiri Benc 	struct sta_info *s;
39f0706e82SJiri Benc 
40f0706e82SJiri Benc 	s = local->sta_hash[STA_HASH(sta->addr)];
41f0706e82SJiri Benc 	if (!s)
42be8755e1SMichael Wu 		return -ENOENT;
43be8755e1SMichael Wu 	if (s == sta) {
44f0706e82SJiri Benc 		local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
45be8755e1SMichael Wu 		return 0;
46f0706e82SJiri Benc 	}
47f0706e82SJiri Benc 
48be8755e1SMichael Wu 	while (s->hnext && s->hnext != sta)
49f0706e82SJiri Benc 		s = s->hnext;
50be8755e1SMichael Wu 	if (s->hnext) {
51be8755e1SMichael Wu 		s->hnext = sta->hnext;
52be8755e1SMichael Wu 		return 0;
53f0706e82SJiri Benc 	}
54f0706e82SJiri Benc 
55be8755e1SMichael Wu 	return -ENOENT;
56f0706e82SJiri Benc }
57f0706e82SJiri Benc 
58f0706e82SJiri Benc struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
59f0706e82SJiri Benc {
60f0706e82SJiri Benc 	struct sta_info *sta;
61f0706e82SJiri Benc 
62be8755e1SMichael Wu 	read_lock_bh(&local->sta_lock);
63f0706e82SJiri Benc 	sta = local->sta_hash[STA_HASH(addr)];
64f0706e82SJiri Benc 	while (sta) {
65f0706e82SJiri Benc 		if (memcmp(sta->addr, addr, ETH_ALEN) == 0) {
66f0706e82SJiri Benc 			__sta_info_get(sta);
67f0706e82SJiri Benc 			break;
68f0706e82SJiri Benc 		}
69f0706e82SJiri Benc 		sta = sta->hnext;
70f0706e82SJiri Benc 	}
71be8755e1SMichael Wu 	read_unlock_bh(&local->sta_lock);
72f0706e82SJiri Benc 
73f0706e82SJiri Benc 	return sta;
74f0706e82SJiri Benc }
75f0706e82SJiri Benc EXPORT_SYMBOL(sta_info_get);
76f0706e82SJiri Benc 
77f0706e82SJiri Benc int sta_info_min_txrate_get(struct ieee80211_local *local)
78f0706e82SJiri Benc {
79f0706e82SJiri Benc 	struct sta_info *sta;
80f0706e82SJiri Benc 	struct ieee80211_hw_mode *mode;
81f0706e82SJiri Benc 	int min_txrate = 9999999;
82f0706e82SJiri Benc 	int i;
83f0706e82SJiri Benc 
84be8755e1SMichael Wu 	read_lock_bh(&local->sta_lock);
85f0706e82SJiri Benc 	mode = local->oper_hw_mode;
86f0706e82SJiri Benc 	for (i = 0; i < STA_HASH_SIZE; i++) {
87f0706e82SJiri Benc 		sta = local->sta_hash[i];
88f0706e82SJiri Benc 		while (sta) {
89f0706e82SJiri Benc 			if (sta->txrate < min_txrate)
90f0706e82SJiri Benc 				min_txrate = sta->txrate;
91f0706e82SJiri Benc 			sta = sta->hnext;
92f0706e82SJiri Benc 		}
93f0706e82SJiri Benc 	}
94be8755e1SMichael Wu 	read_unlock_bh(&local->sta_lock);
95f0706e82SJiri Benc 	if (min_txrate == 9999999)
96f0706e82SJiri Benc 		min_txrate = 0;
97f0706e82SJiri Benc 
98f0706e82SJiri Benc 	return mode->rates[min_txrate].rate;
99f0706e82SJiri Benc }
100f0706e82SJiri Benc 
101f0706e82SJiri Benc 
102f0706e82SJiri Benc static void sta_info_release(struct kref *kref)
103f0706e82SJiri Benc {
104f0706e82SJiri Benc 	struct sta_info *sta = container_of(kref, struct sta_info, kref);
105f0706e82SJiri Benc 	struct ieee80211_local *local = sta->local;
106f0706e82SJiri Benc 	struct sk_buff *skb;
10707db2183SRon Rindjunsky 	int i;
108f0706e82SJiri Benc 
109f0706e82SJiri Benc 	/* free sta structure; it has already been removed from
110f0706e82SJiri Benc 	 * hash table etc. external structures. Make sure that all
111f0706e82SJiri Benc 	 * buffered frames are release (one might have been added
112f0706e82SJiri Benc 	 * after sta_info_free() was called). */
113f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
114f0706e82SJiri Benc 		local->total_ps_buffered--;
115f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
116f0706e82SJiri Benc 	}
117f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
118f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
119f0706e82SJiri Benc 	}
120*fe3bf0f5SRon Rindjunsky 	for (i = 0; i <  STA_TID_NUM; i++) {
12107db2183SRon Rindjunsky 		del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
122*fe3bf0f5SRon Rindjunsky 		del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
123*fe3bf0f5SRon Rindjunsky 	}
124f0706e82SJiri Benc 	rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
125f0706e82SJiri Benc 	rate_control_put(sta->rate_ctrl);
126f0706e82SJiri Benc 	kfree(sta);
127f0706e82SJiri Benc }
128f0706e82SJiri Benc 
129f0706e82SJiri Benc 
130f0706e82SJiri Benc void sta_info_put(struct sta_info *sta)
131f0706e82SJiri Benc {
132f0706e82SJiri Benc 	kref_put(&sta->kref, sta_info_release);
133f0706e82SJiri Benc }
134f0706e82SJiri Benc EXPORT_SYMBOL(sta_info_put);
135f0706e82SJiri Benc 
136f0706e82SJiri Benc 
137f0706e82SJiri Benc struct sta_info * sta_info_add(struct ieee80211_local *local,
138f0706e82SJiri Benc 			       struct net_device *dev, u8 *addr, gfp_t gfp)
139f0706e82SJiri Benc {
140f0706e82SJiri Benc 	struct sta_info *sta;
14116c5f15cSRon Rindjunsky 	int i;
1420795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
143f0706e82SJiri Benc 
144f0706e82SJiri Benc 	sta = kzalloc(sizeof(*sta), gfp);
145f0706e82SJiri Benc 	if (!sta)
146f0706e82SJiri Benc 		return NULL;
147f0706e82SJiri Benc 
148f0706e82SJiri Benc 	kref_init(&sta->kref);
149f0706e82SJiri Benc 
150f0706e82SJiri Benc 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
151f0706e82SJiri Benc 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
152f0706e82SJiri Benc 	if (!sta->rate_ctrl_priv) {
153f0706e82SJiri Benc 		rate_control_put(sta->rate_ctrl);
154f0706e82SJiri Benc 		kfree(sta);
155f0706e82SJiri Benc 		return NULL;
156f0706e82SJiri Benc 	}
157f0706e82SJiri Benc 
158f0706e82SJiri Benc 	memcpy(sta->addr, addr, ETH_ALEN);
159f0706e82SJiri Benc 	sta->local = local;
160f0706e82SJiri Benc 	sta->dev = dev;
16116c5f15cSRon Rindjunsky 	spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
162*fe3bf0f5SRon Rindjunsky 	spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
16316c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
16416c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
16516c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
16616c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
16716c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
168*fe3bf0f5SRon Rindjunsky 		/* tid to tx queue: initialize according to HW (0 is valid) */
169*fe3bf0f5SRon Rindjunsky 		sta->tid_to_tx_q[i] = local->hw.queues;
17016c5f15cSRon Rindjunsky 		/* rx timers */
17116c5f15cSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i].session_timer.function =
17216c5f15cSRon Rindjunsky 			sta_rx_agg_session_timer_expired;
17316c5f15cSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i].session_timer.data =
17416c5f15cSRon Rindjunsky 			(unsigned long)&sta->timer_to_tid[i];
17516c5f15cSRon Rindjunsky 		init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer);
176*fe3bf0f5SRon Rindjunsky 		/* tx timers */
177*fe3bf0f5SRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function =
178*fe3bf0f5SRon Rindjunsky 			sta_addba_resp_timer_expired;
179*fe3bf0f5SRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data =
180*fe3bf0f5SRon Rindjunsky 			(unsigned long)&sta->timer_to_tid[i];
181*fe3bf0f5SRon Rindjunsky 		init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
18216c5f15cSRon Rindjunsky 	}
183f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
184f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
185f0706e82SJiri Benc 	__sta_info_get(sta);	/* sta used by caller, decremented by
186f0706e82SJiri Benc 				 * sta_info_put() */
187be8755e1SMichael Wu 	write_lock_bh(&local->sta_lock);
188f0706e82SJiri Benc 	list_add(&sta->list, &local->sta_list);
189f0706e82SJiri Benc 	local->num_sta++;
190f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
19132bfd35dSJohannes Berg 	if (local->ops->sta_notify) {
19232bfd35dSJohannes Berg 		struct ieee80211_sub_if_data *sdata;
19332bfd35dSJohannes Berg 
19432bfd35dSJohannes Berg 		sdata = IEEE80211_DEV_TO_SUB_IF(dev);
19551fb61e7SJohannes Berg 		if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
19632bfd35dSJohannes Berg 			sdata = sdata->u.vlan.ap;
19732bfd35dSJohannes Berg 
19832bfd35dSJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
199478f8d2bSTomas Winkler 				       STA_NOTIFY_ADD, addr);
20032bfd35dSJohannes Berg 	}
201be8755e1SMichael Wu 	write_unlock_bh(&local->sta_lock);
202f0706e82SJiri Benc 
203f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2040795af57SJoe Perches 	printk(KERN_DEBUG "%s: Added STA %s\n",
205dd1cd4c6SJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mac, addr));
206f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
207f0706e82SJiri Benc 
208e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
209e9f207f0SJiri Benc 	/* debugfs entry adding might sleep, so schedule process
210e9f207f0SJiri Benc 	 * context task for adding entry for STAs that do not yet
211e9f207f0SJiri Benc 	 * have one. */
212e9f207f0SJiri Benc 	queue_work(local->hw.workqueue, &local->sta_debugfs_add);
213e9f207f0SJiri Benc #endif
214e9f207f0SJiri Benc 
215f0706e82SJiri Benc 	return sta;
216f0706e82SJiri Benc }
217f0706e82SJiri Benc 
218be8755e1SMichael Wu /* Caller must hold local->sta_lock */
219be8755e1SMichael Wu void sta_info_remove(struct sta_info *sta)
220f0706e82SJiri Benc {
221f0706e82SJiri Benc 	struct ieee80211_local *local = sta->local;
222f0706e82SJiri Benc 	struct ieee80211_sub_if_data *sdata;
223f0706e82SJiri Benc 
224be8755e1SMichael Wu 	/* don't do anything if we've been removed already */
225be8755e1SMichael Wu 	if (sta_info_hash_del(local, sta))
226be8755e1SMichael Wu 		return;
227be8755e1SMichael Wu 
228f0706e82SJiri Benc 	list_del(&sta->list);
229f0706e82SJiri Benc 	sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
230f0706e82SJiri Benc 	if (sta->flags & WLAN_STA_PS) {
231f0706e82SJiri Benc 		sta->flags &= ~WLAN_STA_PS;
232f0706e82SJiri Benc 		if (sdata->bss)
233f0706e82SJiri Benc 			atomic_dec(&sdata->bss->num_sta_ps);
234f0706e82SJiri Benc 	}
235f0706e82SJiri Benc 	local->num_sta--;
236f0706e82SJiri Benc 	sta_info_remove_aid_ptr(sta);
237be8755e1SMichael Wu 
238f0706e82SJiri Benc }
239f0706e82SJiri Benc 
240be8755e1SMichael Wu void sta_info_free(struct sta_info *sta)
241f0706e82SJiri Benc {
242f0706e82SJiri Benc 	struct sk_buff *skb;
243f0706e82SJiri Benc 	struct ieee80211_local *local = sta->local;
2440795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
245f0706e82SJiri Benc 
246be8755e1SMichael Wu 	might_sleep();
247be8755e1SMichael Wu 
248be8755e1SMichael Wu 	write_lock_bh(&local->sta_lock);
249f0706e82SJiri Benc 	sta_info_remove(sta);
250be8755e1SMichael Wu 	write_unlock_bh(&local->sta_lock);
251f0706e82SJiri Benc 
252f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
253f0706e82SJiri Benc 		local->total_ps_buffered--;
254be8755e1SMichael Wu 		dev_kfree_skb(skb);
255f0706e82SJiri Benc 	}
256f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
257be8755e1SMichael Wu 		dev_kfree_skb(skb);
258f0706e82SJiri Benc 	}
259f0706e82SJiri Benc 
260be8755e1SMichael Wu #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2610795af57SJoe Perches 	printk(KERN_DEBUG "%s: Removed STA %s\n",
262dd1cd4c6SJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
263be8755e1SMichael Wu #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
264be8755e1SMichael Wu 
265be8755e1SMichael Wu 	ieee80211_key_free(sta->key);
266be8755e1SMichael Wu 	sta->key = NULL;
267be8755e1SMichael Wu 
26832bfd35dSJohannes Berg 	if (local->ops->sta_notify) {
26932bfd35dSJohannes Berg 		struct ieee80211_sub_if_data *sdata;
27032bfd35dSJohannes Berg 
27132bfd35dSJohannes Berg 		sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
27232bfd35dSJohannes Berg 
27351fb61e7SJohannes Berg 		if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
27432bfd35dSJohannes Berg 			sdata = sdata->u.vlan.ap;
27532bfd35dSJohannes Berg 
27632bfd35dSJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
277478f8d2bSTomas Winkler 				       STA_NOTIFY_REMOVE, sta->addr);
27832bfd35dSJohannes Berg 	}
279478f8d2bSTomas Winkler 
280be8755e1SMichael Wu 	rate_control_remove_sta_debugfs(sta);
281be8755e1SMichael Wu 	ieee80211_sta_debugfs_remove(sta);
282be8755e1SMichael Wu 
283be8755e1SMichael Wu 	sta_info_put(sta);
284f0706e82SJiri Benc }
285f0706e82SJiri Benc 
286f0706e82SJiri Benc 
287f0706e82SJiri Benc static inline int sta_info_buffer_expired(struct ieee80211_local *local,
288f0706e82SJiri Benc 					  struct sta_info *sta,
289f0706e82SJiri Benc 					  struct sk_buff *skb)
290f0706e82SJiri Benc {
291f0706e82SJiri Benc 	struct ieee80211_tx_packet_data *pkt_data;
292f0706e82SJiri Benc 	int timeout;
293f0706e82SJiri Benc 
294f0706e82SJiri Benc 	if (!skb)
295f0706e82SJiri Benc 		return 0;
296f0706e82SJiri Benc 
297f0706e82SJiri Benc 	pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
298f0706e82SJiri Benc 
299f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
300f0706e82SJiri Benc 	timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
301f0706e82SJiri Benc 		   15625) * HZ;
302f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
303f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
304f0706e82SJiri Benc 	return time_after(jiffies, pkt_data->jiffies + timeout);
305f0706e82SJiri Benc }
306f0706e82SJiri Benc 
307f0706e82SJiri Benc 
308f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
309f0706e82SJiri Benc 					     struct sta_info *sta)
310f0706e82SJiri Benc {
311f0706e82SJiri Benc 	unsigned long flags;
312f0706e82SJiri Benc 	struct sk_buff *skb;
3130795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
314f0706e82SJiri Benc 
315f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
316f0706e82SJiri Benc 		return;
317f0706e82SJiri Benc 
318f0706e82SJiri Benc 	for (;;) {
319f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
320f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
321f0706e82SJiri Benc 		if (sta_info_buffer_expired(local, sta, skb)) {
322f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
323f0706e82SJiri Benc 			if (skb_queue_empty(&sta->ps_tx_buf))
324f0706e82SJiri Benc 				sta->flags &= ~WLAN_STA_TIM;
325f0706e82SJiri Benc 		} else
326f0706e82SJiri Benc 			skb = NULL;
327f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
328f0706e82SJiri Benc 
329f0706e82SJiri Benc 		if (skb) {
330f0706e82SJiri Benc 			local->total_ps_buffered--;
331f0706e82SJiri Benc 			printk(KERN_DEBUG "Buffered frame expired (STA "
3320795af57SJoe Perches 			       "%s)\n", print_mac(mac, sta->addr));
333f0706e82SJiri Benc 			dev_kfree_skb(skb);
334f0706e82SJiri Benc 		} else
335f0706e82SJiri Benc 			break;
336f0706e82SJiri Benc 	}
337f0706e82SJiri Benc }
338f0706e82SJiri Benc 
339f0706e82SJiri Benc 
340f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
341f0706e82SJiri Benc {
342f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
343f0706e82SJiri Benc 	struct sta_info *sta;
344f0706e82SJiri Benc 
345be8755e1SMichael Wu 	read_lock_bh(&local->sta_lock);
346f0706e82SJiri Benc 	list_for_each_entry(sta, &local->sta_list, list) {
347f0706e82SJiri Benc 		__sta_info_get(sta);
348f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
349f0706e82SJiri Benc 		sta_info_put(sta);
350f0706e82SJiri Benc 	}
351be8755e1SMichael Wu 	read_unlock_bh(&local->sta_lock);
352f0706e82SJiri Benc 
3530d174406SJohannes Berg 	local->sta_cleanup.expires =
3540d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
355f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
356f0706e82SJiri Benc }
357f0706e82SJiri Benc 
358e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
359e9f207f0SJiri Benc static void sta_info_debugfs_add_task(struct work_struct *work)
360e9f207f0SJiri Benc {
361e9f207f0SJiri Benc 	struct ieee80211_local *local =
362e9f207f0SJiri Benc 		container_of(work, struct ieee80211_local, sta_debugfs_add);
363e9f207f0SJiri Benc 	struct sta_info *sta, *tmp;
364e9f207f0SJiri Benc 
365e9f207f0SJiri Benc 	while (1) {
366e9f207f0SJiri Benc 		sta = NULL;
367be8755e1SMichael Wu 		read_lock_bh(&local->sta_lock);
368e9f207f0SJiri Benc 		list_for_each_entry(tmp, &local->sta_list, list) {
369be8755e1SMichael Wu 			if (!tmp->debugfs.dir) {
370e9f207f0SJiri Benc 				sta = tmp;
371e9f207f0SJiri Benc 				__sta_info_get(sta);
372e9f207f0SJiri Benc 				break;
373e9f207f0SJiri Benc 			}
374e9f207f0SJiri Benc 		}
375be8755e1SMichael Wu 		read_unlock_bh(&local->sta_lock);
376e9f207f0SJiri Benc 
377e9f207f0SJiri Benc 		if (!sta)
378e9f207f0SJiri Benc 			break;
379e9f207f0SJiri Benc 
380e9f207f0SJiri Benc 		ieee80211_sta_debugfs_add(sta);
381e9f207f0SJiri Benc 		rate_control_add_sta_debugfs(sta);
382e9f207f0SJiri Benc 		sta_info_put(sta);
383e9f207f0SJiri Benc 	}
384e9f207f0SJiri Benc }
385e9f207f0SJiri Benc #endif
386e9f207f0SJiri Benc 
387f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
388f0706e82SJiri Benc {
389be8755e1SMichael Wu 	rwlock_init(&local->sta_lock);
390f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
391f0706e82SJiri Benc 
392b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
393b24b8a24SPavel Emelyanov 		    (unsigned long)local);
3940d174406SJohannes Berg 	local->sta_cleanup.expires =
3950d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
396e9f207f0SJiri Benc 
397e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
398e9f207f0SJiri Benc 	INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
399e9f207f0SJiri Benc #endif
400f0706e82SJiri Benc }
401f0706e82SJiri Benc 
402f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
403f0706e82SJiri Benc {
404f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
405f0706e82SJiri Benc 	return 0;
406f0706e82SJiri Benc }
407f0706e82SJiri Benc 
408f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
409f0706e82SJiri Benc {
410f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
411be8755e1SMichael Wu 	sta_info_flush(local, NULL);
412f0706e82SJiri Benc }
413f0706e82SJiri Benc 
414f0706e82SJiri Benc void sta_info_remove_aid_ptr(struct sta_info *sta)
415f0706e82SJiri Benc {
416f0706e82SJiri Benc 	struct ieee80211_sub_if_data *sdata;
417f0706e82SJiri Benc 
418f0706e82SJiri Benc 	if (sta->aid <= 0)
419f0706e82SJiri Benc 		return;
420f0706e82SJiri Benc 
421f0706e82SJiri Benc 	sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
422f0706e82SJiri Benc 
423f0706e82SJiri Benc 	if (sdata->local->ops->set_tim)
424f0706e82SJiri Benc 		sdata->local->ops->set_tim(local_to_hw(sdata->local),
425f0706e82SJiri Benc 					  sta->aid, 0);
426f0706e82SJiri Benc 	if (sdata->bss)
427f0706e82SJiri Benc 		__bss_tim_clear(sdata->bss, sta->aid);
428f0706e82SJiri Benc }
429f0706e82SJiri Benc 
430f0706e82SJiri Benc 
431f0706e82SJiri Benc /**
432f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
433f0706e82SJiri Benc  * @local: local interface data
434f0706e82SJiri Benc  * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
435f0706e82SJiri Benc  */
436f0706e82SJiri Benc void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
437f0706e82SJiri Benc {
438f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
439be8755e1SMichael Wu 	LIST_HEAD(tmp_list);
440f0706e82SJiri Benc 
441be8755e1SMichael Wu 	write_lock_bh(&local->sta_lock);
442f0706e82SJiri Benc 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
443be8755e1SMichael Wu 		if (!dev || dev == sta->dev) {
444be8755e1SMichael Wu 			__sta_info_get(sta);
445be8755e1SMichael Wu 			sta_info_remove(sta);
446be8755e1SMichael Wu 			list_add_tail(&sta->list, &tmp_list);
447be8755e1SMichael Wu 		}
448be8755e1SMichael Wu 	write_unlock_bh(&local->sta_lock);
449be8755e1SMichael Wu 
450be8755e1SMichael Wu 	list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
451be8755e1SMichael Wu 		sta_info_free(sta);
452be8755e1SMichael Wu 		sta_info_put(sta);
453be8755e1SMichael Wu 	}
454f0706e82SJiri Benc }
455