xref: /openbmc/linux/net/mac80211/sta_info.c (revision 004c872e78d433f84f0a5cd4db7a6c780c0946e1)
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 
78f0706e82SJiri Benc static void sta_info_release(struct kref *kref)
79f0706e82SJiri Benc {
80f0706e82SJiri Benc 	struct sta_info *sta = container_of(kref, struct sta_info, kref);
81f0706e82SJiri Benc 	struct ieee80211_local *local = sta->local;
82f0706e82SJiri Benc 	struct sk_buff *skb;
8307db2183SRon Rindjunsky 	int i;
84f0706e82SJiri Benc 
85f0706e82SJiri Benc 	/* free sta structure; it has already been removed from
86f0706e82SJiri Benc 	 * hash table etc. external structures. Make sure that all
87f0706e82SJiri Benc 	 * buffered frames are release (one might have been added
88f0706e82SJiri Benc 	 * after sta_info_free() was called). */
89f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
90f0706e82SJiri Benc 		local->total_ps_buffered--;
91f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
92f0706e82SJiri Benc 	}
93f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
94f0706e82SJiri Benc 		dev_kfree_skb_any(skb);
95f0706e82SJiri Benc 	}
96fe3bf0f5SRon Rindjunsky 	for (i = 0; i <  STA_TID_NUM; i++) {
9707db2183SRon Rindjunsky 		del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
98fe3bf0f5SRon Rindjunsky 		del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
99fe3bf0f5SRon Rindjunsky 	}
100f0706e82SJiri Benc 	rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
101f0706e82SJiri Benc 	rate_control_put(sta->rate_ctrl);
102f0706e82SJiri Benc 	kfree(sta);
103f0706e82SJiri Benc }
104f0706e82SJiri Benc 
105f0706e82SJiri Benc 
106f0706e82SJiri Benc void sta_info_put(struct sta_info *sta)
107f0706e82SJiri Benc {
108f0706e82SJiri Benc 	kref_put(&sta->kref, sta_info_release);
109f0706e82SJiri Benc }
110f0706e82SJiri Benc EXPORT_SYMBOL(sta_info_put);
111f0706e82SJiri Benc 
112f0706e82SJiri Benc 
113f0706e82SJiri Benc struct sta_info * sta_info_add(struct ieee80211_local *local,
114f0706e82SJiri Benc 			       struct net_device *dev, u8 *addr, gfp_t gfp)
115f0706e82SJiri Benc {
116f0706e82SJiri Benc 	struct sta_info *sta;
11716c5f15cSRon Rindjunsky 	int i;
1180795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
119f0706e82SJiri Benc 
120f0706e82SJiri Benc 	sta = kzalloc(sizeof(*sta), gfp);
121f0706e82SJiri Benc 	if (!sta)
122f0706e82SJiri Benc 		return NULL;
123f0706e82SJiri Benc 
124f0706e82SJiri Benc 	kref_init(&sta->kref);
125f0706e82SJiri Benc 
126f0706e82SJiri Benc 	sta->rate_ctrl = rate_control_get(local->rate_ctrl);
127f0706e82SJiri Benc 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
128f0706e82SJiri Benc 	if (!sta->rate_ctrl_priv) {
129f0706e82SJiri Benc 		rate_control_put(sta->rate_ctrl);
130f0706e82SJiri Benc 		kfree(sta);
131f0706e82SJiri Benc 		return NULL;
132f0706e82SJiri Benc 	}
133f0706e82SJiri Benc 
134f0706e82SJiri Benc 	memcpy(sta->addr, addr, ETH_ALEN);
135f0706e82SJiri Benc 	sta->local = local;
136f0706e82SJiri Benc 	sta->dev = dev;
13716c5f15cSRon Rindjunsky 	spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
138fe3bf0f5SRon Rindjunsky 	spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
13916c5f15cSRon Rindjunsky 	for (i = 0; i < STA_TID_NUM; i++) {
14016c5f15cSRon Rindjunsky 		/* timer_to_tid must be initialized with identity mapping to
14116c5f15cSRon Rindjunsky 		 * enable session_timer's data differentiation. refer to
14216c5f15cSRon Rindjunsky 		 * sta_rx_agg_session_timer_expired for useage */
14316c5f15cSRon Rindjunsky 		sta->timer_to_tid[i] = i;
144fe3bf0f5SRon Rindjunsky 		/* tid to tx queue: initialize according to HW (0 is valid) */
145fe3bf0f5SRon Rindjunsky 		sta->tid_to_tx_q[i] = local->hw.queues;
14616c5f15cSRon Rindjunsky 		/* rx timers */
14716c5f15cSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i].session_timer.function =
14816c5f15cSRon Rindjunsky 			sta_rx_agg_session_timer_expired;
14916c5f15cSRon Rindjunsky 		sta->ampdu_mlme.tid_rx[i].session_timer.data =
15016c5f15cSRon Rindjunsky 			(unsigned long)&sta->timer_to_tid[i];
15116c5f15cSRon Rindjunsky 		init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer);
152fe3bf0f5SRon Rindjunsky 		/* tx timers */
153fe3bf0f5SRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function =
154fe3bf0f5SRon Rindjunsky 			sta_addba_resp_timer_expired;
155fe3bf0f5SRon Rindjunsky 		sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data =
156fe3bf0f5SRon Rindjunsky 			(unsigned long)&sta->timer_to_tid[i];
157fe3bf0f5SRon Rindjunsky 		init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
15816c5f15cSRon Rindjunsky 	}
159f0706e82SJiri Benc 	skb_queue_head_init(&sta->ps_tx_buf);
160f0706e82SJiri Benc 	skb_queue_head_init(&sta->tx_filtered);
161f0706e82SJiri Benc 	__sta_info_get(sta);	/* sta used by caller, decremented by
162f0706e82SJiri Benc 				 * sta_info_put() */
163be8755e1SMichael Wu 	write_lock_bh(&local->sta_lock);
164f0706e82SJiri Benc 	list_add(&sta->list, &local->sta_list);
165f0706e82SJiri Benc 	local->num_sta++;
166f0706e82SJiri Benc 	sta_info_hash_add(local, sta);
16732bfd35dSJohannes Berg 	if (local->ops->sta_notify) {
16832bfd35dSJohannes Berg 		struct ieee80211_sub_if_data *sdata;
16932bfd35dSJohannes Berg 
17032bfd35dSJohannes Berg 		sdata = IEEE80211_DEV_TO_SUB_IF(dev);
17151fb61e7SJohannes Berg 		if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
17232bfd35dSJohannes Berg 			sdata = sdata->u.vlan.ap;
17332bfd35dSJohannes Berg 
17432bfd35dSJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
175478f8d2bSTomas Winkler 				       STA_NOTIFY_ADD, addr);
17632bfd35dSJohannes Berg 	}
177be8755e1SMichael Wu 	write_unlock_bh(&local->sta_lock);
178f0706e82SJiri Benc 
179f0706e82SJiri Benc #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1800795af57SJoe Perches 	printk(KERN_DEBUG "%s: Added STA %s\n",
181dd1cd4c6SJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mac, addr));
182f0706e82SJiri Benc #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
183f0706e82SJiri Benc 
184e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
185e9f207f0SJiri Benc 	/* debugfs entry adding might sleep, so schedule process
186e9f207f0SJiri Benc 	 * context task for adding entry for STAs that do not yet
187e9f207f0SJiri Benc 	 * have one. */
188e9f207f0SJiri Benc 	queue_work(local->hw.workqueue, &local->sta_debugfs_add);
189e9f207f0SJiri Benc #endif
190e9f207f0SJiri Benc 
191f0706e82SJiri Benc 	return sta;
192f0706e82SJiri Benc }
193f0706e82SJiri Benc 
194*004c872eSJohannes Berg static inline void __bss_tim_set(struct ieee80211_if_ap *bss, u16 aid)
195*004c872eSJohannes Berg {
196*004c872eSJohannes Berg 	/*
197*004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
198*004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
199*004c872eSJohannes Berg 	 */
200*004c872eSJohannes Berg 	bss->tim[aid / 8] |= (1 << (aid % 8));
201*004c872eSJohannes Berg }
202*004c872eSJohannes Berg 
203*004c872eSJohannes Berg static inline void __bss_tim_clear(struct ieee80211_if_ap *bss, u16 aid)
204*004c872eSJohannes Berg {
205*004c872eSJohannes Berg 	/*
206*004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
207*004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
208*004c872eSJohannes Berg 	 */
209*004c872eSJohannes Berg 	bss->tim[aid / 8] &= ~(1 << (aid % 8));
210*004c872eSJohannes Berg }
211*004c872eSJohannes Berg 
212*004c872eSJohannes Berg static void __sta_info_set_tim_bit(struct ieee80211_if_ap *bss,
213*004c872eSJohannes Berg 				   struct sta_info *sta)
214*004c872eSJohannes Berg {
215*004c872eSJohannes Berg 	if (bss)
216*004c872eSJohannes Berg 		__bss_tim_set(bss, sta->aid);
217*004c872eSJohannes Berg 	if (sta->local->ops->set_tim)
218*004c872eSJohannes Berg 		sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 1);
219*004c872eSJohannes Berg }
220*004c872eSJohannes Berg 
221*004c872eSJohannes Berg void sta_info_set_tim_bit(struct sta_info *sta)
222*004c872eSJohannes Berg {
223*004c872eSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
224*004c872eSJohannes Berg 
225*004c872eSJohannes Berg 	sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
226*004c872eSJohannes Berg 
227*004c872eSJohannes Berg 	read_lock_bh(&sta->local->sta_lock);
228*004c872eSJohannes Berg 	__sta_info_set_tim_bit(sdata->bss, sta);
229*004c872eSJohannes Berg 	read_unlock_bh(&sta->local->sta_lock);
230*004c872eSJohannes Berg }
231*004c872eSJohannes Berg 
232*004c872eSJohannes Berg static void __sta_info_clear_tim_bit(struct ieee80211_if_ap *bss,
233*004c872eSJohannes Berg 				     struct sta_info *sta)
234*004c872eSJohannes Berg {
235*004c872eSJohannes Berg 	if (bss)
236*004c872eSJohannes Berg 		__bss_tim_clear(bss, sta->aid);
237*004c872eSJohannes Berg 	if (sta->local->ops->set_tim)
238*004c872eSJohannes Berg 		sta->local->ops->set_tim(local_to_hw(sta->local), sta->aid, 0);
239*004c872eSJohannes Berg }
240*004c872eSJohannes Berg 
241*004c872eSJohannes Berg void sta_info_clear_tim_bit(struct sta_info *sta)
242*004c872eSJohannes Berg {
243*004c872eSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
244*004c872eSJohannes Berg 
245*004c872eSJohannes Berg 	sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
246*004c872eSJohannes Berg 
247*004c872eSJohannes Berg 	read_lock_bh(&sta->local->sta_lock);
248*004c872eSJohannes Berg 	__sta_info_clear_tim_bit(sdata->bss, sta);
249*004c872eSJohannes Berg 	read_unlock_bh(&sta->local->sta_lock);
250*004c872eSJohannes Berg }
251*004c872eSJohannes Berg 
252be8755e1SMichael Wu /* Caller must hold local->sta_lock */
253be8755e1SMichael Wu void sta_info_remove(struct sta_info *sta)
254f0706e82SJiri Benc {
255f0706e82SJiri Benc 	struct ieee80211_local *local = sta->local;
256f0706e82SJiri Benc 	struct ieee80211_sub_if_data *sdata;
257f0706e82SJiri Benc 
258be8755e1SMichael Wu 	/* don't do anything if we've been removed already */
259be8755e1SMichael Wu 	if (sta_info_hash_del(local, sta))
260be8755e1SMichael Wu 		return;
261be8755e1SMichael Wu 
262f0706e82SJiri Benc 	list_del(&sta->list);
263f0706e82SJiri Benc 	sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
264f0706e82SJiri Benc 	if (sta->flags & WLAN_STA_PS) {
265f0706e82SJiri Benc 		sta->flags &= ~WLAN_STA_PS;
266f0706e82SJiri Benc 		if (sdata->bss)
267f0706e82SJiri Benc 			atomic_dec(&sdata->bss->num_sta_ps);
268*004c872eSJohannes Berg 		__sta_info_clear_tim_bit(sdata->bss, sta);
269f0706e82SJiri Benc 	}
270f0706e82SJiri Benc 	local->num_sta--;
271f0706e82SJiri Benc }
272f0706e82SJiri Benc 
273be8755e1SMichael Wu void sta_info_free(struct sta_info *sta)
274f0706e82SJiri Benc {
275f0706e82SJiri Benc 	struct sk_buff *skb;
276f0706e82SJiri Benc 	struct ieee80211_local *local = sta->local;
2770795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
278f0706e82SJiri Benc 
279be8755e1SMichael Wu 	might_sleep();
280be8755e1SMichael Wu 
281be8755e1SMichael Wu 	write_lock_bh(&local->sta_lock);
282f0706e82SJiri Benc 	sta_info_remove(sta);
283be8755e1SMichael Wu 	write_unlock_bh(&local->sta_lock);
284f0706e82SJiri Benc 
285f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
286f0706e82SJiri Benc 		local->total_ps_buffered--;
287be8755e1SMichael Wu 		dev_kfree_skb(skb);
288f0706e82SJiri Benc 	}
289f0706e82SJiri Benc 	while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
290be8755e1SMichael Wu 		dev_kfree_skb(skb);
291f0706e82SJiri Benc 	}
292f0706e82SJiri Benc 
293be8755e1SMichael Wu #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2940795af57SJoe Perches 	printk(KERN_DEBUG "%s: Removed STA %s\n",
295dd1cd4c6SJohannes Berg 	       wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
296be8755e1SMichael Wu #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
297be8755e1SMichael Wu 
298be8755e1SMichael Wu 	ieee80211_key_free(sta->key);
299be8755e1SMichael Wu 	sta->key = NULL;
300be8755e1SMichael Wu 
30132bfd35dSJohannes Berg 	if (local->ops->sta_notify) {
30232bfd35dSJohannes Berg 		struct ieee80211_sub_if_data *sdata;
30332bfd35dSJohannes Berg 
30432bfd35dSJohannes Berg 		sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
30532bfd35dSJohannes Berg 
30651fb61e7SJohannes Berg 		if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
30732bfd35dSJohannes Berg 			sdata = sdata->u.vlan.ap;
30832bfd35dSJohannes Berg 
30932bfd35dSJohannes Berg 		local->ops->sta_notify(local_to_hw(local), &sdata->vif,
310478f8d2bSTomas Winkler 				       STA_NOTIFY_REMOVE, sta->addr);
31132bfd35dSJohannes Berg 	}
312478f8d2bSTomas Winkler 
313be8755e1SMichael Wu 	rate_control_remove_sta_debugfs(sta);
314be8755e1SMichael Wu 	ieee80211_sta_debugfs_remove(sta);
315be8755e1SMichael Wu 
316be8755e1SMichael Wu 	sta_info_put(sta);
317f0706e82SJiri Benc }
318f0706e82SJiri Benc 
319f0706e82SJiri Benc 
320f0706e82SJiri Benc static inline int sta_info_buffer_expired(struct ieee80211_local *local,
321f0706e82SJiri Benc 					  struct sta_info *sta,
322f0706e82SJiri Benc 					  struct sk_buff *skb)
323f0706e82SJiri Benc {
324f0706e82SJiri Benc 	struct ieee80211_tx_packet_data *pkt_data;
325f0706e82SJiri Benc 	int timeout;
326f0706e82SJiri Benc 
327f0706e82SJiri Benc 	if (!skb)
328f0706e82SJiri Benc 		return 0;
329f0706e82SJiri Benc 
330f0706e82SJiri Benc 	pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
331f0706e82SJiri Benc 
332f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
333f0706e82SJiri Benc 	timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
334f0706e82SJiri Benc 		   15625) * HZ;
335f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
336f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
337f0706e82SJiri Benc 	return time_after(jiffies, pkt_data->jiffies + timeout);
338f0706e82SJiri Benc }
339f0706e82SJiri Benc 
340f0706e82SJiri Benc 
341f0706e82SJiri Benc static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
342f0706e82SJiri Benc 					     struct sta_info *sta)
343f0706e82SJiri Benc {
344f0706e82SJiri Benc 	unsigned long flags;
345f0706e82SJiri Benc 	struct sk_buff *skb;
346836341a7SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
3470795af57SJoe Perches 	DECLARE_MAC_BUF(mac);
348f0706e82SJiri Benc 
349f0706e82SJiri Benc 	if (skb_queue_empty(&sta->ps_tx_buf))
350f0706e82SJiri Benc 		return;
351f0706e82SJiri Benc 
352f0706e82SJiri Benc 	for (;;) {
353f0706e82SJiri Benc 		spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
354f0706e82SJiri Benc 		skb = skb_peek(&sta->ps_tx_buf);
355836341a7SJohannes Berg 		if (sta_info_buffer_expired(local, sta, skb))
356f0706e82SJiri Benc 			skb = __skb_dequeue(&sta->ps_tx_buf);
357836341a7SJohannes Berg 		else
358f0706e82SJiri Benc 			skb = NULL;
359f0706e82SJiri Benc 		spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
360f0706e82SJiri Benc 
361836341a7SJohannes Berg 		if (!skb)
362836341a7SJohannes Berg 			break;
363836341a7SJohannes Berg 
364836341a7SJohannes Berg 		sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
365f0706e82SJiri Benc 		local->total_ps_buffered--;
366f0706e82SJiri Benc 		printk(KERN_DEBUG "Buffered frame expired (STA "
3670795af57SJoe Perches 		       "%s)\n", print_mac(mac, sta->addr));
368f0706e82SJiri Benc 		dev_kfree_skb(skb);
369836341a7SJohannes Berg 
370*004c872eSJohannes Berg 		if (skb_queue_empty(&sta->ps_tx_buf))
371*004c872eSJohannes Berg 			sta_info_clear_tim_bit(sta);
372f0706e82SJiri Benc 	}
373f0706e82SJiri Benc }
374f0706e82SJiri Benc 
375f0706e82SJiri Benc 
376f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data)
377f0706e82SJiri Benc {
378f0706e82SJiri Benc 	struct ieee80211_local *local = (struct ieee80211_local *) data;
379f0706e82SJiri Benc 	struct sta_info *sta;
380f0706e82SJiri Benc 
381be8755e1SMichael Wu 	read_lock_bh(&local->sta_lock);
382f0706e82SJiri Benc 	list_for_each_entry(sta, &local->sta_list, list) {
383f0706e82SJiri Benc 		__sta_info_get(sta);
384f0706e82SJiri Benc 		sta_info_cleanup_expire_buffered(local, sta);
385f0706e82SJiri Benc 		sta_info_put(sta);
386f0706e82SJiri Benc 	}
387be8755e1SMichael Wu 	read_unlock_bh(&local->sta_lock);
388f0706e82SJiri Benc 
3890d174406SJohannes Berg 	local->sta_cleanup.expires =
3900d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
391f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
392f0706e82SJiri Benc }
393f0706e82SJiri Benc 
394e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
395e9f207f0SJiri Benc static void sta_info_debugfs_add_task(struct work_struct *work)
396e9f207f0SJiri Benc {
397e9f207f0SJiri Benc 	struct ieee80211_local *local =
398e9f207f0SJiri Benc 		container_of(work, struct ieee80211_local, sta_debugfs_add);
399e9f207f0SJiri Benc 	struct sta_info *sta, *tmp;
400e9f207f0SJiri Benc 
401e9f207f0SJiri Benc 	while (1) {
402e9f207f0SJiri Benc 		sta = NULL;
403be8755e1SMichael Wu 		read_lock_bh(&local->sta_lock);
404e9f207f0SJiri Benc 		list_for_each_entry(tmp, &local->sta_list, list) {
405be8755e1SMichael Wu 			if (!tmp->debugfs.dir) {
406e9f207f0SJiri Benc 				sta = tmp;
407e9f207f0SJiri Benc 				__sta_info_get(sta);
408e9f207f0SJiri Benc 				break;
409e9f207f0SJiri Benc 			}
410e9f207f0SJiri Benc 		}
411be8755e1SMichael Wu 		read_unlock_bh(&local->sta_lock);
412e9f207f0SJiri Benc 
413e9f207f0SJiri Benc 		if (!sta)
414e9f207f0SJiri Benc 			break;
415e9f207f0SJiri Benc 
416e9f207f0SJiri Benc 		ieee80211_sta_debugfs_add(sta);
417e9f207f0SJiri Benc 		rate_control_add_sta_debugfs(sta);
418e9f207f0SJiri Benc 		sta_info_put(sta);
419e9f207f0SJiri Benc 	}
420e9f207f0SJiri Benc }
421e9f207f0SJiri Benc #endif
422e9f207f0SJiri Benc 
423f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local)
424f0706e82SJiri Benc {
425be8755e1SMichael Wu 	rwlock_init(&local->sta_lock);
426f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
427f0706e82SJiri Benc 
428b24b8a24SPavel Emelyanov 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
429b24b8a24SPavel Emelyanov 		    (unsigned long)local);
4300d174406SJohannes Berg 	local->sta_cleanup.expires =
4310d174406SJohannes Berg 		round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
432e9f207f0SJiri Benc 
433e9f207f0SJiri Benc #ifdef CONFIG_MAC80211_DEBUGFS
434e9f207f0SJiri Benc 	INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
435e9f207f0SJiri Benc #endif
436f0706e82SJiri Benc }
437f0706e82SJiri Benc 
438f0706e82SJiri Benc int sta_info_start(struct ieee80211_local *local)
439f0706e82SJiri Benc {
440f0706e82SJiri Benc 	add_timer(&local->sta_cleanup);
441f0706e82SJiri Benc 	return 0;
442f0706e82SJiri Benc }
443f0706e82SJiri Benc 
444f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
445f0706e82SJiri Benc {
446f0706e82SJiri Benc 	del_timer(&local->sta_cleanup);
447be8755e1SMichael Wu 	sta_info_flush(local, NULL);
448f0706e82SJiri Benc }
449f0706e82SJiri Benc 
450f0706e82SJiri Benc /**
451f0706e82SJiri Benc  * sta_info_flush - flush matching STA entries from the STA table
452f0706e82SJiri Benc  * @local: local interface data
453f0706e82SJiri Benc  * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
454f0706e82SJiri Benc  */
455f0706e82SJiri Benc void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
456f0706e82SJiri Benc {
457f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
458be8755e1SMichael Wu 	LIST_HEAD(tmp_list);
459f0706e82SJiri Benc 
460be8755e1SMichael Wu 	write_lock_bh(&local->sta_lock);
461f0706e82SJiri Benc 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
462be8755e1SMichael Wu 		if (!dev || dev == sta->dev) {
463be8755e1SMichael Wu 			__sta_info_get(sta);
464be8755e1SMichael Wu 			sta_info_remove(sta);
465be8755e1SMichael Wu 			list_add_tail(&sta->list, &tmp_list);
466be8755e1SMichael Wu 		}
467be8755e1SMichael Wu 	write_unlock_bh(&local->sta_lock);
468be8755e1SMichael Wu 
469be8755e1SMichael Wu 	list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
470be8755e1SMichael Wu 		sta_info_free(sta);
471be8755e1SMichael Wu 		sta_info_put(sta);
472be8755e1SMichael Wu 	}
473f0706e82SJiri Benc }
474