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