blob: 072addc0bffc034ab3519a8dfc7113f94c956d01 [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{
Eliad Pellerd2d66c52011-10-05 11:55:43 +020034 struct ieee80211_vif *vif;
35 struct wl12xx_vif *wlvif;
Juuso Oikarinen90494a92010-07-08 17:50:00 +030036 struct delayed_work *dwork;
37 struct wl1271 *wl;
Eliad Pellerc1b193e2011-03-23 22:22:15 +020038 int ret;
Juuso Oikarinen90494a92010-07-08 17:50:00 +030039
40 dwork = container_of(work, struct delayed_work, work);
41 wl = container_of(dwork, struct wl1271, pspoll_work);
Eliad Pellerd2d66c52011-10-05 11:55:43 +020042 vif = wl->vif; /* TODO: move work into vif struct */
43 wlvif = wl12xx_vif_to_data(vif);
Juuso Oikarinen90494a92010-07-08 17:50:00 +030044
45 wl1271_debug(DEBUG_EVENT, "pspoll work");
46
47 mutex_lock(&wl->mutex);
48
Juuso Oikarinen8c7f4f32010-09-21 06:23:29 +020049 if (unlikely(wl->state == WL1271_STATE_OFF))
50 goto out;
51
Juuso Oikarinen90494a92010-07-08 17:50:00 +030052 if (!test_and_clear_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags))
53 goto out;
54
55 if (!test_bit(WL1271_FLAG_STA_ASSOCIATED, &wl->flags))
56 goto out;
57
58 /*
59 * if we end up here, then we were in powersave when the pspoll
60 * delivery failure occurred, and no-one changed state since, so
61 * we should go back to powersave.
62 */
Eliad Pellerc1b193e2011-03-23 22:22:15 +020063 ret = wl1271_ps_elp_wakeup(wl);
64 if (ret < 0)
65 goto out;
66
Eliad Pellerd2d66c52011-10-05 11:55:43 +020067 wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE, wlvif->basic_rate,
68 true);
Juuso Oikarinen90494a92010-07-08 17:50:00 +030069
Eliad Pellerc1b193e2011-03-23 22:22:15 +020070 wl1271_ps_elp_sleep(wl);
Juuso Oikarinen90494a92010-07-08 17:50:00 +030071out:
72 mutex_unlock(&wl->mutex);
73};
74
Eliad Pellerd2d66c52011-10-05 11:55:43 +020075static void wl1271_event_pspoll_delivery_fail(struct wl1271 *wl,
76 struct wl12xx_vif *wlvif)
Juuso Oikarinen90494a92010-07-08 17:50:00 +030077{
78 int delay = wl->conf.conn.ps_poll_recovery_period;
79 int ret;
80
81 wl->ps_poll_failures++;
82 if (wl->ps_poll_failures == 1)
83 wl1271_info("AP with dysfunctional ps-poll, "
84 "trying to work around it.");
85
86 /* force active mode receive data from the AP */
87 if (test_bit(WL1271_FLAG_PSM, &wl->flags)) {
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +030088 ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE,
Eliad Pellerd2d66c52011-10-05 11:55:43 +020089 wlvif->basic_rate, true);
Juuso Oikarinen90494a92010-07-08 17:50:00 +030090 if (ret < 0)
91 return;
92 set_bit(WL1271_FLAG_PSPOLL_FAILURE, &wl->flags);
93 ieee80211_queue_delayed_work(wl->hw, &wl->pspoll_work,
94 msecs_to_jiffies(delay));
95 }
96
97 /*
98 * If already in active mode, lets we should be getting data from
99 * the AP right away. If we enter PSM too fast after this, and data
100 * remains on the AP, we will get another event like this, and we'll
101 * go into active once more.
102 */
103}
104
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200105static int wl1271_event_ps_report(struct wl1271 *wl,
Eliad Pellerd2d66c52011-10-05 11:55:43 +0200106 struct wl12xx_vif *wlvif,
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200107 struct event_mailbox *mbox,
108 bool *beacon_loss)
109{
110 int ret = 0;
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +0300111 u32 total_retries = wl->conf.conn.psm_entry_retries;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200112
113 wl1271_debug(DEBUG_EVENT, "ps_status: 0x%x", mbox->ps_status);
114
115 switch (mbox->ps_status) {
116 case EVENT_ENTER_POWER_SAVE_FAIL:
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200117 wl1271_debug(DEBUG_PSM, "PSM entry failed");
118
Juuso Oikarinen71449f82009-12-11 15:41:07 +0200119 if (!test_bit(WL1271_FLAG_PSM, &wl->flags)) {
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200120 /* remain in active mode */
Juuso Oikarinen461fa132009-11-23 23:22:13 +0200121 wl->psm_entry_retry = 0;
122 break;
123 }
124
Juuso Oikarinen65cddbf2010-08-24 06:28:03 +0300125 if (wl->psm_entry_retry < total_retries) {
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200126 wl->psm_entry_retry++;
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200127 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE,
Eliad Pellerd2d66c52011-10-05 11:55:43 +0200128 wlvif->basic_rate, true);
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200129 } else {
Juuso Oikarinen1a186a52010-04-01 11:38:23 +0300130 wl1271_info("No ack to nullfunc from AP.");
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200131 wl->psm_entry_retry = 0;
Juuso Oikarinend60772f2010-03-26 12:53:29 +0200132 *beacon_loss = true;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200133 }
134 break;
135 case EVENT_ENTER_POWER_SAVE_SUCCESS:
136 wl->psm_entry_retry = 0;
Juuso Oikarinend8c42c02010-02-18 13:25:36 +0200137
138 /* enable beacon filtering */
139 ret = wl1271_acx_beacon_filter_opt(wl, true);
140 if (ret < 0)
141 break;
142
Shahar Levi0e44eb22011-05-16 15:35:30 +0300143 /*
144 * BET has only a minor effect in 5GHz and masks
145 * channel switch IEs, so we only enable BET on 2.4GHz
146 */
147 if (wl->band == IEEE80211_BAND_2GHZ)
148 /* enable beacon early termination */
149 ret = wl1271_acx_bet_enable(wl, true);
Eliad Peller94390642011-05-13 11:57:13 +0300150
151 if (wl->ps_compl) {
152 complete(wl->ps_compl);
153 wl->ps_compl = NULL;
154 }
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200155 break;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200156 default:
157 break;
158 }
159
160 return ret;
161}
162
Juuso Oikarinen00236aed2010-04-09 11:07:30 +0300163static void wl1271_event_rssi_trigger(struct wl1271 *wl,
164 struct event_mailbox *mbox)
165{
166 enum nl80211_cqm_rssi_threshold_event event;
167 s8 metric = mbox->rssi_snr_trigger_metric[0];
168
169 wl1271_debug(DEBUG_EVENT, "RSSI trigger metric: %d", metric);
170
171 if (metric <= wl->rssi_thold)
172 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
173 else
174 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
175
176 if (event != wl->last_rssi_event)
177 ieee80211_cqm_rssi_notify(wl->vif, event, GFP_KERNEL);
178 wl->last_rssi_event = event;
179}
180
Eliad Peller536129c82011-10-05 11:55:45 +0200181static void wl1271_stop_ba_event(struct wl1271 *wl, struct wl12xx_vif *wlvif)
Shahar Levi70559a02011-05-22 16:10:22 +0300182{
Eliad Peller536129c82011-10-05 11:55:45 +0200183 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
Arik Nemtsovf4d3b6a2011-08-25 12:43:16 +0300184 if (!wl->ba_rx_bitmap)
185 return;
186 ieee80211_stop_rx_ba_session(wl->vif, wl->ba_rx_bitmap,
Eliad Pellercdf09492011-10-05 11:55:44 +0200187 wl->vif->bss_conf.bssid);
Arik Nemtsovf4d3b6a2011-08-25 12:43:16 +0300188 } else {
189 int i;
190 struct wl1271_link *lnk;
Arik Nemtsov6b661892011-09-22 09:52:06 +0300191 for (i = WL1271_AP_STA_HLID_START; i < AP_MAX_LINKS; i++) {
Arik Nemtsovf4d3b6a2011-08-25 12:43:16 +0300192 lnk = &wl->links[i];
193 if (!wl1271_is_active_sta(wl, i) || !lnk->ba_bitmap)
194 continue;
Shahar Levi70559a02011-05-22 16:10:22 +0300195
Arik Nemtsovf4d3b6a2011-08-25 12:43:16 +0300196 ieee80211_stop_rx_ba_session(wl->vif,
197 lnk->ba_bitmap,
198 lnk->addr);
199 }
200 }
Shahar Levi70559a02011-05-22 16:10:22 +0300201}
202
Eliad Peller77ddaa12011-05-15 11:10:29 +0300203static void wl12xx_event_soft_gemini_sense(struct wl1271 *wl,
204 u8 enable)
205{
206 if (enable) {
207 /* disable dynamic PS when requested by the firmware */
208 ieee80211_disable_dyn_ps(wl->vif);
209 set_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
210 } else {
211 ieee80211_enable_dyn_ps(wl->vif);
212 clear_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
213 wl1271_recalc_rx_streaming(wl);
214 }
215
216}
217
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300218static void wl1271_event_mbox_dump(struct event_mailbox *mbox)
219{
220 wl1271_debug(DEBUG_EVENT, "MBOX DUMP:");
221 wl1271_debug(DEBUG_EVENT, "\tvector: 0x%x", mbox->events_vector);
222 wl1271_debug(DEBUG_EVENT, "\tmask: 0x%x", mbox->events_mask);
223}
224
225static int wl1271_event_process(struct wl1271 *wl, struct event_mailbox *mbox)
226{
Eliad Pellerd2d66c52011-10-05 11:55:43 +0200227 struct ieee80211_vif *vif = wl->vif; /* TODO: get as param */
228 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300229 int ret;
230 u32 vector;
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200231 bool beacon_loss = false;
Eliad Peller536129c82011-10-05 11:55:45 +0200232 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
Arik Nemtsov3618f302011-06-26 10:36:03 +0300233 bool disconnect_sta = false;
234 unsigned long sta_bitmap = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300235
236 wl1271_event_mbox_dump(mbox);
237
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300238 vector = le32_to_cpu(mbox->events_vector);
239 vector &= ~(le32_to_cpu(mbox->events_mask));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300240 wl1271_debug(DEBUG_EVENT, "vector: 0x%x", vector);
241
242 if (vector & SCAN_COMPLETE_EVENT_ID) {
Luciano Coelho34dd2aa2010-07-08 17:50:06 +0300243 wl1271_debug(DEBUG_EVENT, "status: 0x%x",
244 mbox->scheduled_scan_status);
245
Eliad Peller784f6942011-10-05 11:55:39 +0200246 wl1271_scan_stm(wl, wl->scan_vif);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300247 }
248
Luciano Coelho6394c012011-05-10 14:28:27 +0300249 if (vector & PERIODIC_SCAN_REPORT_EVENT_ID) {
250 wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_REPORT_EVENT "
251 "(status 0x%0x)", mbox->scheduled_scan_status);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300252
253 wl1271_scan_sched_scan_results(wl);
Luciano Coelho6394c012011-05-10 14:28:27 +0300254 }
255
256 if (vector & PERIODIC_SCAN_COMPLETE_EVENT_ID) {
257 wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_COMPLETE_EVENT "
258 "(status 0x%0x)", mbox->scheduled_scan_status);
Luciano Coelho33c2c062011-05-10 14:46:02 +0300259 if (wl->sched_scanning) {
260 wl1271_scan_sched_scan_stop(wl);
261 ieee80211_sched_scan_stopped(wl->hw);
262 }
Luciano Coelho6394c012011-05-10 14:28:27 +0300263 }
264
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300265 if (vector & SOFT_GEMINI_SENSE_EVENT_ID &&
Eliad Peller536129c82011-10-05 11:55:45 +0200266 wlvif->bss_type == BSS_TYPE_STA_BSS)
Eliad Peller77ddaa12011-05-15 11:10:29 +0300267 wl12xx_event_soft_gemini_sense(wl,
268 mbox->soft_gemini_sense_info);
Juuso Oikarinen8d2ef7b2010-07-08 17:50:03 +0300269
Juuso Oikarinenb771eee2009-10-08 21:56:34 +0300270 /*
271 * The BSS_LOSE_EVENT_ID is only needed while psm (and hence beacon
272 * filtering) is enabled. Without PSM, the stack will receive all
273 * beacons and can detect beacon loss by itself.
Teemu Paasikivi64e29e442010-03-26 12:53:26 +0200274 *
275 * As there's possibility that the driver disables PSM before receiving
276 * BSS_LOSE_EVENT, beacon loss has to be reported to the stack.
277 *
Juuso Oikarinenb771eee2009-10-08 21:56:34 +0300278 */
Arik Nemtsov203c9032010-10-25 11:17:44 +0200279 if ((vector & BSS_LOSE_EVENT_ID) && !is_ap) {
Juuso Oikarinen1a186a52010-04-01 11:38:23 +0300280 wl1271_info("Beacon loss detected.");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300281
Juuso Oikarinenb771eee2009-10-08 21:56:34 +0300282 /* indicate to the stack, that beacons have been lost */
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200283 beacon_loss = true;
284 }
285
Arik Nemtsov203c9032010-10-25 11:17:44 +0200286 if ((vector & PS_REPORT_EVENT_ID) && !is_ap) {
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200287 wl1271_debug(DEBUG_EVENT, "PS_REPORT_EVENT");
Eliad Pellerd2d66c52011-10-05 11:55:43 +0200288 ret = wl1271_event_ps_report(wl, wlvif, mbox, &beacon_loss);
Juuso Oikarinen19ad0712009-11-02 20:22:11 +0200289 if (ret < 0)
290 return ret;
291 }
292
Arik Nemtsov203c9032010-10-25 11:17:44 +0200293 if ((vector & PSPOLL_DELIVERY_FAILURE_EVENT_ID) && !is_ap)
Eliad Pellerd2d66c52011-10-05 11:55:43 +0200294 wl1271_event_pspoll_delivery_fail(wl, wlvif);
Juuso Oikarinen90494a92010-07-08 17:50:00 +0300295
Juuso Oikarinen00236aed2010-04-09 11:07:30 +0300296 if (vector & RSSI_SNR_TRIGGER_0_EVENT_ID) {
297 wl1271_debug(DEBUG_EVENT, "RSSI_SNR_TRIGGER_0_EVENT");
298 if (wl->vif)
299 wl1271_event_rssi_trigger(wl, mbox);
300 }
301
Arik Nemtsovf4d3b6a2011-08-25 12:43:16 +0300302 if ((vector & BA_SESSION_RX_CONSTRAINT_EVENT_ID)) {
Shahar Levi70559a02011-05-22 16:10:22 +0300303 wl1271_debug(DEBUG_EVENT, "BA_SESSION_RX_CONSTRAINT_EVENT_ID. "
Eliad Pellerc690ec82011-08-14 13:17:07 +0300304 "ba_allowed = 0x%x", mbox->rx_ba_allowed);
Shahar Levi70559a02011-05-22 16:10:22 +0300305
Arik Nemtsovf4d3b6a2011-08-25 12:43:16 +0300306 wl->ba_allowed = !!mbox->rx_ba_allowed;
307
308 if (wl->vif && !wl->ba_allowed)
Eliad Peller536129c82011-10-05 11:55:45 +0200309 wl1271_stop_ba_event(wl, wlvif);
Shahar Levi70559a02011-05-22 16:10:22 +0300310 }
311
Shahar Levi6d158ff2011-09-08 13:01:33 +0300312 if ((vector & CHANNEL_SWITCH_COMPLETE_EVENT_ID) && !is_ap) {
313 wl1271_debug(DEBUG_EVENT, "CHANNEL_SWITCH_COMPLETE_EVENT_ID. "
314 "status = 0x%x",
315 mbox->channel_switch_status);
316 /*
317 * That event uses for two cases:
318 * 1) channel switch complete with status=0
319 * 2) channel switch failed status=1
320 */
321 if (test_and_clear_bit(WL1271_FLAG_CS_PROGRESS, &wl->flags) &&
322 (wl->vif))
323 ieee80211_chswitch_done(wl->vif,
324 mbox->channel_switch_status ? false : true);
325 }
326
Arik Nemtsov79ebec72011-08-14 13:17:18 +0300327 if ((vector & DUMMY_PACKET_EVENT_ID)) {
Shahar Leviae47c452011-03-06 16:32:14 +0200328 wl1271_debug(DEBUG_EVENT, "DUMMY_PACKET_ID_EVENT_ID");
329 if (wl->vif)
330 wl1271_tx_dummy_packet(wl);
331 }
332
Arik Nemtsov3618f302011-06-26 10:36:03 +0300333 /*
334 * "TX retries exceeded" has a different meaning according to mode.
335 * In AP mode the offending station is disconnected.
336 */
337 if ((vector & MAX_TX_RETRY_EVENT_ID) && is_ap) {
338 wl1271_debug(DEBUG_EVENT, "MAX_TX_RETRY_EVENT_ID");
339 sta_bitmap |= le16_to_cpu(mbox->sta_tx_retry_exceeded);
340 disconnect_sta = true;
341 }
342
343 if ((vector & INACTIVE_STA_EVENT_ID) && is_ap) {
344 wl1271_debug(DEBUG_EVENT, "INACTIVE_STA_EVENT_ID");
345 sta_bitmap |= le16_to_cpu(mbox->sta_aging_status);
346 disconnect_sta = true;
347 }
348
349 if (is_ap && disconnect_sta) {
350 u32 num_packets = wl->conf.tx.max_tx_retries;
351 struct ieee80211_sta *sta;
352 const u8 *addr;
353 int h;
354
355 for (h = find_first_bit(&sta_bitmap, AP_MAX_LINKS);
356 h < AP_MAX_LINKS;
357 h = find_next_bit(&sta_bitmap, AP_MAX_LINKS, h+1)) {
358 if (!wl1271_is_active_sta(wl, h))
359 continue;
360
361 addr = wl->links[h].addr;
362
363 rcu_read_lock();
364 sta = ieee80211_find_sta(wl->vif, addr);
365 if (sta) {
366 wl1271_debug(DEBUG_EVENT, "remove sta %d", h);
367 ieee80211_report_low_ack(sta, num_packets);
368 }
369 rcu_read_unlock();
370 }
371 }
372
Juuso Oikarinend60772f2010-03-26 12:53:29 +0200373 if (wl->vif && beacon_loss)
374 ieee80211_connection_loss(wl->vif);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300375
376 return 0;
377}
378
379int wl1271_event_unmask(struct wl1271 *wl)
380{
381 int ret;
382
383 ret = wl1271_acx_event_mbox_mask(wl, ~(wl->event_mask));
384 if (ret < 0)
385 return ret;
386
387 return 0;
388}
389
390void wl1271_event_mbox_config(struct wl1271 *wl)
391{
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200392 wl->mbox_ptr[0] = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300393 wl->mbox_ptr[1] = wl->mbox_ptr[0] + sizeof(struct event_mailbox);
394
395 wl1271_debug(DEBUG_EVENT, "MBOX ptrs: 0x%x 0x%x",
396 wl->mbox_ptr[0], wl->mbox_ptr[1]);
397}
398
Juuso Oikarinen13f2dc52009-12-11 15:40:59 +0200399int wl1271_event_handle(struct wl1271 *wl, u8 mbox_num)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300400{
401 struct event_mailbox mbox;
402 int ret;
403
404 wl1271_debug(DEBUG_EVENT, "EVENT on mbox %d", mbox_num);
405
406 if (mbox_num > 1)
407 return -EINVAL;
408
409 /* first we read the mbox descriptor */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200410 wl1271_read(wl, wl->mbox_ptr[mbox_num], &mbox,
411 sizeof(struct event_mailbox), false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300412
413 /* process the descriptor */
414 ret = wl1271_event_process(wl, &mbox);
415 if (ret < 0)
416 return ret;
417
418 /* then we let the firmware know it can go on...*/
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200419 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_EVENT_ACK);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300420
421 return 0;
422}