ps.c 8.1 KB
Newer Older
L
Luciano Coelho 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * This file is part of wl1271
 *
 * Copyright (C) 2008-2009 Nokia Corporation
 *
 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

S
Shahar Levi 已提交
24 25
#include "ps.h"
#include "io.h"
26
#include "tx.h"
27
#include "debug.h"
L
Luciano Coelho 已提交
28 29 30

#define WL1271_WAKEUP_TIMEOUT 500

E
Eliad Peller 已提交
31
#define ELP_ENTRY_DELAY  30
32
#define ELP_ENTRY_DELAY_FORCE_PS  5
33

34
void wl1271_elp_work(struct work_struct *work)
L
Luciano Coelho 已提交
35
{
36 37
	struct delayed_work *dwork;
	struct wl1271 *wl;
38
	struct wl12xx_vif *wlvif;
39
	int ret;
40 41 42 43 44 45 46 47

	dwork = container_of(work, struct delayed_work, work);
	wl = container_of(dwork, struct wl1271, elp_work);

	wl1271_debug(DEBUG_PSM, "elp work");

	mutex_lock(&wl->mutex);

48
	if (unlikely(wl->state != WLCORE_STATE_ON))
49 50
		goto out;

51 52 53 54
	/* our work might have been already cancelled */
	if (unlikely(!test_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
		goto out;

55
	if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
56
		goto out;
L
Luciano Coelho 已提交
57

58
	wl12xx_for_each_wlvif(wl, wlvif) {
59 60 61
		if (wlvif->bss_type == BSS_TYPE_AP_BSS)
			goto out;

E
Eyal Shapira 已提交
62
		if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
63
		    test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
64 65 66
			goto out;
	}

67
	wl1271_debug(DEBUG_PSM, "chip to elp");
68 69 70 71 72 73
	ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_SLEEP);
	if (ret < 0) {
		wl12xx_queue_recovery_work(wl);
		goto out;
	}

74
	set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
75 76 77 78 79 80 81 82

out:
	mutex_unlock(&wl->mutex);
}

/* Routines to toggle sleep mode while in ELP */
void wl1271_ps_elp_sleep(struct wl1271 *wl)
{
83
	struct wl12xx_vif *wlvif;
84
	u32 timeout;
85

86 87 88 89
	/* We do not enter elp sleep in PLT mode */
	if (wl->plt)
		return;

90
	if (wl->sleep_auth != WL1271_PSM_ELP)
91 92
		return;

93 94 95 96
	/* we shouldn't get consecutive sleep requests */
	if (WARN_ON(test_and_set_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags)))
		return;

97
	wl12xx_for_each_wlvif(wl, wlvif) {
98 99 100
		if (wlvif->bss_type == BSS_TYPE_AP_BSS)
			return;

E
Eyal Shapira 已提交
101
		if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
102
		    test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
103 104
			return;
	}
105

106 107
	timeout = wl->conf.conn.forced_ps ?
			ELP_ENTRY_DELAY_FORCE_PS : ELP_ENTRY_DELAY;
108
	ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
109
				     msecs_to_jiffies(timeout));
L
Luciano Coelho 已提交
110 111
}

112
int wl1271_ps_elp_wakeup(struct wl1271 *wl)
L
Luciano Coelho 已提交
113 114 115 116
{
	DECLARE_COMPLETION_ONSTACK(compl);
	unsigned long flags;
	int ret;
117
	unsigned long start_time = jiffies;
L
Luciano Coelho 已提交
118 119
	bool pending = false;

120 121 122 123 124 125 126 127 128 129
	/*
	 * we might try to wake up even if we didn't go to sleep
	 * before (e.g. on boot)
	 */
	if (!test_and_clear_bit(WL1271_FLAG_ELP_REQUESTED, &wl->flags))
		return 0;

	/* don't cancel_sync as it might contend for a mutex and deadlock */
	cancel_delayed_work(&wl->elp_work);

130
	if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
L
Luciano Coelho 已提交
131 132 133 134 135 136 137 138 139
		return 0;

	wl1271_debug(DEBUG_PSM, "waking up chip from elp");

	/*
	 * The spinlock is required here to synchronize both the work and
	 * the completion variable in one entity.
	 */
	spin_lock_irqsave(&wl->wl_lock, flags);
140
	if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
L
Luciano Coelho 已提交
141 142 143 144 145
		pending = true;
	else
		wl->elp_compl = &compl;
	spin_unlock_irqrestore(&wl->wl_lock, flags);

146 147 148 149 150
	ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
	if (ret < 0) {
		wl12xx_queue_recovery_work(wl);
		goto err;
	}
L
Luciano Coelho 已提交
151 152 153 154 155 156

	if (!pending) {
		ret = wait_for_completion_timeout(
			&compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
		if (ret == 0) {
			wl1271_error("ELP wakeup timeout!");
157
			wl12xx_queue_recovery_work(wl);
L
Luciano Coelho 已提交
158 159 160 161 162
			ret = -ETIMEDOUT;
			goto err;
		}
	}

163
	clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
L
Luciano Coelho 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

	wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
		     jiffies_to_msecs(jiffies - start_time));
	goto out;

err:
	spin_lock_irqsave(&wl->wl_lock, flags);
	wl->elp_compl = NULL;
	spin_unlock_irqrestore(&wl->wl_lock, flags);
	return ret;

out:
	return 0;
}

