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