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