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