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 #include "debug.h"
29
30 #define WL1271_WAKEUP_TIMEOUT 500
31
wl1271_elp_work(struct work_struct * work)32 void wl1271_elp_work(struct work_struct *work)
33 {
34 struct delayed_work *dwork;
35 struct wl1271 *wl;
36 struct wl12xx_vif *wlvif;
37
38 dwork = container_of(work, struct delayed_work, work);
39 wl = container_of(dwork, struct wl1271, elp_work);
40
41 wl1271_debug(DEBUG_PSM, "elp work");
42
43 mutex_lock(&wl->mutex);
44
45 if (unlikely(wl->state == WL1271_STATE_OFF))
46 goto out;
47
48 /* our work might have been already cancelled */
49 if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
50 goto out;
51
52 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
53 goto out;
54
55 wl12xx_for_each_wlvif(wl, wlvif) {
56 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
57 goto out;
58
59 if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
60 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
61 goto out;
62 }
63
64 wl1271_debug(DEBUG_PSM, "chip to elp");
65 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
66 set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
67
68 out:
69 mutex_unlock(&wl->mutex);
70 }
71
72 /* Routines to toggle sleep mode while in ELP */
wl1271_ps_elp_sleep(struct wl1271 * wl)73 void wl1271_ps_elp_sleep(struct wl1271 *wl)
74 {
75 struct wl12xx_vif *wlvif;
76
77 /* we shouldn't get consecutive sleep requests */
78 if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
79 return;
80
81 wl12xx_for_each_wlvif(wl, wlvif) {
82 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
83 return;
84
85 if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
86 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
87 return;
88 }
89
90 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
91 msecs_to_jiffies(wl->conf.conn.dynamic_ps_timeout));
92 }
93
wl1271_ps_elp_wakeup(struct wl1271 * wl)94 int wl1271_ps_elp_wakeup(struct wl1271 *wl)
95 {
96 DECLARE_COMPLETION_ONSTACK(compl);
97 unsigned long flags;
98 int ret;
99 u32 start_time = jiffies;
100 bool pending = false;
101
102 /*
103 * we might try to wake up even if we didn't go to sleep
104 * before (e.g. on boot)
105 */
106 if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
107 return 0;
108
109 /* don't cancel_sync as it might contend for a mutex and deadlock */
110 cancel_delayed_work(&wl->elp_work);
111
112 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
113 return 0;
114
115 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
116
117 /*
118 * The spinlock is required here to synchronize both the work and
119 * the completion variable in one entity.
120 */
121 spin_lock_irqsave(&wl->wl_lock, flags);
122 if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
123 pending = true;
124 else
125 wl->elp_compl = &compl;
126 spin_unlock_irqrestore(&wl->wl_lock, flags);
127
128 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
129
130 if (!pending) {
131 ret = wait_for_completion_timeout(
132 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
133 if (ret == 0) {
134 wl1271_error("ELP wakeup timeout!");
135 wl12xx_queue_recovery_work(wl);
136 ret = -ETIMEDOUT;
137 goto err;
138 } else if (ret < 0) {
139 wl1271_error("ELP wakeup completion error.");
140 goto err;
141 }
142 }
143
144 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
145
146 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
147 jiffies_to_msecs(jiffies - start_time));
148 goto out;
149
150 err:
151 spin_lock_irqsave(&wl->wl_lock, flags);
152 wl->elp_compl = NULL;
153 spin_unlock_irqrestore(&wl->wl_lock, flags);
154 return ret;
155
156 out:
157 return 0;
158 }
159
wl1271_ps_set_mode(struct wl1271 * wl,struct wl12xx_vif * wlvif,enum wl1271_cmd_ps_mode mode)160 int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
161 enum wl1271_cmd_ps_mode mode)
162 {
163 int ret;
164 u16 timeout = wl->conf.conn.dynamic_ps_timeout;
165
166 switch (mode) {
167 case STATION_AUTO_PS_MODE:
168 case STATION_POWER_SAVE_MODE:
169 wl1271_debug(DEBUG_PSM, "entering psm (mode=%d,timeout=%u)",
170 mode, timeout);
171
172 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
173 wl->conf.conn.wake_up_event,
174 wl->conf.conn.listen_interval);
175 if (ret < 0) {
176 wl1271_error("couldn't set wake up conditions");
177 return ret;
178 }
179
180 ret = wl1271_cmd_ps_mode(wl, wlvif, mode, timeout);
181 if (ret < 0)
182 return ret;
183
184 set_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
185
186 /* enable beacon early termination. Not relevant for 5GHz */
187 if (wlvif->band == IEEE80211_BAND_2GHZ) {
188 ret = wl1271_acx_bet_enable(wl, wlvif, true);
189 if (ret < 0)
190 return ret;
191 }
192 break;
193 case STATION_ACTIVE_MODE:
194 wl1271_debug(DEBUG_PSM, "leaving psm");
195
196 /* disable beacon early termination */
197 if (wlvif->band == IEEE80211_BAND_2GHZ) {
198 ret = wl1271_acx_bet_enable(wl, wlvif, false);
199 if (ret < 0)
200 return ret;
201 }
202
203 ret = wl1271_cmd_ps_mode(wl, wlvif, mode, 0);
204 if (ret < 0)
205 return ret;
206
207 clear_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
208 break;
209 default:
210 wl1271_warning("trying to set ps to unsupported mode %d", mode);
211 ret = -EINVAL;
212 }
213
214 return ret;
215 }
216
wl1271_ps_filter_frames(struct wl1271 * wl,u8 hlid)217 static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
218 {
219 int i;
220 struct sk_buff *skb;
221 struct ieee80211_tx_info *info;
222 unsigned long flags;
223 int filtered[NUM_TX_QUEUES];
224
225 /* filter all frames currently in the low level queues for this hlid */
226 for (i = 0; i < NUM_TX_QUEUES; i++) {
227 filtered[i] = 0;
228 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
229 filtered[i]++;
230
231 if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
232 continue;
233
234 info = IEEE80211_SKB_CB(skb);
235 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
236 info->status.rates[0].idx = -1;
237 ieee80211_tx_status_ni(wl->hw, skb);
238 }
239 }
240
241 spin_lock_irqsave(&wl->wl_lock, flags);
242 for (i = 0; i < NUM_TX_QUEUES; i++)
243 wl->tx_queue_count[i] -= filtered[i];
244 spin_unlock_irqrestore(&wl->wl_lock, flags);
245
246 wl1271_handle_tx_low_watermark(wl);
247 }
248
wl12xx_ps_link_start(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 hlid,bool clean_queues)249 void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
250 u8 hlid, bool clean_queues)
251 {
252 struct ieee80211_sta *sta;
253 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
254
255 if (test_bit(hlid, &wl->ap_ps_map))
256 return;
257
258 wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
259 "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
260 clean_queues);
261
262 rcu_read_lock();
263 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
264 if (!sta) {
265 wl1271_error("could not find sta %pM for starting ps",
266 wl->links[hlid].addr);
267 rcu_read_unlock();
268 return;
269 }
270
271 ieee80211_sta_ps_transition_ni(sta, true);
272 rcu_read_unlock();
273
274 /* do we want to filter all frames from this link's queues? */
275 if (clean_queues)
276 wl1271_ps_filter_frames(wl, hlid);
277
278 __set_bit(hlid, &wl->ap_ps_map);
279 }
280
wl12xx_ps_link_end(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 hlid)281 void wl12xx_ps_link_end(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
282 {
283 struct ieee80211_sta *sta;
284 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
285
286 if (!test_bit(hlid, &wl->ap_ps_map))
287 return;
288
289 wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
290
291 __clear_bit(hlid, &wl->ap_ps_map);
292
293 rcu_read_lock();
294 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
295 if (!sta) {
296 wl1271_error("could not find sta %pM for ending ps",
297 wl->links[hlid].addr);
298 goto end;
299 }
300
301 ieee80211_sta_ps_transition_ni(sta, false);
302 end:
303 rcu_read_unlock();
304 }
305