2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
29 #define WL1271_WAKEUP_TIMEOUT 500
31 #define ELP_ENTRY_DELAY 30
33 void wl1271_elp_work(struct work_struct *work)
35 struct delayed_work *dwork;
37 struct wl12xx_vif *wlvif;
40 dwork = container_of(work, struct delayed_work, work);
41 wl = container_of(dwork, struct wl1271, elp_work);
43 wl1271_debug(DEBUG_PSM, "elp work");
45 mutex_lock(&wl->mutex);
47 if (unlikely(wl->state != WLCORE_STATE_ON))
50 /* our work might have been already cancelled */
51 if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
54 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
57 wl12xx_for_each_wlvif(wl, wlvif) {
58 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
61 if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
62 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
66 wl1271_debug(DEBUG_PSM, "chip to elp");
67 ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_SLEEP);
69 wl12xx_queue_recovery_work(wl);
73 set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
76 mutex_unlock(&wl->mutex);
79 /* Routines to toggle sleep mode while in ELP */
80 void wl1271_ps_elp_sleep(struct wl1271 *wl)
82 struct wl12xx_vif *wlvif;
85 if (wl->sleep_auth != WL1271_PSM_ELP)
88 /* we shouldn't get consecutive sleep requests */
89 if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
92 wl12xx_for_each_wlvif(wl, wlvif) {
93 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
96 if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
97 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
101 timeout = ELP_ENTRY_DELAY;
102 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
103 msecs_to_jiffies(timeout));
106 int wl1271_ps_elp_wakeup(struct wl1271 *wl)
108 DECLARE_COMPLETION_ONSTACK(compl);
111 u32 start_time = jiffies;
112 bool pending = false;
115 * we might try to wake up even if we didn't go to sleep
116 * before (e.g. on boot)
118 if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
121 /* don't cancel_sync as it might contend for a mutex and deadlock */
122 cancel_delayed_work(&wl->elp_work);
124 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
127 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
130 * The spinlock is required here to synchronize both the work and
131 * the completion variable in one entity.
133 spin_lock_irqsave(&wl->wl_lock, flags);
134 if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
137 wl->elp_compl = &compl;
138 spin_unlock_irqrestore(&wl->wl_lock, flags);
140 ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
142 wl12xx_queue_recovery_work(wl);
147 ret = wait_for_completion_timeout(
148 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
150 wl1271_error("ELP wakeup timeout!");
151 wl12xx_queue_recovery_work(wl);
157 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
159 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
160 jiffies_to_msecs(jiffies - start_time));
164 spin_lock_irqsave(&wl->wl_lock, flags);
165 wl->elp_compl = NULL;
166 spin_unlock_irqrestore(&wl->wl_lock, flags);
173 int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
174 enum wl1271_cmd_ps_mode mode)
177 u16 timeout = wl->conf.conn.dynamic_ps_timeout;
180 case STATION_AUTO_PS_MODE:
181 case STATION_POWER_SAVE_MODE:
182 wl1271_debug(DEBUG_PSM, "entering psm (mode=%d,timeout=%u)",
185 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
186 wl->conf.conn.wake_up_event,
187 wl->conf.conn.listen_interval);
189 wl1271_error("couldn't set wake up conditions");
193 ret = wl1271_cmd_ps_mode(wl, wlvif, mode, timeout);
197 set_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
200 * enable beacon early termination.
201 * Not relevant for 5GHz and for high rates.
203 if ((wlvif->band == IEEE80211_BAND_2GHZ) &&
204 (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
205 ret = wl1271_acx_bet_enable(wl, wlvif, true);
210 case STATION_ACTIVE_MODE:
211 wl1271_debug(DEBUG_PSM, "leaving psm");
213 /* disable beacon early termination */
214 if ((wlvif->band == IEEE80211_BAND_2GHZ) &&
215 (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
216 ret = wl1271_acx_bet_enable(wl, wlvif, false);
221 ret = wl1271_cmd_ps_mode(wl, wlvif, mode, 0);
225 clear_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
228 wl1271_warning("trying to set ps to unsupported mode %d", mode);
235 static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
239 struct ieee80211_tx_info *info;
241 int filtered[NUM_TX_QUEUES];
242 struct wl1271_link *lnk = &wl->links[hlid];
244 /* filter all frames currently in the low level queues for this hlid */
245 for (i = 0; i < NUM_TX_QUEUES; i++) {
247 while ((skb = skb_dequeue(&lnk->tx_queue[i]))) {
250 if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
253 info = IEEE80211_SKB_CB(skb);
254 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
255 info->status.rates[0].idx = -1;
256 ieee80211_tx_status_ni(wl->hw, skb);
260 spin_lock_irqsave(&wl->wl_lock, flags);
261 for (i = 0; i < NUM_TX_QUEUES; i++) {
262 wl->tx_queue_count[i] -= filtered[i];
264 lnk->wlvif->tx_queue_count[i] -= filtered[i];
266 spin_unlock_irqrestore(&wl->wl_lock, flags);
268 wl1271_handle_tx_low_watermark(wl);
271 void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
272 u8 hlid, bool clean_queues)
274 struct ieee80211_sta *sta;
275 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
277 if (test_bit(hlid, &wl->ap_ps_map))
280 wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
281 "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
285 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
287 wl1271_error("could not find sta %pM for starting ps",
288 wl->links[hlid].addr);
293 ieee80211_sta_ps_transition_ni(sta, true);
296 /* do we want to filter all frames from this link's queues? */
298 wl1271_ps_filter_frames(wl, hlid);
300 __set_bit(hlid, &wl->ap_ps_map);
303 void wl12xx_ps_link_end(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
305 struct ieee80211_sta *sta;
306 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
308 if (!test_bit(hlid, &wl->ap_ps_map))
311 wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
313 __clear_bit(hlid, &wl->ap_ps_map);
316 sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
318 wl1271_error("could not find sta %pM for ending ps",
319 wl->links[hlid].addr);
323 ieee80211_sta_ps_transition_ni(sta, false);