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