blob: d7be3aec6fc38cdb2feb83335a150deef07200e6 [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
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
Shahar Levi00d20102010-11-08 11:20:10 +000024#include "wl12xx.h"
25#include "reg.h"
26#include "io.h"
27#include "event.h"
28#include "ps.h"
29#include "scan.h"
Juuso Oikarinen66497dc2009-10-08 21:56:30 +030030#include "wl12xx_80211.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030031
Juuso Oikarinen90494a92010-07-08 17:50:00 +030032void wl1271_pspoll_work(struct work_struct *work)
33{
34 struct delayed_work *dwork;
35 struct wl1271 *wl;
Eliad Pellerc1b193e2011-03-23 22:22:15 +020036 int ret;
Juuso Oikarinen90494a92010-07-08 17:50:00 +030037
38 dwork = container_of(work, struct delayed_work, work);
39 wl = container_of(dwork, struct wl1271, pspoll_work);
40
41 wl1271_debug(DEBUG_EVENT, "pspoll work");
42
43 mutex_lock(&wl->mutex);
44
Juuso Oikarinen8c7f4f32010-09-21 06:23:29 +020045 if (unlikely(wl->state == WL1271_STATE_OFF))
46 goto out;
47
Juuso Oikarinen90494a92010-07-08 17:50:00 +030048 if (!test_and_clear_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags))
49 goto out;
50
51 if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
52 goto out;
53
54 /*
55 * if we end up here, then we were in powersave when the pspoll
56 * delivery failure occurred, and no-one changed state since, so
57 * we should go back to powersave.
58 */
Eliad Pellerc1b193e2011-03-23 22:22:15 +020059 ret = wl1271_ps_elp_wakeup(wl);
60 if (ret < 0)
61 goto out;
62
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +030063 wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE, wl->basic_rate, true);
Juuso Oikarinen90494a92010-07-08 17:50:00 +030064
Eliad Pellerc1b193e2011-03-23 22:22:15 +020065 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen90494a92010-07-08 17:50:00 +030066out:
67 mutex_unlock(&wl->mutex);
68};
69
70static void wl1271_event_pspoll_delivery_fail(struct wl1271 *wl)
71{
72 int delay = wl->conf.conn.ps_poll_recovery_period;
73 int ret;
74
75 wl->ps_poll_failures++;
76 if (wl->ps_poll_failures == 1)
77 wl1271_info("AP with dysfunctional ps-poll, "
78 "trying to work around it.");
79
80 /* force active mode receive data from the AP */
81 if (test_bit(WL1271_FLAG_PSM, &wl->flags)) {
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +030082 ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE,
83 wl->basic_rate, true);
Juuso Oikarinen90494a92010-07-08 17:50:00 +030084 if (ret < 0)
85 return;
86 set_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags);
87 ieee80211_queue_delayed_work(wl->hw, &wl->pspoll_work,
88 msecs_to_jiffies(delay));
89 }
90
91 /*
92 * If already in active mode, lets we should be getting data from
93 * the AP right away. If we enter PSM too fast after this, and data
94 * remains on the AP, we will get another event like this, and we'll
95 * go into active once more.
96 */
97}
98
Juuso Oikarinen19ad0712009-11-02 20:22:11 +020099static int wl1271_event_ps_report(struct wl1271 *wl,
100 struct event_mailbox *mbox,
101 bool *beacon_loss)
102{
103 int ret = 0;
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +0300104 u32 total_retries = wl->conf.conn.psm_entry_retries;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200105
106 wl1271_debug(DEBUG_EVENT, "ps_status: 0x%x", mbox->ps_status);
107
108 switch (mbox->ps_status) {
109 case EVENT_ENTER_POWER_SAVE_FAIL:
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200110 wl1271_debug(DEBUG_PSM, "PSM entry failed");
111
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200112 if (!test_bit(WL1271_FLAG_PSM, &wl->flags)) {
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200113 /* remain in active mode */
Juuso Oikarinen461fa132009-11-23 23:22:13 +0200114 wl->psm_entry_retry = 0;
115 break;
116 }
117
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +0300118 if (wl->psm_entry_retry < total_retries) {
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200119 wl->psm_entry_retry++;
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200120 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE,
Juuso Oikarinen8eab7b42010-09-24 03:10:11 +0200121 wl->basic_rate, true);
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200122 } else {
Juuso Oikarinen1a186a52010-04-01 11:38:23 +0300123 wl1271_info("No ack to nullfunc from AP.");
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200124 wl->psm_entry_retry = 0;
Juuso Oikarinend60772f2010-03-26 12:53:29 +0200125 *beacon_loss = true;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200126 }
127 break;
128 case EVENT_ENTER_POWER_SAVE_SUCCESS:
129 wl->psm_entry_retry = 0;
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200130
131 /* enable beacon filtering */
132 ret = wl1271_acx_beacon_filter_opt(wl, true);
133 if (ret < 0)
134 break;
135
136 /* enable beacon early termination */
137 ret = wl1271_acx_bet_enable(wl, true);
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200138 break;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200139 default:
140 break;
141 }
142
143 return ret;
144}
145
Juuso Oikarinen00236aed2010-04-09 11:07:30 +0300146static void wl1271_event_rssi_trigger(struct wl1271 *wl,
147 struct event_mailbox *mbox)
148{
149 enum nl80211_cqm_rssi_threshold_event event;
150 s8 metric = mbox->rssi_snr_trigger_metric[0];
151
152 wl1271_debug(DEBUG_EVENT, "RSSI trigger metric: %d", metric);
153
154 if (metric <= wl->rssi_thold)
155 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
156 else
157 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
158
159 if (event != wl->last_rssi_event)
160 ieee80211_cqm_rssi_notify(wl->vif, event, GFP_KERNEL);
161 wl->last_rssi_event = event;
162}
163
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300164static void wl1271_event_mbox_dump(struct event_mailbox *mbox)
165{
166 wl1271_debug(DEBUG_EVENT, "MBOX DUMP:");
167 wl1271_debug(DEBUG_EVENT, "\tvector: 0x%x", mbox->events_vector);
168 wl1271_debug(DEBUG_EVENT, "\tmask: 0x%x", mbox->events_mask);
169}
170
171static int wl1271_event_process(struct wl1271 *wl, struct event_mailbox *mbox)
172{
173 int ret;
174 u32 vector;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200175 bool beacon_loss = false;
Arik Nemtsov203c9032010-10-25 11:17:44 +0200176 bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
Arik Nemtsov47684802011-04-26 23:21:51 +0300177 bool disconnect_sta = false;
178 unsigned long sta_bitmap = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300179
180 wl1271_event_mbox_dump(mbox);
181
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300182 vector = le32_to_cpu(mbox->events_vector);
183 vector &= ~(le32_to_cpu(mbox->events_mask));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300184 wl1271_debug(DEBUG_EVENT, "vector: 0x%x", vector);
185
186 if (vector & SCAN_COMPLETE_EVENT_ID) {
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300187 wl1271_debug(DEBUG_EVENT, "status: 0x%x",
188 mbox->scheduled_scan_status);
189
Luciano Coelho08688d62010-07-08 17:50:07 +0300190 wl1271_scan_stm(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300191 }
192
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300193 /* disable dynamic PS when requested by the firmware */
194 if (vector & SOFT_GEMINI_SENSE_EVENT_ID &&
195 wl->bss_type == BSS_TYPE_STA_BSS) {
196 if (mbox->soft_gemini_sense_info)
Juuso Oikarinenf532be62010-07-08 17:50:05 +0300197 ieee80211_disable_dyn_ps(wl->vif);
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300198 else
Juuso Oikarinenf532be62010-07-08 17:50:05 +0300199 ieee80211_enable_dyn_ps(wl->vif);
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300200 }
201
Juuso Oikarinenb771eee2009-10-08 21:56:34 +0300202 /*
203 * The BSS_LOSE_EVENT_ID is only needed while psm (and hence beacon
204 * filtering) is enabled. Without PSM, the stack will receive all
205 * beacons and can detect beacon loss by itself.
Teemu Paasikivi64e29e42010-03-26 12:53:26 +0200206 *
207 * As there's possibility that the driver disables PSM before receiving
208 * BSS_LOSE_EVENT, beacon loss has to be reported to the stack.
209 *
Juuso Oikarinenb771eee2009-10-08 21:56:34 +0300210 */
Arik Nemtsov203c9032010-10-25 11:17:44 +0200211 if ((vector & BSS_LOSE_EVENT_ID) && !is_ap) {
Juuso Oikarinen1a186a52010-04-01 11:38:23 +0300212 wl1271_info("Beacon loss detected.");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300213
Juuso Oikarinenb771eee2009-10-08 21:56:34 +0300214 /* indicate to the stack, that beacons have been lost */
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200215 beacon_loss = true;
216 }
217
Arik Nemtsov203c9032010-10-25 11:17:44 +0200218 if ((vector & PS_REPORT_EVENT_ID) && !is_ap) {
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200219 wl1271_debug(DEBUG_EVENT, "PS_REPORT_EVENT");
220 ret = wl1271_event_ps_report(wl, mbox, &beacon_loss);
221 if (ret < 0)
222 return ret;
223 }
224
Arik Nemtsov203c9032010-10-25 11:17:44 +0200225 if ((vector & PSPOLL_DELIVERY_FAILURE_EVENT_ID) && !is_ap)
Juuso Oikarinen90494a92010-07-08 17:50:00 +0300226 wl1271_event_pspoll_delivery_fail(wl);
227
Juuso Oikarinen00236aed2010-04-09 11:07:30 +0300228 if (vector & RSSI_SNR_TRIGGER_0_EVENT_ID) {
229 wl1271_debug(DEBUG_EVENT, "RSSI_SNR_TRIGGER_0_EVENT");
230 if (wl->vif)
231 wl1271_event_rssi_trigger(wl, mbox);
232 }
233
Shahar Leviae47c452011-03-06 16:32:14 +0200234 if ((vector & DUMMY_PACKET_EVENT_ID) && !is_ap) {
235 wl1271_debug(DEBUG_EVENT, "DUMMY_PACKET_ID_EVENT_ID");
236 if (wl->vif)
237 wl1271_tx_dummy_packet(wl);
238 }
239
Arik Nemtsov47684802011-04-26 23:21:51 +0300240 /*
241 * "TX retries exceeded" has a different meaning according to mode.
242 * In AP mode the offending station is disconnected. In STA mode we
243 * report connection loss.
244 */
245 if (vector & MAX_TX_RETRY_EVENT_ID) {
246 wl1271_debug(DEBUG_EVENT, "MAX_TX_RETRY_EVENT_ID");
247 if (is_ap) {
248 sta_bitmap |= le16_to_cpu(mbox->sta_tx_retry_exceeded);
249 disconnect_sta = true;
250 } else {
251 beacon_loss = true;
252 }
253 }
254
255 if ((vector & INACTIVE_STA_EVENT_ID) && is_ap) {
256 wl1271_debug(DEBUG_EVENT, "INACTIVE_STA_EVENT_ID");
257 sta_bitmap |= le16_to_cpu(mbox->sta_aging_status);
258 disconnect_sta = true;
259 }
260
Juuso Oikarinend60772f2010-03-26 12:53:29 +0200261 if (wl->vif && beacon_loss)
262 ieee80211_connection_loss(wl->vif);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300263
Arik Nemtsov47684802011-04-26 23:21:51 +0300264 if (is_ap && disconnect_sta) {
265 u32 num_packets = wl->conf.tx.max_tx_retries;
266 struct ieee80211_sta *sta;
267 const u8 *addr;
268 int h;
269
270 for (h = find_first_bit(&sta_bitmap, AP_MAX_LINKS);
271 h < AP_MAX_LINKS;
272 h = find_next_bit(&sta_bitmap, AP_MAX_LINKS, h+1)) {
273 if (!wl1271_is_active_sta(wl, h))
274 continue;
275
276 addr = wl->links[h].addr;
277
278 rcu_read_lock();
279 sta = ieee80211_find_sta(wl->vif, addr);
280 if (sta) {
281 wl1271_debug(DEBUG_EVENT, "remove sta %d", h);
282 ieee80211_report_low_ack(sta, num_packets);
283 }
284 rcu_read_unlock();
285 }
286 }
287
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300288 return 0;
289}
290
291int wl1271_event_unmask(struct wl1271 *wl)
292{
293 int ret;
294
295 ret = wl1271_acx_event_mbox_mask(wl, ~(wl->event_mask));
296 if (ret < 0)
297 return ret;
298
299 return 0;
300}
301
302void wl1271_event_mbox_config(struct wl1271 *wl)
303{
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200304 wl->mbox_ptr[0] = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300305 wl->mbox_ptr[1] = wl->mbox_ptr[0] + sizeof(struct event_mailbox);
306
307 wl1271_debug(DEBUG_EVENT, "MBOX ptrs: 0x%x 0x%x",
308 wl->mbox_ptr[0], wl->mbox_ptr[1]);
309}
310
Juuso Oikarinen13f2dc52009-12-11 15:40:59 +0200311int wl1271_event_handle(struct wl1271 *wl, u8 mbox_num)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300312{
313 struct event_mailbox mbox;
314 int ret;
315
316 wl1271_debug(DEBUG_EVENT, "EVENT on mbox %d", mbox_num);
317
318 if (mbox_num > 1)
319 return -EINVAL;
320
321 /* first we read the mbox descriptor */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200322 wl1271_read(wl, wl->mbox_ptr[mbox_num], &mbox,
323 sizeof(struct event_mailbox), false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300324
325 /* process the descriptor */
326 ret = wl1271_event_process(wl, &mbox);
327 if (ret < 0)
328 return ret;
329
330 /* then we let the firmware know it can go on...*/
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200331 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_EVENT_ACK);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300332
333 return 0;
334}