E
Eliad Peller 已提交
179
int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
180
		       enum wl1271_cmd_ps_mode mode)
L
Luciano Coelho 已提交
181 182
{
	int ret;
183
	u16 timeout = wl->conf.conn.dynamic_ps_timeout;
L
Luciano Coelho 已提交
184 185

	switch (mode) {
186
	case STATION_AUTO_PS_MODE:
E
Eyal Shapira 已提交
187
	case STATION_POWER_SAVE_MODE:
188 189
		wl1271_debug(DEBUG_PSM, "entering psm (mode=%d,timeout=%u)",
			     mode, timeout);
190

191 192 193
		ret = wl1271_acx_wake_up_conditions(wl, wlvif,
					    wl->conf.conn.wake_up_event,
					    wl->conf.conn.listen_interval);
194 195 196 197 198
		if (ret < 0) {
			wl1271_error("couldn't set wake up conditions");
			return ret;
		}

199
		ret = wl1271_cmd_ps_mode(wl, wlvif, mode, timeout);
L
Luciano Coelho 已提交
200 201 202
		if (ret < 0)
			return ret;

E
Eyal Shapira 已提交
203
		set_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
204

205 206 207 208 209 210
		/*
		 * enable beacon early termination.
		 * Not relevant for 5GHz and for high rates.
		 */
		if ((wlvif->band == IEEE80211_BAND_2GHZ) &&
		    (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
211 212 213 214
			ret = wl1271_acx_bet_enable(wl, wlvif, true);
			if (ret < 0)
				return ret;
		}
L
Luciano Coelho 已提交
215 216 217 218
		break;
	case STATION_ACTIVE_MODE:
		wl1271_debug(DEBUG_PSM, "leaving psm");

219
		/* disable beacon early termination */
220 221
		if ((wlvif->band == IEEE80211_BAND_2GHZ) &&
		    (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
E
Eliad Peller 已提交
222
			ret = wl1271_acx_bet_enable(wl, wlvif, false);
223 224 225
			if (ret < 0)
				return ret;
		}
226

227
		ret = wl1271_cmd_ps_mode(wl, wlvif, mode, 0);
L
Luciano Coelho 已提交
228 229 230
		if (ret < 0)
			return ret;

E
Eyal Shapira 已提交
231
		clear_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
L
Luciano Coelho 已提交
232
		break;
233 234 235
	default:
		wl1271_warning("trying to set ps to unsupported mode %d", mode);
		ret = -EINVAL;
L
Luciano Coelho 已提交
236 237 238 239
	}

	return ret;
}
240 241 242

static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
{
243
	int i;
244 245 246
	struct sk_buff *skb;
	struct ieee80211_tx_info *info;
	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 255 256 257 258
			filtered[i]++;

			if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
				continue;

259 260 261
			info = IEEE80211_SKB_CB(skb);
			info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
			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 270 271
		if (lnk->wlvif)
			lnk->wlvif->tx_queue_count[i] -= filtered[i];
	}
272 273 274 275 276
	spin_unlock_irqrestore(&wl->wl_lock, flags);

	wl1271_handle_tx_low_watermark(wl);
}

277 278
void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
			  u8 hlid, bool clean_queues)
279 280
{
	struct ieee80211_sta *sta;
281
	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
282

283 284 285 286 287
	if (WARN_ON_ONCE(wlvif->bss_type != BSS_TYPE_AP_BSS))
		return;

	if (!test_bit(hlid, wlvif->ap.sta_hlid_map) ||
	    test_bit(hlid, &wl->ap_ps_map))
288 289
		return;

290 291
	wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
		     "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
292 293 294
		     clean_queues);

	rcu_read_lock();
295
	sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	if (!sta) {
		wl1271_error("could not find sta %pM for starting ps",
			     wl->links[hlid].addr);
		rcu_read_unlock();
		return;
	}

	ieee80211_sta_ps_transition_ni(sta, true);
	rcu_read_unlock();

	/* do we want to filter all frames from this link's queues? */
	if (clean_queues)
		wl1271_ps_filter_frames(wl, hlid);

	__set_bit(hlid, &wl->ap_ps_map);
}

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 319 320 321 322 323 324 325

	if (!test_bit(hlid, &wl->ap_ps_map))
		return;

	wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);

	__clear_bit(hlid, &wl->ap_ps_map);

	rcu_read_lock();
326
	sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
327 328 329 330 331 332 333 334 335 336
	if (!sta) {
		wl1271_error("could not find sta %pM for ending ps",
			     wl->links[hlid].addr);
		goto end;
	}

	ieee80211_sta_ps_transition_ni(sta, false);
end:
	rcu_read_unlock();
}