main.c revision bc07ddb29a7b71ad009bcd84bee4c93908cf22b6
1/*
2 * Copyright (c) 2004-2011 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "core.h"
18#include "hif-ops.h"
19#include "cfg80211.h"
20#include "target.h"
21#include "debug.h"
22
23struct ath6kl_sta *ath6kl_find_sta(struct ath6kl *ar, u8 *node_addr)
24{
25	struct ath6kl_sta *conn = NULL;
26	u8 i, max_conn;
27
28	max_conn = (ar->nw_type == AP_NETWORK) ? AP_MAX_NUM_STA : 0;
29
30	for (i = 0; i < max_conn; i++) {
31		if (memcmp(node_addr, ar->sta_list[i].mac, ETH_ALEN) == 0) {
32			conn = &ar->sta_list[i];
33			break;
34		}
35	}
36
37	return conn;
38}
39
40struct ath6kl_sta *ath6kl_find_sta_by_aid(struct ath6kl *ar, u8 aid)
41{
42	struct ath6kl_sta *conn = NULL;
43	u8 ctr;
44
45	for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
46		if (ar->sta_list[ctr].aid == aid) {
47			conn = &ar->sta_list[ctr];
48			break;
49		}
50	}
51	return conn;
52}
53
54static void ath6kl_add_new_sta(struct ath6kl *ar, u8 *mac, u16 aid, u8 *wpaie,
55			u8 ielen, u8 keymgmt, u8 ucipher, u8 auth)
56{
57	struct ath6kl_sta *sta;
58	u8 free_slot;
59
60	free_slot = aid - 1;
61
62	sta = &ar->sta_list[free_slot];
63	memcpy(sta->mac, mac, ETH_ALEN);
64	if (ielen <= ATH6KL_MAX_IE)
65		memcpy(sta->wpa_ie, wpaie, ielen);
66	sta->aid = aid;
67	sta->keymgmt = keymgmt;
68	sta->ucipher = ucipher;
69	sta->auth = auth;
70
71	ar->sta_list_index = ar->sta_list_index | (1 << free_slot);
72	ar->ap_stats.sta[free_slot].aid = cpu_to_le32(aid);
73}
74
75static void ath6kl_sta_cleanup(struct ath6kl *ar, u8 i)
76{
77	struct ath6kl_sta *sta = &ar->sta_list[i];
78
79	/* empty the queued pkts in the PS queue if any */
80	spin_lock_bh(&sta->psq_lock);
81	skb_queue_purge(&sta->psq);
82	spin_unlock_bh(&sta->psq_lock);
83
84	memset(&ar->ap_stats.sta[sta->aid - 1], 0,
85	       sizeof(struct wmi_per_sta_stat));
86	memset(sta->mac, 0, ETH_ALEN);
87	memset(sta->wpa_ie, 0, ATH6KL_MAX_IE);
88	sta->aid = 0;
89	sta->sta_flags = 0;
90
91	ar->sta_list_index = ar->sta_list_index & ~(1 << i);
92
93}
94
95static u8 ath6kl_remove_sta(struct ath6kl *ar, u8 *mac, u16 reason)
96{
97	u8 i, removed = 0;
98
99	if (is_zero_ether_addr(mac))
100		return removed;
101
102	if (is_broadcast_ether_addr(mac)) {
103		ath6kl_dbg(ATH6KL_DBG_TRC, "deleting all station\n");
104
105		for (i = 0; i < AP_MAX_NUM_STA; i++) {
106			if (!is_zero_ether_addr(ar->sta_list[i].mac)) {
107				ath6kl_sta_cleanup(ar, i);
108				removed = 1;
109			}
110		}
111	} else {
112		for (i = 0; i < AP_MAX_NUM_STA; i++) {
113			if (memcmp(ar->sta_list[i].mac, mac, ETH_ALEN) == 0) {
114				ath6kl_dbg(ATH6KL_DBG_TRC,
115					   "deleting station %pM aid=%d reason=%d\n",
116					   mac, ar->sta_list[i].aid, reason);
117				ath6kl_sta_cleanup(ar, i);
118				removed = 1;
119				break;
120			}
121		}
122	}
123
124	return removed;
125}
126
127enum htc_endpoint_id ath6kl_ac2_endpoint_id(void *devt, u8 ac)
128{
129	struct ath6kl *ar = devt;
130	return ar->ac2ep_map[ac];
131}
132
133struct ath6kl_cookie *ath6kl_alloc_cookie(struct ath6kl *ar)
134{
135	struct ath6kl_cookie *cookie;
136
137	cookie = ar->cookie_list;
138	if (cookie != NULL) {
139		ar->cookie_list = cookie->arc_list_next;
140		ar->cookie_count--;
141	}
142
143	return cookie;
144}
145
146void ath6kl_cookie_init(struct ath6kl *ar)
147{
148	u32 i;
149
150	ar->cookie_list = NULL;
151	ar->cookie_count = 0;
152
153	memset(ar->cookie_mem, 0, sizeof(ar->cookie_mem));
154
155	for (i = 0; i < MAX_COOKIE_NUM; i++)
156		ath6kl_free_cookie(ar, &ar->cookie_mem[i]);
157}
158
159void ath6kl_cookie_cleanup(struct ath6kl *ar)
160{
161	ar->cookie_list = NULL;
162	ar->cookie_count = 0;
163}
164
165void ath6kl_free_cookie(struct ath6kl *ar, struct ath6kl_cookie *cookie)
166{
167	/* Insert first */
168
169	if (!ar || !cookie)
170		return;
171
172	cookie->arc_list_next = ar->cookie_list;
173	ar->cookie_list = cookie;
174	ar->cookie_count++;
175}
176
177/* set the window address register (using 4-byte register access ). */
178static int ath6kl_set_addrwin_reg(struct ath6kl *ar, u32 reg_addr, u32 addr)
179{
180	int status;
181	u8 addr_val[4];
182	s32 i;
183
184	/*
185	 * Write bytes 1,2,3 of the register to set the upper address bytes,
186	 * the LSB is written last to initiate the access cycle
187	 */
188
189	for (i = 1; i <= 3; i++) {
190		/*
191		 * Fill the buffer with the address byte value we want to
192		 * hit 4 times.
193		 */
194		memset(addr_val, ((u8 *)&addr)[i], 4);
195
196		/*
197		 * Hit each byte of the register address with a 4-byte
198		 * write operation to the same address, this is a harmless
199		 * operation.
200		 */
201		status = hif_read_write_sync(ar, reg_addr + i, addr_val,
202					     4, HIF_WR_SYNC_BYTE_FIX);
203		if (status)
204			break;
205	}
206
207	if (status) {
208		ath6kl_err("failed to write initial bytes of 0x%x to window reg: 0x%X\n",
209			   addr, reg_addr);
210		return status;
211	}
212
213	/*
214	 * Write the address register again, this time write the whole
215	 * 4-byte value. The effect here is that the LSB write causes the
216	 * cycle to start, the extra 3 byte write to bytes 1,2,3 has no
217	 * effect since we are writing the same values again
218	 */
219	status = hif_read_write_sync(ar, reg_addr, (u8 *)(&addr),
220				     4, HIF_WR_SYNC_BYTE_INC);
221
222	if (status) {
223		ath6kl_err("failed to write 0x%x to window reg: 0x%X\n",
224			   addr, reg_addr);
225		return status;
226	}
227
228	return 0;
229}
230
231/*
232 * Read from the hardware through its diagnostic window. No cooperation
233 * from the firmware is required for this.
234 */
235int ath6kl_diag_read32(struct ath6kl *ar, u32 address, u32 *value)
236{
237	int ret;
238
239	/* set window register to start read cycle */
240	ret = ath6kl_set_addrwin_reg(ar, WINDOW_READ_ADDR_ADDRESS, address);
241	if (ret)
242		return ret;
243
244	/* read the data */
245	ret = hif_read_write_sync(ar, WINDOW_DATA_ADDRESS, (u8 *) value,
246				  sizeof(*value), HIF_RD_SYNC_BYTE_INC);
247	if (ret) {
248		ath6kl_warn("failed to read32 through diagnose window: %d\n",
249			    ret);
250		return ret;
251	}
252
253	return 0;
254}
255
256/*
257 * Write to the ATH6KL through its diagnostic window. No cooperation from
258 * the Target is required for this.
259 */
260static int ath6kl_diag_write32(struct ath6kl *ar, u32 address, u32 value)
261{
262	int ret;
263
264	/* set write data */
265	ret = hif_read_write_sync(ar, WINDOW_DATA_ADDRESS, (u8 *) &value,
266				  sizeof(value), HIF_WR_SYNC_BYTE_INC);
267	if (ret) {
268		ath6kl_err("failed to write 0x%x during diagnose window to 0x%d\n",
269			   address, value);
270		return ret;
271	}
272
273	/* set window register, which starts the write cycle */
274	return ath6kl_set_addrwin_reg(ar, WINDOW_WRITE_ADDR_ADDRESS,
275				      address);
276}
277
278int ath6kl_diag_read(struct ath6kl *ar, u32 address, void *data, u32 length)
279{
280	u32 count, *buf = data;
281	int ret;
282
283	if (WARN_ON(length % 4))
284		return -EINVAL;
285
286	for (count = 0; count < length / 4; count++, address += 4) {
287		ret = ath6kl_diag_read32(ar, address, &buf[count]);
288		if (ret)
289			return ret;
290	}
291
292	return 0;
293}
294
295int ath6kl_diag_write(struct ath6kl *ar, u32 address, void *data, u32 length)
296{
297	u32 count, *buf = data;
298	int ret;
299
300	if (WARN_ON(length % 4))
301		return -EINVAL;
302
303	for (count = 0; count < length / 4; count++, address += 4) {
304		ret = ath6kl_diag_write32(ar, address, buf[count]);
305		if (ret)
306			return ret;
307	}
308
309	return 0;
310}
311
312int ath6kl_read_fwlogs(struct ath6kl *ar)
313{
314	struct ath6kl_dbglog_hdr debug_hdr;
315	struct ath6kl_dbglog_buf debug_buf;
316	u32 address, length, dropped, firstbuf, debug_hdr_addr;
317	int ret = 0, loop;
318	u8 *buf;
319
320	buf = kmalloc(ATH6KL_FWLOG_PAYLOAD_SIZE, GFP_KERNEL);
321	if (!buf)
322		return -ENOMEM;
323
324	address = TARG_VTOP(ar->target_type,
325			    ath6kl_get_hi_item_addr(ar,
326						    HI_ITEM(hi_dbglog_hdr)));
327
328	ret = ath6kl_diag_read32(ar, address, &debug_hdr_addr);
329	if (ret)
330		goto out;
331
332	/* Get the contents of the ring buffer */
333	if (debug_hdr_addr == 0) {
334		ath6kl_warn("Invalid address for debug_hdr_addr\n");
335		ret = -EINVAL;
336		goto out;
337	}
338
339	address = TARG_VTOP(ar->target_type, debug_hdr_addr);
340	ath6kl_diag_read(ar, address, &debug_hdr, sizeof(debug_hdr));
341
342	address = TARG_VTOP(ar->target_type,
343			    le32_to_cpu(debug_hdr.dbuf_addr));
344	firstbuf = address;
345	dropped = le32_to_cpu(debug_hdr.dropped);
346	ath6kl_diag_read(ar, address, &debug_buf, sizeof(debug_buf));
347
348	loop = 100;
349
350	do {
351		address = TARG_VTOP(ar->target_type,
352				    le32_to_cpu(debug_buf.buffer_addr));
353		length = le32_to_cpu(debug_buf.length);
354
355		if (length != 0 && (le32_to_cpu(debug_buf.length) <=
356				    le32_to_cpu(debug_buf.bufsize))) {
357			length = ALIGN(length, 4);
358
359			ret = ath6kl_diag_read(ar, address,
360					       buf, length);
361			if (ret)
362				goto out;
363
364			ath6kl_debug_fwlog_event(ar, buf, length);
365		}
366
367		address = TARG_VTOP(ar->target_type,
368				    le32_to_cpu(debug_buf.next));
369		ath6kl_diag_read(ar, address, &debug_buf, sizeof(debug_buf));
370		if (ret)
371			goto out;
372
373		loop--;
374
375		if (WARN_ON(loop == 0)) {
376			ret = -ETIMEDOUT;
377			goto out;
378		}
379	} while (address != firstbuf);
380
381out:
382	kfree(buf);
383
384	return ret;
385}
386
387/* FIXME: move to a better place, target.h? */
388#define AR6003_RESET_CONTROL_ADDRESS 0x00004000
389#define AR6004_RESET_CONTROL_ADDRESS 0x00004000
390
391static void ath6kl_reset_device(struct ath6kl *ar, u32 target_type,
392				bool wait_fot_compltn, bool cold_reset)
393{
394	int status = 0;
395	u32 address;
396	u32 data;
397
398	if (target_type != TARGET_TYPE_AR6003 &&
399		target_type != TARGET_TYPE_AR6004)
400		return;
401
402	data = cold_reset ? RESET_CONTROL_COLD_RST : RESET_CONTROL_MBOX_RST;
403
404	switch (target_type) {
405	case TARGET_TYPE_AR6003:
406		address = AR6003_RESET_CONTROL_ADDRESS;
407		break;
408	case TARGET_TYPE_AR6004:
409		address = AR6004_RESET_CONTROL_ADDRESS;
410		break;
411	default:
412		address = AR6003_RESET_CONTROL_ADDRESS;
413		break;
414	}
415
416	status = ath6kl_diag_write32(ar, address, data);
417
418	if (status)
419		ath6kl_err("failed to reset target\n");
420}
421
422void ath6kl_stop_endpoint(struct net_device *dev, bool keep_profile,
423			  bool get_dbglogs)
424{
425	struct ath6kl *ar = ath6kl_priv(dev);
426	static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
427	bool discon_issued;
428
429	netif_stop_queue(dev);
430
431	/* disable the target and the interrupts associated with it */
432	if (test_bit(WMI_READY, &ar->flag)) {
433		discon_issued = (test_bit(CONNECTED, &ar->flag) ||
434				 test_bit(CONNECT_PEND, &ar->flag));
435		ath6kl_disconnect(ar);
436		if (!keep_profile)
437			ath6kl_init_profile_info(ar);
438
439		del_timer(&ar->disconnect_timer);
440
441		clear_bit(WMI_READY, &ar->flag);
442		ath6kl_wmi_shutdown(ar->wmi);
443		clear_bit(WMI_ENABLED, &ar->flag);
444		ar->wmi = NULL;
445
446		/*
447		 * After wmi_shudown all WMI events will be dropped. We
448		 * need to cleanup the buffers allocated in AP mode and
449		 * give disconnect notification to stack, which usually
450		 * happens in the disconnect_event. Simulate the disconnect
451		 * event by calling the function directly. Sometimes
452		 * disconnect_event will be received when the debug logs
453		 * are collected.
454		 */
455		if (discon_issued)
456			ath6kl_disconnect_event(ar, DISCONNECT_CMD,
457						(ar->nw_type & AP_NETWORK) ?
458						bcast_mac : ar->bssid,
459						0, NULL, 0);
460
461		ar->user_key_ctrl = 0;
462
463	} else {
464		ath6kl_dbg(ATH6KL_DBG_TRC,
465			   "%s: wmi is not ready 0x%p 0x%p\n",
466			   __func__, ar, ar->wmi);
467
468		/* Shut down WMI if we have started it */
469		if (test_bit(WMI_ENABLED, &ar->flag)) {
470			ath6kl_dbg(ATH6KL_DBG_TRC,
471				   "%s: shut down wmi\n", __func__);
472			ath6kl_wmi_shutdown(ar->wmi);
473			clear_bit(WMI_ENABLED, &ar->flag);
474			ar->wmi = NULL;
475		}
476	}
477
478	if (ar->htc_target) {
479		ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
480		ath6kl_htc_stop(ar->htc_target);
481	}
482
483	/*
484	 * Try to reset the device if we can. The driver may have been
485	 * configure NOT to reset the target during a debug session.
486	 */
487	ath6kl_dbg(ATH6KL_DBG_TRC,
488		   "attempting to reset target on instance destroy\n");
489	ath6kl_reset_device(ar, ar->target_type, true, true);
490}
491
492static void ath6kl_install_static_wep_keys(struct ath6kl *ar)
493{
494	u8 index;
495	u8 keyusage;
496
497	for (index = WMI_MIN_KEY_INDEX; index <= WMI_MAX_KEY_INDEX; index++) {
498		if (ar->wep_key_list[index].key_len) {
499			keyusage = GROUP_USAGE;
500			if (index == ar->def_txkey_index)
501				keyusage |= TX_USAGE;
502
503			ath6kl_wmi_addkey_cmd(ar->wmi,
504					      index,
505					      WEP_CRYPT,
506					      keyusage,
507					      ar->wep_key_list[index].key_len,
508					      NULL,
509					      ar->wep_key_list[index].key,
510					      KEY_OP_INIT_VAL, NULL,
511					      NO_SYNC_WMIFLAG);
512		}
513	}
514}
515
516static void ath6kl_connect_ap_mode(struct ath6kl *ar, u16 channel, u8 *bssid,
517				   u16 listen_int, u16 beacon_int,
518				   u8 assoc_req_len, u8 *assoc_info)
519{
520	struct net_device *dev = ar->net_dev;
521	u8 *ies = NULL, *wpa_ie = NULL, *pos;
522	size_t ies_len = 0;
523	struct station_info sinfo;
524	struct ath6kl_req_key *ik;
525	int res;
526	u8 key_rsc[ATH6KL_KEY_SEQ_LEN];
527
528	if (memcmp(dev->dev_addr, bssid, ETH_ALEN) == 0) {
529		ik = &ar->ap_mode_bkey;
530
531		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "AP mode started on %u MHz\n",
532			   channel);
533
534		switch (ar->auth_mode) {
535		case NONE_AUTH:
536			if (ar->prwise_crypto == WEP_CRYPT)
537				ath6kl_install_static_wep_keys(ar);
538			break;
539		case WPA_PSK_AUTH:
540		case WPA2_PSK_AUTH:
541		case (WPA_PSK_AUTH|WPA2_PSK_AUTH):
542			if (!ik->valid)
543				break;
544
545			ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "Delayed addkey for "
546				   "the initial group key for AP mode\n");
547			memset(key_rsc, 0, sizeof(key_rsc));
548			res = ath6kl_wmi_addkey_cmd(
549				ar->wmi, ik->key_index, ik->key_type,
550				GROUP_USAGE, ik->key_len, key_rsc, ik->key,
551				KEY_OP_INIT_VAL, NULL, SYNC_BOTH_WMIFLAG);
552			if (res) {
553				ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "Delayed "
554					   "addkey failed: %d\n", res);
555			}
556			break;
557		}
558
559		ath6kl_wmi_bssfilter_cmd(ar->wmi, NONE_BSS_FILTER, 0);
560		set_bit(CONNECTED, &ar->flag);
561		netif_carrier_on(ar->net_dev);
562		return;
563	}
564
565	ath6kl_dbg(ATH6KL_DBG_TRC, "new station %pM aid=%d\n",
566		   bssid, channel);
567
568	if (assoc_req_len > sizeof(struct ieee80211_hdr_3addr)) {
569		struct ieee80211_mgmt *mgmt =
570			(struct ieee80211_mgmt *) assoc_info;
571		if (ieee80211_is_assoc_req(mgmt->frame_control) &&
572		    assoc_req_len >= sizeof(struct ieee80211_hdr_3addr) +
573		    sizeof(mgmt->u.assoc_req)) {
574			ies = mgmt->u.assoc_req.variable;
575			ies_len = assoc_info + assoc_req_len - ies;
576		} else if (ieee80211_is_reassoc_req(mgmt->frame_control) &&
577			   assoc_req_len >= sizeof(struct ieee80211_hdr_3addr)
578			   + sizeof(mgmt->u.reassoc_req)) {
579			ies = mgmt->u.reassoc_req.variable;
580			ies_len = assoc_info + assoc_req_len - ies;
581		}
582	}
583
584	pos = ies;
585	while (pos && pos + 1 < ies + ies_len) {
586		if (pos + 2 + pos[1] > ies + ies_len)
587			break;
588		if (pos[0] == WLAN_EID_RSN)
589			wpa_ie = pos; /* RSN IE */
590		else if (pos[0] == WLAN_EID_VENDOR_SPECIFIC &&
591			 pos[1] >= 4 &&
592			 pos[2] == 0x00 && pos[3] == 0x50 && pos[4] == 0xf2) {
593			if (pos[5] == 0x01)
594				wpa_ie = pos; /* WPA IE */
595			else if (pos[5] == 0x04) {
596				wpa_ie = pos; /* WPS IE */
597				break; /* overrides WPA/RSN IE */
598			}
599		}
600		pos += 2 + pos[1];
601	}
602
603	ath6kl_add_new_sta(ar, bssid, channel, wpa_ie,
604			   wpa_ie ? 2 + wpa_ie[1] : 0,
605			   listen_int & 0xFF, beacon_int,
606			   (listen_int >> 8) & 0xFF);
607
608	/* send event to application */
609	memset(&sinfo, 0, sizeof(sinfo));
610
611	/* TODO: sinfo.generation */
612
613	sinfo.assoc_req_ies = ies;
614	sinfo.assoc_req_ies_len = ies_len;
615	sinfo.filled |= STATION_INFO_ASSOC_REQ_IES;
616
617	cfg80211_new_sta(ar->net_dev, bssid, &sinfo, GFP_KERNEL);
618
619	netif_wake_queue(ar->net_dev);
620
621	return;
622}
623
624/* Functions for Tx credit handling */
625void ath6k_credit_init(struct htc_credit_state_info *cred_info,
626		       struct list_head *ep_list,
627		       int tot_credits)
628{
629	struct htc_endpoint_credit_dist *cur_ep_dist;
630	int count;
631
632	cred_info->cur_free_credits = tot_credits;
633	cred_info->total_avail_credits = tot_credits;
634
635	list_for_each_entry(cur_ep_dist, ep_list, list) {
636		if (cur_ep_dist->endpoint == ENDPOINT_0)
637			continue;
638
639		cur_ep_dist->cred_min = cur_ep_dist->cred_per_msg;
640
641		if (tot_credits > 4)
642			if ((cur_ep_dist->svc_id == WMI_DATA_BK_SVC) ||
643			    (cur_ep_dist->svc_id == WMI_DATA_BE_SVC)) {
644				ath6kl_deposit_credit_to_ep(cred_info,
645						cur_ep_dist,
646						cur_ep_dist->cred_min);
647				cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
648			}
649
650		if (cur_ep_dist->svc_id == WMI_CONTROL_SVC) {
651			ath6kl_deposit_credit_to_ep(cred_info, cur_ep_dist,
652						    cur_ep_dist->cred_min);
653			/*
654			 * Control service is always marked active, it
655			 * never goes inactive EVER.
656			 */
657			cur_ep_dist->dist_flags |= HTC_EP_ACTIVE;
658		} else if (cur_ep_dist->svc_id == WMI_DATA_BK_SVC)
659			/* this is the lowest priority data endpoint */
660			cred_info->lowestpri_ep_dist = cur_ep_dist->list;
661
662		/*
663		 * Streams have to be created (explicit | implicit) for all
664		 * kinds of traffic. BE endpoints are also inactive in the
665		 * beginning. When BE traffic starts it creates implicit
666		 * streams that redistributes credits.
667		 *
668		 * Note: all other endpoints have minimums set but are
669		 * initially given NO credits. credits will be distributed
670		 * as traffic activity demands
671		 */
672	}
673
674	WARN_ON(cred_info->cur_free_credits <= 0);
675
676	list_for_each_entry(cur_ep_dist, ep_list, list) {
677		if (cur_ep_dist->endpoint == ENDPOINT_0)
678			continue;
679
680		if (cur_ep_dist->svc_id == WMI_CONTROL_SVC)
681			cur_ep_dist->cred_norm = cur_ep_dist->cred_per_msg;
682		else {
683			/*
684			 * For the remaining data endpoints, we assume that
685			 * each cred_per_msg are the same. We use a simple
686			 * calculation here, we take the remaining credits
687			 * and determine how many max messages this can
688			 * cover and then set each endpoint's normal value
689			 * equal to 3/4 this amount.
690			 */
691			count = (cred_info->cur_free_credits /
692				 cur_ep_dist->cred_per_msg)
693				* cur_ep_dist->cred_per_msg;
694			count = (count * 3) >> 2;
695			count = max(count, cur_ep_dist->cred_per_msg);
696			cur_ep_dist->cred_norm = count;
697
698		}
699	}
700}
701
702/* initialize and setup credit distribution */
703int ath6k_setup_credit_dist(void *htc_handle,
704			    struct htc_credit_state_info *cred_info)
705{
706	u16 servicepriority[5];
707
708	memset(cred_info, 0, sizeof(struct htc_credit_state_info));
709
710	servicepriority[0] = WMI_CONTROL_SVC;  /* highest */
711	servicepriority[1] = WMI_DATA_VO_SVC;
712	servicepriority[2] = WMI_DATA_VI_SVC;
713	servicepriority[3] = WMI_DATA_BE_SVC;
714	servicepriority[4] = WMI_DATA_BK_SVC; /* lowest */
715
716	/* set priority list */
717	ath6kl_htc_set_credit_dist(htc_handle, cred_info, servicepriority, 5);
718
719	return 0;
720}
721
722/* reduce an ep's credits back to a set limit */
723static void ath6k_reduce_credits(struct htc_credit_state_info *cred_info,
724				 struct htc_endpoint_credit_dist  *ep_dist,
725				 int limit)
726{
727	int credits;
728
729	ep_dist->cred_assngd = limit;
730
731	if (ep_dist->credits <= limit)
732		return;
733
734	credits = ep_dist->credits - limit;
735	ep_dist->credits -= credits;
736	cred_info->cur_free_credits += credits;
737}
738
739static void ath6k_credit_update(struct htc_credit_state_info *cred_info,
740				struct list_head *epdist_list)
741{
742	struct htc_endpoint_credit_dist *cur_dist_list;
743
744	list_for_each_entry(cur_dist_list, epdist_list, list) {
745		if (cur_dist_list->endpoint == ENDPOINT_0)
746			continue;
747
748		if (cur_dist_list->cred_to_dist > 0) {
749			cur_dist_list->credits +=
750					cur_dist_list->cred_to_dist;
751			cur_dist_list->cred_to_dist = 0;
752			if (cur_dist_list->credits >
753			    cur_dist_list->cred_assngd)
754				ath6k_reduce_credits(cred_info,
755						cur_dist_list,
756						cur_dist_list->cred_assngd);
757
758			if (cur_dist_list->credits >
759			    cur_dist_list->cred_norm)
760				ath6k_reduce_credits(cred_info, cur_dist_list,
761						     cur_dist_list->cred_norm);
762
763			if (!(cur_dist_list->dist_flags & HTC_EP_ACTIVE)) {
764				if (cur_dist_list->txq_depth == 0)
765					ath6k_reduce_credits(cred_info,
766							     cur_dist_list, 0);
767			}
768		}
769	}
770}
771
772/*
773 * HTC has an endpoint that needs credits, ep_dist is the endpoint in
774 * question.
775 */
776void ath6k_seek_credits(struct htc_credit_state_info *cred_info,
777			struct htc_endpoint_credit_dist *ep_dist)
778{
779	struct htc_endpoint_credit_dist *curdist_list;
780	int credits = 0;
781	int need;
782
783	if (ep_dist->svc_id == WMI_CONTROL_SVC)
784		goto out;
785
786	if ((ep_dist->svc_id == WMI_DATA_VI_SVC) ||
787	    (ep_dist->svc_id == WMI_DATA_VO_SVC))
788		if ((ep_dist->cred_assngd >= ep_dist->cred_norm))
789			goto out;
790
791	/*
792	 * For all other services, we follow a simple algorithm of:
793	 *
794	 * 1. checking the free pool for credits
795	 * 2. checking lower priority endpoints for credits to take
796	 */
797
798	credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
799
800	if (credits >= ep_dist->seek_cred)
801		goto out;
802
803	/*
804	 * We don't have enough in the free pool, try taking away from
805	 * lower priority services The rule for taking away credits:
806	 *
807	 *   1. Only take from lower priority endpoints
808	 *   2. Only take what is allocated above the minimum (never
809	 *      starve an endpoint completely)
810	 *   3. Only take what you need.
811	 */
812
813	list_for_each_entry_reverse(curdist_list,
814				    &cred_info->lowestpri_ep_dist,
815				    list) {
816		if (curdist_list == ep_dist)
817			break;
818
819		need = ep_dist->seek_cred - cred_info->cur_free_credits;
820
821		if ((curdist_list->cred_assngd - need) >=
822		     curdist_list->cred_min) {
823			/*
824			 * The current one has been allocated more than
825			 * it's minimum and it has enough credits assigned
826			 * above it's minimum to fulfill our need try to
827			 * take away just enough to fulfill our need.
828			 */
829			ath6k_reduce_credits(cred_info, curdist_list,
830					curdist_list->cred_assngd - need);
831
832			if (cred_info->cur_free_credits >=
833			    ep_dist->seek_cred)
834				break;
835		}
836
837		if (curdist_list->endpoint == ENDPOINT_0)
838			break;
839	}
840
841	credits = min(cred_info->cur_free_credits, ep_dist->seek_cred);
842
843out:
844	/* did we find some credits? */
845	if (credits)
846		ath6kl_deposit_credit_to_ep(cred_info, ep_dist, credits);
847
848	ep_dist->seek_cred = 0;
849}
850
851/* redistribute credits based on activity change */
852static void ath6k_redistribute_credits(struct htc_credit_state_info *info,
853				       struct list_head *ep_dist_list)
854{
855	struct htc_endpoint_credit_dist *curdist_list;
856
857	list_for_each_entry(curdist_list, ep_dist_list, list) {
858		if (curdist_list->endpoint == ENDPOINT_0)
859			continue;
860
861		if ((curdist_list->svc_id == WMI_DATA_BK_SVC)  ||
862		    (curdist_list->svc_id == WMI_DATA_BE_SVC))
863			curdist_list->dist_flags |= HTC_EP_ACTIVE;
864
865		if ((curdist_list->svc_id != WMI_CONTROL_SVC) &&
866		    !(curdist_list->dist_flags & HTC_EP_ACTIVE)) {
867			if (curdist_list->txq_depth == 0)
868				ath6k_reduce_credits(info,
869						curdist_list, 0);
870			else
871				ath6k_reduce_credits(info,
872						curdist_list,
873						curdist_list->cred_min);
874		}
875	}
876}
877
878/*
879 *
880 * This function is invoked whenever endpoints require credit
881 * distributions. A lock is held while this function is invoked, this
882 * function shall NOT block. The ep_dist_list is a list of distribution
883 * structures in prioritized order as defined by the call to the
884 * htc_set_credit_dist() api.
885 */
886void ath6k_credit_distribute(struct htc_credit_state_info *cred_info,
887			     struct list_head *ep_dist_list,
888			     enum htc_credit_dist_reason reason)
889{
890	switch (reason) {
891	case HTC_CREDIT_DIST_SEND_COMPLETE:
892		ath6k_credit_update(cred_info, ep_dist_list);
893		break;
894	case HTC_CREDIT_DIST_ACTIVITY_CHANGE:
895		ath6k_redistribute_credits(cred_info, ep_dist_list);
896		break;
897	default:
898		break;
899	}
900
901	WARN_ON(cred_info->cur_free_credits > cred_info->total_avail_credits);
902	WARN_ON(cred_info->cur_free_credits < 0);
903}
904
905void disconnect_timer_handler(unsigned long ptr)
906{
907	struct net_device *dev = (struct net_device *)ptr;
908	struct ath6kl *ar = ath6kl_priv(dev);
909
910	ath6kl_init_profile_info(ar);
911	ath6kl_disconnect(ar);
912}
913
914void ath6kl_disconnect(struct ath6kl *ar)
915{
916	if (test_bit(CONNECTED, &ar->flag) ||
917	    test_bit(CONNECT_PEND, &ar->flag)) {
918		ath6kl_wmi_disconnect_cmd(ar->wmi);
919		/*
920		 * Disconnect command is issued, clear the connect pending
921		 * flag. The connected flag will be cleared in
922		 * disconnect event notification.
923		 */
924		clear_bit(CONNECT_PEND, &ar->flag);
925	}
926}
927
928void ath6kl_deep_sleep_enable(struct ath6kl *ar)
929{
930	switch (ar->sme_state) {
931	case SME_CONNECTING:
932		cfg80211_connect_result(ar->net_dev, ar->bssid, NULL, 0,
933					NULL, 0,
934					WLAN_STATUS_UNSPECIFIED_FAILURE,
935					GFP_KERNEL);
936		break;
937	case SME_CONNECTED:
938	default:
939		/*
940		 * FIXME: oddly enough smeState is in DISCONNECTED during
941		 * suspend, why? Need to send disconnected event in that
942		 * state.
943		 */
944		cfg80211_disconnected(ar->net_dev, 0, NULL, 0, GFP_KERNEL);
945		break;
946	}
947
948	if (test_bit(CONNECTED, &ar->flag) ||
949	    test_bit(CONNECT_PEND, &ar->flag))
950		ath6kl_wmi_disconnect_cmd(ar->wmi);
951
952	ar->sme_state = SME_DISCONNECTED;
953
954	/* disable scanning */
955	if (ath6kl_wmi_scanparams_cmd(ar->wmi, 0xFFFF, 0, 0, 0, 0, 0, 0, 0,
956				      0, 0) != 0)
957		printk(KERN_WARNING "ath6kl: failed to disable scan "
958		       "during suspend\n");
959
960	ath6kl_cfg80211_scan_complete_event(ar, -ECANCELED);
961}
962
963/* WMI Event handlers */
964
965static const char *get_hw_id_string(u32 id)
966{
967	switch (id) {
968	case AR6003_REV1_VERSION:
969		return "1.0";
970	case AR6003_REV2_VERSION:
971		return "2.0";
972	case AR6003_REV3_VERSION:
973		return "2.1.1";
974	default:
975		return "unknown";
976	}
977}
978
979void ath6kl_ready_event(void *devt, u8 *datap, u32 sw_ver, u32 abi_ver)
980{
981	struct ath6kl *ar = devt;
982	struct net_device *dev = ar->net_dev;
983
984	memcpy(dev->dev_addr, datap, ETH_ALEN);
985	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: mac addr = %pM\n",
986		   __func__, dev->dev_addr);
987
988	ar->version.wlan_ver = sw_ver;
989	ar->version.abi_ver = abi_ver;
990
991	snprintf(ar->wdev->wiphy->fw_version,
992		 sizeof(ar->wdev->wiphy->fw_version),
993		 "%u.%u.%u.%u",
994		 (ar->version.wlan_ver & 0xf0000000) >> 28,
995		 (ar->version.wlan_ver & 0x0f000000) >> 24,
996		 (ar->version.wlan_ver & 0x00ff0000) >> 16,
997		 (ar->version.wlan_ver & 0x0000ffff));
998
999	/* indicate to the waiting thread that the ready event was received */
1000	set_bit(WMI_READY, &ar->flag);
1001	wake_up(&ar->event_wq);
1002
1003	ath6kl_info("hw %s fw %s%s\n",
1004		    get_hw_id_string(ar->wdev->wiphy->hw_version),
1005		    ar->wdev->wiphy->fw_version,
1006		    test_bit(TESTMODE, &ar->flag) ? " testmode" : "");
1007}
1008
1009void ath6kl_scan_complete_evt(struct ath6kl *ar, int status)
1010{
1011	ath6kl_cfg80211_scan_complete_event(ar, status);
1012
1013	if (!ar->usr_bss_filter)
1014		ath6kl_wmi_bssfilter_cmd(ar->wmi, NONE_BSS_FILTER, 0);
1015
1016	ath6kl_dbg(ATH6KL_DBG_WLAN_SCAN, "scan complete: %d\n", status);
1017}
1018
1019void ath6kl_connect_event(struct ath6kl *ar, u16 channel, u8 *bssid,
1020			  u16 listen_int, u16 beacon_int,
1021			  enum network_type net_type, u8 beacon_ie_len,
1022			  u8 assoc_req_len, u8 assoc_resp_len,
1023			  u8 *assoc_info)
1024{
1025	unsigned long flags;
1026
1027	if (ar->nw_type == AP_NETWORK) {
1028		ath6kl_connect_ap_mode(ar, channel, bssid, listen_int,
1029				       beacon_int, assoc_req_len,
1030				       assoc_info + beacon_ie_len);
1031		return;
1032	}
1033
1034	ath6kl_cfg80211_connect_event(ar, channel, bssid,
1035				      listen_int, beacon_int,
1036				      net_type, beacon_ie_len,
1037				      assoc_req_len, assoc_resp_len,
1038				      assoc_info);
1039
1040	memcpy(ar->bssid, bssid, sizeof(ar->bssid));
1041	ar->bss_ch = channel;
1042
1043	if ((ar->nw_type == INFRA_NETWORK))
1044		ath6kl_wmi_listeninterval_cmd(ar->wmi, ar->listen_intvl_t,
1045					      ar->listen_intvl_b);
1046
1047	netif_wake_queue(ar->net_dev);
1048
1049	/* Update connect & link status atomically */
1050	spin_lock_irqsave(&ar->lock, flags);
1051	set_bit(CONNECTED, &ar->flag);
1052	clear_bit(CONNECT_PEND, &ar->flag);
1053	netif_carrier_on(ar->net_dev);
1054	spin_unlock_irqrestore(&ar->lock, flags);
1055
1056	aggr_reset_state(ar->aggr_cntxt);
1057	ar->reconnect_flag = 0;
1058
1059	if ((ar->nw_type == ADHOC_NETWORK) && ar->ibss_ps_enable) {
1060		memset(ar->node_map, 0, sizeof(ar->node_map));
1061		ar->node_num = 0;
1062		ar->next_ep_id = ENDPOINT_2;
1063	}
1064
1065	if (!ar->usr_bss_filter)
1066		ath6kl_wmi_bssfilter_cmd(ar->wmi, NONE_BSS_FILTER, 0);
1067}
1068
1069void ath6kl_tkip_micerr_event(struct ath6kl *ar, u8 keyid, bool ismcast)
1070{
1071	struct ath6kl_sta *sta;
1072	u8 tsc[6];
1073	/*
1074	 * For AP case, keyid will have aid of STA which sent pkt with
1075	 * MIC error. Use this aid to get MAC & send it to hostapd.
1076	 */
1077	if (ar->nw_type == AP_NETWORK) {
1078		sta = ath6kl_find_sta_by_aid(ar, (keyid >> 2));
1079		if (!sta)
1080			return;
1081
1082		ath6kl_dbg(ATH6KL_DBG_TRC,
1083			   "ap tkip mic error received from aid=%d\n", keyid);
1084
1085		memset(tsc, 0, sizeof(tsc)); /* FIX: get correct TSC */
1086		cfg80211_michael_mic_failure(ar->net_dev, sta->mac,
1087					     NL80211_KEYTYPE_PAIRWISE, keyid,
1088					     tsc, GFP_KERNEL);
1089	} else
1090		ath6kl_cfg80211_tkip_micerr_event(ar, keyid, ismcast);
1091
1092}
1093
1094static void ath6kl_update_target_stats(struct ath6kl *ar, u8 *ptr, u32 len)
1095{
1096	struct wmi_target_stats *tgt_stats =
1097		(struct wmi_target_stats *) ptr;
1098	struct target_stats *stats = &ar->target_stats;
1099	struct tkip_ccmp_stats *ccmp_stats;
1100	struct bss *conn_bss = NULL;
1101	struct cserv_stats *c_stats;
1102	u8 ac;
1103
1104	if (len < sizeof(*tgt_stats))
1105		return;
1106
1107	/* update the RSSI of the connected bss */
1108	if (test_bit(CONNECTED, &ar->flag)) {
1109		conn_bss = ath6kl_wmi_find_node(ar->wmi, ar->bssid);
1110		if (conn_bss) {
1111			c_stats = &tgt_stats->cserv_stats;
1112			conn_bss->ni_rssi =
1113				a_sle16_to_cpu(c_stats->cs_ave_beacon_rssi);
1114			conn_bss->ni_snr =
1115				tgt_stats->cserv_stats.cs_ave_beacon_snr;
1116			ath6kl_wmi_node_return(ar->wmi, conn_bss);
1117		}
1118	}
1119
1120	ath6kl_dbg(ATH6KL_DBG_TRC, "updating target stats\n");
1121
1122	stats->tx_pkt += le32_to_cpu(tgt_stats->stats.tx.pkt);
1123	stats->tx_byte += le32_to_cpu(tgt_stats->stats.tx.byte);
1124	stats->tx_ucast_pkt += le32_to_cpu(tgt_stats->stats.tx.ucast_pkt);
1125	stats->tx_ucast_byte += le32_to_cpu(tgt_stats->stats.tx.ucast_byte);
1126	stats->tx_mcast_pkt += le32_to_cpu(tgt_stats->stats.tx.mcast_pkt);
1127	stats->tx_mcast_byte += le32_to_cpu(tgt_stats->stats.tx.mcast_byte);
1128	stats->tx_bcast_pkt  += le32_to_cpu(tgt_stats->stats.tx.bcast_pkt);
1129	stats->tx_bcast_byte += le32_to_cpu(tgt_stats->stats.tx.bcast_byte);
1130	stats->tx_rts_success_cnt +=
1131		le32_to_cpu(tgt_stats->stats.tx.rts_success_cnt);
1132
1133	for (ac = 0; ac < WMM_NUM_AC; ac++)
1134		stats->tx_pkt_per_ac[ac] +=
1135			le32_to_cpu(tgt_stats->stats.tx.pkt_per_ac[ac]);
1136
1137	stats->tx_err += le32_to_cpu(tgt_stats->stats.tx.err);
1138	stats->tx_fail_cnt += le32_to_cpu(tgt_stats->stats.tx.fail_cnt);
1139	stats->tx_retry_cnt += le32_to_cpu(tgt_stats->stats.tx.retry_cnt);
1140	stats->tx_mult_retry_cnt +=
1141		le32_to_cpu(tgt_stats->stats.tx.mult_retry_cnt);
1142	stats->tx_rts_fail_cnt +=
1143		le32_to_cpu(tgt_stats->stats.tx.rts_fail_cnt);
1144	stats->tx_ucast_rate =
1145	    ath6kl_wmi_get_rate(a_sle32_to_cpu(tgt_stats->stats.tx.ucast_rate));
1146
1147	stats->rx_pkt += le32_to_cpu(tgt_stats->stats.rx.pkt);
1148	stats->rx_byte += le32_to_cpu(tgt_stats->stats.rx.byte);
1149	stats->rx_ucast_pkt += le32_to_cpu(tgt_stats->stats.rx.ucast_pkt);
1150	stats->rx_ucast_byte += le32_to_cpu(tgt_stats->stats.rx.ucast_byte);
1151	stats->rx_mcast_pkt += le32_to_cpu(tgt_stats->stats.rx.mcast_pkt);
1152	stats->rx_mcast_byte += le32_to_cpu(tgt_stats->stats.rx.mcast_byte);
1153	stats->rx_bcast_pkt += le32_to_cpu(tgt_stats->stats.rx.bcast_pkt);
1154	stats->rx_bcast_byte += le32_to_cpu(tgt_stats->stats.rx.bcast_byte);
1155	stats->rx_frgment_pkt += le32_to_cpu(tgt_stats->stats.rx.frgment_pkt);
1156	stats->rx_err += le32_to_cpu(tgt_stats->stats.rx.err);
1157	stats->rx_crc_err += le32_to_cpu(tgt_stats->stats.rx.crc_err);
1158	stats->rx_key_cache_miss +=
1159		le32_to_cpu(tgt_stats->stats.rx.key_cache_miss);
1160	stats->rx_decrypt_err += le32_to_cpu(tgt_stats->stats.rx.decrypt_err);
1161	stats->rx_dupl_frame += le32_to_cpu(tgt_stats->stats.rx.dupl_frame);
1162	stats->rx_ucast_rate =
1163	    ath6kl_wmi_get_rate(a_sle32_to_cpu(tgt_stats->stats.rx.ucast_rate));
1164
1165	ccmp_stats = &tgt_stats->stats.tkip_ccmp_stats;
1166
1167	stats->tkip_local_mic_fail +=
1168		le32_to_cpu(ccmp_stats->tkip_local_mic_fail);
1169	stats->tkip_cnter_measures_invoked +=
1170		le32_to_cpu(ccmp_stats->tkip_cnter_measures_invoked);
1171	stats->tkip_fmt_err += le32_to_cpu(ccmp_stats->tkip_fmt_err);
1172
1173	stats->ccmp_fmt_err += le32_to_cpu(ccmp_stats->ccmp_fmt_err);
1174	stats->ccmp_replays += le32_to_cpu(ccmp_stats->ccmp_replays);
1175
1176	stats->pwr_save_fail_cnt +=
1177		le32_to_cpu(tgt_stats->pm_stats.pwr_save_failure_cnt);
1178	stats->noise_floor_calib =
1179		a_sle32_to_cpu(tgt_stats->noise_floor_calib);
1180
1181	stats->cs_bmiss_cnt +=
1182		le32_to_cpu(tgt_stats->cserv_stats.cs_bmiss_cnt);
1183	stats->cs_low_rssi_cnt +=
1184		le32_to_cpu(tgt_stats->cserv_stats.cs_low_rssi_cnt);
1185	stats->cs_connect_cnt +=
1186		le16_to_cpu(tgt_stats->cserv_stats.cs_connect_cnt);
1187	stats->cs_discon_cnt +=
1188		le16_to_cpu(tgt_stats->cserv_stats.cs_discon_cnt);
1189
1190	stats->cs_ave_beacon_rssi =
1191		a_sle16_to_cpu(tgt_stats->cserv_stats.cs_ave_beacon_rssi);
1192
1193	stats->cs_last_roam_msec =
1194		tgt_stats->cserv_stats.cs_last_roam_msec;
1195	stats->cs_snr = tgt_stats->cserv_stats.cs_snr;
1196	stats->cs_rssi = a_sle16_to_cpu(tgt_stats->cserv_stats.cs_rssi);
1197
1198	stats->lq_val = le32_to_cpu(tgt_stats->lq_val);
1199
1200	stats->wow_pkt_dropped +=
1201		le32_to_cpu(tgt_stats->wow_stats.wow_pkt_dropped);
1202	stats->wow_host_pkt_wakeups +=
1203		tgt_stats->wow_stats.wow_host_pkt_wakeups;
1204	stats->wow_host_evt_wakeups +=
1205		tgt_stats->wow_stats.wow_host_evt_wakeups;
1206	stats->wow_evt_discarded +=
1207		le16_to_cpu(tgt_stats->wow_stats.wow_evt_discarded);
1208
1209	if (test_bit(STATS_UPDATE_PEND, &ar->flag)) {
1210		clear_bit(STATS_UPDATE_PEND, &ar->flag);
1211		wake_up(&ar->event_wq);
1212	}
1213}
1214
1215static void ath6kl_add_le32(__le32 *var, __le32 val)
1216{
1217	*var = cpu_to_le32(le32_to_cpu(*var) + le32_to_cpu(val));
1218}
1219
1220void ath6kl_tgt_stats_event(struct ath6kl *ar, u8 *ptr, u32 len)
1221{
1222	struct wmi_ap_mode_stat *p = (struct wmi_ap_mode_stat *) ptr;
1223	struct wmi_ap_mode_stat *ap = &ar->ap_stats;
1224	struct wmi_per_sta_stat *st_ap, *st_p;
1225	u8 ac;
1226
1227	if (ar->nw_type == AP_NETWORK) {
1228		if (len < sizeof(*p))
1229			return;
1230
1231		for (ac = 0; ac < AP_MAX_NUM_STA; ac++) {
1232			st_ap = &ap->sta[ac];
1233			st_p = &p->sta[ac];
1234
1235			ath6kl_add_le32(&st_ap->tx_bytes, st_p->tx_bytes);
1236			ath6kl_add_le32(&st_ap->tx_pkts, st_p->tx_pkts);
1237			ath6kl_add_le32(&st_ap->tx_error, st_p->tx_error);
1238			ath6kl_add_le32(&st_ap->tx_discard, st_p->tx_discard);
1239			ath6kl_add_le32(&st_ap->rx_bytes, st_p->rx_bytes);
1240			ath6kl_add_le32(&st_ap->rx_pkts, st_p->rx_pkts);
1241			ath6kl_add_le32(&st_ap->rx_error, st_p->rx_error);
1242			ath6kl_add_le32(&st_ap->rx_discard, st_p->rx_discard);
1243		}
1244
1245	} else {
1246		ath6kl_update_target_stats(ar, ptr, len);
1247	}
1248}
1249
1250void ath6kl_wakeup_event(void *dev)
1251{
1252	struct ath6kl *ar = (struct ath6kl *) dev;
1253
1254	wake_up(&ar->event_wq);
1255}
1256
1257void ath6kl_txpwr_rx_evt(void *devt, u8 tx_pwr)
1258{
1259	struct ath6kl *ar = (struct ath6kl *) devt;
1260
1261	ar->tx_pwr = tx_pwr;
1262	wake_up(&ar->event_wq);
1263}
1264
1265void ath6kl_pspoll_event(struct ath6kl *ar, u8 aid)
1266{
1267	struct ath6kl_sta *conn;
1268	struct sk_buff *skb;
1269	bool psq_empty = false;
1270
1271	conn = ath6kl_find_sta_by_aid(ar, aid);
1272
1273	if (!conn)
1274		return;
1275	/*
1276	 * Send out a packet queued on ps queue. When the ps queue
1277	 * becomes empty update the PVB for this station.
1278	 */
1279	spin_lock_bh(&conn->psq_lock);
1280	psq_empty  = skb_queue_empty(&conn->psq);
1281	spin_unlock_bh(&conn->psq_lock);
1282
1283	if (psq_empty)
1284		/* TODO: Send out a NULL data frame */
1285		return;
1286
1287	spin_lock_bh(&conn->psq_lock);
1288	skb = skb_dequeue(&conn->psq);
1289	spin_unlock_bh(&conn->psq_lock);
1290
1291	conn->sta_flags |= STA_PS_POLLED;
1292	ath6kl_data_tx(skb, ar->net_dev);
1293	conn->sta_flags &= ~STA_PS_POLLED;
1294
1295	spin_lock_bh(&conn->psq_lock);
1296	psq_empty  = skb_queue_empty(&conn->psq);
1297	spin_unlock_bh(&conn->psq_lock);
1298
1299	if (psq_empty)
1300		ath6kl_wmi_set_pvb_cmd(ar->wmi, conn->aid, 0);
1301}
1302
1303void ath6kl_dtimexpiry_event(struct ath6kl *ar)
1304{
1305	bool mcastq_empty = false;
1306	struct sk_buff *skb;
1307
1308	/*
1309	 * If there are no associated STAs, ignore the DTIM expiry event.
1310	 * There can be potential race conditions where the last associated
1311	 * STA may disconnect & before the host could clear the 'Indicate
1312	 * DTIM' request to the firmware, the firmware would have just
1313	 * indicated a DTIM expiry event. The race is between 'clear DTIM
1314	 * expiry cmd' going from the host to the firmware & the DTIM
1315	 * expiry event happening from the firmware to the host.
1316	 */
1317	if (!ar->sta_list_index)
1318		return;
1319
1320	spin_lock_bh(&ar->mcastpsq_lock);
1321	mcastq_empty = skb_queue_empty(&ar->mcastpsq);
1322	spin_unlock_bh(&ar->mcastpsq_lock);
1323
1324	if (mcastq_empty)
1325		return;
1326
1327	/* set the STA flag to dtim_expired for the frame to go out */
1328	set_bit(DTIM_EXPIRED, &ar->flag);
1329
1330	spin_lock_bh(&ar->mcastpsq_lock);
1331	while ((skb = skb_dequeue(&ar->mcastpsq)) != NULL) {
1332		spin_unlock_bh(&ar->mcastpsq_lock);
1333
1334		ath6kl_data_tx(skb, ar->net_dev);
1335
1336		spin_lock_bh(&ar->mcastpsq_lock);
1337	}
1338	spin_unlock_bh(&ar->mcastpsq_lock);
1339
1340	clear_bit(DTIM_EXPIRED, &ar->flag);
1341
1342	/* clear the LSB of the BitMapCtl field of the TIM IE */
1343	ath6kl_wmi_set_pvb_cmd(ar->wmi, MCAST_AID, 0);
1344}
1345
1346void ath6kl_disconnect_event(struct ath6kl *ar, u8 reason, u8 *bssid,
1347			     u8 assoc_resp_len, u8 *assoc_info,
1348			     u16 prot_reason_status)
1349{
1350	struct bss *wmi_ssid_node = NULL;
1351	unsigned long flags;
1352
1353	if (ar->nw_type == AP_NETWORK) {
1354		if (!ath6kl_remove_sta(ar, bssid, prot_reason_status))
1355			return;
1356
1357		/* if no more associated STAs, empty the mcast PS q */
1358		if (ar->sta_list_index == 0) {
1359			spin_lock_bh(&ar->mcastpsq_lock);
1360			skb_queue_purge(&ar->mcastpsq);
1361			spin_unlock_bh(&ar->mcastpsq_lock);
1362
1363			/* clear the LSB of the TIM IE's BitMapCtl field */
1364			if (test_bit(WMI_READY, &ar->flag))
1365				ath6kl_wmi_set_pvb_cmd(ar->wmi, MCAST_AID, 0);
1366		}
1367
1368		if (!is_broadcast_ether_addr(bssid)) {
1369			/* send event to application */
1370			cfg80211_del_sta(ar->net_dev, bssid, GFP_KERNEL);
1371		}
1372
1373		if (memcmp(ar->net_dev->dev_addr, bssid, ETH_ALEN) == 0)
1374			clear_bit(CONNECTED, &ar->flag);
1375		return;
1376	}
1377
1378	ath6kl_cfg80211_disconnect_event(ar, reason, bssid,
1379				       assoc_resp_len, assoc_info,
1380				       prot_reason_status);
1381
1382	aggr_reset_state(ar->aggr_cntxt);
1383
1384	del_timer(&ar->disconnect_timer);
1385
1386	ath6kl_dbg(ATH6KL_DBG_WLAN_CONNECT,
1387		   "disconnect reason is %d\n", reason);
1388
1389	/*
1390	 * If the event is due to disconnect cmd from the host, only they
1391	 * the target would stop trying to connect. Under any other
1392	 * condition, target would keep trying to connect.
1393	 */
1394	if (reason == DISCONNECT_CMD) {
1395		if (!ar->usr_bss_filter && test_bit(WMI_READY, &ar->flag))
1396			ath6kl_wmi_bssfilter_cmd(ar->wmi, NONE_BSS_FILTER, 0);
1397	} else {
1398		set_bit(CONNECT_PEND, &ar->flag);
1399		if (((reason == ASSOC_FAILED) &&
1400		    (prot_reason_status == 0x11)) ||
1401		    ((reason == ASSOC_FAILED) && (prot_reason_status == 0x0)
1402		     && (ar->reconnect_flag == 1))) {
1403			set_bit(CONNECTED, &ar->flag);
1404			return;
1405		}
1406	}
1407
1408	if ((reason == NO_NETWORK_AVAIL) && test_bit(WMI_READY, &ar->flag))  {
1409		ath6kl_wmi_node_free(ar->wmi, bssid);
1410
1411		/*
1412		 * In case any other same SSID nodes are present remove it,
1413		 * since those nodes also not available now.
1414		 */
1415		do {
1416			/*
1417			 * Find the nodes based on SSID and remove it
1418			 *
1419			 * Note: This case will not work out for
1420			 * Hidden-SSID
1421			 */
1422			wmi_ssid_node = ath6kl_wmi_find_ssid_node(ar->wmi,
1423								  ar->ssid,
1424								  ar->ssid_len,
1425								  false,
1426								  true);
1427
1428			if (wmi_ssid_node)
1429				ath6kl_wmi_node_free(ar->wmi,
1430						     wmi_ssid_node->ni_macaddr);
1431
1432		} while (wmi_ssid_node);
1433	}
1434
1435	/* update connect & link status atomically */
1436	spin_lock_irqsave(&ar->lock, flags);
1437	clear_bit(CONNECTED, &ar->flag);
1438	netif_carrier_off(ar->net_dev);
1439	spin_unlock_irqrestore(&ar->lock, flags);
1440
1441	if ((reason != CSERV_DISCONNECT) || (ar->reconnect_flag != 1))
1442		ar->reconnect_flag = 0;
1443
1444	if (reason != CSERV_DISCONNECT)
1445		ar->user_key_ctrl = 0;
1446
1447	netif_stop_queue(ar->net_dev);
1448	memset(ar->bssid, 0, sizeof(ar->bssid));
1449	ar->bss_ch = 0;
1450
1451	ath6kl_tx_data_cleanup(ar);
1452}
1453
1454static int ath6kl_open(struct net_device *dev)
1455{
1456	struct ath6kl *ar = ath6kl_priv(dev);
1457	unsigned long flags;
1458
1459	spin_lock_irqsave(&ar->lock, flags);
1460
1461	set_bit(WLAN_ENABLED, &ar->flag);
1462
1463	if (test_bit(CONNECTED, &ar->flag)) {
1464		netif_carrier_on(dev);
1465		netif_wake_queue(dev);
1466	} else
1467		netif_carrier_off(dev);
1468
1469	spin_unlock_irqrestore(&ar->lock, flags);
1470
1471	return 0;
1472}
1473
1474static int ath6kl_close(struct net_device *dev)
1475{
1476	struct ath6kl *ar = ath6kl_priv(dev);
1477
1478	netif_stop_queue(dev);
1479
1480	ath6kl_disconnect(ar);
1481
1482	if (test_bit(WMI_READY, &ar->flag)) {
1483		if (ath6kl_wmi_scanparams_cmd(ar->wmi, 0xFFFF, 0, 0, 0, 0, 0, 0,
1484					      0, 0, 0))
1485			return -EIO;
1486
1487		clear_bit(WLAN_ENABLED, &ar->flag);
1488	}
1489
1490	ath6kl_cfg80211_scan_complete_event(ar, -ECANCELED);
1491
1492	return 0;
1493}
1494
1495static struct net_device_stats *ath6kl_get_stats(struct net_device *dev)
1496{
1497	struct ath6kl *ar = ath6kl_priv(dev);
1498
1499	return &ar->net_stats;
1500}
1501
1502static struct net_device_ops ath6kl_netdev_ops = {
1503	.ndo_open               = ath6kl_open,
1504	.ndo_stop               = ath6kl_close,
1505	.ndo_start_xmit         = ath6kl_data_tx,
1506	.ndo_get_stats          = ath6kl_get_stats,
1507};
1508
1509void init_netdev(struct net_device *dev)
1510{
1511	dev->netdev_ops = &ath6kl_netdev_ops;
1512	dev->watchdog_timeo = ATH6KL_TX_TIMEOUT;
1513
1514	dev->needed_headroom = ETH_HLEN;
1515	dev->needed_headroom += sizeof(struct ath6kl_llc_snap_hdr) +
1516				sizeof(struct wmi_data_hdr) + HTC_HDR_LENGTH
1517				+ WMI_MAX_TX_META_SZ + ATH6KL_HTC_ALIGN_BYTES;
1518
1519	return;
1520}
1521