blob: a7a11088dd3104f2f8b9a2941d567c20bba822b1 [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 Coelho0f4e3122011-10-07 11:02:42 +030028#include "debug.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030029
30#define WL1271_WAKEUP_TIMEOUT 500
31
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030032void wl1271_elp_work(struct work_struct *work)
33{
34 struct delayed_work *dwork;
35 struct wl1271 *wl;
Eliad Pellerc29bb002011-10-10 10:13:03 +020036 struct wl12xx_vif *wlvif;
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030037
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
Juuso Oikarinen8c7f4f32010-09-21 06:23:29 +020045 if (unlikely(wl->state == WL1271_STATE_OFF))
46 goto out;
47
Eliad Pellera665d6e2011-04-03 02:01:59 +030048 /* our work might have been already cancelled */
49 if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
50 goto out;
51
Eliad Pellerc29bb002011-10-10 10:13:03 +020052 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030053 goto out;
54
Eliad Pellerc29bb002011-10-10 10:13:03 +020055 wl12xx_for_each_wlvif(wl, wlvif) {
56 if (!test_bit(WLVIF_FLAG_PSM, &wlvif->flags) &&
57 !test_bit(WL1271_FLAG_IDLE, &wl->flags))
58 goto out;
59 }
60
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030061 wl1271_debug(DEBUG_PSM, "chip to elp");
Juuso Oikarinen74621412009-10-12 15:08:54 +030062 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
Juuso Oikarinen71449f82009-12-11 15:41:07 +020063 set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030064
65out:
66 mutex_unlock(&wl->mutex);
67}
68
69#define ELP_ENTRY_DELAY 5
70
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030071/* Routines to toggle sleep mode while in ELP */
72void wl1271_ps_elp_sleep(struct wl1271 *wl)
73{
Eliad Pellerc29bb002011-10-10 10:13:03 +020074 struct wl12xx_vif *wlvif;
75
Eliad Pellera665d6e2011-04-03 02:01:59 +030076 /* we shouldn't get consecutive sleep requests */
77 if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
78 return;
79
Eliad Pellerc29bb002011-10-10 10:13:03 +020080 wl12xx_for_each_wlvif(wl, wlvif) {
81 if (!test_bit(WLVIF_FLAG_PSM, &wlvif->flags) &&
82 !test_bit(WL1271_FLAG_IDLE, &wl->flags))
83 return;
84 }
Eliad Pellera665d6e2011-04-03 02:01:59 +030085
86 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
87 msecs_to_jiffies(ELP_ENTRY_DELAY));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030088}
89
Ido Yariva6208652011-03-01 15:14:41 +020090int wl1271_ps_elp_wakeup(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030091{
92 DECLARE_COMPLETION_ONSTACK(compl);
93 unsigned long flags;
94 int ret;
95 u32 start_time = jiffies;
96 bool pending = false;
97
Eliad Pellera665d6e2011-04-03 02:01:59 +030098 /*
99 * we might try to wake up even if we didn't go to sleep
100 * before (e.g. on boot)
101 */
102 if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
103 return 0;
104
105 /* don't cancel_sync as it might contend for a mutex and deadlock */
106 cancel_delayed_work(&wl->elp_work);
107
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200108 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300109 return 0;
110
111 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
112
113 /*
114 * The spinlock is required here to synchronize both the work and
115 * the completion variable in one entity.
116 */
117 spin_lock_irqsave(&wl->wl_lock, flags);
Ido Yariva6208652011-03-01 15:14:41 +0200118 if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300119 pending = true;
120 else
121 wl->elp_compl = &compl;
122 spin_unlock_irqrestore(&wl->wl_lock, flags);
123
Juuso Oikarinen74621412009-10-12 15:08:54 +0300124 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300125
126 if (!pending) {
127 ret = wait_for_completion_timeout(
128 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
129 if (ret == 0) {
130 wl1271_error("ELP wakeup timeout!");
Ido Yarivbaacb9a2011-06-06 14:57:05 +0300131 wl12xx_queue_recovery_work(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300132 ret = -ETIMEDOUT;
133 goto err;
134 } else if (ret < 0) {
135 wl1271_error("ELP wakeup completion error.");
136 goto err;
137 }
138 }
139
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200140 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300141
142 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
143 jiffies_to_msecs(jiffies - start_time));
144 goto out;
145
146err:
147 spin_lock_irqsave(&wl->wl_lock, flags);
148 wl->elp_compl = NULL;
149 spin_unlock_irqrestore(&wl->wl_lock, flags);
150 return ret;
151
152out:
153 return 0;
154}
155
Eliad Peller0603d892011-10-05 11:55:51 +0200156int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
157 enum wl1271_cmd_ps_mode mode, u32 rates, bool send)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300158{
159 int ret;
160
161 switch (mode) {
162 case STATION_POWER_SAVE_MODE:
163 wl1271_debug(DEBUG_PSM, "entering psm");
Juuso Oikarinen19221672009-10-08 21:56:35 +0300164
Eliad Peller0603d892011-10-05 11:55:51 +0200165 ret = wl1271_acx_wake_up_conditions(wl, wlvif);
Juuso Oikarinen03f06b72010-08-10 06:38:36 +0200166 if (ret < 0) {
167 wl1271_error("couldn't set wake up conditions");
168 return ret;
169 }
170
Eliad Peller0603d892011-10-05 11:55:51 +0200171 ret = wl1271_cmd_ps_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300172 if (ret < 0)
173 return ret;
174
Eliad Pellerc29bb002011-10-10 10:13:03 +0200175 set_bit(WLVIF_FLAG_PSM, &wlvif->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300176 break;
177 case STATION_ACTIVE_MODE:
178 default:
179 wl1271_debug(DEBUG_PSM, "leaving psm");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300180
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300181 /* disable beacon early termination */
Eliad Peller1b92f152011-10-10 10:13:09 +0200182 if (wlvif->band == IEEE80211_BAND_2GHZ) {
Eliad Peller0603d892011-10-05 11:55:51 +0200183 ret = wl1271_acx_bet_enable(wl, wlvif, false);
Shahar Levi0e44eb22011-05-16 15:35:30 +0300184 if (ret < 0)
185 return ret;
186 }
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300187
Eliad Peller0603d892011-10-05 11:55:51 +0200188 ret = wl1271_cmd_ps_mode(wl, wlvif, STATION_ACTIVE_MODE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300189 if (ret < 0)
190 return ret;
191
Eliad Pellerc29bb002011-10-10 10:13:03 +0200192 clear_bit(WLVIF_FLAG_PSM, &wlvif->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300193 break;
194 }
195
196 return ret;
197}
Arik Nemtsovb622d992011-02-23 00:22:31 +0200198
199static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
200{
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300201 int i;
Arik Nemtsovb622d992011-02-23 00:22:31 +0200202 struct sk_buff *skb;
203 struct ieee80211_tx_info *info;
204 unsigned long flags;
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300205 int filtered[NUM_TX_QUEUES];
Arik Nemtsovb622d992011-02-23 00:22:31 +0200206
Arik Nemtsovf8e0af62011-08-25 12:43:12 +0300207 /* filter all frames currently in the low level queues for this hlid */
Arik Nemtsovb622d992011-02-23 00:22:31 +0200208 for (i = 0; i < NUM_TX_QUEUES; i++) {
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300209 filtered[i] = 0;
Arik Nemtsovb622d992011-02-23 00:22:31 +0200210 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
Arik Nemtsovf8e0af62011-08-25 12:43:12 +0300211 filtered[i]++;
212
213 if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
214 continue;
215
Arik Nemtsovb622d992011-02-23 00:22:31 +0200216 info = IEEE80211_SKB_CB(skb);
217 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
218 info->status.rates[0].idx = -1;
Eliad Pellerc27d3ac2011-06-07 10:40:39 +0300219 ieee80211_tx_status_ni(wl->hw, skb);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200220 }
221 }
222
223 spin_lock_irqsave(&wl->wl_lock, flags);
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300224 for (i = 0; i < NUM_TX_QUEUES; i++)
225 wl->tx_queue_count[i] -= filtered[i];
Arik Nemtsovb622d992011-02-23 00:22:31 +0200226 spin_unlock_irqrestore(&wl->wl_lock, flags);
227
228 wl1271_handle_tx_low_watermark(wl);
229}
230
Eliad Peller6e8cd332011-10-10 10:13:13 +0200231void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
232 u8 hlid, bool clean_queues)
Arik Nemtsovb622d992011-02-23 00:22:31 +0200233{
234 struct ieee80211_sta *sta;
Eliad Peller6e8cd332011-10-10 10:13:13 +0200235 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200236
237 if (test_bit(hlid, &wl->ap_ps_map))
238 return;
239
Arik Nemtsov9b17f1b2011-08-14 13:17:35 +0300240 wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
241 "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
Arik Nemtsovb622d992011-02-23 00:22:31 +0200242 clean_queues);
243
244 rcu_read_lock();
Eliad Peller6e8cd332011-10-10 10:13:13 +0200245 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200246 if (!sta) {
247 wl1271_error("could not find sta %pM for starting ps",
248 wl->links[hlid].addr);
249 rcu_read_unlock();
250 return;
251 }
252
253 ieee80211_sta_ps_transition_ni(sta, true);
254 rcu_read_unlock();
255
256 /* do we want to filter all frames from this link's queues? */
257 if (clean_queues)
258 wl1271_ps_filter_frames(wl, hlid);
259
260 __set_bit(hlid, &wl->ap_ps_map);
261}
262
Eliad Peller6e8cd332011-10-10 10:13:13 +0200263void wl12xx_ps_link_end(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
Arik Nemtsovb622d992011-02-23 00:22:31 +0200264{
265 struct ieee80211_sta *sta;
Eliad Peller6e8cd332011-10-10 10:13:13 +0200266 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200267
268 if (!test_bit(hlid, &wl->ap_ps_map))
269 return;
270
271 wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
272
273 __clear_bit(hlid, &wl->ap_ps_map);
274
275 rcu_read_lock();
Eliad Peller6e8cd332011-10-10 10:13:13 +0200276 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200277 if (!sta) {
278 wl1271_error("could not find sta %pM for ending ps",
279 wl->links[hlid].addr);
280 goto end;
281 }
282
283 ieee80211_sta_ps_transition_ni(sta, false);
284end:
285 rcu_read_unlock();
286}