blob: e6c839af903830557ae3b63e77c2432124778cbb [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
24#include "wl1271.h"
25#include "wl1271_reg.h"
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020026#include "wl1271_io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030027#include "wl1271_event.h"
28#include "wl1271_ps.h"
Luciano Coelho34dd2aa2010-07-08 17:50:06 +030029#include "wl1271_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;
36
37 dwork = container_of(work, struct delayed_work, work);
38 wl = container_of(dwork, struct wl1271, pspoll_work);
39
40 wl1271_debug(DEBUG_EVENT, "pspoll work");
41
42 mutex_lock(&wl->mutex);
43
Juuso Oikarinen8c7f4f32010-09-21 06:23:29 +020044 if (unlikely(wl->state == WL1271_STATE_OFF))
45 goto out;
46
Juuso Oikarinen90494a92010-07-08 17:50:00 +030047 if (!test_and_clear_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags))
48 goto out;
49
50 if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
51 goto out;
52
53 /*
54 * if we end up here, then we were in powersave when the pspoll
55 * delivery failure occurred, and no-one changed state since, so
56 * we should go back to powersave.
57 */
Juuso Oikarinen65cddbf12010-08-24 06:28:03 +030058 wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE, wl->basic_rate, true);
Juuso Oikarinen90494a92010-07-08 17:50:00 +030059
60out:
61 mutex_unlock(&wl->mutex);
62};
63
64static void wl1271_event_pspoll_delivery_fail(struct wl1271 *wl)
65{
66 int delay = wl->conf.conn.ps_poll_recovery_period;
67 int ret;
68
69 wl->ps_poll_failures++;
70 if (wl->ps_poll_failures == 1)
71 wl1271_info("AP with dysfunctional ps-poll, "
72 "trying to work around it.");
73
74 /* force active mode receive data from the AP */
75 if (test_bit(WL1271_FLAG_PSM, &wl->flags)) {
Juuso Oikarinen65cddbf12010-08-24 06:28:03 +030076 ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE,
77 wl->basic_rate, true);
Juuso Oikarinen90494a92010-07-08 17:50:00 +030078 if (ret < 0)
79 return;
80 set_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags);
81 ieee80211_queue_delayed_work(wl->hw, &wl->pspoll_work,
82 msecs_to_jiffies(delay));
83 }
84
85 /*
86 * If already in active mode, lets we should be getting data from
87 * the AP right away. If we enter PSM too fast after this, and data
88 * remains on the AP, we will get another event like this, and we'll
89 * go into active once more.
90 */
91}
92
Juuso Oikarinen19ad0712009-11-02 20:22:11 +020093static int wl1271_event_ps_report(struct wl1271 *wl,
94 struct event_mailbox *mbox,
95 bool *beacon_loss)
96{
97 int ret = 0;
Juuso Oikarinen65cddbf12010-08-24 06:28:03 +030098 u32 total_retries = wl->conf.conn.psm_entry_retries;
99 u32 rates;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200100
101 wl1271_debug(DEBUG_EVENT, "ps_status: 0x%x", mbox->ps_status);
102
103 switch (mbox->ps_status) {
104 case EVENT_ENTER_POWER_SAVE_FAIL:
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200105 wl1271_debug(DEBUG_PSM, "PSM entry failed");
106
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200107 if (!test_bit(WL1271_FLAG_PSM, &wl->flags)) {
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200108 /* remain in active mode */
Juuso Oikarinen461fa132009-11-23 23:22:13 +0200109 wl->psm_entry_retry = 0;
110 break;
111 }
112
Juuso Oikarinen65cddbf12010-08-24 06:28:03 +0300113 if (wl->psm_entry_retry < total_retries) {
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200114 wl->psm_entry_retry++;
Juuso Oikarinen65cddbf12010-08-24 06:28:03 +0300115 if (wl->psm_entry_retry == total_retries)
116 rates = wl->basic_rate;
117 else
118 rates = wl->basic_rate_set;
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200119 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE,
Juuso Oikarinen65cddbf12010-08-24 06:28:03 +0300120 rates, true);
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200121 } else {
Juuso Oikarinen1a186a52010-04-01 11:38:23 +0300122 wl1271_info("No ack to nullfunc from AP.");
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200123 wl->psm_entry_retry = 0;
Juuso Oikarinend60772f2010-03-26 12:53:29 +0200124 *beacon_loss = true;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200125 }
126 break;
127 case EVENT_ENTER_POWER_SAVE_SUCCESS:
128 wl->psm_entry_retry = 0;
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200129
130 /* enable beacon filtering */
131 ret = wl1271_acx_beacon_filter_opt(wl, true);
132 if (ret < 0)
133 break;
134
135 /* enable beacon early termination */
136 ret = wl1271_acx_bet_enable(wl, true);
137 if (ret < 0)
138 break;
139
140 /* go to extremely low power mode */
141 wl1271_ps_elp_sleep(wl);
142 if (ret < 0)
143 break;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200144 break;
145 case EVENT_EXIT_POWER_SAVE_FAIL:
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200146 wl1271_debug(DEBUG_PSM, "PSM exit failed");
147
148 if (test_bit(WL1271_FLAG_PSM, &wl->flags)) {
149 wl->psm_entry_retry = 0;
150 break;
151 }
152
Juuso Oikarinen30240fc2010-02-18 13:25:38 +0200153 /* make sure the firmware goes to active mode - the frame to
154 be sent next will indicate to the AP, that we are active. */
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200155 ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE,
Juuso Oikarinen65cddbf12010-08-24 06:28:03 +0300156 wl->basic_rate, false);
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200157 break;
158 case EVENT_EXIT_POWER_SAVE_SUCCESS:
159 default:
160 break;
161 }
162
163 return ret;
164}
165
Juuso Oikarinen00236aed2010-04-09 11:07:30 +0300166static void wl1271_event_rssi_trigger(struct wl1271 *wl,
167 struct event_mailbox *mbox)
168{
169 enum nl80211_cqm_rssi_threshold_event event;
170 s8 metric = mbox->rssi_snr_trigger_metric[0];
171
172 wl1271_debug(DEBUG_EVENT, "RSSI trigger metric: %d", metric);
173
174 if (metric <= wl->rssi_thold)
175 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
176 else
177 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
178
179 if (event != wl->last_rssi_event)
180 ieee80211_cqm_rssi_notify(wl->vif, event, GFP_KERNEL);
181 wl->last_rssi_event = event;
182}
183
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300184static void wl1271_event_mbox_dump(struct event_mailbox *mbox)
185{
186 wl1271_debug(DEBUG_EVENT, "MBOX DUMP:");
187 wl1271_debug(DEBUG_EVENT, "\tvector: 0x%x", mbox->events_vector);
188 wl1271_debug(DEBUG_EVENT, "\tmask: 0x%x", mbox->events_mask);
189}
190
191static int wl1271_event_process(struct wl1271 *wl, struct event_mailbox *mbox)
192{
193 int ret;
194 u32 vector;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200195 bool beacon_loss = false;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300196
197 wl1271_event_mbox_dump(mbox);
198
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300199 vector = le32_to_cpu(mbox->events_vector);
200 vector &= ~(le32_to_cpu(mbox->events_mask));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300201 wl1271_debug(DEBUG_EVENT, "vector: 0x%x", vector);
202
203 if (vector & SCAN_COMPLETE_EVENT_ID) {
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300204 wl1271_debug(DEBUG_EVENT, "status: 0x%x",
205 mbox->scheduled_scan_status);
206
Luciano Coelho08688d62010-07-08 17:50:07 +0300207 wl1271_scan_stm(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300208 }
209
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300210 /* disable dynamic PS when requested by the firmware */
211 if (vector & SOFT_GEMINI_SENSE_EVENT_ID &&
212 wl->bss_type == BSS_TYPE_STA_BSS) {
213 if (mbox->soft_gemini_sense_info)
Juuso Oikarinenf532be62010-07-08 17:50:05 +0300214 ieee80211_disable_dyn_ps(wl->vif);
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300215 else
Juuso Oikarinenf532be62010-07-08 17:50:05 +0300216 ieee80211_enable_dyn_ps(wl->vif);
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300217 }
218
Juuso Oikarinenb771eee2009-10-08 21:56:34 +0300219 /*
220 * The BSS_LOSE_EVENT_ID is only needed while psm (and hence beacon
221 * filtering) is enabled. Without PSM, the stack will receive all
222 * beacons and can detect beacon loss by itself.
Teemu Paasikivi64e29e42010-03-26 12:53:26 +0200223 *
224 * As there's possibility that the driver disables PSM before receiving
225 * BSS_LOSE_EVENT, beacon loss has to be reported to the stack.
226 *
Juuso Oikarinenb771eee2009-10-08 21:56:34 +0300227 */
Teemu Paasikivi64e29e42010-03-26 12:53:26 +0200228 if (vector & BSS_LOSE_EVENT_ID) {
Juuso Oikarinen1a186a52010-04-01 11:38:23 +0300229 wl1271_info("Beacon loss detected.");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300230
Juuso Oikarinenb771eee2009-10-08 21:56:34 +0300231 /* indicate to the stack, that beacons have been lost */
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200232 beacon_loss = true;
233 }
234
235 if (vector & PS_REPORT_EVENT_ID) {
236 wl1271_debug(DEBUG_EVENT, "PS_REPORT_EVENT");
237 ret = wl1271_event_ps_report(wl, mbox, &beacon_loss);
238 if (ret < 0)
239 return ret;
240 }
241
Juuso Oikarinen90494a92010-07-08 17:50:00 +0300242 if (vector & PSPOLL_DELIVERY_FAILURE_EVENT_ID)
243 wl1271_event_pspoll_delivery_fail(wl);
244
Juuso Oikarinen00236aed2010-04-09 11:07:30 +0300245 if (vector & RSSI_SNR_TRIGGER_0_EVENT_ID) {
246 wl1271_debug(DEBUG_EVENT, "RSSI_SNR_TRIGGER_0_EVENT");
247 if (wl->vif)
248 wl1271_event_rssi_trigger(wl, mbox);
249 }
250
Juuso Oikarinend60772f2010-03-26 12:53:29 +0200251 if (wl->vif && beacon_loss)
252 ieee80211_connection_loss(wl->vif);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300253
254 return 0;
255}
256
257int wl1271_event_unmask(struct wl1271 *wl)
258{
259 int ret;
260
261 ret = wl1271_acx_event_mbox_mask(wl, ~(wl->event_mask));
262 if (ret < 0)
263 return ret;
264
265 return 0;
266}
267
268void wl1271_event_mbox_config(struct wl1271 *wl)
269{
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200270 wl->mbox_ptr[0] = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300271 wl->mbox_ptr[1] = wl->mbox_ptr[0] + sizeof(struct event_mailbox);
272
273 wl1271_debug(DEBUG_EVENT, "MBOX ptrs: 0x%x 0x%x",
274 wl->mbox_ptr[0], wl->mbox_ptr[1]);
275}
276
Juuso Oikarinen13f2dc52009-12-11 15:40:59 +0200277int wl1271_event_handle(struct wl1271 *wl, u8 mbox_num)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300278{
279 struct event_mailbox mbox;
280 int ret;
281
282 wl1271_debug(DEBUG_EVENT, "EVENT on mbox %d", mbox_num);
283
284 if (mbox_num > 1)
285 return -EINVAL;
286
287 /* first we read the mbox descriptor */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200288 wl1271_read(wl, wl->mbox_ptr[mbox_num], &mbox,
289 sizeof(struct event_mailbox), false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300290
291 /* process the descriptor */
292 ret = wl1271_event_process(wl, &mbox);
293 if (ret < 0)
294 return ret;
295
296 /* then we let the firmware know it can go on...*/
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200297 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_EVENT_ACK);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300298
299 return 0;
300}