blob: 971f13e792da0bf304c15f877ecdcb7d3c8bdf6a [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 "reg.h"
25#include "ps.h"
26#include "io.h"
Arik Nemtsovb622d992011-02-23 00:22:31 +020027#include "tx.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030028
29#define WL1271_WAKEUP_TIMEOUT 500
30
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030031void 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
Juuso Oikarinen8c7f4f32010-09-21 06:23:29 +020043 if (unlikely(wl->state == WL1271_STATE_OFF))
44 goto out;
45
Juuso Oikarinen71449f82009-12-11 15:41:07 +020046 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) ||
Juuso Oikarinene1972812010-04-09 11:07:29 +030047 (!test_bit(WL1271_FLAG_PSM, &wl->flags) &&
48 !test_bit(WL1271_FLAG_IDLE, &wl->flags)))
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030049 goto out;
50
51 wl1271_debug(DEBUG_PSM, "chip to elp");
Juuso Oikarinen74621412009-10-12 15:08:54 +030052 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
Juuso Oikarinen71449f82009-12-11 15:41:07 +020053 set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030054
55out:
56 mutex_unlock(&wl->mutex);
57}
58
59#define ELP_ENTRY_DELAY 5
60
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030061/* Routines to toggle sleep mode while in ELP */
62void wl1271_ps_elp_sleep(struct wl1271 *wl)
63{
Juuso Oikarinene1972812010-04-09 11:07:29 +030064 if (test_bit(WL1271_FLAG_PSM, &wl->flags) ||
65 test_bit(WL1271_FLAG_IDLE, &wl->flags)) {
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030066 cancel_delayed_work(&wl->elp_work);
67 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +020068 msecs_to_jiffies(ELP_ENTRY_DELAY));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030069 }
70}
71
Ido Yariva6208652011-03-01 15:14:41 +020072int wl1271_ps_elp_wakeup(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030073{
74 DECLARE_COMPLETION_ONSTACK(compl);
75 unsigned long flags;
76 int ret;
77 u32 start_time = jiffies;
78 bool pending = false;
79
Juuso Oikarinen71449f82009-12-11 15:41:07 +020080 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030081 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);
Ido Yariva6208652011-03-01 15:14:41 +020090 if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030091 pending = true;
92 else
93 wl->elp_compl = &compl;
94 spin_unlock_irqrestore(&wl->wl_lock, flags);
95
Juuso Oikarinen74621412009-10-12 15:08:54 +030096 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030097
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!");
Juuso Oikarinen52b0e7a2010-09-21 06:23:31 +0200103 ieee80211_queue_work(wl->hw, &wl->recovery_work);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300104 ret = -ETIMEDOUT;
105 goto err;
106 } else if (ret < 0) {
107 wl1271_error("ELP wakeup completion error.");
108 goto err;
109 }
110 }
111
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200112 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300113
114 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
115 jiffies_to_msecs(jiffies - start_time));
116 goto out;
117
118err:
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
124out:
125 return 0;
126}
127
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200128int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode,
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +0300129 u32 rates, bool send)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300130{
131 int ret;
132
133 switch (mode) {
134 case STATION_POWER_SAVE_MODE:
135 wl1271_debug(DEBUG_PSM, "entering psm");
Juuso Oikarinen19221672009-10-08 21:56:35 +0300136
Juuso Oikarinen03f06b72010-08-10 06:38:36 +0200137 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
Eliad Pellerc8bde242011-02-02 09:59:35 +0200143 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300144 if (ret < 0)
145 return ret;
146
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200147 set_bit(WL1271_FLAG_PSM, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300148 break;
149 case STATION_ACTIVE_MODE:
150 default:
151 wl1271_debug(DEBUG_PSM, "leaving psm");
Ido Yariva6208652011-03-01 15:14:41 +0200152 ret = wl1271_ps_elp_wakeup(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300153 if (ret < 0)
154 return ret;
155
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300156 /* disable beacon early termination */
157 ret = wl1271_acx_bet_enable(wl, false);
158 if (ret < 0)
159 return ret;
160
Juuso Oikarinen19221672009-10-08 21:56:35 +0300161 /* disable beacon filtering */
162 ret = wl1271_acx_beacon_filter_opt(wl, false);
163 if (ret < 0)
164 return ret;
165
Eliad Pellerc8bde242011-02-02 09:59:35 +0200166 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300167 if (ret < 0)
168 return ret;
169
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200170 clear_bit(WL1271_FLAG_PSM, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300171 break;
172 }
173
174 return ret;
175}
Arik Nemtsovb622d992011-02-23 00:22:31 +0200176
177static 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
202void 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
232void 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);
252end:
253 rcu_read_unlock();
254}