| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 1 | /* | 
|  | 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 Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 24 | #include "reg.h" | 
|  | 25 | #include "ps.h" | 
|  | 26 | #include "io.h" | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 27 | #include "tx.h" | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 28 |  | 
|  | 29 | #define WL1271_WAKEUP_TIMEOUT 500 | 
|  | 30 |  | 
| Juuso Oikarinen | 37b70a8 | 2009-10-08 21:56:21 +0300 | [diff] [blame] | 31 | void 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 Oikarinen | 8c7f4f3 | 2010-09-21 06:23:29 +0200 | [diff] [blame] | 43 | if (unlikely(wl->state == WL1271_STATE_OFF)) | 
|  | 44 | goto out; | 
|  | 45 |  | 
| Eliad Peller | a665d6e | 2011-04-03 02:01:59 +0300 | [diff] [blame] | 46 | /* our work might have been already cancelled */ | 
|  | 47 | if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))) | 
|  | 48 | goto out; | 
|  | 49 |  | 
| Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 50 | if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) || | 
| Juuso Oikarinen | e197281 | 2010-04-09 11:07:29 +0300 | [diff] [blame] | 51 | (!test_bit(WL1271_FLAG_PSM, &wl->flags) && | 
|  | 52 | !test_bit(WL1271_FLAG_IDLE, &wl->flags))) | 
| Juuso Oikarinen | 37b70a8 | 2009-10-08 21:56:21 +0300 | [diff] [blame] | 53 | goto out; | 
|  | 54 |  | 
|  | 55 | wl1271_debug(DEBUG_PSM, "chip to elp"); | 
| Juuso Oikarinen | 7462141 | 2009-10-12 15:08:54 +0300 | [diff] [blame] | 56 | wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP); | 
| Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 57 | set_bit(WL1271_FLAG_IN_ELP, &wl->flags); | 
| Juuso Oikarinen | 37b70a8 | 2009-10-08 21:56:21 +0300 | [diff] [blame] | 58 |  | 
|  | 59 | out: | 
|  | 60 | mutex_unlock(&wl->mutex); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | #define ELP_ENTRY_DELAY  5 | 
|  | 64 |  | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 65 | /* Routines to toggle sleep mode while in ELP */ | 
|  | 66 | void wl1271_ps_elp_sleep(struct wl1271 *wl) | 
|  | 67 | { | 
| Eliad Peller | a665d6e | 2011-04-03 02:01:59 +0300 | [diff] [blame] | 68 | /* we shouldn't get consecutive sleep requests */ | 
|  | 69 | if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))) | 
|  | 70 | return; | 
|  | 71 |  | 
|  | 72 | if (!test_bit(WL1271_FLAG_PSM, &wl->flags) && | 
|  | 73 | !test_bit(WL1271_FLAG_IDLE, &wl->flags)) | 
|  | 74 | return; | 
|  | 75 |  | 
|  | 76 | ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, | 
|  | 77 | msecs_to_jiffies(ELP_ENTRY_DELAY)); | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
| Ido Yariv | a620865 | 2011-03-01 15:14:41 +0200 | [diff] [blame] | 80 | int wl1271_ps_elp_wakeup(struct wl1271 *wl) | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 81 | { | 
|  | 82 | DECLARE_COMPLETION_ONSTACK(compl); | 
|  | 83 | unsigned long flags; | 
|  | 84 | int ret; | 
|  | 85 | u32 start_time = jiffies; | 
|  | 86 | bool pending = false; | 
|  | 87 |  | 
| Eliad Peller | a665d6e | 2011-04-03 02:01:59 +0300 | [diff] [blame] | 88 | /* | 
|  | 89 | * we might try to wake up even if we didn't go to sleep | 
|  | 90 | * before (e.g. on boot) | 
|  | 91 | */ | 
|  | 92 | if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)) | 
|  | 93 | return 0; | 
|  | 94 |  | 
|  | 95 | /* don't cancel_sync as it might contend for a mutex and deadlock */ | 
|  | 96 | cancel_delayed_work(&wl->elp_work); | 
|  | 97 |  | 
| Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 98 | if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags)) | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 99 | return 0; | 
|  | 100 |  | 
|  | 101 | wl1271_debug(DEBUG_PSM, "waking up chip from elp"); | 
|  | 102 |  | 
|  | 103 | /* | 
|  | 104 | * The spinlock is required here to synchronize both the work and | 
|  | 105 | * the completion variable in one entity. | 
|  | 106 | */ | 
|  | 107 | spin_lock_irqsave(&wl->wl_lock, flags); | 
| Ido Yariv | a620865 | 2011-03-01 15:14:41 +0200 | [diff] [blame] | 108 | if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags)) | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 109 | pending = true; | 
|  | 110 | else | 
|  | 111 | wl->elp_compl = &compl; | 
|  | 112 | spin_unlock_irqrestore(&wl->wl_lock, flags); | 
|  | 113 |  | 
| Juuso Oikarinen | 7462141 | 2009-10-12 15:08:54 +0300 | [diff] [blame] | 114 | wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP); | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 115 |  | 
|  | 116 | if (!pending) { | 
|  | 117 | ret = wait_for_completion_timeout( | 
|  | 118 | &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT)); | 
|  | 119 | if (ret == 0) { | 
|  | 120 | wl1271_error("ELP wakeup timeout!"); | 
| Ido Yariv | baacb9a | 2011-06-06 14:57:05 +0300 | [diff] [blame] | 121 | wl12xx_queue_recovery_work(wl); | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 122 | ret = -ETIMEDOUT; | 
|  | 123 | goto err; | 
|  | 124 | } else if (ret < 0) { | 
|  | 125 | wl1271_error("ELP wakeup completion error."); | 
|  | 126 | goto err; | 
|  | 127 | } | 
|  | 128 | } | 
|  | 129 |  | 
| Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 130 | clear_bit(WL1271_FLAG_IN_ELP, &wl->flags); | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 131 |  | 
|  | 132 | wl1271_debug(DEBUG_PSM, "wakeup time: %u ms", | 
|  | 133 | jiffies_to_msecs(jiffies - start_time)); | 
|  | 134 | goto out; | 
|  | 135 |  | 
|  | 136 | err: | 
|  | 137 | spin_lock_irqsave(&wl->wl_lock, flags); | 
|  | 138 | wl->elp_compl = NULL; | 
|  | 139 | spin_unlock_irqrestore(&wl->wl_lock, flags); | 
|  | 140 | return ret; | 
|  | 141 |  | 
|  | 142 | out: | 
|  | 143 | return 0; | 
|  | 144 | } | 
|  | 145 |  | 
| Juuso Oikarinen | d8c42c0 | 2010-02-18 13:25:36 +0200 | [diff] [blame] | 146 | int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode, | 
| Juuso Oikarinen | 65cddbf | 2010-08-24 06:28:03 +0300 | [diff] [blame] | 147 | u32 rates, bool send) | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 148 | { | 
|  | 149 | int ret; | 
|  | 150 |  | 
|  | 151 | switch (mode) { | 
|  | 152 | case STATION_POWER_SAVE_MODE: | 
|  | 153 | wl1271_debug(DEBUG_PSM, "entering psm"); | 
| Juuso Oikarinen | 1922167 | 2009-10-08 21:56:35 +0300 | [diff] [blame] | 154 |  | 
| Juuso Oikarinen | 03f06b7 | 2010-08-10 06:38:36 +0200 | [diff] [blame] | 155 | ret = wl1271_acx_wake_up_conditions(wl); | 
|  | 156 | if (ret < 0) { | 
|  | 157 | wl1271_error("couldn't set wake up conditions"); | 
|  | 158 | return ret; | 
|  | 159 | } | 
|  | 160 |  | 
| Eliad Peller | c8bde24 | 2011-02-02 09:59:35 +0200 | [diff] [blame] | 161 | ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE); | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 162 | if (ret < 0) | 
|  | 163 | return ret; | 
|  | 164 |  | 
| Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 165 | set_bit(WL1271_FLAG_PSM, &wl->flags); | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 166 | break; | 
|  | 167 | case STATION_ACTIVE_MODE: | 
|  | 168 | default: | 
|  | 169 | wl1271_debug(DEBUG_PSM, "leaving psm"); | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 170 |  | 
| Juuso Oikarinen | 11f70f9 | 2009-10-13 12:47:46 +0300 | [diff] [blame] | 171 | /* disable beacon early termination */ | 
| Shahar Levi | 0e44eb2 | 2011-05-16 15:35:30 +0300 | [diff] [blame] | 172 | if (wl->band == IEEE80211_BAND_2GHZ) { | 
|  | 173 | ret = wl1271_acx_bet_enable(wl, false); | 
|  | 174 | if (ret < 0) | 
|  | 175 | return ret; | 
|  | 176 | } | 
| Juuso Oikarinen | 11f70f9 | 2009-10-13 12:47:46 +0300 | [diff] [blame] | 177 |  | 
| Juuso Oikarinen | 1922167 | 2009-10-08 21:56:35 +0300 | [diff] [blame] | 178 | /* disable beacon filtering */ | 
|  | 179 | ret = wl1271_acx_beacon_filter_opt(wl, false); | 
|  | 180 | if (ret < 0) | 
|  | 181 | return ret; | 
|  | 182 |  | 
| Eliad Peller | c8bde24 | 2011-02-02 09:59:35 +0200 | [diff] [blame] | 183 | ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE); | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 184 | if (ret < 0) | 
|  | 185 | return ret; | 
|  | 186 |  | 
| Juuso Oikarinen | 71449f8 | 2009-12-11 15:41:07 +0200 | [diff] [blame] | 187 | clear_bit(WL1271_FLAG_PSM, &wl->flags); | 
| Luciano Coelho | f5fc0f8 | 2009-08-06 16:25:28 +0300 | [diff] [blame] | 188 | break; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | return ret; | 
|  | 192 | } | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 193 |  | 
|  | 194 | static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid) | 
|  | 195 | { | 
| Arik Nemtsov | f1a4638 | 2011-07-07 14:25:23 +0300 | [diff] [blame] | 196 | int i; | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 197 | struct sk_buff *skb; | 
|  | 198 | struct ieee80211_tx_info *info; | 
|  | 199 | unsigned long flags; | 
| Arik Nemtsov | f1a4638 | 2011-07-07 14:25:23 +0300 | [diff] [blame] | 200 | int filtered[NUM_TX_QUEUES]; | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 201 |  | 
| Arik Nemtsov | f8e0af6 | 2011-08-25 12:43:12 +0300 | [diff] [blame] | 202 | /* filter all frames currently in the low level queues for this hlid */ | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 203 | for (i = 0; i < NUM_TX_QUEUES; i++) { | 
| Arik Nemtsov | f1a4638 | 2011-07-07 14:25:23 +0300 | [diff] [blame] | 204 | filtered[i] = 0; | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 205 | while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) { | 
| Arik Nemtsov | f8e0af6 | 2011-08-25 12:43:12 +0300 | [diff] [blame] | 206 | filtered[i]++; | 
|  | 207 |  | 
|  | 208 | if (WARN_ON(wl12xx_is_dummy_packet(wl, skb))) | 
|  | 209 | continue; | 
|  | 210 |  | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 211 | info = IEEE80211_SKB_CB(skb); | 
|  | 212 | info->flags |= IEEE80211_TX_STAT_TX_FILTERED; | 
|  | 213 | info->status.rates[0].idx = -1; | 
| Eliad Peller | c27d3ac | 2011-06-07 10:40:39 +0300 | [diff] [blame] | 214 | ieee80211_tx_status_ni(wl->hw, skb); | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 215 | } | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | spin_lock_irqsave(&wl->wl_lock, flags); | 
| Arik Nemtsov | f1a4638 | 2011-07-07 14:25:23 +0300 | [diff] [blame] | 219 | for (i = 0; i < NUM_TX_QUEUES; i++) | 
|  | 220 | wl->tx_queue_count[i] -= filtered[i]; | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 221 | spin_unlock_irqrestore(&wl->wl_lock, flags); | 
|  | 222 |  | 
|  | 223 | wl1271_handle_tx_low_watermark(wl); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | void wl1271_ps_link_start(struct wl1271 *wl, u8 hlid, bool clean_queues) | 
|  | 227 | { | 
|  | 228 | struct ieee80211_sta *sta; | 
|  | 229 |  | 
|  | 230 | if (test_bit(hlid, &wl->ap_ps_map)) | 
|  | 231 | return; | 
|  | 232 |  | 
| Arik Nemtsov | 9b17f1b | 2011-08-14 13:17:35 +0300 | [diff] [blame] | 233 | wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d " | 
|  | 234 | "clean_queues %d", hlid, wl->links[hlid].allocated_pkts, | 
| Arik Nemtsov | b622d99 | 2011-02-23 00:22:31 +0200 | [diff] [blame] | 235 | clean_queues); | 
|  | 236 |  | 
|  | 237 | rcu_read_lock(); | 
|  | 238 | sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr); | 
|  | 239 | if (!sta) { | 
|  | 240 | wl1271_error("could not find sta %pM for starting ps", | 
|  | 241 | wl->links[hlid].addr); | 
|  | 242 | rcu_read_unlock(); | 
|  | 243 | return; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | ieee80211_sta_ps_transition_ni(sta, true); | 
|  | 247 | rcu_read_unlock(); | 
|  | 248 |  | 
|  | 249 | /* do we want to filter all frames from this link's queues? */ | 
|  | 250 | if (clean_queues) | 
|  | 251 | wl1271_ps_filter_frames(wl, hlid); | 
|  | 252 |  | 
|  | 253 | __set_bit(hlid, &wl->ap_ps_map); | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | void wl1271_ps_link_end(struct wl1271 *wl, u8 hlid) | 
|  | 257 | { | 
|  | 258 | struct ieee80211_sta *sta; | 
|  | 259 |  | 
|  | 260 | if (!test_bit(hlid, &wl->ap_ps_map)) | 
|  | 261 | return; | 
|  | 262 |  | 
|  | 263 | wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid); | 
|  | 264 |  | 
|  | 265 | __clear_bit(hlid, &wl->ap_ps_map); | 
|  | 266 |  | 
|  | 267 | rcu_read_lock(); | 
|  | 268 | sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr); | 
|  | 269 | if (!sta) { | 
|  | 270 | wl1271_error("could not find sta %pM for ending ps", | 
|  | 271 | wl->links[hlid].addr); | 
|  | 272 | goto end; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | ieee80211_sta_ps_transition_ni(sta, false); | 
|  | 276 | end: | 
|  | 277 | rcu_read_unlock(); | 
|  | 278 | } |