wl12xx: move debugging definitions to a separate file
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / wl12xx / event.c
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 "wl12xx.h"
25 #include "debug.h"
26 #include "reg.h"
27 #include "io.h"
28 #include "event.h"
29 #include "ps.h"
30 #include "scan.h"
31 #include "wl12xx_80211.h"
32
33 void wl1271_pspoll_work(struct work_struct *work)
34 {
35         struct ieee80211_vif *vif;
36         struct wl12xx_vif *wlvif;
37         struct delayed_work *dwork;
38         struct wl1271 *wl;
39         int ret;
40
41         dwork = container_of(work, struct delayed_work, work);
42         wlvif = container_of(dwork, struct wl12xx_vif, pspoll_work);
43         vif = container_of((void *)wlvif, struct ieee80211_vif, drv_priv);
44         wl = wlvif->wl;
45
46         wl1271_debug(DEBUG_EVENT, "pspoll work");
47
48         mutex_lock(&wl->mutex);
49
50         if (unlikely(wl->state == WL1271_STATE_OFF))
51                 goto out;
52
53         if (!test_and_clear_bit(WLVIF_FLAG_PSPOLL_FAILURE, &wlvif->flags))
54                 goto out;
55
56         if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
57                 goto out;
58
59         /*
60          * if we end up here, then we were in powersave when the pspoll
61          * delivery failure occurred, and no-one changed state since, so
62          * we should go back to powersave.
63          */
64         ret = wl1271_ps_elp_wakeup(wl);
65         if (ret < 0)
66                 goto out;
67
68         wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE,
69                            wlvif->basic_rate, true);
70
71         wl1271_ps_elp_sleep(wl);
72 out:
73         mutex_unlock(&wl->mutex);
74 };
75
76 static void wl1271_event_pspoll_delivery_fail(struct wl1271 *wl,
77                                               struct wl12xx_vif *wlvif)
78 {
79         int delay = wl->conf.conn.ps_poll_recovery_period;
80         int ret;
81
82         wlvif->ps_poll_failures++;
83         if (wlvif->ps_poll_failures == 1)
84                 wl1271_info("AP with dysfunctional ps-poll, "
85                             "trying to work around it.");
86
87         /* force active mode receive data from the AP */
88         if (test_bit(WLVIF_FLAG_PSM, &wlvif->flags)) {
89                 ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE,
90                                          wlvif->basic_rate, true);
91                 if (ret < 0)
92                         return;
93                 set_bit(WLVIF_FLAG_PSPOLL_FAILURE, &wlvif->flags);
94                 ieee80211_queue_delayed_work(wl->hw, &wlvif->pspoll_work,
95                                              msecs_to_jiffies(delay));
96         }
97
98         /*
99          * If already in active mode, lets we should be getting data from
100          * the AP right away. If we enter PSM too fast after this, and data
101          * remains on the AP, we will get another event like this, and we'll
102          * go into active once more.
103          */
104 }
105
106 static int wl1271_event_ps_report(struct wl1271 *wl,
107                                   struct wl12xx_vif *wlvif,
108                                   struct event_mailbox *mbox,
109                                   bool *beacon_loss)
110 {
111         int ret = 0;
112         u32 total_retries = wl->conf.conn.psm_entry_retries;
113
114         wl1271_debug(DEBUG_EVENT, "ps_status: 0x%x", mbox->ps_status);
115
116         switch (mbox->ps_status) {
117         case EVENT_ENTER_POWER_SAVE_FAIL:
118                 wl1271_debug(DEBUG_PSM, "PSM entry failed");
119
120                 if (!test_bit(WLVIF_FLAG_PSM, &wlvif->flags)) {
121                         /* remain in active mode */
122                         wlvif->psm_entry_retry = 0;
123                         break;
124                 }
125
126                 if (wlvif->psm_entry_retry < total_retries) {
127                         wlvif->psm_entry_retry++;
128                         ret = wl1271_ps_set_mode(wl, wlvif,
129                                                  STATION_POWER_SAVE_MODE,
130                                                  wlvif->basic_rate, true);
131                 } else {
132                         wl1271_info("No ack to nullfunc from AP.");
133                         wlvif->psm_entry_retry = 0;
134                         *beacon_loss = true;
135                 }
136                 break;
137         case EVENT_ENTER_POWER_SAVE_SUCCESS:
138                 wlvif->psm_entry_retry = 0;
139
140                 /* enable beacon filtering */
141                 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
142                 if (ret < 0)
143                         break;
144
145                 /*
146                  * BET has only a minor effect in 5GHz and masks
147                  * channel switch IEs, so we only enable BET on 2.4GHz
148                 */
149                 if (wlvif->band == IEEE80211_BAND_2GHZ)
150                         /* enable beacon early termination */
151                         ret = wl1271_acx_bet_enable(wl, wlvif, true);
152
153                 if (wlvif->ps_compl) {
154                         complete(wlvif->ps_compl);
155                         wlvif->ps_compl = NULL;
156                 }
157                 break;
158         default:
159                 break;
160         }
161
162         return ret;
163 }
164
165 static void wl1271_event_rssi_trigger(struct wl1271 *wl,
166                                       struct wl12xx_vif *wlvif,
167                                       struct event_mailbox *mbox)
168 {
169         struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
170         enum nl80211_cqm_rssi_threshold_event event;
171         s8 metric = mbox->rssi_snr_trigger_metric[0];
172
173         wl1271_debug(DEBUG_EVENT, "RSSI trigger metric: %d", metric);
174
175         if (metric <= wlvif->rssi_thold)
176                 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
177         else
178                 event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
179
180         if (event != wlvif->last_rssi_event)
181                 ieee80211_cqm_rssi_notify(vif, event, GFP_KERNEL);
182         wlvif->last_rssi_event = event;
183 }
184
185 static void wl1271_stop_ba_event(struct wl1271 *wl, struct wl12xx_vif *wlvif)
186 {
187         struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
188
189         if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
190                 if (!wlvif->sta.ba_rx_bitmap)
191                         return;
192                 ieee80211_stop_rx_ba_session(vif, wlvif->sta.ba_rx_bitmap,
193                                              vif->bss_conf.bssid);
194         } else {
195                 u8 hlid;
196                 struct wl1271_link *lnk;
197                 for_each_set_bit(hlid, wlvif->ap.sta_hlid_map,
198                                  WL12XX_MAX_LINKS) {
199                         lnk = &wl->links[hlid];
200                         if (!lnk->ba_bitmap)
201                                 continue;
202
203                         ieee80211_stop_rx_ba_session(vif,
204                                                      lnk->ba_bitmap,
205                                                      lnk->addr);
206                 }
207         }
208 }
209
210 static void wl12xx_event_soft_gemini_sense(struct wl1271 *wl,
211                                                u8 enable)
212 {
213         struct ieee80211_vif *vif;
214         struct wl12xx_vif *wlvif;
215
216         if (enable) {
217                 /* disable dynamic PS when requested by the firmware */
218                 wl12xx_for_each_wlvif_sta(wl, wlvif) {
219                         vif = wl12xx_wlvif_to_vif(wlvif);
220                         ieee80211_disable_dyn_ps(vif);
221                 }
222                 set_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
223         } else {
224                 clear_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
225                 wl12xx_for_each_wlvif_sta(wl, wlvif) {
226                         vif = wl12xx_wlvif_to_vif(wlvif);
227                         ieee80211_enable_dyn_ps(vif);
228                         wl1271_recalc_rx_streaming(wl, wlvif);
229                 }
230         }
231
232 }
233
234 static void wl1271_event_mbox_dump(struct event_mailbox *mbox)
235 {
236         wl1271_debug(DEBUG_EVENT, "MBOX DUMP:");
237         wl1271_debug(DEBUG_EVENT, "\tvector: 0x%x", mbox->events_vector);
238         wl1271_debug(DEBUG_EVENT, "\tmask: 0x%x", mbox->events_mask);
239 }
240
241 static int wl1271_event_process(struct wl1271 *wl, struct event_mailbox *mbox)
242 {
243         struct ieee80211_vif *vif;
244         struct wl12xx_vif *wlvif;
245         int ret;
246         u32 vector;
247         bool beacon_loss = false;
248         bool disconnect_sta = false;
249         unsigned long sta_bitmap = 0;
250
251         wl1271_event_mbox_dump(mbox);
252
253         vector = le32_to_cpu(mbox->events_vector);
254         vector &= ~(le32_to_cpu(mbox->events_mask));
255         wl1271_debug(DEBUG_EVENT, "vector: 0x%x", vector);
256
257         if (vector & SCAN_COMPLETE_EVENT_ID) {
258                 wl1271_debug(DEBUG_EVENT, "status: 0x%x",
259                              mbox->scheduled_scan_status);
260
261                 wl1271_scan_stm(wl, wl->scan_vif);
262         }
263
264         if (vector & PERIODIC_SCAN_REPORT_EVENT_ID) {
265                 wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_REPORT_EVENT "
266                              "(status 0x%0x)", mbox->scheduled_scan_status);
267
268                 wl1271_scan_sched_scan_results(wl);
269         }
270
271         if (vector & PERIODIC_SCAN_COMPLETE_EVENT_ID) {
272                 wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_COMPLETE_EVENT "
273                              "(status 0x%0x)", mbox->scheduled_scan_status);
274                 if (wl->sched_scanning) {
275                         wl1271_scan_sched_scan_stop(wl);
276                         ieee80211_sched_scan_stopped(wl->hw);
277                 }
278         }
279
280         if (vector & SOFT_GEMINI_SENSE_EVENT_ID)
281                 wl12xx_event_soft_gemini_sense(wl,
282                                                mbox->soft_gemini_sense_info);
283
284         /*
285          * The BSS_LOSE_EVENT_ID is only needed while psm (and hence beacon
286          * filtering) is enabled. Without PSM, the stack will receive all
287          * beacons and can detect beacon loss by itself.
288          *
289          * As there's possibility that the driver disables PSM before receiving
290          * BSS_LOSE_EVENT, beacon loss has to be reported to the stack.
291          *
292          */
293         if (vector & BSS_LOSE_EVENT_ID) {
294                 /* TODO: check for multi-role */
295                 wl1271_info("Beacon loss detected.");
296
297                 /* indicate to the stack, that beacons have been lost */
298                 beacon_loss = true;
299         }
300
301         if (vector & PS_REPORT_EVENT_ID) {
302                 wl1271_debug(DEBUG_EVENT, "PS_REPORT_EVENT");
303                 wl12xx_for_each_wlvif_sta(wl, wlvif) {
304                         ret = wl1271_event_ps_report(wl, wlvif,
305                                                      mbox, &beacon_loss);
306                         if (ret < 0)
307                                 return ret;
308                 }
309         }
310
311         if (vector & PSPOLL_DELIVERY_FAILURE_EVENT_ID)
312                 wl12xx_for_each_wlvif_sta(wl, wlvif) {
313                         wl1271_event_pspoll_delivery_fail(wl, wlvif);
314                 }
315
316         if (vector & RSSI_SNR_TRIGGER_0_EVENT_ID) {
317                 /* TODO: check actual multi-role support */
318                 wl1271_debug(DEBUG_EVENT, "RSSI_SNR_TRIGGER_0_EVENT");
319                 wl12xx_for_each_wlvif_sta(wl, wlvif) {
320                         wl1271_event_rssi_trigger(wl, wlvif, mbox);
321                 }
322         }
323
324         if (vector & BA_SESSION_RX_CONSTRAINT_EVENT_ID) {
325                 u8 role_id = mbox->role_id;
326                 wl1271_debug(DEBUG_EVENT, "BA_SESSION_RX_CONSTRAINT_EVENT_ID. "
327                              "ba_allowed = 0x%x, role_id=%d",
328                              mbox->rx_ba_allowed, role_id);
329
330                 wl12xx_for_each_wlvif(wl, wlvif) {
331                         if (role_id != 0xff && role_id != wlvif->role_id)
332                                 continue;
333
334                         wlvif->ba_allowed = !!mbox->rx_ba_allowed;
335                         if (!wlvif->ba_allowed)
336                                 wl1271_stop_ba_event(wl, wlvif);
337                 }
338         }
339
340         if (vector & CHANNEL_SWITCH_COMPLETE_EVENT_ID) {
341                 wl1271_debug(DEBUG_EVENT, "CHANNEL_SWITCH_COMPLETE_EVENT_ID. "
342                                           "status = 0x%x",
343                                           mbox->channel_switch_status);
344                 /*
345                  * That event uses for two cases:
346                  * 1) channel switch complete with status=0
347                  * 2) channel switch failed status=1
348                  */
349
350                 /* TODO: configure only the relevant vif */
351                 wl12xx_for_each_wlvif_sta(wl, wlvif) {
352                         struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
353                         bool success;
354
355                         if (!test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS,
356                                                 &wl->flags))
357                                 continue;
358
359                         success = mbox->channel_switch_status ? false : true;
360                         ieee80211_chswitch_done(vif, success);
361                 }
362         }
363
364         if ((vector & DUMMY_PACKET_EVENT_ID)) {
365                 wl1271_debug(DEBUG_EVENT, "DUMMY_PACKET_ID_EVENT_ID");
366                 wl1271_tx_dummy_packet(wl);
367         }
368
369         /*
370          * "TX retries exceeded" has a different meaning according to mode.
371          * In AP mode the offending station is disconnected.
372          */
373         if (vector & MAX_TX_RETRY_EVENT_ID) {
374                 wl1271_debug(DEBUG_EVENT, "MAX_TX_RETRY_EVENT_ID");
375                 sta_bitmap |= le16_to_cpu(mbox->sta_tx_retry_exceeded);
376                 disconnect_sta = true;
377         }
378
379         if (vector & INACTIVE_STA_EVENT_ID) {
380                 wl1271_debug(DEBUG_EVENT, "INACTIVE_STA_EVENT_ID");
381                 sta_bitmap |= le16_to_cpu(mbox->sta_aging_status);
382                 disconnect_sta = true;
383         }
384
385         if (disconnect_sta) {
386                 u32 num_packets = wl->conf.tx.max_tx_retries;
387                 struct ieee80211_sta *sta;
388                 const u8 *addr;
389                 int h;
390
391                 for_each_set_bit(h, &sta_bitmap, WL12XX_MAX_LINKS) {
392                         bool found = false;
393                         /* find the ap vif connected to this sta */
394                         wl12xx_for_each_wlvif_ap(wl, wlvif) {
395                                 if (!test_bit(h, wlvif->ap.sta_hlid_map))
396                                         continue;
397                                 found = true;
398                                 break;
399                         }
400                         if (!found)
401                                 continue;
402
403                         vif = wl12xx_wlvif_to_vif(wlvif);
404                         addr = wl->links[h].addr;
405
406                         rcu_read_lock();
407                         sta = ieee80211_find_sta(vif, addr);
408                         if (sta) {
409                                 wl1271_debug(DEBUG_EVENT, "remove sta %d", h);
410                                 ieee80211_report_low_ack(sta, num_packets);
411                         }
412                         rcu_read_unlock();
413                 }
414         }
415
416         if (beacon_loss)
417                 wl12xx_for_each_wlvif_sta(wl, wlvif) {
418                         vif = wl12xx_wlvif_to_vif(wlvif);
419                         ieee80211_connection_loss(vif);
420                 }
421
422         return 0;
423 }
424
425 int wl1271_event_unmask(struct wl1271 *wl)
426 {
427         int ret;
428
429         ret = wl1271_acx_event_mbox_mask(wl, ~(wl->event_mask));
430         if (ret < 0)
431                 return ret;
432
433         return 0;
434 }
435
436 void wl1271_event_mbox_config(struct wl1271 *wl)
437 {
438         wl->mbox_ptr[0] = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
439         wl->mbox_ptr[1] = wl->mbox_ptr[0] + sizeof(struct event_mailbox);
440
441         wl1271_debug(DEBUG_EVENT, "MBOX ptrs: 0x%x 0x%x",
442                      wl->mbox_ptr[0], wl->mbox_ptr[1]);
443 }
444
445 int wl1271_event_handle(struct wl1271 *wl, u8 mbox_num)
446 {
447         struct event_mailbox mbox;
448         int ret;
449
450         wl1271_debug(DEBUG_EVENT, "EVENT on mbox %d", mbox_num);
451
452         if (mbox_num > 1)
453                 return -EINVAL;
454
455         /* first we read the mbox descriptor */
456         wl1271_read(wl, wl->mbox_ptr[mbox_num], &mbox,
457                     sizeof(struct event_mailbox), false);
458
459         /* process the descriptor */
460         ret = wl1271_event_process(wl, &mbox);
461         if (ret < 0)
462                 return ret;
463
464         /* then we let the firmware know it can go on...*/
465         wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_EVENT_ACK);
466
467         return 0;
468 }