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