blob: b8deada5d0206203a3d8462253dcb185662d1927 [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");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300152
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300153 /* disable beacon early termination */
154 ret = wl1271_acx_bet_enable(wl, false);
155 if (ret < 0)
156 return ret;
157
Juuso Oikarinen19221672009-10-08 21:56:35 +0300158 /* disable beacon filtering */
159 ret = wl1271_acx_beacon_filter_opt(wl, false);
160 if (ret < 0)
161 return ret;
162
Eliad Pellerc8bde242011-02-02 09:59:35 +0200163 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300164 if (ret < 0)
165 return ret;
166
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200167 clear_bit(WL1271_FLAG_PSM, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300168 break;
169 }
170
171 return ret;
172}
Arik Nemtsovb622d992011-02-23 00:22:31 +0200173
174static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
175{
176 int i, filtered = 0;
177 struct sk_buff *skb;
178 struct ieee80211_tx_info *info;
179 unsigned long flags;
180
181 /* filter all frames currently the low level queus for this hlid */
182 for (i = 0; i < NUM_TX_QUEUES; i++) {
183 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
184 info = IEEE80211_SKB_CB(skb);
185 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
186 info->status.rates[0].idx = -1;
187 ieee80211_tx_status(wl->hw, skb);
188 filtered++;
189 }
190 }
191
192 spin_lock_irqsave(&wl->wl_lock, flags);
193 wl->tx_queue_count -= filtered;
194 spin_unlock_irqrestore(&wl->wl_lock, flags);
195
196 wl1271_handle_tx_low_watermark(wl);
197}
198
199void wl1271_ps_link_start(struct wl1271 *wl, u8 hlid, bool clean_queues)
200{
201 struct ieee80211_sta *sta;
202
203 if (test_bit(hlid, &wl->ap_ps_map))
204 return;
205
206 wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d blks %d "
207 "clean_queues %d", hlid, wl->links[hlid].allocated_blks,
208 clean_queues);
209
210 rcu_read_lock();
211 sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
212 if (!sta) {
213 wl1271_error("could not find sta %pM for starting ps",
214 wl->links[hlid].addr);
215 rcu_read_unlock();
216 return;
217 }
218
219 ieee80211_sta_ps_transition_ni(sta, true);
220 rcu_read_unlock();
221
222 /* do we want to filter all frames from this link's queues? */
223 if (clean_queues)
224 wl1271_ps_filter_frames(wl, hlid);
225
226 __set_bit(hlid, &wl->ap_ps_map);
227}
228
229void wl1271_ps_link_end(struct wl1271 *wl, u8 hlid)
230{
231 struct ieee80211_sta *sta;
232
233 if (!test_bit(hlid, &wl->ap_ps_map))
234 return;
235
236 wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
237
238 __clear_bit(hlid, &wl->ap_ps_map);
239
240 rcu_read_lock();
241 sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
242 if (!sta) {
243 wl1271_error("could not find sta %pM for ending ps",
244 wl->links[hlid].addr);
245 goto end;
246 }
247
248 ieee80211_sta_ps_transition_ni(sta, false);
249end:
250 rcu_read_unlock();
251}