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