xref: /openbmc/linux/drivers/net/wireless/ti/wlcore/ps.c (revision 8e8e69d6)
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 int wl1271_ps_set_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
30 		       enum wl1271_cmd_ps_mode mode)
31 {
32 	int ret;
33 	u16 timeout = wl->conf.conn.dynamic_ps_timeout;
34 
35 	switch (mode) {
36 	case STATION_AUTO_PS_MODE:
37 	case STATION_POWER_SAVE_MODE:
38 		wl1271_debug(DEBUG_PSM, "entering psm (mode=%d,timeout=%u)",
39 			     mode, timeout);
40 
41 		ret = wl1271_acx_wake_up_conditions(wl, wlvif,
42 					    wl->conf.conn.wake_up_event,
43 					    wl->conf.conn.listen_interval);
44 		if (ret < 0) {
45 			wl1271_error("couldn't set wake up conditions");
46 			return ret;
47 		}
48 
49 		ret = wl1271_cmd_ps_mode(wl, wlvif, mode, timeout);
50 		if (ret < 0)
51 			return ret;
52 
53 		set_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
54 
55 		/*
56 		 * enable beacon early termination.
57 		 * Not relevant for 5GHz and for high rates.
58 		 */
59 		if ((wlvif->band == NL80211_BAND_2GHZ) &&
60 		    (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
61 			ret = wl1271_acx_bet_enable(wl, wlvif, true);
62 			if (ret < 0)
63 				return ret;
64 		}
65 		break;
66 	case STATION_ACTIVE_MODE:
67 		wl1271_debug(DEBUG_PSM, "leaving psm");
68 
69 		/* disable beacon early termination */
70 		if ((wlvif->band == NL80211_BAND_2GHZ) &&
71 		    (wlvif->basic_rate < CONF_HW_BIT_RATE_9MBPS)) {
72 			ret = wl1271_acx_bet_enable(wl, wlvif, false);
73 			if (ret < 0)
74 				return ret;
75 		}
76 
77 		ret = wl1271_cmd_ps_mode(wl, wlvif, mode, 0);
78 		if (ret < 0)
79 			return ret;
80 
81 		clear_bit(WLVIF_FLAG_IN_PS, &wlvif->flags);
82 		break;
83 	default:
84 		wl1271_warning("trying to set ps to unsupported mode %d", mode);
85 		ret = -EINVAL;
86 	}
87 
88 	return ret;
89 }
90 
91 static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
92 {
93 	int i;
94 	struct sk_buff *skb;
95 	struct ieee80211_tx_info *info;
96 	unsigned long flags;
97 	int filtered[NUM_TX_QUEUES];
98 	struct wl1271_link *lnk = &wl->links[hlid];
99 
100 	/* filter all frames currently in the low level queues for this hlid */
101 	for (i = 0; i < NUM_TX_QUEUES; i++) {
102 		filtered[i] = 0;
103 		while ((skb = skb_dequeue(&lnk->tx_queue[i]))) {
104 			filtered[i]++;
105 
106 			if (WARN_ON(wl12xx_is_dummy_packet(wl, skb)))
107 				continue;
108 
109 			info = IEEE80211_SKB_CB(skb);
110 			info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
111 			info->status.rates[0].idx = -1;
112 			ieee80211_tx_status_ni(wl->hw, skb);
113 		}
114 	}
115 
116 	spin_lock_irqsave(&wl->wl_lock, flags);
117 	for (i = 0; i < NUM_TX_QUEUES; i++) {
118 		wl->tx_queue_count[i] -= filtered[i];
119 		if (lnk->wlvif)
120 			lnk->wlvif->tx_queue_count[i] -= filtered[i];
121 	}
122 	spin_unlock_irqrestore(&wl->wl_lock, flags);
123 
124 	wl1271_handle_tx_low_watermark(wl);
125 }
126 
127 void wl12xx_ps_link_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
128 			  u8 hlid, bool clean_queues)
129 {
130 	struct ieee80211_sta *sta;
131 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
132 
133 	if (WARN_ON_ONCE(wlvif->bss_type != BSS_TYPE_AP_BSS))
134 		return;
135 
136 	if (!test_bit(hlid, wlvif->ap.sta_hlid_map) ||
137 	    test_bit(hlid, &wl->ap_ps_map))
138 		return;
139 
140 	wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d pkts %d "
141 		     "clean_queues %d", hlid, wl->links[hlid].allocated_pkts,
142 		     clean_queues);
143 
144 	rcu_read_lock();
145 	sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
146 	if (!sta) {
147 		wl1271_error("could not find sta %pM for starting ps",
148 			     wl->links[hlid].addr);
149 		rcu_read_unlock();
150 		return;
151 	}
152 
153 	ieee80211_sta_ps_transition_ni(sta, true);
154 	rcu_read_unlock();
155 
156 	/* do we want to filter all frames from this link's queues? */
157 	if (clean_queues)
158 		wl1271_ps_filter_frames(wl, hlid);
159 
160 	__set_bit(hlid, &wl->ap_ps_map);
161 }
162 
163 void wl12xx_ps_link_end(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
164 {
165 	struct ieee80211_sta *sta;
166 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
167 
168 	if (!test_bit(hlid, &wl->ap_ps_map))
169 		return;
170 
171 	wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
172 
173 	__clear_bit(hlid, &wl->ap_ps_map);
174 
175 	rcu_read_lock();
176 	sta = ieee80211_find_sta(vif, wl->links[hlid].addr);
177 	if (!sta) {
178 		wl1271_error("could not find sta %pM for ending ps",
179 			     wl->links[hlid].addr);
180 		goto end;
181 	}
182 
183 	ieee80211_sta_ps_transition_ni(sta, false);
184 end:
185 	rcu_read_unlock();
186 }
187