blob: 958535dee9f3440d2526ead024cc1082d155a543 [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
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
Shahar Levi00d20102010-11-08 11:20:10 +000024#include "ps.h"
25#include "io.h"
Arik Nemtsovb622d992011-02-23 00:22:31 +020026#include "tx.h"
Luciano Coelho0f4e3122011-10-07 11:02:42 +030027#include "debug.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030028
29#define WL1271_WAKEUP_TIMEOUT 500
30
Eliad Peller1ab0f212012-05-15 17:08:52 +030031#define ELP_ENTRY_DELAY 5
32
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030033void wl1271_elp_work(struct work_struct *work)
34{
35 struct delayed_work *dwork;
36 struct wl1271 *wl;
Eliad Pellerc29bb002011-10-10 10:13:03 +020037 struct wl12xx_vif *wlvif;
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030038
39 dwork = container_of(work, struct delayed_work, work);
40 wl = container_of(dwork, struct wl1271, elp_work);
41
42 wl1271_debug(DEBUG_PSM, "elp work");
43
44 mutex_lock(&wl->mutex);
45
Juuso Oikarinen8c7f4f32010-09-21 06:23:29 +020046 if (unlikely(wl->state == WL1271_STATE_OFF))
47 goto out;
48
Eliad Pellera665d6e2011-04-03 02:01:59 +030049 /* our work might have been already cancelled */
50 if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
51 goto out;
52
Eliad Pellerc29bb002011-10-10 10:13:03 +020053 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030054 goto out;
55
Eliad Pellerc29bb002011-10-10 10:13:03 +020056 wl12xx_for_each_wlvif(wl, wlvif) {
Eliad Pellera0c7b782011-12-18 20:25:41 +020057 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
58 goto out;
59
Eyal Shapira5c0dc2f2012-02-02 19:06:45 +020060 if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
Eliad Pellera0c7b782011-12-18 20:25:41 +020061 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
Eliad Pellerc29bb002011-10-10 10:13:03 +020062 goto out;
63 }
64
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030065 wl1271_debug(DEBUG_PSM, "chip to elp");
Luciano Coelho00782132011-11-29 13:38:37 +020066 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_SLEEP);
Juuso Oikarinen71449f82009-12-11 15:41:07 +020067 set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030068
69out:
70 mutex_unlock(&wl->mutex);
71}
72
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030073/* Routines to toggle sleep mode while in ELP */
74void wl1271_ps_elp_sleep(struct wl1271 *wl)
75{
Eliad Pellerc29bb002011-10-10 10:13:03 +020076 struct wl12xx_vif *wlvif;
Eliad Peller1ab0f212012-05-15 17:08:52 +030077 u32 timeout;
Eliad Pellerc29bb002011-10-10 10:13:03 +020078
Luciano Coelho441101f2011-11-30 15:07:20 +020079 if (wl->quirks & WLCORE_QUIRK_NO_ELP)
80 return;
81
Eliad Pellera665d6e2011-04-03 02:01:59 +030082 /* we shouldn't get consecutive sleep requests */
83 if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
84 return;
85
Eliad Pellerc29bb002011-10-10 10:13:03 +020086 wl12xx_for_each_wlvif(wl, wlvif) {
Eliad Pellera0c7b782011-12-18 20:25:41 +020087 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
88 return;
89
Eyal Shapira5c0dc2f2012-02-02 19:06:45 +020090 if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
Eliad Pellera0c7b782011-12-18 20:25:41 +020091 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
Eliad Pellerc29bb002011-10-10 10:13:03 +020092 return;
93 }
Eliad Pellera665d6e2011-04-03 02:01:59 +030094
Eliad Peller1ab0f212012-05-15 17:08:52 +030095 if (wl->conf.conn.forced_ps)
96 timeout = ELP_ENTRY_DELAY;
97 else
98 timeout = wl->conf.conn.dynamic_ps_timeout;
99
Eliad Pellera665d6e2011-04-03 02:01:59 +0300100 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
Eliad Peller1ab0f212012-05-15 17:08:52 +0300101 msecs_to_jiffies(timeout));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300102}
103
Ido Yariva6208652011-03-01 15:14:41 +0200104int wl1271_ps_elp_wakeup(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300105{
106 DECLARE_COMPLETION_ONSTACK(compl);
107 unsigned long flags;
108 int ret;
109 u32 start_time = jiffies;
110 bool pending = false;
111
Eliad Pellera665d6e2011-04-03 02:01:59 +0300112 /*
113 * we might try to wake up even if we didn't go to sleep
114 * before (e.g. on boot)
115 */
116 if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
117 return 0;
118
119 /* don't cancel_sync as it might contend for a mutex and deadlock */
120 cancel_delayed_work(&wl->elp_work);
121
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200122 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300123 return 0;
124
125 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
126
127 /*
128 * The spinlock is required here to synchronize both the work and
129 * the completion variable in one entity.
130 */
131 spin_lock_irqsave(&wl->wl_lock, flags);
Ido Yariva6208652011-03-01 15:14:41 +0200132 if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300133 pending = true;
134 else
135 wl->elp_compl = &compl;
136 spin_unlock_irqrestore(&wl->wl_lock, flags);
137
Luciano Coelho00782132011-11-29 13:38:37 +0200138 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300139
140 if (!pending) {
141 ret = wait_for_completion_timeout(
142 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
143 if (ret == 0) {
144 wl1271_error("ELP wakeup timeout!");
Ido Yarivbaacb9ae2011-06-06 14:57:05 +0300145 wl12xx_queue_recovery_work(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300146 ret = -ETIMEDOUT;
147 goto err;
148 } else if (ret < 0) {
149 wl1271_error("ELP wakeup completion error.");
150 goto err;
151 }
152 }
153
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200154 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300155
156 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
157 jiffies_to_msecs(jiffies - start_time));
158 goto out;
159
160err:
161 spin_lock_irqsave(&wl->wl_lock, flags);
162 wl->elp_compl = NULL;
163 spin_unlock_irqrestore(&wl->wl_lock, flags);
164 return ret;
165
166out:
167 return 0;
168}
169
Eliad Peller0603d892011-10-05 11:55:51 +0200170int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
Eyal Shapira248a0012012-01-31 11:57:23 +0200171 enum wl1271_cmd_ps_mode mode)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300172{
173 int ret;
Eyal Shapiraf1d63a52012-01-31 11:57:21 +0200174 u16 timeout = wl->conf.conn.dynamic_ps_timeout;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300175
176 switch (mode) {
Eyal Shapiraf1d63a52012-01-31 11:57:21 +0200177 case STATION_AUTO_PS_MODE:
Eyal Shapira5c0dc2f2012-02-02 19:06:45 +0200178 case STATION_POWER_SAVE_MODE:
Eyal Shapiraf1d63a52012-01-31 11:57:21 +0200179 wl1271_debug(DEBUG_PSM, "entering psm (mode=%d,timeout=%u)",
180 mode, timeout);
Juuso Oikarinen19221672009-10-08 21:56:35 +0300181
Eyal Shapiradae728f2012-02-02 12:03:39 +0200182 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
183 wl->conf.conn.wake_up_event,
184 wl->conf.conn.listen_interval);
Juuso Oikarinen03f06b72010-08-10 06:38:36 +0200185 if (ret < 0) {
186 wl1271_error("couldn't set wake up conditions");
187 return ret;
188 }
189
Eyal Shapiraf1d63a52012-01-31 11:57:21 +0200190 ret = wl1271_cmd_ps_mode(wl, wlvif, mode, timeout);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300191 if (ret < 0)
192 return ret;
193
Eyal Shapira5c0dc2f2012-02-02 19:06:45 +0200194 set_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
Eyal Shapiraed471d32012-01-31 11:57:24 +0200195
196 /* enable beacon early termination. Not relevant for 5GHz */
197 if (wlvif->band == IEEE80211_BAND_2GHZ) {
198 ret = wl1271_acx_bet_enable(wl, wlvif, true);
199 if (ret < 0)
200 return ret;
201 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300202 break;
203 case STATION_ACTIVE_MODE:
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300204 wl1271_debug(DEBUG_PSM, "leaving psm");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300205
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300206 /* disable beacon early termination */
Eliad Peller1b92f152011-10-10 10:13:09 +0200207 if (wlvif->band == IEEE80211_BAND_2GHZ) {
Eliad Peller0603d892011-10-05 11:55:51 +0200208 ret = wl1271_acx_bet_enable(wl, wlvif, false);
Shahar Levi0e44eb22011-05-16 15:35:30 +0300209 if (ret < 0)
210 return ret;
211 }
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300212
Eyal Shapiraf1d63a52012-01-31 11:57:21 +0200213 ret = wl1271_cmd_ps_mode(wl, wlvif, mode, 0);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300214 if (ret < 0)
215 return ret;
216
Eyal Shapira5c0dc2f2012-02-02 19:06:45 +0200217 clear_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300218 break;
Eyal Shapiraf1d63a52012-01-31 11:57:21 +0200219 default:
220 wl1271_warning("trying to set ps to unsupported mode %d", mode);
221 ret = -EINVAL;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300222 }
223
224 return ret;
225}
Arik Nemtsovb622d992011-02-23 00:22:31 +0200226
227static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
228{
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300229 int i;
Arik Nemtsovb622d992011-02-23 00:22:31 +0200230 struct sk_buff *skb;
231 struct ieee80211_tx_info *info;
232 unsigned long flags;
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300233 int filtered[NUM_TX_QUEUES];
Arik Nemtsovb622d992011-02-23 00:22:31 +0200234
Arik Nemtsovf8e0af62011-08-25 12:43:12 +0300235 /* filter all frames currently in the low level queues for this hlid */
Arik Nemtsovb622d992011-02-23 00:22:31 +0200236 for (i = 0; i < NUM_TX_QUEUES; i++) {
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300237 filtered[i] = 0;
Arik Nemtsovb622d992011-02-23 00:22:31 +0200238 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
Arik Nemtsovf8e0af62011-08-25 12:43:12 +0300239 filtered[i]++;
240
241 if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
242 continue;
243
Arik Nemtsovb622d992011-02-23 00:22:31 +0200244 info = IEEE80211_SKB_CB(skb);
245 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
246 info->status.rates[0].idx = -1;
Eliad Pellerc27d3ac2011-06-07 10:40:39 +0300247 ieee80211_tx_status_ni(wl->hw, skb);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200248 }
249 }
250
251 spin_lock_irqsave(&wl->wl_lock, flags);
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300252 for (i = 0; i < NUM_TX_QUEUES; i++)
253 wl->tx_queue_count[i] -= filtered[i];
Arik Nemtsovb622d992011-02-23 00:22:31 +0200254 spin_unlock_irqrestore(&wl->wl_lock, flags);
255
256 wl1271_handle_tx_low_watermark(wl);
257}
258
Eliad Peller6e8cd332011-10-10 10:13:13 +0200259void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
260 u8 hlid, bool clean_queues)
Arik Nemtsovb622d992011-02-23 00:22:31 +0200261{
262 struct ieee80211_sta *sta;
Eliad Peller6e8cd332011-10-10 10:13:13 +0200263 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200264
265 if (test_bit(hlid, &wl->ap_ps_map))
266 return;
267
Arik Nemtsov9b17f1b2011-08-14 13:17:35 +0300268 wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
269 "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
Arik Nemtsovb622d992011-02-23 00:22:31 +0200270 clean_queues);
271
272 rcu_read_lock();
Eliad Peller6e8cd332011-10-10 10:13:13 +0200273 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200274 if (!sta) {
275 wl1271_error("could not find sta %pM for starting ps",
276 wl->links[hlid].addr);
277 rcu_read_unlock();
278 return;
279 }
280
281 ieee80211_sta_ps_transition_ni(sta, true);
282 rcu_read_unlock();
283
284 /* do we want to filter all frames from this link's queues? */
285 if (clean_queues)
286 wl1271_ps_filter_frames(wl, hlid);
287
288 __set_bit(hlid, &wl->ap_ps_map);
289}
290
Eliad Peller6e8cd332011-10-10 10:13:13 +0200291void wl12xx_ps_link_end(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
Arik Nemtsovb622d992011-02-23 00:22:31 +0200292{
293 struct ieee80211_sta *sta;
Eliad Peller6e8cd332011-10-10 10:13:13 +0200294 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200295
296 if (!test_bit(hlid, &wl->ap_ps_map))
297 return;
298
299 wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
300
301 __clear_bit(hlid, &wl->ap_ps_map);
302
303 rcu_read_lock();
Eliad Peller6e8cd332011-10-10 10:13:13 +0200304 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200305 if (!sta) {
306 wl1271_error("could not find sta %pM for ending ps",
307 wl->links[hlid].addr);
308 goto end;
309 }
310
311 ieee80211_sta_ps_transition_ni(sta, false);
312end:
313 rcu_read_unlock();
314}