batman-adv: remove duplicate code from function is_bidirectional_neigh()
[cascardo/linux.git] / drivers / net / wireless / wl12xx / ps.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23
24 #include "reg.h"
25 #include "ps.h"
26 #include "io.h"
27 #include "tx.h"
28
29 #define WL1271_WAKEUP_TIMEOUT 500
30
31 void wl1271_elp_work(struct work_struct *work)
32 {
33         struct delayed_work *dwork;
34         struct wl1271 *wl;
35
36         dwork = container_of(work, struct delayed_work, work);
37         wl = container_of(dwork, struct wl1271, elp_work);
38
39         wl1271_debug(DEBUG_PSM, "elp work");
40
41         mutex_lock(&wl->mutex);
42
43         if (unlikely(wl->state == WL1271_STATE_OFF))
44                 goto out;
45
46         if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) ||
47             (!test_bit(WL1271_FLAG_PSM, &wl->flags) &&
48              !test_bit(WL1271_FLAG_IDLE, &wl->flags)))
49                 goto out;
50
51         wl1271_debug(DEBUG_PSM, "chip to elp");
52         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
53         set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
54
55 out:
56         mutex_unlock(&wl->mutex);
57 }
58
59 #define ELP_ENTRY_DELAY  5
60
61 /* Routines to toggle sleep mode while in ELP */
62 void wl1271_ps_elp_sleep(struct wl1271 *wl)
63 {
64         if (test_bit(WL1271_FLAG_PSM, &wl->flags) ||
65             test_bit(WL1271_FLAG_IDLE, &wl->flags)) {
66                 cancel_delayed_work(&wl->elp_work);
67                 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
68                                              msecs_to_jiffies(ELP_ENTRY_DELAY));
69         }
70 }
71
72 int wl1271_ps_elp_wakeup(struct wl1271 *wl)
73 {
74         DECLARE_COMPLETION_ONSTACK(compl);
75         unsigned long flags;
76         int ret;
77         u32 start_time = jiffies;
78         bool pending = false;
79
80         if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
81                 return 0;
82
83         wl1271_debug(DEBUG_PSM, "waking up chip from elp");
84
85         /*
86          * The spinlock is required here to synchronize both the work and
87          * the completion variable in one entity.
88          */
89         spin_lock_irqsave(&wl->wl_lock, flags);
90         if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
91                 pending = true;
92         else
93                 wl->elp_compl = &compl;
94         spin_unlock_irqrestore(&wl->wl_lock, flags);
95
96         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
97
98         if (!pending) {
99                 ret = wait_for_completion_timeout(
100                         &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
101                 if (ret == 0) {
102                         wl1271_error("ELP wakeup timeout!");
103                         ieee80211_queue_work(wl->hw, &wl->recovery_work);
104                         ret = -ETIMEDOUT;
105                         goto err;
106                 } else if (ret < 0) {
107                         wl1271_error("ELP wakeup completion error.");
108                         goto err;
109                 }
110         }
111
112         clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
113
114         wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
115                      jiffies_to_msecs(jiffies - start_time));
116         goto out;
117
118 err:
119         spin_lock_irqsave(&wl->wl_lock, flags);
120         wl->elp_compl = NULL;
121         spin_unlock_irqrestore(&wl->wl_lock, flags);
122         return ret;
123
124 out:
125         return 0;
126 }
127
128 int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode,
129                        u32 rates, bool send)
130 {
131         int ret;
132
133         switch (mode) {
134         case STATION_POWER_SAVE_MODE:
135                 wl1271_debug(DEBUG_PSM, "entering psm");
136
137                 ret = wl1271_acx_wake_up_conditions(wl);
138                 if (ret < 0) {
139                         wl1271_error("couldn't set wake up conditions");
140                         return ret;
141                 }
142
143                 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
144                 if (ret < 0)
145                         return ret;
146
147                 set_bit(WL1271_FLAG_PSM, &wl->flags);
148                 break;
149         case STATION_ACTIVE_MODE:
150         default:
151                 wl1271_debug(DEBUG_PSM, "leaving psm");
152                 ret = wl1271_ps_elp_wakeup(wl);
153                 if (ret < 0)
154                         return ret;
155
156                 /* disable beacon early termination */
157                 ret = wl1271_acx_bet_enable(wl, false);
158                 if (ret < 0)
159                         return ret;
160
161                 /* disable beacon filtering */
162                 ret = wl1271_acx_beacon_filter_opt(wl, false);
163                 if (ret < 0)
164                         return ret;
165
166                 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
167                 if (ret < 0)
168                         return ret;
169
170                 clear_bit(WL1271_FLAG_PSM, &wl->flags);
171                 break;
172         }
173
174         return ret;
175 }
176
177 static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
178 {
179         int i, filtered = 0;
180         struct sk_buff *skb;
181         struct ieee80211_tx_info *info;
182         unsigned long flags;
183
184         /* filter all frames currently the low level queus for this hlid */
185         for (i = 0; i < NUM_TX_QUEUES; i++) {
186                 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
187                         info = IEEE80211_SKB_CB(skb);
188                         info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
189                         info->status.rates[0].idx = -1;
190                         ieee80211_tx_status(wl->hw, skb);
191                         filtered++;
192                 }
193         }
194
195         spin_lock_irqsave(&wl->wl_lock, flags);
196         wl->tx_queue_count -= filtered;
197         spin_unlock_irqrestore(&wl->wl_lock, flags);
198
199         wl1271_handle_tx_low_watermark(wl);
200 }
201
202 void wl1271_ps_link_start(struct wl1271 *wl, u8 hlid, bool clean_queues)
203 {
204         struct ieee80211_sta *sta;
205
206         if (test_bit(hlid, &wl->ap_ps_map))
207                 return;
208
209         wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d blks %d "
210                      "clean_queues %d", hlid, wl->links[hlid].allocated_blks,
211                      clean_queues);
212
213         rcu_read_lock();
214         sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
215         if (!sta) {
216                 wl1271_error("could not find sta %pM for starting ps",
217                              wl->links[hlid].addr);
218                 rcu_read_unlock();
219                 return;
220         }
221
222         ieee80211_sta_ps_transition_ni(sta, true);
223         rcu_read_unlock();
224
225         /* do we want to filter all frames from this link's queues? */
226         if (clean_queues)
227                 wl1271_ps_filter_frames(wl, hlid);
228
229         __set_bit(hlid, &wl->ap_ps_map);
230 }
231
232 void wl1271_ps_link_end(struct wl1271 *wl, u8 hlid)
233 {
234         struct ieee80211_sta *sta;
235
236         if (!test_bit(hlid, &wl->ap_ps_map))
237                 return;
238
239         wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
240
241         __clear_bit(hlid, &wl->ap_ps_map);
242
243         rcu_read_lock();
244         sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
245         if (!sta) {
246                 wl1271_error("could not find sta %pM for ending ps",
247                              wl->links[hlid].addr);
248                 goto end;
249         }
250
251         ieee80211_sta_ps_transition_ni(sta, false);
252 end:
253         rcu_read_unlock();
254 }