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