blob: 8cd81cec2841961780fc401fb90daf494a4d53af [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;
Eliad Pellerc29bb002011-10-10 10:13:03 +020035 struct wl12xx_vif *wlvif;
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030036
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
Juuso Oikarinen8c7f4f32010-09-21 06:23:29 +020044 if (unlikely(wl->state == WL1271_STATE_OFF))
45 goto out;
46
Eliad Pellera665d6e2011-04-03 02:01:59 +030047 /* our work might have been already cancelled */
48 if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
49 goto out;
50
Eliad Pellerc29bb002011-10-10 10:13:03 +020051 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030052 goto out;
53
Eliad Pellerc29bb002011-10-10 10:13:03 +020054 wl12xx_for_each_wlvif(wl, wlvif) {
55 if (!test_bit(WLVIF_FLAG_PSM, &wlvif->flags) &&
56 !test_bit(WL1271_FLAG_IDLE, &wl->flags))
57 goto out;
58 }
59
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030060 wl1271_debug(DEBUG_PSM, "chip to elp");
Juuso Oikarinen74621412009-10-12 15:08:54 +030061 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
Juuso Oikarinen71449f82009-12-11 15:41:07 +020062 set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Juuso Oikarinen37b70a82009-10-08 21:56:21 +030063
64out:
65 mutex_unlock(&wl->mutex);
66}
67
68#define ELP_ENTRY_DELAY 5
69
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030070/* Routines to toggle sleep mode while in ELP */
71void wl1271_ps_elp_sleep(struct wl1271 *wl)
72{
Eliad Pellerc29bb002011-10-10 10:13:03 +020073 struct wl12xx_vif *wlvif;
74
Eliad Pellera665d6e2011-04-03 02:01:59 +030075 /* we shouldn't get consecutive sleep requests */
76 if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
77 return;
78
Eliad Pellerc29bb002011-10-10 10:13:03 +020079 wl12xx_for_each_wlvif(wl, wlvif) {
80 if (!test_bit(WLVIF_FLAG_PSM, &wlvif->flags) &&
81 !test_bit(WL1271_FLAG_IDLE, &wl->flags))
82 return;
83 }
Eliad Pellera665d6e2011-04-03 02:01:59 +030084
85 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
86 msecs_to_jiffies(ELP_ENTRY_DELAY));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030087}
88
Ido Yariva6208652011-03-01 15:14:41 +020089int wl1271_ps_elp_wakeup(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030090{
91 DECLARE_COMPLETION_ONSTACK(compl);
92 unsigned long flags;
93 int ret;
94 u32 start_time = jiffies;
95 bool pending = false;
96
Eliad Pellera665d6e2011-04-03 02:01:59 +030097 /*
98 * we might try to wake up even if we didn't go to sleep
99 * before (e.g. on boot)
100 */
101 if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
102 return 0;
103
104 /* don't cancel_sync as it might contend for a mutex and deadlock */
105 cancel_delayed_work(&wl->elp_work);
106
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200107 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300108 return 0;
109
110 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
111
112 /*
113 * The spinlock is required here to synchronize both the work and
114 * the completion variable in one entity.
115 */
116 spin_lock_irqsave(&wl->wl_lock, flags);
Ido Yariva6208652011-03-01 15:14:41 +0200117 if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300118 pending = true;
119 else
120 wl->elp_compl = &compl;
121 spin_unlock_irqrestore(&wl->wl_lock, flags);
122
Juuso Oikarinen74621412009-10-12 15:08:54 +0300123 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300124
125 if (!pending) {
126 ret = wait_for_completion_timeout(
127 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
128 if (ret == 0) {
129 wl1271_error("ELP wakeup timeout!");
Ido Yarivbaacb9a2011-06-06 14:57:05 +0300130 wl12xx_queue_recovery_work(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300131 ret = -ETIMEDOUT;
132 goto err;
133 } else if (ret < 0) {
134 wl1271_error("ELP wakeup completion error.");
135 goto err;
136 }
137 }
138
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200139 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300140
141 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
142 jiffies_to_msecs(jiffies - start_time));
143 goto out;
144
145err:
146 spin_lock_irqsave(&wl->wl_lock, flags);
147 wl->elp_compl = NULL;
148 spin_unlock_irqrestore(&wl->wl_lock, flags);
149 return ret;
150
151out:
152 return 0;
153}
154
Eliad Peller0603d892011-10-05 11:55:51 +0200155int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
156 enum wl1271_cmd_ps_mode mode, u32 rates, bool send)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300157{
158 int ret;
159
160 switch (mode) {
161 case STATION_POWER_SAVE_MODE:
162 wl1271_debug(DEBUG_PSM, "entering psm");
Juuso Oikarinen19221672009-10-08 21:56:35 +0300163
Eliad Peller0603d892011-10-05 11:55:51 +0200164 ret = wl1271_acx_wake_up_conditions(wl, wlvif);
Juuso Oikarinen03f06b72010-08-10 06:38:36 +0200165 if (ret < 0) {
166 wl1271_error("couldn't set wake up conditions");
167 return ret;
168 }
169
Eliad Peller0603d892011-10-05 11:55:51 +0200170 ret = wl1271_cmd_ps_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300171 if (ret < 0)
172 return ret;
173
Eliad Pellerc29bb002011-10-10 10:13:03 +0200174 set_bit(WLVIF_FLAG_PSM, &wlvif->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300175 break;
176 case STATION_ACTIVE_MODE:
177 default:
178 wl1271_debug(DEBUG_PSM, "leaving psm");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300179
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300180 /* disable beacon early termination */
Shahar Levi0e44eb22011-05-16 15:35:30 +0300181 if (wl->band == IEEE80211_BAND_2GHZ) {
Eliad Peller0603d892011-10-05 11:55:51 +0200182 ret = wl1271_acx_bet_enable(wl, wlvif, false);
Shahar Levi0e44eb22011-05-16 15:35:30 +0300183 if (ret < 0)
184 return ret;
185 }
Juuso Oikarinen11f70f92009-10-13 12:47:46 +0300186
Juuso Oikarinen19221672009-10-08 21:56:35 +0300187 /* disable beacon filtering */
Eliad Peller0603d892011-10-05 11:55:51 +0200188 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
Juuso Oikarinen19221672009-10-08 21:56:35 +0300189 if (ret < 0)
190 return ret;
191
Eliad Peller0603d892011-10-05 11:55:51 +0200192 ret = wl1271_cmd_ps_mode(wl, wlvif, STATION_ACTIVE_MODE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300193 if (ret < 0)
194 return ret;
195
Eliad Pellerc29bb002011-10-10 10:13:03 +0200196 clear_bit(WLVIF_FLAG_PSM, &wlvif->flags);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300197 break;
198 }
199
200 return ret;
201}
Arik Nemtsovb622d992011-02-23 00:22:31 +0200202
203static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
204{
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300205 int i;
Arik Nemtsovb622d992011-02-23 00:22:31 +0200206 struct sk_buff *skb;
207 struct ieee80211_tx_info *info;
208 unsigned long flags;
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300209 int filtered[NUM_TX_QUEUES];
Arik Nemtsovb622d992011-02-23 00:22:31 +0200210
Arik Nemtsovf8e0af62011-08-25 12:43:12 +0300211 /* filter all frames currently in the low level queues for this hlid */
Arik Nemtsovb622d992011-02-23 00:22:31 +0200212 for (i = 0; i < NUM_TX_QUEUES; i++) {
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300213 filtered[i] = 0;
Arik Nemtsovb622d992011-02-23 00:22:31 +0200214 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
Arik Nemtsovf8e0af62011-08-25 12:43:12 +0300215 filtered[i]++;
216
217 if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
218 continue;
219
Arik Nemtsovb622d992011-02-23 00:22:31 +0200220 info = IEEE80211_SKB_CB(skb);
221 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
222 info->status.rates[0].idx = -1;
Eliad Pellerc27d3ac2011-06-07 10:40:39 +0300223 ieee80211_tx_status_ni(wl->hw, skb);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200224 }
225 }
226
227 spin_lock_irqsave(&wl->wl_lock, flags);
Arik Nemtsovf1a46382011-07-07 14:25:23 +0300228 for (i = 0; i < NUM_TX_QUEUES; i++)
229 wl->tx_queue_count[i] -= filtered[i];
Arik Nemtsovb622d992011-02-23 00:22:31 +0200230 spin_unlock_irqrestore(&wl->wl_lock, flags);
231
232 wl1271_handle_tx_low_watermark(wl);
233}
234
235void wl1271_ps_link_start(struct wl1271 *wl, u8 hlid, bool clean_queues)
236{
237 struct ieee80211_sta *sta;
238
239 if (test_bit(hlid, &wl->ap_ps_map))
240 return;
241
Arik Nemtsov9b17f1b2011-08-14 13:17:35 +0300242 wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
243 "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
Arik Nemtsovb622d992011-02-23 00:22:31 +0200244 clean_queues);
245
246 rcu_read_lock();
247 sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
248 if (!sta) {
249 wl1271_error("could not find sta %pM for starting ps",
250 wl->links[hlid].addr);
251 rcu_read_unlock();
252 return;
253 }
254
255 ieee80211_sta_ps_transition_ni(sta, true);
256 rcu_read_unlock();
257
258 /* do we want to filter all frames from this link's queues? */
259 if (clean_queues)
260 wl1271_ps_filter_frames(wl, hlid);
261
262 __set_bit(hlid, &wl->ap_ps_map);
263}
264
265void wl1271_ps_link_end(struct wl1271 *wl, u8 hlid)
266{
267 struct ieee80211_sta *sta;
268
269 if (!test_bit(hlid, &wl->ap_ps_map))
270 return;
271
272 wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
273
274 __clear_bit(hlid, &wl->ap_ps_map);
275
276 rcu_read_lock();
277 sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
278 if (!sta) {
279 wl1271_error("could not find sta %pM for ending ps",
280 wl->links[hlid].addr);
281 goto end;
282 }
283
284 ieee80211_sta_ps_transition_ni(sta, false);
285end:
286 rcu_read_unlock();
287}