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