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