xref: /openbmc/linux/drivers/net/wireless/ath/ath6kl/wmi.c (revision 5b628549)
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <linux/ip.h>
19 #include <linux/in.h>
20 #include "core.h"
21 #include "debug.h"
22 #include "testmode.h"
23 #include "trace.h"
24 #include "../regd.h"
25 #include "../regd_common.h"
26 
27 static int ath6kl_wmi_sync_point(struct wmi *wmi, u8 if_idx);
28 
29 static const s32 wmi_rate_tbl[][2] = {
30 	/* {W/O SGI, with SGI} */
31 	{1000, 1000},
32 	{2000, 2000},
33 	{5500, 5500},
34 	{11000, 11000},
35 	{6000, 6000},
36 	{9000, 9000},
37 	{12000, 12000},
38 	{18000, 18000},
39 	{24000, 24000},
40 	{36000, 36000},
41 	{48000, 48000},
42 	{54000, 54000},
43 	{6500, 7200},
44 	{13000, 14400},
45 	{19500, 21700},
46 	{26000, 28900},
47 	{39000, 43300},
48 	{52000, 57800},
49 	{58500, 65000},
50 	{65000, 72200},
51 	{13500, 15000},
52 	{27000, 30000},
53 	{40500, 45000},
54 	{54000, 60000},
55 	{81000, 90000},
56 	{108000, 120000},
57 	{121500, 135000},
58 	{135000, 150000},
59 	{0, 0}
60 };
61 
62 static const s32 wmi_rate_tbl_mcs15[][2] = {
63 	/* {W/O SGI, with SGI} */
64 	{1000, 1000},
65 	{2000, 2000},
66 	{5500, 5500},
67 	{11000, 11000},
68 	{6000, 6000},
69 	{9000, 9000},
70 	{12000, 12000},
71 	{18000, 18000},
72 	{24000, 24000},
73 	{36000, 36000},
74 	{48000, 48000},
75 	{54000, 54000},
76 	{6500, 7200},     /* HT 20, MCS 0 */
77 	{13000, 14400},
78 	{19500, 21700},
79 	{26000, 28900},
80 	{39000, 43300},
81 	{52000, 57800},
82 	{58500, 65000},
83 	{65000, 72200},
84 	{13000, 14400},   /* HT 20, MCS 8 */
85 	{26000, 28900},
86 	{39000, 43300},
87 	{52000, 57800},
88 	{78000, 86700},
89 	{104000, 115600},
90 	{117000, 130000},
91 	{130000, 144400}, /* HT 20, MCS 15 */
92 	{13500, 15000},   /*HT 40, MCS 0 */
93 	{27000, 30000},
94 	{40500, 45000},
95 	{54000, 60000},
96 	{81000, 90000},
97 	{108000, 120000},
98 	{121500, 135000},
99 	{135000, 150000},
100 	{27000, 30000},   /*HT 40, MCS 8 */
101 	{54000, 60000},
102 	{81000, 90000},
103 	{108000, 120000},
104 	{162000, 180000},
105 	{216000, 240000},
106 	{243000, 270000},
107 	{270000, 300000}, /*HT 40, MCS 15 */
108 	{0, 0}
109 };
110 
111 /* 802.1d to AC mapping. Refer pg 57 of WMM-test-plan-v1.2 */
112 static const u8 up_to_ac[] = {
113 	WMM_AC_BE,
114 	WMM_AC_BK,
115 	WMM_AC_BK,
116 	WMM_AC_BE,
117 	WMM_AC_VI,
118 	WMM_AC_VI,
119 	WMM_AC_VO,
120 	WMM_AC_VO,
121 };
122 
123 void ath6kl_wmi_set_control_ep(struct wmi *wmi, enum htc_endpoint_id ep_id)
124 {
125 	if (WARN_ON(ep_id == ENDPOINT_UNUSED || ep_id >= ENDPOINT_MAX))
126 		return;
127 
128 	wmi->ep_id = ep_id;
129 }
130 
131 enum htc_endpoint_id ath6kl_wmi_get_control_ep(struct wmi *wmi)
132 {
133 	return wmi->ep_id;
134 }
135 
136 struct ath6kl_vif *ath6kl_get_vif_by_index(struct ath6kl *ar, u8 if_idx)
137 {
138 	struct ath6kl_vif *vif, *found = NULL;
139 
140 	if (WARN_ON(if_idx > (ar->vif_max - 1)))
141 		return NULL;
142 
143 	/* FIXME: Locking */
144 	spin_lock_bh(&ar->list_lock);
145 	list_for_each_entry(vif, &ar->vif_list, list) {
146 		if (vif->fw_vif_idx == if_idx) {
147 			found = vif;
148 			break;
149 		}
150 	}
151 	spin_unlock_bh(&ar->list_lock);
152 
153 	return found;
154 }
155 
156 /*  Performs DIX to 802.3 encapsulation for transmit packets.
157  *  Assumes the entire DIX header is contiguous and that there is
158  *  enough room in the buffer for a 802.3 mac header and LLC+SNAP headers.
159  */
160 int ath6kl_wmi_dix_2_dot3(struct wmi *wmi, struct sk_buff *skb)
161 {
162 	struct ath6kl_llc_snap_hdr *llc_hdr;
163 	struct ethhdr *eth_hdr;
164 	size_t new_len;
165 	__be16 type;
166 	u8 *datap;
167 	u16 size;
168 
169 	if (WARN_ON(skb == NULL))
170 		return -EINVAL;
171 
172 	size = sizeof(struct ath6kl_llc_snap_hdr) + sizeof(struct wmi_data_hdr);
173 	if (skb_headroom(skb) < size)
174 		return -ENOMEM;
175 
176 	eth_hdr = (struct ethhdr *) skb->data;
177 	type = eth_hdr->h_proto;
178 
179 	if (!is_ethertype(be16_to_cpu(type))) {
180 		ath6kl_dbg(ATH6KL_DBG_WMI,
181 			   "%s: pkt is already in 802.3 format\n", __func__);
182 		return 0;
183 	}
184 
185 	new_len = skb->len - sizeof(*eth_hdr) + sizeof(*llc_hdr);
186 
187 	skb_push(skb, sizeof(struct ath6kl_llc_snap_hdr));
188 	datap = skb->data;
189 
190 	eth_hdr->h_proto = cpu_to_be16(new_len);
191 
192 	memcpy(datap, eth_hdr, sizeof(*eth_hdr));
193 
194 	llc_hdr = (struct ath6kl_llc_snap_hdr *)(datap + sizeof(*eth_hdr));
195 	llc_hdr->dsap = 0xAA;
196 	llc_hdr->ssap = 0xAA;
197 	llc_hdr->cntl = 0x03;
198 	llc_hdr->org_code[0] = 0x0;
199 	llc_hdr->org_code[1] = 0x0;
200 	llc_hdr->org_code[2] = 0x0;
201 	llc_hdr->eth_type = type;
202 
203 	return 0;
204 }
205 
206 static int ath6kl_wmi_meta_add(struct wmi *wmi, struct sk_buff *skb,
207 			       u8 *version, void *tx_meta_info)
208 {
209 	struct wmi_tx_meta_v1 *v1;
210 	struct wmi_tx_meta_v2 *v2;
211 
212 	if (WARN_ON(skb == NULL || version == NULL))
213 		return -EINVAL;
214 
215 	switch (*version) {
216 	case WMI_META_VERSION_1:
217 		skb_push(skb, WMI_MAX_TX_META_SZ);
218 		v1 = (struct wmi_tx_meta_v1 *) skb->data;
219 		v1->pkt_id = 0;
220 		v1->rate_plcy_id = 0;
221 		*version = WMI_META_VERSION_1;
222 		break;
223 	case WMI_META_VERSION_2:
224 		skb_push(skb, WMI_MAX_TX_META_SZ);
225 		v2 = (struct wmi_tx_meta_v2 *) skb->data;
226 		memcpy(v2, (struct wmi_tx_meta_v2 *) tx_meta_info,
227 		       sizeof(struct wmi_tx_meta_v2));
228 		break;
229 	}
230 
231 	return 0;
232 }
233 
234 int ath6kl_wmi_data_hdr_add(struct wmi *wmi, struct sk_buff *skb,
235 			    u8 msg_type, u32 flags,
236 			    enum wmi_data_hdr_data_type data_type,
237 			    u8 meta_ver, void *tx_meta_info, u8 if_idx)
238 {
239 	struct wmi_data_hdr *data_hdr;
240 	int ret;
241 
242 	if (WARN_ON(skb == NULL || (if_idx > wmi->parent_dev->vif_max - 1)))
243 		return -EINVAL;
244 
245 	if (tx_meta_info) {
246 		ret = ath6kl_wmi_meta_add(wmi, skb, &meta_ver, tx_meta_info);
247 		if (ret)
248 			return ret;
249 	}
250 
251 	skb_push(skb, sizeof(struct wmi_data_hdr));
252 
253 	data_hdr = (struct wmi_data_hdr *)skb->data;
254 	memset(data_hdr, 0, sizeof(struct wmi_data_hdr));
255 
256 	data_hdr->info = msg_type << WMI_DATA_HDR_MSG_TYPE_SHIFT;
257 	data_hdr->info |= data_type << WMI_DATA_HDR_DATA_TYPE_SHIFT;
258 
259 	if (flags & WMI_DATA_HDR_FLAGS_MORE)
260 		data_hdr->info |= WMI_DATA_HDR_MORE;
261 
262 	if (flags & WMI_DATA_HDR_FLAGS_EOSP)
263 		data_hdr->info3 |= cpu_to_le16(WMI_DATA_HDR_EOSP);
264 
265 	data_hdr->info2 |= cpu_to_le16(meta_ver << WMI_DATA_HDR_META_SHIFT);
266 	data_hdr->info3 |= cpu_to_le16(if_idx & WMI_DATA_HDR_IF_IDX_MASK);
267 
268 	return 0;
269 }
270 
271 u8 ath6kl_wmi_determine_user_priority(u8 *pkt, u32 layer2_pri)
272 {
273 	struct iphdr *ip_hdr = (struct iphdr *) pkt;
274 	u8 ip_pri;
275 
276 	/*
277 	 * Determine IPTOS priority
278 	 *
279 	 * IP-TOS - 8bits
280 	 *          : DSCP(6-bits) ECN(2-bits)
281 	 *          : DSCP - P2 P1 P0 X X X
282 	 * where (P2 P1 P0) form 802.1D
283 	 */
284 	ip_pri = ip_hdr->tos >> 5;
285 	ip_pri &= 0x7;
286 
287 	if ((layer2_pri & 0x7) > ip_pri)
288 		return (u8) layer2_pri & 0x7;
289 	else
290 		return ip_pri;
291 }
292 
293 u8 ath6kl_wmi_get_traffic_class(u8 user_priority)
294 {
295 	return  up_to_ac[user_priority & 0x7];
296 }
297 
298 int ath6kl_wmi_implicit_create_pstream(struct wmi *wmi, u8 if_idx,
299 				       struct sk_buff *skb,
300 				       u32 layer2_priority, bool wmm_enabled,
301 				       u8 *ac)
302 {
303 	struct wmi_data_hdr *data_hdr;
304 	struct ath6kl_llc_snap_hdr *llc_hdr;
305 	struct wmi_create_pstream_cmd cmd;
306 	u32 meta_size, hdr_size;
307 	u16 ip_type = IP_ETHERTYPE;
308 	u8 stream_exist, usr_pri;
309 	u8 traffic_class = WMM_AC_BE;
310 	u8 *datap;
311 
312 	if (WARN_ON(skb == NULL))
313 		return -EINVAL;
314 
315 	datap = skb->data;
316 	data_hdr = (struct wmi_data_hdr *) datap;
317 
318 	meta_size = ((le16_to_cpu(data_hdr->info2) >> WMI_DATA_HDR_META_SHIFT) &
319 		     WMI_DATA_HDR_META_MASK) ? WMI_MAX_TX_META_SZ : 0;
320 
321 	if (!wmm_enabled) {
322 		/* If WMM is disabled all traffic goes as BE traffic */
323 		usr_pri = 0;
324 	} else {
325 		hdr_size = sizeof(struct ethhdr);
326 
327 		llc_hdr = (struct ath6kl_llc_snap_hdr *)(datap +
328 							 sizeof(struct
329 								wmi_data_hdr) +
330 							 meta_size + hdr_size);
331 
332 		if (llc_hdr->eth_type == htons(ip_type)) {
333 			/*
334 			 * Extract the endpoint info from the TOS field
335 			 * in the IP header.
336 			 */
337 			usr_pri =
338 			   ath6kl_wmi_determine_user_priority(((u8 *) llc_hdr) +
339 					sizeof(struct ath6kl_llc_snap_hdr),
340 					layer2_priority);
341 		} else {
342 			usr_pri = layer2_priority & 0x7;
343 		}
344 
345 		/*
346 		 * Queue the EAPOL frames in the same WMM_AC_VO queue
347 		 * as that of management frames.
348 		 */
349 		if (skb->protocol == cpu_to_be16(ETH_P_PAE))
350 			usr_pri = WMI_VOICE_USER_PRIORITY;
351 	}
352 
353 	/*
354 	 * workaround for WMM S5
355 	 *
356 	 * FIXME: wmi->traffic_class is always 100 so this test doesn't
357 	 * make sense
358 	 */
359 	if ((wmi->traffic_class == WMM_AC_VI) &&
360 	    ((usr_pri == 5) || (usr_pri == 4)))
361 		usr_pri = 1;
362 
363 	/* Convert user priority to traffic class */
364 	traffic_class = up_to_ac[usr_pri & 0x7];
365 
366 	wmi_data_hdr_set_up(data_hdr, usr_pri);
367 
368 	spin_lock_bh(&wmi->lock);
369 	stream_exist = wmi->fat_pipe_exist;
370 	spin_unlock_bh(&wmi->lock);
371 
372 	if (!(stream_exist & (1 << traffic_class))) {
373 		memset(&cmd, 0, sizeof(cmd));
374 		cmd.traffic_class = traffic_class;
375 		cmd.user_pri = usr_pri;
376 		cmd.inactivity_int =
377 			cpu_to_le32(WMI_IMPLICIT_PSTREAM_INACTIVITY_INT);
378 		/* Implicit streams are created with TSID 0xFF */
379 		cmd.tsid = WMI_IMPLICIT_PSTREAM;
380 		ath6kl_wmi_create_pstream_cmd(wmi, if_idx, &cmd);
381 	}
382 
383 	*ac = traffic_class;
384 
385 	return 0;
386 }
387 
388 int ath6kl_wmi_dot11_hdr_remove(struct wmi *wmi, struct sk_buff *skb)
389 {
390 	struct ieee80211_hdr_3addr *pwh, wh;
391 	struct ath6kl_llc_snap_hdr *llc_hdr;
392 	struct ethhdr eth_hdr;
393 	u32 hdr_size;
394 	u8 *datap;
395 	__le16 sub_type;
396 
397 	if (WARN_ON(skb == NULL))
398 		return -EINVAL;
399 
400 	datap = skb->data;
401 	pwh = (struct ieee80211_hdr_3addr *) datap;
402 
403 	sub_type = pwh->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
404 
405 	memcpy((u8 *) &wh, datap, sizeof(struct ieee80211_hdr_3addr));
406 
407 	/* Strip off the 802.11 header */
408 	if (sub_type == cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
409 		hdr_size = roundup(sizeof(struct ieee80211_qos_hdr),
410 				   sizeof(u32));
411 		skb_pull(skb, hdr_size);
412 	} else if (sub_type == cpu_to_le16(IEEE80211_STYPE_DATA)) {
413 		skb_pull(skb, sizeof(struct ieee80211_hdr_3addr));
414 	}
415 
416 	datap = skb->data;
417 	llc_hdr = (struct ath6kl_llc_snap_hdr *)(datap);
418 
419 	memset(&eth_hdr, 0, sizeof(eth_hdr));
420 	eth_hdr.h_proto = llc_hdr->eth_type;
421 
422 	switch ((le16_to_cpu(wh.frame_control)) &
423 		(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
424 	case IEEE80211_FCTL_TODS:
425 		memcpy(eth_hdr.h_dest, wh.addr3, ETH_ALEN);
426 		memcpy(eth_hdr.h_source, wh.addr2, ETH_ALEN);
427 		break;
428 	case IEEE80211_FCTL_FROMDS:
429 		memcpy(eth_hdr.h_dest, wh.addr1, ETH_ALEN);
430 		memcpy(eth_hdr.h_source, wh.addr3, ETH_ALEN);
431 		break;
432 	case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
433 		break;
434 	default:
435 		memcpy(eth_hdr.h_dest, wh.addr1, ETH_ALEN);
436 		memcpy(eth_hdr.h_source, wh.addr2, ETH_ALEN);
437 		break;
438 	}
439 
440 	skb_pull(skb, sizeof(struct ath6kl_llc_snap_hdr));
441 	skb_push(skb, sizeof(eth_hdr));
442 
443 	datap = skb->data;
444 
445 	memcpy(datap, &eth_hdr, sizeof(eth_hdr));
446 
447 	return 0;
448 }
449 
450 /*
451  * Performs 802.3 to DIX encapsulation for received packets.
452  * Assumes the entire 802.3 header is contiguous.
453  */
454 int ath6kl_wmi_dot3_2_dix(struct sk_buff *skb)
455 {
456 	struct ath6kl_llc_snap_hdr *llc_hdr;
457 	struct ethhdr eth_hdr;
458 	u8 *datap;
459 
460 	if (WARN_ON(skb == NULL))
461 		return -EINVAL;
462 
463 	datap = skb->data;
464 
465 	memcpy(&eth_hdr, datap, sizeof(eth_hdr));
466 
467 	llc_hdr = (struct ath6kl_llc_snap_hdr *) (datap + sizeof(eth_hdr));
468 	eth_hdr.h_proto = llc_hdr->eth_type;
469 
470 	skb_pull(skb, sizeof(struct ath6kl_llc_snap_hdr));
471 	datap = skb->data;
472 
473 	memcpy(datap, &eth_hdr, sizeof(eth_hdr));
474 
475 	return 0;
476 }
477 
478 static int ath6kl_wmi_tx_complete_event_rx(u8 *datap, int len)
479 {
480 	struct tx_complete_msg_v1 *msg_v1;
481 	struct wmi_tx_complete_event *evt;
482 	int index;
483 	u16 size;
484 
485 	evt = (struct wmi_tx_complete_event *) datap;
486 
487 	ath6kl_dbg(ATH6KL_DBG_WMI, "comp: %d %d %d\n",
488 		   evt->num_msg, evt->msg_len, evt->msg_type);
489 
490 	for (index = 0; index < evt->num_msg; index++) {
491 		size = sizeof(struct wmi_tx_complete_event) +
492 		    (index * sizeof(struct tx_complete_msg_v1));
493 		msg_v1 = (struct tx_complete_msg_v1 *)(datap + size);
494 
495 		ath6kl_dbg(ATH6KL_DBG_WMI, "msg: %d %d %d %d\n",
496 			   msg_v1->status, msg_v1->pkt_id,
497 			   msg_v1->rate_idx, msg_v1->ack_failures);
498 	}
499 
500 	return 0;
501 }
502 
503 static int ath6kl_wmi_remain_on_chnl_event_rx(struct wmi *wmi, u8 *datap,
504 					      int len, struct ath6kl_vif *vif)
505 {
506 	struct wmi_remain_on_chnl_event *ev;
507 	u32 freq;
508 	u32 dur;
509 	struct ieee80211_channel *chan;
510 	struct ath6kl *ar = wmi->parent_dev;
511 	u32 id;
512 
513 	if (len < sizeof(*ev))
514 		return -EINVAL;
515 
516 	ev = (struct wmi_remain_on_chnl_event *) datap;
517 	freq = le32_to_cpu(ev->freq);
518 	dur = le32_to_cpu(ev->duration);
519 	ath6kl_dbg(ATH6KL_DBG_WMI, "remain_on_chnl: freq=%u dur=%u\n",
520 		   freq, dur);
521 	chan = ieee80211_get_channel(ar->wiphy, freq);
522 	if (!chan) {
523 		ath6kl_dbg(ATH6KL_DBG_WMI,
524 			   "remain_on_chnl: Unknown channel (freq=%u)\n",
525 			   freq);
526 		return -EINVAL;
527 	}
528 	id = vif->last_roc_id;
529 	cfg80211_ready_on_channel(&vif->wdev, id, chan,
530 				  dur, GFP_ATOMIC);
531 
532 	return 0;
533 }
534 
535 static int ath6kl_wmi_cancel_remain_on_chnl_event_rx(struct wmi *wmi,
536 						     u8 *datap, int len,
537 						     struct ath6kl_vif *vif)
538 {
539 	struct wmi_cancel_remain_on_chnl_event *ev;
540 	u32 freq;
541 	u32 dur;
542 	struct ieee80211_channel *chan;
543 	struct ath6kl *ar = wmi->parent_dev;
544 	u32 id;
545 
546 	if (len < sizeof(*ev))
547 		return -EINVAL;
548 
549 	ev = (struct wmi_cancel_remain_on_chnl_event *) datap;
550 	freq = le32_to_cpu(ev->freq);
551 	dur = le32_to_cpu(ev->duration);
552 	ath6kl_dbg(ATH6KL_DBG_WMI,
553 		   "cancel_remain_on_chnl: freq=%u dur=%u status=%u\n",
554 		   freq, dur, ev->status);
555 	chan = ieee80211_get_channel(ar->wiphy, freq);
556 	if (!chan) {
557 		ath6kl_dbg(ATH6KL_DBG_WMI,
558 			   "cancel_remain_on_chnl: Unknown channel (freq=%u)\n",
559 			   freq);
560 		return -EINVAL;
561 	}
562 	if (vif->last_cancel_roc_id &&
563 	    vif->last_cancel_roc_id + 1 == vif->last_roc_id)
564 		id = vif->last_cancel_roc_id; /* event for cancel command */
565 	else
566 		id = vif->last_roc_id; /* timeout on uncanceled r-o-c */
567 	vif->last_cancel_roc_id = 0;
568 	cfg80211_remain_on_channel_expired(&vif->wdev, id, chan, GFP_ATOMIC);
569 
570 	return 0;
571 }
572 
573 static int ath6kl_wmi_tx_status_event_rx(struct wmi *wmi, u8 *datap, int len,
574 					 struct ath6kl_vif *vif)
575 {
576 	struct wmi_tx_status_event *ev;
577 	u32 id;
578 
579 	if (len < sizeof(*ev))
580 		return -EINVAL;
581 
582 	ev = (struct wmi_tx_status_event *) datap;
583 	id = le32_to_cpu(ev->id);
584 	ath6kl_dbg(ATH6KL_DBG_WMI, "tx_status: id=%x ack_status=%u\n",
585 		   id, ev->ack_status);
586 	if (wmi->last_mgmt_tx_frame) {
587 		cfg80211_mgmt_tx_status(&vif->wdev, id,
588 					wmi->last_mgmt_tx_frame,
589 					wmi->last_mgmt_tx_frame_len,
590 					!!ev->ack_status, GFP_ATOMIC);
591 		kfree(wmi->last_mgmt_tx_frame);
592 		wmi->last_mgmt_tx_frame = NULL;
593 		wmi->last_mgmt_tx_frame_len = 0;
594 	}
595 
596 	return 0;
597 }
598 
599 static int ath6kl_wmi_rx_probe_req_event_rx(struct wmi *wmi, u8 *datap, int len,
600 					    struct ath6kl_vif *vif)
601 {
602 	struct wmi_p2p_rx_probe_req_event *ev;
603 	u32 freq;
604 	u16 dlen;
605 
606 	if (len < sizeof(*ev))
607 		return -EINVAL;
608 
609 	ev = (struct wmi_p2p_rx_probe_req_event *) datap;
610 	freq = le32_to_cpu(ev->freq);
611 	dlen = le16_to_cpu(ev->len);
612 	if (datap + len < ev->data + dlen) {
613 		ath6kl_err("invalid wmi_p2p_rx_probe_req_event: len=%d dlen=%u\n",
614 			   len, dlen);
615 		return -EINVAL;
616 	}
617 	ath6kl_dbg(ATH6KL_DBG_WMI,
618 		   "rx_probe_req: len=%u freq=%u probe_req_report=%d\n",
619 		   dlen, freq, vif->probe_req_report);
620 
621 	if (vif->probe_req_report || vif->nw_type == AP_NETWORK)
622 		cfg80211_rx_mgmt(&vif->wdev, freq, 0, ev->data, dlen, 0);
623 
624 	return 0;
625 }
626 
627 static int ath6kl_wmi_p2p_capabilities_event_rx(u8 *datap, int len)
628 {
629 	struct wmi_p2p_capabilities_event *ev;
630 	u16 dlen;
631 
632 	if (len < sizeof(*ev))
633 		return -EINVAL;
634 
635 	ev = (struct wmi_p2p_capabilities_event *) datap;
636 	dlen = le16_to_cpu(ev->len);
637 	ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_capab: len=%u\n", dlen);
638 
639 	return 0;
640 }
641 
642 static int ath6kl_wmi_rx_action_event_rx(struct wmi *wmi, u8 *datap, int len,
643 					 struct ath6kl_vif *vif)
644 {
645 	struct wmi_rx_action_event *ev;
646 	u32 freq;
647 	u16 dlen;
648 
649 	if (len < sizeof(*ev))
650 		return -EINVAL;
651 
652 	ev = (struct wmi_rx_action_event *) datap;
653 	freq = le32_to_cpu(ev->freq);
654 	dlen = le16_to_cpu(ev->len);
655 	if (datap + len < ev->data + dlen) {
656 		ath6kl_err("invalid wmi_rx_action_event: len=%d dlen=%u\n",
657 			   len, dlen);
658 		return -EINVAL;
659 	}
660 	ath6kl_dbg(ATH6KL_DBG_WMI, "rx_action: len=%u freq=%u\n", dlen, freq);
661 	cfg80211_rx_mgmt(&vif->wdev, freq, 0, ev->data, dlen, 0);
662 
663 	return 0;
664 }
665 
666 static int ath6kl_wmi_p2p_info_event_rx(u8 *datap, int len)
667 {
668 	struct wmi_p2p_info_event *ev;
669 	u32 flags;
670 	u16 dlen;
671 
672 	if (len < sizeof(*ev))
673 		return -EINVAL;
674 
675 	ev = (struct wmi_p2p_info_event *) datap;
676 	flags = le32_to_cpu(ev->info_req_flags);
677 	dlen = le16_to_cpu(ev->len);
678 	ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_info: flags=%x len=%d\n", flags, dlen);
679 
680 	if (flags & P2P_FLAG_CAPABILITIES_REQ) {
681 		struct wmi_p2p_capabilities *cap;
682 		if (dlen < sizeof(*cap))
683 			return -EINVAL;
684 		cap = (struct wmi_p2p_capabilities *) ev->data;
685 		ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_info: GO Power Save = %d\n",
686 			   cap->go_power_save);
687 	}
688 
689 	if (flags & P2P_FLAG_MACADDR_REQ) {
690 		struct wmi_p2p_macaddr *mac;
691 		if (dlen < sizeof(*mac))
692 			return -EINVAL;
693 		mac = (struct wmi_p2p_macaddr *) ev->data;
694 		ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_info: MAC Address = %pM\n",
695 			   mac->mac_addr);
696 	}
697 
698 	if (flags & P2P_FLAG_HMODEL_REQ) {
699 		struct wmi_p2p_hmodel *mod;
700 		if (dlen < sizeof(*mod))
701 			return -EINVAL;
702 		mod = (struct wmi_p2p_hmodel *) ev->data;
703 		ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_info: P2P Model = %d (%s)\n",
704 			   mod->p2p_model,
705 			   mod->p2p_model ? "host" : "firmware");
706 	}
707 	return 0;
708 }
709 
710 static inline struct sk_buff *ath6kl_wmi_get_new_buf(u32 size)
711 {
712 	struct sk_buff *skb;
713 
714 	skb = ath6kl_buf_alloc(size);
715 	if (!skb)
716 		return NULL;
717 
718 	skb_put(skb, size);
719 	if (size)
720 		memset(skb->data, 0, size);
721 
722 	return skb;
723 }
724 
725 /* Send a "simple" wmi command -- one with no arguments */
726 static int ath6kl_wmi_simple_cmd(struct wmi *wmi, u8 if_idx,
727 				 enum wmi_cmd_id cmd_id)
728 {
729 	struct sk_buff *skb;
730 	int ret;
731 
732 	skb = ath6kl_wmi_get_new_buf(0);
733 	if (!skb)
734 		return -ENOMEM;
735 
736 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, cmd_id, NO_SYNC_WMIFLAG);
737 
738 	return ret;
739 }
740 
741 static int ath6kl_wmi_ready_event_rx(struct wmi *wmi, u8 *datap, int len)
742 {
743 	struct wmi_ready_event_2 *ev = (struct wmi_ready_event_2 *) datap;
744 
745 	if (len < sizeof(struct wmi_ready_event_2))
746 		return -EINVAL;
747 
748 	ath6kl_ready_event(wmi->parent_dev, ev->mac_addr,
749 			   le32_to_cpu(ev->sw_version),
750 			   le32_to_cpu(ev->abi_version), ev->phy_cap);
751 
752 	return 0;
753 }
754 
755 /*
756  * Mechanism to modify the roaming behavior in the firmware. The lower rssi
757  * at which the station has to roam can be passed with
758  * WMI_SET_LRSSI_SCAN_PARAMS. Subtract 96 from RSSI to get the signal level
759  * in dBm.
760  */
761 int ath6kl_wmi_set_roam_lrssi_cmd(struct wmi *wmi, u8 lrssi)
762 {
763 	struct sk_buff *skb;
764 	struct roam_ctrl_cmd *cmd;
765 
766 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
767 	if (!skb)
768 		return -ENOMEM;
769 
770 	cmd = (struct roam_ctrl_cmd *) skb->data;
771 
772 	cmd->info.params.lrssi_scan_period = cpu_to_le16(DEF_LRSSI_SCAN_PERIOD);
773 	cmd->info.params.lrssi_scan_threshold = a_cpu_to_sle16(lrssi +
774 						       DEF_SCAN_FOR_ROAM_INTVL);
775 	cmd->info.params.lrssi_roam_threshold = a_cpu_to_sle16(lrssi);
776 	cmd->info.params.roam_rssi_floor = DEF_LRSSI_ROAM_FLOOR;
777 	cmd->roam_ctrl = WMI_SET_LRSSI_SCAN_PARAMS;
778 
779 	return ath6kl_wmi_cmd_send(wmi, 0, skb, WMI_SET_ROAM_CTRL_CMDID,
780 			    NO_SYNC_WMIFLAG);
781 }
782 
783 int ath6kl_wmi_force_roam_cmd(struct wmi *wmi, const u8 *bssid)
784 {
785 	struct sk_buff *skb;
786 	struct roam_ctrl_cmd *cmd;
787 
788 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
789 	if (!skb)
790 		return -ENOMEM;
791 
792 	cmd = (struct roam_ctrl_cmd *) skb->data;
793 
794 	memcpy(cmd->info.bssid, bssid, ETH_ALEN);
795 	cmd->roam_ctrl = WMI_FORCE_ROAM;
796 
797 	ath6kl_dbg(ATH6KL_DBG_WMI, "force roam to %pM\n", bssid);
798 	return ath6kl_wmi_cmd_send(wmi, 0, skb, WMI_SET_ROAM_CTRL_CMDID,
799 				   NO_SYNC_WMIFLAG);
800 }
801 
802 int ath6kl_wmi_ap_set_beacon_intvl_cmd(struct wmi *wmi, u8 if_idx,
803 				       u32 beacon_intvl)
804 {
805 	struct sk_buff *skb;
806 	struct set_beacon_int_cmd *cmd;
807 
808 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
809 	if (!skb)
810 		return -ENOMEM;
811 
812 	cmd = (struct set_beacon_int_cmd *) skb->data;
813 
814 	cmd->beacon_intvl = cpu_to_le32(beacon_intvl);
815 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb,
816 				   WMI_SET_BEACON_INT_CMDID, NO_SYNC_WMIFLAG);
817 }
818 
819 int ath6kl_wmi_ap_set_dtim_cmd(struct wmi *wmi, u8 if_idx, u32 dtim_period)
820 {
821 	struct sk_buff *skb;
822 	struct set_dtim_cmd *cmd;
823 
824 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
825 	if (!skb)
826 		return -ENOMEM;
827 
828 	cmd = (struct set_dtim_cmd *) skb->data;
829 
830 	cmd->dtim_period = cpu_to_le32(dtim_period);
831 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb,
832 				   WMI_AP_SET_DTIM_CMDID, NO_SYNC_WMIFLAG);
833 }
834 
835 int ath6kl_wmi_set_roam_mode_cmd(struct wmi *wmi, enum wmi_roam_mode mode)
836 {
837 	struct sk_buff *skb;
838 	struct roam_ctrl_cmd *cmd;
839 
840 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
841 	if (!skb)
842 		return -ENOMEM;
843 
844 	cmd = (struct roam_ctrl_cmd *) skb->data;
845 
846 	cmd->info.roam_mode = mode;
847 	cmd->roam_ctrl = WMI_SET_ROAM_MODE;
848 
849 	ath6kl_dbg(ATH6KL_DBG_WMI, "set roam mode %d\n", mode);
850 	return ath6kl_wmi_cmd_send(wmi, 0, skb, WMI_SET_ROAM_CTRL_CMDID,
851 				   NO_SYNC_WMIFLAG);
852 }
853 
854 static int ath6kl_wmi_connect_event_rx(struct wmi *wmi, u8 *datap, int len,
855 				       struct ath6kl_vif *vif)
856 {
857 	struct wmi_connect_event *ev;
858 	u8 *pie, *peie;
859 
860 	if (len < sizeof(struct wmi_connect_event))
861 		return -EINVAL;
862 
863 	ev = (struct wmi_connect_event *) datap;
864 
865 	if (vif->nw_type == AP_NETWORK) {
866 		/* AP mode start/STA connected event */
867 		struct net_device *dev = vif->ndev;
868 		if (memcmp(dev->dev_addr, ev->u.ap_bss.bssid, ETH_ALEN) == 0) {
869 			ath6kl_dbg(ATH6KL_DBG_WMI,
870 				   "%s: freq %d bssid %pM (AP started)\n",
871 				   __func__, le16_to_cpu(ev->u.ap_bss.ch),
872 				   ev->u.ap_bss.bssid);
873 			ath6kl_connect_ap_mode_bss(
874 				vif, le16_to_cpu(ev->u.ap_bss.ch));
875 		} else {
876 			ath6kl_dbg(ATH6KL_DBG_WMI,
877 				   "%s: aid %u mac_addr %pM auth=%u keymgmt=%u cipher=%u apsd_info=%u (STA connected)\n",
878 				   __func__, ev->u.ap_sta.aid,
879 				   ev->u.ap_sta.mac_addr,
880 				   ev->u.ap_sta.auth,
881 				   ev->u.ap_sta.keymgmt,
882 				   le16_to_cpu(ev->u.ap_sta.cipher),
883 				   ev->u.ap_sta.apsd_info);
884 
885 			ath6kl_connect_ap_mode_sta(
886 				vif, ev->u.ap_sta.aid, ev->u.ap_sta.mac_addr,
887 				ev->u.ap_sta.keymgmt,
888 				le16_to_cpu(ev->u.ap_sta.cipher),
889 				ev->u.ap_sta.auth, ev->assoc_req_len,
890 				ev->assoc_info + ev->beacon_ie_len,
891 				ev->u.ap_sta.apsd_info);
892 		}
893 		return 0;
894 	}
895 
896 	/* STA/IBSS mode connection event */
897 
898 	ath6kl_dbg(ATH6KL_DBG_WMI,
899 		   "wmi event connect freq %d bssid %pM listen_intvl %d beacon_intvl %d type %d\n",
900 		   le16_to_cpu(ev->u.sta.ch), ev->u.sta.bssid,
901 		   le16_to_cpu(ev->u.sta.listen_intvl),
902 		   le16_to_cpu(ev->u.sta.beacon_intvl),
903 		   le32_to_cpu(ev->u.sta.nw_type));
904 
905 	/* Start of assoc rsp IEs */
906 	pie = ev->assoc_info + ev->beacon_ie_len +
907 	      ev->assoc_req_len + (sizeof(u16) * 3); /* capinfo, status, aid */
908 
909 	/* End of assoc rsp IEs */
910 	peie = ev->assoc_info + ev->beacon_ie_len + ev->assoc_req_len +
911 	    ev->assoc_resp_len;
912 
913 	while (pie < peie) {
914 		switch (*pie) {
915 		case WLAN_EID_VENDOR_SPECIFIC:
916 			if (pie[1] > 3 && pie[2] == 0x00 && pie[3] == 0x50 &&
917 			    pie[4] == 0xf2 && pie[5] == WMM_OUI_TYPE) {
918 				/* WMM OUT (00:50:F2) */
919 				if (pie[1] > 5 &&
920 				    pie[6] == WMM_PARAM_OUI_SUBTYPE)
921 					wmi->is_wmm_enabled = true;
922 			}
923 			break;
924 		}
925 
926 		if (wmi->is_wmm_enabled)
927 			break;
928 
929 		pie += pie[1] + 2;
930 	}
931 
932 	ath6kl_connect_event(vif, le16_to_cpu(ev->u.sta.ch),
933 			     ev->u.sta.bssid,
934 			     le16_to_cpu(ev->u.sta.listen_intvl),
935 			     le16_to_cpu(ev->u.sta.beacon_intvl),
936 			     le32_to_cpu(ev->u.sta.nw_type),
937 			     ev->beacon_ie_len, ev->assoc_req_len,
938 			     ev->assoc_resp_len, ev->assoc_info);
939 
940 	return 0;
941 }
942 
943 static struct country_code_to_enum_rd *
944 ath6kl_regd_find_country(u16 countryCode)
945 {
946 	int i;
947 
948 	for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
949 		if (allCountries[i].countryCode == countryCode)
950 			return &allCountries[i];
951 	}
952 
953 	return NULL;
954 }
955 
956 static struct reg_dmn_pair_mapping *
957 ath6kl_get_regpair(u16 regdmn)
958 {
959 	int i;
960 
961 	if (regdmn == NO_ENUMRD)
962 		return NULL;
963 
964 	for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
965 		if (regDomainPairs[i].reg_domain == regdmn)
966 			return &regDomainPairs[i];
967 	}
968 
969 	return NULL;
970 }
971 
972 static struct country_code_to_enum_rd *
973 ath6kl_regd_find_country_by_rd(u16 regdmn)
974 {
975 	int i;
976 
977 	for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
978 		if (allCountries[i].regDmnEnum == regdmn)
979 			return &allCountries[i];
980 	}
981 
982 	return NULL;
983 }
984 
985 static void ath6kl_wmi_regdomain_event(struct wmi *wmi, u8 *datap, int len)
986 {
987 	struct ath6kl_wmi_regdomain *ev;
988 	struct country_code_to_enum_rd *country = NULL;
989 	struct reg_dmn_pair_mapping *regpair = NULL;
990 	char alpha2[2];
991 	u32 reg_code;
992 
993 	ev = (struct ath6kl_wmi_regdomain *) datap;
994 	reg_code = le32_to_cpu(ev->reg_code);
995 
996 	if ((reg_code >> ATH6KL_COUNTRY_RD_SHIFT) & COUNTRY_ERD_FLAG) {
997 		country = ath6kl_regd_find_country((u16) reg_code);
998 	} else if (!(((u16) reg_code & WORLD_SKU_MASK) == WORLD_SKU_PREFIX)) {
999 		regpair = ath6kl_get_regpair((u16) reg_code);
1000 		country = ath6kl_regd_find_country_by_rd((u16) reg_code);
1001 		if (regpair)
1002 			ath6kl_dbg(ATH6KL_DBG_WMI, "Regpair used: 0x%0x\n",
1003 				   regpair->reg_domain);
1004 		else
1005 			ath6kl_warn("Regpair not found reg_code 0x%0x\n",
1006 				    reg_code);
1007 	}
1008 
1009 	if (country && wmi->parent_dev->wiphy_registered) {
1010 		alpha2[0] = country->isoName[0];
1011 		alpha2[1] = country->isoName[1];
1012 
1013 		regulatory_hint(wmi->parent_dev->wiphy, alpha2);
1014 
1015 		ath6kl_dbg(ATH6KL_DBG_WMI, "Country alpha2 being used: %c%c\n",
1016 			   alpha2[0], alpha2[1]);
1017 	}
1018 }
1019 
1020 static int ath6kl_wmi_disconnect_event_rx(struct wmi *wmi, u8 *datap, int len,
1021 					  struct ath6kl_vif *vif)
1022 {
1023 	struct wmi_disconnect_event *ev;
1024 	wmi->traffic_class = 100;
1025 
1026 	if (len < sizeof(struct wmi_disconnect_event))
1027 		return -EINVAL;
1028 
1029 	ev = (struct wmi_disconnect_event *) datap;
1030 
1031 	ath6kl_dbg(ATH6KL_DBG_WMI,
1032 		   "wmi event disconnect proto_reason %d bssid %pM wmi_reason %d assoc_resp_len %d\n",
1033 		   le16_to_cpu(ev->proto_reason_status), ev->bssid,
1034 		   ev->disconn_reason, ev->assoc_resp_len);
1035 
1036 	wmi->is_wmm_enabled = false;
1037 
1038 	ath6kl_disconnect_event(vif, ev->disconn_reason,
1039 				ev->bssid, ev->assoc_resp_len, ev->assoc_info,
1040 				le16_to_cpu(ev->proto_reason_status));
1041 
1042 	return 0;
1043 }
1044 
1045 static int ath6kl_wmi_peer_node_event_rx(struct wmi *wmi, u8 *datap, int len)
1046 {
1047 	struct wmi_peer_node_event *ev;
1048 
1049 	if (len < sizeof(struct wmi_peer_node_event))
1050 		return -EINVAL;
1051 
1052 	ev = (struct wmi_peer_node_event *) datap;
1053 
1054 	if (ev->event_code == PEER_NODE_JOIN_EVENT)
1055 		ath6kl_dbg(ATH6KL_DBG_WMI, "joined node with mac addr: %pM\n",
1056 			   ev->peer_mac_addr);
1057 	else if (ev->event_code == PEER_NODE_LEAVE_EVENT)
1058 		ath6kl_dbg(ATH6KL_DBG_WMI, "left node with mac addr: %pM\n",
1059 			   ev->peer_mac_addr);
1060 
1061 	return 0;
1062 }
1063 
1064 static int ath6kl_wmi_tkip_micerr_event_rx(struct wmi *wmi, u8 *datap, int len,
1065 					   struct ath6kl_vif *vif)
1066 {
1067 	struct wmi_tkip_micerr_event *ev;
1068 
1069 	if (len < sizeof(struct wmi_tkip_micerr_event))
1070 		return -EINVAL;
1071 
1072 	ev = (struct wmi_tkip_micerr_event *) datap;
1073 
1074 	ath6kl_tkip_micerr_event(vif, ev->key_id, ev->is_mcast);
1075 
1076 	return 0;
1077 }
1078 
1079 void ath6kl_wmi_sscan_timer(struct timer_list *t)
1080 {
1081 	struct ath6kl_vif *vif = from_timer(vif, t, sched_scan_timer);
1082 
1083 	cfg80211_sched_scan_results(vif->ar->wiphy, 0);
1084 }
1085 
1086 static int ath6kl_wmi_bssinfo_event_rx(struct wmi *wmi, u8 *datap, int len,
1087 				       struct ath6kl_vif *vif)
1088 {
1089 	struct wmi_bss_info_hdr2 *bih;
1090 	u8 *buf;
1091 	struct ieee80211_channel *channel;
1092 	struct ath6kl *ar = wmi->parent_dev;
1093 	struct cfg80211_bss *bss;
1094 
1095 	if (len <= sizeof(struct wmi_bss_info_hdr2))
1096 		return -EINVAL;
1097 
1098 	bih = (struct wmi_bss_info_hdr2 *) datap;
1099 	buf = datap + sizeof(struct wmi_bss_info_hdr2);
1100 	len -= sizeof(struct wmi_bss_info_hdr2);
1101 
1102 	ath6kl_dbg(ATH6KL_DBG_WMI,
1103 		   "bss info evt - ch %u, snr %d, rssi %d, bssid \"%pM\" "
1104 		   "frame_type=%d\n",
1105 		   bih->ch, bih->snr, bih->snr - 95, bih->bssid,
1106 		   bih->frame_type);
1107 
1108 	if (bih->frame_type != BEACON_FTYPE &&
1109 	    bih->frame_type != PROBERESP_FTYPE)
1110 		return 0; /* Only update BSS table for now */
1111 
1112 	if (bih->frame_type == BEACON_FTYPE &&
1113 	    test_bit(CLEAR_BSSFILTER_ON_BEACON, &vif->flags)) {
1114 		clear_bit(CLEAR_BSSFILTER_ON_BEACON, &vif->flags);
1115 		ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx,
1116 					 NONE_BSS_FILTER, 0);
1117 	}
1118 
1119 	channel = ieee80211_get_channel(ar->wiphy, le16_to_cpu(bih->ch));
1120 	if (channel == NULL)
1121 		return -EINVAL;
1122 
1123 	if (len < 8 + 2 + 2)
1124 		return -EINVAL;
1125 
1126 	if (bih->frame_type == BEACON_FTYPE &&
1127 	    test_bit(CONNECTED, &vif->flags) &&
1128 	    memcmp(bih->bssid, vif->bssid, ETH_ALEN) == 0) {
1129 		const u8 *tim;
1130 		tim = cfg80211_find_ie(WLAN_EID_TIM, buf + 8 + 2 + 2,
1131 				       len - 8 - 2 - 2);
1132 		if (tim && tim[1] >= 2) {
1133 			vif->assoc_bss_dtim_period = tim[3];
1134 			set_bit(DTIM_PERIOD_AVAIL, &vif->flags);
1135 		}
1136 	}
1137 
1138 	bss = cfg80211_inform_bss(ar->wiphy, channel,
1139 				  bih->frame_type == BEACON_FTYPE ?
1140 					CFG80211_BSS_FTYPE_BEACON :
1141 					CFG80211_BSS_FTYPE_PRESP,
1142 				  bih->bssid, get_unaligned_le64((__le64 *)buf),
1143 				  get_unaligned_le16(((__le16 *)buf) + 5),
1144 				  get_unaligned_le16(((__le16 *)buf) + 4),
1145 				  buf + 8 + 2 + 2, len - 8 - 2 - 2,
1146 				  (bih->snr - 95) * 100, GFP_ATOMIC);
1147 	if (bss == NULL)
1148 		return -ENOMEM;
1149 	cfg80211_put_bss(ar->wiphy, bss);
1150 
1151 	/*
1152 	 * Firmware doesn't return any event when scheduled scan has
1153 	 * finished, so we need to use a timer to find out when there are
1154 	 * no more results.
1155 	 *
1156 	 * The timer is started from the first bss info received, otherwise
1157 	 * the timer would not ever fire if the scan interval is short
1158 	 * enough.
1159 	 */
1160 	if (test_bit(SCHED_SCANNING, &vif->flags) &&
1161 	    !timer_pending(&vif->sched_scan_timer)) {
1162 		mod_timer(&vif->sched_scan_timer, jiffies +
1163 			  msecs_to_jiffies(ATH6KL_SCHED_SCAN_RESULT_DELAY));
1164 	}
1165 
1166 	return 0;
1167 }
1168 
1169 /* Inactivity timeout of a fatpipe(pstream) at the target */
1170 static int ath6kl_wmi_pstream_timeout_event_rx(struct wmi *wmi, u8 *datap,
1171 					       int len)
1172 {
1173 	struct wmi_pstream_timeout_event *ev;
1174 
1175 	if (len < sizeof(struct wmi_pstream_timeout_event))
1176 		return -EINVAL;
1177 
1178 	ev = (struct wmi_pstream_timeout_event *) datap;
1179 
1180 	/*
1181 	 * When the pstream (fat pipe == AC) timesout, it means there were
1182 	 * no thinStreams within this pstream & it got implicitly created
1183 	 * due to data flow on this AC. We start the inactivity timer only
1184 	 * for implicitly created pstream. Just reset the host state.
1185 	 */
1186 	spin_lock_bh(&wmi->lock);
1187 	wmi->stream_exist_for_ac[ev->traffic_class] = 0;
1188 	wmi->fat_pipe_exist &= ~(1 << ev->traffic_class);
1189 	spin_unlock_bh(&wmi->lock);
1190 
1191 	/* Indicate inactivity to driver layer for this fatpipe (pstream) */
1192 	ath6kl_indicate_tx_activity(wmi->parent_dev, ev->traffic_class, false);
1193 
1194 	return 0;
1195 }
1196 
1197 static int ath6kl_wmi_bitrate_reply_rx(struct wmi *wmi, u8 *datap, int len)
1198 {
1199 	struct wmi_bit_rate_reply *reply;
1200 	s32 rate;
1201 	u32 sgi, index;
1202 
1203 	if (len < sizeof(struct wmi_bit_rate_reply))
1204 		return -EINVAL;
1205 
1206 	reply = (struct wmi_bit_rate_reply *) datap;
1207 
1208 	ath6kl_dbg(ATH6KL_DBG_WMI, "rateindex %d\n", reply->rate_index);
1209 
1210 	if (reply->rate_index == (s8) RATE_AUTO) {
1211 		rate = RATE_AUTO;
1212 	} else {
1213 		index = reply->rate_index & 0x7f;
1214 		if (WARN_ON_ONCE(index > (RATE_MCS_7_40 + 1)))
1215 			return -EINVAL;
1216 
1217 		sgi = (reply->rate_index & 0x80) ? 1 : 0;
1218 		rate = wmi_rate_tbl[index][sgi];
1219 	}
1220 
1221 	ath6kl_wakeup_event(wmi->parent_dev);
1222 
1223 	return 0;
1224 }
1225 
1226 static int ath6kl_wmi_test_rx(struct wmi *wmi, u8 *datap, int len)
1227 {
1228 	ath6kl_tm_rx_event(wmi->parent_dev, datap, len);
1229 
1230 	return 0;
1231 }
1232 
1233 static int ath6kl_wmi_ratemask_reply_rx(struct wmi *wmi, u8 *datap, int len)
1234 {
1235 	if (len < sizeof(struct wmi_fix_rates_reply))
1236 		return -EINVAL;
1237 
1238 	ath6kl_wakeup_event(wmi->parent_dev);
1239 
1240 	return 0;
1241 }
1242 
1243 static int ath6kl_wmi_ch_list_reply_rx(struct wmi *wmi, u8 *datap, int len)
1244 {
1245 	if (len < sizeof(struct wmi_channel_list_reply))
1246 		return -EINVAL;
1247 
1248 	ath6kl_wakeup_event(wmi->parent_dev);
1249 
1250 	return 0;
1251 }
1252 
1253 static int ath6kl_wmi_tx_pwr_reply_rx(struct wmi *wmi, u8 *datap, int len)
1254 {
1255 	struct wmi_tx_pwr_reply *reply;
1256 
1257 	if (len < sizeof(struct wmi_tx_pwr_reply))
1258 		return -EINVAL;
1259 
1260 	reply = (struct wmi_tx_pwr_reply *) datap;
1261 	ath6kl_txpwr_rx_evt(wmi->parent_dev, reply->dbM);
1262 
1263 	return 0;
1264 }
1265 
1266 static int ath6kl_wmi_keepalive_reply_rx(struct wmi *wmi, u8 *datap, int len)
1267 {
1268 	if (len < sizeof(struct wmi_get_keepalive_cmd))
1269 		return -EINVAL;
1270 
1271 	ath6kl_wakeup_event(wmi->parent_dev);
1272 
1273 	return 0;
1274 }
1275 
1276 static int ath6kl_wmi_scan_complete_rx(struct wmi *wmi, u8 *datap, int len,
1277 				       struct ath6kl_vif *vif)
1278 {
1279 	struct wmi_scan_complete_event *ev;
1280 
1281 	ev = (struct wmi_scan_complete_event *) datap;
1282 
1283 	ath6kl_scan_complete_evt(vif, a_sle32_to_cpu(ev->status));
1284 	wmi->is_probe_ssid = false;
1285 
1286 	return 0;
1287 }
1288 
1289 static int ath6kl_wmi_neighbor_report_event_rx(struct wmi *wmi, u8 *datap,
1290 					       int len, struct ath6kl_vif *vif)
1291 {
1292 	struct wmi_neighbor_report_event *ev;
1293 	u8 i;
1294 
1295 	if (len < sizeof(*ev))
1296 		return -EINVAL;
1297 	ev = (struct wmi_neighbor_report_event *) datap;
1298 	if (sizeof(*ev) + ev->num_neighbors * sizeof(struct wmi_neighbor_info)
1299 	    > len) {
1300 		ath6kl_dbg(ATH6KL_DBG_WMI,
1301 			   "truncated neighbor event (num=%d len=%d)\n",
1302 			   ev->num_neighbors, len);
1303 		return -EINVAL;
1304 	}
1305 	for (i = 0; i < ev->num_neighbors; i++) {
1306 		ath6kl_dbg(ATH6KL_DBG_WMI, "neighbor %d/%d - %pM 0x%x\n",
1307 			   i + 1, ev->num_neighbors, ev->neighbor[i].bssid,
1308 			   ev->neighbor[i].bss_flags);
1309 		cfg80211_pmksa_candidate_notify(vif->ndev, i,
1310 						ev->neighbor[i].bssid,
1311 						!!(ev->neighbor[i].bss_flags &
1312 						   WMI_PREAUTH_CAPABLE_BSS),
1313 						GFP_ATOMIC);
1314 	}
1315 
1316 	return 0;
1317 }
1318 
1319 /*
1320  * Target is reporting a programming error.  This is for
1321  * developer aid only.  Target only checks a few common violations
1322  * and it is responsibility of host to do all error checking.
1323  * Behavior of target after wmi error event is undefined.
1324  * A reset is recommended.
1325  */
1326 static int ath6kl_wmi_error_event_rx(struct wmi *wmi, u8 *datap, int len)
1327 {
1328 	const char *type = "unknown error";
1329 	struct wmi_cmd_error_event *ev;
1330 	ev = (struct wmi_cmd_error_event *) datap;
1331 
1332 	switch (ev->err_code) {
1333 	case INVALID_PARAM:
1334 		type = "invalid parameter";
1335 		break;
1336 	case ILLEGAL_STATE:
1337 		type = "invalid state";
1338 		break;
1339 	case INTERNAL_ERROR:
1340 		type = "internal error";
1341 		break;
1342 	}
1343 
1344 	ath6kl_dbg(ATH6KL_DBG_WMI, "programming error, cmd=%d %s\n",
1345 		   ev->cmd_id, type);
1346 
1347 	return 0;
1348 }
1349 
1350 static int ath6kl_wmi_stats_event_rx(struct wmi *wmi, u8 *datap, int len,
1351 				     struct ath6kl_vif *vif)
1352 {
1353 	ath6kl_tgt_stats_event(vif, datap, len);
1354 
1355 	return 0;
1356 }
1357 
1358 static u8 ath6kl_wmi_get_upper_threshold(s16 rssi,
1359 					 struct sq_threshold_params *sq_thresh,
1360 					 u32 size)
1361 {
1362 	u32 index;
1363 	u8 threshold = (u8) sq_thresh->upper_threshold[size - 1];
1364 
1365 	/* The list is already in sorted order. Get the next lower value */
1366 	for (index = 0; index < size; index++) {
1367 		if (rssi < sq_thresh->upper_threshold[index]) {
1368 			threshold = (u8) sq_thresh->upper_threshold[index];
1369 			break;
1370 		}
1371 	}
1372 
1373 	return threshold;
1374 }
1375 
1376 static u8 ath6kl_wmi_get_lower_threshold(s16 rssi,
1377 					 struct sq_threshold_params *sq_thresh,
1378 					 u32 size)
1379 {
1380 	u32 index;
1381 	u8 threshold = (u8) sq_thresh->lower_threshold[size - 1];
1382 
1383 	/* The list is already in sorted order. Get the next lower value */
1384 	for (index = 0; index < size; index++) {
1385 		if (rssi > sq_thresh->lower_threshold[index]) {
1386 			threshold = (u8) sq_thresh->lower_threshold[index];
1387 			break;
1388 		}
1389 	}
1390 
1391 	return threshold;
1392 }
1393 
1394 static int ath6kl_wmi_send_rssi_threshold_params(struct wmi *wmi,
1395 			struct wmi_rssi_threshold_params_cmd *rssi_cmd)
1396 {
1397 	struct sk_buff *skb;
1398 	struct wmi_rssi_threshold_params_cmd *cmd;
1399 
1400 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1401 	if (!skb)
1402 		return -ENOMEM;
1403 
1404 	cmd = (struct wmi_rssi_threshold_params_cmd *) skb->data;
1405 	memcpy(cmd, rssi_cmd, sizeof(struct wmi_rssi_threshold_params_cmd));
1406 
1407 	return ath6kl_wmi_cmd_send(wmi, 0, skb, WMI_RSSI_THRESHOLD_PARAMS_CMDID,
1408 				   NO_SYNC_WMIFLAG);
1409 }
1410 
1411 static int ath6kl_wmi_rssi_threshold_event_rx(struct wmi *wmi, u8 *datap,
1412 					      int len)
1413 {
1414 	struct wmi_rssi_threshold_event *reply;
1415 	struct wmi_rssi_threshold_params_cmd cmd;
1416 	struct sq_threshold_params *sq_thresh;
1417 	enum wmi_rssi_threshold_val new_threshold;
1418 	u8 upper_rssi_threshold, lower_rssi_threshold;
1419 	s16 rssi;
1420 	int ret;
1421 
1422 	if (len < sizeof(struct wmi_rssi_threshold_event))
1423 		return -EINVAL;
1424 
1425 	reply = (struct wmi_rssi_threshold_event *) datap;
1426 	new_threshold = (enum wmi_rssi_threshold_val) reply->range;
1427 	rssi = a_sle16_to_cpu(reply->rssi);
1428 
1429 	sq_thresh = &wmi->sq_threshld[SIGNAL_QUALITY_METRICS_RSSI];
1430 
1431 	/*
1432 	 * Identify the threshold breached and communicate that to the app.
1433 	 * After that install a new set of thresholds based on the signal
1434 	 * quality reported by the target
1435 	 */
1436 	if (new_threshold) {
1437 		/* Upper threshold breached */
1438 		if (rssi < sq_thresh->upper_threshold[0]) {
1439 			ath6kl_dbg(ATH6KL_DBG_WMI,
1440 				   "spurious upper rssi threshold event: %d\n",
1441 				   rssi);
1442 		} else if ((rssi < sq_thresh->upper_threshold[1]) &&
1443 			   (rssi >= sq_thresh->upper_threshold[0])) {
1444 			new_threshold = WMI_RSSI_THRESHOLD1_ABOVE;
1445 		} else if ((rssi < sq_thresh->upper_threshold[2]) &&
1446 			   (rssi >= sq_thresh->upper_threshold[1])) {
1447 			new_threshold = WMI_RSSI_THRESHOLD2_ABOVE;
1448 		} else if ((rssi < sq_thresh->upper_threshold[3]) &&
1449 			   (rssi >= sq_thresh->upper_threshold[2])) {
1450 			new_threshold = WMI_RSSI_THRESHOLD3_ABOVE;
1451 		} else if ((rssi < sq_thresh->upper_threshold[4]) &&
1452 			   (rssi >= sq_thresh->upper_threshold[3])) {
1453 			new_threshold = WMI_RSSI_THRESHOLD4_ABOVE;
1454 		} else if ((rssi < sq_thresh->upper_threshold[5]) &&
1455 			   (rssi >= sq_thresh->upper_threshold[4])) {
1456 			new_threshold = WMI_RSSI_THRESHOLD5_ABOVE;
1457 		} else if (rssi >= sq_thresh->upper_threshold[5]) {
1458 			new_threshold = WMI_RSSI_THRESHOLD6_ABOVE;
1459 		}
1460 	} else {
1461 		/* Lower threshold breached */
1462 		if (rssi > sq_thresh->lower_threshold[0]) {
1463 			ath6kl_dbg(ATH6KL_DBG_WMI,
1464 				   "spurious lower rssi threshold event: %d %d\n",
1465 				rssi, sq_thresh->lower_threshold[0]);
1466 		} else if ((rssi > sq_thresh->lower_threshold[1]) &&
1467 			   (rssi <= sq_thresh->lower_threshold[0])) {
1468 			new_threshold = WMI_RSSI_THRESHOLD6_BELOW;
1469 		} else if ((rssi > sq_thresh->lower_threshold[2]) &&
1470 			   (rssi <= sq_thresh->lower_threshold[1])) {
1471 			new_threshold = WMI_RSSI_THRESHOLD5_BELOW;
1472 		} else if ((rssi > sq_thresh->lower_threshold[3]) &&
1473 			   (rssi <= sq_thresh->lower_threshold[2])) {
1474 			new_threshold = WMI_RSSI_THRESHOLD4_BELOW;
1475 		} else if ((rssi > sq_thresh->lower_threshold[4]) &&
1476 			   (rssi <= sq_thresh->lower_threshold[3])) {
1477 			new_threshold = WMI_RSSI_THRESHOLD3_BELOW;
1478 		} else if ((rssi > sq_thresh->lower_threshold[5]) &&
1479 			   (rssi <= sq_thresh->lower_threshold[4])) {
1480 			new_threshold = WMI_RSSI_THRESHOLD2_BELOW;
1481 		} else if (rssi <= sq_thresh->lower_threshold[5]) {
1482 			new_threshold = WMI_RSSI_THRESHOLD1_BELOW;
1483 		}
1484 	}
1485 
1486 	/* Calculate and install the next set of thresholds */
1487 	lower_rssi_threshold = ath6kl_wmi_get_lower_threshold(rssi, sq_thresh,
1488 				       sq_thresh->lower_threshold_valid_count);
1489 	upper_rssi_threshold = ath6kl_wmi_get_upper_threshold(rssi, sq_thresh,
1490 				       sq_thresh->upper_threshold_valid_count);
1491 
1492 	/* Issue a wmi command to install the thresholds */
1493 	cmd.thresh_above1_val = a_cpu_to_sle16(upper_rssi_threshold);
1494 	cmd.thresh_below1_val = a_cpu_to_sle16(lower_rssi_threshold);
1495 	cmd.weight = sq_thresh->weight;
1496 	cmd.poll_time = cpu_to_le32(sq_thresh->polling_interval);
1497 
1498 	ret = ath6kl_wmi_send_rssi_threshold_params(wmi, &cmd);
1499 	if (ret) {
1500 		ath6kl_err("unable to configure rssi thresholds\n");
1501 		return -EIO;
1502 	}
1503 
1504 	return 0;
1505 }
1506 
1507 static int ath6kl_wmi_cac_event_rx(struct wmi *wmi, u8 *datap, int len,
1508 				   struct ath6kl_vif *vif)
1509 {
1510 	struct wmi_cac_event *reply;
1511 	struct ieee80211_tspec_ie *ts;
1512 	u16 active_tsids, tsinfo;
1513 	u8 tsid, index;
1514 	u8 ts_id;
1515 
1516 	if (len < sizeof(struct wmi_cac_event))
1517 		return -EINVAL;
1518 
1519 	reply = (struct wmi_cac_event *) datap;
1520 
1521 	if ((reply->cac_indication == CAC_INDICATION_ADMISSION_RESP) &&
1522 	    (reply->status_code != IEEE80211_TSPEC_STATUS_ADMISS_ACCEPTED)) {
1523 		ts = (struct ieee80211_tspec_ie *) &(reply->tspec_suggestion);
1524 		tsinfo = le16_to_cpu(ts->tsinfo);
1525 		tsid = (tsinfo >> IEEE80211_WMM_IE_TSPEC_TID_SHIFT) &
1526 			IEEE80211_WMM_IE_TSPEC_TID_MASK;
1527 
1528 		ath6kl_wmi_delete_pstream_cmd(wmi, vif->fw_vif_idx,
1529 					      reply->ac, tsid);
1530 	} else if (reply->cac_indication == CAC_INDICATION_NO_RESP) {
1531 		/*
1532 		 * Following assumes that there is only one outstanding
1533 		 * ADDTS request when this event is received
1534 		 */
1535 		spin_lock_bh(&wmi->lock);
1536 		active_tsids = wmi->stream_exist_for_ac[reply->ac];
1537 		spin_unlock_bh(&wmi->lock);
1538 
1539 		for (index = 0; index < sizeof(active_tsids) * 8; index++) {
1540 			if ((active_tsids >> index) & 1)
1541 				break;
1542 		}
1543 		if (index < (sizeof(active_tsids) * 8))
1544 			ath6kl_wmi_delete_pstream_cmd(wmi, vif->fw_vif_idx,
1545 						      reply->ac, index);
1546 	}
1547 
1548 	/*
1549 	 * Clear active tsids and Add missing handling
1550 	 * for delete qos stream from AP
1551 	 */
1552 	else if (reply->cac_indication == CAC_INDICATION_DELETE) {
1553 		ts = (struct ieee80211_tspec_ie *) &(reply->tspec_suggestion);
1554 		tsinfo = le16_to_cpu(ts->tsinfo);
1555 		ts_id = ((tsinfo >> IEEE80211_WMM_IE_TSPEC_TID_SHIFT) &
1556 			 IEEE80211_WMM_IE_TSPEC_TID_MASK);
1557 
1558 		spin_lock_bh(&wmi->lock);
1559 		wmi->stream_exist_for_ac[reply->ac] &= ~(1 << ts_id);
1560 		active_tsids = wmi->stream_exist_for_ac[reply->ac];
1561 		spin_unlock_bh(&wmi->lock);
1562 
1563 		/* Indicate stream inactivity to driver layer only if all tsids
1564 		 * within this AC are deleted.
1565 		 */
1566 		if (!active_tsids) {
1567 			ath6kl_indicate_tx_activity(wmi->parent_dev, reply->ac,
1568 						    false);
1569 			wmi->fat_pipe_exist &= ~(1 << reply->ac);
1570 		}
1571 	}
1572 
1573 	return 0;
1574 }
1575 
1576 static int ath6kl_wmi_txe_notify_event_rx(struct wmi *wmi, u8 *datap, int len,
1577 					  struct ath6kl_vif *vif)
1578 {
1579 	struct wmi_txe_notify_event *ev;
1580 	u32 rate, pkts;
1581 
1582 	if (len < sizeof(*ev))
1583 		return -EINVAL;
1584 
1585 	if (vif->nw_type != INFRA_NETWORK ||
1586 	    !test_bit(ATH6KL_FW_CAPABILITY_TX_ERR_NOTIFY,
1587 		      vif->ar->fw_capabilities))
1588 		return -EOPNOTSUPP;
1589 
1590 	if (vif->sme_state != SME_CONNECTED)
1591 		return -ENOTCONN;
1592 
1593 	ev = (struct wmi_txe_notify_event *) datap;
1594 	rate = le32_to_cpu(ev->rate);
1595 	pkts = le32_to_cpu(ev->pkts);
1596 
1597 	ath6kl_dbg(ATH6KL_DBG_WMI, "TXE notify event: peer %pM rate %d%% pkts %d intvl %ds\n",
1598 		   vif->bssid, rate, pkts, vif->txe_intvl);
1599 
1600 	cfg80211_cqm_txe_notify(vif->ndev, vif->bssid, pkts,
1601 				rate, vif->txe_intvl, GFP_KERNEL);
1602 
1603 	return 0;
1604 }
1605 
1606 int ath6kl_wmi_set_txe_notify(struct wmi *wmi, u8 idx,
1607 			      u32 rate, u32 pkts, u32 intvl)
1608 {
1609 	struct sk_buff *skb;
1610 	struct wmi_txe_notify_cmd *cmd;
1611 
1612 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1613 	if (!skb)
1614 		return -ENOMEM;
1615 
1616 	cmd = (struct wmi_txe_notify_cmd *) skb->data;
1617 	cmd->rate = cpu_to_le32(rate);
1618 	cmd->pkts = cpu_to_le32(pkts);
1619 	cmd->intvl = cpu_to_le32(intvl);
1620 
1621 	return ath6kl_wmi_cmd_send(wmi, idx, skb, WMI_SET_TXE_NOTIFY_CMDID,
1622 				   NO_SYNC_WMIFLAG);
1623 }
1624 
1625 int ath6kl_wmi_set_rssi_filter_cmd(struct wmi *wmi, u8 if_idx, s8 rssi)
1626 {
1627 	struct sk_buff *skb;
1628 	struct wmi_set_rssi_filter_cmd *cmd;
1629 	int ret;
1630 
1631 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1632 	if (!skb)
1633 		return -ENOMEM;
1634 
1635 	cmd = (struct wmi_set_rssi_filter_cmd *) skb->data;
1636 	cmd->rssi = rssi;
1637 
1638 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_RSSI_FILTER_CMDID,
1639 				  NO_SYNC_WMIFLAG);
1640 	return ret;
1641 }
1642 
1643 static int ath6kl_wmi_send_snr_threshold_params(struct wmi *wmi,
1644 			struct wmi_snr_threshold_params_cmd *snr_cmd)
1645 {
1646 	struct sk_buff *skb;
1647 	struct wmi_snr_threshold_params_cmd *cmd;
1648 
1649 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1650 	if (!skb)
1651 		return -ENOMEM;
1652 
1653 	cmd = (struct wmi_snr_threshold_params_cmd *) skb->data;
1654 	memcpy(cmd, snr_cmd, sizeof(struct wmi_snr_threshold_params_cmd));
1655 
1656 	return ath6kl_wmi_cmd_send(wmi, 0, skb, WMI_SNR_THRESHOLD_PARAMS_CMDID,
1657 				   NO_SYNC_WMIFLAG);
1658 }
1659 
1660 static int ath6kl_wmi_snr_threshold_event_rx(struct wmi *wmi, u8 *datap,
1661 					     int len)
1662 {
1663 	struct wmi_snr_threshold_event *reply;
1664 	struct sq_threshold_params *sq_thresh;
1665 	struct wmi_snr_threshold_params_cmd cmd;
1666 	enum wmi_snr_threshold_val new_threshold;
1667 	u8 upper_snr_threshold, lower_snr_threshold;
1668 	s16 snr;
1669 	int ret;
1670 
1671 	if (len < sizeof(struct wmi_snr_threshold_event))
1672 		return -EINVAL;
1673 
1674 	reply = (struct wmi_snr_threshold_event *) datap;
1675 
1676 	new_threshold = (enum wmi_snr_threshold_val) reply->range;
1677 	snr = reply->snr;
1678 
1679 	sq_thresh = &wmi->sq_threshld[SIGNAL_QUALITY_METRICS_SNR];
1680 
1681 	/*
1682 	 * Identify the threshold breached and communicate that to the app.
1683 	 * After that install a new set of thresholds based on the signal
1684 	 * quality reported by the target.
1685 	 */
1686 	if (new_threshold) {
1687 		/* Upper threshold breached */
1688 		if (snr < sq_thresh->upper_threshold[0]) {
1689 			ath6kl_dbg(ATH6KL_DBG_WMI,
1690 				   "spurious upper snr threshold event: %d\n",
1691 				   snr);
1692 		} else if ((snr < sq_thresh->upper_threshold[1]) &&
1693 			   (snr >= sq_thresh->upper_threshold[0])) {
1694 			new_threshold = WMI_SNR_THRESHOLD1_ABOVE;
1695 		} else if ((snr < sq_thresh->upper_threshold[2]) &&
1696 			   (snr >= sq_thresh->upper_threshold[1])) {
1697 			new_threshold = WMI_SNR_THRESHOLD2_ABOVE;
1698 		} else if ((snr < sq_thresh->upper_threshold[3]) &&
1699 			   (snr >= sq_thresh->upper_threshold[2])) {
1700 			new_threshold = WMI_SNR_THRESHOLD3_ABOVE;
1701 		} else if (snr >= sq_thresh->upper_threshold[3]) {
1702 			new_threshold = WMI_SNR_THRESHOLD4_ABOVE;
1703 		}
1704 	} else {
1705 		/* Lower threshold breached */
1706 		if (snr > sq_thresh->lower_threshold[0]) {
1707 			ath6kl_dbg(ATH6KL_DBG_WMI,
1708 				   "spurious lower snr threshold event: %d\n",
1709 				   sq_thresh->lower_threshold[0]);
1710 		} else if ((snr > sq_thresh->lower_threshold[1]) &&
1711 			   (snr <= sq_thresh->lower_threshold[0])) {
1712 			new_threshold = WMI_SNR_THRESHOLD4_BELOW;
1713 		} else if ((snr > sq_thresh->lower_threshold[2]) &&
1714 			   (snr <= sq_thresh->lower_threshold[1])) {
1715 			new_threshold = WMI_SNR_THRESHOLD3_BELOW;
1716 		} else if ((snr > sq_thresh->lower_threshold[3]) &&
1717 			   (snr <= sq_thresh->lower_threshold[2])) {
1718 			new_threshold = WMI_SNR_THRESHOLD2_BELOW;
1719 		} else if (snr <= sq_thresh->lower_threshold[3]) {
1720 			new_threshold = WMI_SNR_THRESHOLD1_BELOW;
1721 		}
1722 	}
1723 
1724 	/* Calculate and install the next set of thresholds */
1725 	lower_snr_threshold = ath6kl_wmi_get_lower_threshold(snr, sq_thresh,
1726 				       sq_thresh->lower_threshold_valid_count);
1727 	upper_snr_threshold = ath6kl_wmi_get_upper_threshold(snr, sq_thresh,
1728 				       sq_thresh->upper_threshold_valid_count);
1729 
1730 	/* Issue a wmi command to install the thresholds */
1731 	cmd.thresh_above1_val = upper_snr_threshold;
1732 	cmd.thresh_below1_val = lower_snr_threshold;
1733 	cmd.weight = sq_thresh->weight;
1734 	cmd.poll_time = cpu_to_le32(sq_thresh->polling_interval);
1735 
1736 	ath6kl_dbg(ATH6KL_DBG_WMI,
1737 		   "snr: %d, threshold: %d, lower: %d, upper: %d\n",
1738 		   snr, new_threshold,
1739 		   lower_snr_threshold, upper_snr_threshold);
1740 
1741 	ret = ath6kl_wmi_send_snr_threshold_params(wmi, &cmd);
1742 	if (ret) {
1743 		ath6kl_err("unable to configure snr threshold\n");
1744 		return -EIO;
1745 	}
1746 
1747 	return 0;
1748 }
1749 
1750 static int ath6kl_wmi_aplist_event_rx(struct wmi *wmi, u8 *datap, int len)
1751 {
1752 	u16 ap_info_entry_size;
1753 	struct wmi_aplist_event *ev = (struct wmi_aplist_event *) datap;
1754 	struct wmi_ap_info_v1 *ap_info_v1;
1755 	u8 index;
1756 
1757 	if (len < sizeof(struct wmi_aplist_event) ||
1758 	    ev->ap_list_ver != APLIST_VER1)
1759 		return -EINVAL;
1760 
1761 	ap_info_entry_size = sizeof(struct wmi_ap_info_v1);
1762 	ap_info_v1 = (struct wmi_ap_info_v1 *) ev->ap_list;
1763 
1764 	ath6kl_dbg(ATH6KL_DBG_WMI,
1765 		   "number of APs in aplist event: %d\n", ev->num_ap);
1766 
1767 	if (len < (int) (sizeof(struct wmi_aplist_event) +
1768 			 (ev->num_ap - 1) * ap_info_entry_size))
1769 		return -EINVAL;
1770 
1771 	/* AP list version 1 contents */
1772 	for (index = 0; index < ev->num_ap; index++) {
1773 		ath6kl_dbg(ATH6KL_DBG_WMI, "AP#%d BSSID %pM Channel %d\n",
1774 			   index, ap_info_v1->bssid, ap_info_v1->channel);
1775 		ap_info_v1++;
1776 	}
1777 
1778 	return 0;
1779 }
1780 
1781 int ath6kl_wmi_cmd_send(struct wmi *wmi, u8 if_idx, struct sk_buff *skb,
1782 			enum wmi_cmd_id cmd_id, enum wmi_sync_flag sync_flag)
1783 {
1784 	struct wmi_cmd_hdr *cmd_hdr;
1785 	enum htc_endpoint_id ep_id = wmi->ep_id;
1786 	int ret;
1787 	u16 info1;
1788 
1789 	if (WARN_ON(skb == NULL ||
1790 		    (if_idx > (wmi->parent_dev->vif_max - 1)))) {
1791 		dev_kfree_skb(skb);
1792 		return -EINVAL;
1793 	}
1794 
1795 	ath6kl_dbg(ATH6KL_DBG_WMI, "wmi tx id %d len %d flag %d\n",
1796 		   cmd_id, skb->len, sync_flag);
1797 	ath6kl_dbg_dump(ATH6KL_DBG_WMI_DUMP, NULL, "wmi tx ",
1798 			skb->data, skb->len);
1799 
1800 	if (sync_flag >= END_WMIFLAG) {
1801 		dev_kfree_skb(skb);
1802 		return -EINVAL;
1803 	}
1804 
1805 	if ((sync_flag == SYNC_BEFORE_WMIFLAG) ||
1806 	    (sync_flag == SYNC_BOTH_WMIFLAG)) {
1807 		/*
1808 		 * Make sure all data currently queued is transmitted before
1809 		 * the cmd execution.  Establish a new sync point.
1810 		 */
1811 		ath6kl_wmi_sync_point(wmi, if_idx);
1812 	}
1813 
1814 	skb_push(skb, sizeof(struct wmi_cmd_hdr));
1815 
1816 	cmd_hdr = (struct wmi_cmd_hdr *) skb->data;
1817 	cmd_hdr->cmd_id = cpu_to_le16(cmd_id);
1818 	info1 = if_idx & WMI_CMD_HDR_IF_ID_MASK;
1819 	cmd_hdr->info1 = cpu_to_le16(info1);
1820 
1821 	/* Only for OPT_TX_CMD, use BE endpoint. */
1822 	if (cmd_id == WMI_OPT_TX_FRAME_CMDID) {
1823 		ret = ath6kl_wmi_data_hdr_add(wmi, skb, OPT_MSGTYPE,
1824 					      false, false, 0, NULL, if_idx);
1825 		if (ret) {
1826 			dev_kfree_skb(skb);
1827 			return ret;
1828 		}
1829 		ep_id = ath6kl_ac2_endpoint_id(wmi->parent_dev, WMM_AC_BE);
1830 	}
1831 
1832 	ath6kl_control_tx(wmi->parent_dev, skb, ep_id);
1833 
1834 	if ((sync_flag == SYNC_AFTER_WMIFLAG) ||
1835 	    (sync_flag == SYNC_BOTH_WMIFLAG)) {
1836 		/*
1837 		 * Make sure all new data queued waits for the command to
1838 		 * execute. Establish a new sync point.
1839 		 */
1840 		ath6kl_wmi_sync_point(wmi, if_idx);
1841 	}
1842 
1843 	return 0;
1844 }
1845 
1846 int ath6kl_wmi_connect_cmd(struct wmi *wmi, u8 if_idx,
1847 			   enum network_type nw_type,
1848 			   enum dot11_auth_mode dot11_auth_mode,
1849 			   enum auth_mode auth_mode,
1850 			   enum ath6kl_crypto_type pairwise_crypto,
1851 			   u8 pairwise_crypto_len,
1852 			   enum ath6kl_crypto_type group_crypto,
1853 			   u8 group_crypto_len, int ssid_len, u8 *ssid,
1854 			   u8 *bssid, u16 channel, u32 ctrl_flags,
1855 			   u8 nw_subtype)
1856 {
1857 	struct sk_buff *skb;
1858 	struct wmi_connect_cmd *cc;
1859 	int ret;
1860 
1861 	ath6kl_dbg(ATH6KL_DBG_WMI,
1862 		   "wmi connect bssid %pM freq %d flags 0x%x ssid_len %d "
1863 		   "type %d dot11_auth %d auth %d pairwise %d group %d\n",
1864 		   bssid, channel, ctrl_flags, ssid_len, nw_type,
1865 		   dot11_auth_mode, auth_mode, pairwise_crypto, group_crypto);
1866 	ath6kl_dbg_dump(ATH6KL_DBG_WMI, NULL, "ssid ", ssid, ssid_len);
1867 
1868 	wmi->traffic_class = 100;
1869 
1870 	if ((pairwise_crypto == NONE_CRYPT) && (group_crypto != NONE_CRYPT))
1871 		return -EINVAL;
1872 
1873 	if ((pairwise_crypto != NONE_CRYPT) && (group_crypto == NONE_CRYPT))
1874 		return -EINVAL;
1875 
1876 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_connect_cmd));
1877 	if (!skb)
1878 		return -ENOMEM;
1879 
1880 	cc = (struct wmi_connect_cmd *) skb->data;
1881 
1882 	if (ssid_len)
1883 		memcpy(cc->ssid, ssid, ssid_len);
1884 
1885 	cc->ssid_len = ssid_len;
1886 	cc->nw_type = nw_type;
1887 	cc->dot11_auth_mode = dot11_auth_mode;
1888 	cc->auth_mode = auth_mode;
1889 	cc->prwise_crypto_type = pairwise_crypto;
1890 	cc->prwise_crypto_len = pairwise_crypto_len;
1891 	cc->grp_crypto_type = group_crypto;
1892 	cc->grp_crypto_len = group_crypto_len;
1893 	cc->ch = cpu_to_le16(channel);
1894 	cc->ctrl_flags = cpu_to_le32(ctrl_flags);
1895 	cc->nw_subtype = nw_subtype;
1896 
1897 	if (bssid != NULL)
1898 		memcpy(cc->bssid, bssid, ETH_ALEN);
1899 
1900 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_CONNECT_CMDID,
1901 				  NO_SYNC_WMIFLAG);
1902 
1903 	return ret;
1904 }
1905 
1906 int ath6kl_wmi_reconnect_cmd(struct wmi *wmi, u8 if_idx, u8 *bssid,
1907 			     u16 channel)
1908 {
1909 	struct sk_buff *skb;
1910 	struct wmi_reconnect_cmd *cc;
1911 	int ret;
1912 
1913 	ath6kl_dbg(ATH6KL_DBG_WMI, "wmi reconnect bssid %pM freq %d\n",
1914 		   bssid, channel);
1915 
1916 	wmi->traffic_class = 100;
1917 
1918 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_reconnect_cmd));
1919 	if (!skb)
1920 		return -ENOMEM;
1921 
1922 	cc = (struct wmi_reconnect_cmd *) skb->data;
1923 	cc->channel = cpu_to_le16(channel);
1924 
1925 	if (bssid != NULL)
1926 		memcpy(cc->bssid, bssid, ETH_ALEN);
1927 
1928 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_RECONNECT_CMDID,
1929 				  NO_SYNC_WMIFLAG);
1930 
1931 	return ret;
1932 }
1933 
1934 int ath6kl_wmi_disconnect_cmd(struct wmi *wmi, u8 if_idx)
1935 {
1936 	int ret;
1937 
1938 	ath6kl_dbg(ATH6KL_DBG_WMI, "wmi disconnect\n");
1939 
1940 	wmi->traffic_class = 100;
1941 
1942 	/* Disconnect command does not need to do a SYNC before. */
1943 	ret = ath6kl_wmi_simple_cmd(wmi, if_idx, WMI_DISCONNECT_CMDID);
1944 
1945 	return ret;
1946 }
1947 
1948 /* ath6kl_wmi_start_scan_cmd is to be deprecated. Use
1949  * ath6kl_wmi_begin_scan_cmd instead. The new function supports P2P
1950  * mgmt operations using station interface.
1951  */
1952 static int ath6kl_wmi_startscan_cmd(struct wmi *wmi, u8 if_idx,
1953 				    enum wmi_scan_type scan_type,
1954 				    u32 force_fgscan, u32 is_legacy,
1955 				    u32 home_dwell_time,
1956 				    u32 force_scan_interval,
1957 				    s8 num_chan, u16 *ch_list)
1958 {
1959 	struct sk_buff *skb;
1960 	struct wmi_start_scan_cmd *sc;
1961 	s8 size;
1962 	int i, ret;
1963 
1964 	size = sizeof(struct wmi_start_scan_cmd);
1965 
1966 	if ((scan_type != WMI_LONG_SCAN) && (scan_type != WMI_SHORT_SCAN))
1967 		return -EINVAL;
1968 
1969 	if (num_chan > WMI_MAX_CHANNELS)
1970 		return -EINVAL;
1971 
1972 	if (num_chan)
1973 		size += sizeof(u16) * (num_chan - 1);
1974 
1975 	skb = ath6kl_wmi_get_new_buf(size);
1976 	if (!skb)
1977 		return -ENOMEM;
1978 
1979 	sc = (struct wmi_start_scan_cmd *) skb->data;
1980 	sc->scan_type = scan_type;
1981 	sc->force_fg_scan = cpu_to_le32(force_fgscan);
1982 	sc->is_legacy = cpu_to_le32(is_legacy);
1983 	sc->home_dwell_time = cpu_to_le32(home_dwell_time);
1984 	sc->force_scan_intvl = cpu_to_le32(force_scan_interval);
1985 	sc->num_ch = num_chan;
1986 
1987 	for (i = 0; i < num_chan; i++)
1988 		sc->ch_list[i] = cpu_to_le16(ch_list[i]);
1989 
1990 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_START_SCAN_CMDID,
1991 				  NO_SYNC_WMIFLAG);
1992 
1993 	return ret;
1994 }
1995 
1996 /*
1997  * beginscan supports (compared to old startscan) P2P mgmt operations using
1998  * station interface, send additional information like supported rates to
1999  * advertise and xmit rates for probe requests
2000  */
2001 int ath6kl_wmi_beginscan_cmd(struct wmi *wmi, u8 if_idx,
2002 			     enum wmi_scan_type scan_type,
2003 			     u32 force_fgscan, u32 is_legacy,
2004 			     u32 home_dwell_time, u32 force_scan_interval,
2005 			     s8 num_chan, u16 *ch_list, u32 no_cck, u32 *rates)
2006 {
2007 	struct ieee80211_supported_band *sband;
2008 	struct sk_buff *skb;
2009 	struct wmi_begin_scan_cmd *sc;
2010 	s8 size, *supp_rates;
2011 	int i, band, ret;
2012 	struct ath6kl *ar = wmi->parent_dev;
2013 	int num_rates;
2014 	u32 ratemask;
2015 
2016 	if (!test_bit(ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX,
2017 		      ar->fw_capabilities)) {
2018 		return ath6kl_wmi_startscan_cmd(wmi, if_idx,
2019 						scan_type, force_fgscan,
2020 						is_legacy, home_dwell_time,
2021 						force_scan_interval,
2022 						num_chan, ch_list);
2023 	}
2024 
2025 	size = sizeof(struct wmi_begin_scan_cmd);
2026 
2027 	if ((scan_type != WMI_LONG_SCAN) && (scan_type != WMI_SHORT_SCAN))
2028 		return -EINVAL;
2029 
2030 	if (num_chan > WMI_MAX_CHANNELS)
2031 		return -EINVAL;
2032 
2033 	if (num_chan)
2034 		size += sizeof(u16) * (num_chan - 1);
2035 
2036 	skb = ath6kl_wmi_get_new_buf(size);
2037 	if (!skb)
2038 		return -ENOMEM;
2039 
2040 	sc = (struct wmi_begin_scan_cmd *) skb->data;
2041 	sc->scan_type = scan_type;
2042 	sc->force_fg_scan = cpu_to_le32(force_fgscan);
2043 	sc->is_legacy = cpu_to_le32(is_legacy);
2044 	sc->home_dwell_time = cpu_to_le32(home_dwell_time);
2045 	sc->force_scan_intvl = cpu_to_le32(force_scan_interval);
2046 	sc->no_cck = cpu_to_le32(no_cck);
2047 	sc->num_ch = num_chan;
2048 
2049 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
2050 		sband = ar->wiphy->bands[band];
2051 
2052 		if (!sband)
2053 			continue;
2054 
2055 		if (WARN_ON(band >= ATH6KL_NUM_BANDS))
2056 			break;
2057 
2058 		ratemask = rates[band];
2059 		supp_rates = sc->supp_rates[band].rates;
2060 		num_rates = 0;
2061 
2062 		for (i = 0; i < sband->n_bitrates; i++) {
2063 			if ((BIT(i) & ratemask) == 0)
2064 				continue; /* skip rate */
2065 			supp_rates[num_rates++] =
2066 			    (u8) (sband->bitrates[i].bitrate / 5);
2067 		}
2068 		sc->supp_rates[band].nrates = num_rates;
2069 	}
2070 
2071 	for (i = 0; i < num_chan; i++)
2072 		sc->ch_list[i] = cpu_to_le16(ch_list[i]);
2073 
2074 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_BEGIN_SCAN_CMDID,
2075 				  NO_SYNC_WMIFLAG);
2076 
2077 	return ret;
2078 }
2079 
2080 int ath6kl_wmi_enable_sched_scan_cmd(struct wmi *wmi, u8 if_idx, bool enable)
2081 {
2082 	struct sk_buff *skb;
2083 	struct wmi_enable_sched_scan_cmd *sc;
2084 	int ret;
2085 
2086 	skb = ath6kl_wmi_get_new_buf(sizeof(*sc));
2087 	if (!skb)
2088 		return -ENOMEM;
2089 
2090 	ath6kl_dbg(ATH6KL_DBG_WMI, "%s scheduled scan on vif %d\n",
2091 		   enable ? "enabling" : "disabling", if_idx);
2092 	sc = (struct wmi_enable_sched_scan_cmd *) skb->data;
2093 	sc->enable = enable ? 1 : 0;
2094 
2095 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb,
2096 				  WMI_ENABLE_SCHED_SCAN_CMDID,
2097 				  NO_SYNC_WMIFLAG);
2098 	return ret;
2099 }
2100 
2101 int ath6kl_wmi_scanparams_cmd(struct wmi *wmi, u8 if_idx,
2102 			      u16 fg_start_sec,
2103 			      u16 fg_end_sec, u16 bg_sec,
2104 			      u16 minact_chdw_msec, u16 maxact_chdw_msec,
2105 			      u16 pas_chdw_msec, u8 short_scan_ratio,
2106 			      u8 scan_ctrl_flag, u32 max_dfsch_act_time,
2107 			      u16 maxact_scan_per_ssid)
2108 {
2109 	struct sk_buff *skb;
2110 	struct wmi_scan_params_cmd *sc;
2111 	int ret;
2112 
2113 	skb = ath6kl_wmi_get_new_buf(sizeof(*sc));
2114 	if (!skb)
2115 		return -ENOMEM;
2116 
2117 	sc = (struct wmi_scan_params_cmd *) skb->data;
2118 	sc->fg_start_period = cpu_to_le16(fg_start_sec);
2119 	sc->fg_end_period = cpu_to_le16(fg_end_sec);
2120 	sc->bg_period = cpu_to_le16(bg_sec);
2121 	sc->minact_chdwell_time = cpu_to_le16(minact_chdw_msec);
2122 	sc->maxact_chdwell_time = cpu_to_le16(maxact_chdw_msec);
2123 	sc->pas_chdwell_time = cpu_to_le16(pas_chdw_msec);
2124 	sc->short_scan_ratio = short_scan_ratio;
2125 	sc->scan_ctrl_flags = scan_ctrl_flag;
2126 	sc->max_dfsch_act_time = cpu_to_le32(max_dfsch_act_time);
2127 	sc->maxact_scan_per_ssid = cpu_to_le16(maxact_scan_per_ssid);
2128 
2129 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_SCAN_PARAMS_CMDID,
2130 				  NO_SYNC_WMIFLAG);
2131 	return ret;
2132 }
2133 
2134 int ath6kl_wmi_bssfilter_cmd(struct wmi *wmi, u8 if_idx, u8 filter, u32 ie_mask)
2135 {
2136 	struct sk_buff *skb;
2137 	struct wmi_bss_filter_cmd *cmd;
2138 	int ret;
2139 
2140 	if (filter >= LAST_BSS_FILTER)
2141 		return -EINVAL;
2142 
2143 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2144 	if (!skb)
2145 		return -ENOMEM;
2146 
2147 	cmd = (struct wmi_bss_filter_cmd *) skb->data;
2148 	cmd->bss_filter = filter;
2149 	cmd->ie_mask = cpu_to_le32(ie_mask);
2150 
2151 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_BSS_FILTER_CMDID,
2152 				  NO_SYNC_WMIFLAG);
2153 	return ret;
2154 }
2155 
2156 int ath6kl_wmi_probedssid_cmd(struct wmi *wmi, u8 if_idx, u8 index, u8 flag,
2157 			      u8 ssid_len, u8 *ssid)
2158 {
2159 	struct sk_buff *skb;
2160 	struct wmi_probed_ssid_cmd *cmd;
2161 	int ret;
2162 
2163 	if (index >= MAX_PROBED_SSIDS)
2164 		return -EINVAL;
2165 
2166 	if (ssid_len > sizeof(cmd->ssid))
2167 		return -EINVAL;
2168 
2169 	if ((flag & (DISABLE_SSID_FLAG | ANY_SSID_FLAG)) && (ssid_len > 0))
2170 		return -EINVAL;
2171 
2172 	if ((flag & SPECIFIC_SSID_FLAG) && !ssid_len)
2173 		return -EINVAL;
2174 
2175 	if (flag & SPECIFIC_SSID_FLAG)
2176 		wmi->is_probe_ssid = true;
2177 
2178 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2179 	if (!skb)
2180 		return -ENOMEM;
2181 
2182 	cmd = (struct wmi_probed_ssid_cmd *) skb->data;
2183 	cmd->entry_index = index;
2184 	cmd->flag = flag;
2185 	cmd->ssid_len = ssid_len;
2186 	memcpy(cmd->ssid, ssid, ssid_len);
2187 
2188 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_PROBED_SSID_CMDID,
2189 				  NO_SYNC_WMIFLAG);
2190 	return ret;
2191 }
2192 
2193 int ath6kl_wmi_listeninterval_cmd(struct wmi *wmi, u8 if_idx,
2194 				  u16 listen_interval,
2195 				  u16 listen_beacons)
2196 {
2197 	struct sk_buff *skb;
2198 	struct wmi_listen_int_cmd *cmd;
2199 	int ret;
2200 
2201 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2202 	if (!skb)
2203 		return -ENOMEM;
2204 
2205 	cmd = (struct wmi_listen_int_cmd *) skb->data;
2206 	cmd->listen_intvl = cpu_to_le16(listen_interval);
2207 	cmd->num_beacons = cpu_to_le16(listen_beacons);
2208 
2209 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_LISTEN_INT_CMDID,
2210 				  NO_SYNC_WMIFLAG);
2211 	return ret;
2212 }
2213 
2214 int ath6kl_wmi_bmisstime_cmd(struct wmi *wmi, u8 if_idx,
2215 			     u16 bmiss_time, u16 num_beacons)
2216 {
2217 	struct sk_buff *skb;
2218 	struct wmi_bmiss_time_cmd *cmd;
2219 	int ret;
2220 
2221 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2222 	if (!skb)
2223 		return -ENOMEM;
2224 
2225 	cmd = (struct wmi_bmiss_time_cmd *) skb->data;
2226 	cmd->bmiss_time = cpu_to_le16(bmiss_time);
2227 	cmd->num_beacons = cpu_to_le16(num_beacons);
2228 
2229 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_BMISS_TIME_CMDID,
2230 				  NO_SYNC_WMIFLAG);
2231 	return ret;
2232 }
2233 
2234 int ath6kl_wmi_powermode_cmd(struct wmi *wmi, u8 if_idx, u8 pwr_mode)
2235 {
2236 	struct sk_buff *skb;
2237 	struct wmi_power_mode_cmd *cmd;
2238 	int ret;
2239 
2240 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2241 	if (!skb)
2242 		return -ENOMEM;
2243 
2244 	cmd = (struct wmi_power_mode_cmd *) skb->data;
2245 	cmd->pwr_mode = pwr_mode;
2246 	wmi->pwr_mode = pwr_mode;
2247 
2248 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_POWER_MODE_CMDID,
2249 				  NO_SYNC_WMIFLAG);
2250 	return ret;
2251 }
2252 
2253 int ath6kl_wmi_pmparams_cmd(struct wmi *wmi, u8 if_idx, u16 idle_period,
2254 			    u16 ps_poll_num, u16 dtim_policy,
2255 			    u16 tx_wakeup_policy, u16 num_tx_to_wakeup,
2256 			    u16 ps_fail_event_policy)
2257 {
2258 	struct sk_buff *skb;
2259 	struct wmi_power_params_cmd *pm;
2260 	int ret;
2261 
2262 	skb = ath6kl_wmi_get_new_buf(sizeof(*pm));
2263 	if (!skb)
2264 		return -ENOMEM;
2265 
2266 	pm = (struct wmi_power_params_cmd *)skb->data;
2267 	pm->idle_period = cpu_to_le16(idle_period);
2268 	pm->pspoll_number = cpu_to_le16(ps_poll_num);
2269 	pm->dtim_policy = cpu_to_le16(dtim_policy);
2270 	pm->tx_wakeup_policy = cpu_to_le16(tx_wakeup_policy);
2271 	pm->num_tx_to_wakeup = cpu_to_le16(num_tx_to_wakeup);
2272 	pm->ps_fail_event_policy = cpu_to_le16(ps_fail_event_policy);
2273 
2274 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_POWER_PARAMS_CMDID,
2275 				  NO_SYNC_WMIFLAG);
2276 	return ret;
2277 }
2278 
2279 int ath6kl_wmi_disctimeout_cmd(struct wmi *wmi, u8 if_idx, u8 timeout)
2280 {
2281 	struct sk_buff *skb;
2282 	struct wmi_disc_timeout_cmd *cmd;
2283 	int ret;
2284 
2285 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2286 	if (!skb)
2287 		return -ENOMEM;
2288 
2289 	cmd = (struct wmi_disc_timeout_cmd *) skb->data;
2290 	cmd->discon_timeout = timeout;
2291 
2292 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_DISC_TIMEOUT_CMDID,
2293 				  NO_SYNC_WMIFLAG);
2294 
2295 	if (ret == 0)
2296 		ath6kl_debug_set_disconnect_timeout(wmi->parent_dev, timeout);
2297 
2298 	return ret;
2299 }
2300 
2301 int ath6kl_wmi_addkey_cmd(struct wmi *wmi, u8 if_idx, u8 key_index,
2302 			  enum ath6kl_crypto_type key_type,
2303 			  u8 key_usage, u8 key_len,
2304 			  u8 *key_rsc, unsigned int key_rsc_len,
2305 			  u8 *key_material,
2306 			  u8 key_op_ctrl, u8 *mac_addr,
2307 			  enum wmi_sync_flag sync_flag)
2308 {
2309 	struct sk_buff *skb;
2310 	struct wmi_add_cipher_key_cmd *cmd;
2311 	int ret;
2312 
2313 	ath6kl_dbg(ATH6KL_DBG_WMI,
2314 		   "addkey cmd: key_index=%u key_type=%d key_usage=%d key_len=%d key_op_ctrl=%d\n",
2315 		   key_index, key_type, key_usage, key_len, key_op_ctrl);
2316 
2317 	if ((key_index > WMI_MAX_KEY_INDEX) || (key_len > WMI_MAX_KEY_LEN) ||
2318 	    (key_material == NULL) || key_rsc_len > 8)
2319 		return -EINVAL;
2320 
2321 	if ((WEP_CRYPT != key_type) && (NULL == key_rsc))
2322 		return -EINVAL;
2323 
2324 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2325 	if (!skb)
2326 		return -ENOMEM;
2327 
2328 	cmd = (struct wmi_add_cipher_key_cmd *) skb->data;
2329 	cmd->key_index = key_index;
2330 	cmd->key_type = key_type;
2331 	cmd->key_usage = key_usage;
2332 	cmd->key_len = key_len;
2333 	memcpy(cmd->key, key_material, key_len);
2334 
2335 	if (key_rsc != NULL)
2336 		memcpy(cmd->key_rsc, key_rsc, key_rsc_len);
2337 
2338 	cmd->key_op_ctrl = key_op_ctrl;
2339 
2340 	if (mac_addr)
2341 		memcpy(cmd->key_mac_addr, mac_addr, ETH_ALEN);
2342 
2343 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_ADD_CIPHER_KEY_CMDID,
2344 				  sync_flag);
2345 
2346 	return ret;
2347 }
2348 
2349 int ath6kl_wmi_add_krk_cmd(struct wmi *wmi, u8 if_idx, const u8 *krk)
2350 {
2351 	struct sk_buff *skb;
2352 	struct wmi_add_krk_cmd *cmd;
2353 	int ret;
2354 
2355 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2356 	if (!skb)
2357 		return -ENOMEM;
2358 
2359 	cmd = (struct wmi_add_krk_cmd *) skb->data;
2360 	memcpy(cmd->krk, krk, WMI_KRK_LEN);
2361 
2362 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_ADD_KRK_CMDID,
2363 				  NO_SYNC_WMIFLAG);
2364 
2365 	return ret;
2366 }
2367 
2368 int ath6kl_wmi_deletekey_cmd(struct wmi *wmi, u8 if_idx, u8 key_index)
2369 {
2370 	struct sk_buff *skb;
2371 	struct wmi_delete_cipher_key_cmd *cmd;
2372 	int ret;
2373 
2374 	if (key_index > WMI_MAX_KEY_INDEX)
2375 		return -EINVAL;
2376 
2377 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2378 	if (!skb)
2379 		return -ENOMEM;
2380 
2381 	cmd = (struct wmi_delete_cipher_key_cmd *) skb->data;
2382 	cmd->key_index = key_index;
2383 
2384 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_DELETE_CIPHER_KEY_CMDID,
2385 				  NO_SYNC_WMIFLAG);
2386 
2387 	return ret;
2388 }
2389 
2390 int ath6kl_wmi_setpmkid_cmd(struct wmi *wmi, u8 if_idx, const u8 *bssid,
2391 			    const u8 *pmkid, bool set)
2392 {
2393 	struct sk_buff *skb;
2394 	struct wmi_setpmkid_cmd *cmd;
2395 	int ret;
2396 
2397 	if (bssid == NULL)
2398 		return -EINVAL;
2399 
2400 	if (set && pmkid == NULL)
2401 		return -EINVAL;
2402 
2403 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2404 	if (!skb)
2405 		return -ENOMEM;
2406 
2407 	cmd = (struct wmi_setpmkid_cmd *) skb->data;
2408 	memcpy(cmd->bssid, bssid, ETH_ALEN);
2409 	if (set) {
2410 		memcpy(cmd->pmkid, pmkid, sizeof(cmd->pmkid));
2411 		cmd->enable = PMKID_ENABLE;
2412 	} else {
2413 		memset(cmd->pmkid, 0, sizeof(cmd->pmkid));
2414 		cmd->enable = PMKID_DISABLE;
2415 	}
2416 
2417 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_PMKID_CMDID,
2418 				  NO_SYNC_WMIFLAG);
2419 
2420 	return ret;
2421 }
2422 
2423 static int ath6kl_wmi_data_sync_send(struct wmi *wmi, struct sk_buff *skb,
2424 			      enum htc_endpoint_id ep_id, u8 if_idx)
2425 {
2426 	struct wmi_data_hdr *data_hdr;
2427 	int ret;
2428 
2429 	if (WARN_ON(skb == NULL || ep_id == wmi->ep_id)) {
2430 		dev_kfree_skb(skb);
2431 		return -EINVAL;
2432 	}
2433 
2434 	skb_push(skb, sizeof(struct wmi_data_hdr));
2435 
2436 	data_hdr = (struct wmi_data_hdr *) skb->data;
2437 	data_hdr->info = SYNC_MSGTYPE << WMI_DATA_HDR_MSG_TYPE_SHIFT;
2438 	data_hdr->info3 = cpu_to_le16(if_idx & WMI_DATA_HDR_IF_IDX_MASK);
2439 
2440 	ret = ath6kl_control_tx(wmi->parent_dev, skb, ep_id);
2441 
2442 	return ret;
2443 }
2444 
2445 static int ath6kl_wmi_sync_point(struct wmi *wmi, u8 if_idx)
2446 {
2447 	struct sk_buff *skb;
2448 	struct wmi_sync_cmd *cmd;
2449 	struct wmi_data_sync_bufs data_sync_bufs[WMM_NUM_AC];
2450 	enum htc_endpoint_id ep_id;
2451 	u8 index, num_pri_streams = 0;
2452 	int ret = 0;
2453 
2454 	memset(data_sync_bufs, 0, sizeof(data_sync_bufs));
2455 
2456 	spin_lock_bh(&wmi->lock);
2457 
2458 	for (index = 0; index < WMM_NUM_AC; index++) {
2459 		if (wmi->fat_pipe_exist & (1 << index)) {
2460 			num_pri_streams++;
2461 			data_sync_bufs[num_pri_streams - 1].traffic_class =
2462 			    index;
2463 		}
2464 	}
2465 
2466 	spin_unlock_bh(&wmi->lock);
2467 
2468 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2469 	if (!skb)
2470 		return -ENOMEM;
2471 
2472 	cmd = (struct wmi_sync_cmd *) skb->data;
2473 
2474 	/*
2475 	 * In the SYNC cmd sent on the control Ep, send a bitmap
2476 	 * of the data eps on which the Data Sync will be sent
2477 	 */
2478 	cmd->data_sync_map = wmi->fat_pipe_exist;
2479 
2480 	for (index = 0; index < num_pri_streams; index++) {
2481 		data_sync_bufs[index].skb = ath6kl_buf_alloc(0);
2482 		if (data_sync_bufs[index].skb == NULL) {
2483 			ret = -ENOMEM;
2484 			break;
2485 		}
2486 	}
2487 
2488 	/*
2489 	 * If buffer allocation for any of the dataSync fails,
2490 	 * then do not send the Synchronize cmd on the control ep
2491 	 */
2492 	if (ret)
2493 		goto free_cmd_skb;
2494 
2495 	/*
2496 	 * Send sync cmd followed by sync data messages on all
2497 	 * endpoints being used
2498 	 */
2499 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SYNCHRONIZE_CMDID,
2500 				  NO_SYNC_WMIFLAG);
2501 
2502 	if (ret)
2503 		goto free_data_skb;
2504 
2505 	for (index = 0; index < num_pri_streams; index++) {
2506 		if (WARN_ON(!data_sync_bufs[index].skb))
2507 			goto free_data_skb;
2508 
2509 		ep_id = ath6kl_ac2_endpoint_id(wmi->parent_dev,
2510 					       data_sync_bufs[index].
2511 					       traffic_class);
2512 		ret =
2513 		    ath6kl_wmi_data_sync_send(wmi, data_sync_bufs[index].skb,
2514 					      ep_id, if_idx);
2515 
2516 		data_sync_bufs[index].skb = NULL;
2517 
2518 		if (ret)
2519 			goto free_data_skb;
2520 	}
2521 
2522 	return 0;
2523 
2524 free_cmd_skb:
2525 	/* free up any resources left over (possibly due to an error) */
2526 	dev_kfree_skb(skb);
2527 
2528 free_data_skb:
2529 	for (index = 0; index < num_pri_streams; index++)
2530 		dev_kfree_skb((struct sk_buff *)data_sync_bufs[index].skb);
2531 
2532 	return ret;
2533 }
2534 
2535 int ath6kl_wmi_create_pstream_cmd(struct wmi *wmi, u8 if_idx,
2536 				  struct wmi_create_pstream_cmd *params)
2537 {
2538 	struct sk_buff *skb;
2539 	struct wmi_create_pstream_cmd *cmd;
2540 	u8 fatpipe_exist_for_ac = 0;
2541 	s32 min_phy = 0;
2542 	s32 nominal_phy = 0;
2543 	int ret;
2544 
2545 	if (!((params->user_pri <= 0x7) &&
2546 	      (up_to_ac[params->user_pri & 0x7] == params->traffic_class) &&
2547 	      (params->traffic_direc == UPLINK_TRAFFIC ||
2548 	       params->traffic_direc == DNLINK_TRAFFIC ||
2549 	       params->traffic_direc == BIDIR_TRAFFIC) &&
2550 	      (params->traffic_type == TRAFFIC_TYPE_APERIODIC ||
2551 	       params->traffic_type == TRAFFIC_TYPE_PERIODIC) &&
2552 	      (params->voice_psc_cap == DISABLE_FOR_THIS_AC ||
2553 	       params->voice_psc_cap == ENABLE_FOR_THIS_AC ||
2554 	       params->voice_psc_cap == ENABLE_FOR_ALL_AC) &&
2555 	      (params->tsid == WMI_IMPLICIT_PSTREAM ||
2556 	       params->tsid <= WMI_MAX_THINSTREAM))) {
2557 		return -EINVAL;
2558 	}
2559 
2560 	/*
2561 	 * Check nominal PHY rate is >= minimalPHY,
2562 	 * so that DUT can allow TSRS IE
2563 	 */
2564 
2565 	/* Get the physical rate (units of bps) */
2566 	min_phy = ((le32_to_cpu(params->min_phy_rate) / 1000) / 1000);
2567 
2568 	/* Check minimal phy < nominal phy rate */
2569 	if (params->nominal_phy >= min_phy) {
2570 		/* unit of 500 kbps */
2571 		nominal_phy = (params->nominal_phy * 1000) / 500;
2572 		ath6kl_dbg(ATH6KL_DBG_WMI,
2573 			   "TSRS IE enabled::MinPhy %x->NominalPhy ===> %x\n",
2574 			   min_phy, nominal_phy);
2575 
2576 		params->nominal_phy = nominal_phy;
2577 	} else {
2578 		params->nominal_phy = 0;
2579 	}
2580 
2581 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2582 	if (!skb)
2583 		return -ENOMEM;
2584 
2585 	ath6kl_dbg(ATH6KL_DBG_WMI,
2586 		   "sending create_pstream_cmd: ac=%d  tsid:%d\n",
2587 		   params->traffic_class, params->tsid);
2588 
2589 	cmd = (struct wmi_create_pstream_cmd *) skb->data;
2590 	memcpy(cmd, params, sizeof(*cmd));
2591 
2592 	/* This is an implicitly created Fat pipe */
2593 	if ((u32) params->tsid == (u32) WMI_IMPLICIT_PSTREAM) {
2594 		spin_lock_bh(&wmi->lock);
2595 		fatpipe_exist_for_ac = (wmi->fat_pipe_exist &
2596 					(1 << params->traffic_class));
2597 		wmi->fat_pipe_exist |= (1 << params->traffic_class);
2598 		spin_unlock_bh(&wmi->lock);
2599 	} else {
2600 		/* explicitly created thin stream within a fat pipe */
2601 		spin_lock_bh(&wmi->lock);
2602 		fatpipe_exist_for_ac = (wmi->fat_pipe_exist &
2603 					(1 << params->traffic_class));
2604 		wmi->stream_exist_for_ac[params->traffic_class] |=
2605 		    (1 << params->tsid);
2606 		/*
2607 		 * If a thinstream becomes active, the fat pipe automatically
2608 		 * becomes active
2609 		 */
2610 		wmi->fat_pipe_exist |= (1 << params->traffic_class);
2611 		spin_unlock_bh(&wmi->lock);
2612 	}
2613 
2614 	/*
2615 	 * Indicate activty change to driver layer only if this is the
2616 	 * first TSID to get created in this AC explicitly or an implicit
2617 	 * fat pipe is getting created.
2618 	 */
2619 	if (!fatpipe_exist_for_ac)
2620 		ath6kl_indicate_tx_activity(wmi->parent_dev,
2621 					    params->traffic_class, true);
2622 
2623 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_CREATE_PSTREAM_CMDID,
2624 				  NO_SYNC_WMIFLAG);
2625 	return ret;
2626 }
2627 
2628 int ath6kl_wmi_delete_pstream_cmd(struct wmi *wmi, u8 if_idx, u8 traffic_class,
2629 				  u8 tsid)
2630 {
2631 	struct sk_buff *skb;
2632 	struct wmi_delete_pstream_cmd *cmd;
2633 	u16 active_tsids = 0;
2634 	int ret;
2635 
2636 	if (traffic_class > 3) {
2637 		ath6kl_err("invalid traffic class: %d\n", traffic_class);
2638 		return -EINVAL;
2639 	}
2640 
2641 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2642 	if (!skb)
2643 		return -ENOMEM;
2644 
2645 	cmd = (struct wmi_delete_pstream_cmd *) skb->data;
2646 	cmd->traffic_class = traffic_class;
2647 	cmd->tsid = tsid;
2648 
2649 	spin_lock_bh(&wmi->lock);
2650 	active_tsids = wmi->stream_exist_for_ac[traffic_class];
2651 	spin_unlock_bh(&wmi->lock);
2652 
2653 	if (!(active_tsids & (1 << tsid))) {
2654 		dev_kfree_skb(skb);
2655 		ath6kl_dbg(ATH6KL_DBG_WMI,
2656 			   "TSID %d doesn't exist for traffic class: %d\n",
2657 			   tsid, traffic_class);
2658 		return -ENODATA;
2659 	}
2660 
2661 	ath6kl_dbg(ATH6KL_DBG_WMI,
2662 		   "sending delete_pstream_cmd: traffic class: %d tsid=%d\n",
2663 		   traffic_class, tsid);
2664 
2665 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_DELETE_PSTREAM_CMDID,
2666 				  SYNC_BEFORE_WMIFLAG);
2667 
2668 	spin_lock_bh(&wmi->lock);
2669 	wmi->stream_exist_for_ac[traffic_class] &= ~(1 << tsid);
2670 	active_tsids = wmi->stream_exist_for_ac[traffic_class];
2671 	spin_unlock_bh(&wmi->lock);
2672 
2673 	/*
2674 	 * Indicate stream inactivity to driver layer only if all tsids
2675 	 * within this AC are deleted.
2676 	 */
2677 	if (!active_tsids) {
2678 		ath6kl_indicate_tx_activity(wmi->parent_dev,
2679 					    traffic_class, false);
2680 		wmi->fat_pipe_exist &= ~(1 << traffic_class);
2681 	}
2682 
2683 	return ret;
2684 }
2685 
2686 int ath6kl_wmi_set_ip_cmd(struct wmi *wmi, u8 if_idx,
2687 			  __be32 ips0, __be32 ips1)
2688 {
2689 	struct sk_buff *skb;
2690 	struct wmi_set_ip_cmd *cmd;
2691 	int ret;
2692 
2693 	/* Multicast address are not valid */
2694 	if (ipv4_is_multicast(ips0) ||
2695 	    ipv4_is_multicast(ips1))
2696 		return -EINVAL;
2697 
2698 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_ip_cmd));
2699 	if (!skb)
2700 		return -ENOMEM;
2701 
2702 	cmd = (struct wmi_set_ip_cmd *) skb->data;
2703 	cmd->ips[0] = ips0;
2704 	cmd->ips[1] = ips1;
2705 
2706 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_IP_CMDID,
2707 				  NO_SYNC_WMIFLAG);
2708 	return ret;
2709 }
2710 
2711 static void ath6kl_wmi_relinquish_implicit_pstream_credits(struct wmi *wmi)
2712 {
2713 	u16 active_tsids;
2714 	u8 stream_exist;
2715 	int i;
2716 
2717 	/*
2718 	 * Relinquish credits from all implicitly created pstreams
2719 	 * since when we go to sleep. If user created explicit
2720 	 * thinstreams exists with in a fatpipe leave them intact
2721 	 * for the user to delete.
2722 	 */
2723 	spin_lock_bh(&wmi->lock);
2724 	stream_exist = wmi->fat_pipe_exist;
2725 	spin_unlock_bh(&wmi->lock);
2726 
2727 	for (i = 0; i < WMM_NUM_AC; i++) {
2728 		if (stream_exist & (1 << i)) {
2729 			/*
2730 			 * FIXME: Is this lock & unlock inside
2731 			 * for loop correct? may need rework.
2732 			 */
2733 			spin_lock_bh(&wmi->lock);
2734 			active_tsids = wmi->stream_exist_for_ac[i];
2735 			spin_unlock_bh(&wmi->lock);
2736 
2737 			/*
2738 			 * If there are no user created thin streams
2739 			 * delete the fatpipe
2740 			 */
2741 			if (!active_tsids) {
2742 				stream_exist &= ~(1 << i);
2743 				/*
2744 				 * Indicate inactivity to driver layer for
2745 				 * this fatpipe (pstream)
2746 				 */
2747 				ath6kl_indicate_tx_activity(wmi->parent_dev,
2748 							    i, false);
2749 			}
2750 		}
2751 	}
2752 
2753 	/* FIXME: Can we do this assignment without locking ? */
2754 	spin_lock_bh(&wmi->lock);
2755 	wmi->fat_pipe_exist = stream_exist;
2756 	spin_unlock_bh(&wmi->lock);
2757 }
2758 
2759 static int ath6kl_set_bitrate_mask64(struct wmi *wmi, u8 if_idx,
2760 				     const struct cfg80211_bitrate_mask *mask)
2761 {
2762 	struct sk_buff *skb;
2763 	int ret, mode, band;
2764 	u64 mcsrate, ratemask[ATH6KL_NUM_BANDS];
2765 	struct wmi_set_tx_select_rates64_cmd *cmd;
2766 
2767 	memset(&ratemask, 0, sizeof(ratemask));
2768 
2769 	/* only check 2.4 and 5 GHz bands, skip the rest */
2770 	for (band = 0; band <= NL80211_BAND_5GHZ; band++) {
2771 		/* copy legacy rate mask */
2772 		ratemask[band] = mask->control[band].legacy;
2773 		if (band == NL80211_BAND_5GHZ)
2774 			ratemask[band] =
2775 				mask->control[band].legacy << 4;
2776 
2777 		/* copy mcs rate mask */
2778 		mcsrate = mask->control[band].ht_mcs[1];
2779 		mcsrate <<= 8;
2780 		mcsrate |= mask->control[band].ht_mcs[0];
2781 		ratemask[band] |= mcsrate << 12;
2782 		ratemask[band] |= mcsrate << 28;
2783 	}
2784 
2785 	ath6kl_dbg(ATH6KL_DBG_WMI,
2786 		   "Ratemask 64 bit: 2.4:%llx 5:%llx\n",
2787 		   ratemask[0], ratemask[1]);
2788 
2789 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd) * WMI_RATES_MODE_MAX);
2790 	if (!skb)
2791 		return -ENOMEM;
2792 
2793 	cmd = (struct wmi_set_tx_select_rates64_cmd *) skb->data;
2794 	for (mode = 0; mode < WMI_RATES_MODE_MAX; mode++) {
2795 		/* A mode operate in 5GHZ band */
2796 		if (mode == WMI_RATES_MODE_11A ||
2797 		    mode == WMI_RATES_MODE_11A_HT20 ||
2798 		    mode == WMI_RATES_MODE_11A_HT40)
2799 			band = NL80211_BAND_5GHZ;
2800 		else
2801 			band = NL80211_BAND_2GHZ;
2802 		cmd->ratemask[mode] = cpu_to_le64(ratemask[band]);
2803 	}
2804 
2805 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb,
2806 				  WMI_SET_TX_SELECT_RATES_CMDID,
2807 				  NO_SYNC_WMIFLAG);
2808 	return ret;
2809 }
2810 
2811 static int ath6kl_set_bitrate_mask32(struct wmi *wmi, u8 if_idx,
2812 				     const struct cfg80211_bitrate_mask *mask)
2813 {
2814 	struct sk_buff *skb;
2815 	int ret, mode, band;
2816 	u32 mcsrate, ratemask[ATH6KL_NUM_BANDS];
2817 	struct wmi_set_tx_select_rates32_cmd *cmd;
2818 
2819 	memset(&ratemask, 0, sizeof(ratemask));
2820 
2821 	/* only check 2.4 and 5 GHz bands, skip the rest */
2822 	for (band = 0; band <= NL80211_BAND_5GHZ; band++) {
2823 		/* copy legacy rate mask */
2824 		ratemask[band] = mask->control[band].legacy;
2825 		if (band == NL80211_BAND_5GHZ)
2826 			ratemask[band] =
2827 				mask->control[band].legacy << 4;
2828 
2829 		/* copy mcs rate mask */
2830 		mcsrate = mask->control[band].ht_mcs[0];
2831 		ratemask[band] |= mcsrate << 12;
2832 		ratemask[band] |= mcsrate << 20;
2833 	}
2834 
2835 	ath6kl_dbg(ATH6KL_DBG_WMI,
2836 		   "Ratemask 32 bit: 2.4:%x 5:%x\n",
2837 		   ratemask[0], ratemask[1]);
2838 
2839 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd) * WMI_RATES_MODE_MAX);
2840 	if (!skb)
2841 		return -ENOMEM;
2842 
2843 	cmd = (struct wmi_set_tx_select_rates32_cmd *) skb->data;
2844 	for (mode = 0; mode < WMI_RATES_MODE_MAX; mode++) {
2845 		/* A mode operate in 5GHZ band */
2846 		if (mode == WMI_RATES_MODE_11A ||
2847 		    mode == WMI_RATES_MODE_11A_HT20 ||
2848 		    mode == WMI_RATES_MODE_11A_HT40)
2849 			band = NL80211_BAND_5GHZ;
2850 		else
2851 			band = NL80211_BAND_2GHZ;
2852 		cmd->ratemask[mode] = cpu_to_le32(ratemask[band]);
2853 	}
2854 
2855 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb,
2856 				  WMI_SET_TX_SELECT_RATES_CMDID,
2857 				  NO_SYNC_WMIFLAG);
2858 	return ret;
2859 }
2860 
2861 int ath6kl_wmi_set_bitrate_mask(struct wmi *wmi, u8 if_idx,
2862 				const struct cfg80211_bitrate_mask *mask)
2863 {
2864 	struct ath6kl *ar = wmi->parent_dev;
2865 
2866 	if (test_bit(ATH6KL_FW_CAPABILITY_64BIT_RATES,
2867 		     ar->fw_capabilities))
2868 		return ath6kl_set_bitrate_mask64(wmi, if_idx, mask);
2869 	else
2870 		return ath6kl_set_bitrate_mask32(wmi, if_idx, mask);
2871 }
2872 
2873 int ath6kl_wmi_set_host_sleep_mode_cmd(struct wmi *wmi, u8 if_idx,
2874 				       enum ath6kl_host_mode host_mode)
2875 {
2876 	struct sk_buff *skb;
2877 	struct wmi_set_host_sleep_mode_cmd *cmd;
2878 	int ret;
2879 
2880 	if ((host_mode != ATH6KL_HOST_MODE_ASLEEP) &&
2881 	    (host_mode != ATH6KL_HOST_MODE_AWAKE)) {
2882 		ath6kl_err("invalid host sleep mode: %d\n", host_mode);
2883 		return -EINVAL;
2884 	}
2885 
2886 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2887 	if (!skb)
2888 		return -ENOMEM;
2889 
2890 	cmd = (struct wmi_set_host_sleep_mode_cmd *) skb->data;
2891 
2892 	if (host_mode == ATH6KL_HOST_MODE_ASLEEP) {
2893 		ath6kl_wmi_relinquish_implicit_pstream_credits(wmi);
2894 		cmd->asleep = cpu_to_le32(1);
2895 	} else {
2896 		cmd->awake = cpu_to_le32(1);
2897 	}
2898 
2899 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb,
2900 				  WMI_SET_HOST_SLEEP_MODE_CMDID,
2901 				  NO_SYNC_WMIFLAG);
2902 	return ret;
2903 }
2904 
2905 /* This command has zero length payload */
2906 static int ath6kl_wmi_host_sleep_mode_cmd_prcd_evt_rx(struct wmi *wmi,
2907 						      struct ath6kl_vif *vif)
2908 {
2909 	struct ath6kl *ar = wmi->parent_dev;
2910 
2911 	set_bit(HOST_SLEEP_MODE_CMD_PROCESSED, &vif->flags);
2912 	wake_up(&ar->event_wq);
2913 
2914 	return 0;
2915 }
2916 
2917 int ath6kl_wmi_set_wow_mode_cmd(struct wmi *wmi, u8 if_idx,
2918 				enum ath6kl_wow_mode wow_mode,
2919 				u32 filter, u16 host_req_delay)
2920 {
2921 	struct sk_buff *skb;
2922 	struct wmi_set_wow_mode_cmd *cmd;
2923 	int ret;
2924 
2925 	if ((wow_mode != ATH6KL_WOW_MODE_ENABLE) &&
2926 	    wow_mode != ATH6KL_WOW_MODE_DISABLE) {
2927 		ath6kl_err("invalid wow mode: %d\n", wow_mode);
2928 		return -EINVAL;
2929 	}
2930 
2931 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2932 	if (!skb)
2933 		return -ENOMEM;
2934 
2935 	cmd = (struct wmi_set_wow_mode_cmd *) skb->data;
2936 	cmd->enable_wow = cpu_to_le32(wow_mode);
2937 	cmd->filter = cpu_to_le32(filter);
2938 	cmd->host_req_delay = cpu_to_le16(host_req_delay);
2939 
2940 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_WOW_MODE_CMDID,
2941 				  NO_SYNC_WMIFLAG);
2942 	return ret;
2943 }
2944 
2945 int ath6kl_wmi_add_wow_pattern_cmd(struct wmi *wmi, u8 if_idx,
2946 				   u8 list_id, u8 filter_size,
2947 				   u8 filter_offset, const u8 *filter,
2948 				   const u8 *mask)
2949 {
2950 	struct sk_buff *skb;
2951 	struct wmi_add_wow_pattern_cmd *cmd;
2952 	u16 size;
2953 	u8 *filter_mask;
2954 	int ret;
2955 
2956 	/*
2957 	 * Allocate additional memory in the buffer to hold
2958 	 * filter and mask value, which is twice of filter_size.
2959 	 */
2960 	size = sizeof(*cmd) + (2 * filter_size);
2961 
2962 	skb = ath6kl_wmi_get_new_buf(size);
2963 	if (!skb)
2964 		return -ENOMEM;
2965 
2966 	cmd = (struct wmi_add_wow_pattern_cmd *) skb->data;
2967 	cmd->filter_list_id = list_id;
2968 	cmd->filter_size = filter_size;
2969 	cmd->filter_offset = filter_offset;
2970 
2971 	memcpy(cmd->filter, filter, filter_size);
2972 
2973 	filter_mask = (u8 *) (cmd->filter + filter_size);
2974 	memcpy(filter_mask, mask, filter_size);
2975 
2976 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_ADD_WOW_PATTERN_CMDID,
2977 				  NO_SYNC_WMIFLAG);
2978 
2979 	return ret;
2980 }
2981 
2982 int ath6kl_wmi_del_wow_pattern_cmd(struct wmi *wmi, u8 if_idx,
2983 				   u16 list_id, u16 filter_id)
2984 {
2985 	struct sk_buff *skb;
2986 	struct wmi_del_wow_pattern_cmd *cmd;
2987 	int ret;
2988 
2989 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2990 	if (!skb)
2991 		return -ENOMEM;
2992 
2993 	cmd = (struct wmi_del_wow_pattern_cmd *) skb->data;
2994 	cmd->filter_list_id = cpu_to_le16(list_id);
2995 	cmd->filter_id = cpu_to_le16(filter_id);
2996 
2997 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_DEL_WOW_PATTERN_CMDID,
2998 				  NO_SYNC_WMIFLAG);
2999 	return ret;
3000 }
3001 
3002 static int ath6kl_wmi_cmd_send_xtnd(struct wmi *wmi, struct sk_buff *skb,
3003 				    enum wmix_command_id cmd_id,
3004 				    enum wmi_sync_flag sync_flag)
3005 {
3006 	struct wmix_cmd_hdr *cmd_hdr;
3007 	int ret;
3008 
3009 	skb_push(skb, sizeof(struct wmix_cmd_hdr));
3010 
3011 	cmd_hdr = (struct wmix_cmd_hdr *) skb->data;
3012 	cmd_hdr->cmd_id = cpu_to_le32(cmd_id);
3013 
3014 	ret = ath6kl_wmi_cmd_send(wmi, 0, skb, WMI_EXTENSION_CMDID, sync_flag);
3015 
3016 	return ret;
3017 }
3018 
3019 int ath6kl_wmi_get_challenge_resp_cmd(struct wmi *wmi, u32 cookie, u32 source)
3020 {
3021 	struct sk_buff *skb;
3022 	struct wmix_hb_challenge_resp_cmd *cmd;
3023 	int ret;
3024 
3025 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3026 	if (!skb)
3027 		return -ENOMEM;
3028 
3029 	cmd = (struct wmix_hb_challenge_resp_cmd *) skb->data;
3030 	cmd->cookie = cpu_to_le32(cookie);
3031 	cmd->source = cpu_to_le32(source);
3032 
3033 	ret = ath6kl_wmi_cmd_send_xtnd(wmi, skb, WMIX_HB_CHALLENGE_RESP_CMDID,
3034 				       NO_SYNC_WMIFLAG);
3035 	return ret;
3036 }
3037 
3038 int ath6kl_wmi_config_debug_module_cmd(struct wmi *wmi, u32 valid, u32 config)
3039 {
3040 	struct ath6kl_wmix_dbglog_cfg_module_cmd *cmd;
3041 	struct sk_buff *skb;
3042 	int ret;
3043 
3044 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3045 	if (!skb)
3046 		return -ENOMEM;
3047 
3048 	cmd = (struct ath6kl_wmix_dbglog_cfg_module_cmd *) skb->data;
3049 	cmd->valid = cpu_to_le32(valid);
3050 	cmd->config = cpu_to_le32(config);
3051 
3052 	ret = ath6kl_wmi_cmd_send_xtnd(wmi, skb, WMIX_DBGLOG_CFG_MODULE_CMDID,
3053 				       NO_SYNC_WMIFLAG);
3054 	return ret;
3055 }
3056 
3057 int ath6kl_wmi_get_stats_cmd(struct wmi *wmi, u8 if_idx)
3058 {
3059 	return ath6kl_wmi_simple_cmd(wmi, if_idx, WMI_GET_STATISTICS_CMDID);
3060 }
3061 
3062 int ath6kl_wmi_set_tx_pwr_cmd(struct wmi *wmi, u8 if_idx, u8 dbM)
3063 {
3064 	struct sk_buff *skb;
3065 	struct wmi_set_tx_pwr_cmd *cmd;
3066 	int ret;
3067 
3068 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_tx_pwr_cmd));
3069 	if (!skb)
3070 		return -ENOMEM;
3071 
3072 	cmd = (struct wmi_set_tx_pwr_cmd *) skb->data;
3073 	cmd->dbM = dbM;
3074 
3075 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_TX_PWR_CMDID,
3076 				  NO_SYNC_WMIFLAG);
3077 
3078 	return ret;
3079 }
3080 
3081 int ath6kl_wmi_get_tx_pwr_cmd(struct wmi *wmi, u8 if_idx)
3082 {
3083 	return ath6kl_wmi_simple_cmd(wmi, if_idx, WMI_GET_TX_PWR_CMDID);
3084 }
3085 
3086 int ath6kl_wmi_get_roam_tbl_cmd(struct wmi *wmi)
3087 {
3088 	return ath6kl_wmi_simple_cmd(wmi, 0, WMI_GET_ROAM_TBL_CMDID);
3089 }
3090 
3091 int ath6kl_wmi_set_lpreamble_cmd(struct wmi *wmi, u8 if_idx, u8 status,
3092 				 u8 preamble_policy)
3093 {
3094 	struct sk_buff *skb;
3095 	struct wmi_set_lpreamble_cmd *cmd;
3096 	int ret;
3097 
3098 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_lpreamble_cmd));
3099 	if (!skb)
3100 		return -ENOMEM;
3101 
3102 	cmd = (struct wmi_set_lpreamble_cmd *) skb->data;
3103 	cmd->status = status;
3104 	cmd->preamble_policy = preamble_policy;
3105 
3106 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_LPREAMBLE_CMDID,
3107 				  NO_SYNC_WMIFLAG);
3108 	return ret;
3109 }
3110 
3111 int ath6kl_wmi_set_rts_cmd(struct wmi *wmi, u16 threshold)
3112 {
3113 	struct sk_buff *skb;
3114 	struct wmi_set_rts_cmd *cmd;
3115 	int ret;
3116 
3117 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_rts_cmd));
3118 	if (!skb)
3119 		return -ENOMEM;
3120 
3121 	cmd = (struct wmi_set_rts_cmd *) skb->data;
3122 	cmd->threshold = cpu_to_le16(threshold);
3123 
3124 	ret = ath6kl_wmi_cmd_send(wmi, 0, skb, WMI_SET_RTS_CMDID,
3125 				  NO_SYNC_WMIFLAG);
3126 	return ret;
3127 }
3128 
3129 int ath6kl_wmi_set_wmm_txop(struct wmi *wmi, u8 if_idx, enum wmi_txop_cfg cfg)
3130 {
3131 	struct sk_buff *skb;
3132 	struct wmi_set_wmm_txop_cmd *cmd;
3133 	int ret;
3134 
3135 	if (!((cfg == WMI_TXOP_DISABLED) || (cfg == WMI_TXOP_ENABLED)))
3136 		return -EINVAL;
3137 
3138 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_wmm_txop_cmd));
3139 	if (!skb)
3140 		return -ENOMEM;
3141 
3142 	cmd = (struct wmi_set_wmm_txop_cmd *) skb->data;
3143 	cmd->txop_enable = cfg;
3144 
3145 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_WMM_TXOP_CMDID,
3146 				  NO_SYNC_WMIFLAG);
3147 	return ret;
3148 }
3149 
3150 int ath6kl_wmi_set_keepalive_cmd(struct wmi *wmi, u8 if_idx,
3151 				 u8 keep_alive_intvl)
3152 {
3153 	struct sk_buff *skb;
3154 	struct wmi_set_keepalive_cmd *cmd;
3155 	int ret;
3156 
3157 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3158 	if (!skb)
3159 		return -ENOMEM;
3160 
3161 	cmd = (struct wmi_set_keepalive_cmd *) skb->data;
3162 	cmd->keep_alive_intvl = keep_alive_intvl;
3163 
3164 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_KEEPALIVE_CMDID,
3165 				  NO_SYNC_WMIFLAG);
3166 
3167 	if (ret == 0)
3168 		ath6kl_debug_set_keepalive(wmi->parent_dev, keep_alive_intvl);
3169 
3170 	return ret;
3171 }
3172 
3173 int ath6kl_wmi_set_htcap_cmd(struct wmi *wmi, u8 if_idx,
3174 			     enum nl80211_band band,
3175 			     struct ath6kl_htcap *htcap)
3176 {
3177 	struct sk_buff *skb;
3178 	struct wmi_set_htcap_cmd *cmd;
3179 
3180 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3181 	if (!skb)
3182 		return -ENOMEM;
3183 
3184 	cmd = (struct wmi_set_htcap_cmd *) skb->data;
3185 
3186 	/*
3187 	 * NOTE: Band in firmware matches enum nl80211_band, it is unlikely
3188 	 * this will be changed in firmware. If at all there is any change in
3189 	 * band value, the host needs to be fixed.
3190 	 */
3191 	cmd->band = band;
3192 	cmd->ht_enable = !!htcap->ht_enable;
3193 	cmd->ht20_sgi = !!(htcap->cap_info & IEEE80211_HT_CAP_SGI_20);
3194 	cmd->ht40_supported =
3195 		!!(htcap->cap_info & IEEE80211_HT_CAP_SUP_WIDTH_20_40);
3196 	cmd->ht40_sgi = !!(htcap->cap_info & IEEE80211_HT_CAP_SGI_40);
3197 	cmd->intolerant_40mhz =
3198 		!!(htcap->cap_info & IEEE80211_HT_CAP_40MHZ_INTOLERANT);
3199 	cmd->max_ampdu_len_exp = htcap->ampdu_factor;
3200 
3201 	ath6kl_dbg(ATH6KL_DBG_WMI,
3202 		   "Set htcap: band:%d ht_enable:%d 40mhz:%d sgi_20mhz:%d sgi_40mhz:%d 40mhz_intolerant:%d ampdu_len_exp:%d\n",
3203 		   cmd->band, cmd->ht_enable, cmd->ht40_supported,
3204 		   cmd->ht20_sgi, cmd->ht40_sgi, cmd->intolerant_40mhz,
3205 		   cmd->max_ampdu_len_exp);
3206 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_HT_CAP_CMDID,
3207 				   NO_SYNC_WMIFLAG);
3208 }
3209 
3210 int ath6kl_wmi_test_cmd(struct wmi *wmi, void *buf, size_t len)
3211 {
3212 	struct sk_buff *skb;
3213 	int ret;
3214 
3215 	skb = ath6kl_wmi_get_new_buf(len);
3216 	if (!skb)
3217 		return -ENOMEM;
3218 
3219 	memcpy(skb->data, buf, len);
3220 
3221 	ret = ath6kl_wmi_cmd_send(wmi, 0, skb, WMI_TEST_CMDID, NO_SYNC_WMIFLAG);
3222 
3223 	return ret;
3224 }
3225 
3226 int ath6kl_wmi_mcast_filter_cmd(struct wmi *wmi, u8 if_idx, bool mc_all_on)
3227 {
3228 	struct sk_buff *skb;
3229 	struct wmi_mcast_filter_cmd *cmd;
3230 	int ret;
3231 
3232 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3233 	if (!skb)
3234 		return -ENOMEM;
3235 
3236 	cmd = (struct wmi_mcast_filter_cmd *) skb->data;
3237 	cmd->mcast_all_enable = mc_all_on;
3238 
3239 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_MCAST_FILTER_CMDID,
3240 				  NO_SYNC_WMIFLAG);
3241 	return ret;
3242 }
3243 
3244 int ath6kl_wmi_add_del_mcast_filter_cmd(struct wmi *wmi, u8 if_idx,
3245 					u8 *filter, bool add_filter)
3246 {
3247 	struct sk_buff *skb;
3248 	struct wmi_mcast_filter_add_del_cmd *cmd;
3249 	int ret;
3250 
3251 	if ((filter[0] != 0x33 || filter[1] != 0x33) &&
3252 	    (filter[0] != 0x01 || filter[1] != 0x00 ||
3253 	    filter[2] != 0x5e || filter[3] > 0x7f)) {
3254 		ath6kl_warn("invalid multicast filter address\n");
3255 		return -EINVAL;
3256 	}
3257 
3258 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3259 	if (!skb)
3260 		return -ENOMEM;
3261 
3262 	cmd = (struct wmi_mcast_filter_add_del_cmd *) skb->data;
3263 	memcpy(cmd->mcast_mac, filter, ATH6KL_MCAST_FILTER_MAC_ADDR_SIZE);
3264 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb,
3265 				  add_filter ? WMI_SET_MCAST_FILTER_CMDID :
3266 				  WMI_DEL_MCAST_FILTER_CMDID,
3267 				  NO_SYNC_WMIFLAG);
3268 
3269 	return ret;
3270 }
3271 
3272 int ath6kl_wmi_sta_bmiss_enhance_cmd(struct wmi *wmi, u8 if_idx, bool enhance)
3273 {
3274 	struct sk_buff *skb;
3275 	struct wmi_sta_bmiss_enhance_cmd *cmd;
3276 	int ret;
3277 
3278 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3279 	if (!skb)
3280 		return -ENOMEM;
3281 
3282 	cmd = (struct wmi_sta_bmiss_enhance_cmd *) skb->data;
3283 	cmd->enable = enhance ? 1 : 0;
3284 
3285 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb,
3286 				  WMI_STA_BMISS_ENHANCE_CMDID,
3287 				  NO_SYNC_WMIFLAG);
3288 	return ret;
3289 }
3290 
3291 int ath6kl_wmi_set_regdomain_cmd(struct wmi *wmi, const char *alpha2)
3292 {
3293 	struct sk_buff *skb;
3294 	struct wmi_set_regdomain_cmd *cmd;
3295 
3296 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3297 	if (!skb)
3298 		return -ENOMEM;
3299 
3300 	cmd = (struct wmi_set_regdomain_cmd *) skb->data;
3301 	memcpy(cmd->iso_name, alpha2, 2);
3302 
3303 	return ath6kl_wmi_cmd_send(wmi, 0, skb,
3304 				   WMI_SET_REGDOMAIN_CMDID,
3305 				   NO_SYNC_WMIFLAG);
3306 }
3307 
3308 s32 ath6kl_wmi_get_rate(struct wmi *wmi, s8 rate_index)
3309 {
3310 	struct ath6kl *ar = wmi->parent_dev;
3311 	u8 sgi = 0;
3312 	s32 ret;
3313 
3314 	if (rate_index == RATE_AUTO)
3315 		return 0;
3316 
3317 	/* SGI is stored as the MSB of the rate_index */
3318 	if (rate_index & RATE_INDEX_MSB) {
3319 		rate_index &= RATE_INDEX_WITHOUT_SGI_MASK;
3320 		sgi = 1;
3321 	}
3322 
3323 	if (test_bit(ATH6KL_FW_CAPABILITY_RATETABLE_MCS15,
3324 		     ar->fw_capabilities)) {
3325 		if (WARN_ON(rate_index >= ARRAY_SIZE(wmi_rate_tbl_mcs15)))
3326 			return 0;
3327 
3328 		ret = wmi_rate_tbl_mcs15[(u32) rate_index][sgi];
3329 	} else {
3330 		if (WARN_ON(rate_index >= ARRAY_SIZE(wmi_rate_tbl)))
3331 			return 0;
3332 
3333 		ret = wmi_rate_tbl[(u32) rate_index][sgi];
3334 	}
3335 
3336 	return ret;
3337 }
3338 
3339 static int ath6kl_wmi_get_pmkid_list_event_rx(struct wmi *wmi, u8 *datap,
3340 					      u32 len)
3341 {
3342 	struct wmi_pmkid_list_reply *reply;
3343 	u32 expected_len;
3344 
3345 	if (len < sizeof(struct wmi_pmkid_list_reply))
3346 		return -EINVAL;
3347 
3348 	reply = (struct wmi_pmkid_list_reply *)datap;
3349 	expected_len = sizeof(reply->num_pmkid) +
3350 		le32_to_cpu(reply->num_pmkid) * WMI_PMKID_LEN;
3351 
3352 	if (len < expected_len)
3353 		return -EINVAL;
3354 
3355 	return 0;
3356 }
3357 
3358 static int ath6kl_wmi_addba_req_event_rx(struct wmi *wmi, u8 *datap, int len,
3359 					 struct ath6kl_vif *vif)
3360 {
3361 	struct wmi_addba_req_event *cmd = (struct wmi_addba_req_event *) datap;
3362 
3363 	aggr_recv_addba_req_evt(vif, cmd->tid,
3364 				le16_to_cpu(cmd->st_seq_no), cmd->win_sz);
3365 
3366 	return 0;
3367 }
3368 
3369 static int ath6kl_wmi_delba_req_event_rx(struct wmi *wmi, u8 *datap, int len,
3370 					 struct ath6kl_vif *vif)
3371 {
3372 	struct wmi_delba_event *cmd = (struct wmi_delba_event *) datap;
3373 
3374 	aggr_recv_delba_req_evt(vif, cmd->tid);
3375 
3376 	return 0;
3377 }
3378 
3379 /*  AP mode functions */
3380 
3381 int ath6kl_wmi_ap_profile_commit(struct wmi *wmip, u8 if_idx,
3382 				 struct wmi_connect_cmd *p)
3383 {
3384 	struct sk_buff *skb;
3385 	struct wmi_connect_cmd *cm;
3386 	int res;
3387 
3388 	skb = ath6kl_wmi_get_new_buf(sizeof(*cm));
3389 	if (!skb)
3390 		return -ENOMEM;
3391 
3392 	cm = (struct wmi_connect_cmd *) skb->data;
3393 	memcpy(cm, p, sizeof(*cm));
3394 
3395 	res = ath6kl_wmi_cmd_send(wmip, if_idx, skb, WMI_AP_CONFIG_COMMIT_CMDID,
3396 				  NO_SYNC_WMIFLAG);
3397 	ath6kl_dbg(ATH6KL_DBG_WMI,
3398 		   "%s: nw_type=%u auth_mode=%u ch=%u ctrl_flags=0x%x-> res=%d\n",
3399 		   __func__, p->nw_type, p->auth_mode, le16_to_cpu(p->ch),
3400 		   le32_to_cpu(p->ctrl_flags), res);
3401 	return res;
3402 }
3403 
3404 int ath6kl_wmi_ap_set_mlme(struct wmi *wmip, u8 if_idx, u8 cmd, const u8 *mac,
3405 			   u16 reason)
3406 {
3407 	struct sk_buff *skb;
3408 	struct wmi_ap_set_mlme_cmd *cm;
3409 
3410 	skb = ath6kl_wmi_get_new_buf(sizeof(*cm));
3411 	if (!skb)
3412 		return -ENOMEM;
3413 
3414 	cm = (struct wmi_ap_set_mlme_cmd *) skb->data;
3415 	memcpy(cm->mac, mac, ETH_ALEN);
3416 	cm->reason = cpu_to_le16(reason);
3417 	cm->cmd = cmd;
3418 
3419 	ath6kl_dbg(ATH6KL_DBG_WMI, "ap_set_mlme: cmd=%d reason=%d\n", cm->cmd,
3420 		   cm->reason);
3421 
3422 	return ath6kl_wmi_cmd_send(wmip, if_idx, skb, WMI_AP_SET_MLME_CMDID,
3423 				   NO_SYNC_WMIFLAG);
3424 }
3425 
3426 int ath6kl_wmi_ap_hidden_ssid(struct wmi *wmi, u8 if_idx, bool enable)
3427 {
3428 	struct sk_buff *skb;
3429 	struct wmi_ap_hidden_ssid_cmd *cmd;
3430 
3431 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3432 	if (!skb)
3433 		return -ENOMEM;
3434 
3435 	cmd = (struct wmi_ap_hidden_ssid_cmd *) skb->data;
3436 	cmd->hidden_ssid = enable ? 1 : 0;
3437 
3438 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_AP_HIDDEN_SSID_CMDID,
3439 				   NO_SYNC_WMIFLAG);
3440 }
3441 
3442 /* This command will be used to enable/disable AP uAPSD feature */
3443 int ath6kl_wmi_ap_set_apsd(struct wmi *wmi, u8 if_idx, u8 enable)
3444 {
3445 	struct wmi_ap_set_apsd_cmd *cmd;
3446 	struct sk_buff *skb;
3447 
3448 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3449 	if (!skb)
3450 		return -ENOMEM;
3451 
3452 	cmd = (struct wmi_ap_set_apsd_cmd *)skb->data;
3453 	cmd->enable = enable;
3454 
3455 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_AP_SET_APSD_CMDID,
3456 				   NO_SYNC_WMIFLAG);
3457 }
3458 
3459 int ath6kl_wmi_set_apsd_bfrd_traf(struct wmi *wmi, u8 if_idx,
3460 					     u16 aid, u16 bitmap, u32 flags)
3461 {
3462 	struct wmi_ap_apsd_buffered_traffic_cmd *cmd;
3463 	struct sk_buff *skb;
3464 
3465 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3466 	if (!skb)
3467 		return -ENOMEM;
3468 
3469 	cmd = (struct wmi_ap_apsd_buffered_traffic_cmd *)skb->data;
3470 	cmd->aid = cpu_to_le16(aid);
3471 	cmd->bitmap = cpu_to_le16(bitmap);
3472 	cmd->flags = cpu_to_le32(flags);
3473 
3474 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb,
3475 				   WMI_AP_APSD_BUFFERED_TRAFFIC_CMDID,
3476 				   NO_SYNC_WMIFLAG);
3477 }
3478 
3479 static int ath6kl_wmi_pspoll_event_rx(struct wmi *wmi, u8 *datap, int len,
3480 				      struct ath6kl_vif *vif)
3481 {
3482 	struct wmi_pspoll_event *ev;
3483 
3484 	if (len < sizeof(struct wmi_pspoll_event))
3485 		return -EINVAL;
3486 
3487 	ev = (struct wmi_pspoll_event *) datap;
3488 
3489 	ath6kl_pspoll_event(vif, le16_to_cpu(ev->aid));
3490 
3491 	return 0;
3492 }
3493 
3494 static int ath6kl_wmi_dtimexpiry_event_rx(struct wmi *wmi, u8 *datap, int len,
3495 					  struct ath6kl_vif *vif)
3496 {
3497 	ath6kl_dtimexpiry_event(vif);
3498 
3499 	return 0;
3500 }
3501 
3502 int ath6kl_wmi_set_pvb_cmd(struct wmi *wmi, u8 if_idx, u16 aid,
3503 			   bool flag)
3504 {
3505 	struct sk_buff *skb;
3506 	struct wmi_ap_set_pvb_cmd *cmd;
3507 	int ret;
3508 
3509 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_ap_set_pvb_cmd));
3510 	if (!skb)
3511 		return -ENOMEM;
3512 
3513 	cmd = (struct wmi_ap_set_pvb_cmd *) skb->data;
3514 	cmd->aid = cpu_to_le16(aid);
3515 	cmd->rsvd = cpu_to_le16(0);
3516 	cmd->flag = cpu_to_le32(flag);
3517 
3518 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_AP_SET_PVB_CMDID,
3519 				  NO_SYNC_WMIFLAG);
3520 
3521 	return ret;
3522 }
3523 
3524 int ath6kl_wmi_set_rx_frame_format_cmd(struct wmi *wmi, u8 if_idx,
3525 				       u8 rx_meta_ver,
3526 				       bool rx_dot11_hdr, bool defrag_on_host)
3527 {
3528 	struct sk_buff *skb;
3529 	struct wmi_rx_frame_format_cmd *cmd;
3530 	int ret;
3531 
3532 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3533 	if (!skb)
3534 		return -ENOMEM;
3535 
3536 	cmd = (struct wmi_rx_frame_format_cmd *) skb->data;
3537 	cmd->dot11_hdr = rx_dot11_hdr ? 1 : 0;
3538 	cmd->defrag_on_host = defrag_on_host ? 1 : 0;
3539 	cmd->meta_ver = rx_meta_ver;
3540 
3541 	/* Delete the local aggr state, on host */
3542 	ret = ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_RX_FRAME_FORMAT_CMDID,
3543 				  NO_SYNC_WMIFLAG);
3544 
3545 	return ret;
3546 }
3547 
3548 int ath6kl_wmi_set_appie_cmd(struct wmi *wmi, u8 if_idx, u8 mgmt_frm_type,
3549 			     const u8 *ie, u8 ie_len)
3550 {
3551 	struct sk_buff *skb;
3552 	struct wmi_set_appie_cmd *p;
3553 
3554 	skb = ath6kl_wmi_get_new_buf(sizeof(*p) + ie_len);
3555 	if (!skb)
3556 		return -ENOMEM;
3557 
3558 	ath6kl_dbg(ATH6KL_DBG_WMI,
3559 		   "set_appie_cmd: mgmt_frm_type=%u ie_len=%u\n",
3560 		   mgmt_frm_type, ie_len);
3561 	p = (struct wmi_set_appie_cmd *) skb->data;
3562 	p->mgmt_frm_type = mgmt_frm_type;
3563 	p->ie_len = ie_len;
3564 
3565 	if (ie != NULL && ie_len > 0)
3566 		memcpy(p->ie_info, ie, ie_len);
3567 
3568 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_APPIE_CMDID,
3569 				   NO_SYNC_WMIFLAG);
3570 }
3571 
3572 int ath6kl_wmi_set_ie_cmd(struct wmi *wmi, u8 if_idx, u8 ie_id, u8 ie_field,
3573 			  const u8 *ie_info, u8 ie_len)
3574 {
3575 	struct sk_buff *skb;
3576 	struct wmi_set_ie_cmd *p;
3577 
3578 	skb = ath6kl_wmi_get_new_buf(sizeof(*p) + ie_len);
3579 	if (!skb)
3580 		return -ENOMEM;
3581 
3582 	ath6kl_dbg(ATH6KL_DBG_WMI, "set_ie_cmd: ie_id=%u ie_ie_field=%u ie_len=%u\n",
3583 		   ie_id, ie_field, ie_len);
3584 	p = (struct wmi_set_ie_cmd *) skb->data;
3585 	p->ie_id = ie_id;
3586 	p->ie_field = ie_field;
3587 	p->ie_len = ie_len;
3588 	if (ie_info && ie_len > 0)
3589 		memcpy(p->ie_info, ie_info, ie_len);
3590 
3591 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SET_IE_CMDID,
3592 				   NO_SYNC_WMIFLAG);
3593 }
3594 
3595 int ath6kl_wmi_disable_11b_rates_cmd(struct wmi *wmi, bool disable)
3596 {
3597 	struct sk_buff *skb;
3598 	struct wmi_disable_11b_rates_cmd *cmd;
3599 
3600 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3601 	if (!skb)
3602 		return -ENOMEM;
3603 
3604 	ath6kl_dbg(ATH6KL_DBG_WMI, "disable_11b_rates_cmd: disable=%u\n",
3605 		   disable);
3606 	cmd = (struct wmi_disable_11b_rates_cmd *) skb->data;
3607 	cmd->disable = disable ? 1 : 0;
3608 
3609 	return ath6kl_wmi_cmd_send(wmi, 0, skb, WMI_DISABLE_11B_RATES_CMDID,
3610 				   NO_SYNC_WMIFLAG);
3611 }
3612 
3613 int ath6kl_wmi_remain_on_chnl_cmd(struct wmi *wmi, u8 if_idx, u32 freq, u32 dur)
3614 {
3615 	struct sk_buff *skb;
3616 	struct wmi_remain_on_chnl_cmd *p;
3617 
3618 	skb = ath6kl_wmi_get_new_buf(sizeof(*p));
3619 	if (!skb)
3620 		return -ENOMEM;
3621 
3622 	ath6kl_dbg(ATH6KL_DBG_WMI, "remain_on_chnl_cmd: freq=%u dur=%u\n",
3623 		   freq, dur);
3624 	p = (struct wmi_remain_on_chnl_cmd *) skb->data;
3625 	p->freq = cpu_to_le32(freq);
3626 	p->duration = cpu_to_le32(dur);
3627 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_REMAIN_ON_CHNL_CMDID,
3628 				   NO_SYNC_WMIFLAG);
3629 }
3630 
3631 /* ath6kl_wmi_send_action_cmd is to be deprecated. Use
3632  * ath6kl_wmi_send_mgmt_cmd instead. The new function supports P2P
3633  * mgmt operations using station interface.
3634  */
3635 static int ath6kl_wmi_send_action_cmd(struct wmi *wmi, u8 if_idx, u32 id,
3636 				      u32 freq, u32 wait, const u8 *data,
3637 				      u16 data_len)
3638 {
3639 	struct sk_buff *skb;
3640 	struct wmi_send_action_cmd *p;
3641 	u8 *buf;
3642 
3643 	if (wait)
3644 		return -EINVAL; /* Offload for wait not supported */
3645 
3646 	buf = kmalloc(data_len, GFP_KERNEL);
3647 	if (!buf)
3648 		return -ENOMEM;
3649 
3650 	skb = ath6kl_wmi_get_new_buf(sizeof(*p) + data_len);
3651 	if (!skb) {
3652 		kfree(buf);
3653 		return -ENOMEM;
3654 	}
3655 
3656 	kfree(wmi->last_mgmt_tx_frame);
3657 	memcpy(buf, data, data_len);
3658 	wmi->last_mgmt_tx_frame = buf;
3659 	wmi->last_mgmt_tx_frame_len = data_len;
3660 
3661 	ath6kl_dbg(ATH6KL_DBG_WMI,
3662 		   "send_action_cmd: id=%u freq=%u wait=%u len=%u\n",
3663 		   id, freq, wait, data_len);
3664 	p = (struct wmi_send_action_cmd *) skb->data;
3665 	p->id = cpu_to_le32(id);
3666 	p->freq = cpu_to_le32(freq);
3667 	p->wait = cpu_to_le32(wait);
3668 	p->len = cpu_to_le16(data_len);
3669 	memcpy(p->data, data, data_len);
3670 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SEND_ACTION_CMDID,
3671 				   NO_SYNC_WMIFLAG);
3672 }
3673 
3674 static int __ath6kl_wmi_send_mgmt_cmd(struct wmi *wmi, u8 if_idx, u32 id,
3675 				      u32 freq, u32 wait, const u8 *data,
3676 				      u16 data_len, u32 no_cck)
3677 {
3678 	struct sk_buff *skb;
3679 	struct wmi_send_mgmt_cmd *p;
3680 	u8 *buf;
3681 
3682 	if (wait)
3683 		return -EINVAL; /* Offload for wait not supported */
3684 
3685 	buf = kmalloc(data_len, GFP_KERNEL);
3686 	if (!buf)
3687 		return -ENOMEM;
3688 
3689 	skb = ath6kl_wmi_get_new_buf(sizeof(*p) + data_len);
3690 	if (!skb) {
3691 		kfree(buf);
3692 		return -ENOMEM;
3693 	}
3694 
3695 	kfree(wmi->last_mgmt_tx_frame);
3696 	memcpy(buf, data, data_len);
3697 	wmi->last_mgmt_tx_frame = buf;
3698 	wmi->last_mgmt_tx_frame_len = data_len;
3699 
3700 	ath6kl_dbg(ATH6KL_DBG_WMI,
3701 		   "send_action_cmd: id=%u freq=%u wait=%u len=%u\n",
3702 		   id, freq, wait, data_len);
3703 	p = (struct wmi_send_mgmt_cmd *) skb->data;
3704 	p->id = cpu_to_le32(id);
3705 	p->freq = cpu_to_le32(freq);
3706 	p->wait = cpu_to_le32(wait);
3707 	p->no_cck = cpu_to_le32(no_cck);
3708 	p->len = cpu_to_le16(data_len);
3709 	memcpy(p->data, data, data_len);
3710 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_SEND_MGMT_CMDID,
3711 				   NO_SYNC_WMIFLAG);
3712 }
3713 
3714 int ath6kl_wmi_send_mgmt_cmd(struct wmi *wmi, u8 if_idx, u32 id, u32 freq,
3715 				u32 wait, const u8 *data, u16 data_len,
3716 				u32 no_cck)
3717 {
3718 	int status;
3719 	struct ath6kl *ar = wmi->parent_dev;
3720 
3721 	if (test_bit(ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX,
3722 		     ar->fw_capabilities)) {
3723 		/*
3724 		 * If capable of doing P2P mgmt operations using
3725 		 * station interface, send additional information like
3726 		 * supported rates to advertise and xmit rates for
3727 		 * probe requests
3728 		 */
3729 		status = __ath6kl_wmi_send_mgmt_cmd(ar->wmi, if_idx, id, freq,
3730 						    wait, data, data_len,
3731 						    no_cck);
3732 	} else {
3733 		status = ath6kl_wmi_send_action_cmd(ar->wmi, if_idx, id, freq,
3734 						    wait, data, data_len);
3735 	}
3736 
3737 	return status;
3738 }
3739 
3740 int ath6kl_wmi_send_probe_response_cmd(struct wmi *wmi, u8 if_idx, u32 freq,
3741 				       const u8 *dst, const u8 *data,
3742 				       u16 data_len)
3743 {
3744 	struct sk_buff *skb;
3745 	struct wmi_p2p_probe_response_cmd *p;
3746 	size_t cmd_len = sizeof(*p) + data_len;
3747 
3748 	if (data_len == 0)
3749 		cmd_len++; /* work around target minimum length requirement */
3750 
3751 	skb = ath6kl_wmi_get_new_buf(cmd_len);
3752 	if (!skb)
3753 		return -ENOMEM;
3754 
3755 	ath6kl_dbg(ATH6KL_DBG_WMI,
3756 		   "send_probe_response_cmd: freq=%u dst=%pM len=%u\n",
3757 		   freq, dst, data_len);
3758 	p = (struct wmi_p2p_probe_response_cmd *) skb->data;
3759 	p->freq = cpu_to_le32(freq);
3760 	memcpy(p->destination_addr, dst, ETH_ALEN);
3761 	p->len = cpu_to_le16(data_len);
3762 	memcpy(p->data, data, data_len);
3763 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb,
3764 				   WMI_SEND_PROBE_RESPONSE_CMDID,
3765 				   NO_SYNC_WMIFLAG);
3766 }
3767 
3768 int ath6kl_wmi_probe_report_req_cmd(struct wmi *wmi, u8 if_idx, bool enable)
3769 {
3770 	struct sk_buff *skb;
3771 	struct wmi_probe_req_report_cmd *p;
3772 
3773 	skb = ath6kl_wmi_get_new_buf(sizeof(*p));
3774 	if (!skb)
3775 		return -ENOMEM;
3776 
3777 	ath6kl_dbg(ATH6KL_DBG_WMI, "probe_report_req_cmd: enable=%u\n",
3778 		   enable);
3779 	p = (struct wmi_probe_req_report_cmd *) skb->data;
3780 	p->enable = enable ? 1 : 0;
3781 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_PROBE_REQ_REPORT_CMDID,
3782 				   NO_SYNC_WMIFLAG);
3783 }
3784 
3785 int ath6kl_wmi_info_req_cmd(struct wmi *wmi, u8 if_idx, u32 info_req_flags)
3786 {
3787 	struct sk_buff *skb;
3788 	struct wmi_get_p2p_info *p;
3789 
3790 	skb = ath6kl_wmi_get_new_buf(sizeof(*p));
3791 	if (!skb)
3792 		return -ENOMEM;
3793 
3794 	ath6kl_dbg(ATH6KL_DBG_WMI, "info_req_cmd: flags=%x\n",
3795 		   info_req_flags);
3796 	p = (struct wmi_get_p2p_info *) skb->data;
3797 	p->info_req_flags = cpu_to_le32(info_req_flags);
3798 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_GET_P2P_INFO_CMDID,
3799 				   NO_SYNC_WMIFLAG);
3800 }
3801 
3802 int ath6kl_wmi_cancel_remain_on_chnl_cmd(struct wmi *wmi, u8 if_idx)
3803 {
3804 	ath6kl_dbg(ATH6KL_DBG_WMI, "cancel_remain_on_chnl_cmd\n");
3805 	return ath6kl_wmi_simple_cmd(wmi, if_idx,
3806 				     WMI_CANCEL_REMAIN_ON_CHNL_CMDID);
3807 }
3808 
3809 int ath6kl_wmi_set_inact_period(struct wmi *wmi, u8 if_idx, int inact_timeout)
3810 {
3811 	struct sk_buff *skb;
3812 	struct wmi_set_inact_period_cmd *cmd;
3813 
3814 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
3815 	if (!skb)
3816 		return -ENOMEM;
3817 
3818 	cmd = (struct wmi_set_inact_period_cmd *) skb->data;
3819 	cmd->inact_period = cpu_to_le32(inact_timeout);
3820 	cmd->num_null_func = 0;
3821 
3822 	return ath6kl_wmi_cmd_send(wmi, if_idx, skb, WMI_AP_CONN_INACT_CMDID,
3823 				   NO_SYNC_WMIFLAG);
3824 }
3825 
3826 static void ath6kl_wmi_hb_challenge_resp_event(struct wmi *wmi, u8 *datap,
3827 					       int len)
3828 {
3829 	struct wmix_hb_challenge_resp_cmd *cmd;
3830 
3831 	if (len < sizeof(struct wmix_hb_challenge_resp_cmd))
3832 		return;
3833 
3834 	cmd = (struct wmix_hb_challenge_resp_cmd *) datap;
3835 	ath6kl_recovery_hb_event(wmi->parent_dev,
3836 				 le32_to_cpu(cmd->cookie));
3837 }
3838 
3839 static int ath6kl_wmi_control_rx_xtnd(struct wmi *wmi, struct sk_buff *skb)
3840 {
3841 	struct wmix_cmd_hdr *cmd;
3842 	u32 len;
3843 	u16 id;
3844 	u8 *datap;
3845 	int ret = 0;
3846 
3847 	if (skb->len < sizeof(struct wmix_cmd_hdr)) {
3848 		ath6kl_err("bad packet 1\n");
3849 		return -EINVAL;
3850 	}
3851 
3852 	cmd = (struct wmix_cmd_hdr *) skb->data;
3853 	id = le32_to_cpu(cmd->cmd_id);
3854 
3855 	skb_pull(skb, sizeof(struct wmix_cmd_hdr));
3856 
3857 	datap = skb->data;
3858 	len = skb->len;
3859 
3860 	switch (id) {
3861 	case WMIX_HB_CHALLENGE_RESP_EVENTID:
3862 		ath6kl_dbg(ATH6KL_DBG_WMI, "wmi event hb challenge resp\n");
3863 		ath6kl_wmi_hb_challenge_resp_event(wmi, datap, len);
3864 		break;
3865 	case WMIX_DBGLOG_EVENTID:
3866 		ath6kl_dbg(ATH6KL_DBG_WMI, "wmi event dbglog len %d\n", len);
3867 		ath6kl_debug_fwlog_event(wmi->parent_dev, datap, len);
3868 		break;
3869 	default:
3870 		ath6kl_warn("unknown cmd id 0x%x\n", id);
3871 		ret = -EINVAL;
3872 		break;
3873 	}
3874 
3875 	return ret;
3876 }
3877 
3878 static int ath6kl_wmi_roam_tbl_event_rx(struct wmi *wmi, u8 *datap, int len)
3879 {
3880 	return ath6kl_debug_roam_tbl_event(wmi->parent_dev, datap, len);
3881 }
3882 
3883 /* Process interface specific wmi events, caller would free the datap */
3884 static int ath6kl_wmi_proc_events_vif(struct wmi *wmi, u16 if_idx, u16 cmd_id,
3885 					u8 *datap, u32 len)
3886 {
3887 	struct ath6kl_vif *vif;
3888 
3889 	vif = ath6kl_get_vif_by_index(wmi->parent_dev, if_idx);
3890 	if (!vif) {
3891 		ath6kl_dbg(ATH6KL_DBG_WMI,
3892 			   "Wmi event for unavailable vif, vif_index:%d\n",
3893 			    if_idx);
3894 		return -EINVAL;
3895 	}
3896 
3897 	switch (cmd_id) {
3898 	case WMI_CONNECT_EVENTID:
3899 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_CONNECT_EVENTID\n");
3900 		return ath6kl_wmi_connect_event_rx(wmi, datap, len, vif);
3901 	case WMI_DISCONNECT_EVENTID:
3902 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_DISCONNECT_EVENTID\n");
3903 		return ath6kl_wmi_disconnect_event_rx(wmi, datap, len, vif);
3904 	case WMI_TKIP_MICERR_EVENTID:
3905 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TKIP_MICERR_EVENTID\n");
3906 		return ath6kl_wmi_tkip_micerr_event_rx(wmi, datap, len, vif);
3907 	case WMI_BSSINFO_EVENTID:
3908 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_BSSINFO_EVENTID\n");
3909 		return ath6kl_wmi_bssinfo_event_rx(wmi, datap, len, vif);
3910 	case WMI_NEIGHBOR_REPORT_EVENTID:
3911 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_NEIGHBOR_REPORT_EVENTID\n");
3912 		return ath6kl_wmi_neighbor_report_event_rx(wmi, datap, len,
3913 							   vif);
3914 	case WMI_SCAN_COMPLETE_EVENTID:
3915 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_SCAN_COMPLETE_EVENTID\n");
3916 		return ath6kl_wmi_scan_complete_rx(wmi, datap, len, vif);
3917 	case WMI_REPORT_STATISTICS_EVENTID:
3918 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REPORT_STATISTICS_EVENTID\n");
3919 		return ath6kl_wmi_stats_event_rx(wmi, datap, len, vif);
3920 	case WMI_CAC_EVENTID:
3921 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_CAC_EVENTID\n");
3922 		return ath6kl_wmi_cac_event_rx(wmi, datap, len, vif);
3923 	case WMI_PSPOLL_EVENTID:
3924 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_PSPOLL_EVENTID\n");
3925 		return ath6kl_wmi_pspoll_event_rx(wmi, datap, len, vif);
3926 	case WMI_DTIMEXPIRY_EVENTID:
3927 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_DTIMEXPIRY_EVENTID\n");
3928 		return ath6kl_wmi_dtimexpiry_event_rx(wmi, datap, len, vif);
3929 	case WMI_ADDBA_REQ_EVENTID:
3930 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_ADDBA_REQ_EVENTID\n");
3931 		return ath6kl_wmi_addba_req_event_rx(wmi, datap, len, vif);
3932 	case WMI_DELBA_REQ_EVENTID:
3933 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_DELBA_REQ_EVENTID\n");
3934 		return ath6kl_wmi_delba_req_event_rx(wmi, datap, len, vif);
3935 	case WMI_SET_HOST_SLEEP_MODE_CMD_PROCESSED_EVENTID:
3936 		ath6kl_dbg(ATH6KL_DBG_WMI,
3937 			   "WMI_SET_HOST_SLEEP_MODE_CMD_PROCESSED_EVENTID");
3938 		return ath6kl_wmi_host_sleep_mode_cmd_prcd_evt_rx(wmi, vif);
3939 	case WMI_REMAIN_ON_CHNL_EVENTID:
3940 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REMAIN_ON_CHNL_EVENTID\n");
3941 		return ath6kl_wmi_remain_on_chnl_event_rx(wmi, datap, len, vif);
3942 	case WMI_CANCEL_REMAIN_ON_CHNL_EVENTID:
3943 		ath6kl_dbg(ATH6KL_DBG_WMI,
3944 			   "WMI_CANCEL_REMAIN_ON_CHNL_EVENTID\n");
3945 		return ath6kl_wmi_cancel_remain_on_chnl_event_rx(wmi, datap,
3946 								 len, vif);
3947 	case WMI_TX_STATUS_EVENTID:
3948 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TX_STATUS_EVENTID\n");
3949 		return ath6kl_wmi_tx_status_event_rx(wmi, datap, len, vif);
3950 	case WMI_RX_PROBE_REQ_EVENTID:
3951 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_RX_PROBE_REQ_EVENTID\n");
3952 		return ath6kl_wmi_rx_probe_req_event_rx(wmi, datap, len, vif);
3953 	case WMI_RX_ACTION_EVENTID:
3954 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_RX_ACTION_EVENTID\n");
3955 		return ath6kl_wmi_rx_action_event_rx(wmi, datap, len, vif);
3956 	case WMI_TXE_NOTIFY_EVENTID:
3957 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TXE_NOTIFY_EVENTID\n");
3958 		return ath6kl_wmi_txe_notify_event_rx(wmi, datap, len, vif);
3959 	default:
3960 		ath6kl_dbg(ATH6KL_DBG_WMI, "unknown cmd id 0x%x\n", cmd_id);
3961 		return -EINVAL;
3962 	}
3963 
3964 	return 0;
3965 }
3966 
3967 static int ath6kl_wmi_proc_events(struct wmi *wmi, struct sk_buff *skb)
3968 {
3969 	struct wmi_cmd_hdr *cmd;
3970 	int ret = 0;
3971 	u32 len;
3972 	u16 id;
3973 	u8 if_idx;
3974 	u8 *datap;
3975 
3976 	cmd = (struct wmi_cmd_hdr *) skb->data;
3977 	id = le16_to_cpu(cmd->cmd_id);
3978 	if_idx = le16_to_cpu(cmd->info1) & WMI_CMD_HDR_IF_ID_MASK;
3979 
3980 	skb_pull(skb, sizeof(struct wmi_cmd_hdr));
3981 	datap = skb->data;
3982 	len = skb->len;
3983 
3984 	ath6kl_dbg(ATH6KL_DBG_WMI, "wmi rx id %d len %d\n", id, len);
3985 	ath6kl_dbg_dump(ATH6KL_DBG_WMI_DUMP, NULL, "wmi rx ",
3986 			datap, len);
3987 
3988 	switch (id) {
3989 	case WMI_GET_BITRATE_CMDID:
3990 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_BITRATE_CMDID\n");
3991 		ret = ath6kl_wmi_bitrate_reply_rx(wmi, datap, len);
3992 		break;
3993 	case WMI_GET_CHANNEL_LIST_CMDID:
3994 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_CHANNEL_LIST_CMDID\n");
3995 		ret = ath6kl_wmi_ch_list_reply_rx(wmi, datap, len);
3996 		break;
3997 	case WMI_GET_TX_PWR_CMDID:
3998 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_TX_PWR_CMDID\n");
3999 		ret = ath6kl_wmi_tx_pwr_reply_rx(wmi, datap, len);
4000 		break;
4001 	case WMI_READY_EVENTID:
4002 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_READY_EVENTID\n");
4003 		ret = ath6kl_wmi_ready_event_rx(wmi, datap, len);
4004 		break;
4005 	case WMI_PEER_NODE_EVENTID:
4006 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_PEER_NODE_EVENTID\n");
4007 		ret = ath6kl_wmi_peer_node_event_rx(wmi, datap, len);
4008 		break;
4009 	case WMI_REGDOMAIN_EVENTID:
4010 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REGDOMAIN_EVENTID\n");
4011 		ath6kl_wmi_regdomain_event(wmi, datap, len);
4012 		break;
4013 	case WMI_PSTREAM_TIMEOUT_EVENTID:
4014 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_PSTREAM_TIMEOUT_EVENTID\n");
4015 		ret = ath6kl_wmi_pstream_timeout_event_rx(wmi, datap, len);
4016 		break;
4017 	case WMI_CMDERROR_EVENTID:
4018 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_CMDERROR_EVENTID\n");
4019 		ret = ath6kl_wmi_error_event_rx(wmi, datap, len);
4020 		break;
4021 	case WMI_RSSI_THRESHOLD_EVENTID:
4022 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_RSSI_THRESHOLD_EVENTID\n");
4023 		ret = ath6kl_wmi_rssi_threshold_event_rx(wmi, datap, len);
4024 		break;
4025 	case WMI_ERROR_REPORT_EVENTID:
4026 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_ERROR_REPORT_EVENTID\n");
4027 		break;
4028 	case WMI_OPT_RX_FRAME_EVENTID:
4029 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_OPT_RX_FRAME_EVENTID\n");
4030 		/* this event has been deprecated */
4031 		break;
4032 	case WMI_REPORT_ROAM_TBL_EVENTID:
4033 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REPORT_ROAM_TBL_EVENTID\n");
4034 		ret = ath6kl_wmi_roam_tbl_event_rx(wmi, datap, len);
4035 		break;
4036 	case WMI_EXTENSION_EVENTID:
4037 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_EXTENSION_EVENTID\n");
4038 		ret = ath6kl_wmi_control_rx_xtnd(wmi, skb);
4039 		break;
4040 	case WMI_CHANNEL_CHANGE_EVENTID:
4041 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_CHANNEL_CHANGE_EVENTID\n");
4042 		break;
4043 	case WMI_REPORT_ROAM_DATA_EVENTID:
4044 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REPORT_ROAM_DATA_EVENTID\n");
4045 		break;
4046 	case WMI_TEST_EVENTID:
4047 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TEST_EVENTID\n");
4048 		ret = ath6kl_wmi_test_rx(wmi, datap, len);
4049 		break;
4050 	case WMI_GET_FIXRATES_CMDID:
4051 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_FIXRATES_CMDID\n");
4052 		ret = ath6kl_wmi_ratemask_reply_rx(wmi, datap, len);
4053 		break;
4054 	case WMI_TX_RETRY_ERR_EVENTID:
4055 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TX_RETRY_ERR_EVENTID\n");
4056 		break;
4057 	case WMI_SNR_THRESHOLD_EVENTID:
4058 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_SNR_THRESHOLD_EVENTID\n");
4059 		ret = ath6kl_wmi_snr_threshold_event_rx(wmi, datap, len);
4060 		break;
4061 	case WMI_LQ_THRESHOLD_EVENTID:
4062 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_LQ_THRESHOLD_EVENTID\n");
4063 		break;
4064 	case WMI_APLIST_EVENTID:
4065 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_APLIST_EVENTID\n");
4066 		ret = ath6kl_wmi_aplist_event_rx(wmi, datap, len);
4067 		break;
4068 	case WMI_GET_KEEPALIVE_CMDID:
4069 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_KEEPALIVE_CMDID\n");
4070 		ret = ath6kl_wmi_keepalive_reply_rx(wmi, datap, len);
4071 		break;
4072 	case WMI_GET_WOW_LIST_EVENTID:
4073 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_WOW_LIST_EVENTID\n");
4074 		break;
4075 	case WMI_GET_PMKID_LIST_EVENTID:
4076 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_PMKID_LIST_EVENTID\n");
4077 		ret = ath6kl_wmi_get_pmkid_list_event_rx(wmi, datap, len);
4078 		break;
4079 	case WMI_SET_PARAMS_REPLY_EVENTID:
4080 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_SET_PARAMS_REPLY_EVENTID\n");
4081 		break;
4082 	case WMI_ADDBA_RESP_EVENTID:
4083 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_ADDBA_RESP_EVENTID\n");
4084 		break;
4085 	case WMI_REPORT_BTCOEX_CONFIG_EVENTID:
4086 		ath6kl_dbg(ATH6KL_DBG_WMI,
4087 			   "WMI_REPORT_BTCOEX_CONFIG_EVENTID\n");
4088 		break;
4089 	case WMI_REPORT_BTCOEX_STATS_EVENTID:
4090 		ath6kl_dbg(ATH6KL_DBG_WMI,
4091 			   "WMI_REPORT_BTCOEX_STATS_EVENTID\n");
4092 		break;
4093 	case WMI_TX_COMPLETE_EVENTID:
4094 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TX_COMPLETE_EVENTID\n");
4095 		ret = ath6kl_wmi_tx_complete_event_rx(datap, len);
4096 		break;
4097 	case WMI_P2P_CAPABILITIES_EVENTID:
4098 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_P2P_CAPABILITIES_EVENTID\n");
4099 		ret = ath6kl_wmi_p2p_capabilities_event_rx(datap, len);
4100 		break;
4101 	case WMI_P2P_INFO_EVENTID:
4102 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_P2P_INFO_EVENTID\n");
4103 		ret = ath6kl_wmi_p2p_info_event_rx(datap, len);
4104 		break;
4105 	default:
4106 		/* may be the event is interface specific */
4107 		ret = ath6kl_wmi_proc_events_vif(wmi, if_idx, id, datap, len);
4108 		break;
4109 	}
4110 
4111 	dev_kfree_skb(skb);
4112 	return ret;
4113 }
4114 
4115 /* Control Path */
4116 int ath6kl_wmi_control_rx(struct wmi *wmi, struct sk_buff *skb)
4117 {
4118 	if (WARN_ON(skb == NULL))
4119 		return -EINVAL;
4120 
4121 	if (skb->len < sizeof(struct wmi_cmd_hdr)) {
4122 		ath6kl_err("bad packet 1\n");
4123 		dev_kfree_skb(skb);
4124 		return -EINVAL;
4125 	}
4126 
4127 	trace_ath6kl_wmi_event(skb->data, skb->len);
4128 
4129 	return ath6kl_wmi_proc_events(wmi, skb);
4130 }
4131 
4132 void ath6kl_wmi_reset(struct wmi *wmi)
4133 {
4134 	spin_lock_bh(&wmi->lock);
4135 
4136 	wmi->fat_pipe_exist = 0;
4137 	memset(wmi->stream_exist_for_ac, 0, sizeof(wmi->stream_exist_for_ac));
4138 
4139 	spin_unlock_bh(&wmi->lock);
4140 }
4141 
4142 void *ath6kl_wmi_init(struct ath6kl *dev)
4143 {
4144 	struct wmi *wmi;
4145 
4146 	wmi = kzalloc(sizeof(struct wmi), GFP_KERNEL);
4147 	if (!wmi)
4148 		return NULL;
4149 
4150 	spin_lock_init(&wmi->lock);
4151 
4152 	wmi->parent_dev = dev;
4153 
4154 	wmi->pwr_mode = REC_POWER;
4155 
4156 	ath6kl_wmi_reset(wmi);
4157 
4158 	return wmi;
4159 }
4160 
4161 void ath6kl_wmi_shutdown(struct wmi *wmi)
4162 {
4163 	if (!wmi)
4164 		return;
4165 
4166 	kfree(wmi->last_mgmt_tx_frame);
4167 	kfree(wmi);
4168 }
4169