2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/netdevice.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/if_arp.h>
17 #include <linux/timer.h>
19 #include <net/mac80211.h>
20 #include "ieee80211_i.h"
21 #include "ieee80211_rate.h"
23 #include "debugfs_sta.h"
25 /* Caller must hold local->sta_lock */
26 static void sta_info_hash_add(struct ieee80211_local *local,
29 sta->hnext = local->sta_hash[STA_HASH(sta->addr)];
30 local->sta_hash[STA_HASH(sta->addr)] = sta;
34 /* Caller must hold local->sta_lock */
35 static int sta_info_hash_del(struct ieee80211_local *local,
40 s = local->sta_hash[STA_HASH(sta->addr)];
44 local->sta_hash[STA_HASH(sta->addr)] = s->hnext;
48 while (s->hnext && s->hnext != sta)
51 s->hnext = sta->hnext;
58 struct sta_info *sta_info_get(struct ieee80211_local *local, u8 *addr)
62 read_lock_bh(&local->sta_lock);
63 sta = local->sta_hash[STA_HASH(addr)];
65 if (memcmp(sta->addr, addr, ETH_ALEN) == 0) {
71 read_unlock_bh(&local->sta_lock);
75 EXPORT_SYMBOL(sta_info_get);
77 int sta_info_min_txrate_get(struct ieee80211_local *local)
80 struct ieee80211_hw_mode *mode;
81 int min_txrate = 9999999;
84 read_lock_bh(&local->sta_lock);
85 mode = local->oper_hw_mode;
86 for (i = 0; i < STA_HASH_SIZE; i++) {
87 sta = local->sta_hash[i];
89 if (sta->txrate < min_txrate)
90 min_txrate = sta->txrate;
94 read_unlock_bh(&local->sta_lock);
95 if (min_txrate == 9999999)
98 return mode->rates[min_txrate].rate;
102 static void sta_info_release(struct kref *kref)
104 struct sta_info *sta = container_of(kref, struct sta_info, kref);
105 struct ieee80211_local *local = sta->local;
109 /* free sta structure; it has already been removed from
110 * hash table etc. external structures. Make sure that all
111 * buffered frames are release (one might have been added
112 * after sta_info_free() was called). */
113 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
114 local->total_ps_buffered--;
115 dev_kfree_skb_any(skb);
117 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
118 dev_kfree_skb_any(skb);
120 for (i = 0; i < STA_TID_NUM; i++) {
121 del_timer_sync(&sta->ampdu_mlme.tid_rx[i].session_timer);
122 del_timer_sync(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
124 rate_control_free_sta(sta->rate_ctrl, sta->rate_ctrl_priv);
125 rate_control_put(sta->rate_ctrl);
130 void sta_info_put(struct sta_info *sta)
132 kref_put(&sta->kref, sta_info_release);
134 EXPORT_SYMBOL(sta_info_put);
137 struct sta_info * sta_info_add(struct ieee80211_local *local,
138 struct net_device *dev, u8 *addr, gfp_t gfp)
140 struct sta_info *sta;
142 DECLARE_MAC_BUF(mac);
144 sta = kzalloc(sizeof(*sta), gfp);
148 kref_init(&sta->kref);
150 sta->rate_ctrl = rate_control_get(local->rate_ctrl);
151 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, gfp);
152 if (!sta->rate_ctrl_priv) {
153 rate_control_put(sta->rate_ctrl);
158 memcpy(sta->addr, addr, ETH_ALEN);
161 spin_lock_init(&sta->ampdu_mlme.ampdu_rx);
162 spin_lock_init(&sta->ampdu_mlme.ampdu_tx);
163 for (i = 0; i < STA_TID_NUM; i++) {
164 /* timer_to_tid must be initialized with identity mapping to
165 * enable session_timer's data differentiation. refer to
166 * sta_rx_agg_session_timer_expired for useage */
167 sta->timer_to_tid[i] = i;
168 /* tid to tx queue: initialize according to HW (0 is valid) */
169 sta->tid_to_tx_q[i] = local->hw.queues;
171 sta->ampdu_mlme.tid_rx[i].session_timer.function =
172 sta_rx_agg_session_timer_expired;
173 sta->ampdu_mlme.tid_rx[i].session_timer.data =
174 (unsigned long)&sta->timer_to_tid[i];
175 init_timer(&sta->ampdu_mlme.tid_rx[i].session_timer);
177 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.function =
178 sta_addba_resp_timer_expired;
179 sta->ampdu_mlme.tid_tx[i].addba_resp_timer.data =
180 (unsigned long)&sta->timer_to_tid[i];
181 init_timer(&sta->ampdu_mlme.tid_tx[i].addba_resp_timer);
183 skb_queue_head_init(&sta->ps_tx_buf);
184 skb_queue_head_init(&sta->tx_filtered);
185 __sta_info_get(sta); /* sta used by caller, decremented by
187 write_lock_bh(&local->sta_lock);
188 list_add(&sta->list, &local->sta_list);
190 sta_info_hash_add(local, sta);
191 if (local->ops->sta_notify) {
192 struct ieee80211_sub_if_data *sdata;
194 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
195 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
196 sdata = sdata->u.vlan.ap;
198 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
199 STA_NOTIFY_ADD, addr);
201 write_unlock_bh(&local->sta_lock);
203 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
204 printk(KERN_DEBUG "%s: Added STA %s\n",
205 wiphy_name(local->hw.wiphy), print_mac(mac, addr));
206 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
208 #ifdef CONFIG_MAC80211_DEBUGFS
209 /* debugfs entry adding might sleep, so schedule process
210 * context task for adding entry for STAs that do not yet
212 queue_work(local->hw.workqueue, &local->sta_debugfs_add);
218 /* Caller must hold local->sta_lock */
219 void sta_info_remove(struct sta_info *sta)
221 struct ieee80211_local *local = sta->local;
222 struct ieee80211_sub_if_data *sdata;
224 /* don't do anything if we've been removed already */
225 if (sta_info_hash_del(local, sta))
228 list_del(&sta->list);
229 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
230 if (sta->flags & WLAN_STA_PS) {
231 sta->flags &= ~WLAN_STA_PS;
233 atomic_dec(&sdata->bss->num_sta_ps);
236 sta_info_remove_aid_ptr(sta);
240 void sta_info_free(struct sta_info *sta)
243 struct ieee80211_local *local = sta->local;
244 DECLARE_MAC_BUF(mac);
248 write_lock_bh(&local->sta_lock);
249 sta_info_remove(sta);
250 write_unlock_bh(&local->sta_lock);
252 while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
253 local->total_ps_buffered--;
256 while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
260 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
261 printk(KERN_DEBUG "%s: Removed STA %s\n",
262 wiphy_name(local->hw.wiphy), print_mac(mac, sta->addr));
263 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
265 ieee80211_key_free(sta->key);
268 if (local->ops->sta_notify) {
269 struct ieee80211_sub_if_data *sdata;
271 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
273 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
274 sdata = sdata->u.vlan.ap;
276 local->ops->sta_notify(local_to_hw(local), &sdata->vif,
277 STA_NOTIFY_REMOVE, sta->addr);
280 rate_control_remove_sta_debugfs(sta);
281 ieee80211_sta_debugfs_remove(sta);
287 static inline int sta_info_buffer_expired(struct ieee80211_local *local,
288 struct sta_info *sta,
291 struct ieee80211_tx_packet_data *pkt_data;
297 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
299 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
300 timeout = (sta->listen_interval * local->hw.conf.beacon_int * 32 /
302 if (timeout < STA_TX_BUFFER_EXPIRE)
303 timeout = STA_TX_BUFFER_EXPIRE;
304 return time_after(jiffies, pkt_data->jiffies + timeout);
308 static void sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
309 struct sta_info *sta)
313 DECLARE_MAC_BUF(mac);
315 if (skb_queue_empty(&sta->ps_tx_buf))
319 spin_lock_irqsave(&sta->ps_tx_buf.lock, flags);
320 skb = skb_peek(&sta->ps_tx_buf);
321 if (sta_info_buffer_expired(local, sta, skb)) {
322 skb = __skb_dequeue(&sta->ps_tx_buf);
323 if (skb_queue_empty(&sta->ps_tx_buf))
324 sta->flags &= ~WLAN_STA_TIM;
327 spin_unlock_irqrestore(&sta->ps_tx_buf.lock, flags);
330 local->total_ps_buffered--;
331 printk(KERN_DEBUG "Buffered frame expired (STA "
332 "%s)\n", print_mac(mac, sta->addr));
340 static void sta_info_cleanup(unsigned long data)
342 struct ieee80211_local *local = (struct ieee80211_local *) data;
343 struct sta_info *sta;
345 read_lock_bh(&local->sta_lock);
346 list_for_each_entry(sta, &local->sta_list, list) {
348 sta_info_cleanup_expire_buffered(local, sta);
351 read_unlock_bh(&local->sta_lock);
353 local->sta_cleanup.expires =
354 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
355 add_timer(&local->sta_cleanup);
358 #ifdef CONFIG_MAC80211_DEBUGFS
359 static void sta_info_debugfs_add_task(struct work_struct *work)
361 struct ieee80211_local *local =
362 container_of(work, struct ieee80211_local, sta_debugfs_add);
363 struct sta_info *sta, *tmp;
367 read_lock_bh(&local->sta_lock);
368 list_for_each_entry(tmp, &local->sta_list, list) {
369 if (!tmp->debugfs.dir) {
375 read_unlock_bh(&local->sta_lock);
380 ieee80211_sta_debugfs_add(sta);
381 rate_control_add_sta_debugfs(sta);
387 void sta_info_init(struct ieee80211_local *local)
389 rwlock_init(&local->sta_lock);
390 INIT_LIST_HEAD(&local->sta_list);
392 setup_timer(&local->sta_cleanup, sta_info_cleanup,
393 (unsigned long)local);
394 local->sta_cleanup.expires =
395 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL);
397 #ifdef CONFIG_MAC80211_DEBUGFS
398 INIT_WORK(&local->sta_debugfs_add, sta_info_debugfs_add_task);
402 int sta_info_start(struct ieee80211_local *local)
404 add_timer(&local->sta_cleanup);
408 void sta_info_stop(struct ieee80211_local *local)
410 del_timer(&local->sta_cleanup);
411 sta_info_flush(local, NULL);
414 void sta_info_remove_aid_ptr(struct sta_info *sta)
416 struct ieee80211_sub_if_data *sdata;
421 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
423 if (sdata->local->ops->set_tim)
424 sdata->local->ops->set_tim(local_to_hw(sdata->local),
427 __bss_tim_clear(sdata->bss, sta->aid);
432 * sta_info_flush - flush matching STA entries from the STA table
433 * @local: local interface data
434 * @dev: matching rule for the net device (sta->dev) or %NULL to match all STAs
436 void sta_info_flush(struct ieee80211_local *local, struct net_device *dev)
438 struct sta_info *sta, *tmp;
441 write_lock_bh(&local->sta_lock);
442 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
443 if (!dev || dev == sta->dev) {
445 sta_info_remove(sta);
446 list_add_tail(&sta->list, &tmp_list);
448 write_unlock_bh(&local->sta_lock);
450 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {