main.c revision 1b2df4073447234034e2329f0df584c6346a8ec3
1/*
2 * Copyright (c) 2004-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "core.h"
19#include "hif-ops.h"
20#include "cfg80211.h"
21#include "target.h"
22#include "debug.h"
23
24struct ath6kl_sta *ath6kl_find_sta(struct ath6kl_vif *vif, u8 *node_addr)
25{
26	struct ath6kl *ar = vif->ar;
27	struct ath6kl_sta *conn = NULL;
28	u8 i, max_conn;
29
30	max_conn = (vif->nw_type == AP_NETWORK) ? AP_MAX_NUM_STA : 0;
31
32	for (i = 0; i < max_conn; i++) {
33		if (memcmp(node_addr, ar->sta_list[i].mac, ETH_ALEN) == 0) {
34			conn = &ar->sta_list[i];
35			break;
36		}
37	}
38
39	return conn;
40}
41
42struct ath6kl_sta *ath6kl_find_sta_by_aid(struct ath6kl *ar, u8 aid)
43{
44	struct ath6kl_sta *conn = NULL;
45	u8 ctr;
46
47	for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
48		if (ar->sta_list[ctr].aid == aid) {
49			conn = &ar->sta_list[ctr];
50			break;
51		}
52	}
53	return conn;
54}
55
56static void ath6kl_add_new_sta(struct ath6kl_vif *vif, u8 *mac, u16 aid,
57			       u8 *wpaie, size_t ielen, u8 keymgmt,
58			       u8 ucipher, u8 auth, u8 apsd_info)
59{
60	struct ath6kl *ar = vif->ar;
61	struct ath6kl_sta *sta;
62	u8 free_slot;
63
64	free_slot = aid - 1;
65
66	sta = &ar->sta_list[free_slot];
67	memcpy(sta->mac, mac, ETH_ALEN);
68	if (ielen <= ATH6KL_MAX_IE)
69		memcpy(sta->wpa_ie, wpaie, ielen);
70	sta->aid = aid;
71	sta->keymgmt = keymgmt;
72	sta->ucipher = ucipher;
73	sta->auth = auth;
74	sta->apsd_info = apsd_info;
75
76	ar->sta_list_index = ar->sta_list_index | (1 << free_slot);
77	ar->ap_stats.sta[free_slot].aid = cpu_to_le32(aid);
78	aggr_conn_init(vif, vif->aggr_cntxt, sta->aggr_conn);
79}
80
81static void ath6kl_sta_cleanup(struct ath6kl *ar, u8 i)
82{
83	struct ath6kl_sta *sta = &ar->sta_list[i];
84
85	/* empty the queued pkts in the PS queue if any */
86	spin_lock_bh(&sta->psq_lock);
87	skb_queue_purge(&sta->psq);
88	skb_queue_purge(&sta->apsdq);
89	spin_unlock_bh(&sta->psq_lock);
90
91	memset(&ar->ap_stats.sta[sta->aid - 1], 0,
92	       sizeof(struct wmi_per_sta_stat));
93	memset(sta->mac, 0, ETH_ALEN);
94	memset(sta->wpa_ie, 0, ATH6KL_MAX_IE);
95	sta->aid = 0;
96	sta->sta_flags = 0;
97
98	ar->sta_list_index = ar->sta_list_index & ~(1 << i);
99	aggr_reset_state(sta->aggr_conn);
100}
101
102static u8 ath6kl_remove_sta(struct ath6kl *ar, u8 *mac, u16 reason)
103{
104	u8 i, removed = 0;
105
106	if (is_zero_ether_addr(mac))
107		return removed;
108
109	if (is_broadcast_ether_addr(mac)) {
110		ath6kl_dbg(ATH6KL_DBG_TRC, "deleting all station\n");
111
112		for (i = 0; i < AP_MAX_NUM_STA; i++) {
113			if (!is_zero_ether_addr(ar->sta_list[i].mac)) {
114				ath6kl_sta_cleanup(ar, i);
115				removed = 1;
116			}
117		}
118	} else {
119		for (i = 0; i < AP_MAX_NUM_STA; i++) {
120			if (memcmp(ar->sta_list[i].mac, mac, ETH_ALEN) == 0) {
121				ath6kl_dbg(ATH6KL_DBG_TRC,
122					   "deleting station %pM aid=%d reason=%d\n",
123					   mac, ar->sta_list[i].aid, reason);
124				ath6kl_sta_cleanup(ar, i);
125				removed = 1;
126				break;
127			}
128		}
129	}
130
131	return removed;
132}
133
134enum htc_endpoint_id ath6kl_ac2_endpoint_id(void *devt, u8 ac)
135{
136	struct ath6kl *ar = devt;
137	return ar->ac2ep_map[ac];
138}
139
140struct ath6kl_cookie *ath6kl_alloc_cookie(struct ath6kl *ar)
141{
142	struct ath6kl_cookie *cookie;
143
144	cookie = ar->cookie_list;
145	if (cookie != NULL) {
146		ar->cookie_list = cookie->arc_list_next;
147		ar->cookie_count--;
148	}
149
150	return cookie;
151}
152
153void ath6kl_cookie_init(struct ath6kl *ar)
154{
155	u32 i;
156
157	ar->cookie_list = NULL;
158	ar->cookie_count = 0;
159
160	memset(ar->cookie_mem, 0, sizeof(ar->cookie_mem));
161
162	for (i = 0; i < MAX_COOKIE_NUM; i++)
163		ath6kl_free_cookie(ar, &ar->cookie_mem[i]);
164}
165
166void ath6kl_cookie_cleanup(struct ath6kl *ar)
167{
168	ar->cookie_list = NULL;
169	ar->cookie_count = 0;
170}
171
172void ath6kl_free_cookie(struct ath6kl *ar, struct ath6kl_cookie *cookie)
173{
174	/* Insert first */
175
176	if (!ar || !cookie)
177		return;
178
179	cookie->arc_list_next = ar->cookie_list;
180	ar->cookie_list = cookie;
181	ar->cookie_count++;
182}
183
184/*
185 * Read from the hardware through its diagnostic window. No cooperation
186 * from the firmware is required for this.
187 */
188int ath6kl_diag_read32(struct ath6kl *ar, u32 address, u32 *value)
189{
190	int ret;
191
192	ret = ath6kl_hif_diag_read32(ar, address, value);
193	if (ret) {
194		ath6kl_warn("failed to read32 through diagnose window: %d\n",
195			    ret);
196		return ret;
197	}
198
199	return 0;
200}
201
202/*
203 * Write to the ATH6KL through its diagnostic window. No cooperation from
204 * the Target is required for this.
205 */
206int ath6kl_diag_write32(struct ath6kl *ar, u32 address, __le32 value)
207{
208	int ret;
209
210	ret = ath6kl_hif_diag_write32(ar, address, value);
211
212	if (ret) {
213		ath6kl_err("failed to write 0x%x during diagnose window to 0x%d\n",
214			   address, value);
215		return ret;
216	}
217
218	return 0;
219}
220
221int ath6kl_diag_read(struct ath6kl *ar, u32 address, void *data, u32 length)
222{
223	u32 count, *buf = data;
224	int ret;
225
226	if (WARN_ON(length % 4))
227		return -EINVAL;
228
229	for (count = 0; count < length / 4; count++, address += 4) {
230		ret = ath6kl_diag_read32(ar, address, &buf[count]);
231		if (ret)
232			return ret;
233	}
234
235	return 0;
236}
237
238int ath6kl_diag_write(struct ath6kl *ar, u32 address, void *data, u32 length)
239{
240	u32 count;
241	__le32 *buf = data;
242	int ret;
243
244	if (WARN_ON(length % 4))
245		return -EINVAL;
246
247	for (count = 0; count < length / 4; count++, address += 4) {
248		ret = ath6kl_diag_write32(ar, address, buf[count]);
249		if (ret)
250			return ret;
251	}
252
253	return 0;
254}
255
256int ath6kl_read_fwlogs(struct ath6kl *ar)
257{
258	struct ath6kl_dbglog_hdr debug_hdr;
259	struct ath6kl_dbglog_buf debug_buf;
260	u32 address, length, dropped, firstbuf, debug_hdr_addr;
261	int ret, loop;
262	u8 *buf;
263
264	buf = kmalloc(ATH6KL_FWLOG_PAYLOAD_SIZE, GFP_KERNEL);
265	if (!buf)
266		return -ENOMEM;
267
268	address = TARG_VTOP(ar->target_type,
269			    ath6kl_get_hi_item_addr(ar,
270						    HI_ITEM(hi_dbglog_hdr)));
271
272	ret = ath6kl_diag_read32(ar, address, &debug_hdr_addr);
273	if (ret)
274		goto out;
275
276	/* Get the contents of the ring buffer */
277	if (debug_hdr_addr == 0) {
278		ath6kl_warn("Invalid address for debug_hdr_addr\n");
279		ret = -EINVAL;
280		goto out;
281	}
282
283	address = TARG_VTOP(ar->target_type, debug_hdr_addr);
284	ath6kl_diag_read(ar, address, &debug_hdr, sizeof(debug_hdr));
285
286	address = TARG_VTOP(ar->target_type,
287			    le32_to_cpu(debug_hdr.dbuf_addr));
288	firstbuf = address;
289	dropped = le32_to_cpu(debug_hdr.dropped);
290	ath6kl_diag_read(ar, address, &debug_buf, sizeof(debug_buf));
291
292	loop = 100;
293
294	do {
295		address = TARG_VTOP(ar->target_type,
296				    le32_to_cpu(debug_buf.buffer_addr));
297		length = le32_to_cpu(debug_buf.length);
298
299		if (length != 0 && (le32_to_cpu(debug_buf.length) <=
300				    le32_to_cpu(debug_buf.bufsize))) {
301			length = ALIGN(length, 4);
302
303			ret = ath6kl_diag_read(ar, address,
304					       buf, length);
305			if (ret)
306				goto out;
307
308			ath6kl_debug_fwlog_event(ar, buf, length);
309		}
310
311		address = TARG_VTOP(ar->target_type,
312				    le32_to_cpu(debug_buf.next));
313		ath6kl_diag_read(ar, address, &debug_buf, sizeof(debug_buf));
314		if (ret)
315			goto out;
316
317		loop--;
318
319		if (WARN_ON(loop == 0)) {
320			ret = -ETIMEDOUT;
321			goto out;
322		}
323	} while (address != firstbuf);
324
325out:
326	kfree(buf);
327
328	return ret;
329}
330
331/* FIXME: move to a better place, target.h? */
332#define AR6003_RESET_CONTROL_ADDRESS 0x00004000
333#define AR6004_RESET_CONTROL_ADDRESS 0x00004000
334
335void ath6kl_reset_device(struct ath6kl *ar, u32 target_type,
336			 bool wait_fot_compltn, bool cold_reset)
337{
338	int status = 0;
339	u32 address;
340	__le32 data;
341
342	if (target_type != TARGET_TYPE_AR6003 &&
343		target_type != TARGET_TYPE_AR6004)
344		return;
345
346	data = cold_reset ? cpu_to_le32(RESET_CONTROL_COLD_RST) :
347			    cpu_to_le32(RESET_CONTROL_MBOX_RST);
348
349	switch (target_type) {
350	case TARGET_TYPE_AR6003:
351		address = AR6003_RESET_CONTROL_ADDRESS;
352		break;
353	case TARGET_TYPE_AR6004:
354		address = AR6004_RESET_CONTROL_ADDRESS;
355		break;
356	}
357
358	status = ath6kl_diag_write32(ar, address, data);
359
360	if (status)
361		ath6kl_err("failed to reset target\n");
362}
363
364static void ath6kl_install_static_wep_keys(struct ath6kl_vif *vif)
365{
366	u8 index;
367	u8 keyusage;
368
369	for (index = 0; index <= WMI_MAX_KEY_INDEX; index++) {
370		if (vif->wep_key_list[index].key_len) {
371			keyusage = GROUP_USAGE;
372			if (index == vif->def_txkey_index)
373				keyusage |= TX_USAGE;
374
375			ath6kl_wmi_addkey_cmd(vif->ar->wmi, vif->fw_vif_idx,
376					      index,
377					      WEP_CRYPT,
378					      keyusage,
379					      vif->wep_key_list[index].key_len,
380					      NULL, 0,
381					      vif->wep_key_list[index].key,
382					      KEY_OP_INIT_VAL, NULL,
383					      NO_SYNC_WMIFLAG);
384		}
385	}
386}
387
388void ath6kl_connect_ap_mode_bss(struct ath6kl_vif *vif, u16 channel)
389{
390	struct ath6kl *ar = vif->ar;
391	struct ath6kl_req_key *ik;
392	int res;
393	u8 key_rsc[ATH6KL_KEY_SEQ_LEN];
394
395	ik = &ar->ap_mode_bkey;
396
397	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "AP mode started on %u MHz\n", channel);
398
399	switch (vif->auth_mode) {
400	case NONE_AUTH:
401		if (vif->prwise_crypto == WEP_CRYPT)
402			ath6kl_install_static_wep_keys(vif);
403		if (!ik->valid || ik->key_type != WAPI_CRYPT)
404			break;
405		/* for WAPI, we need to set the delayed group key, continue: */
406	case WPA_PSK_AUTH:
407	case WPA2_PSK_AUTH:
408	case (WPA_PSK_AUTH | WPA2_PSK_AUTH):
409		if (!ik->valid)
410			break;
411
412		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "Delayed addkey for "
413			   "the initial group key for AP mode\n");
414		memset(key_rsc, 0, sizeof(key_rsc));
415		res = ath6kl_wmi_addkey_cmd(
416			ar->wmi, vif->fw_vif_idx, ik->key_index, ik->key_type,
417			GROUP_USAGE, ik->key_len, key_rsc, ATH6KL_KEY_SEQ_LEN,
418			ik->key,
419			KEY_OP_INIT_VAL, NULL, SYNC_BOTH_WMIFLAG);
420		if (res) {
421			ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "Delayed "
422				   "addkey failed: %d\n", res);
423		}
424		break;
425	}
426
427	ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx, NONE_BSS_FILTER, 0);
428	set_bit(CONNECTED, &vif->flags);
429	netif_carrier_on(vif->ndev);
430}
431
432void ath6kl_connect_ap_mode_sta(struct ath6kl_vif *vif, u16 aid, u8 *mac_addr,
433				u8 keymgmt, u8 ucipher, u8 auth,
434				u8 assoc_req_len, u8 *assoc_info, u8 apsd_info)
435{
436	u8 *ies = NULL, *wpa_ie = NULL, *pos;
437	size_t ies_len = 0;
438	struct station_info sinfo;
439
440	ath6kl_dbg(ATH6KL_DBG_TRC, "new station %pM aid=%d\n", mac_addr, aid);
441
442	if (assoc_req_len > sizeof(struct ieee80211_hdr_3addr)) {
443		struct ieee80211_mgmt *mgmt =
444			(struct ieee80211_mgmt *) assoc_info;
445		if (ieee80211_is_assoc_req(mgmt->frame_control) &&
446		    assoc_req_len >= sizeof(struct ieee80211_hdr_3addr) +
447		    sizeof(mgmt->u.assoc_req)) {
448			ies = mgmt->u.assoc_req.variable;
449			ies_len = assoc_info + assoc_req_len - ies;
450		} else if (ieee80211_is_reassoc_req(mgmt->frame_control) &&
451			   assoc_req_len >= sizeof(struct ieee80211_hdr_3addr)
452			   + sizeof(mgmt->u.reassoc_req)) {
453			ies = mgmt->u.reassoc_req.variable;
454			ies_len = assoc_info + assoc_req_len - ies;
455		}
456	}
457
458	pos = ies;
459	while (pos && pos + 1 < ies + ies_len) {
460		if (pos + 2 + pos[1] > ies + ies_len)
461			break;
462		if (pos[0] == WLAN_EID_RSN)
463			wpa_ie = pos; /* RSN IE */
464		else if (pos[0] == WLAN_EID_VENDOR_SPECIFIC &&
465			 pos[1] >= 4 &&
466			 pos[2] == 0x00 && pos[3] == 0x50 && pos[4] == 0xf2) {
467			if (pos[5] == 0x01)
468				wpa_ie = pos; /* WPA IE */
469			else if (pos[5] == 0x04) {
470				wpa_ie = pos; /* WPS IE */
471				break; /* overrides WPA/RSN IE */
472			}
473		} else if (pos[0] == 0x44 && wpa_ie == NULL) {
474			/*
475			 * Note: WAPI Parameter Set IE re-uses Element ID that
476			 * was officially allocated for BSS AC Access Delay. As
477			 * such, we need to be a bit more careful on when
478			 * parsing the frame. However, BSS AC Access Delay
479			 * element is not supposed to be included in
480			 * (Re)Association Request frames, so this should not
481			 * cause problems.
482			 */
483			wpa_ie = pos; /* WAPI IE */
484			break;
485		}
486		pos += 2 + pos[1];
487	}
488
489	ath6kl_add_new_sta(vif, mac_addr, aid, wpa_ie,
490			   wpa_ie ? 2 + wpa_ie[1] : 0,
491			   keymgmt, ucipher, auth, apsd_info);
492
493	/* send event to application */
494	memset(&sinfo, 0, sizeof(sinfo));
495
496	/* TODO: sinfo.generation */
497
498	sinfo.assoc_req_ies = ies;
499	sinfo.assoc_req_ies_len = ies_len;
500	sinfo.filled |= STATION_INFO_ASSOC_REQ_IES;
501
502	cfg80211_new_sta(vif->ndev, mac_addr, &sinfo, GFP_KERNEL);
503
504	netif_wake_queue(vif->ndev);
505}
506
507void disconnect_timer_handler(unsigned long ptr)
508{
509	struct net_device *dev = (struct net_device *)ptr;
510	struct ath6kl_vif *vif = netdev_priv(dev);
511
512	ath6kl_init_profile_info(vif);
513	ath6kl_disconnect(vif);
514}
515
516void ath6kl_disconnect(struct ath6kl_vif *vif)
517{
518	if (test_bit(CONNECTED, &vif->flags) ||
519	    test_bit(CONNECT_PEND, &vif->flags)) {
520		ath6kl_wmi_disconnect_cmd(vif->ar->wmi, vif->fw_vif_idx);
521		/*
522		 * Disconnect command is issued, clear the connect pending
523		 * flag. The connected flag will be cleared in
524		 * disconnect event notification.
525		 */
526		clear_bit(CONNECT_PEND, &vif->flags);
527	}
528}
529
530/* WMI Event handlers */
531
532void ath6kl_ready_event(void *devt, u8 *datap, u32 sw_ver, u32 abi_ver)
533{
534	struct ath6kl *ar = devt;
535
536	memcpy(ar->mac_addr, datap, ETH_ALEN);
537	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: mac addr = %pM\n",
538		   __func__, ar->mac_addr);
539
540	ar->version.wlan_ver = sw_ver;
541	ar->version.abi_ver = abi_ver;
542
543	snprintf(ar->wiphy->fw_version,
544		 sizeof(ar->wiphy->fw_version),
545		 "%u.%u.%u.%u",
546		 (ar->version.wlan_ver & 0xf0000000) >> 28,
547		 (ar->version.wlan_ver & 0x0f000000) >> 24,
548		 (ar->version.wlan_ver & 0x00ff0000) >> 16,
549		 (ar->version.wlan_ver & 0x0000ffff));
550
551	/* indicate to the waiting thread that the ready event was received */
552	set_bit(WMI_READY, &ar->flag);
553	wake_up(&ar->event_wq);
554}
555
556void ath6kl_scan_complete_evt(struct ath6kl_vif *vif, int status)
557{
558	struct ath6kl *ar = vif->ar;
559	bool aborted = false;
560
561	if (status != WMI_SCAN_STATUS_SUCCESS)
562		aborted = true;
563
564	ath6kl_cfg80211_scan_complete_event(vif, aborted);
565
566	if (!ar->usr_bss_filter) {
567		clear_bit(CLEAR_BSSFILTER_ON_BEACON, &vif->flags);
568		ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx,
569					 NONE_BSS_FILTER, 0);
570	}
571
572	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "scan complete: %d\n", status);
573}
574
575void ath6kl_connect_event(struct ath6kl_vif *vif, u16 channel, u8 *bssid,
576			  u16 listen_int, u16 beacon_int,
577			  enum network_type net_type, u8 beacon_ie_len,
578			  u8 assoc_req_len, u8 assoc_resp_len,
579			  u8 *assoc_info)
580{
581	struct ath6kl *ar = vif->ar;
582
583	ath6kl_cfg80211_connect_event(vif, channel, bssid,
584				      listen_int, beacon_int,
585				      net_type, beacon_ie_len,
586				      assoc_req_len, assoc_resp_len,
587				      assoc_info);
588
589	memcpy(vif->bssid, bssid, sizeof(vif->bssid));
590	vif->bss_ch = channel;
591
592	if ((vif->nw_type == INFRA_NETWORK)) {
593		ar->listen_intvl_b = listen_int;
594		ath6kl_wmi_listeninterval_cmd(ar->wmi, vif->fw_vif_idx,
595					      0, ar->listen_intvl_b);
596	}
597
598	netif_wake_queue(vif->ndev);
599
600	/* Update connect & link status atomically */
601	spin_lock_bh(&vif->if_lock);
602	set_bit(CONNECTED, &vif->flags);
603	clear_bit(CONNECT_PEND, &vif->flags);
604	netif_carrier_on(vif->ndev);
605	spin_unlock_bh(&vif->if_lock);
606
607	aggr_reset_state(vif->aggr_cntxt->aggr_conn);
608	vif->reconnect_flag = 0;
609
610	if ((vif->nw_type == ADHOC_NETWORK) && ar->ibss_ps_enable) {
611		memset(ar->node_map, 0, sizeof(ar->node_map));
612		ar->node_num = 0;
613		ar->next_ep_id = ENDPOINT_2;
614	}
615
616	if (!ar->usr_bss_filter) {
617		set_bit(CLEAR_BSSFILTER_ON_BEACON, &vif->flags);
618		ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx,
619					 CURRENT_BSS_FILTER, 0);
620	}
621}
622
623void ath6kl_tkip_micerr_event(struct ath6kl_vif *vif, u8 keyid, bool ismcast)
624{
625	struct ath6kl_sta *sta;
626	struct ath6kl *ar = vif->ar;
627	u8 tsc[6];
628
629	/*
630	 * For AP case, keyid will have aid of STA which sent pkt with
631	 * MIC error. Use this aid to get MAC & send it to hostapd.
632	 */
633	if (vif->nw_type == AP_NETWORK) {
634		sta = ath6kl_find_sta_by_aid(ar, (keyid >> 2));
635		if (!sta)
636			return;
637
638		ath6kl_dbg(ATH6KL_DBG_TRC,
639			   "ap tkip mic error received from aid=%d\n", keyid);
640
641		memset(tsc, 0, sizeof(tsc)); /* FIX: get correct TSC */
642		cfg80211_michael_mic_failure(vif->ndev, sta->mac,
643					     NL80211_KEYTYPE_PAIRWISE, keyid,
644					     tsc, GFP_KERNEL);
645	} else
646		ath6kl_cfg80211_tkip_micerr_event(vif, keyid, ismcast);
647
648}
649
650static void ath6kl_update_target_stats(struct ath6kl_vif *vif, u8 *ptr, u32 len)
651{
652	struct wmi_target_stats *tgt_stats =
653		(struct wmi_target_stats *) ptr;
654	struct ath6kl *ar = vif->ar;
655	struct target_stats *stats = &vif->target_stats;
656	struct tkip_ccmp_stats *ccmp_stats;
657	u8 ac;
658
659	if (len < sizeof(*tgt_stats))
660		return;
661
662	ath6kl_dbg(ATH6KL_DBG_TRC, "updating target stats\n");
663
664	stats->tx_pkt += le32_to_cpu(tgt_stats->stats.tx.pkt);
665	stats->tx_byte += le32_to_cpu(tgt_stats->stats.tx.byte);
666	stats->tx_ucast_pkt += le32_to_cpu(tgt_stats->stats.tx.ucast_pkt);
667	stats->tx_ucast_byte += le32_to_cpu(tgt_stats->stats.tx.ucast_byte);
668	stats->tx_mcast_pkt += le32_to_cpu(tgt_stats->stats.tx.mcast_pkt);
669	stats->tx_mcast_byte += le32_to_cpu(tgt_stats->stats.tx.mcast_byte);
670	stats->tx_bcast_pkt  += le32_to_cpu(tgt_stats->stats.tx.bcast_pkt);
671	stats->tx_bcast_byte += le32_to_cpu(tgt_stats->stats.tx.bcast_byte);
672	stats->tx_rts_success_cnt +=
673		le32_to_cpu(tgt_stats->stats.tx.rts_success_cnt);
674
675	for (ac = 0; ac < WMM_NUM_AC; ac++)
676		stats->tx_pkt_per_ac[ac] +=
677			le32_to_cpu(tgt_stats->stats.tx.pkt_per_ac[ac]);
678
679	stats->tx_err += le32_to_cpu(tgt_stats->stats.tx.err);
680	stats->tx_fail_cnt += le32_to_cpu(tgt_stats->stats.tx.fail_cnt);
681	stats->tx_retry_cnt += le32_to_cpu(tgt_stats->stats.tx.retry_cnt);
682	stats->tx_mult_retry_cnt +=
683		le32_to_cpu(tgt_stats->stats.tx.mult_retry_cnt);
684	stats->tx_rts_fail_cnt +=
685		le32_to_cpu(tgt_stats->stats.tx.rts_fail_cnt);
686	stats->tx_ucast_rate =
687	    ath6kl_wmi_get_rate(a_sle32_to_cpu(tgt_stats->stats.tx.ucast_rate));
688
689	stats->rx_pkt += le32_to_cpu(tgt_stats->stats.rx.pkt);
690	stats->rx_byte += le32_to_cpu(tgt_stats->stats.rx.byte);
691	stats->rx_ucast_pkt += le32_to_cpu(tgt_stats->stats.rx.ucast_pkt);
692	stats->rx_ucast_byte += le32_to_cpu(tgt_stats->stats.rx.ucast_byte);
693	stats->rx_mcast_pkt += le32_to_cpu(tgt_stats->stats.rx.mcast_pkt);
694	stats->rx_mcast_byte += le32_to_cpu(tgt_stats->stats.rx.mcast_byte);
695	stats->rx_bcast_pkt += le32_to_cpu(tgt_stats->stats.rx.bcast_pkt);
696	stats->rx_bcast_byte += le32_to_cpu(tgt_stats->stats.rx.bcast_byte);
697	stats->rx_frgment_pkt += le32_to_cpu(tgt_stats->stats.rx.frgment_pkt);
698	stats->rx_err += le32_to_cpu(tgt_stats->stats.rx.err);
699	stats->rx_crc_err += le32_to_cpu(tgt_stats->stats.rx.crc_err);
700	stats->rx_key_cache_miss +=
701		le32_to_cpu(tgt_stats->stats.rx.key_cache_miss);
702	stats->rx_decrypt_err += le32_to_cpu(tgt_stats->stats.rx.decrypt_err);
703	stats->rx_dupl_frame += le32_to_cpu(tgt_stats->stats.rx.dupl_frame);
704	stats->rx_ucast_rate =
705	    ath6kl_wmi_get_rate(a_sle32_to_cpu(tgt_stats->stats.rx.ucast_rate));
706
707	ccmp_stats = &tgt_stats->stats.tkip_ccmp_stats;
708
709	stats->tkip_local_mic_fail +=
710		le32_to_cpu(ccmp_stats->tkip_local_mic_fail);
711	stats->tkip_cnter_measures_invoked +=
712		le32_to_cpu(ccmp_stats->tkip_cnter_measures_invoked);
713	stats->tkip_fmt_err += le32_to_cpu(ccmp_stats->tkip_fmt_err);
714
715	stats->ccmp_fmt_err += le32_to_cpu(ccmp_stats->ccmp_fmt_err);
716	stats->ccmp_replays += le32_to_cpu(ccmp_stats->ccmp_replays);
717
718	stats->pwr_save_fail_cnt +=
719		le32_to_cpu(tgt_stats->pm_stats.pwr_save_failure_cnt);
720	stats->noise_floor_calib =
721		a_sle32_to_cpu(tgt_stats->noise_floor_calib);
722
723	stats->cs_bmiss_cnt +=
724		le32_to_cpu(tgt_stats->cserv_stats.cs_bmiss_cnt);
725	stats->cs_low_rssi_cnt +=
726		le32_to_cpu(tgt_stats->cserv_stats.cs_low_rssi_cnt);
727	stats->cs_connect_cnt +=
728		le16_to_cpu(tgt_stats->cserv_stats.cs_connect_cnt);
729	stats->cs_discon_cnt +=
730		le16_to_cpu(tgt_stats->cserv_stats.cs_discon_cnt);
731
732	stats->cs_ave_beacon_rssi =
733		a_sle16_to_cpu(tgt_stats->cserv_stats.cs_ave_beacon_rssi);
734
735	stats->cs_last_roam_msec =
736		tgt_stats->cserv_stats.cs_last_roam_msec;
737	stats->cs_snr = tgt_stats->cserv_stats.cs_snr;
738	stats->cs_rssi = a_sle16_to_cpu(tgt_stats->cserv_stats.cs_rssi);
739
740	stats->lq_val = le32_to_cpu(tgt_stats->lq_val);
741
742	stats->wow_pkt_dropped +=
743		le32_to_cpu(tgt_stats->wow_stats.wow_pkt_dropped);
744	stats->wow_host_pkt_wakeups +=
745		tgt_stats->wow_stats.wow_host_pkt_wakeups;
746	stats->wow_host_evt_wakeups +=
747		tgt_stats->wow_stats.wow_host_evt_wakeups;
748	stats->wow_evt_discarded +=
749		le16_to_cpu(tgt_stats->wow_stats.wow_evt_discarded);
750
751	if (test_bit(STATS_UPDATE_PEND, &vif->flags)) {
752		clear_bit(STATS_UPDATE_PEND, &vif->flags);
753		wake_up(&ar->event_wq);
754	}
755}
756
757static void ath6kl_add_le32(__le32 *var, __le32 val)
758{
759	*var = cpu_to_le32(le32_to_cpu(*var) + le32_to_cpu(val));
760}
761
762void ath6kl_tgt_stats_event(struct ath6kl_vif *vif, u8 *ptr, u32 len)
763{
764	struct wmi_ap_mode_stat *p = (struct wmi_ap_mode_stat *) ptr;
765	struct ath6kl *ar = vif->ar;
766	struct wmi_ap_mode_stat *ap = &ar->ap_stats;
767	struct wmi_per_sta_stat *st_ap, *st_p;
768	u8 ac;
769
770	if (vif->nw_type == AP_NETWORK) {
771		if (len < sizeof(*p))
772			return;
773
774		for (ac = 0; ac < AP_MAX_NUM_STA; ac++) {
775			st_ap = &ap->sta[ac];
776			st_p = &p->sta[ac];
777
778			ath6kl_add_le32(&st_ap->tx_bytes, st_p->tx_bytes);
779			ath6kl_add_le32(&st_ap->tx_pkts, st_p->tx_pkts);
780			ath6kl_add_le32(&st_ap->tx_error, st_p->tx_error);
781			ath6kl_add_le32(&st_ap->tx_discard, st_p->tx_discard);
782			ath6kl_add_le32(&st_ap->rx_bytes, st_p->rx_bytes);
783			ath6kl_add_le32(&st_ap->rx_pkts, st_p->rx_pkts);
784			ath6kl_add_le32(&st_ap->rx_error, st_p->rx_error);
785			ath6kl_add_le32(&st_ap->rx_discard, st_p->rx_discard);
786		}
787
788	} else {
789		ath6kl_update_target_stats(vif, ptr, len);
790	}
791}
792
793void ath6kl_wakeup_event(void *dev)
794{
795	struct ath6kl *ar = (struct ath6kl *) dev;
796
797	wake_up(&ar->event_wq);
798}
799
800void ath6kl_txpwr_rx_evt(void *devt, u8 tx_pwr)
801{
802	struct ath6kl *ar = (struct ath6kl *) devt;
803
804	ar->tx_pwr = tx_pwr;
805	wake_up(&ar->event_wq);
806}
807
808void ath6kl_pspoll_event(struct ath6kl_vif *vif, u8 aid)
809{
810	struct ath6kl_sta *conn;
811	struct sk_buff *skb;
812	bool psq_empty = false;
813	struct ath6kl *ar = vif->ar;
814
815	conn = ath6kl_find_sta_by_aid(ar, aid);
816
817	if (!conn)
818		return;
819	/*
820	 * Send out a packet queued on ps queue. When the ps queue
821	 * becomes empty update the PVB for this station.
822	 */
823	spin_lock_bh(&conn->psq_lock);
824	psq_empty  = skb_queue_empty(&conn->psq);
825	spin_unlock_bh(&conn->psq_lock);
826
827	if (psq_empty)
828		/* TODO: Send out a NULL data frame */
829		return;
830
831	spin_lock_bh(&conn->psq_lock);
832	skb = skb_dequeue(&conn->psq);
833	spin_unlock_bh(&conn->psq_lock);
834
835	conn->sta_flags |= STA_PS_POLLED;
836	ath6kl_data_tx(skb, vif->ndev);
837	conn->sta_flags &= ~STA_PS_POLLED;
838
839	spin_lock_bh(&conn->psq_lock);
840	psq_empty  = skb_queue_empty(&conn->psq);
841	spin_unlock_bh(&conn->psq_lock);
842
843	if (psq_empty)
844		ath6kl_wmi_set_pvb_cmd(ar->wmi, vif->fw_vif_idx, conn->aid, 0);
845}
846
847void ath6kl_dtimexpiry_event(struct ath6kl_vif *vif)
848{
849	bool mcastq_empty = false;
850	struct sk_buff *skb;
851	struct ath6kl *ar = vif->ar;
852
853	/*
854	 * If there are no associated STAs, ignore the DTIM expiry event.
855	 * There can be potential race conditions where the last associated
856	 * STA may disconnect & before the host could clear the 'Indicate
857	 * DTIM' request to the firmware, the firmware would have just
858	 * indicated a DTIM expiry event. The race is between 'clear DTIM
859	 * expiry cmd' going from the host to the firmware & the DTIM
860	 * expiry event happening from the firmware to the host.
861	 */
862	if (!ar->sta_list_index)
863		return;
864
865	spin_lock_bh(&ar->mcastpsq_lock);
866	mcastq_empty = skb_queue_empty(&ar->mcastpsq);
867	spin_unlock_bh(&ar->mcastpsq_lock);
868
869	if (mcastq_empty)
870		return;
871
872	/* set the STA flag to dtim_expired for the frame to go out */
873	set_bit(DTIM_EXPIRED, &vif->flags);
874
875	spin_lock_bh(&ar->mcastpsq_lock);
876	while ((skb = skb_dequeue(&ar->mcastpsq)) != NULL) {
877		spin_unlock_bh(&ar->mcastpsq_lock);
878
879		ath6kl_data_tx(skb, vif->ndev);
880
881		spin_lock_bh(&ar->mcastpsq_lock);
882	}
883	spin_unlock_bh(&ar->mcastpsq_lock);
884
885	clear_bit(DTIM_EXPIRED, &vif->flags);
886
887	/* clear the LSB of the BitMapCtl field of the TIM IE */
888	ath6kl_wmi_set_pvb_cmd(ar->wmi, vif->fw_vif_idx, MCAST_AID, 0);
889}
890
891void ath6kl_disconnect_event(struct ath6kl_vif *vif, u8 reason, u8 *bssid,
892			     u8 assoc_resp_len, u8 *assoc_info,
893			     u16 prot_reason_status)
894{
895	struct ath6kl *ar = vif->ar;
896
897	if (vif->nw_type == AP_NETWORK) {
898		if (!ath6kl_remove_sta(ar, bssid, prot_reason_status))
899			return;
900
901		/* if no more associated STAs, empty the mcast PS q */
902		if (ar->sta_list_index == 0) {
903			spin_lock_bh(&ar->mcastpsq_lock);
904			skb_queue_purge(&ar->mcastpsq);
905			spin_unlock_bh(&ar->mcastpsq_lock);
906
907			/* clear the LSB of the TIM IE's BitMapCtl field */
908			if (test_bit(WMI_READY, &ar->flag))
909				ath6kl_wmi_set_pvb_cmd(ar->wmi, vif->fw_vif_idx,
910						       MCAST_AID, 0);
911		}
912
913		if (!is_broadcast_ether_addr(bssid)) {
914			/* send event to application */
915			cfg80211_del_sta(vif->ndev, bssid, GFP_KERNEL);
916		}
917
918		if (memcmp(vif->ndev->dev_addr, bssid, ETH_ALEN) == 0) {
919			memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
920			clear_bit(CONNECTED, &vif->flags);
921		}
922		return;
923	}
924
925	ath6kl_cfg80211_disconnect_event(vif, reason, bssid,
926				       assoc_resp_len, assoc_info,
927				       prot_reason_status);
928
929	aggr_reset_state(vif->aggr_cntxt->aggr_conn);
930
931	del_timer(&vif->disconnect_timer);
932
933	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "disconnect reason is %d\n", reason);
934
935	/*
936	 * If the event is due to disconnect cmd from the host, only they
937	 * the target would stop trying to connect. Under any other
938	 * condition, target would keep trying to connect.
939	 */
940	if (reason == DISCONNECT_CMD) {
941		if (!ar->usr_bss_filter && test_bit(WMI_READY, &ar->flag))
942			ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx,
943						 NONE_BSS_FILTER, 0);
944	} else {
945		set_bit(CONNECT_PEND, &vif->flags);
946		if (((reason == ASSOC_FAILED) &&
947		    (prot_reason_status == 0x11)) ||
948		    ((reason == ASSOC_FAILED) && (prot_reason_status == 0x0)
949		     && (vif->reconnect_flag == 1))) {
950			set_bit(CONNECTED, &vif->flags);
951			return;
952		}
953	}
954
955	/* update connect & link status atomically */
956	spin_lock_bh(&vif->if_lock);
957	clear_bit(CONNECTED, &vif->flags);
958	netif_carrier_off(vif->ndev);
959	spin_unlock_bh(&vif->if_lock);
960
961	if ((reason != CSERV_DISCONNECT) || (vif->reconnect_flag != 1))
962		vif->reconnect_flag = 0;
963
964	if (reason != CSERV_DISCONNECT)
965		ar->user_key_ctrl = 0;
966
967	netif_stop_queue(vif->ndev);
968	memset(vif->bssid, 0, sizeof(vif->bssid));
969	vif->bss_ch = 0;
970
971	ath6kl_tx_data_cleanup(ar);
972}
973
974struct ath6kl_vif *ath6kl_vif_first(struct ath6kl *ar)
975{
976	struct ath6kl_vif *vif;
977
978	spin_lock_bh(&ar->list_lock);
979	if (list_empty(&ar->vif_list)) {
980		spin_unlock_bh(&ar->list_lock);
981		return NULL;
982	}
983
984	vif = list_first_entry(&ar->vif_list, struct ath6kl_vif, list);
985
986	spin_unlock_bh(&ar->list_lock);
987
988	return vif;
989}
990
991static int ath6kl_open(struct net_device *dev)
992{
993	struct ath6kl_vif *vif = netdev_priv(dev);
994
995	set_bit(WLAN_ENABLED, &vif->flags);
996
997	if (test_bit(CONNECTED, &vif->flags)) {
998		netif_carrier_on(dev);
999		netif_wake_queue(dev);
1000	} else
1001		netif_carrier_off(dev);
1002
1003	return 0;
1004}
1005
1006static int ath6kl_close(struct net_device *dev)
1007{
1008	struct ath6kl_vif *vif = netdev_priv(dev);
1009
1010	netif_stop_queue(dev);
1011
1012	ath6kl_cfg80211_stop(vif);
1013
1014	clear_bit(WLAN_ENABLED, &vif->flags);
1015
1016	return 0;
1017}
1018
1019static struct net_device_stats *ath6kl_get_stats(struct net_device *dev)
1020{
1021	struct ath6kl_vif *vif = netdev_priv(dev);
1022
1023	return &vif->net_stats;
1024}
1025
1026static int ath6kl_set_features(struct net_device *dev,
1027			       netdev_features_t features)
1028{
1029	struct ath6kl_vif *vif = netdev_priv(dev);
1030	struct ath6kl *ar = vif->ar;
1031	int err = 0;
1032
1033	if ((features & NETIF_F_RXCSUM) &&
1034	    (ar->rx_meta_ver != WMI_META_VERSION_2)) {
1035		ar->rx_meta_ver = WMI_META_VERSION_2;
1036		err = ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi,
1037							 vif->fw_vif_idx,
1038							 ar->rx_meta_ver, 0, 0);
1039		if (err) {
1040			dev->features = features & ~NETIF_F_RXCSUM;
1041			return err;
1042		}
1043	} else if (!(features & NETIF_F_RXCSUM) &&
1044		   (ar->rx_meta_ver == WMI_META_VERSION_2)) {
1045		ar->rx_meta_ver = 0;
1046		err = ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi,
1047							 vif->fw_vif_idx,
1048							 ar->rx_meta_ver, 0, 0);
1049		if (err) {
1050			dev->features = features | NETIF_F_RXCSUM;
1051			return err;
1052		}
1053
1054	}
1055
1056	return err;
1057}
1058
1059static void ath6kl_set_multicast_list(struct net_device *ndev)
1060{
1061	struct ath6kl_vif *vif = netdev_priv(ndev);
1062	bool mc_all_on = false, mc_all_off = false;
1063	int mc_count = netdev_mc_count(ndev);
1064	struct netdev_hw_addr *ha;
1065	bool found;
1066	struct ath6kl_mc_filter *mc_filter, *tmp;
1067	struct list_head mc_filter_new;
1068	int ret;
1069
1070	if (!test_bit(WMI_READY, &vif->ar->flag) ||
1071	    !test_bit(WLAN_ENABLED, &vif->flags))
1072		return;
1073
1074	mc_all_on = !!(ndev->flags & IFF_PROMISC) ||
1075		    !!(ndev->flags & IFF_ALLMULTI) ||
1076		    !!(mc_count > ATH6K_MAX_MC_FILTERS_PER_LIST);
1077
1078	mc_all_off = !(ndev->flags & IFF_MULTICAST) || mc_count == 0;
1079
1080	if (mc_all_on || mc_all_off) {
1081		/* Enable/disable all multicast */
1082		ath6kl_dbg(ATH6KL_DBG_TRC, "%s multicast filter\n",
1083			  mc_all_on ? "enabling" : "disabling");
1084		ret = ath6kl_wmi_mcast_filter_cmd(vif->ar->wmi, vif->fw_vif_idx,
1085						  mc_all_on);
1086		if (ret)
1087			ath6kl_warn("Failed to %s multicast receive\n",
1088				    mc_all_on ? "enable" : "disable");
1089		return;
1090	}
1091
1092	list_for_each_entry_safe(mc_filter, tmp, &vif->mc_filter, list) {
1093		found = false;
1094		netdev_for_each_mc_addr(ha, ndev) {
1095			if (memcmp(ha->addr, mc_filter->hw_addr,
1096			    ATH6KL_MCAST_FILTER_MAC_ADDR_SIZE) == 0) {
1097				found = true;
1098				break;
1099			}
1100		}
1101
1102		if (!found) {
1103			/*
1104			 * Delete the filter which was previously set
1105			 * but not in the new request.
1106			 */
1107			ath6kl_dbg(ATH6KL_DBG_TRC,
1108				   "Removing %pM from multicast filter\n",
1109				   mc_filter->hw_addr);
1110			ret = ath6kl_wmi_add_del_mcast_filter_cmd(vif->ar->wmi,
1111					vif->fw_vif_idx, mc_filter->hw_addr,
1112					false);
1113			if (ret) {
1114				ath6kl_warn("Failed to remove multicast filter:%pM\n",
1115					     mc_filter->hw_addr);
1116				return;
1117			}
1118
1119			list_del(&mc_filter->list);
1120			kfree(mc_filter);
1121		}
1122	}
1123
1124	INIT_LIST_HEAD(&mc_filter_new);
1125
1126	netdev_for_each_mc_addr(ha, ndev) {
1127		found = false;
1128		list_for_each_entry(mc_filter, &vif->mc_filter, list) {
1129			if (memcmp(ha->addr, mc_filter->hw_addr,
1130			    ATH6KL_MCAST_FILTER_MAC_ADDR_SIZE) == 0) {
1131				found = true;
1132				break;
1133			}
1134		}
1135
1136		if (!found) {
1137			mc_filter = kzalloc(sizeof(struct ath6kl_mc_filter),
1138					    GFP_ATOMIC);
1139			if (!mc_filter) {
1140				WARN_ON(1);
1141				goto out;
1142			}
1143
1144			memcpy(mc_filter->hw_addr, ha->addr,
1145			       ATH6KL_MCAST_FILTER_MAC_ADDR_SIZE);
1146			/* Set the multicast filter */
1147			ath6kl_dbg(ATH6KL_DBG_TRC,
1148				   "Adding %pM to multicast filter list\n",
1149				   mc_filter->hw_addr);
1150			ret = ath6kl_wmi_add_del_mcast_filter_cmd(vif->ar->wmi,
1151					vif->fw_vif_idx, mc_filter->hw_addr,
1152					true);
1153			if (ret) {
1154				ath6kl_warn("Failed to add multicast filter :%pM\n",
1155					     mc_filter->hw_addr);
1156				kfree(mc_filter);
1157				goto out;
1158			}
1159
1160			list_add_tail(&mc_filter->list, &mc_filter_new);
1161		}
1162	}
1163
1164out:
1165	list_splice_tail(&mc_filter_new, &vif->mc_filter);
1166}
1167
1168static const struct net_device_ops ath6kl_netdev_ops = {
1169	.ndo_open               = ath6kl_open,
1170	.ndo_stop               = ath6kl_close,
1171	.ndo_start_xmit         = ath6kl_data_tx,
1172	.ndo_get_stats          = ath6kl_get_stats,
1173	.ndo_set_features       = ath6kl_set_features,
1174	.ndo_set_rx_mode	= ath6kl_set_multicast_list,
1175};
1176
1177void init_netdev(struct net_device *dev)
1178{
1179	dev->netdev_ops = &ath6kl_netdev_ops;
1180	dev->destructor = free_netdev;
1181	dev->watchdog_timeo = ATH6KL_TX_TIMEOUT;
1182
1183	dev->needed_headroom = ETH_HLEN;
1184	dev->needed_headroom += sizeof(struct ath6kl_llc_snap_hdr) +
1185				sizeof(struct wmi_data_hdr) + HTC_HDR_LENGTH
1186				+ WMI_MAX_TX_META_SZ + ATH6KL_HTC_ALIGN_BYTES;
1187
1188	return;
1189}
1190