xref: /openbmc/linux/drivers/net/wireless/ath/ath6kl/wmi.c (revision 32c1087460626f9cfa2b397eafd247bf039bacac)
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/ip.h>
18 #include "core.h"
19 #include "debug.h"
20 #include "testmode.h"
21 #include "../regd.h"
22 #include "../regd_common.h"
23 
24 static int ath6kl_wmi_sync_point(struct wmi *wmi);
25 
26 static const s32 wmi_rate_tbl[][2] = {
27 	/* {W/O SGI, with SGI} */
28 	{1000, 1000},
29 	{2000, 2000},
30 	{5500, 5500},
31 	{11000, 11000},
32 	{6000, 6000},
33 	{9000, 9000},
34 	{12000, 12000},
35 	{18000, 18000},
36 	{24000, 24000},
37 	{36000, 36000},
38 	{48000, 48000},
39 	{54000, 54000},
40 	{6500, 7200},
41 	{13000, 14400},
42 	{19500, 21700},
43 	{26000, 28900},
44 	{39000, 43300},
45 	{52000, 57800},
46 	{58500, 65000},
47 	{65000, 72200},
48 	{13500, 15000},
49 	{27000, 30000},
50 	{40500, 45000},
51 	{54000, 60000},
52 	{81000, 90000},
53 	{108000, 120000},
54 	{121500, 135000},
55 	{135000, 150000},
56 	{0, 0}
57 };
58 
59 /* 802.1d to AC mapping. Refer pg 57 of WMM-test-plan-v1.2 */
60 static const u8 up_to_ac[] = {
61 	WMM_AC_BE,
62 	WMM_AC_BK,
63 	WMM_AC_BK,
64 	WMM_AC_BE,
65 	WMM_AC_VI,
66 	WMM_AC_VI,
67 	WMM_AC_VO,
68 	WMM_AC_VO,
69 };
70 
71 void ath6kl_wmi_set_control_ep(struct wmi *wmi, enum htc_endpoint_id ep_id)
72 {
73 	if (WARN_ON(ep_id == ENDPOINT_UNUSED || ep_id >= ENDPOINT_MAX))
74 		return;
75 
76 	wmi->ep_id = ep_id;
77 }
78 
79 enum htc_endpoint_id ath6kl_wmi_get_control_ep(struct wmi *wmi)
80 {
81 	return wmi->ep_id;
82 }
83 
84 /*  Performs DIX to 802.3 encapsulation for transmit packets.
85  *  Assumes the entire DIX header is contigous and that there is
86  *  enough room in the buffer for a 802.3 mac header and LLC+SNAP headers.
87  */
88 int ath6kl_wmi_dix_2_dot3(struct wmi *wmi, struct sk_buff *skb)
89 {
90 	struct ath6kl_llc_snap_hdr *llc_hdr;
91 	struct ethhdr *eth_hdr;
92 	size_t new_len;
93 	__be16 type;
94 	u8 *datap;
95 	u16 size;
96 
97 	if (WARN_ON(skb == NULL))
98 		return -EINVAL;
99 
100 	size = sizeof(struct ath6kl_llc_snap_hdr) + sizeof(struct wmi_data_hdr);
101 	if (skb_headroom(skb) < size)
102 		return -ENOMEM;
103 
104 	eth_hdr = (struct ethhdr *) skb->data;
105 	type = eth_hdr->h_proto;
106 
107 	if (!is_ethertype(be16_to_cpu(type))) {
108 		ath6kl_dbg(ATH6KL_DBG_WMI,
109 			"%s: pkt is already in 802.3 format\n", __func__);
110 		return 0;
111 	}
112 
113 	new_len = skb->len - sizeof(*eth_hdr) + sizeof(*llc_hdr);
114 
115 	skb_push(skb, sizeof(struct ath6kl_llc_snap_hdr));
116 	datap = skb->data;
117 
118 	eth_hdr->h_proto = cpu_to_be16(new_len);
119 
120 	memcpy(datap, eth_hdr, sizeof(*eth_hdr));
121 
122 	llc_hdr = (struct ath6kl_llc_snap_hdr *)(datap + sizeof(*eth_hdr));
123 	llc_hdr->dsap = 0xAA;
124 	llc_hdr->ssap = 0xAA;
125 	llc_hdr->cntl = 0x03;
126 	llc_hdr->org_code[0] = 0x0;
127 	llc_hdr->org_code[1] = 0x0;
128 	llc_hdr->org_code[2] = 0x0;
129 	llc_hdr->eth_type = type;
130 
131 	return 0;
132 }
133 
134 static int ath6kl_wmi_meta_add(struct wmi *wmi, struct sk_buff *skb,
135 			       u8 *version, void *tx_meta_info)
136 {
137 	struct wmi_tx_meta_v1 *v1;
138 	struct wmi_tx_meta_v2 *v2;
139 
140 	if (WARN_ON(skb == NULL || version == NULL))
141 		return -EINVAL;
142 
143 	switch (*version) {
144 	case WMI_META_VERSION_1:
145 		skb_push(skb, WMI_MAX_TX_META_SZ);
146 		v1 = (struct wmi_tx_meta_v1 *) skb->data;
147 		v1->pkt_id = 0;
148 		v1->rate_plcy_id = 0;
149 		*version = WMI_META_VERSION_1;
150 		break;
151 	case WMI_META_VERSION_2:
152 		skb_push(skb, WMI_MAX_TX_META_SZ);
153 		v2 = (struct wmi_tx_meta_v2 *) skb->data;
154 		memcpy(v2, (struct wmi_tx_meta_v2 *) tx_meta_info,
155 		       sizeof(struct wmi_tx_meta_v2));
156 		break;
157 	}
158 
159 	return 0;
160 }
161 
162 int ath6kl_wmi_data_hdr_add(struct wmi *wmi, struct sk_buff *skb,
163 			    u8 msg_type, bool more_data,
164 			    enum wmi_data_hdr_data_type data_type,
165 			    u8 meta_ver, void *tx_meta_info)
166 {
167 	struct wmi_data_hdr *data_hdr;
168 	int ret;
169 
170 	if (WARN_ON(skb == NULL))
171 		return -EINVAL;
172 
173 	if (tx_meta_info) {
174 		ret = ath6kl_wmi_meta_add(wmi, skb, &meta_ver, tx_meta_info);
175 		if (ret)
176 			return ret;
177 	}
178 
179 	skb_push(skb, sizeof(struct wmi_data_hdr));
180 
181 	data_hdr = (struct wmi_data_hdr *)skb->data;
182 	memset(data_hdr, 0, sizeof(struct wmi_data_hdr));
183 
184 	data_hdr->info = msg_type << WMI_DATA_HDR_MSG_TYPE_SHIFT;
185 	data_hdr->info |= data_type << WMI_DATA_HDR_DATA_TYPE_SHIFT;
186 
187 	if (more_data)
188 		data_hdr->info |=
189 		    WMI_DATA_HDR_MORE_MASK << WMI_DATA_HDR_MORE_SHIFT;
190 
191 	data_hdr->info2 = cpu_to_le16(meta_ver << WMI_DATA_HDR_META_SHIFT);
192 	data_hdr->info3 = 0;
193 
194 	return 0;
195 }
196 
197 static u8 ath6kl_wmi_determine_user_priority(u8 *pkt, u32 layer2_pri)
198 {
199 	struct iphdr *ip_hdr = (struct iphdr *) pkt;
200 	u8 ip_pri;
201 
202 	/*
203 	 * Determine IPTOS priority
204 	 *
205 	 * IP-TOS - 8bits
206 	 *          : DSCP(6-bits) ECN(2-bits)
207 	 *          : DSCP - P2 P1 P0 X X X
208 	 * where (P2 P1 P0) form 802.1D
209 	 */
210 	ip_pri = ip_hdr->tos >> 5;
211 	ip_pri &= 0x7;
212 
213 	if ((layer2_pri & 0x7) > ip_pri)
214 		return (u8) layer2_pri & 0x7;
215 	else
216 		return ip_pri;
217 }
218 
219 int ath6kl_wmi_implicit_create_pstream(struct wmi *wmi, struct sk_buff *skb,
220 				       u32 layer2_priority, bool wmm_enabled,
221 				       u8 *ac)
222 {
223 	struct wmi_data_hdr *data_hdr;
224 	struct ath6kl_llc_snap_hdr *llc_hdr;
225 	struct wmi_create_pstream_cmd cmd;
226 	u32 meta_size, hdr_size;
227 	u16 ip_type = IP_ETHERTYPE;
228 	u8 stream_exist, usr_pri;
229 	u8 traffic_class = WMM_AC_BE;
230 	u8 *datap;
231 
232 	if (WARN_ON(skb == NULL))
233 		return -EINVAL;
234 
235 	datap = skb->data;
236 	data_hdr = (struct wmi_data_hdr *) datap;
237 
238 	meta_size = ((le16_to_cpu(data_hdr->info2) >> WMI_DATA_HDR_META_SHIFT) &
239 		     WMI_DATA_HDR_META_MASK) ? WMI_MAX_TX_META_SZ : 0;
240 
241 	if (!wmm_enabled) {
242 		/* If WMM is disabled all traffic goes as BE traffic */
243 		usr_pri = 0;
244 	} else {
245 		hdr_size = sizeof(struct ethhdr);
246 
247 		llc_hdr = (struct ath6kl_llc_snap_hdr *)(datap +
248 							 sizeof(struct
249 								wmi_data_hdr) +
250 							 meta_size + hdr_size);
251 
252 		if (llc_hdr->eth_type == htons(ip_type)) {
253 			/*
254 			 * Extract the endpoint info from the TOS field
255 			 * in the IP header.
256 			 */
257 			usr_pri =
258 			   ath6kl_wmi_determine_user_priority(((u8 *) llc_hdr) +
259 					sizeof(struct ath6kl_llc_snap_hdr),
260 					layer2_priority);
261 		} else
262 			usr_pri = layer2_priority & 0x7;
263 	}
264 
265 	/* workaround for WMM S5 */
266 	if ((wmi->traffic_class == WMM_AC_VI) &&
267 	    ((usr_pri == 5) || (usr_pri == 4)))
268 		usr_pri = 1;
269 
270 	/* Convert user priority to traffic class */
271 	traffic_class = up_to_ac[usr_pri & 0x7];
272 
273 	wmi_data_hdr_set_up(data_hdr, usr_pri);
274 
275 	spin_lock_bh(&wmi->lock);
276 	stream_exist = wmi->fat_pipe_exist;
277 	spin_unlock_bh(&wmi->lock);
278 
279 	if (!(stream_exist & (1 << traffic_class))) {
280 		memset(&cmd, 0, sizeof(cmd));
281 		cmd.traffic_class = traffic_class;
282 		cmd.user_pri = usr_pri;
283 		cmd.inactivity_int =
284 			cpu_to_le32(WMI_IMPLICIT_PSTREAM_INACTIVITY_INT);
285 		/* Implicit streams are created with TSID 0xFF */
286 		cmd.tsid = WMI_IMPLICIT_PSTREAM;
287 		ath6kl_wmi_create_pstream_cmd(wmi, &cmd);
288 	}
289 
290 	*ac = traffic_class;
291 
292 	return 0;
293 }
294 
295 int ath6kl_wmi_dot11_hdr_remove(struct wmi *wmi, struct sk_buff *skb)
296 {
297 	struct ieee80211_hdr_3addr *pwh, wh;
298 	struct ath6kl_llc_snap_hdr *llc_hdr;
299 	struct ethhdr eth_hdr;
300 	u32 hdr_size;
301 	u8 *datap;
302 	__le16 sub_type;
303 
304 	if (WARN_ON(skb == NULL))
305 		return -EINVAL;
306 
307 	datap = skb->data;
308 	pwh = (struct ieee80211_hdr_3addr *) datap;
309 
310 	sub_type = pwh->frame_control & cpu_to_le16(IEEE80211_FCTL_STYPE);
311 
312 	memcpy((u8 *) &wh, datap, sizeof(struct ieee80211_hdr_3addr));
313 
314 	/* Strip off the 802.11 header */
315 	if (sub_type == cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
316 		hdr_size = roundup(sizeof(struct ieee80211_qos_hdr),
317 				   sizeof(u32));
318 		skb_pull(skb, hdr_size);
319 	} else if (sub_type == cpu_to_le16(IEEE80211_STYPE_DATA))
320 		skb_pull(skb, sizeof(struct ieee80211_hdr_3addr));
321 
322 	datap = skb->data;
323 	llc_hdr = (struct ath6kl_llc_snap_hdr *)(datap);
324 
325 	memset(&eth_hdr, 0, sizeof(eth_hdr));
326 	eth_hdr.h_proto = llc_hdr->eth_type;
327 
328 	switch ((le16_to_cpu(wh.frame_control)) &
329 		(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
330 	case 0:
331 		memcpy(eth_hdr.h_dest, wh.addr1, ETH_ALEN);
332 		memcpy(eth_hdr.h_source, wh.addr2, ETH_ALEN);
333 		break;
334 	case IEEE80211_FCTL_TODS:
335 		memcpy(eth_hdr.h_dest, wh.addr3, ETH_ALEN);
336 		memcpy(eth_hdr.h_source, wh.addr2, ETH_ALEN);
337 		break;
338 	case IEEE80211_FCTL_FROMDS:
339 		memcpy(eth_hdr.h_dest, wh.addr1, ETH_ALEN);
340 		memcpy(eth_hdr.h_source, wh.addr3, ETH_ALEN);
341 		break;
342 	case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
343 		break;
344 	}
345 
346 	skb_pull(skb, sizeof(struct ath6kl_llc_snap_hdr));
347 	skb_push(skb, sizeof(eth_hdr));
348 
349 	datap = skb->data;
350 
351 	memcpy(datap, &eth_hdr, sizeof(eth_hdr));
352 
353 	return 0;
354 }
355 
356 /*
357  * Performs 802.3 to DIX encapsulation for received packets.
358  * Assumes the entire 802.3 header is contigous.
359  */
360 int ath6kl_wmi_dot3_2_dix(struct sk_buff *skb)
361 {
362 	struct ath6kl_llc_snap_hdr *llc_hdr;
363 	struct ethhdr eth_hdr;
364 	u8 *datap;
365 
366 	if (WARN_ON(skb == NULL))
367 		return -EINVAL;
368 
369 	datap = skb->data;
370 
371 	memcpy(&eth_hdr, datap, sizeof(eth_hdr));
372 
373 	llc_hdr = (struct ath6kl_llc_snap_hdr *) (datap + sizeof(eth_hdr));
374 	eth_hdr.h_proto = llc_hdr->eth_type;
375 
376 	skb_pull(skb, sizeof(struct ath6kl_llc_snap_hdr));
377 	datap = skb->data;
378 
379 	memcpy(datap, &eth_hdr, sizeof(eth_hdr));
380 
381 	return 0;
382 }
383 
384 static int ath6kl_wmi_tx_complete_event_rx(u8 *datap, int len)
385 {
386 	struct tx_complete_msg_v1 *msg_v1;
387 	struct wmi_tx_complete_event *evt;
388 	int index;
389 	u16 size;
390 
391 	evt = (struct wmi_tx_complete_event *) datap;
392 
393 	ath6kl_dbg(ATH6KL_DBG_WMI, "comp: %d %d %d\n",
394 		   evt->num_msg, evt->msg_len, evt->msg_type);
395 
396 	if (!AR_DBG_LVL_CHECK(ATH6KL_DBG_WMI))
397 		return 0;
398 
399 	for (index = 0; index < evt->num_msg; index++) {
400 		size = sizeof(struct wmi_tx_complete_event) +
401 		    (index * sizeof(struct tx_complete_msg_v1));
402 		msg_v1 = (struct tx_complete_msg_v1 *)(datap + size);
403 
404 		ath6kl_dbg(ATH6KL_DBG_WMI, "msg: %d %d %d %d\n",
405 			   msg_v1->status, msg_v1->pkt_id,
406 			   msg_v1->rate_idx, msg_v1->ack_failures);
407 	}
408 
409 	return 0;
410 }
411 
412 static int ath6kl_wmi_remain_on_chnl_event_rx(struct wmi *wmi, u8 *datap,
413 					      int len)
414 {
415 	struct wmi_remain_on_chnl_event *ev;
416 	u32 freq;
417 	u32 dur;
418 	struct ieee80211_channel *chan;
419 	struct ath6kl *ar = wmi->parent_dev;
420 
421 	if (len < sizeof(*ev))
422 		return -EINVAL;
423 
424 	ev = (struct wmi_remain_on_chnl_event *) datap;
425 	freq = le32_to_cpu(ev->freq);
426 	dur = le32_to_cpu(ev->duration);
427 	ath6kl_dbg(ATH6KL_DBG_WMI, "remain_on_chnl: freq=%u dur=%u\n",
428 		   freq, dur);
429 	chan = ieee80211_get_channel(ar->wdev->wiphy, freq);
430 	if (!chan) {
431 		ath6kl_dbg(ATH6KL_DBG_WMI, "remain_on_chnl: Unknown channel "
432 			   "(freq=%u)\n", freq);
433 		return -EINVAL;
434 	}
435 	cfg80211_ready_on_channel(ar->net_dev, 1, chan, NL80211_CHAN_NO_HT,
436 				  dur, GFP_ATOMIC);
437 
438 	return 0;
439 }
440 
441 static int ath6kl_wmi_cancel_remain_on_chnl_event_rx(struct wmi *wmi,
442 						     u8 *datap, int len)
443 {
444 	struct wmi_cancel_remain_on_chnl_event *ev;
445 	u32 freq;
446 	u32 dur;
447 	struct ieee80211_channel *chan;
448 	struct ath6kl *ar = wmi->parent_dev;
449 
450 	if (len < sizeof(*ev))
451 		return -EINVAL;
452 
453 	ev = (struct wmi_cancel_remain_on_chnl_event *) datap;
454 	freq = le32_to_cpu(ev->freq);
455 	dur = le32_to_cpu(ev->duration);
456 	ath6kl_dbg(ATH6KL_DBG_WMI, "cancel_remain_on_chnl: freq=%u dur=%u "
457 		   "status=%u\n", freq, dur, ev->status);
458 	chan = ieee80211_get_channel(ar->wdev->wiphy, freq);
459 	if (!chan) {
460 		ath6kl_dbg(ATH6KL_DBG_WMI, "cancel_remain_on_chnl: Unknown "
461 			   "channel (freq=%u)\n", freq);
462 		return -EINVAL;
463 	}
464 	cfg80211_remain_on_channel_expired(ar->net_dev, 1, chan,
465 					   NL80211_CHAN_NO_HT, GFP_ATOMIC);
466 
467 	return 0;
468 }
469 
470 static int ath6kl_wmi_tx_status_event_rx(struct wmi *wmi, u8 *datap, int len)
471 {
472 	struct wmi_tx_status_event *ev;
473 	u32 id;
474 	struct ath6kl *ar = wmi->parent_dev;
475 
476 	if (len < sizeof(*ev))
477 		return -EINVAL;
478 
479 	ev = (struct wmi_tx_status_event *) datap;
480 	id = le32_to_cpu(ev->id);
481 	ath6kl_dbg(ATH6KL_DBG_WMI, "tx_status: id=%x ack_status=%u\n",
482 		   id, ev->ack_status);
483 	if (wmi->last_mgmt_tx_frame) {
484 		cfg80211_mgmt_tx_status(ar->net_dev, id,
485 					wmi->last_mgmt_tx_frame,
486 					wmi->last_mgmt_tx_frame_len,
487 					!!ev->ack_status, GFP_ATOMIC);
488 		kfree(wmi->last_mgmt_tx_frame);
489 		wmi->last_mgmt_tx_frame = NULL;
490 		wmi->last_mgmt_tx_frame_len = 0;
491 	}
492 
493 	return 0;
494 }
495 
496 static int ath6kl_wmi_rx_probe_req_event_rx(struct wmi *wmi, u8 *datap, int len)
497 {
498 	struct wmi_p2p_rx_probe_req_event *ev;
499 	u32 freq;
500 	u16 dlen;
501 	struct ath6kl *ar = wmi->parent_dev;
502 
503 	if (len < sizeof(*ev))
504 		return -EINVAL;
505 
506 	ev = (struct wmi_p2p_rx_probe_req_event *) datap;
507 	freq = le32_to_cpu(ev->freq);
508 	dlen = le16_to_cpu(ev->len);
509 	if (datap + len < ev->data + dlen) {
510 		ath6kl_err("invalid wmi_p2p_rx_probe_req_event: "
511 			   "len=%d dlen=%u\n", len, dlen);
512 		return -EINVAL;
513 	}
514 	ath6kl_dbg(ATH6KL_DBG_WMI, "rx_probe_req: len=%u freq=%u "
515 		   "probe_req_report=%d\n",
516 		   dlen, freq, ar->probe_req_report);
517 
518 	if (ar->probe_req_report || ar->nw_type == AP_NETWORK)
519 		cfg80211_rx_mgmt(ar->net_dev, freq, ev->data, dlen, GFP_ATOMIC);
520 
521 	return 0;
522 }
523 
524 static int ath6kl_wmi_p2p_capabilities_event_rx(u8 *datap, int len)
525 {
526 	struct wmi_p2p_capabilities_event *ev;
527 	u16 dlen;
528 
529 	if (len < sizeof(*ev))
530 		return -EINVAL;
531 
532 	ev = (struct wmi_p2p_capabilities_event *) datap;
533 	dlen = le16_to_cpu(ev->len);
534 	ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_capab: len=%u\n", dlen);
535 
536 	return 0;
537 }
538 
539 static int ath6kl_wmi_rx_action_event_rx(struct wmi *wmi, u8 *datap, int len)
540 {
541 	struct wmi_rx_action_event *ev;
542 	u32 freq;
543 	u16 dlen;
544 	struct ath6kl *ar = wmi->parent_dev;
545 
546 	if (len < sizeof(*ev))
547 		return -EINVAL;
548 
549 	ev = (struct wmi_rx_action_event *) datap;
550 	freq = le32_to_cpu(ev->freq);
551 	dlen = le16_to_cpu(ev->len);
552 	if (datap + len < ev->data + dlen) {
553 		ath6kl_err("invalid wmi_rx_action_event: "
554 			   "len=%d dlen=%u\n", len, dlen);
555 		return -EINVAL;
556 	}
557 	ath6kl_dbg(ATH6KL_DBG_WMI, "rx_action: len=%u freq=%u\n", dlen, freq);
558 	cfg80211_rx_mgmt(ar->net_dev, freq, ev->data, dlen, GFP_ATOMIC);
559 
560 	return 0;
561 }
562 
563 static int ath6kl_wmi_p2p_info_event_rx(u8 *datap, int len)
564 {
565 	struct wmi_p2p_info_event *ev;
566 	u32 flags;
567 	u16 dlen;
568 
569 	if (len < sizeof(*ev))
570 		return -EINVAL;
571 
572 	ev = (struct wmi_p2p_info_event *) datap;
573 	flags = le32_to_cpu(ev->info_req_flags);
574 	dlen = le16_to_cpu(ev->len);
575 	ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_info: flags=%x len=%d\n", flags, dlen);
576 
577 	if (flags & P2P_FLAG_CAPABILITIES_REQ) {
578 		struct wmi_p2p_capabilities *cap;
579 		if (dlen < sizeof(*cap))
580 			return -EINVAL;
581 		cap = (struct wmi_p2p_capabilities *) ev->data;
582 		ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_info: GO Power Save = %d\n",
583 			   cap->go_power_save);
584 	}
585 
586 	if (flags & P2P_FLAG_MACADDR_REQ) {
587 		struct wmi_p2p_macaddr *mac;
588 		if (dlen < sizeof(*mac))
589 			return -EINVAL;
590 		mac = (struct wmi_p2p_macaddr *) ev->data;
591 		ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_info: MAC Address = %pM\n",
592 			   mac->mac_addr);
593 	}
594 
595 	if (flags & P2P_FLAG_HMODEL_REQ) {
596 		struct wmi_p2p_hmodel *mod;
597 		if (dlen < sizeof(*mod))
598 			return -EINVAL;
599 		mod = (struct wmi_p2p_hmodel *) ev->data;
600 		ath6kl_dbg(ATH6KL_DBG_WMI, "p2p_info: P2P Model = %d (%s)\n",
601 			   mod->p2p_model,
602 			   mod->p2p_model ? "host" : "firmware");
603 	}
604 	return 0;
605 }
606 
607 static inline struct sk_buff *ath6kl_wmi_get_new_buf(u32 size)
608 {
609 	struct sk_buff *skb;
610 
611 	skb = ath6kl_buf_alloc(size);
612 	if (!skb)
613 		return NULL;
614 
615 	skb_put(skb, size);
616 	if (size)
617 		memset(skb->data, 0, size);
618 
619 	return skb;
620 }
621 
622 /* Send a "simple" wmi command -- one with no arguments */
623 static int ath6kl_wmi_simple_cmd(struct wmi *wmi, enum wmi_cmd_id cmd_id)
624 {
625 	struct sk_buff *skb;
626 	int ret;
627 
628 	skb = ath6kl_wmi_get_new_buf(0);
629 	if (!skb)
630 		return -ENOMEM;
631 
632 	ret = ath6kl_wmi_cmd_send(wmi, skb, cmd_id, NO_SYNC_WMIFLAG);
633 
634 	return ret;
635 }
636 
637 static int ath6kl_wmi_ready_event_rx(struct wmi *wmi, u8 *datap, int len)
638 {
639 	struct wmi_ready_event_2 *ev = (struct wmi_ready_event_2 *) datap;
640 
641 	if (len < sizeof(struct wmi_ready_event_2))
642 		return -EINVAL;
643 
644 	wmi->ready = true;
645 	ath6kl_ready_event(wmi->parent_dev, ev->mac_addr,
646 			   le32_to_cpu(ev->sw_version),
647 			   le32_to_cpu(ev->abi_version));
648 
649 	return 0;
650 }
651 
652 /*
653  * Mechanism to modify the roaming behavior in the firmware. The lower rssi
654  * at which the station has to roam can be passed with
655  * WMI_SET_LRSSI_SCAN_PARAMS. Subtract 96 from RSSI to get the signal level
656  * in dBm.
657  */
658 int ath6kl_wmi_set_roam_lrssi_cmd(struct wmi *wmi, u8 lrssi)
659 {
660 	struct sk_buff *skb;
661 	struct roam_ctrl_cmd *cmd;
662 
663 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
664 	if (!skb)
665 		return -ENOMEM;
666 
667 	cmd = (struct roam_ctrl_cmd *) skb->data;
668 
669 	cmd->info.params.lrssi_scan_period = cpu_to_le16(DEF_LRSSI_SCAN_PERIOD);
670 	cmd->info.params.lrssi_scan_threshold = a_cpu_to_sle16(lrssi +
671 						       DEF_SCAN_FOR_ROAM_INTVL);
672 	cmd->info.params.lrssi_roam_threshold = a_cpu_to_sle16(lrssi);
673 	cmd->info.params.roam_rssi_floor = DEF_LRSSI_ROAM_FLOOR;
674 	cmd->roam_ctrl = WMI_SET_LRSSI_SCAN_PARAMS;
675 
676 	ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_ROAM_CTRL_CMDID, NO_SYNC_WMIFLAG);
677 
678 	return 0;
679 }
680 
681 static int ath6kl_wmi_connect_event_rx(struct wmi *wmi, u8 *datap, int len)
682 {
683 	struct wmi_connect_event *ev;
684 	u8 *pie, *peie;
685 	struct ath6kl *ar = wmi->parent_dev;
686 
687 	if (len < sizeof(struct wmi_connect_event))
688 		return -EINVAL;
689 
690 	ev = (struct wmi_connect_event *) datap;
691 
692 	if (ar->nw_type == AP_NETWORK) {
693 		/* AP mode start/STA connected event */
694 		struct net_device *dev = ar->net_dev;
695 		if (memcmp(dev->dev_addr, ev->u.ap_bss.bssid, ETH_ALEN) == 0) {
696 			ath6kl_dbg(ATH6KL_DBG_WMI, "%s: freq %d bssid %pM "
697 				   "(AP started)\n",
698 				   __func__, le16_to_cpu(ev->u.ap_bss.ch),
699 				   ev->u.ap_bss.bssid);
700 			ath6kl_connect_ap_mode_bss(
701 				ar, le16_to_cpu(ev->u.ap_bss.ch));
702 		} else {
703 			ath6kl_dbg(ATH6KL_DBG_WMI, "%s: aid %u mac_addr %pM "
704 				   "auth=%u keymgmt=%u cipher=%u apsd_info=%u "
705 				   "(STA connected)\n",
706 				   __func__, ev->u.ap_sta.aid,
707 				   ev->u.ap_sta.mac_addr,
708 				   ev->u.ap_sta.auth,
709 				   ev->u.ap_sta.keymgmt,
710 				   le16_to_cpu(ev->u.ap_sta.cipher),
711 				   ev->u.ap_sta.apsd_info);
712 			ath6kl_connect_ap_mode_sta(
713 				ar, ev->u.ap_sta.aid, ev->u.ap_sta.mac_addr,
714 				ev->u.ap_sta.keymgmt,
715 				le16_to_cpu(ev->u.ap_sta.cipher),
716 				ev->u.ap_sta.auth, ev->assoc_req_len,
717 				ev->assoc_info + ev->beacon_ie_len);
718 		}
719 		return 0;
720 	}
721 
722 	/* STA/IBSS mode connection event */
723 
724 	ath6kl_dbg(ATH6KL_DBG_WMI, "%s: freq %d bssid %pM\n",
725 		   __func__, le16_to_cpu(ev->u.sta.ch), ev->u.sta.bssid);
726 
727 	/* Start of assoc rsp IEs */
728 	pie = ev->assoc_info + ev->beacon_ie_len +
729 	      ev->assoc_req_len + (sizeof(u16) * 3); /* capinfo, status, aid */
730 
731 	/* End of assoc rsp IEs */
732 	peie = ev->assoc_info + ev->beacon_ie_len + ev->assoc_req_len +
733 	    ev->assoc_resp_len;
734 
735 	while (pie < peie) {
736 		switch (*pie) {
737 		case WLAN_EID_VENDOR_SPECIFIC:
738 			if (pie[1] > 3 && pie[2] == 0x00 && pie[3] == 0x50 &&
739 			    pie[4] == 0xf2 && pie[5] == WMM_OUI_TYPE) {
740 				/* WMM OUT (00:50:F2) */
741 				if (pie[1] > 5
742 				    && pie[6] == WMM_PARAM_OUI_SUBTYPE)
743 					wmi->is_wmm_enabled = true;
744 			}
745 			break;
746 		}
747 
748 		if (wmi->is_wmm_enabled)
749 			break;
750 
751 		pie += pie[1] + 2;
752 	}
753 
754 	ath6kl_connect_event(wmi->parent_dev, le16_to_cpu(ev->u.sta.ch),
755 			     ev->u.sta.bssid,
756 			     le16_to_cpu(ev->u.sta.listen_intvl),
757 			     le16_to_cpu(ev->u.sta.beacon_intvl),
758 			     le32_to_cpu(ev->u.sta.nw_type),
759 			     ev->beacon_ie_len, ev->assoc_req_len,
760 			     ev->assoc_resp_len, ev->assoc_info);
761 
762 	return 0;
763 }
764 
765 static struct country_code_to_enum_rd *
766 ath6kl_regd_find_country(u16 countryCode)
767 {
768 	int i;
769 
770 	for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
771 		if (allCountries[i].countryCode == countryCode)
772 			return &allCountries[i];
773 	}
774 
775 	return NULL;
776 }
777 
778 static struct reg_dmn_pair_mapping *
779 ath6kl_get_regpair(u16 regdmn)
780 {
781 	int i;
782 
783 	if (regdmn == NO_ENUMRD)
784 		return NULL;
785 
786 	for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
787 		if (regDomainPairs[i].regDmnEnum == regdmn)
788 			return &regDomainPairs[i];
789 	}
790 
791 	return NULL;
792 }
793 
794 static struct country_code_to_enum_rd *
795 ath6kl_regd_find_country_by_rd(u16 regdmn)
796 {
797 	int i;
798 
799 	for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
800 		if (allCountries[i].regDmnEnum == regdmn)
801 			return &allCountries[i];
802 	}
803 
804 	return NULL;
805 }
806 
807 static void ath6kl_wmi_regdomain_event(struct wmi *wmi, u8 *datap, int len)
808 {
809 
810 	struct ath6kl_wmi_regdomain *ev;
811 	struct country_code_to_enum_rd *country = NULL;
812 	struct reg_dmn_pair_mapping *regpair = NULL;
813 	char alpha2[2];
814 	u32 reg_code;
815 
816 	ev = (struct ath6kl_wmi_regdomain *) datap;
817 	reg_code = le32_to_cpu(ev->reg_code);
818 
819 	if ((reg_code >> ATH6KL_COUNTRY_RD_SHIFT) & COUNTRY_ERD_FLAG)
820 		country = ath6kl_regd_find_country((u16) reg_code);
821 	else if (!(((u16) reg_code & WORLD_SKU_MASK) == WORLD_SKU_PREFIX)) {
822 
823 		regpair = ath6kl_get_regpair((u16) reg_code);
824 		country = ath6kl_regd_find_country_by_rd((u16) reg_code);
825 		ath6kl_dbg(ATH6KL_DBG_WMI, "ath6kl: Regpair used: 0x%0x\n",
826 				regpair->regDmnEnum);
827 	}
828 
829 	if (country) {
830 		alpha2[0] = country->isoName[0];
831 		alpha2[1] = country->isoName[1];
832 
833 		regulatory_hint(wmi->parent_dev->wdev->wiphy, alpha2);
834 
835 		ath6kl_dbg(ATH6KL_DBG_WMI, "ath6kl: Country alpha2 being used: %c%c\n",
836 				alpha2[0], alpha2[1]);
837 	}
838 }
839 
840 static int ath6kl_wmi_disconnect_event_rx(struct wmi *wmi, u8 *datap, int len)
841 {
842 	struct wmi_disconnect_event *ev;
843 	wmi->traffic_class = 100;
844 
845 	if (len < sizeof(struct wmi_disconnect_event))
846 		return -EINVAL;
847 
848 	ev = (struct wmi_disconnect_event *) datap;
849 
850 	wmi->is_wmm_enabled = false;
851 	wmi->pair_crypto_type = NONE_CRYPT;
852 	wmi->grp_crypto_type = NONE_CRYPT;
853 
854 	ath6kl_disconnect_event(wmi->parent_dev, ev->disconn_reason,
855 				ev->bssid, ev->assoc_resp_len, ev->assoc_info,
856 				le16_to_cpu(ev->proto_reason_status));
857 
858 	return 0;
859 }
860 
861 static int ath6kl_wmi_peer_node_event_rx(struct wmi *wmi, u8 *datap, int len)
862 {
863 	struct wmi_peer_node_event *ev;
864 
865 	if (len < sizeof(struct wmi_peer_node_event))
866 		return -EINVAL;
867 
868 	ev = (struct wmi_peer_node_event *) datap;
869 
870 	if (ev->event_code == PEER_NODE_JOIN_EVENT)
871 		ath6kl_dbg(ATH6KL_DBG_WMI, "joined node with mac addr: %pM\n",
872 			   ev->peer_mac_addr);
873 	else if (ev->event_code == PEER_NODE_LEAVE_EVENT)
874 		ath6kl_dbg(ATH6KL_DBG_WMI, "left node with mac addr: %pM\n",
875 			   ev->peer_mac_addr);
876 
877 	return 0;
878 }
879 
880 static int ath6kl_wmi_tkip_micerr_event_rx(struct wmi *wmi, u8 *datap, int len)
881 {
882 	struct wmi_tkip_micerr_event *ev;
883 
884 	if (len < sizeof(struct wmi_tkip_micerr_event))
885 		return -EINVAL;
886 
887 	ev = (struct wmi_tkip_micerr_event *) datap;
888 
889 	ath6kl_tkip_micerr_event(wmi->parent_dev, ev->key_id, ev->is_mcast);
890 
891 	return 0;
892 }
893 
894 static int ath6kl_wmi_bssinfo_event_rx(struct wmi *wmi, u8 *datap, int len)
895 {
896 	struct wmi_bss_info_hdr2 *bih;
897 	u8 *buf;
898 	struct ieee80211_channel *channel;
899 	struct ath6kl *ar = wmi->parent_dev;
900 	struct ieee80211_mgmt *mgmt;
901 	struct cfg80211_bss *bss;
902 
903 	if (len <= sizeof(struct wmi_bss_info_hdr2))
904 		return -EINVAL;
905 
906 	bih = (struct wmi_bss_info_hdr2 *) datap;
907 	buf = datap + sizeof(struct wmi_bss_info_hdr2);
908 	len -= sizeof(struct wmi_bss_info_hdr2);
909 
910 	ath6kl_dbg(ATH6KL_DBG_WMI,
911 		   "bss info evt - ch %u, snr %d, rssi %d, bssid \"%pM\" "
912 		   "frame_type=%d\n",
913 		   bih->ch, bih->snr, bih->snr - 95, bih->bssid,
914 		   bih->frame_type);
915 
916 	if (bih->frame_type != BEACON_FTYPE &&
917 	    bih->frame_type != PROBERESP_FTYPE)
918 		return 0; /* Only update BSS table for now */
919 
920 	if (bih->frame_type == BEACON_FTYPE &&
921 	    test_bit(CLEAR_BSSFILTER_ON_BEACON, &ar->flag)) {
922 		clear_bit(CLEAR_BSSFILTER_ON_BEACON, &ar->flag);
923 		ath6kl_wmi_bssfilter_cmd(ar->wmi, NONE_BSS_FILTER, 0);
924 	}
925 
926 	channel = ieee80211_get_channel(ar->wdev->wiphy, le16_to_cpu(bih->ch));
927 	if (channel == NULL)
928 		return -EINVAL;
929 
930 	if (len < 8 + 2 + 2)
931 		return -EINVAL;
932 
933 	if (bih->frame_type == BEACON_FTYPE && test_bit(CONNECTED, &ar->flag) &&
934 	    memcmp(bih->bssid, ar->bssid, ETH_ALEN) == 0) {
935 		const u8 *tim;
936 		tim = cfg80211_find_ie(WLAN_EID_TIM, buf + 8 + 2 + 2,
937 				       len - 8 - 2 - 2);
938 		if (tim && tim[1] >= 2) {
939 			ar->assoc_bss_dtim_period = tim[3];
940 			set_bit(DTIM_PERIOD_AVAIL, &ar->flag);
941 		}
942 	}
943 
944 	/*
945 	 * In theory, use of cfg80211_inform_bss() would be more natural here
946 	 * since we do not have the full frame. However, at least for now,
947 	 * cfg80211 can only distinguish Beacon and Probe Response frames from
948 	 * each other when using cfg80211_inform_bss_frame(), so let's build a
949 	 * fake IEEE 802.11 header to be able to take benefit of this.
950 	 */
951 	mgmt = kmalloc(24 + len, GFP_ATOMIC);
952 	if (mgmt == NULL)
953 		return -EINVAL;
954 
955 	if (bih->frame_type == BEACON_FTYPE) {
956 		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
957 						  IEEE80211_STYPE_BEACON);
958 		memset(mgmt->da, 0xff, ETH_ALEN);
959 	} else {
960 		struct net_device *dev = ar->net_dev;
961 
962 		mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
963 						  IEEE80211_STYPE_PROBE_RESP);
964 		memcpy(mgmt->da, dev->dev_addr, ETH_ALEN);
965 	}
966 	mgmt->duration = cpu_to_le16(0);
967 	memcpy(mgmt->sa, bih->bssid, ETH_ALEN);
968 	memcpy(mgmt->bssid, bih->bssid, ETH_ALEN);
969 	mgmt->seq_ctrl = cpu_to_le16(0);
970 
971 	memcpy(&mgmt->u.beacon, buf, len);
972 
973 	bss = cfg80211_inform_bss_frame(ar->wdev->wiphy, channel, mgmt,
974 					24 + len, (bih->snr - 95) * 100,
975 					GFP_ATOMIC);
976 	kfree(mgmt);
977 	if (bss == NULL)
978 		return -ENOMEM;
979 	cfg80211_put_bss(bss);
980 
981 	return 0;
982 }
983 
984 /* Inactivity timeout of a fatpipe(pstream) at the target */
985 static int ath6kl_wmi_pstream_timeout_event_rx(struct wmi *wmi, u8 *datap,
986 					       int len)
987 {
988 	struct wmi_pstream_timeout_event *ev;
989 
990 	if (len < sizeof(struct wmi_pstream_timeout_event))
991 		return -EINVAL;
992 
993 	ev = (struct wmi_pstream_timeout_event *) datap;
994 
995 	/*
996 	 * When the pstream (fat pipe == AC) timesout, it means there were
997 	 * no thinStreams within this pstream & it got implicitly created
998 	 * due to data flow on this AC. We start the inactivity timer only
999 	 * for implicitly created pstream. Just reset the host state.
1000 	 */
1001 	spin_lock_bh(&wmi->lock);
1002 	wmi->stream_exist_for_ac[ev->traffic_class] = 0;
1003 	wmi->fat_pipe_exist &= ~(1 << ev->traffic_class);
1004 	spin_unlock_bh(&wmi->lock);
1005 
1006 	/* Indicate inactivity to driver layer for this fatpipe (pstream) */
1007 	ath6kl_indicate_tx_activity(wmi->parent_dev, ev->traffic_class, false);
1008 
1009 	return 0;
1010 }
1011 
1012 static int ath6kl_wmi_bitrate_reply_rx(struct wmi *wmi, u8 *datap, int len)
1013 {
1014 	struct wmi_bit_rate_reply *reply;
1015 	s32 rate;
1016 	u32 sgi, index;
1017 
1018 	if (len < sizeof(struct wmi_bit_rate_reply))
1019 		return -EINVAL;
1020 
1021 	reply = (struct wmi_bit_rate_reply *) datap;
1022 
1023 	ath6kl_dbg(ATH6KL_DBG_WMI, "rateindex %d\n", reply->rate_index);
1024 
1025 	if (reply->rate_index == (s8) RATE_AUTO) {
1026 		rate = RATE_AUTO;
1027 	} else {
1028 		index = reply->rate_index & 0x7f;
1029 		sgi = (reply->rate_index & 0x80) ? 1 : 0;
1030 		rate = wmi_rate_tbl[index][sgi];
1031 	}
1032 
1033 	ath6kl_wakeup_event(wmi->parent_dev);
1034 
1035 	return 0;
1036 }
1037 
1038 static int ath6kl_wmi_tcmd_test_report_rx(struct wmi *wmi, u8 *datap, int len)
1039 {
1040 	ath6kl_tm_rx_report_event(wmi->parent_dev, datap, len);
1041 
1042 	return 0;
1043 }
1044 
1045 static int ath6kl_wmi_ratemask_reply_rx(struct wmi *wmi, u8 *datap, int len)
1046 {
1047 	if (len < sizeof(struct wmi_fix_rates_reply))
1048 		return -EINVAL;
1049 
1050 	ath6kl_wakeup_event(wmi->parent_dev);
1051 
1052 	return 0;
1053 }
1054 
1055 static int ath6kl_wmi_ch_list_reply_rx(struct wmi *wmi, u8 *datap, int len)
1056 {
1057 	if (len < sizeof(struct wmi_channel_list_reply))
1058 		return -EINVAL;
1059 
1060 	ath6kl_wakeup_event(wmi->parent_dev);
1061 
1062 	return 0;
1063 }
1064 
1065 static int ath6kl_wmi_tx_pwr_reply_rx(struct wmi *wmi, u8 *datap, int len)
1066 {
1067 	struct wmi_tx_pwr_reply *reply;
1068 
1069 	if (len < sizeof(struct wmi_tx_pwr_reply))
1070 		return -EINVAL;
1071 
1072 	reply = (struct wmi_tx_pwr_reply *) datap;
1073 	ath6kl_txpwr_rx_evt(wmi->parent_dev, reply->dbM);
1074 
1075 	return 0;
1076 }
1077 
1078 static int ath6kl_wmi_keepalive_reply_rx(struct wmi *wmi, u8 *datap, int len)
1079 {
1080 	if (len < sizeof(struct wmi_get_keepalive_cmd))
1081 		return -EINVAL;
1082 
1083 	ath6kl_wakeup_event(wmi->parent_dev);
1084 
1085 	return 0;
1086 }
1087 
1088 static int ath6kl_wmi_scan_complete_rx(struct wmi *wmi, u8 *datap, int len)
1089 {
1090 	struct wmi_scan_complete_event *ev;
1091 
1092 	ev = (struct wmi_scan_complete_event *) datap;
1093 
1094 	ath6kl_scan_complete_evt(wmi->parent_dev, a_sle32_to_cpu(ev->status));
1095 	wmi->is_probe_ssid = false;
1096 
1097 	return 0;
1098 }
1099 
1100 /*
1101  * Target is reporting a programming error.  This is for
1102  * developer aid only.  Target only checks a few common violations
1103  * and it is responsibility of host to do all error checking.
1104  * Behavior of target after wmi error event is undefined.
1105  * A reset is recommended.
1106  */
1107 static int ath6kl_wmi_error_event_rx(struct wmi *wmi, u8 *datap, int len)
1108 {
1109 	const char *type = "unknown error";
1110 	struct wmi_cmd_error_event *ev;
1111 	ev = (struct wmi_cmd_error_event *) datap;
1112 
1113 	switch (ev->err_code) {
1114 	case INVALID_PARAM:
1115 		type = "invalid parameter";
1116 		break;
1117 	case ILLEGAL_STATE:
1118 		type = "invalid state";
1119 		break;
1120 	case INTERNAL_ERROR:
1121 		type = "internal error";
1122 		break;
1123 	}
1124 
1125 	ath6kl_dbg(ATH6KL_DBG_WMI, "programming error, cmd=%d %s\n",
1126 		   ev->cmd_id, type);
1127 
1128 	return 0;
1129 }
1130 
1131 static int ath6kl_wmi_stats_event_rx(struct wmi *wmi, u8 *datap, int len)
1132 {
1133 	ath6kl_tgt_stats_event(wmi->parent_dev, datap, len);
1134 
1135 	return 0;
1136 }
1137 
1138 static u8 ath6kl_wmi_get_upper_threshold(s16 rssi,
1139 					 struct sq_threshold_params *sq_thresh,
1140 					 u32 size)
1141 {
1142 	u32 index;
1143 	u8 threshold = (u8) sq_thresh->upper_threshold[size - 1];
1144 
1145 	/* The list is already in sorted order. Get the next lower value */
1146 	for (index = 0; index < size; index++) {
1147 		if (rssi < sq_thresh->upper_threshold[index]) {
1148 			threshold = (u8) sq_thresh->upper_threshold[index];
1149 			break;
1150 		}
1151 	}
1152 
1153 	return threshold;
1154 }
1155 
1156 static u8 ath6kl_wmi_get_lower_threshold(s16 rssi,
1157 					 struct sq_threshold_params *sq_thresh,
1158 					 u32 size)
1159 {
1160 	u32 index;
1161 	u8 threshold = (u8) sq_thresh->lower_threshold[size - 1];
1162 
1163 	/* The list is already in sorted order. Get the next lower value */
1164 	for (index = 0; index < size; index++) {
1165 		if (rssi > sq_thresh->lower_threshold[index]) {
1166 			threshold = (u8) sq_thresh->lower_threshold[index];
1167 			break;
1168 		}
1169 	}
1170 
1171 	return threshold;
1172 }
1173 
1174 static int ath6kl_wmi_send_rssi_threshold_params(struct wmi *wmi,
1175 			struct wmi_rssi_threshold_params_cmd *rssi_cmd)
1176 {
1177 	struct sk_buff *skb;
1178 	struct wmi_rssi_threshold_params_cmd *cmd;
1179 
1180 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1181 	if (!skb)
1182 		return -ENOMEM;
1183 
1184 	cmd = (struct wmi_rssi_threshold_params_cmd *) skb->data;
1185 	memcpy(cmd, rssi_cmd, sizeof(struct wmi_rssi_threshold_params_cmd));
1186 
1187 	return ath6kl_wmi_cmd_send(wmi, skb, WMI_RSSI_THRESHOLD_PARAMS_CMDID,
1188 				   NO_SYNC_WMIFLAG);
1189 }
1190 
1191 static int ath6kl_wmi_rssi_threshold_event_rx(struct wmi *wmi, u8 *datap,
1192 					      int len)
1193 {
1194 	struct wmi_rssi_threshold_event *reply;
1195 	struct wmi_rssi_threshold_params_cmd cmd;
1196 	struct sq_threshold_params *sq_thresh;
1197 	enum wmi_rssi_threshold_val new_threshold;
1198 	u8 upper_rssi_threshold, lower_rssi_threshold;
1199 	s16 rssi;
1200 	int ret;
1201 
1202 	if (len < sizeof(struct wmi_rssi_threshold_event))
1203 		return -EINVAL;
1204 
1205 	reply = (struct wmi_rssi_threshold_event *) datap;
1206 	new_threshold = (enum wmi_rssi_threshold_val) reply->range;
1207 	rssi = a_sle16_to_cpu(reply->rssi);
1208 
1209 	sq_thresh = &wmi->sq_threshld[SIGNAL_QUALITY_METRICS_RSSI];
1210 
1211 	/*
1212 	 * Identify the threshold breached and communicate that to the app.
1213 	 * After that install a new set of thresholds based on the signal
1214 	 * quality reported by the target
1215 	 */
1216 	if (new_threshold) {
1217 		/* Upper threshold breached */
1218 		if (rssi < sq_thresh->upper_threshold[0]) {
1219 			ath6kl_dbg(ATH6KL_DBG_WMI,
1220 				"spurious upper rssi threshold event: %d\n",
1221 				rssi);
1222 		} else if ((rssi < sq_thresh->upper_threshold[1]) &&
1223 			   (rssi >= sq_thresh->upper_threshold[0])) {
1224 			new_threshold = WMI_RSSI_THRESHOLD1_ABOVE;
1225 		} else if ((rssi < sq_thresh->upper_threshold[2]) &&
1226 			   (rssi >= sq_thresh->upper_threshold[1])) {
1227 			new_threshold = WMI_RSSI_THRESHOLD2_ABOVE;
1228 		} else if ((rssi < sq_thresh->upper_threshold[3]) &&
1229 			   (rssi >= sq_thresh->upper_threshold[2])) {
1230 			new_threshold = WMI_RSSI_THRESHOLD3_ABOVE;
1231 		} else if ((rssi < sq_thresh->upper_threshold[4]) &&
1232 			   (rssi >= sq_thresh->upper_threshold[3])) {
1233 			new_threshold = WMI_RSSI_THRESHOLD4_ABOVE;
1234 		} else if ((rssi < sq_thresh->upper_threshold[5]) &&
1235 			   (rssi >= sq_thresh->upper_threshold[4])) {
1236 			new_threshold = WMI_RSSI_THRESHOLD5_ABOVE;
1237 		} else if (rssi >= sq_thresh->upper_threshold[5]) {
1238 			new_threshold = WMI_RSSI_THRESHOLD6_ABOVE;
1239 		}
1240 	} else {
1241 		/* Lower threshold breached */
1242 		if (rssi > sq_thresh->lower_threshold[0]) {
1243 			ath6kl_dbg(ATH6KL_DBG_WMI,
1244 				"spurious lower rssi threshold event: %d %d\n",
1245 				rssi, sq_thresh->lower_threshold[0]);
1246 		} else if ((rssi > sq_thresh->lower_threshold[1]) &&
1247 			   (rssi <= sq_thresh->lower_threshold[0])) {
1248 			new_threshold = WMI_RSSI_THRESHOLD6_BELOW;
1249 		} else if ((rssi > sq_thresh->lower_threshold[2]) &&
1250 			   (rssi <= sq_thresh->lower_threshold[1])) {
1251 			new_threshold = WMI_RSSI_THRESHOLD5_BELOW;
1252 		} else if ((rssi > sq_thresh->lower_threshold[3]) &&
1253 			   (rssi <= sq_thresh->lower_threshold[2])) {
1254 			new_threshold = WMI_RSSI_THRESHOLD4_BELOW;
1255 		} else if ((rssi > sq_thresh->lower_threshold[4]) &&
1256 			   (rssi <= sq_thresh->lower_threshold[3])) {
1257 			new_threshold = WMI_RSSI_THRESHOLD3_BELOW;
1258 		} else if ((rssi > sq_thresh->lower_threshold[5]) &&
1259 			   (rssi <= sq_thresh->lower_threshold[4])) {
1260 			new_threshold = WMI_RSSI_THRESHOLD2_BELOW;
1261 		} else if (rssi <= sq_thresh->lower_threshold[5]) {
1262 			new_threshold = WMI_RSSI_THRESHOLD1_BELOW;
1263 		}
1264 	}
1265 
1266 	/* Calculate and install the next set of thresholds */
1267 	lower_rssi_threshold = ath6kl_wmi_get_lower_threshold(rssi, sq_thresh,
1268 				       sq_thresh->lower_threshold_valid_count);
1269 	upper_rssi_threshold = ath6kl_wmi_get_upper_threshold(rssi, sq_thresh,
1270 				       sq_thresh->upper_threshold_valid_count);
1271 
1272 	/* Issue a wmi command to install the thresholds */
1273 	cmd.thresh_above1_val = a_cpu_to_sle16(upper_rssi_threshold);
1274 	cmd.thresh_below1_val = a_cpu_to_sle16(lower_rssi_threshold);
1275 	cmd.weight = sq_thresh->weight;
1276 	cmd.poll_time = cpu_to_le32(sq_thresh->polling_interval);
1277 
1278 	ret = ath6kl_wmi_send_rssi_threshold_params(wmi, &cmd);
1279 	if (ret) {
1280 		ath6kl_err("unable to configure rssi thresholds\n");
1281 		return -EIO;
1282 	}
1283 
1284 	return 0;
1285 }
1286 
1287 static int ath6kl_wmi_cac_event_rx(struct wmi *wmi, u8 *datap, int len)
1288 {
1289 	struct wmi_cac_event *reply;
1290 	struct ieee80211_tspec_ie *ts;
1291 	u16 active_tsids, tsinfo;
1292 	u8 tsid, index;
1293 	u8 ts_id;
1294 
1295 	if (len < sizeof(struct wmi_cac_event))
1296 		return -EINVAL;
1297 
1298 	reply = (struct wmi_cac_event *) datap;
1299 
1300 	if ((reply->cac_indication == CAC_INDICATION_ADMISSION_RESP) &&
1301 	    (reply->status_code != IEEE80211_TSPEC_STATUS_ADMISS_ACCEPTED)) {
1302 
1303 		ts = (struct ieee80211_tspec_ie *) &(reply->tspec_suggestion);
1304 		tsinfo = le16_to_cpu(ts->tsinfo);
1305 		tsid = (tsinfo >> IEEE80211_WMM_IE_TSPEC_TID_SHIFT) &
1306 			IEEE80211_WMM_IE_TSPEC_TID_MASK;
1307 
1308 		ath6kl_wmi_delete_pstream_cmd(wmi, reply->ac, tsid);
1309 	} else if (reply->cac_indication == CAC_INDICATION_NO_RESP) {
1310 		/*
1311 		 * Following assumes that there is only one outstanding
1312 		 * ADDTS request when this event is received
1313 		 */
1314 		spin_lock_bh(&wmi->lock);
1315 		active_tsids = wmi->stream_exist_for_ac[reply->ac];
1316 		spin_unlock_bh(&wmi->lock);
1317 
1318 		for (index = 0; index < sizeof(active_tsids) * 8; index++) {
1319 			if ((active_tsids >> index) & 1)
1320 				break;
1321 		}
1322 		if (index < (sizeof(active_tsids) * 8))
1323 			ath6kl_wmi_delete_pstream_cmd(wmi, reply->ac, index);
1324 	}
1325 
1326 	/*
1327 	 * Clear active tsids and Add missing handling
1328 	 * for delete qos stream from AP
1329 	 */
1330 	else if (reply->cac_indication == CAC_INDICATION_DELETE) {
1331 
1332 		ts = (struct ieee80211_tspec_ie *) &(reply->tspec_suggestion);
1333 		tsinfo = le16_to_cpu(ts->tsinfo);
1334 		ts_id = ((tsinfo >> IEEE80211_WMM_IE_TSPEC_TID_SHIFT) &
1335 			 IEEE80211_WMM_IE_TSPEC_TID_MASK);
1336 
1337 		spin_lock_bh(&wmi->lock);
1338 		wmi->stream_exist_for_ac[reply->ac] &= ~(1 << ts_id);
1339 		active_tsids = wmi->stream_exist_for_ac[reply->ac];
1340 		spin_unlock_bh(&wmi->lock);
1341 
1342 		/* Indicate stream inactivity to driver layer only if all tsids
1343 		 * within this AC are deleted.
1344 		 */
1345 		if (!active_tsids) {
1346 			ath6kl_indicate_tx_activity(wmi->parent_dev, reply->ac,
1347 						    false);
1348 			wmi->fat_pipe_exist &= ~(1 << reply->ac);
1349 		}
1350 	}
1351 
1352 	return 0;
1353 }
1354 
1355 static int ath6kl_wmi_send_snr_threshold_params(struct wmi *wmi,
1356 			struct wmi_snr_threshold_params_cmd *snr_cmd)
1357 {
1358 	struct sk_buff *skb;
1359 	struct wmi_snr_threshold_params_cmd *cmd;
1360 
1361 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1362 	if (!skb)
1363 		return -ENOMEM;
1364 
1365 	cmd = (struct wmi_snr_threshold_params_cmd *) skb->data;
1366 	memcpy(cmd, snr_cmd, sizeof(struct wmi_snr_threshold_params_cmd));
1367 
1368 	return ath6kl_wmi_cmd_send(wmi, skb, WMI_SNR_THRESHOLD_PARAMS_CMDID,
1369 				   NO_SYNC_WMIFLAG);
1370 }
1371 
1372 static int ath6kl_wmi_snr_threshold_event_rx(struct wmi *wmi, u8 *datap,
1373 					     int len)
1374 {
1375 	struct wmi_snr_threshold_event *reply;
1376 	struct sq_threshold_params *sq_thresh;
1377 	struct wmi_snr_threshold_params_cmd cmd;
1378 	enum wmi_snr_threshold_val new_threshold;
1379 	u8 upper_snr_threshold, lower_snr_threshold;
1380 	s16 snr;
1381 	int ret;
1382 
1383 	if (len < sizeof(struct wmi_snr_threshold_event))
1384 		return -EINVAL;
1385 
1386 	reply = (struct wmi_snr_threshold_event *) datap;
1387 
1388 	new_threshold = (enum wmi_snr_threshold_val) reply->range;
1389 	snr = reply->snr;
1390 
1391 	sq_thresh = &wmi->sq_threshld[SIGNAL_QUALITY_METRICS_SNR];
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 (snr < sq_thresh->upper_threshold[0]) {
1401 			ath6kl_dbg(ATH6KL_DBG_WMI,
1402 				"spurious upper snr threshold event: %d\n",
1403 				snr);
1404 		} else if ((snr < sq_thresh->upper_threshold[1]) &&
1405 			   (snr >= sq_thresh->upper_threshold[0])) {
1406 			new_threshold = WMI_SNR_THRESHOLD1_ABOVE;
1407 		} else if ((snr < sq_thresh->upper_threshold[2]) &&
1408 			   (snr >= sq_thresh->upper_threshold[1])) {
1409 			new_threshold = WMI_SNR_THRESHOLD2_ABOVE;
1410 		} else if ((snr < sq_thresh->upper_threshold[3]) &&
1411 			   (snr >= sq_thresh->upper_threshold[2])) {
1412 			new_threshold = WMI_SNR_THRESHOLD3_ABOVE;
1413 		} else if (snr >= sq_thresh->upper_threshold[3]) {
1414 			new_threshold = WMI_SNR_THRESHOLD4_ABOVE;
1415 		}
1416 	} else {
1417 		/* Lower threshold breached */
1418 		if (snr > sq_thresh->lower_threshold[0]) {
1419 			ath6kl_dbg(ATH6KL_DBG_WMI,
1420 				"spurious lower snr threshold event: %d\n",
1421 				sq_thresh->lower_threshold[0]);
1422 		} else if ((snr > sq_thresh->lower_threshold[1]) &&
1423 			   (snr <= sq_thresh->lower_threshold[0])) {
1424 			new_threshold = WMI_SNR_THRESHOLD4_BELOW;
1425 		} else if ((snr > sq_thresh->lower_threshold[2]) &&
1426 			   (snr <= sq_thresh->lower_threshold[1])) {
1427 			new_threshold = WMI_SNR_THRESHOLD3_BELOW;
1428 		} else if ((snr > sq_thresh->lower_threshold[3]) &&
1429 			   (snr <= sq_thresh->lower_threshold[2])) {
1430 			new_threshold = WMI_SNR_THRESHOLD2_BELOW;
1431 		} else if (snr <= sq_thresh->lower_threshold[3]) {
1432 			new_threshold = WMI_SNR_THRESHOLD1_BELOW;
1433 		}
1434 	}
1435 
1436 	/* Calculate and install the next set of thresholds */
1437 	lower_snr_threshold = ath6kl_wmi_get_lower_threshold(snr, sq_thresh,
1438 				       sq_thresh->lower_threshold_valid_count);
1439 	upper_snr_threshold = ath6kl_wmi_get_upper_threshold(snr, sq_thresh,
1440 				       sq_thresh->upper_threshold_valid_count);
1441 
1442 	/* Issue a wmi command to install the thresholds */
1443 	cmd.thresh_above1_val = upper_snr_threshold;
1444 	cmd.thresh_below1_val = lower_snr_threshold;
1445 	cmd.weight = sq_thresh->weight;
1446 	cmd.poll_time = cpu_to_le32(sq_thresh->polling_interval);
1447 
1448 	ath6kl_dbg(ATH6KL_DBG_WMI,
1449 		   "snr: %d, threshold: %d, lower: %d, upper: %d\n",
1450 		   snr, new_threshold,
1451 		   lower_snr_threshold, upper_snr_threshold);
1452 
1453 	ret = ath6kl_wmi_send_snr_threshold_params(wmi, &cmd);
1454 	if (ret) {
1455 		ath6kl_err("unable to configure snr threshold\n");
1456 		return -EIO;
1457 	}
1458 
1459 	return 0;
1460 }
1461 
1462 static int ath6kl_wmi_aplist_event_rx(struct wmi *wmi, u8 *datap, int len)
1463 {
1464 	u16 ap_info_entry_size;
1465 	struct wmi_aplist_event *ev = (struct wmi_aplist_event *) datap;
1466 	struct wmi_ap_info_v1 *ap_info_v1;
1467 	u8 index;
1468 
1469 	if (len < sizeof(struct wmi_aplist_event) ||
1470 	    ev->ap_list_ver != APLIST_VER1)
1471 		return -EINVAL;
1472 
1473 	ap_info_entry_size = sizeof(struct wmi_ap_info_v1);
1474 	ap_info_v1 = (struct wmi_ap_info_v1 *) ev->ap_list;
1475 
1476 	ath6kl_dbg(ATH6KL_DBG_WMI,
1477 		   "number of APs in aplist event: %d\n", ev->num_ap);
1478 
1479 	if (len < (int) (sizeof(struct wmi_aplist_event) +
1480 			 (ev->num_ap - 1) * ap_info_entry_size))
1481 		return -EINVAL;
1482 
1483 	/* AP list version 1 contents */
1484 	for (index = 0; index < ev->num_ap; index++) {
1485 		ath6kl_dbg(ATH6KL_DBG_WMI, "AP#%d BSSID %pM Channel %d\n",
1486 			   index, ap_info_v1->bssid, ap_info_v1->channel);
1487 		ap_info_v1++;
1488 	}
1489 
1490 	return 0;
1491 }
1492 
1493 int ath6kl_wmi_cmd_send(struct wmi *wmi, struct sk_buff *skb,
1494 			enum wmi_cmd_id cmd_id, enum wmi_sync_flag sync_flag)
1495 {
1496 	struct wmi_cmd_hdr *cmd_hdr;
1497 	enum htc_endpoint_id ep_id = wmi->ep_id;
1498 	int ret;
1499 
1500 	ath6kl_dbg(ATH6KL_DBG_WMI, "%s: cmd_id=%d\n", __func__, cmd_id);
1501 
1502 	if (WARN_ON(skb == NULL))
1503 		return -EINVAL;
1504 
1505 	if (sync_flag >= END_WMIFLAG) {
1506 		dev_kfree_skb(skb);
1507 		return -EINVAL;
1508 	}
1509 
1510 	if ((sync_flag == SYNC_BEFORE_WMIFLAG) ||
1511 	    (sync_flag == SYNC_BOTH_WMIFLAG)) {
1512 		/*
1513 		 * Make sure all data currently queued is transmitted before
1514 		 * the cmd execution.  Establish a new sync point.
1515 		 */
1516 		ath6kl_wmi_sync_point(wmi);
1517 	}
1518 
1519 	skb_push(skb, sizeof(struct wmi_cmd_hdr));
1520 
1521 	cmd_hdr = (struct wmi_cmd_hdr *) skb->data;
1522 	cmd_hdr->cmd_id = cpu_to_le16(cmd_id);
1523 	cmd_hdr->info1 = 0;	/* added for virtual interface */
1524 
1525 	/* Only for OPT_TX_CMD, use BE endpoint. */
1526 	if (cmd_id == WMI_OPT_TX_FRAME_CMDID) {
1527 		ret = ath6kl_wmi_data_hdr_add(wmi, skb, OPT_MSGTYPE,
1528 					      false, false, 0, NULL);
1529 		if (ret) {
1530 			dev_kfree_skb(skb);
1531 			return ret;
1532 		}
1533 		ep_id = ath6kl_ac2_endpoint_id(wmi->parent_dev, WMM_AC_BE);
1534 	}
1535 
1536 	ath6kl_control_tx(wmi->parent_dev, skb, ep_id);
1537 
1538 	if ((sync_flag == SYNC_AFTER_WMIFLAG) ||
1539 	    (sync_flag == SYNC_BOTH_WMIFLAG)) {
1540 		/*
1541 		 * Make sure all new data queued waits for the command to
1542 		 * execute. Establish a new sync point.
1543 		 */
1544 		ath6kl_wmi_sync_point(wmi);
1545 	}
1546 
1547 	return 0;
1548 }
1549 
1550 int ath6kl_wmi_connect_cmd(struct wmi *wmi, enum network_type nw_type,
1551 			   enum dot11_auth_mode dot11_auth_mode,
1552 			   enum auth_mode auth_mode,
1553 			   enum crypto_type pairwise_crypto,
1554 			   u8 pairwise_crypto_len,
1555 			   enum crypto_type group_crypto,
1556 			   u8 group_crypto_len, int ssid_len, u8 *ssid,
1557 			   u8 *bssid, u16 channel, u32 ctrl_flags)
1558 {
1559 	struct sk_buff *skb;
1560 	struct wmi_connect_cmd *cc;
1561 	int ret;
1562 
1563 	wmi->traffic_class = 100;
1564 
1565 	if ((pairwise_crypto == NONE_CRYPT) && (group_crypto != NONE_CRYPT))
1566 		return -EINVAL;
1567 
1568 	if ((pairwise_crypto != NONE_CRYPT) && (group_crypto == NONE_CRYPT))
1569 		return -EINVAL;
1570 
1571 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_connect_cmd));
1572 	if (!skb)
1573 		return -ENOMEM;
1574 
1575 	cc = (struct wmi_connect_cmd *) skb->data;
1576 
1577 	if (ssid_len)
1578 		memcpy(cc->ssid, ssid, ssid_len);
1579 
1580 	cc->ssid_len = ssid_len;
1581 	cc->nw_type = nw_type;
1582 	cc->dot11_auth_mode = dot11_auth_mode;
1583 	cc->auth_mode = auth_mode;
1584 	cc->prwise_crypto_type = pairwise_crypto;
1585 	cc->prwise_crypto_len = pairwise_crypto_len;
1586 	cc->grp_crypto_type = group_crypto;
1587 	cc->grp_crypto_len = group_crypto_len;
1588 	cc->ch = cpu_to_le16(channel);
1589 	cc->ctrl_flags = cpu_to_le32(ctrl_flags);
1590 
1591 	if (bssid != NULL)
1592 		memcpy(cc->bssid, bssid, ETH_ALEN);
1593 
1594 	wmi->pair_crypto_type = pairwise_crypto;
1595 	wmi->grp_crypto_type = group_crypto;
1596 
1597 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_CONNECT_CMDID, NO_SYNC_WMIFLAG);
1598 
1599 	return ret;
1600 }
1601 
1602 int ath6kl_wmi_reconnect_cmd(struct wmi *wmi, u8 *bssid, u16 channel)
1603 {
1604 	struct sk_buff *skb;
1605 	struct wmi_reconnect_cmd *cc;
1606 	int ret;
1607 
1608 	wmi->traffic_class = 100;
1609 
1610 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_reconnect_cmd));
1611 	if (!skb)
1612 		return -ENOMEM;
1613 
1614 	cc = (struct wmi_reconnect_cmd *) skb->data;
1615 	cc->channel = cpu_to_le16(channel);
1616 
1617 	if (bssid != NULL)
1618 		memcpy(cc->bssid, bssid, ETH_ALEN);
1619 
1620 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_RECONNECT_CMDID,
1621 				  NO_SYNC_WMIFLAG);
1622 
1623 	return ret;
1624 }
1625 
1626 int ath6kl_wmi_disconnect_cmd(struct wmi *wmi)
1627 {
1628 	int ret;
1629 
1630 	wmi->traffic_class = 100;
1631 
1632 	/* Disconnect command does not need to do a SYNC before. */
1633 	ret = ath6kl_wmi_simple_cmd(wmi, WMI_DISCONNECT_CMDID);
1634 
1635 	return ret;
1636 }
1637 
1638 int ath6kl_wmi_startscan_cmd(struct wmi *wmi, enum wmi_scan_type scan_type,
1639 			     u32 force_fgscan, u32 is_legacy,
1640 			     u32 home_dwell_time, u32 force_scan_interval,
1641 			     s8 num_chan, u16 *ch_list)
1642 {
1643 	struct sk_buff *skb;
1644 	struct wmi_start_scan_cmd *sc;
1645 	s8 size;
1646 	int i, ret;
1647 
1648 	size = sizeof(struct wmi_start_scan_cmd);
1649 
1650 	if ((scan_type != WMI_LONG_SCAN) && (scan_type != WMI_SHORT_SCAN))
1651 		return -EINVAL;
1652 
1653 	if (num_chan > WMI_MAX_CHANNELS)
1654 		return -EINVAL;
1655 
1656 	if (num_chan)
1657 		size += sizeof(u16) * (num_chan - 1);
1658 
1659 	skb = ath6kl_wmi_get_new_buf(size);
1660 	if (!skb)
1661 		return -ENOMEM;
1662 
1663 	sc = (struct wmi_start_scan_cmd *) skb->data;
1664 	sc->scan_type = scan_type;
1665 	sc->force_fg_scan = cpu_to_le32(force_fgscan);
1666 	sc->is_legacy = cpu_to_le32(is_legacy);
1667 	sc->home_dwell_time = cpu_to_le32(home_dwell_time);
1668 	sc->force_scan_intvl = cpu_to_le32(force_scan_interval);
1669 	sc->num_ch = num_chan;
1670 
1671 	for (i = 0; i < num_chan; i++)
1672 		sc->ch_list[i] = cpu_to_le16(ch_list[i]);
1673 
1674 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_START_SCAN_CMDID,
1675 				  NO_SYNC_WMIFLAG);
1676 
1677 	return ret;
1678 }
1679 
1680 int ath6kl_wmi_scanparams_cmd(struct wmi *wmi, u16 fg_start_sec,
1681 			      u16 fg_end_sec, u16 bg_sec,
1682 			      u16 minact_chdw_msec, u16 maxact_chdw_msec,
1683 			      u16 pas_chdw_msec, u8 short_scan_ratio,
1684 			      u8 scan_ctrl_flag, u32 max_dfsch_act_time,
1685 			      u16 maxact_scan_per_ssid)
1686 {
1687 	struct sk_buff *skb;
1688 	struct wmi_scan_params_cmd *sc;
1689 	int ret;
1690 
1691 	skb = ath6kl_wmi_get_new_buf(sizeof(*sc));
1692 	if (!skb)
1693 		return -ENOMEM;
1694 
1695 	sc = (struct wmi_scan_params_cmd *) skb->data;
1696 	sc->fg_start_period = cpu_to_le16(fg_start_sec);
1697 	sc->fg_end_period = cpu_to_le16(fg_end_sec);
1698 	sc->bg_period = cpu_to_le16(bg_sec);
1699 	sc->minact_chdwell_time = cpu_to_le16(minact_chdw_msec);
1700 	sc->maxact_chdwell_time = cpu_to_le16(maxact_chdw_msec);
1701 	sc->pas_chdwell_time = cpu_to_le16(pas_chdw_msec);
1702 	sc->short_scan_ratio = short_scan_ratio;
1703 	sc->scan_ctrl_flags = scan_ctrl_flag;
1704 	sc->max_dfsch_act_time = cpu_to_le32(max_dfsch_act_time);
1705 	sc->maxact_scan_per_ssid = cpu_to_le16(maxact_scan_per_ssid);
1706 
1707 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_SCAN_PARAMS_CMDID,
1708 				  NO_SYNC_WMIFLAG);
1709 	return ret;
1710 }
1711 
1712 int ath6kl_wmi_bssfilter_cmd(struct wmi *wmi, u8 filter, u32 ie_mask)
1713 {
1714 	struct sk_buff *skb;
1715 	struct wmi_bss_filter_cmd *cmd;
1716 	int ret;
1717 
1718 	if (filter >= LAST_BSS_FILTER)
1719 		return -EINVAL;
1720 
1721 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1722 	if (!skb)
1723 		return -ENOMEM;
1724 
1725 	cmd = (struct wmi_bss_filter_cmd *) skb->data;
1726 	cmd->bss_filter = filter;
1727 	cmd->ie_mask = cpu_to_le32(ie_mask);
1728 
1729 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_BSS_FILTER_CMDID,
1730 				  NO_SYNC_WMIFLAG);
1731 	return ret;
1732 }
1733 
1734 int ath6kl_wmi_probedssid_cmd(struct wmi *wmi, u8 index, u8 flag,
1735 			      u8 ssid_len, u8 *ssid)
1736 {
1737 	struct sk_buff *skb;
1738 	struct wmi_probed_ssid_cmd *cmd;
1739 	int ret;
1740 
1741 	if (index > MAX_PROBED_SSID_INDEX)
1742 		return -EINVAL;
1743 
1744 	if (ssid_len > sizeof(cmd->ssid))
1745 		return -EINVAL;
1746 
1747 	if ((flag & (DISABLE_SSID_FLAG | ANY_SSID_FLAG)) && (ssid_len > 0))
1748 		return -EINVAL;
1749 
1750 	if ((flag & SPECIFIC_SSID_FLAG) && !ssid_len)
1751 		return -EINVAL;
1752 
1753 	if (flag & SPECIFIC_SSID_FLAG)
1754 		wmi->is_probe_ssid = true;
1755 
1756 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1757 	if (!skb)
1758 		return -ENOMEM;
1759 
1760 	cmd = (struct wmi_probed_ssid_cmd *) skb->data;
1761 	cmd->entry_index = index;
1762 	cmd->flag = flag;
1763 	cmd->ssid_len = ssid_len;
1764 	memcpy(cmd->ssid, ssid, ssid_len);
1765 
1766 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_PROBED_SSID_CMDID,
1767 				  NO_SYNC_WMIFLAG);
1768 	return ret;
1769 }
1770 
1771 int ath6kl_wmi_listeninterval_cmd(struct wmi *wmi, u16 listen_interval,
1772 				  u16 listen_beacons)
1773 {
1774 	struct sk_buff *skb;
1775 	struct wmi_listen_int_cmd *cmd;
1776 	int ret;
1777 
1778 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1779 	if (!skb)
1780 		return -ENOMEM;
1781 
1782 	cmd = (struct wmi_listen_int_cmd *) skb->data;
1783 	cmd->listen_intvl = cpu_to_le16(listen_interval);
1784 	cmd->num_beacons = cpu_to_le16(listen_beacons);
1785 
1786 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_LISTEN_INT_CMDID,
1787 				  NO_SYNC_WMIFLAG);
1788 	return ret;
1789 }
1790 
1791 int ath6kl_wmi_powermode_cmd(struct wmi *wmi, u8 pwr_mode)
1792 {
1793 	struct sk_buff *skb;
1794 	struct wmi_power_mode_cmd *cmd;
1795 	int ret;
1796 
1797 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1798 	if (!skb)
1799 		return -ENOMEM;
1800 
1801 	cmd = (struct wmi_power_mode_cmd *) skb->data;
1802 	cmd->pwr_mode = pwr_mode;
1803 	wmi->pwr_mode = pwr_mode;
1804 
1805 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_POWER_MODE_CMDID,
1806 				  NO_SYNC_WMIFLAG);
1807 	return ret;
1808 }
1809 
1810 int ath6kl_wmi_pmparams_cmd(struct wmi *wmi, u16 idle_period,
1811 			    u16 ps_poll_num, u16 dtim_policy,
1812 			    u16 tx_wakeup_policy, u16 num_tx_to_wakeup,
1813 			    u16 ps_fail_event_policy)
1814 {
1815 	struct sk_buff *skb;
1816 	struct wmi_power_params_cmd *pm;
1817 	int ret;
1818 
1819 	skb = ath6kl_wmi_get_new_buf(sizeof(*pm));
1820 	if (!skb)
1821 		return -ENOMEM;
1822 
1823 	pm = (struct wmi_power_params_cmd *)skb->data;
1824 	pm->idle_period = cpu_to_le16(idle_period);
1825 	pm->pspoll_number = cpu_to_le16(ps_poll_num);
1826 	pm->dtim_policy = cpu_to_le16(dtim_policy);
1827 	pm->tx_wakeup_policy = cpu_to_le16(tx_wakeup_policy);
1828 	pm->num_tx_to_wakeup = cpu_to_le16(num_tx_to_wakeup);
1829 	pm->ps_fail_event_policy = cpu_to_le16(ps_fail_event_policy);
1830 
1831 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_POWER_PARAMS_CMDID,
1832 				  NO_SYNC_WMIFLAG);
1833 	return ret;
1834 }
1835 
1836 int ath6kl_wmi_disctimeout_cmd(struct wmi *wmi, u8 timeout)
1837 {
1838 	struct sk_buff *skb;
1839 	struct wmi_disc_timeout_cmd *cmd;
1840 	int ret;
1841 
1842 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1843 	if (!skb)
1844 		return -ENOMEM;
1845 
1846 	cmd = (struct wmi_disc_timeout_cmd *) skb->data;
1847 	cmd->discon_timeout = timeout;
1848 
1849 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_DISC_TIMEOUT_CMDID,
1850 				  NO_SYNC_WMIFLAG);
1851 	return ret;
1852 }
1853 
1854 int ath6kl_wmi_addkey_cmd(struct wmi *wmi, u8 key_index,
1855 			  enum crypto_type key_type,
1856 			  u8 key_usage, u8 key_len,
1857 			  u8 *key_rsc, u8 *key_material,
1858 			  u8 key_op_ctrl, u8 *mac_addr,
1859 			  enum wmi_sync_flag sync_flag)
1860 {
1861 	struct sk_buff *skb;
1862 	struct wmi_add_cipher_key_cmd *cmd;
1863 	int ret;
1864 
1865 	ath6kl_dbg(ATH6KL_DBG_WMI, "addkey cmd: key_index=%u key_type=%d "
1866 		   "key_usage=%d key_len=%d key_op_ctrl=%d\n",
1867 		   key_index, key_type, key_usage, key_len, key_op_ctrl);
1868 
1869 	if ((key_index > WMI_MAX_KEY_INDEX) || (key_len > WMI_MAX_KEY_LEN) ||
1870 	    (key_material == NULL))
1871 		return -EINVAL;
1872 
1873 	if ((WEP_CRYPT != key_type) && (NULL == key_rsc))
1874 		return -EINVAL;
1875 
1876 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1877 	if (!skb)
1878 		return -ENOMEM;
1879 
1880 	cmd = (struct wmi_add_cipher_key_cmd *) skb->data;
1881 	cmd->key_index = key_index;
1882 	cmd->key_type = key_type;
1883 	cmd->key_usage = key_usage;
1884 	cmd->key_len = key_len;
1885 	memcpy(cmd->key, key_material, key_len);
1886 
1887 	if (key_rsc != NULL)
1888 		memcpy(cmd->key_rsc, key_rsc, sizeof(cmd->key_rsc));
1889 
1890 	cmd->key_op_ctrl = key_op_ctrl;
1891 
1892 	if (mac_addr)
1893 		memcpy(cmd->key_mac_addr, mac_addr, ETH_ALEN);
1894 
1895 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_ADD_CIPHER_KEY_CMDID,
1896 				  sync_flag);
1897 
1898 	return ret;
1899 }
1900 
1901 int ath6kl_wmi_add_krk_cmd(struct wmi *wmi, u8 *krk)
1902 {
1903 	struct sk_buff *skb;
1904 	struct wmi_add_krk_cmd *cmd;
1905 	int ret;
1906 
1907 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1908 	if (!skb)
1909 		return -ENOMEM;
1910 
1911 	cmd = (struct wmi_add_krk_cmd *) skb->data;
1912 	memcpy(cmd->krk, krk, WMI_KRK_LEN);
1913 
1914 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_ADD_KRK_CMDID, NO_SYNC_WMIFLAG);
1915 
1916 	return ret;
1917 }
1918 
1919 int ath6kl_wmi_deletekey_cmd(struct wmi *wmi, u8 key_index)
1920 {
1921 	struct sk_buff *skb;
1922 	struct wmi_delete_cipher_key_cmd *cmd;
1923 	int ret;
1924 
1925 	if (key_index > WMI_MAX_KEY_INDEX)
1926 		return -EINVAL;
1927 
1928 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1929 	if (!skb)
1930 		return -ENOMEM;
1931 
1932 	cmd = (struct wmi_delete_cipher_key_cmd *) skb->data;
1933 	cmd->key_index = key_index;
1934 
1935 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_DELETE_CIPHER_KEY_CMDID,
1936 				  NO_SYNC_WMIFLAG);
1937 
1938 	return ret;
1939 }
1940 
1941 int ath6kl_wmi_setpmkid_cmd(struct wmi *wmi, const u8 *bssid,
1942 			    const u8 *pmkid, bool set)
1943 {
1944 	struct sk_buff *skb;
1945 	struct wmi_setpmkid_cmd *cmd;
1946 	int ret;
1947 
1948 	if (bssid == NULL)
1949 		return -EINVAL;
1950 
1951 	if (set && pmkid == NULL)
1952 		return -EINVAL;
1953 
1954 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
1955 	if (!skb)
1956 		return -ENOMEM;
1957 
1958 	cmd = (struct wmi_setpmkid_cmd *) skb->data;
1959 	memcpy(cmd->bssid, bssid, ETH_ALEN);
1960 	if (set) {
1961 		memcpy(cmd->pmkid, pmkid, sizeof(cmd->pmkid));
1962 		cmd->enable = PMKID_ENABLE;
1963 	} else {
1964 		memset(cmd->pmkid, 0, sizeof(cmd->pmkid));
1965 		cmd->enable = PMKID_DISABLE;
1966 	}
1967 
1968 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_PMKID_CMDID,
1969 				  NO_SYNC_WMIFLAG);
1970 
1971 	return ret;
1972 }
1973 
1974 static int ath6kl_wmi_data_sync_send(struct wmi *wmi, struct sk_buff *skb,
1975 			      enum htc_endpoint_id ep_id)
1976 {
1977 	struct wmi_data_hdr *data_hdr;
1978 	int ret;
1979 
1980 	if (WARN_ON(skb == NULL || ep_id == wmi->ep_id))
1981 		return -EINVAL;
1982 
1983 	skb_push(skb, sizeof(struct wmi_data_hdr));
1984 
1985 	data_hdr = (struct wmi_data_hdr *) skb->data;
1986 	data_hdr->info = SYNC_MSGTYPE << WMI_DATA_HDR_MSG_TYPE_SHIFT;
1987 	data_hdr->info3 = 0;
1988 
1989 	ret = ath6kl_control_tx(wmi->parent_dev, skb, ep_id);
1990 
1991 	return ret;
1992 }
1993 
1994 static int ath6kl_wmi_sync_point(struct wmi *wmi)
1995 {
1996 	struct sk_buff *skb;
1997 	struct wmi_sync_cmd *cmd;
1998 	struct wmi_data_sync_bufs data_sync_bufs[WMM_NUM_AC];
1999 	enum htc_endpoint_id ep_id;
2000 	u8 index, num_pri_streams = 0;
2001 	int ret = 0;
2002 
2003 	memset(data_sync_bufs, 0, sizeof(data_sync_bufs));
2004 
2005 	spin_lock_bh(&wmi->lock);
2006 
2007 	for (index = 0; index < WMM_NUM_AC; index++) {
2008 		if (wmi->fat_pipe_exist & (1 << index)) {
2009 			num_pri_streams++;
2010 			data_sync_bufs[num_pri_streams - 1].traffic_class =
2011 			    index;
2012 		}
2013 	}
2014 
2015 	spin_unlock_bh(&wmi->lock);
2016 
2017 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2018 	if (!skb) {
2019 		ret = -ENOMEM;
2020 		goto free_skb;
2021 	}
2022 
2023 	cmd = (struct wmi_sync_cmd *) skb->data;
2024 
2025 	/*
2026 	 * In the SYNC cmd sent on the control Ep, send a bitmap
2027 	 * of the data eps on which the Data Sync will be sent
2028 	 */
2029 	cmd->data_sync_map = wmi->fat_pipe_exist;
2030 
2031 	for (index = 0; index < num_pri_streams; index++) {
2032 		data_sync_bufs[index].skb = ath6kl_buf_alloc(0);
2033 		if (data_sync_bufs[index].skb == NULL) {
2034 			ret = -ENOMEM;
2035 			break;
2036 		}
2037 	}
2038 
2039 	/*
2040 	 * If buffer allocation for any of the dataSync fails,
2041 	 * then do not send the Synchronize cmd on the control ep
2042 	 */
2043 	if (ret)
2044 		goto free_skb;
2045 
2046 	/*
2047 	 * Send sync cmd followed by sync data messages on all
2048 	 * endpoints being used
2049 	 */
2050 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SYNCHRONIZE_CMDID,
2051 				  NO_SYNC_WMIFLAG);
2052 
2053 	if (ret)
2054 		goto free_skb;
2055 
2056 	/* cmd buffer sent, we no longer own it */
2057 	skb = NULL;
2058 
2059 	for (index = 0; index < num_pri_streams; index++) {
2060 
2061 		if (WARN_ON(!data_sync_bufs[index].skb))
2062 			break;
2063 
2064 		ep_id = ath6kl_ac2_endpoint_id(wmi->parent_dev,
2065 					       data_sync_bufs[index].
2066 					       traffic_class);
2067 		ret =
2068 		    ath6kl_wmi_data_sync_send(wmi, data_sync_bufs[index].skb,
2069 					      ep_id);
2070 
2071 		if (ret)
2072 			break;
2073 
2074 		data_sync_bufs[index].skb = NULL;
2075 	}
2076 
2077 free_skb:
2078 	/* free up any resources left over (possibly due to an error) */
2079 	if (skb)
2080 		dev_kfree_skb(skb);
2081 
2082 	for (index = 0; index < num_pri_streams; index++) {
2083 		if (data_sync_bufs[index].skb != NULL) {
2084 			dev_kfree_skb((struct sk_buff *)data_sync_bufs[index].
2085 				      skb);
2086 		}
2087 	}
2088 
2089 	return ret;
2090 }
2091 
2092 int ath6kl_wmi_create_pstream_cmd(struct wmi *wmi,
2093 				  struct wmi_create_pstream_cmd *params)
2094 {
2095 	struct sk_buff *skb;
2096 	struct wmi_create_pstream_cmd *cmd;
2097 	u8 fatpipe_exist_for_ac = 0;
2098 	s32 min_phy = 0;
2099 	s32 nominal_phy = 0;
2100 	int ret;
2101 
2102 	if (!((params->user_pri < 8) &&
2103 	      (params->user_pri <= 0x7) &&
2104 	      (up_to_ac[params->user_pri & 0x7] == params->traffic_class) &&
2105 	      (params->traffic_direc == UPLINK_TRAFFIC ||
2106 	       params->traffic_direc == DNLINK_TRAFFIC ||
2107 	       params->traffic_direc == BIDIR_TRAFFIC) &&
2108 	      (params->traffic_type == TRAFFIC_TYPE_APERIODIC ||
2109 	       params->traffic_type == TRAFFIC_TYPE_PERIODIC) &&
2110 	      (params->voice_psc_cap == DISABLE_FOR_THIS_AC ||
2111 	       params->voice_psc_cap == ENABLE_FOR_THIS_AC ||
2112 	       params->voice_psc_cap == ENABLE_FOR_ALL_AC) &&
2113 	      (params->tsid == WMI_IMPLICIT_PSTREAM ||
2114 	       params->tsid <= WMI_MAX_THINSTREAM))) {
2115 		return -EINVAL;
2116 	}
2117 
2118 	/*
2119 	 * Check nominal PHY rate is >= minimalPHY,
2120 	 * so that DUT can allow TSRS IE
2121 	 */
2122 
2123 	/* Get the physical rate (units of bps) */
2124 	min_phy = ((le32_to_cpu(params->min_phy_rate) / 1000) / 1000);
2125 
2126 	/* Check minimal phy < nominal phy rate */
2127 	if (params->nominal_phy >= min_phy) {
2128 		/* unit of 500 kbps */
2129 		nominal_phy = (params->nominal_phy * 1000) / 500;
2130 		ath6kl_dbg(ATH6KL_DBG_WMI,
2131 			   "TSRS IE enabled::MinPhy %x->NominalPhy ===> %x\n",
2132 			   min_phy, nominal_phy);
2133 
2134 		params->nominal_phy = nominal_phy;
2135 	} else {
2136 		params->nominal_phy = 0;
2137 	}
2138 
2139 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2140 	if (!skb)
2141 		return -ENOMEM;
2142 
2143 	ath6kl_dbg(ATH6KL_DBG_WMI,
2144 		   "sending create_pstream_cmd: ac=%d  tsid:%d\n",
2145 		   params->traffic_class, params->tsid);
2146 
2147 	cmd = (struct wmi_create_pstream_cmd *) skb->data;
2148 	memcpy(cmd, params, sizeof(*cmd));
2149 
2150 	/* This is an implicitly created Fat pipe */
2151 	if ((u32) params->tsid == (u32) WMI_IMPLICIT_PSTREAM) {
2152 		spin_lock_bh(&wmi->lock);
2153 		fatpipe_exist_for_ac = (wmi->fat_pipe_exist &
2154 					(1 << params->traffic_class));
2155 		wmi->fat_pipe_exist |= (1 << params->traffic_class);
2156 		spin_unlock_bh(&wmi->lock);
2157 	} else {
2158 		/* explicitly created thin stream within a fat pipe */
2159 		spin_lock_bh(&wmi->lock);
2160 		fatpipe_exist_for_ac = (wmi->fat_pipe_exist &
2161 					(1 << params->traffic_class));
2162 		wmi->stream_exist_for_ac[params->traffic_class] |=
2163 		    (1 << params->tsid);
2164 		/*
2165 		 * If a thinstream becomes active, the fat pipe automatically
2166 		 * becomes active
2167 		 */
2168 		wmi->fat_pipe_exist |= (1 << params->traffic_class);
2169 		spin_unlock_bh(&wmi->lock);
2170 	}
2171 
2172 	/*
2173 	 * Indicate activty change to driver layer only if this is the
2174 	 * first TSID to get created in this AC explicitly or an implicit
2175 	 * fat pipe is getting created.
2176 	 */
2177 	if (!fatpipe_exist_for_ac)
2178 		ath6kl_indicate_tx_activity(wmi->parent_dev,
2179 					    params->traffic_class, true);
2180 
2181 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_CREATE_PSTREAM_CMDID,
2182 				  NO_SYNC_WMIFLAG);
2183 	return ret;
2184 }
2185 
2186 int ath6kl_wmi_delete_pstream_cmd(struct wmi *wmi, u8 traffic_class, u8 tsid)
2187 {
2188 	struct sk_buff *skb;
2189 	struct wmi_delete_pstream_cmd *cmd;
2190 	u16 active_tsids = 0;
2191 	int ret;
2192 
2193 	if (traffic_class > 3) {
2194 		ath6kl_err("invalid traffic class: %d\n", traffic_class);
2195 		return -EINVAL;
2196 	}
2197 
2198 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2199 	if (!skb)
2200 		return -ENOMEM;
2201 
2202 	cmd = (struct wmi_delete_pstream_cmd *) skb->data;
2203 	cmd->traffic_class = traffic_class;
2204 	cmd->tsid = tsid;
2205 
2206 	spin_lock_bh(&wmi->lock);
2207 	active_tsids = wmi->stream_exist_for_ac[traffic_class];
2208 	spin_unlock_bh(&wmi->lock);
2209 
2210 	if (!(active_tsids & (1 << tsid))) {
2211 		dev_kfree_skb(skb);
2212 		ath6kl_dbg(ATH6KL_DBG_WMI,
2213 			   "TSID %d doesn't exist for traffic class: %d\n",
2214 			   tsid, traffic_class);
2215 		return -ENODATA;
2216 	}
2217 
2218 	ath6kl_dbg(ATH6KL_DBG_WMI,
2219 		   "sending delete_pstream_cmd: traffic class: %d tsid=%d\n",
2220 		   traffic_class, tsid);
2221 
2222 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_DELETE_PSTREAM_CMDID,
2223 				  SYNC_BEFORE_WMIFLAG);
2224 
2225 	spin_lock_bh(&wmi->lock);
2226 	wmi->stream_exist_for_ac[traffic_class] &= ~(1 << tsid);
2227 	active_tsids = wmi->stream_exist_for_ac[traffic_class];
2228 	spin_unlock_bh(&wmi->lock);
2229 
2230 	/*
2231 	 * Indicate stream inactivity to driver layer only if all tsids
2232 	 * within this AC are deleted.
2233 	 */
2234 	if (!active_tsids) {
2235 		ath6kl_indicate_tx_activity(wmi->parent_dev,
2236 					    traffic_class, false);
2237 		wmi->fat_pipe_exist &= ~(1 << traffic_class);
2238 	}
2239 
2240 	return ret;
2241 }
2242 
2243 int ath6kl_wmi_set_ip_cmd(struct wmi *wmi, struct wmi_set_ip_cmd *ip_cmd)
2244 {
2245 	struct sk_buff *skb;
2246 	struct wmi_set_ip_cmd *cmd;
2247 	int ret;
2248 
2249 	/* Multicast address are not valid */
2250 	if ((*((u8 *) &ip_cmd->ips[0]) >= 0xE0) ||
2251 	    (*((u8 *) &ip_cmd->ips[1]) >= 0xE0))
2252 		return -EINVAL;
2253 
2254 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_ip_cmd));
2255 	if (!skb)
2256 		return -ENOMEM;
2257 
2258 	cmd = (struct wmi_set_ip_cmd *) skb->data;
2259 	memcpy(cmd, ip_cmd, sizeof(struct wmi_set_ip_cmd));
2260 
2261 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_IP_CMDID, NO_SYNC_WMIFLAG);
2262 	return ret;
2263 }
2264 
2265 static int ath6kl_wmi_get_wow_list_event_rx(struct wmi *wmi, u8 * datap,
2266 					    int len)
2267 {
2268 	if (len < sizeof(struct wmi_get_wow_list_reply))
2269 		return -EINVAL;
2270 
2271 	return 0;
2272 }
2273 
2274 static int ath6kl_wmi_cmd_send_xtnd(struct wmi *wmi, struct sk_buff *skb,
2275 				    enum wmix_command_id cmd_id,
2276 				    enum wmi_sync_flag sync_flag)
2277 {
2278 	struct wmix_cmd_hdr *cmd_hdr;
2279 	int ret;
2280 
2281 	skb_push(skb, sizeof(struct wmix_cmd_hdr));
2282 
2283 	cmd_hdr = (struct wmix_cmd_hdr *) skb->data;
2284 	cmd_hdr->cmd_id = cpu_to_le32(cmd_id);
2285 
2286 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_EXTENSION_CMDID, sync_flag);
2287 
2288 	return ret;
2289 }
2290 
2291 int ath6kl_wmi_get_challenge_resp_cmd(struct wmi *wmi, u32 cookie, u32 source)
2292 {
2293 	struct sk_buff *skb;
2294 	struct wmix_hb_challenge_resp_cmd *cmd;
2295 	int ret;
2296 
2297 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2298 	if (!skb)
2299 		return -ENOMEM;
2300 
2301 	cmd = (struct wmix_hb_challenge_resp_cmd *) skb->data;
2302 	cmd->cookie = cpu_to_le32(cookie);
2303 	cmd->source = cpu_to_le32(source);
2304 
2305 	ret = ath6kl_wmi_cmd_send_xtnd(wmi, skb, WMIX_HB_CHALLENGE_RESP_CMDID,
2306 				       NO_SYNC_WMIFLAG);
2307 	return ret;
2308 }
2309 
2310 int ath6kl_wmi_config_debug_module_cmd(struct wmi *wmi, u32 valid, u32 config)
2311 {
2312 	struct ath6kl_wmix_dbglog_cfg_module_cmd *cmd;
2313 	struct sk_buff *skb;
2314 	int ret;
2315 
2316 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2317 	if (!skb)
2318 		return -ENOMEM;
2319 
2320 	cmd = (struct ath6kl_wmix_dbglog_cfg_module_cmd *) skb->data;
2321 	cmd->valid = cpu_to_le32(valid);
2322 	cmd->config = cpu_to_le32(config);
2323 
2324 	ret = ath6kl_wmi_cmd_send_xtnd(wmi, skb, WMIX_DBGLOG_CFG_MODULE_CMDID,
2325 				       NO_SYNC_WMIFLAG);
2326 	return ret;
2327 }
2328 
2329 int ath6kl_wmi_get_stats_cmd(struct wmi *wmi)
2330 {
2331 	return ath6kl_wmi_simple_cmd(wmi, WMI_GET_STATISTICS_CMDID);
2332 }
2333 
2334 int ath6kl_wmi_set_tx_pwr_cmd(struct wmi *wmi, u8 dbM)
2335 {
2336 	struct sk_buff *skb;
2337 	struct wmi_set_tx_pwr_cmd *cmd;
2338 	int ret;
2339 
2340 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_tx_pwr_cmd));
2341 	if (!skb)
2342 		return -ENOMEM;
2343 
2344 	cmd = (struct wmi_set_tx_pwr_cmd *) skb->data;
2345 	cmd->dbM = dbM;
2346 
2347 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_TX_PWR_CMDID,
2348 				  NO_SYNC_WMIFLAG);
2349 
2350 	return ret;
2351 }
2352 
2353 int ath6kl_wmi_get_tx_pwr_cmd(struct wmi *wmi)
2354 {
2355 	return ath6kl_wmi_simple_cmd(wmi, WMI_GET_TX_PWR_CMDID);
2356 }
2357 
2358 int ath6kl_wmi_set_lpreamble_cmd(struct wmi *wmi, u8 status, u8 preamble_policy)
2359 {
2360 	struct sk_buff *skb;
2361 	struct wmi_set_lpreamble_cmd *cmd;
2362 	int ret;
2363 
2364 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_lpreamble_cmd));
2365 	if (!skb)
2366 		return -ENOMEM;
2367 
2368 	cmd = (struct wmi_set_lpreamble_cmd *) skb->data;
2369 	cmd->status = status;
2370 	cmd->preamble_policy = preamble_policy;
2371 
2372 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_LPREAMBLE_CMDID,
2373 				  NO_SYNC_WMIFLAG);
2374 	return ret;
2375 }
2376 
2377 int ath6kl_wmi_set_rts_cmd(struct wmi *wmi, u16 threshold)
2378 {
2379 	struct sk_buff *skb;
2380 	struct wmi_set_rts_cmd *cmd;
2381 	int ret;
2382 
2383 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_rts_cmd));
2384 	if (!skb)
2385 		return -ENOMEM;
2386 
2387 	cmd = (struct wmi_set_rts_cmd *) skb->data;
2388 	cmd->threshold = cpu_to_le16(threshold);
2389 
2390 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_RTS_CMDID, NO_SYNC_WMIFLAG);
2391 	return ret;
2392 }
2393 
2394 int ath6kl_wmi_set_wmm_txop(struct wmi *wmi, enum wmi_txop_cfg cfg)
2395 {
2396 	struct sk_buff *skb;
2397 	struct wmi_set_wmm_txop_cmd *cmd;
2398 	int ret;
2399 
2400 	if (!((cfg == WMI_TXOP_DISABLED) || (cfg == WMI_TXOP_ENABLED)))
2401 		return -EINVAL;
2402 
2403 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_set_wmm_txop_cmd));
2404 	if (!skb)
2405 		return -ENOMEM;
2406 
2407 	cmd = (struct wmi_set_wmm_txop_cmd *) skb->data;
2408 	cmd->txop_enable = cfg;
2409 
2410 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_WMM_TXOP_CMDID,
2411 				  NO_SYNC_WMIFLAG);
2412 	return ret;
2413 }
2414 
2415 int ath6kl_wmi_set_keepalive_cmd(struct wmi *wmi, u8 keep_alive_intvl)
2416 {
2417 	struct sk_buff *skb;
2418 	struct wmi_set_keepalive_cmd *cmd;
2419 	int ret;
2420 
2421 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2422 	if (!skb)
2423 		return -ENOMEM;
2424 
2425 	cmd = (struct wmi_set_keepalive_cmd *) skb->data;
2426 	cmd->keep_alive_intvl = keep_alive_intvl;
2427 	wmi->keep_alive_intvl = keep_alive_intvl;
2428 
2429 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_KEEPALIVE_CMDID,
2430 				  NO_SYNC_WMIFLAG);
2431 	return ret;
2432 }
2433 
2434 int ath6kl_wmi_test_cmd(struct wmi *wmi, void *buf, size_t len)
2435 {
2436 	struct sk_buff *skb;
2437 	int ret;
2438 
2439 	skb = ath6kl_wmi_get_new_buf(len);
2440 	if (!skb)
2441 		return -ENOMEM;
2442 
2443 	memcpy(skb->data, buf, len);
2444 
2445 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_TEST_CMDID, NO_SYNC_WMIFLAG);
2446 
2447 	return ret;
2448 }
2449 
2450 
2451 s32 ath6kl_wmi_get_rate(s8 rate_index)
2452 {
2453 	if (rate_index == RATE_AUTO)
2454 		return 0;
2455 
2456 	return wmi_rate_tbl[(u32) rate_index][0];
2457 }
2458 
2459 static int ath6kl_wmi_get_pmkid_list_event_rx(struct wmi *wmi, u8 *datap,
2460 					      u32 len)
2461 {
2462 	struct wmi_pmkid_list_reply *reply;
2463 	u32 expected_len;
2464 
2465 	if (len < sizeof(struct wmi_pmkid_list_reply))
2466 		return -EINVAL;
2467 
2468 	reply = (struct wmi_pmkid_list_reply *)datap;
2469 	expected_len = sizeof(reply->num_pmkid) +
2470 		le32_to_cpu(reply->num_pmkid) * WMI_PMKID_LEN;
2471 
2472 	if (len < expected_len)
2473 		return -EINVAL;
2474 
2475 	return 0;
2476 }
2477 
2478 static int ath6kl_wmi_addba_req_event_rx(struct wmi *wmi, u8 *datap, int len)
2479 {
2480 	struct wmi_addba_req_event *cmd = (struct wmi_addba_req_event *) datap;
2481 
2482 	aggr_recv_addba_req_evt(wmi->parent_dev, cmd->tid,
2483 				le16_to_cpu(cmd->st_seq_no), cmd->win_sz);
2484 
2485 	return 0;
2486 }
2487 
2488 static int ath6kl_wmi_delba_req_event_rx(struct wmi *wmi, u8 *datap, int len)
2489 {
2490 	struct wmi_delba_event *cmd = (struct wmi_delba_event *) datap;
2491 
2492 	aggr_recv_delba_req_evt(wmi->parent_dev, cmd->tid);
2493 
2494 	return 0;
2495 }
2496 
2497 /*  AP mode functions */
2498 
2499 int ath6kl_wmi_ap_profile_commit(struct wmi *wmip, struct wmi_connect_cmd *p)
2500 {
2501 	struct sk_buff *skb;
2502 	struct wmi_connect_cmd *cm;
2503 	int res;
2504 
2505 	skb = ath6kl_wmi_get_new_buf(sizeof(*cm));
2506 	if (!skb)
2507 		return -ENOMEM;
2508 
2509 	cm = (struct wmi_connect_cmd *) skb->data;
2510 	memcpy(cm, p, sizeof(*cm));
2511 
2512 	res = ath6kl_wmi_cmd_send(wmip, skb, WMI_AP_CONFIG_COMMIT_CMDID,
2513 				  NO_SYNC_WMIFLAG);
2514 	ath6kl_dbg(ATH6KL_DBG_WMI, "%s: nw_type=%u auth_mode=%u ch=%u "
2515 		   "ctrl_flags=0x%x-> res=%d\n",
2516 		   __func__, p->nw_type, p->auth_mode, le16_to_cpu(p->ch),
2517 		   le32_to_cpu(p->ctrl_flags), res);
2518 	return res;
2519 }
2520 
2521 int ath6kl_wmi_ap_set_mlme(struct wmi *wmip, u8 cmd, const u8 *mac, u16 reason)
2522 {
2523 	struct sk_buff *skb;
2524 	struct wmi_ap_set_mlme_cmd *cm;
2525 
2526 	skb = ath6kl_wmi_get_new_buf(sizeof(*cm));
2527 	if (!skb)
2528 		return -ENOMEM;
2529 
2530 	cm = (struct wmi_ap_set_mlme_cmd *) skb->data;
2531 	memcpy(cm->mac, mac, ETH_ALEN);
2532 	cm->reason = cpu_to_le16(reason);
2533 	cm->cmd = cmd;
2534 
2535 	return ath6kl_wmi_cmd_send(wmip, skb, WMI_AP_SET_MLME_CMDID,
2536 				   NO_SYNC_WMIFLAG);
2537 }
2538 
2539 static int ath6kl_wmi_pspoll_event_rx(struct wmi *wmi, u8 *datap, int len)
2540 {
2541 	struct wmi_pspoll_event *ev;
2542 
2543 	if (len < sizeof(struct wmi_pspoll_event))
2544 		return -EINVAL;
2545 
2546 	ev = (struct wmi_pspoll_event *) datap;
2547 
2548 	ath6kl_pspoll_event(wmi->parent_dev, le16_to_cpu(ev->aid));
2549 
2550 	return 0;
2551 }
2552 
2553 static int ath6kl_wmi_dtimexpiry_event_rx(struct wmi *wmi, u8 *datap, int len)
2554 {
2555 	ath6kl_dtimexpiry_event(wmi->parent_dev);
2556 
2557 	return 0;
2558 }
2559 
2560 int ath6kl_wmi_set_pvb_cmd(struct wmi *wmi, u16 aid, bool flag)
2561 {
2562 	struct sk_buff *skb;
2563 	struct wmi_ap_set_pvb_cmd *cmd;
2564 	int ret;
2565 
2566 	skb = ath6kl_wmi_get_new_buf(sizeof(struct wmi_ap_set_pvb_cmd));
2567 	if (!skb)
2568 		return -ENOMEM;
2569 
2570 	cmd = (struct wmi_ap_set_pvb_cmd *) skb->data;
2571 	cmd->aid = cpu_to_le16(aid);
2572 	cmd->rsvd = cpu_to_le16(0);
2573 	cmd->flag = cpu_to_le32(flag);
2574 
2575 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_AP_SET_PVB_CMDID,
2576 				  NO_SYNC_WMIFLAG);
2577 
2578 	return 0;
2579 }
2580 
2581 int ath6kl_wmi_set_rx_frame_format_cmd(struct wmi *wmi, u8 rx_meta_ver,
2582 				       bool rx_dot11_hdr, bool defrag_on_host)
2583 {
2584 	struct sk_buff *skb;
2585 	struct wmi_rx_frame_format_cmd *cmd;
2586 	int ret;
2587 
2588 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2589 	if (!skb)
2590 		return -ENOMEM;
2591 
2592 	cmd = (struct wmi_rx_frame_format_cmd *) skb->data;
2593 	cmd->dot11_hdr = rx_dot11_hdr ? 1 : 0;
2594 	cmd->defrag_on_host = defrag_on_host ? 1 : 0;
2595 	cmd->meta_ver = rx_meta_ver;
2596 
2597 	/* Delete the local aggr state, on host */
2598 	ret = ath6kl_wmi_cmd_send(wmi, skb, WMI_RX_FRAME_FORMAT_CMDID,
2599 				  NO_SYNC_WMIFLAG);
2600 
2601 	return ret;
2602 }
2603 
2604 int ath6kl_wmi_set_appie_cmd(struct wmi *wmi, u8 mgmt_frm_type, const u8 *ie,
2605 			     u8 ie_len)
2606 {
2607 	struct sk_buff *skb;
2608 	struct wmi_set_appie_cmd *p;
2609 
2610 	skb = ath6kl_wmi_get_new_buf(sizeof(*p) + ie_len);
2611 	if (!skb)
2612 		return -ENOMEM;
2613 
2614 	ath6kl_dbg(ATH6KL_DBG_WMI, "set_appie_cmd: mgmt_frm_type=%u "
2615 		   "ie_len=%u\n", mgmt_frm_type, ie_len);
2616 	p = (struct wmi_set_appie_cmd *) skb->data;
2617 	p->mgmt_frm_type = mgmt_frm_type;
2618 	p->ie_len = ie_len;
2619 	memcpy(p->ie_info, ie, ie_len);
2620 	return ath6kl_wmi_cmd_send(wmi, skb, WMI_SET_APPIE_CMDID,
2621 				   NO_SYNC_WMIFLAG);
2622 }
2623 
2624 int ath6kl_wmi_disable_11b_rates_cmd(struct wmi *wmi, bool disable)
2625 {
2626 	struct sk_buff *skb;
2627 	struct wmi_disable_11b_rates_cmd *cmd;
2628 
2629 	skb = ath6kl_wmi_get_new_buf(sizeof(*cmd));
2630 	if (!skb)
2631 		return -ENOMEM;
2632 
2633 	ath6kl_dbg(ATH6KL_DBG_WMI, "disable_11b_rates_cmd: disable=%u\n",
2634 		   disable);
2635 	cmd = (struct wmi_disable_11b_rates_cmd *) skb->data;
2636 	cmd->disable = disable ? 1 : 0;
2637 
2638 	return ath6kl_wmi_cmd_send(wmi, skb, WMI_DISABLE_11B_RATES_CMDID,
2639 				   NO_SYNC_WMIFLAG);
2640 }
2641 
2642 int ath6kl_wmi_remain_on_chnl_cmd(struct wmi *wmi, u32 freq, u32 dur)
2643 {
2644 	struct sk_buff *skb;
2645 	struct wmi_remain_on_chnl_cmd *p;
2646 
2647 	skb = ath6kl_wmi_get_new_buf(sizeof(*p));
2648 	if (!skb)
2649 		return -ENOMEM;
2650 
2651 	ath6kl_dbg(ATH6KL_DBG_WMI, "remain_on_chnl_cmd: freq=%u dur=%u\n",
2652 		   freq, dur);
2653 	p = (struct wmi_remain_on_chnl_cmd *) skb->data;
2654 	p->freq = cpu_to_le32(freq);
2655 	p->duration = cpu_to_le32(dur);
2656 	return ath6kl_wmi_cmd_send(wmi, skb, WMI_REMAIN_ON_CHNL_CMDID,
2657 				   NO_SYNC_WMIFLAG);
2658 }
2659 
2660 int ath6kl_wmi_send_action_cmd(struct wmi *wmi, u32 id, u32 freq, u32 wait,
2661 			       const u8 *data, u16 data_len)
2662 {
2663 	struct sk_buff *skb;
2664 	struct wmi_send_action_cmd *p;
2665 	u8 *buf;
2666 
2667 	if (wait)
2668 		return -EINVAL; /* Offload for wait not supported */
2669 
2670 	buf = kmalloc(data_len, GFP_KERNEL);
2671 	if (!buf)
2672 		return -ENOMEM;
2673 
2674 	skb = ath6kl_wmi_get_new_buf(sizeof(*p) + data_len);
2675 	if (!skb) {
2676 		kfree(buf);
2677 		return -ENOMEM;
2678 	}
2679 
2680 	kfree(wmi->last_mgmt_tx_frame);
2681 	wmi->last_mgmt_tx_frame = buf;
2682 	wmi->last_mgmt_tx_frame_len = data_len;
2683 
2684 	ath6kl_dbg(ATH6KL_DBG_WMI, "send_action_cmd: id=%u freq=%u wait=%u "
2685 		   "len=%u\n", id, freq, wait, data_len);
2686 	p = (struct wmi_send_action_cmd *) skb->data;
2687 	p->id = cpu_to_le32(id);
2688 	p->freq = cpu_to_le32(freq);
2689 	p->wait = cpu_to_le32(wait);
2690 	p->len = cpu_to_le16(data_len);
2691 	memcpy(p->data, data, data_len);
2692 	return ath6kl_wmi_cmd_send(wmi, skb, WMI_SEND_ACTION_CMDID,
2693 				   NO_SYNC_WMIFLAG);
2694 }
2695 
2696 int ath6kl_wmi_send_probe_response_cmd(struct wmi *wmi, u32 freq,
2697 				       const u8 *dst,
2698 				       const u8 *data, u16 data_len)
2699 {
2700 	struct sk_buff *skb;
2701 	struct wmi_p2p_probe_response_cmd *p;
2702 
2703 	skb = ath6kl_wmi_get_new_buf(sizeof(*p) + data_len);
2704 	if (!skb)
2705 		return -ENOMEM;
2706 
2707 	ath6kl_dbg(ATH6KL_DBG_WMI, "send_probe_response_cmd: freq=%u dst=%pM "
2708 		   "len=%u\n", freq, dst, data_len);
2709 	p = (struct wmi_p2p_probe_response_cmd *) skb->data;
2710 	p->freq = cpu_to_le32(freq);
2711 	memcpy(p->destination_addr, dst, ETH_ALEN);
2712 	p->len = cpu_to_le16(data_len);
2713 	memcpy(p->data, data, data_len);
2714 	return ath6kl_wmi_cmd_send(wmi, skb, WMI_SEND_PROBE_RESPONSE_CMDID,
2715 				   NO_SYNC_WMIFLAG);
2716 }
2717 
2718 int ath6kl_wmi_probe_report_req_cmd(struct wmi *wmi, bool enable)
2719 {
2720 	struct sk_buff *skb;
2721 	struct wmi_probe_req_report_cmd *p;
2722 
2723 	skb = ath6kl_wmi_get_new_buf(sizeof(*p));
2724 	if (!skb)
2725 		return -ENOMEM;
2726 
2727 	ath6kl_dbg(ATH6KL_DBG_WMI, "probe_report_req_cmd: enable=%u\n",
2728 		   enable);
2729 	p = (struct wmi_probe_req_report_cmd *) skb->data;
2730 	p->enable = enable ? 1 : 0;
2731 	return ath6kl_wmi_cmd_send(wmi, skb, WMI_PROBE_REQ_REPORT_CMDID,
2732 				   NO_SYNC_WMIFLAG);
2733 }
2734 
2735 int ath6kl_wmi_info_req_cmd(struct wmi *wmi, u32 info_req_flags)
2736 {
2737 	struct sk_buff *skb;
2738 	struct wmi_get_p2p_info *p;
2739 
2740 	skb = ath6kl_wmi_get_new_buf(sizeof(*p));
2741 	if (!skb)
2742 		return -ENOMEM;
2743 
2744 	ath6kl_dbg(ATH6KL_DBG_WMI, "info_req_cmd: flags=%x\n",
2745 		   info_req_flags);
2746 	p = (struct wmi_get_p2p_info *) skb->data;
2747 	p->info_req_flags = cpu_to_le32(info_req_flags);
2748 	return ath6kl_wmi_cmd_send(wmi, skb, WMI_GET_P2P_INFO_CMDID,
2749 				   NO_SYNC_WMIFLAG);
2750 }
2751 
2752 int ath6kl_wmi_cancel_remain_on_chnl_cmd(struct wmi *wmi)
2753 {
2754 	ath6kl_dbg(ATH6KL_DBG_WMI, "cancel_remain_on_chnl_cmd\n");
2755 	return ath6kl_wmi_simple_cmd(wmi, WMI_CANCEL_REMAIN_ON_CHNL_CMDID);
2756 }
2757 
2758 static int ath6kl_wmi_control_rx_xtnd(struct wmi *wmi, struct sk_buff *skb)
2759 {
2760 	struct wmix_cmd_hdr *cmd;
2761 	u32 len;
2762 	u16 id;
2763 	u8 *datap;
2764 	int ret = 0;
2765 
2766 	if (skb->len < sizeof(struct wmix_cmd_hdr)) {
2767 		ath6kl_err("bad packet 1\n");
2768 		wmi->stat.cmd_len_err++;
2769 		return -EINVAL;
2770 	}
2771 
2772 	cmd = (struct wmix_cmd_hdr *) skb->data;
2773 	id = le32_to_cpu(cmd->cmd_id);
2774 
2775 	skb_pull(skb, sizeof(struct wmix_cmd_hdr));
2776 
2777 	datap = skb->data;
2778 	len = skb->len;
2779 
2780 	switch (id) {
2781 	case WMIX_HB_CHALLENGE_RESP_EVENTID:
2782 		break;
2783 	case WMIX_DBGLOG_EVENTID:
2784 		ath6kl_debug_fwlog_event(wmi->parent_dev, datap, len);
2785 		break;
2786 	default:
2787 		ath6kl_err("unknown cmd id 0x%x\n", id);
2788 		wmi->stat.cmd_id_err++;
2789 		ret = -EINVAL;
2790 		break;
2791 	}
2792 
2793 	return ret;
2794 }
2795 
2796 /* Control Path */
2797 int ath6kl_wmi_control_rx(struct wmi *wmi, struct sk_buff *skb)
2798 {
2799 	struct wmi_cmd_hdr *cmd;
2800 	u32 len;
2801 	u16 id;
2802 	u8 *datap;
2803 	int ret = 0;
2804 
2805 	if (WARN_ON(skb == NULL))
2806 		return -EINVAL;
2807 
2808 	if (skb->len < sizeof(struct wmi_cmd_hdr)) {
2809 		ath6kl_err("bad packet 1\n");
2810 		dev_kfree_skb(skb);
2811 		wmi->stat.cmd_len_err++;
2812 		return -EINVAL;
2813 	}
2814 
2815 	cmd = (struct wmi_cmd_hdr *) skb->data;
2816 	id = le16_to_cpu(cmd->cmd_id);
2817 
2818 	skb_pull(skb, sizeof(struct wmi_cmd_hdr));
2819 
2820 	datap = skb->data;
2821 	len = skb->len;
2822 
2823 	ath6kl_dbg(ATH6KL_DBG_WMI, "%s: wmi id: %d\n", __func__, id);
2824 	ath6kl_dbg_dump(ATH6KL_DBG_RAW_BYTES, "msg payload ", datap, len);
2825 
2826 	switch (id) {
2827 	case WMI_GET_BITRATE_CMDID:
2828 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_BITRATE_CMDID\n");
2829 		ret = ath6kl_wmi_bitrate_reply_rx(wmi, datap, len);
2830 		break;
2831 	case WMI_GET_CHANNEL_LIST_CMDID:
2832 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_CHANNEL_LIST_CMDID\n");
2833 		ret = ath6kl_wmi_ch_list_reply_rx(wmi, datap, len);
2834 		break;
2835 	case WMI_GET_TX_PWR_CMDID:
2836 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_TX_PWR_CMDID\n");
2837 		ret = ath6kl_wmi_tx_pwr_reply_rx(wmi, datap, len);
2838 		break;
2839 	case WMI_READY_EVENTID:
2840 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_READY_EVENTID\n");
2841 		ret = ath6kl_wmi_ready_event_rx(wmi, datap, len);
2842 		break;
2843 	case WMI_CONNECT_EVENTID:
2844 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_CONNECT_EVENTID\n");
2845 		ret = ath6kl_wmi_connect_event_rx(wmi, datap, len);
2846 		break;
2847 	case WMI_DISCONNECT_EVENTID:
2848 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_DISCONNECT_EVENTID\n");
2849 		ret = ath6kl_wmi_disconnect_event_rx(wmi, datap, len);
2850 		break;
2851 	case WMI_PEER_NODE_EVENTID:
2852 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_PEER_NODE_EVENTID\n");
2853 		ret = ath6kl_wmi_peer_node_event_rx(wmi, datap, len);
2854 		break;
2855 	case WMI_TKIP_MICERR_EVENTID:
2856 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TKIP_MICERR_EVENTID\n");
2857 		ret = ath6kl_wmi_tkip_micerr_event_rx(wmi, datap, len);
2858 		break;
2859 	case WMI_BSSINFO_EVENTID:
2860 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_BSSINFO_EVENTID\n");
2861 		ret = ath6kl_wmi_bssinfo_event_rx(wmi, datap, len);
2862 		break;
2863 	case WMI_REGDOMAIN_EVENTID:
2864 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REGDOMAIN_EVENTID\n");
2865 		ath6kl_wmi_regdomain_event(wmi, datap, len);
2866 		break;
2867 	case WMI_PSTREAM_TIMEOUT_EVENTID:
2868 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_PSTREAM_TIMEOUT_EVENTID\n");
2869 		ret = ath6kl_wmi_pstream_timeout_event_rx(wmi, datap, len);
2870 		break;
2871 	case WMI_NEIGHBOR_REPORT_EVENTID:
2872 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_NEIGHBOR_REPORT_EVENTID\n");
2873 		break;
2874 	case WMI_SCAN_COMPLETE_EVENTID:
2875 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_SCAN_COMPLETE_EVENTID\n");
2876 		ret = ath6kl_wmi_scan_complete_rx(wmi, datap, len);
2877 		break;
2878 	case WMI_CMDERROR_EVENTID:
2879 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_CMDERROR_EVENTID\n");
2880 		ret = ath6kl_wmi_error_event_rx(wmi, datap, len);
2881 		break;
2882 	case WMI_REPORT_STATISTICS_EVENTID:
2883 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REPORT_STATISTICS_EVENTID\n");
2884 		ret = ath6kl_wmi_stats_event_rx(wmi, datap, len);
2885 		break;
2886 	case WMI_RSSI_THRESHOLD_EVENTID:
2887 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_RSSI_THRESHOLD_EVENTID\n");
2888 		ret = ath6kl_wmi_rssi_threshold_event_rx(wmi, datap, len);
2889 		break;
2890 	case WMI_ERROR_REPORT_EVENTID:
2891 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_ERROR_REPORT_EVENTID\n");
2892 		break;
2893 	case WMI_OPT_RX_FRAME_EVENTID:
2894 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_OPT_RX_FRAME_EVENTID\n");
2895 		/* this event has been deprecated */
2896 		break;
2897 	case WMI_REPORT_ROAM_TBL_EVENTID:
2898 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REPORT_ROAM_TBL_EVENTID\n");
2899 		break;
2900 	case WMI_EXTENSION_EVENTID:
2901 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_EXTENSION_EVENTID\n");
2902 		ret = ath6kl_wmi_control_rx_xtnd(wmi, skb);
2903 		break;
2904 	case WMI_CAC_EVENTID:
2905 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_CAC_EVENTID\n");
2906 		ret = ath6kl_wmi_cac_event_rx(wmi, datap, len);
2907 		break;
2908 	case WMI_CHANNEL_CHANGE_EVENTID:
2909 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_CHANNEL_CHANGE_EVENTID\n");
2910 		break;
2911 	case WMI_REPORT_ROAM_DATA_EVENTID:
2912 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REPORT_ROAM_DATA_EVENTID\n");
2913 		break;
2914 	case WMI_TEST_EVENTID:
2915 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TEST_EVENTID\n");
2916 		ret = ath6kl_wmi_tcmd_test_report_rx(wmi, datap, len);
2917 		break;
2918 	case WMI_GET_FIXRATES_CMDID:
2919 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_FIXRATES_CMDID\n");
2920 		ret = ath6kl_wmi_ratemask_reply_rx(wmi, datap, len);
2921 		break;
2922 	case WMI_TX_RETRY_ERR_EVENTID:
2923 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TX_RETRY_ERR_EVENTID\n");
2924 		break;
2925 	case WMI_SNR_THRESHOLD_EVENTID:
2926 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_SNR_THRESHOLD_EVENTID\n");
2927 		ret = ath6kl_wmi_snr_threshold_event_rx(wmi, datap, len);
2928 		break;
2929 	case WMI_LQ_THRESHOLD_EVENTID:
2930 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_LQ_THRESHOLD_EVENTID\n");
2931 		break;
2932 	case WMI_APLIST_EVENTID:
2933 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_APLIST_EVENTID\n");
2934 		ret = ath6kl_wmi_aplist_event_rx(wmi, datap, len);
2935 		break;
2936 	case WMI_GET_KEEPALIVE_CMDID:
2937 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_KEEPALIVE_CMDID\n");
2938 		ret = ath6kl_wmi_keepalive_reply_rx(wmi, datap, len);
2939 		break;
2940 	case WMI_GET_WOW_LIST_EVENTID:
2941 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_WOW_LIST_EVENTID\n");
2942 		ret = ath6kl_wmi_get_wow_list_event_rx(wmi, datap, len);
2943 		break;
2944 	case WMI_GET_PMKID_LIST_EVENTID:
2945 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_GET_PMKID_LIST_EVENTID\n");
2946 		ret = ath6kl_wmi_get_pmkid_list_event_rx(wmi, datap, len);
2947 		break;
2948 	case WMI_PSPOLL_EVENTID:
2949 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_PSPOLL_EVENTID\n");
2950 		ret = ath6kl_wmi_pspoll_event_rx(wmi, datap, len);
2951 		break;
2952 	case WMI_DTIMEXPIRY_EVENTID:
2953 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_DTIMEXPIRY_EVENTID\n");
2954 		ret = ath6kl_wmi_dtimexpiry_event_rx(wmi, datap, len);
2955 		break;
2956 	case WMI_SET_PARAMS_REPLY_EVENTID:
2957 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_SET_PARAMS_REPLY_EVENTID\n");
2958 		break;
2959 	case WMI_ADDBA_REQ_EVENTID:
2960 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_ADDBA_REQ_EVENTID\n");
2961 		ret = ath6kl_wmi_addba_req_event_rx(wmi, datap, len);
2962 		break;
2963 	case WMI_ADDBA_RESP_EVENTID:
2964 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_ADDBA_RESP_EVENTID\n");
2965 		break;
2966 	case WMI_DELBA_REQ_EVENTID:
2967 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_DELBA_REQ_EVENTID\n");
2968 		ret = ath6kl_wmi_delba_req_event_rx(wmi, datap, len);
2969 		break;
2970 	case WMI_REPORT_BTCOEX_CONFIG_EVENTID:
2971 		ath6kl_dbg(ATH6KL_DBG_WMI,
2972 			   "WMI_REPORT_BTCOEX_CONFIG_EVENTID\n");
2973 		break;
2974 	case WMI_REPORT_BTCOEX_STATS_EVENTID:
2975 		ath6kl_dbg(ATH6KL_DBG_WMI,
2976 			   "WMI_REPORT_BTCOEX_STATS_EVENTID\n");
2977 		break;
2978 	case WMI_TX_COMPLETE_EVENTID:
2979 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TX_COMPLETE_EVENTID\n");
2980 		ret = ath6kl_wmi_tx_complete_event_rx(datap, len);
2981 		break;
2982 	case WMI_REMAIN_ON_CHNL_EVENTID:
2983 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_REMAIN_ON_CHNL_EVENTID\n");
2984 		ret = ath6kl_wmi_remain_on_chnl_event_rx(wmi, datap, len);
2985 		break;
2986 	case WMI_CANCEL_REMAIN_ON_CHNL_EVENTID:
2987 		ath6kl_dbg(ATH6KL_DBG_WMI,
2988 			   "WMI_CANCEL_REMAIN_ON_CHNL_EVENTID\n");
2989 		ret = ath6kl_wmi_cancel_remain_on_chnl_event_rx(wmi, datap,
2990 								len);
2991 		break;
2992 	case WMI_TX_STATUS_EVENTID:
2993 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_TX_STATUS_EVENTID\n");
2994 		ret = ath6kl_wmi_tx_status_event_rx(wmi, datap, len);
2995 		break;
2996 	case WMI_RX_PROBE_REQ_EVENTID:
2997 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_RX_PROBE_REQ_EVENTID\n");
2998 		ret = ath6kl_wmi_rx_probe_req_event_rx(wmi, datap, len);
2999 		break;
3000 	case WMI_P2P_CAPABILITIES_EVENTID:
3001 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_P2P_CAPABILITIES_EVENTID\n");
3002 		ret = ath6kl_wmi_p2p_capabilities_event_rx(datap, len);
3003 		break;
3004 	case WMI_RX_ACTION_EVENTID:
3005 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_RX_ACTION_EVENTID\n");
3006 		ret = ath6kl_wmi_rx_action_event_rx(wmi, datap, len);
3007 		break;
3008 	case WMI_P2P_INFO_EVENTID:
3009 		ath6kl_dbg(ATH6KL_DBG_WMI, "WMI_P2P_INFO_EVENTID\n");
3010 		ret = ath6kl_wmi_p2p_info_event_rx(datap, len);
3011 		break;
3012 	default:
3013 		ath6kl_dbg(ATH6KL_DBG_WMI, "unknown cmd id 0x%x\n", id);
3014 		wmi->stat.cmd_id_err++;
3015 		ret = -EINVAL;
3016 		break;
3017 	}
3018 
3019 	dev_kfree_skb(skb);
3020 
3021 	return ret;
3022 }
3023 
3024 static void ath6kl_wmi_qos_state_init(struct wmi *wmi)
3025 {
3026 	if (!wmi)
3027 		return;
3028 
3029 	spin_lock_bh(&wmi->lock);
3030 
3031 	wmi->fat_pipe_exist = 0;
3032 	memset(wmi->stream_exist_for_ac, 0, sizeof(wmi->stream_exist_for_ac));
3033 
3034 	spin_unlock_bh(&wmi->lock);
3035 }
3036 
3037 void *ath6kl_wmi_init(struct ath6kl *dev)
3038 {
3039 	struct wmi *wmi;
3040 
3041 	wmi = kzalloc(sizeof(struct wmi), GFP_KERNEL);
3042 	if (!wmi)
3043 		return NULL;
3044 
3045 	spin_lock_init(&wmi->lock);
3046 
3047 	wmi->parent_dev = dev;
3048 
3049 	ath6kl_wmi_qos_state_init(wmi);
3050 
3051 	wmi->pwr_mode = REC_POWER;
3052 	wmi->phy_mode = WMI_11G_MODE;
3053 
3054 	wmi->pair_crypto_type = NONE_CRYPT;
3055 	wmi->grp_crypto_type = NONE_CRYPT;
3056 
3057 	wmi->ht_allowed[A_BAND_24GHZ] = 1;
3058 	wmi->ht_allowed[A_BAND_5GHZ] = 1;
3059 
3060 	return wmi;
3061 }
3062 
3063 void ath6kl_wmi_shutdown(struct wmi *wmi)
3064 {
3065 	if (!wmi)
3066 		return;
3067 
3068 	kfree(wmi->last_mgmt_tx_frame);
3069 	kfree(wmi);
3070 }
3071