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 */
43void
44mwifiex_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	eth_zero_addr(priv->cfg_bssid);
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
145static 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 */
233int 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					del_timer(&adapter->wakeup_timer);
316					break;
317				}
318				if (!mwifiex_send_null_packet
319					(priv,
320					 MWIFIEX_TxPD_POWER_MGMT_NULL_PACKET |
321					 MWIFIEX_TxPD_POWER_MGMT_LAST_PACKET))
322						adapter->ps_state =
323							PS_STATE_SLEEP;
324					return 0;
325			}
326		}
327		adapter->ps_state = PS_STATE_AWAKE;
328		adapter->pm_wakeup_card_req = false;
329		adapter->pm_wakeup_fw_try = false;
330		del_timer(&adapter->wakeup_timer);
331
332		break;
333
334	case EVENT_DEEP_SLEEP_AWAKE:
335		adapter->if_ops.wakeup_complete(adapter);
336		dev_dbg(adapter->dev, "event: DS_AWAKE\n");
337		if (adapter->is_deep_sleep)
338			adapter->is_deep_sleep = false;
339		break;
340
341	case EVENT_HS_ACT_REQ:
342		dev_dbg(adapter->dev, "event: HS_ACT_REQ\n");
343		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_HS_CFG_ENH,
344				       0, 0, NULL, false);
345		break;
346
347	case EVENT_MIC_ERR_UNICAST:
348		dev_dbg(adapter->dev, "event: UNICAST MIC ERROR\n");
349		cfg80211_michael_mic_failure(priv->netdev, priv->cfg_bssid,
350					     NL80211_KEYTYPE_PAIRWISE,
351					     -1, NULL, GFP_KERNEL);
352		break;
353
354	case EVENT_MIC_ERR_MULTICAST:
355		dev_dbg(adapter->dev, "event: MULTICAST MIC ERROR\n");
356		cfg80211_michael_mic_failure(priv->netdev, priv->cfg_bssid,
357					     NL80211_KEYTYPE_GROUP,
358					     -1, NULL, GFP_KERNEL);
359		break;
360	case EVENT_MIB_CHANGED:
361	case EVENT_INIT_DONE:
362		break;
363
364	case EVENT_ADHOC_BCN_LOST:
365		dev_dbg(adapter->dev, "event: ADHOC_BCN_LOST\n");
366		priv->adhoc_is_link_sensed = false;
367		mwifiex_clean_txrx(priv);
368		mwifiex_stop_net_dev_queue(priv->netdev, adapter);
369		if (netif_carrier_ok(priv->netdev))
370			netif_carrier_off(priv->netdev);
371		break;
372
373	case EVENT_BG_SCAN_REPORT:
374		dev_dbg(adapter->dev, "event: BGS_REPORT\n");
375		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_BG_SCAN_QUERY,
376				       HostCmd_ACT_GEN_GET, 0, NULL, false);
377		break;
378
379	case EVENT_PORT_RELEASE:
380		dev_dbg(adapter->dev, "event: PORT RELEASE\n");
381		break;
382
383	case EVENT_EXT_SCAN_REPORT:
384		dev_dbg(adapter->dev, "event: EXT_SCAN Report\n");
385		if (adapter->ext_scan)
386			ret = mwifiex_handle_event_ext_scan_report(priv,
387						adapter->event_skb->data);
388
389		break;
390
391	case EVENT_WMM_STATUS_CHANGE:
392		dev_dbg(adapter->dev, "event: WMM status changed\n");
393		ret = mwifiex_send_cmd(priv, HostCmd_CMD_WMM_GET_STATUS,
394				       0, 0, NULL, false);
395		break;
396
397	case EVENT_RSSI_LOW:
398		cfg80211_cqm_rssi_notify(priv->netdev,
399					 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
400					 GFP_KERNEL);
401		mwifiex_send_cmd(priv, HostCmd_CMD_RSSI_INFO,
402				 HostCmd_ACT_GEN_GET, 0, NULL, false);
403		priv->subsc_evt_rssi_state = RSSI_LOW_RECVD;
404		dev_dbg(adapter->dev, "event: Beacon RSSI_LOW\n");
405		break;
406	case EVENT_SNR_LOW:
407		dev_dbg(adapter->dev, "event: Beacon SNR_LOW\n");
408		break;
409	case EVENT_MAX_FAIL:
410		dev_dbg(adapter->dev, "event: MAX_FAIL\n");
411		break;
412	case EVENT_RSSI_HIGH:
413		cfg80211_cqm_rssi_notify(priv->netdev,
414					 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
415					 GFP_KERNEL);
416		mwifiex_send_cmd(priv, HostCmd_CMD_RSSI_INFO,
417				 HostCmd_ACT_GEN_GET, 0, NULL, false);
418		priv->subsc_evt_rssi_state = RSSI_HIGH_RECVD;
419		dev_dbg(adapter->dev, "event: Beacon RSSI_HIGH\n");
420		break;
421	case EVENT_SNR_HIGH:
422		dev_dbg(adapter->dev, "event: Beacon SNR_HIGH\n");
423		break;
424	case EVENT_DATA_RSSI_LOW:
425		dev_dbg(adapter->dev, "event: Data RSSI_LOW\n");
426		break;
427	case EVENT_DATA_SNR_LOW:
428		dev_dbg(adapter->dev, "event: Data SNR_LOW\n");
429		break;
430	case EVENT_DATA_RSSI_HIGH:
431		dev_dbg(adapter->dev, "event: Data RSSI_HIGH\n");
432		break;
433	case EVENT_DATA_SNR_HIGH:
434		dev_dbg(adapter->dev, "event: Data SNR_HIGH\n");
435		break;
436	case EVENT_LINK_QUALITY:
437		dev_dbg(adapter->dev, "event: Link Quality\n");
438		break;
439	case EVENT_PRE_BEACON_LOST:
440		dev_dbg(adapter->dev, "event: Pre-Beacon Lost\n");
441		break;
442	case EVENT_IBSS_COALESCED:
443		dev_dbg(adapter->dev, "event: IBSS_COALESCED\n");
444		ret = mwifiex_send_cmd(priv,
445				HostCmd_CMD_802_11_IBSS_COALESCING_STATUS,
446				HostCmd_ACT_GEN_GET, 0, NULL, false);
447		break;
448	case EVENT_ADDBA:
449		dev_dbg(adapter->dev, "event: ADDBA Request\n");
450		mwifiex_send_cmd(priv, HostCmd_CMD_11N_ADDBA_RSP,
451				 HostCmd_ACT_GEN_SET, 0,
452				 adapter->event_body, false);
453		break;
454	case EVENT_DELBA:
455		dev_dbg(adapter->dev, "event: DELBA Request\n");
456		mwifiex_11n_delete_ba_stream(priv, adapter->event_body);
457		break;
458	case EVENT_BA_STREAM_TIEMOUT:
459		dev_dbg(adapter->dev, "event:  BA Stream timeout\n");
460		mwifiex_11n_ba_stream_timeout(priv,
461					      (struct host_cmd_ds_11n_batimeout
462					       *)
463					      adapter->event_body);
464		break;
465	case EVENT_AMSDU_AGGR_CTRL:
466		ctrl = le16_to_cpu(*(__le16 *)adapter->event_body);
467		dev_dbg(adapter->dev, "event: AMSDU_AGGR_CTRL %d\n", ctrl);
468
469		adapter->tx_buf_size =
470				min_t(u16, adapter->curr_tx_buf_size, ctrl);
471		dev_dbg(adapter->dev, "event: tx_buf_size %d\n",
472			adapter->tx_buf_size);
473		break;
474
475	case EVENT_WEP_ICV_ERR:
476		dev_dbg(adapter->dev, "event: WEP ICV error\n");
477		break;
478
479	case EVENT_BW_CHANGE:
480		dev_dbg(adapter->dev, "event: BW Change\n");
481		break;
482
483	case EVENT_HOSTWAKE_STAIE:
484		dev_dbg(adapter->dev, "event: HOSTWAKE_STAIE %d\n", eventcause);
485		break;
486
487	case EVENT_REMAIN_ON_CHAN_EXPIRED:
488		dev_dbg(adapter->dev, "event: Remain on channel expired\n");
489		cfg80211_remain_on_channel_expired(&priv->wdev,
490						   priv->roc_cfg.cookie,
491						   &priv->roc_cfg.chan,
492						   GFP_ATOMIC);
493
494		memset(&priv->roc_cfg, 0x00, sizeof(struct mwifiex_roc_cfg));
495
496		break;
497
498	case EVENT_CHANNEL_SWITCH_ANN:
499		dev_dbg(adapter->dev, "event: Channel Switch Announcement\n");
500		priv->csa_expire_time =
501				jiffies + msecs_to_jiffies(DFS_CHAN_MOVE_TIME);
502		priv->csa_chan = priv->curr_bss_params.bss_descriptor.channel;
503		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_DEAUTHENTICATE,
504			HostCmd_ACT_GEN_SET, 0,
505			priv->curr_bss_params.bss_descriptor.mac_address,
506			false);
507		break;
508
509	case EVENT_TDLS_GENERIC_EVENT:
510		ret = mwifiex_parse_tdls_event(priv, adapter->event_skb);
511		break;
512
513	case EVENT_TX_STATUS_REPORT:
514		dev_dbg(adapter->dev, "event: TX_STATUS Report\n");
515		mwifiex_parse_tx_status_event(priv, adapter->event_body);
516		break;
517
518	case EVENT_CHANNEL_REPORT_RDY:
519		dev_dbg(adapter->dev, "event: Channel Report\n");
520		ret = mwifiex_11h_handle_chanrpt_ready(priv,
521						       adapter->event_skb);
522		break;
523	case EVENT_RADAR_DETECTED:
524		dev_dbg(adapter->dev, "event: Radar detected\n");
525		ret = mwifiex_11h_handle_radar_detected(priv,
526							adapter->event_skb);
527		break;
528	default:
529		dev_dbg(adapter->dev, "event: unknown event id: %#x\n",
530			eventcause);
531		break;
532	}
533
534	return ret;
535}
536