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