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 24 #include "ps.h" 25 #include "io.h" 26 #include "tx.h" 27 #include "debug.h" 28 29 #define WL1271_WAKEUP_TIMEOUT 500 30 31 #define ELP_ENTRY_DELAY 5 32 33 void wl1271_elp_work(struct work_struct *work) 34 { 35 struct delayed_work *dwork; 36 struct wl1271 *wl; 37 struct wl12xx_vif *wlvif; 38 int ret; 39 40 dwork = container_of(work, struct delayed_work, work); 41 wl = container_of(dwork, struct wl1271, elp_work); 42 43 wl1271_debug(DEBUG_PSM, "elp work"); 44 45 mutex_lock(&wl->mutex); 46 47 if (unlikely(wl->state == WL1271_STATE_OFF)) 48 goto out; 49 50 /* our work might have been already cancelled */ 51 if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))) 52 goto out; 53 54 if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags)) 55 goto out; 56 57 wl12xx_for_each_wlvif(wl, wlvif) { 58 if (wlvif->bss_type == BSS_TYPE_AP_BSS) 59 goto out; 60 61 if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) && 62 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) 63 goto out; 64 } 65 66 wl1271_debug(DEBUG_PSM, "chip to elp"); 67 ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_SLEEP); 68 if (ret < 0) { 69 wl12xx_queue_recovery_work(wl); 70 goto out; 71 } 72 73 set_bit(WL1271_FLAG_IN_ELP, &wl->flags); 74 75 out: 76 mutex_unlock(&wl->mutex); 77 } 78 79 /* Routines to toggle sleep mode while in ELP */ 80 void wl1271_ps_elp_sleep(struct wl1271 *wl) 81 { 82 struct wl12xx_vif *wlvif; 83 u32 timeout; 84 85 if (wl->sleep_auth != WL1271_PSM_ELP) 86 return; 87 88 /* we shouldn't get consecutive sleep requests */ 89 if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))) 90 return; 91 92 wl12xx_for_each_wlvif(wl, wlvif) { 93 if (wlvif->bss_type == BSS_TYPE_AP_BSS) 94 return; 95 96 if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) && 97 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) 98 return; 99 } 100 101 if (wl->conf.conn.forced_ps) 102 timeout = ELP_ENTRY_DELAY; 103 else 104 timeout = wl->conf.conn.dynamic_ps_timeout; 105 106 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, 107 msecs_to_jiffies(timeout)); 108 } 109 110 int wl1271_ps_elp_wakeup(struct wl1271 *wl) 111 { 112 DECLARE_COMPLETION_ONSTACK(compl); 113 unsigned long flags; 114 int ret; 115 u32 start_time = jiffies; 116 bool pending = false; 117 118 /* 119 * we might try to wake up even if we didn't go to sleep 120 * before (e.g. on boot) 121 */ 122 if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)) 123 return 0; 124 125 /* don't cancel_sync as it might contend for a mutex and deadlock */ 126 cancel_delayed_work(&wl->elp_work); 127 128 if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags)) 129 return 0; 130 131 wl1271_debug(DEBUG_PSM, "waking up chip from elp"); 132 133 /* 134 * The spinlock is required here to synchronize both the work and 135 * the completion variable in one entity. 136 */ 137 spin_lock_irqsave(&wl->wl_lock, flags); 138 if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags)) 139 pending = true; 140 else 141 wl->elp_compl = &compl; 142 spin_unlock_irqrestore(&wl->wl_lock, flags); 143 144 ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP); 145 if (ret < 0) { 146 wl12xx_queue_recovery_work(wl); 147 goto err; 148 } 149 150 if (!pending) { 151 ret = wait_for_completion_timeout( 152 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT)); 153 if (ret == 0) { 154 wl1271_error("ELP wakeup timeout!"); 155 wl12xx_queue_recovery_work(wl); 156 ret = -ETIMEDOUT; 157 goto err; 158 } else if (ret < 0) { 159 wl1271_error("ELP wakeup completion error."); 160 goto err; 161 } 162 } 163 164 clear_bit(WL1271_FLAG_IN_ELP, &wl->flags); 165 166 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms", 167 jiffies_to_msecs(jiffies - start_time)); 168 goto out; 169 170 err: 171 spin_lock_irqsave(&wl->wl_lock, flags); 172 wl->elp_compl = NULL; 173 spin_unlock_irqrestore(&wl->wl_lock, flags); 174 return ret; 175 176 out: 177 return 0; 178 } 179 180 int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif, 181 enum wl1271_cmd_ps_mode mode) 182 { 183 int ret; 184 u16 timeout = wl->conf.conn.dynamic_ps_timeout; 185 186 switch (mode) { 187 case STATION_AUTO_PS_MODE: 188 case STATION_POWER_SAVE_MODE: 189 wl1271_debug(DEBUG_PSM, "entering psm (mode=%d,timeout=%u)", 190 mode, timeout); 191 192 ret = wl1271_acx_wake_up_conditions(wl, wlvif, 193 wl->conf.conn.wake_up_event, 194 wl->conf.conn.listen_interval); 195 if (ret < 0) { 196 wl1271_error("couldn't set wake up conditions"); 197 return ret; 198 } 199 200 ret = wl1271_cmd_ps_mode(wl, wlvif, mode, timeout); 201 if (ret < 0) 202 return ret; 203 204 set_bit(WLVIF_FLAG_IN_PS, &wlvif->flags); 205 206 /* 207 * enable beacon early termination. 208 * Not relevant for 5GHz and for high rates. 209 */ 210 if ((wlvif->band == IEEE80211_BAND_2GHZ) && 211 (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) { 212 ret = wl1271_acx_bet_enable(wl, wlvif, true); 213 if (ret < 0) 214 return ret; 215 } 216 break; 217 case STATION_ACTIVE_MODE: 218 wl1271_debug(DEBUG_PSM, "leaving psm"); 219 220 /* disable beacon early termination */ 221 if ((wlvif->band == IEEE80211_BAND_2GHZ) && 222 (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) { 223 ret = wl1271_acx_bet_enable(wl, wlvif, false); 224 if (ret < 0) 225 return ret; 226 } 227 228 ret = wl1271_cmd_ps_mode(wl, wlvif, mode, 0); 229 if (ret < 0) 230 return ret; 231 232 clear_bit(WLVIF_FLAG_IN_PS, &wlvif->flags); 233 break; 234 default: 235 wl1271_warning("trying to set ps to unsupported mode %d", mode); 236 ret = -EINVAL; 237 } 238 239 return ret; 240 } 241 242 static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid) 243 { 244 int i; 245 struct sk_buff *skb; 246 struct ieee80211_tx_info *info; 247 unsigned long flags; 248 int filtered[NUM_TX_QUEUES]; 249 250 /* filter all frames currently in the low level queues for this hlid */ 251 for (i = 0; i < NUM_TX_QUEUES; i++) { 252 filtered[i] = 0; 253 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) { 254 filtered[i]++; 255 256 if (WARN_ON(wl12xx_is_dummy_packet(wl, skb))) 257 continue; 258 259 info = IEEE80211_SKB_CB(skb); 260 info->flags |= IEEE80211_TX_STAT_TX_FILTERED; 261 info->status.rates[0].idx = -1; 262 ieee80211_tx_status_ni(wl->hw, skb); 263 } 264 } 265 266 spin_lock_irqsave(&wl->wl_lock, flags); 267 for (i = 0; i < NUM_TX_QUEUES; i++) 268 wl->tx_queue_count[i] -= filtered[i]; 269 spin_unlock_irqrestore(&wl->wl_lock, flags); 270 271 wl1271_handle_tx_low_watermark(wl); 272 } 273 274 void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif, 275 u8 hlid, bool clean_queues) 276 { 277 struct ieee80211_sta *sta; 278 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 279 280 if (test_bit(hlid, &wl->ap_ps_map)) 281 return; 282 283 wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d " 284 "clean_queues %d", hlid, wl->links[hlid].allocated_pkts, 285 clean_queues); 286 287 rcu_read_lock(); 288 sta = ieee80211_find_sta(vif, wl->links[hlid].addr); 289 if (!sta) { 290 wl1271_error("could not find sta %pM for starting ps", 291 wl->links[hlid].addr); 292 rcu_read_unlock(); 293 return; 294 } 295 296 ieee80211_sta_ps_transition_ni(sta, true); 297 rcu_read_unlock(); 298 299 /* do we want to filter all frames from this link's queues? */ 300 if (clean_queues) 301 wl1271_ps_filter_frames(wl, hlid); 302 303 __set_bit(hlid, &wl->ap_ps_map); 304 } 305 306 void wl12xx_ps_link_end(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid) 307 { 308 struct ieee80211_sta *sta; 309 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 310 311 if (!test_bit(hlid, &wl->ap_ps_map)) 312 return; 313 314 wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid); 315 316 __clear_bit(hlid, &wl->ap_ps_map); 317 318 rcu_read_lock(); 319 sta = ieee80211_find_sta(vif, wl->links[hlid].addr); 320 if (!sta) { 321 wl1271_error("could not find sta %pM for ending ps", 322 wl->links[hlid].addr); 323 goto end; 324 } 325 326 ieee80211_sta_ps_transition_ni(sta, false); 327 end: 328 rcu_read_unlock(); 329 } 330