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