mwifiex: handle radar detect event from FW
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / mwifiex / sta_event.c
1 /*
2  * Marvell Wireless LAN device driver: station event handling
3  *
4  * Copyright (C) 2011-2014, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "decl.h"
21 #include "ioctl.h"
22 #include "util.h"
23 #include "fw.h"
24 #include "main.h"
25 #include "wmm.h"
26 #include "11n.h"
27
28 /*
29  * This function resets the connection state.
30  *
31  * The function is invoked after receiving a disconnect event from firmware,
32  * and performs the following actions -
33  *      - Set media status to disconnected
34  *      - Clean up Tx and Rx packets
35  *      - Resets SNR/NF/RSSI value in driver
36  *      - Resets security configurations in driver
37  *      - Enables auto data rate
38  *      - Saves the previous SSID and BSSID so that they can
39  *        be used for re-association, if required
40  *      - Erases current SSID and BSSID information
41  *      - Sends a disconnect event to upper layers/applications.
42  */
43 void
44 mwifiex_reset_connect_state(struct mwifiex_private *priv, u16 reason_code)
45 {
46         struct mwifiex_adapter *adapter = priv->adapter;
47
48         if (!priv->media_connected)
49                 return;
50
51         dev_dbg(adapter->dev, "info: handles disconnect event\n");
52
53         priv->media_connected = false;
54
55         priv->scan_block = false;
56
57         if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA) &&
58             ISSUPP_TDLS_ENABLED(priv->adapter->fw_cap_info)) {
59                 mwifiex_disable_all_tdls_links(priv);
60
61                 if (priv->adapter->auto_tdls)
62                         mwifiex_clean_auto_tdls(priv);
63         }
64
65         /* Free Tx and Rx packets, report disconnect to upper layer */
66         mwifiex_clean_txrx(priv);
67
68         /* Reset SNR/NF/RSSI values */
69         priv->data_rssi_last = 0;
70         priv->data_nf_last = 0;
71         priv->data_rssi_avg = 0;
72         priv->data_nf_avg = 0;
73         priv->bcn_rssi_last = 0;
74         priv->bcn_nf_last = 0;
75         priv->bcn_rssi_avg = 0;
76         priv->bcn_nf_avg = 0;
77         priv->rxpd_rate = 0;
78         priv->rxpd_htinfo = 0;
79         priv->sec_info.wpa_enabled = false;
80         priv->sec_info.wpa2_enabled = false;
81         priv->wpa_ie_len = 0;
82
83         priv->sec_info.wapi_enabled = false;
84         priv->wapi_ie_len = 0;
85         priv->sec_info.wapi_key_on = false;
86
87         priv->sec_info.encryption_mode = 0;
88
89         /* Enable auto data rate */
90         priv->is_data_rate_auto = true;
91         priv->data_rate = 0;
92
93         if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_STA ||
94              GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) && priv->hist_data)
95                 mwifiex_hist_data_reset(priv);
96
97         if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
98                 priv->adhoc_state = ADHOC_IDLE;
99                 priv->adhoc_is_link_sensed = false;
100         }
101
102         /*
103          * Memorize the previous SSID and BSSID so
104          * it could be used for re-assoc
105          */
106
107         dev_dbg(adapter->dev, "info: previous SSID=%s, SSID len=%u\n",
108                 priv->prev_ssid.ssid, priv->prev_ssid.ssid_len);
109
110         dev_dbg(adapter->dev, "info: current SSID=%s, SSID len=%u\n",
111                 priv->curr_bss_params.bss_descriptor.ssid.ssid,
112                 priv->curr_bss_params.bss_descriptor.ssid.ssid_len);
113
114         memcpy(&priv->prev_ssid,
115                &priv->curr_bss_params.bss_descriptor.ssid,
116                sizeof(struct cfg80211_ssid));
117
118         memcpy(priv->prev_bssid,
119                priv->curr_bss_params.bss_descriptor.mac_address, ETH_ALEN);
120
121         /* Need to erase the current SSID and BSSID info */
122         memset(&priv->curr_bss_params, 0x00, sizeof(priv->curr_bss_params));
123
124         adapter->tx_lock_flag = false;
125         adapter->pps_uapsd_mode = false;
126
127         if (adapter->is_cmd_timedout && adapter->curr_cmd)
128                 return;
129         priv->media_connected = false;
130         dev_dbg(adapter->dev,
131                 "info: successfully disconnected from %pM: reason code %d\n",
132                 priv->cfg_bssid, reason_code);
133         if (priv->bss_mode == NL80211_IFTYPE_STATION ||
134             priv->bss_mode == NL80211_IFTYPE_P2P_CLIENT) {
135                 cfg80211_disconnected(priv->netdev, reason_code, NULL, 0,
136                                       GFP_KERNEL);
137         }
138         memset(priv->cfg_bssid, 0, ETH_ALEN);
139
140         mwifiex_stop_net_dev_queue(priv->netdev, adapter);
141         if (netif_carrier_ok(priv->netdev))
142                 netif_carrier_off(priv->netdev);
143 }
144
145 static int mwifiex_parse_tdls_event(struct mwifiex_private *priv,
146                                     struct sk_buff *event_skb)
147 {
148         int ret = 0;
149         struct mwifiex_adapter *adapter = priv->adapter;
150         struct mwifiex_sta_node *sta_ptr;
151         struct mwifiex_tdls_generic_event *tdls_evt =
152                         (void *)event_skb->data + sizeof(adapter->event_cause);
153
154         /* reserved 2 bytes are not mandatory in tdls event */
155         if (event_skb->len < (sizeof(struct mwifiex_tdls_generic_event) -
156                               sizeof(u16) - sizeof(adapter->event_cause))) {
157                 dev_err(adapter->dev, "Invalid event length!\n");
158                 return -1;
159         }
160
161         sta_ptr = mwifiex_get_sta_entry(priv, tdls_evt->peer_mac);
162         if (!sta_ptr) {
163                 dev_err(adapter->dev, "cannot get sta entry!\n");
164                 return -1;
165         }
166
167         switch (le16_to_cpu(tdls_evt->type)) {
168         case TDLS_EVENT_LINK_TEAR_DOWN:
169                 cfg80211_tdls_oper_request(priv->netdev,
170                                            tdls_evt->peer_mac,
171                                            NL80211_TDLS_TEARDOWN,
172                                            le16_to_cpu(tdls_evt->u.reason_code),
173                                            GFP_KERNEL);
174                 break;
175         default:
176                 break;
177         }
178
179         return ret;
180 }
181
182 /*
183  * This function handles events generated by firmware.
184  *
185  * This is a generic function and handles all events.
186  *
187  * Event specific routines are called by this function based
188  * upon the generated event cause.
189  *
190  * For the following events, the function just forwards them to upper
191  * layers, optionally recording the change -
192  *      - EVENT_LINK_SENSED
193  *      - EVENT_MIC_ERR_UNICAST
194  *      - EVENT_MIC_ERR_MULTICAST
195  *      - EVENT_PORT_RELEASE
196  *      - EVENT_RSSI_LOW
197  *      - EVENT_SNR_LOW
198  *      - EVENT_MAX_FAIL
199  *      - EVENT_RSSI_HIGH
200  *      - EVENT_SNR_HIGH
201  *      - EVENT_DATA_RSSI_LOW
202  *      - EVENT_DATA_SNR_LOW
203  *      - EVENT_DATA_RSSI_HIGH
204  *      - EVENT_DATA_SNR_HIGH
205  *      - EVENT_LINK_QUALITY
206  *      - EVENT_PRE_BEACON_LOST
207  *      - EVENT_IBSS_COALESCED
208  *      - EVENT_WEP_ICV_ERR
209  *      - EVENT_BW_CHANGE
210  *      - EVENT_HOSTWAKE_STAIE
211   *
212  * For the following events, no action is taken -
213  *      - EVENT_MIB_CHANGED
214  *      - EVENT_INIT_DONE
215  *      - EVENT_DUMMY_HOST_WAKEUP_SIGNAL
216  *
217  * Rest of the supported events requires driver handling -
218  *      - EVENT_DEAUTHENTICATED
219  *      - EVENT_DISASSOCIATED
220  *      - EVENT_LINK_LOST
221  *      - EVENT_PS_SLEEP
222  *      - EVENT_PS_AWAKE
223  *      - EVENT_DEEP_SLEEP_AWAKE
224  *      - EVENT_HS_ACT_REQ
225  *      - EVENT_ADHOC_BCN_LOST
226  *      - EVENT_BG_SCAN_REPORT
227  *      - EVENT_WMM_STATUS_CHANGE
228  *      - EVENT_ADDBA
229  *      - EVENT_DELBA
230  *      - EVENT_BA_STREAM_TIEMOUT
231  *      - EVENT_AMSDU_AGGR_CTRL
232  */
233 int mwifiex_process_sta_event(struct mwifiex_private *priv)
234 {
235         struct mwifiex_adapter *adapter = priv->adapter;
236         int ret = 0;
237         u32 eventcause = adapter->event_cause;
238         u16 ctrl, reason_code;
239
240         switch (eventcause) {
241         case EVENT_DUMMY_HOST_WAKEUP_SIGNAL:
242                 dev_err(adapter->dev,
243                         "invalid EVENT: DUMMY_HOST_WAKEUP_SIGNAL, ignore it\n");
244                 break;
245         case EVENT_LINK_SENSED:
246                 dev_dbg(adapter->dev, "event: LINK_SENSED\n");
247                 if (!netif_carrier_ok(priv->netdev))
248                         netif_carrier_on(priv->netdev);
249                 mwifiex_wake_up_net_dev_queue(priv->netdev, adapter);
250                 break;
251
252         case EVENT_DEAUTHENTICATED:
253                 dev_dbg(adapter->dev, "event: Deauthenticated\n");
254                 if (priv->wps.session_enable) {
255                         dev_dbg(adapter->dev,
256                                 "info: receive deauth event in wps session\n");
257                         break;
258                 }
259                 adapter->dbg.num_event_deauth++;
260                 if (priv->media_connected) {
261                         reason_code =
262                                 le16_to_cpu(*(__le16 *)adapter->event_body);
263                         mwifiex_reset_connect_state(priv, reason_code);
264                 }
265                 break;
266
267         case EVENT_DISASSOCIATED:
268                 dev_dbg(adapter->dev, "event: Disassociated\n");
269                 if (priv->wps.session_enable) {
270                         dev_dbg(adapter->dev,
271                                 "info: receive disassoc event in wps session\n");
272                         break;
273                 }
274                 adapter->dbg.num_event_disassoc++;
275                 if (priv->media_connected) {
276                         reason_code =
277                                 le16_to_cpu(*(__le16 *)adapter->event_body);
278                         mwifiex_reset_connect_state(priv, reason_code);
279                 }
280                 break;
281
282         case EVENT_LINK_LOST:
283                 dev_dbg(adapter->dev, "event: Link lost\n");
284                 adapter->dbg.num_event_link_lost++;
285                 if (priv->media_connected) {
286                         reason_code =
287                                 le16_to_cpu(*(__le16 *)adapter->event_body);
288                         mwifiex_reset_connect_state(priv, reason_code);
289                 }
290                 break;
291
292         case EVENT_PS_SLEEP:
293                 dev_dbg(adapter->dev, "info: EVENT: SLEEP\n");
294
295                 adapter->ps_state = PS_STATE_PRE_SLEEP;
296
297                 mwifiex_check_ps_cond(adapter);
298                 break;
299
300         case EVENT_PS_AWAKE:
301                 dev_dbg(adapter->dev, "info: EVENT: AWAKE\n");
302                 if (!adapter->pps_uapsd_mode &&
303                     priv->media_connected && adapter->sleep_period.period) {
304                                 adapter->pps_uapsd_mode = true;
305                                 dev_dbg(adapter->dev,
306                                         "event: PPS/UAPSD mode activated\n");
307                 }
308                 adapter->tx_lock_flag = false;
309                 if (adapter->pps_uapsd_mode && adapter->gen_null_pkt) {
310                         if (mwifiex_check_last_packet_indication(priv)) {
311                                 if (adapter->data_sent) {
312                                         adapter->ps_state = PS_STATE_AWAKE;
313                                         adapter->pm_wakeup_card_req = false;
314                                         adapter->pm_wakeup_fw_try = false;
315                                         mod_timer(&adapter->wakeup_timer,
316                                                   jiffies + (HZ*3));
317                                         break;
318                                 }
319                                 if (!mwifiex_send_null_packet
320                                         (priv,
321                                          MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
322                                          MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET))
323                                                 adapter->ps_state =
324                                                         PS_STATE_SLEEP;
325                                         return 0;
326                         }
327                 }
328                 adapter->ps_state = PS_STATE_AWAKE;
329                 adapter->pm_wakeup_card_req = false;
330                 adapter->pm_wakeup_fw_try = false;
331                 del_timer_sync(&adapter->wakeup_timer);
332
333                 break;
334
335         case EVENT_DEEP_SLEEP_AWAKE:
336                 adapter->if_ops.wakeup_complete(adapter);
337                 dev_dbg(adapter->dev, "event: DS_AWAKE\n");
338                 if (adapter->is_deep_sleep)
339                         adapter->is_deep_sleep = false;
340                 break;
341
342         case EVENT_HS_ACT_REQ:
343                 dev_dbg(adapter->dev, "event: HS_ACT_REQ\n");
344                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_HS_CFG_ENH,
345                                        0, 0, NULL, false);
346                 break;
347
348         case EVENT_MIC_ERR_UNICAST:
349                 dev_dbg(adapter->dev, "event: UNICAST MIC ERROR\n");
350                 cfg80211_michael_mic_failure(priv->netdev, priv->cfg_bssid,
351                                              NL80211_KEYTYPE_PAIRWISE,
352                                              -1, NULL, GFP_KERNEL);
353                 break;
354
355         case EVENT_MIC_ERR_MULTICAST:
356                 dev_dbg(adapter->dev, "event: MULTICAST MIC ERROR\n");
357                 cfg80211_michael_mic_failure(priv->netdev, priv->cfg_bssid,
358                                              NL80211_KEYTYPE_GROUP,
359                                              -1, NULL, GFP_KERNEL);
360                 break;
361         case EVENT_MIB_CHANGED:
362         case EVENT_INIT_DONE:
363                 break;
364
365         case EVENT_ADHOC_BCN_LOST:
366                 dev_dbg(adapter->dev, "event: ADHOC_BCN_LOST\n");
367                 priv->adhoc_is_link_sensed = false;
368                 mwifiex_clean_txrx(priv);
369                 mwifiex_stop_net_dev_queue(priv->netdev, adapter);
370                 if (netif_carrier_ok(priv->netdev))
371                         netif_carrier_off(priv->netdev);
372                 break;
373
374         case EVENT_BG_SCAN_REPORT:
375                 dev_dbg(adapter->dev, "event: BGS_REPORT\n");
376                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_BG_SCAN_QUERY,
377                                        HostCmd_ACT_GEN_GET, 0, NULL, false);
378                 break;
379
380         case EVENT_PORT_RELEASE:
381                 dev_dbg(adapter->dev, "event: PORT RELEASE\n");
382                 break;
383
384         case EVENT_EXT_SCAN_REPORT:
385                 dev_dbg(adapter->dev, "event: EXT_SCAN Report\n");
386                 if (adapter->ext_scan)
387                         ret = mwifiex_handle_event_ext_scan_report(priv,
388                                                 adapter->event_skb->data);
389
390                 break;
391
392         case EVENT_WMM_STATUS_CHANGE:
393                 dev_dbg(adapter->dev, "event: WMM status changed\n");
394                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_WMM_GET_STATUS,
395                                        0, 0, NULL, false);
396                 break;
397
398         case EVENT_RSSI_LOW:
399                 cfg80211_cqm_rssi_notify(priv->netdev,
400                                          NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
401                                          GFP_KERNEL);
402                 mwifiex_send_cmd(priv, HostCmd_CMD_RSSI_INFO,
403                                  HostCmd_ACT_GEN_GET, 0, NULL, false);
404                 priv->subsc_evt_rssi_state = RSSI_LOW_RECVD;
405                 dev_dbg(adapter->dev, "event: Beacon RSSI_LOW\n");
406                 break;
407         case EVENT_SNR_LOW:
408                 dev_dbg(adapter->dev, "event: Beacon SNR_LOW\n");
409                 break;
410         case EVENT_MAX_FAIL:
411                 dev_dbg(adapter->dev, "event: MAX_FAIL\n");
412                 break;
413         case EVENT_RSSI_HIGH:
414                 cfg80211_cqm_rssi_notify(priv->netdev,
415                                          NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
416                                          GFP_KERNEL);
417                 mwifiex_send_cmd(priv, HostCmd_CMD_RSSI_INFO,
418                                  HostCmd_ACT_GEN_GET, 0, NULL, false);
419                 priv->subsc_evt_rssi_state = RSSI_HIGH_RECVD;
420                 dev_dbg(adapter->dev, "event: Beacon RSSI_HIGH\n");
421                 break;
422         case EVENT_SNR_HIGH:
423                 dev_dbg(adapter->dev, "event: Beacon SNR_HIGH\n");
424                 break;
425         case EVENT_DATA_RSSI_LOW:
426                 dev_dbg(adapter->dev, "event: Data RSSI_LOW\n");
427                 break;
428         case EVENT_DATA_SNR_LOW:
429                 dev_dbg(adapter->dev, "event: Data SNR_LOW\n");
430                 break;
431         case EVENT_DATA_RSSI_HIGH:
432                 dev_dbg(adapter->dev, "event: Data RSSI_HIGH\n");
433                 break;
434         case EVENT_DATA_SNR_HIGH:
435                 dev_dbg(adapter->dev, "event: Data SNR_HIGH\n");
436                 break;
437         case EVENT_LINK_QUALITY:
438                 dev_dbg(adapter->dev, "event: Link Quality\n");
439                 break;
440         case EVENT_PRE_BEACON_LOST:
441                 dev_dbg(adapter->dev, "event: Pre-Beacon Lost\n");
442                 break;
443         case EVENT_IBSS_COALESCED:
444                 dev_dbg(adapter->dev, "event: IBSS_COALESCED\n");
445                 ret = mwifiex_send_cmd(priv,
446                                 HostCmd_CMD_802_11_IBSS_COALESCING_STATUS,
447                                 HostCmd_ACT_GEN_GET, 0, NULL, false);
448                 break;
449         case EVENT_ADDBA:
450                 dev_dbg(adapter->dev, "event: ADDBA Request\n");
451                 mwifiex_send_cmd(priv, HostCmd_CMD_11N_ADDBA_RSP,
452                                  HostCmd_ACT_GEN_SET, 0,
453                                  adapter->event_body, false);
454                 break;
455         case EVENT_DELBA:
456                 dev_dbg(adapter->dev, "event: DELBA Request\n");
457                 mwifiex_11n_delete_ba_stream(priv, adapter->event_body);
458                 break;
459         case EVENT_BA_STREAM_TIEMOUT:
460                 dev_dbg(adapter->dev, "event:  BA Stream timeout\n");
461                 mwifiex_11n_ba_stream_timeout(priv,
462                                               (struct host_cmd_ds_11n_batimeout
463                                                *)
464                                               adapter->event_body);
465                 break;
466         case EVENT_AMSDU_AGGR_CTRL:
467                 ctrl = le16_to_cpu(*(__le16 *)adapter->event_body);
468                 dev_dbg(adapter->dev, "event: AMSDU_AGGR_CTRL %d\n", ctrl);
469
470                 adapter->tx_buf_size =
471                                 min_t(u16, adapter->curr_tx_buf_size, ctrl);
472                 dev_dbg(adapter->dev, "event: tx_buf_size %d\n",
473                         adapter->tx_buf_size);
474                 break;
475
476         case EVENT_WEP_ICV_ERR:
477                 dev_dbg(adapter->dev, "event: WEP ICV error\n");
478                 break;
479
480         case EVENT_BW_CHANGE:
481                 dev_dbg(adapter->dev, "event: BW Change\n");
482                 break;
483
484         case EVENT_HOSTWAKE_STAIE:
485                 dev_dbg(adapter->dev, "event: HOSTWAKE_STAIE %d\n", eventcause);
486                 break;
487
488         case EVENT_REMAIN_ON_CHAN_EXPIRED:
489                 dev_dbg(adapter->dev, "event: Remain on channel expired\n");
490                 cfg80211_remain_on_channel_expired(&priv->wdev,
491                                                    priv->roc_cfg.cookie,
492                                                    &priv->roc_cfg.chan,
493                                                    GFP_ATOMIC);
494
495                 memset(&priv->roc_cfg, 0x00, sizeof(struct mwifiex_roc_cfg));
496
497                 break;
498
499         case EVENT_CHANNEL_SWITCH_ANN:
500                 dev_dbg(adapter->dev, "event: Channel Switch Announcement\n");
501                 priv->csa_expire_time =
502                                 jiffies + msecs_to_jiffies(DFS_CHAN_MOVE_TIME);
503                 priv->csa_chan = priv->curr_bss_params.bss_descriptor.channel;
504                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_DEAUTHENTICATE,
505                         HostCmd_ACT_GEN_SET, 0,
506                         priv->curr_bss_params.bss_descriptor.mac_address,
507                         false);
508                 break;
509
510         case EVENT_TDLS_GENERIC_EVENT:
511                 ret = mwifiex_parse_tdls_event(priv, adapter->event_skb);
512                 break;
513
514         case EVENT_TX_STATUS_REPORT:
515                 dev_dbg(adapter->dev, "event: TX_STATUS Report\n");
516                 mwifiex_parse_tx_status_event(priv, adapter->event_body);
517                 break;
518
519         case EVENT_CHANNEL_REPORT_RDY:
520                 dev_dbg(adapter->dev, "event: Channel Report\n");
521                 ret = mwifiex_11h_handle_chanrpt_ready(priv,
522                                                        adapter->event_skb);
523                 break;
524         case EVENT_RADAR_DETECTED:
525                 dev_dbg(adapter->dev, "event: Radar detected\n");
526                 ret = mwifiex_11h_handle_radar_detected(priv,
527                                                         adapter->event_skb);
528                 break;
529         default:
530                 dev_dbg(adapter->dev, "event: unknown event id: %#x\n",
531                         eventcause);
532                 break;
533         }
534
535         return ret;
536 }