ath10k: plug possible memory leak in WMI
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / wmi.c
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 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/skbuff.h>
19
20 #include "core.h"
21 #include "htc.h"
22 #include "debug.h"
23 #include "wmi.h"
24 #include "mac.h"
25
26 void ath10k_wmi_flush_tx(struct ath10k *ar)
27 {
28         int ret;
29
30         lockdep_assert_held(&ar->conf_mutex);
31
32         if (ar->state == ATH10K_STATE_WEDGED) {
33                 ath10k_warn("wmi flush skipped - device is wedged anyway\n");
34                 return;
35         }
36
37         ret = wait_event_timeout(ar->wmi.wq,
38                                  atomic_read(&ar->wmi.pending_tx_count) == 0,
39                                  5*HZ);
40         if (atomic_read(&ar->wmi.pending_tx_count) == 0)
41                 return;
42
43         if (ret == 0)
44                 ret = -ETIMEDOUT;
45
46         if (ret < 0)
47                 ath10k_warn("wmi flush failed (%d)\n", ret);
48 }
49
50 int ath10k_wmi_wait_for_service_ready(struct ath10k *ar)
51 {
52         int ret;
53         ret = wait_for_completion_timeout(&ar->wmi.service_ready,
54                                           WMI_SERVICE_READY_TIMEOUT_HZ);
55         return ret;
56 }
57
58 int ath10k_wmi_wait_for_unified_ready(struct ath10k *ar)
59 {
60         int ret;
61         ret = wait_for_completion_timeout(&ar->wmi.unified_ready,
62                                           WMI_UNIFIED_READY_TIMEOUT_HZ);
63         return ret;
64 }
65
66 static struct sk_buff *ath10k_wmi_alloc_skb(u32 len)
67 {
68         struct sk_buff *skb;
69         u32 round_len = roundup(len, 4);
70
71         skb = ath10k_htc_alloc_skb(WMI_SKB_HEADROOM + round_len);
72         if (!skb)
73                 return NULL;
74
75         skb_reserve(skb, WMI_SKB_HEADROOM);
76         if (!IS_ALIGNED((unsigned long)skb->data, 4))
77                 ath10k_warn("Unaligned WMI skb\n");
78
79         skb_put(skb, round_len);
80         memset(skb->data, 0, round_len);
81
82         return skb;
83 }
84
85 static void ath10k_wmi_htc_tx_complete(struct ath10k *ar, struct sk_buff *skb)
86 {
87         dev_kfree_skb(skb);
88
89         if (atomic_sub_return(1, &ar->wmi.pending_tx_count) == 0)
90                 wake_up(&ar->wmi.wq);
91 }
92
93 /* WMI command API */
94 static int ath10k_wmi_cmd_send(struct ath10k *ar, struct sk_buff *skb,
95                                enum wmi_cmd_id cmd_id)
96 {
97         struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
98         struct wmi_cmd_hdr *cmd_hdr;
99         int status;
100         u32 cmd = 0;
101
102         if (skb_push(skb, sizeof(struct wmi_cmd_hdr)) == NULL)
103                 return -ENOMEM;
104
105         cmd |= SM(cmd_id, WMI_CMD_HDR_CMD_ID);
106
107         cmd_hdr = (struct wmi_cmd_hdr *)skb->data;
108         cmd_hdr->cmd_id = __cpu_to_le32(cmd);
109
110         if (atomic_add_return(1, &ar->wmi.pending_tx_count) >
111             WMI_MAX_PENDING_TX_COUNT) {
112                 /* avoid using up memory when FW hangs */
113                 dev_kfree_skb(skb);
114                 atomic_dec(&ar->wmi.pending_tx_count);
115                 return -EBUSY;
116         }
117
118         memset(skb_cb, 0, sizeof(*skb_cb));
119
120         trace_ath10k_wmi_cmd(cmd_id, skb->data, skb->len);
121
122         status = ath10k_htc_send(&ar->htc, ar->wmi.eid, skb);
123         if (status) {
124                 dev_kfree_skb_any(skb);
125                 atomic_dec(&ar->wmi.pending_tx_count);
126                 return status;
127         }
128
129         return 0;
130 }
131
132 static int ath10k_wmi_event_scan(struct ath10k *ar, struct sk_buff *skb)
133 {
134         struct wmi_scan_event *event = (struct wmi_scan_event *)skb->data;
135         enum wmi_scan_event_type event_type;
136         enum wmi_scan_completion_reason reason;
137         u32 freq;
138         u32 req_id;
139         u32 scan_id;
140         u32 vdev_id;
141
142         event_type = __le32_to_cpu(event->event_type);
143         reason     = __le32_to_cpu(event->reason);
144         freq       = __le32_to_cpu(event->channel_freq);
145         req_id     = __le32_to_cpu(event->scan_req_id);
146         scan_id    = __le32_to_cpu(event->scan_id);
147         vdev_id    = __le32_to_cpu(event->vdev_id);
148
149         ath10k_dbg(ATH10K_DBG_WMI, "WMI_SCAN_EVENTID\n");
150         ath10k_dbg(ATH10K_DBG_WMI,
151                    "scan event type %d reason %d freq %d req_id %d "
152                    "scan_id %d vdev_id %d\n",
153                    event_type, reason, freq, req_id, scan_id, vdev_id);
154
155         spin_lock_bh(&ar->data_lock);
156
157         switch (event_type) {
158         case WMI_SCAN_EVENT_STARTED:
159                 ath10k_dbg(ATH10K_DBG_WMI, "SCAN_EVENT_STARTED\n");
160                 if (ar->scan.in_progress && ar->scan.is_roc)
161                         ieee80211_ready_on_channel(ar->hw);
162
163                 complete(&ar->scan.started);
164                 break;
165         case WMI_SCAN_EVENT_COMPLETED:
166                 ath10k_dbg(ATH10K_DBG_WMI, "SCAN_EVENT_COMPLETED\n");
167                 switch (reason) {
168                 case WMI_SCAN_REASON_COMPLETED:
169                         ath10k_dbg(ATH10K_DBG_WMI, "SCAN_REASON_COMPLETED\n");
170                         break;
171                 case WMI_SCAN_REASON_CANCELLED:
172                         ath10k_dbg(ATH10K_DBG_WMI, "SCAN_REASON_CANCELED\n");
173                         break;
174                 case WMI_SCAN_REASON_PREEMPTED:
175                         ath10k_dbg(ATH10K_DBG_WMI, "SCAN_REASON_PREEMPTED\n");
176                         break;
177                 case WMI_SCAN_REASON_TIMEDOUT:
178                         ath10k_dbg(ATH10K_DBG_WMI, "SCAN_REASON_TIMEDOUT\n");
179                         break;
180                 default:
181                         break;
182                 }
183
184                 ar->scan_channel = NULL;
185                 if (!ar->scan.in_progress) {
186                         ath10k_warn("no scan requested, ignoring\n");
187                         break;
188                 }
189
190                 if (ar->scan.is_roc) {
191                         ath10k_offchan_tx_purge(ar);
192
193                         if (!ar->scan.aborting)
194                                 ieee80211_remain_on_channel_expired(ar->hw);
195                 } else {
196                         ieee80211_scan_completed(ar->hw, ar->scan.aborting);
197                 }
198
199                 del_timer(&ar->scan.timeout);
200                 complete_all(&ar->scan.completed);
201                 ar->scan.in_progress = false;
202                 break;
203         case WMI_SCAN_EVENT_BSS_CHANNEL:
204                 ath10k_dbg(ATH10K_DBG_WMI, "SCAN_EVENT_BSS_CHANNEL\n");
205                 ar->scan_channel = NULL;
206                 break;
207         case WMI_SCAN_EVENT_FOREIGN_CHANNEL:
208                 ath10k_dbg(ATH10K_DBG_WMI, "SCAN_EVENT_FOREIGN_CHANNEL\n");
209                 ar->scan_channel = ieee80211_get_channel(ar->hw->wiphy, freq);
210                 if (ar->scan.in_progress && ar->scan.is_roc &&
211                     ar->scan.roc_freq == freq) {
212                         complete(&ar->scan.on_channel);
213                 }
214                 break;
215         case WMI_SCAN_EVENT_DEQUEUED:
216                 ath10k_dbg(ATH10K_DBG_WMI, "SCAN_EVENT_DEQUEUED\n");
217                 break;
218         case WMI_SCAN_EVENT_PREEMPTED:
219                 ath10k_dbg(ATH10K_DBG_WMI, "WMI_SCAN_EVENT_PREEMPTED\n");
220                 break;
221         case WMI_SCAN_EVENT_START_FAILED:
222                 ath10k_dbg(ATH10K_DBG_WMI, "WMI_SCAN_EVENT_START_FAILED\n");
223                 break;
224         default:
225                 break;
226         }
227
228         spin_unlock_bh(&ar->data_lock);
229         return 0;
230 }
231
232 static inline enum ieee80211_band phy_mode_to_band(u32 phy_mode)
233 {
234         enum ieee80211_band band;
235
236         switch (phy_mode) {
237         case MODE_11A:
238         case MODE_11NA_HT20:
239         case MODE_11NA_HT40:
240         case MODE_11AC_VHT20:
241         case MODE_11AC_VHT40:
242         case MODE_11AC_VHT80:
243                 band = IEEE80211_BAND_5GHZ;
244                 break;
245         case MODE_11G:
246         case MODE_11B:
247         case MODE_11GONLY:
248         case MODE_11NG_HT20:
249         case MODE_11NG_HT40:
250         case MODE_11AC_VHT20_2G:
251         case MODE_11AC_VHT40_2G:
252         case MODE_11AC_VHT80_2G:
253         default:
254                 band = IEEE80211_BAND_2GHZ;
255         }
256
257         return band;
258 }
259
260 static inline u8 get_rate_idx(u32 rate, enum ieee80211_band band)
261 {
262         u8 rate_idx = 0;
263
264         /* rate in Kbps */
265         switch (rate) {
266         case 1000:
267                 rate_idx = 0;
268                 break;
269         case 2000:
270                 rate_idx = 1;
271                 break;
272         case 5500:
273                 rate_idx = 2;
274                 break;
275         case 11000:
276                 rate_idx = 3;
277                 break;
278         case 6000:
279                 rate_idx = 4;
280                 break;
281         case 9000:
282                 rate_idx = 5;
283                 break;
284         case 12000:
285                 rate_idx = 6;
286                 break;
287         case 18000:
288                 rate_idx = 7;
289                 break;
290         case 24000:
291                 rate_idx = 8;
292                 break;
293         case 36000:
294                 rate_idx = 9;
295                 break;
296         case 48000:
297                 rate_idx = 10;
298                 break;
299         case 54000:
300                 rate_idx = 11;
301                 break;
302         default:
303                 break;
304         }
305
306         if (band == IEEE80211_BAND_5GHZ) {
307                 if (rate_idx > 3)
308                         /* Omit CCK rates */
309                         rate_idx -= 4;
310                 else
311                         rate_idx = 0;
312         }
313
314         return rate_idx;
315 }
316
317 static int ath10k_wmi_event_mgmt_rx(struct ath10k *ar, struct sk_buff *skb)
318 {
319         struct wmi_mgmt_rx_event *event = (struct wmi_mgmt_rx_event *)skb->data;
320         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
321         struct ieee80211_hdr *hdr;
322         u32 rx_status;
323         u32 channel;
324         u32 phy_mode;
325         u32 snr;
326         u32 rate;
327         u32 buf_len;
328         u16 fc;
329
330         channel   = __le32_to_cpu(event->hdr.channel);
331         buf_len   = __le32_to_cpu(event->hdr.buf_len);
332         rx_status = __le32_to_cpu(event->hdr.status);
333         snr       = __le32_to_cpu(event->hdr.snr);
334         phy_mode  = __le32_to_cpu(event->hdr.phy_mode);
335         rate      = __le32_to_cpu(event->hdr.rate);
336
337         memset(status, 0, sizeof(*status));
338
339         ath10k_dbg(ATH10K_DBG_MGMT,
340                    "event mgmt rx status %08x\n", rx_status);
341
342         if (rx_status & WMI_RX_STATUS_ERR_DECRYPT) {
343                 dev_kfree_skb(skb);
344                 return 0;
345         }
346
347         if (rx_status & WMI_RX_STATUS_ERR_KEY_CACHE_MISS) {
348                 dev_kfree_skb(skb);
349                 return 0;
350         }
351
352         if (rx_status & WMI_RX_STATUS_ERR_CRC)
353                 status->flag |= RX_FLAG_FAILED_FCS_CRC;
354         if (rx_status & WMI_RX_STATUS_ERR_MIC)
355                 status->flag |= RX_FLAG_MMIC_ERROR;
356
357         status->band = phy_mode_to_band(phy_mode);
358         status->freq = ieee80211_channel_to_frequency(channel, status->band);
359         status->signal = snr + ATH10K_DEFAULT_NOISE_FLOOR;
360         status->rate_idx = get_rate_idx(rate, status->band);
361
362         skb_pull(skb, sizeof(event->hdr));
363
364         hdr = (struct ieee80211_hdr *)skb->data;
365         fc = le16_to_cpu(hdr->frame_control);
366
367         if (fc & IEEE80211_FCTL_PROTECTED) {
368                 status->flag |= RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED |
369                                 RX_FLAG_MMIC_STRIPPED;
370                 hdr->frame_control = __cpu_to_le16(fc &
371                                         ~IEEE80211_FCTL_PROTECTED);
372         }
373
374         ath10k_dbg(ATH10K_DBG_MGMT,
375                    "event mgmt rx skb %p len %d ftype %02x stype %02x\n",
376                    skb, skb->len,
377                    fc & IEEE80211_FCTL_FTYPE, fc & IEEE80211_FCTL_STYPE);
378
379         ath10k_dbg(ATH10K_DBG_MGMT,
380                    "event mgmt rx freq %d band %d snr %d, rate_idx %d\n",
381                    status->freq, status->band, status->signal,
382                    status->rate_idx);
383
384         /*
385          * packets from HTC come aligned to 4byte boundaries
386          * because they can originally come in along with a trailer
387          */
388         skb_trim(skb, buf_len);
389
390         ieee80211_rx(ar->hw, skb);
391         return 0;
392 }
393
394 static int freq_to_idx(struct ath10k *ar, int freq)
395 {
396         struct ieee80211_supported_band *sband;
397         int band, ch, idx = 0;
398
399         for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
400                 sband = ar->hw->wiphy->bands[band];
401                 if (!sband)
402                         continue;
403
404                 for (ch = 0; ch < sband->n_channels; ch++, idx++)
405                         if (sband->channels[ch].center_freq == freq)
406                                 goto exit;
407         }
408
409 exit:
410         return idx;
411 }
412
413 static void ath10k_wmi_event_chan_info(struct ath10k *ar, struct sk_buff *skb)
414 {
415         struct wmi_chan_info_event *ev;
416         struct survey_info *survey;
417         u32 err_code, freq, cmd_flags, noise_floor, rx_clear_count, cycle_count;
418         int idx;
419
420         ev = (struct wmi_chan_info_event *)skb->data;
421
422         err_code = __le32_to_cpu(ev->err_code);
423         freq = __le32_to_cpu(ev->freq);
424         cmd_flags = __le32_to_cpu(ev->cmd_flags);
425         noise_floor = __le32_to_cpu(ev->noise_floor);
426         rx_clear_count = __le32_to_cpu(ev->rx_clear_count);
427         cycle_count = __le32_to_cpu(ev->cycle_count);
428
429         ath10k_dbg(ATH10K_DBG_WMI,
430                    "chan info err_code %d freq %d cmd_flags %d noise_floor %d rx_clear_count %d cycle_count %d\n",
431                    err_code, freq, cmd_flags, noise_floor, rx_clear_count,
432                    cycle_count);
433
434         spin_lock_bh(&ar->data_lock);
435
436         if (!ar->scan.in_progress) {
437                 ath10k_warn("chan info event without a scan request?\n");
438                 goto exit;
439         }
440
441         idx = freq_to_idx(ar, freq);
442         if (idx >= ARRAY_SIZE(ar->survey)) {
443                 ath10k_warn("chan info: invalid frequency %d (idx %d out of bounds)\n",
444                             freq, idx);
445                 goto exit;
446         }
447
448         if (cmd_flags & WMI_CHAN_INFO_FLAG_COMPLETE) {
449                 /* During scanning chan info is reported twice for each
450                  * visited channel. The reported cycle count is global
451                  * and per-channel cycle count must be calculated */
452
453                 cycle_count -= ar->survey_last_cycle_count;
454                 rx_clear_count -= ar->survey_last_rx_clear_count;
455
456                 survey = &ar->survey[idx];
457                 survey->channel_time = WMI_CHAN_INFO_MSEC(cycle_count);
458                 survey->channel_time_rx = WMI_CHAN_INFO_MSEC(rx_clear_count);
459                 survey->noise = noise_floor;
460                 survey->filled = SURVEY_INFO_CHANNEL_TIME |
461                                  SURVEY_INFO_CHANNEL_TIME_RX |
462                                  SURVEY_INFO_NOISE_DBM;
463         }
464
465         ar->survey_last_rx_clear_count = rx_clear_count;
466         ar->survey_last_cycle_count = cycle_count;
467
468 exit:
469         spin_unlock_bh(&ar->data_lock);
470 }
471
472 static void ath10k_wmi_event_echo(struct ath10k *ar, struct sk_buff *skb)
473 {
474         ath10k_dbg(ATH10K_DBG_WMI, "WMI_ECHO_EVENTID\n");
475 }
476
477 static void ath10k_wmi_event_debug_mesg(struct ath10k *ar, struct sk_buff *skb)
478 {
479         ath10k_dbg(ATH10K_DBG_WMI, "WMI_DEBUG_MESG_EVENTID\n");
480 }
481
482 static void ath10k_wmi_event_update_stats(struct ath10k *ar,
483                                           struct sk_buff *skb)
484 {
485         struct wmi_stats_event *ev = (struct wmi_stats_event *)skb->data;
486
487         ath10k_dbg(ATH10K_DBG_WMI, "WMI_UPDATE_STATS_EVENTID\n");
488
489         ath10k_debug_read_target_stats(ar, ev);
490 }
491
492 static void ath10k_wmi_event_vdev_start_resp(struct ath10k *ar,
493                                              struct sk_buff *skb)
494 {
495         struct wmi_vdev_start_response_event *ev;
496
497         ath10k_dbg(ATH10K_DBG_WMI, "WMI_VDEV_START_RESP_EVENTID\n");
498
499         ev = (struct wmi_vdev_start_response_event *)skb->data;
500
501         if (WARN_ON(__le32_to_cpu(ev->status)))
502                 return;
503
504         complete(&ar->vdev_setup_done);
505 }
506
507 static void ath10k_wmi_event_vdev_stopped(struct ath10k *ar,
508                                           struct sk_buff *skb)
509 {
510         ath10k_dbg(ATH10K_DBG_WMI, "WMI_VDEV_STOPPED_EVENTID\n");
511         complete(&ar->vdev_setup_done);
512 }
513
514 static void ath10k_wmi_event_peer_sta_kickout(struct ath10k *ar,
515                                               struct sk_buff *skb)
516 {
517         ath10k_dbg(ATH10K_DBG_WMI, "WMI_PEER_STA_KICKOUT_EVENTID\n");
518 }
519
520 /*
521  * FIXME
522  *
523  * We don't report to mac80211 sleep state of connected
524  * stations. Due to this mac80211 can't fill in TIM IE
525  * correctly.
526  *
527  * I know of no way of getting nullfunc frames that contain
528  * sleep transition from connected stations - these do not
529  * seem to be sent from the target to the host. There also
530  * doesn't seem to be a dedicated event for that. So the
531  * only way left to do this would be to read tim_bitmap
532  * during SWBA.
533  *
534  * We could probably try using tim_bitmap from SWBA to tell
535  * mac80211 which stations are asleep and which are not. The
536  * problem here is calling mac80211 functions so many times
537  * could take too long and make us miss the time to submit
538  * the beacon to the target.
539  *
540  * So as a workaround we try to extend the TIM IE if there
541  * is unicast buffered for stations with aid > 7 and fill it
542  * in ourselves.
543  */
544 static void ath10k_wmi_update_tim(struct ath10k *ar,
545                                   struct ath10k_vif *arvif,
546                                   struct sk_buff *bcn,
547                                   struct wmi_bcn_info *bcn_info)
548 {
549         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)bcn->data;
550         struct ieee80211_tim_ie *tim;
551         u8 *ies, *ie;
552         u8 ie_len, pvm_len;
553
554         /* if next SWBA has no tim_changed the tim_bitmap is garbage.
555          * we must copy the bitmap upon change and reuse it later */
556         if (__le32_to_cpu(bcn_info->tim_info.tim_changed)) {
557                 int i;
558
559                 BUILD_BUG_ON(sizeof(arvif->u.ap.tim_bitmap) !=
560                              sizeof(bcn_info->tim_info.tim_bitmap));
561
562                 for (i = 0; i < sizeof(arvif->u.ap.tim_bitmap); i++) {
563                         __le32 t = bcn_info->tim_info.tim_bitmap[i / 4];
564                         u32 v = __le32_to_cpu(t);
565                         arvif->u.ap.tim_bitmap[i] = (v >> ((i % 4) * 8)) & 0xFF;
566                 }
567
568                 /* FW reports either length 0 or 16
569                  * so we calculate this on our own */
570                 arvif->u.ap.tim_len = 0;
571                 for (i = 0; i < sizeof(arvif->u.ap.tim_bitmap); i++)
572                         if (arvif->u.ap.tim_bitmap[i])
573                                 arvif->u.ap.tim_len = i;
574
575                 arvif->u.ap.tim_len++;
576         }
577
578         ies = bcn->data;
579         ies += ieee80211_hdrlen(hdr->frame_control);
580         ies += 12; /* fixed parameters */
581
582         ie = (u8 *)cfg80211_find_ie(WLAN_EID_TIM, ies,
583                                     (u8 *)skb_tail_pointer(bcn) - ies);
584         if (!ie) {
585                 if (arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
586                         ath10k_warn("no tim ie found;\n");
587                 return;
588         }
589
590         tim = (void *)ie + 2;
591         ie_len = ie[1];
592         pvm_len = ie_len - 3; /* exclude dtim count, dtim period, bmap ctl */
593
594         if (pvm_len < arvif->u.ap.tim_len) {
595                 int expand_size = sizeof(arvif->u.ap.tim_bitmap) - pvm_len;
596                 int move_size = skb_tail_pointer(bcn) - (ie + 2 + ie_len);
597                 void *next_ie = ie + 2 + ie_len;
598
599                 if (skb_put(bcn, expand_size)) {
600                         memmove(next_ie + expand_size, next_ie, move_size);
601
602                         ie[1] += expand_size;
603                         ie_len += expand_size;
604                         pvm_len += expand_size;
605                 } else {
606                         ath10k_warn("tim expansion failed\n");
607                 }
608         }
609
610         if (pvm_len > sizeof(arvif->u.ap.tim_bitmap)) {
611                 ath10k_warn("tim pvm length is too great (%d)\n", pvm_len);
612                 return;
613         }
614
615         tim->bitmap_ctrl = !!__le32_to_cpu(bcn_info->tim_info.tim_mcast);
616         memcpy(tim->virtual_map, arvif->u.ap.tim_bitmap, pvm_len);
617
618         ath10k_dbg(ATH10K_DBG_MGMT, "dtim %d/%d mcast %d pvmlen %d\n",
619                    tim->dtim_count, tim->dtim_period,
620                    tim->bitmap_ctrl, pvm_len);
621 }
622
623 static void ath10k_p2p_fill_noa_ie(u8 *data, u32 len,
624                                    struct wmi_p2p_noa_info *noa)
625 {
626         struct ieee80211_p2p_noa_attr *noa_attr;
627         u8  ctwindow_oppps = noa->ctwindow_oppps;
628         u8 ctwindow = ctwindow_oppps >> WMI_P2P_OPPPS_CTWINDOW_OFFSET;
629         bool oppps = !!(ctwindow_oppps & WMI_P2P_OPPPS_ENABLE_BIT);
630         __le16 *noa_attr_len;
631         u16 attr_len;
632         u8 noa_descriptors = noa->num_descriptors;
633         int i;
634
635         /* P2P IE */
636         data[0] = WLAN_EID_VENDOR_SPECIFIC;
637         data[1] = len - 2;
638         data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
639         data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
640         data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
641         data[5] = WLAN_OUI_TYPE_WFA_P2P;
642
643         /* NOA ATTR */
644         data[6] = IEEE80211_P2P_ATTR_ABSENCE_NOTICE;
645         noa_attr_len = (__le16 *)&data[7]; /* 2 bytes */
646         noa_attr = (struct ieee80211_p2p_noa_attr *)&data[9];
647
648         noa_attr->index = noa->index;
649         noa_attr->oppps_ctwindow = ctwindow;
650         if (oppps)
651                 noa_attr->oppps_ctwindow |= IEEE80211_P2P_OPPPS_ENABLE_BIT;
652
653         for (i = 0; i < noa_descriptors; i++) {
654                 noa_attr->desc[i].count =
655                         __le32_to_cpu(noa->descriptors[i].type_count);
656                 noa_attr->desc[i].duration = noa->descriptors[i].duration;
657                 noa_attr->desc[i].interval = noa->descriptors[i].interval;
658                 noa_attr->desc[i].start_time = noa->descriptors[i].start_time;
659         }
660
661         attr_len = 2; /* index + oppps_ctwindow */
662         attr_len += noa_descriptors * sizeof(struct ieee80211_p2p_noa_desc);
663         *noa_attr_len = __cpu_to_le16(attr_len);
664 }
665
666 static u32 ath10k_p2p_calc_noa_ie_len(struct wmi_p2p_noa_info *noa)
667 {
668         u32 len = 0;
669         u8 noa_descriptors = noa->num_descriptors;
670         u8 opp_ps_info = noa->ctwindow_oppps;
671         bool opps_enabled = !!(opp_ps_info & WMI_P2P_OPPPS_ENABLE_BIT);
672
673
674         if (!noa_descriptors && !opps_enabled)
675                 return len;
676
677         len += 1 + 1 + 4; /* EID + len + OUI */
678         len += 1 + 2; /* noa attr  + attr len */
679         len += 1 + 1; /* index + oppps_ctwindow */
680         len += noa_descriptors * sizeof(struct ieee80211_p2p_noa_desc);
681
682         return len;
683 }
684
685 static void ath10k_wmi_update_noa(struct ath10k *ar, struct ath10k_vif *arvif,
686                                   struct sk_buff *bcn,
687                                   struct wmi_bcn_info *bcn_info)
688 {
689         struct wmi_p2p_noa_info *noa = &bcn_info->p2p_noa_info;
690         u8 *new_data, *old_data = arvif->u.ap.noa_data;
691         u32 new_len;
692
693         if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
694                 return;
695
696         ath10k_dbg(ATH10K_DBG_MGMT, "noa changed: %d\n", noa->changed);
697         if (noa->changed & WMI_P2P_NOA_CHANGED_BIT) {
698                 new_len = ath10k_p2p_calc_noa_ie_len(noa);
699                 if (!new_len)
700                         goto cleanup;
701
702                 new_data = kmalloc(new_len, GFP_ATOMIC);
703                 if (!new_data)
704                         goto cleanup;
705
706                 ath10k_p2p_fill_noa_ie(new_data, new_len, noa);
707
708                 spin_lock_bh(&ar->data_lock);
709                 arvif->u.ap.noa_data = new_data;
710                 arvif->u.ap.noa_len = new_len;
711                 spin_unlock_bh(&ar->data_lock);
712                 kfree(old_data);
713         }
714
715         if (arvif->u.ap.noa_data)
716                 if (!pskb_expand_head(bcn, 0, arvif->u.ap.noa_len, GFP_ATOMIC))
717                         memcpy(skb_put(bcn, arvif->u.ap.noa_len),
718                                arvif->u.ap.noa_data,
719                                arvif->u.ap.noa_len);
720         return;
721
722 cleanup:
723         spin_lock_bh(&ar->data_lock);
724         arvif->u.ap.noa_data = NULL;
725         arvif->u.ap.noa_len = 0;
726         spin_unlock_bh(&ar->data_lock);
727         kfree(old_data);
728 }
729
730
731 static void ath10k_wmi_event_host_swba(struct ath10k *ar, struct sk_buff *skb)
732 {
733         struct wmi_host_swba_event *ev;
734         u32 map;
735         int i = -1;
736         struct wmi_bcn_info *bcn_info;
737         struct ath10k_vif *arvif;
738         struct wmi_bcn_tx_arg arg;
739         struct sk_buff *bcn;
740         int vdev_id = 0;
741         int ret;
742
743         ath10k_dbg(ATH10K_DBG_MGMT, "WMI_HOST_SWBA_EVENTID\n");
744
745         ev = (struct wmi_host_swba_event *)skb->data;
746         map = __le32_to_cpu(ev->vdev_map);
747
748         ath10k_dbg(ATH10K_DBG_MGMT, "host swba:\n"
749                    "-vdev map 0x%x\n",
750                    ev->vdev_map);
751
752         for (; map; map >>= 1, vdev_id++) {
753                 if (!(map & 0x1))
754                         continue;
755
756                 i++;
757
758                 if (i >= WMI_MAX_AP_VDEV) {
759                         ath10k_warn("swba has corrupted vdev map\n");
760                         break;
761                 }
762
763                 bcn_info = &ev->bcn_info[i];
764
765                 ath10k_dbg(ATH10K_DBG_MGMT,
766                            "-bcn_info[%d]:\n"
767                            "--tim_len %d\n"
768                            "--tim_mcast %d\n"
769                            "--tim_changed %d\n"
770                            "--tim_num_ps_pending %d\n"
771                            "--tim_bitmap 0x%08x%08x%08x%08x\n",
772                            i,
773                            __le32_to_cpu(bcn_info->tim_info.tim_len),
774                            __le32_to_cpu(bcn_info->tim_info.tim_mcast),
775                            __le32_to_cpu(bcn_info->tim_info.tim_changed),
776                            __le32_to_cpu(bcn_info->tim_info.tim_num_ps_pending),
777                            __le32_to_cpu(bcn_info->tim_info.tim_bitmap[3]),
778                            __le32_to_cpu(bcn_info->tim_info.tim_bitmap[2]),
779                            __le32_to_cpu(bcn_info->tim_info.tim_bitmap[1]),
780                            __le32_to_cpu(bcn_info->tim_info.tim_bitmap[0]));
781
782                 arvif = ath10k_get_arvif(ar, vdev_id);
783                 if (arvif == NULL) {
784                         ath10k_warn("no vif for vdev_id %d found\n", vdev_id);
785                         continue;
786                 }
787
788                 bcn = ieee80211_beacon_get(ar->hw, arvif->vif);
789                 if (!bcn) {
790                         ath10k_warn("could not get mac80211 beacon\n");
791                         continue;
792                 }
793
794                 ath10k_tx_h_seq_no(bcn);
795                 ath10k_wmi_update_tim(ar, arvif, bcn, bcn_info);
796                 ath10k_wmi_update_noa(ar, arvif, bcn, bcn_info);
797
798                 arg.vdev_id = arvif->vdev_id;
799                 arg.tx_rate = 0;
800                 arg.tx_power = 0;
801                 arg.bcn = bcn->data;
802                 arg.bcn_len = bcn->len;
803
804                 ret = ath10k_wmi_beacon_send(ar, &arg);
805                 if (ret)
806                         ath10k_warn("could not send beacon (%d)\n", ret);
807
808                 dev_kfree_skb_any(bcn);
809         }
810 }
811
812 static void ath10k_wmi_event_tbttoffset_update(struct ath10k *ar,
813                                                struct sk_buff *skb)
814 {
815         ath10k_dbg(ATH10K_DBG_WMI, "WMI_TBTTOFFSET_UPDATE_EVENTID\n");
816 }
817
818 static void ath10k_wmi_event_phyerr(struct ath10k *ar, struct sk_buff *skb)
819 {
820         ath10k_dbg(ATH10K_DBG_WMI, "WMI_PHYERR_EVENTID\n");
821 }
822
823 static void ath10k_wmi_event_roam(struct ath10k *ar, struct sk_buff *skb)
824 {
825         ath10k_dbg(ATH10K_DBG_WMI, "WMI_ROAM_EVENTID\n");
826 }
827
828 static void ath10k_wmi_event_profile_match(struct ath10k *ar,
829                                     struct sk_buff *skb)
830 {
831         ath10k_dbg(ATH10K_DBG_WMI, "WMI_PROFILE_MATCH\n");
832 }
833
834 static void ath10k_wmi_event_debug_print(struct ath10k *ar,
835                                   struct sk_buff *skb)
836 {
837         ath10k_dbg(ATH10K_DBG_WMI, "WMI_DEBUG_PRINT_EVENTID\n");
838 }
839
840 static void ath10k_wmi_event_pdev_qvit(struct ath10k *ar, struct sk_buff *skb)
841 {
842         ath10k_dbg(ATH10K_DBG_WMI, "WMI_PDEV_QVIT_EVENTID\n");
843 }
844
845 static void ath10k_wmi_event_wlan_profile_data(struct ath10k *ar,
846                                                struct sk_buff *skb)
847 {
848         ath10k_dbg(ATH10K_DBG_WMI, "WMI_WLAN_PROFILE_DATA_EVENTID\n");
849 }
850
851 static void ath10k_wmi_event_rtt_measurement_report(struct ath10k *ar,
852                                              struct sk_buff *skb)
853 {
854         ath10k_dbg(ATH10K_DBG_WMI, "WMI_RTT_MEASUREMENT_REPORT_EVENTID\n");
855 }
856
857 static void ath10k_wmi_event_tsf_measurement_report(struct ath10k *ar,
858                                              struct sk_buff *skb)
859 {
860         ath10k_dbg(ATH10K_DBG_WMI, "WMI_TSF_MEASUREMENT_REPORT_EVENTID\n");
861 }
862
863 static void ath10k_wmi_event_rtt_error_report(struct ath10k *ar,
864                                               struct sk_buff *skb)
865 {
866         ath10k_dbg(ATH10K_DBG_WMI, "WMI_RTT_ERROR_REPORT_EVENTID\n");
867 }
868
869 static void ath10k_wmi_event_wow_wakeup_host(struct ath10k *ar,
870                                              struct sk_buff *skb)
871 {
872         ath10k_dbg(ATH10K_DBG_WMI, "WMI_WOW_WAKEUP_HOST_EVENTID\n");
873 }
874
875 static void ath10k_wmi_event_dcs_interference(struct ath10k *ar,
876                                               struct sk_buff *skb)
877 {
878         ath10k_dbg(ATH10K_DBG_WMI, "WMI_DCS_INTERFERENCE_EVENTID\n");
879 }
880
881 static void ath10k_wmi_event_pdev_tpc_config(struct ath10k *ar,
882                                              struct sk_buff *skb)
883 {
884         ath10k_dbg(ATH10K_DBG_WMI, "WMI_PDEV_TPC_CONFIG_EVENTID\n");
885 }
886
887 static void ath10k_wmi_event_pdev_ftm_intg(struct ath10k *ar,
888                                            struct sk_buff *skb)
889 {
890         ath10k_dbg(ATH10K_DBG_WMI, "WMI_PDEV_FTM_INTG_EVENTID\n");
891 }
892
893 static void ath10k_wmi_event_gtk_offload_status(struct ath10k *ar,
894                                          struct sk_buff *skb)
895 {
896         ath10k_dbg(ATH10K_DBG_WMI, "WMI_GTK_OFFLOAD_STATUS_EVENTID\n");
897 }
898
899 static void ath10k_wmi_event_gtk_rekey_fail(struct ath10k *ar,
900                                             struct sk_buff *skb)
901 {
902         ath10k_dbg(ATH10K_DBG_WMI, "WMI_GTK_REKEY_FAIL_EVENTID\n");
903 }
904
905 static void ath10k_wmi_event_delba_complete(struct ath10k *ar,
906                                             struct sk_buff *skb)
907 {
908         ath10k_dbg(ATH10K_DBG_WMI, "WMI_TX_DELBA_COMPLETE_EVENTID\n");
909 }
910
911 static void ath10k_wmi_event_addba_complete(struct ath10k *ar,
912                                             struct sk_buff *skb)
913 {
914         ath10k_dbg(ATH10K_DBG_WMI, "WMI_TX_ADDBA_COMPLETE_EVENTID\n");
915 }
916
917 static void ath10k_wmi_event_vdev_install_key_complete(struct ath10k *ar,
918                                                 struct sk_buff *skb)
919 {
920         ath10k_dbg(ATH10K_DBG_WMI, "WMI_VDEV_INSTALL_KEY_COMPLETE_EVENTID\n");
921 }
922
923 static void ath10k_wmi_service_ready_event_rx(struct ath10k *ar,
924                                               struct sk_buff *skb)
925 {
926         struct wmi_service_ready_event *ev = (void *)skb->data;
927
928         if (skb->len < sizeof(*ev)) {
929                 ath10k_warn("Service ready event was %d B but expected %zu B. Wrong firmware version?\n",
930                             skb->len, sizeof(*ev));
931                 return;
932         }
933
934         ar->hw_min_tx_power = __le32_to_cpu(ev->hw_min_tx_power);
935         ar->hw_max_tx_power = __le32_to_cpu(ev->hw_max_tx_power);
936         ar->ht_cap_info = __le32_to_cpu(ev->ht_cap_info);
937         ar->vht_cap_info = __le32_to_cpu(ev->vht_cap_info);
938         ar->fw_version_major =
939                 (__le32_to_cpu(ev->sw_version) & 0xff000000) >> 24;
940         ar->fw_version_minor = (__le32_to_cpu(ev->sw_version) & 0x00ffffff);
941         ar->fw_version_release =
942                 (__le32_to_cpu(ev->sw_version_1) & 0xffff0000) >> 16;
943         ar->fw_version_build = (__le32_to_cpu(ev->sw_version_1) & 0x0000ffff);
944         ar->phy_capability = __le32_to_cpu(ev->phy_capability);
945         ar->num_rf_chains = __le32_to_cpu(ev->num_rf_chains);
946
947         if (ar->num_rf_chains > WMI_MAX_SPATIAL_STREAM) {
948                 ath10k_warn("hardware advertises support for more spatial streams than it should (%d > %d)\n",
949                             ar->num_rf_chains, WMI_MAX_SPATIAL_STREAM);
950                 ar->num_rf_chains = WMI_MAX_SPATIAL_STREAM;
951         }
952
953         ar->ath_common.regulatory.current_rd =
954                 __le32_to_cpu(ev->hal_reg_capabilities.eeprom_rd);
955
956         ath10k_debug_read_service_map(ar, ev->wmi_service_bitmap,
957                                       sizeof(ev->wmi_service_bitmap));
958
959         if (strlen(ar->hw->wiphy->fw_version) == 0) {
960                 snprintf(ar->hw->wiphy->fw_version,
961                          sizeof(ar->hw->wiphy->fw_version),
962                          "%u.%u.%u.%u",
963                          ar->fw_version_major,
964                          ar->fw_version_minor,
965                          ar->fw_version_release,
966                          ar->fw_version_build);
967         }
968
969         /* FIXME: it probably should be better to support this */
970         if (__le32_to_cpu(ev->num_mem_reqs) > 0) {
971                 ath10k_warn("target requested %d memory chunks; ignoring\n",
972                             __le32_to_cpu(ev->num_mem_reqs));
973         }
974
975         ath10k_dbg(ATH10K_DBG_WMI,
976                    "wmi event service ready sw_ver 0x%08x sw_ver1 0x%08x abi_ver %u phy_cap 0x%08x ht_cap 0x%08x vht_cap 0x%08x vht_supp_msc 0x%08x sys_cap_info 0x%08x mem_reqs %u num_rf_chains %u\n",
977                    __le32_to_cpu(ev->sw_version),
978                    __le32_to_cpu(ev->sw_version_1),
979                    __le32_to_cpu(ev->abi_version),
980                    __le32_to_cpu(ev->phy_capability),
981                    __le32_to_cpu(ev->ht_cap_info),
982                    __le32_to_cpu(ev->vht_cap_info),
983                    __le32_to_cpu(ev->vht_supp_mcs),
984                    __le32_to_cpu(ev->sys_cap_info),
985                    __le32_to_cpu(ev->num_mem_reqs),
986                    __le32_to_cpu(ev->num_rf_chains));
987
988         complete(&ar->wmi.service_ready);
989 }
990
991 static int ath10k_wmi_ready_event_rx(struct ath10k *ar, struct sk_buff *skb)
992 {
993         struct wmi_ready_event *ev = (struct wmi_ready_event *)skb->data;
994
995         if (WARN_ON(skb->len < sizeof(*ev)))
996                 return -EINVAL;
997
998         memcpy(ar->mac_addr, ev->mac_addr.addr, ETH_ALEN);
999
1000         ath10k_dbg(ATH10K_DBG_WMI,
1001                    "wmi event ready sw_version %u abi_version %u mac_addr %pM status %d\n",
1002                    __le32_to_cpu(ev->sw_version),
1003                    __le32_to_cpu(ev->abi_version),
1004                    ev->mac_addr.addr,
1005                    __le32_to_cpu(ev->status));
1006
1007         complete(&ar->wmi.unified_ready);
1008         return 0;
1009 }
1010
1011 static void ath10k_wmi_event_process(struct ath10k *ar, struct sk_buff *skb)
1012 {
1013         struct wmi_cmd_hdr *cmd_hdr;
1014         enum wmi_event_id id;
1015         u16 len;
1016
1017         cmd_hdr = (struct wmi_cmd_hdr *)skb->data;
1018         id = MS(__le32_to_cpu(cmd_hdr->cmd_id), WMI_CMD_HDR_CMD_ID);
1019
1020         if (skb_pull(skb, sizeof(struct wmi_cmd_hdr)) == NULL)
1021                 return;
1022
1023         len = skb->len;
1024
1025         trace_ath10k_wmi_event(id, skb->data, skb->len);
1026
1027         switch (id) {
1028         case WMI_MGMT_RX_EVENTID:
1029                 ath10k_wmi_event_mgmt_rx(ar, skb);
1030                 /* mgmt_rx() owns the skb now! */
1031                 return;
1032         case WMI_SCAN_EVENTID:
1033                 ath10k_wmi_event_scan(ar, skb);
1034                 break;
1035         case WMI_CHAN_INFO_EVENTID:
1036                 ath10k_wmi_event_chan_info(ar, skb);
1037                 break;
1038         case WMI_ECHO_EVENTID:
1039                 ath10k_wmi_event_echo(ar, skb);
1040                 break;
1041         case WMI_DEBUG_MESG_EVENTID:
1042                 ath10k_wmi_event_debug_mesg(ar, skb);
1043                 break;
1044         case WMI_UPDATE_STATS_EVENTID:
1045                 ath10k_wmi_event_update_stats(ar, skb);
1046                 break;
1047         case WMI_VDEV_START_RESP_EVENTID:
1048                 ath10k_wmi_event_vdev_start_resp(ar, skb);
1049                 break;
1050         case WMI_VDEV_STOPPED_EVENTID:
1051                 ath10k_wmi_event_vdev_stopped(ar, skb);
1052                 break;
1053         case WMI_PEER_STA_KICKOUT_EVENTID:
1054                 ath10k_wmi_event_peer_sta_kickout(ar, skb);
1055                 break;
1056         case WMI_HOST_SWBA_EVENTID:
1057                 ath10k_wmi_event_host_swba(ar, skb);
1058                 break;
1059         case WMI_TBTTOFFSET_UPDATE_EVENTID:
1060                 ath10k_wmi_event_tbttoffset_update(ar, skb);
1061                 break;
1062         case WMI_PHYERR_EVENTID:
1063                 ath10k_wmi_event_phyerr(ar, skb);
1064                 break;
1065         case WMI_ROAM_EVENTID:
1066                 ath10k_wmi_event_roam(ar, skb);
1067                 break;
1068         case WMI_PROFILE_MATCH:
1069                 ath10k_wmi_event_profile_match(ar, skb);
1070                 break;
1071         case WMI_DEBUG_PRINT_EVENTID:
1072                 ath10k_wmi_event_debug_print(ar, skb);
1073                 break;
1074         case WMI_PDEV_QVIT_EVENTID:
1075                 ath10k_wmi_event_pdev_qvit(ar, skb);
1076                 break;
1077         case WMI_WLAN_PROFILE_DATA_EVENTID:
1078                 ath10k_wmi_event_wlan_profile_data(ar, skb);
1079                 break;
1080         case WMI_RTT_MEASUREMENT_REPORT_EVENTID:
1081                 ath10k_wmi_event_rtt_measurement_report(ar, skb);
1082                 break;
1083         case WMI_TSF_MEASUREMENT_REPORT_EVENTID:
1084                 ath10k_wmi_event_tsf_measurement_report(ar, skb);
1085                 break;
1086         case WMI_RTT_ERROR_REPORT_EVENTID:
1087                 ath10k_wmi_event_rtt_error_report(ar, skb);
1088                 break;
1089         case WMI_WOW_WAKEUP_HOST_EVENTID:
1090                 ath10k_wmi_event_wow_wakeup_host(ar, skb);
1091                 break;
1092         case WMI_DCS_INTERFERENCE_EVENTID:
1093                 ath10k_wmi_event_dcs_interference(ar, skb);
1094                 break;
1095         case WMI_PDEV_TPC_CONFIG_EVENTID:
1096                 ath10k_wmi_event_pdev_tpc_config(ar, skb);
1097                 break;
1098         case WMI_PDEV_FTM_INTG_EVENTID:
1099                 ath10k_wmi_event_pdev_ftm_intg(ar, skb);
1100                 break;
1101         case WMI_GTK_OFFLOAD_STATUS_EVENTID:
1102                 ath10k_wmi_event_gtk_offload_status(ar, skb);
1103                 break;
1104         case WMI_GTK_REKEY_FAIL_EVENTID:
1105                 ath10k_wmi_event_gtk_rekey_fail(ar, skb);
1106                 break;
1107         case WMI_TX_DELBA_COMPLETE_EVENTID:
1108                 ath10k_wmi_event_delba_complete(ar, skb);
1109                 break;
1110         case WMI_TX_ADDBA_COMPLETE_EVENTID:
1111                 ath10k_wmi_event_addba_complete(ar, skb);
1112                 break;
1113         case WMI_VDEV_INSTALL_KEY_COMPLETE_EVENTID:
1114                 ath10k_wmi_event_vdev_install_key_complete(ar, skb);
1115                 break;
1116         case WMI_SERVICE_READY_EVENTID:
1117                 ath10k_wmi_service_ready_event_rx(ar, skb);
1118                 break;
1119         case WMI_READY_EVENTID:
1120                 ath10k_wmi_ready_event_rx(ar, skb);
1121                 break;
1122         default:
1123                 ath10k_warn("Unknown eventid: %d\n", id);
1124                 break;
1125         }
1126
1127         dev_kfree_skb(skb);
1128 }
1129
1130 static void ath10k_wmi_event_work(struct work_struct *work)
1131 {
1132         struct ath10k *ar = container_of(work, struct ath10k,
1133                                          wmi.wmi_event_work);
1134         struct sk_buff *skb;
1135
1136         for (;;) {
1137                 skb = skb_dequeue(&ar->wmi.wmi_event_list);
1138                 if (!skb)
1139                         break;
1140
1141                 ath10k_wmi_event_process(ar, skb);
1142         }
1143 }
1144
1145 static void ath10k_wmi_process_rx(struct ath10k *ar, struct sk_buff *skb)
1146 {
1147         struct wmi_cmd_hdr *cmd_hdr = (struct wmi_cmd_hdr *)skb->data;
1148         enum wmi_event_id event_id;
1149
1150         event_id = MS(__le32_to_cpu(cmd_hdr->cmd_id), WMI_CMD_HDR_CMD_ID);
1151
1152         /* some events require to be handled ASAP
1153          * thus can't be defered to a worker thread */
1154         switch (event_id) {
1155         case WMI_HOST_SWBA_EVENTID:
1156         case WMI_MGMT_RX_EVENTID:
1157                 ath10k_wmi_event_process(ar, skb);
1158                 return;
1159         default:
1160                 break;
1161         }
1162
1163         skb_queue_tail(&ar->wmi.wmi_event_list, skb);
1164         queue_work(ar->workqueue, &ar->wmi.wmi_event_work);
1165 }
1166
1167 /* WMI Initialization functions */
1168 int ath10k_wmi_attach(struct ath10k *ar)
1169 {
1170         init_completion(&ar->wmi.service_ready);
1171         init_completion(&ar->wmi.unified_ready);
1172         init_waitqueue_head(&ar->wmi.wq);
1173
1174         skb_queue_head_init(&ar->wmi.wmi_event_list);
1175         INIT_WORK(&ar->wmi.wmi_event_work, ath10k_wmi_event_work);
1176
1177         return 0;
1178 }
1179
1180 void ath10k_wmi_detach(struct ath10k *ar)
1181 {
1182         /* HTC should've drained the packets already */
1183         if (WARN_ON(atomic_read(&ar->wmi.pending_tx_count) > 0))
1184                 ath10k_warn("there are still pending packets\n");
1185
1186         cancel_work_sync(&ar->wmi.wmi_event_work);
1187         skb_queue_purge(&ar->wmi.wmi_event_list);
1188 }
1189
1190 int ath10k_wmi_connect_htc_service(struct ath10k *ar)
1191 {
1192         int status;
1193         struct ath10k_htc_svc_conn_req conn_req;
1194         struct ath10k_htc_svc_conn_resp conn_resp;
1195
1196         memset(&conn_req, 0, sizeof(conn_req));
1197         memset(&conn_resp, 0, sizeof(conn_resp));
1198
1199         /* these fields are the same for all service endpoints */
1200         conn_req.ep_ops.ep_tx_complete = ath10k_wmi_htc_tx_complete;
1201         conn_req.ep_ops.ep_rx_complete = ath10k_wmi_process_rx;
1202
1203         /* connect to control service */
1204         conn_req.service_id = ATH10K_HTC_SVC_ID_WMI_CONTROL;
1205
1206         status = ath10k_htc_connect_service(&ar->htc, &conn_req, &conn_resp);
1207         if (status) {
1208                 ath10k_warn("failed to connect to WMI CONTROL service status: %d\n",
1209                             status);
1210                 return status;
1211         }
1212
1213         ar->wmi.eid = conn_resp.eid;
1214         return 0;
1215 }
1216
1217 int ath10k_wmi_pdev_set_regdomain(struct ath10k *ar, u16 rd, u16 rd2g,
1218                                   u16 rd5g, u16 ctl2g, u16 ctl5g)
1219 {
1220         struct wmi_pdev_set_regdomain_cmd *cmd;
1221         struct sk_buff *skb;
1222
1223         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1224         if (!skb)
1225                 return -ENOMEM;
1226
1227         cmd = (struct wmi_pdev_set_regdomain_cmd *)skb->data;
1228         cmd->reg_domain = __cpu_to_le32(rd);
1229         cmd->reg_domain_2G = __cpu_to_le32(rd2g);
1230         cmd->reg_domain_5G = __cpu_to_le32(rd5g);
1231         cmd->conformance_test_limit_2G = __cpu_to_le32(ctl2g);
1232         cmd->conformance_test_limit_5G = __cpu_to_le32(ctl5g);
1233
1234         ath10k_dbg(ATH10K_DBG_WMI,
1235                    "wmi pdev regdomain rd %x rd2g %x rd5g %x ctl2g %x ctl5g %x\n",
1236                    rd, rd2g, rd5g, ctl2g, ctl5g);
1237
1238         return ath10k_wmi_cmd_send(ar, skb, WMI_PDEV_SET_REGDOMAIN_CMDID);
1239 }
1240
1241 int ath10k_wmi_pdev_set_channel(struct ath10k *ar,
1242                                 const struct wmi_channel_arg *arg)
1243 {
1244         struct wmi_set_channel_cmd *cmd;
1245         struct sk_buff *skb;
1246
1247         if (arg->passive)
1248                 return -EINVAL;
1249
1250         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1251         if (!skb)
1252                 return -ENOMEM;
1253
1254         cmd = (struct wmi_set_channel_cmd *)skb->data;
1255         cmd->chan.mhz               = __cpu_to_le32(arg->freq);
1256         cmd->chan.band_center_freq1 = __cpu_to_le32(arg->freq);
1257         cmd->chan.mode              = arg->mode;
1258         cmd->chan.min_power         = arg->min_power;
1259         cmd->chan.max_power         = arg->max_power;
1260         cmd->chan.reg_power         = arg->max_reg_power;
1261         cmd->chan.reg_classid       = arg->reg_class_id;
1262         cmd->chan.antenna_max       = arg->max_antenna_gain;
1263
1264         ath10k_dbg(ATH10K_DBG_WMI,
1265                    "wmi set channel mode %d freq %d\n",
1266                    arg->mode, arg->freq);
1267
1268         return ath10k_wmi_cmd_send(ar, skb, WMI_PDEV_SET_CHANNEL_CMDID);
1269 }
1270
1271 int ath10k_wmi_pdev_suspend_target(struct ath10k *ar)
1272 {
1273         struct wmi_pdev_suspend_cmd *cmd;
1274         struct sk_buff *skb;
1275
1276         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1277         if (!skb)
1278                 return -ENOMEM;
1279
1280         cmd = (struct wmi_pdev_suspend_cmd *)skb->data;
1281         cmd->suspend_opt = WMI_PDEV_SUSPEND;
1282
1283         return ath10k_wmi_cmd_send(ar, skb, WMI_PDEV_SUSPEND_CMDID);
1284 }
1285
1286 int ath10k_wmi_pdev_resume_target(struct ath10k *ar)
1287 {
1288         struct sk_buff *skb;
1289
1290         skb = ath10k_wmi_alloc_skb(0);
1291         if (skb == NULL)
1292                 return -ENOMEM;
1293
1294         return ath10k_wmi_cmd_send(ar, skb, WMI_PDEV_RESUME_CMDID);
1295 }
1296
1297 int ath10k_wmi_pdev_set_param(struct ath10k *ar, enum wmi_pdev_param id,
1298                               u32 value)
1299 {
1300         struct wmi_pdev_set_param_cmd *cmd;
1301         struct sk_buff *skb;
1302
1303         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1304         if (!skb)
1305                 return -ENOMEM;
1306
1307         cmd = (struct wmi_pdev_set_param_cmd *)skb->data;
1308         cmd->param_id    = __cpu_to_le32(id);
1309         cmd->param_value = __cpu_to_le32(value);
1310
1311         ath10k_dbg(ATH10K_DBG_WMI, "wmi pdev set param %d value %d\n",
1312                    id, value);
1313         return ath10k_wmi_cmd_send(ar, skb, WMI_PDEV_SET_PARAM_CMDID);
1314 }
1315
1316 int ath10k_wmi_cmd_init(struct ath10k *ar)
1317 {
1318         struct wmi_init_cmd *cmd;
1319         struct sk_buff *buf;
1320         struct wmi_resource_config config = {};
1321         u32 val;
1322
1323         config.num_vdevs = __cpu_to_le32(TARGET_NUM_VDEVS);
1324         config.num_peers = __cpu_to_le32(TARGET_NUM_PEERS + TARGET_NUM_VDEVS);
1325         config.num_offload_peers = __cpu_to_le32(TARGET_NUM_OFFLOAD_PEERS);
1326
1327         config.num_offload_reorder_bufs =
1328                 __cpu_to_le32(TARGET_NUM_OFFLOAD_REORDER_BUFS);
1329
1330         config.num_peer_keys = __cpu_to_le32(TARGET_NUM_PEER_KEYS);
1331         config.num_tids = __cpu_to_le32(TARGET_NUM_TIDS);
1332         config.ast_skid_limit = __cpu_to_le32(TARGET_AST_SKID_LIMIT);
1333         config.tx_chain_mask = __cpu_to_le32(TARGET_TX_CHAIN_MASK);
1334         config.rx_chain_mask = __cpu_to_le32(TARGET_RX_CHAIN_MASK);
1335         config.rx_timeout_pri_vo = __cpu_to_le32(TARGET_RX_TIMEOUT_LO_PRI);
1336         config.rx_timeout_pri_vi = __cpu_to_le32(TARGET_RX_TIMEOUT_LO_PRI);
1337         config.rx_timeout_pri_be = __cpu_to_le32(TARGET_RX_TIMEOUT_LO_PRI);
1338         config.rx_timeout_pri_bk = __cpu_to_le32(TARGET_RX_TIMEOUT_HI_PRI);
1339         config.rx_decap_mode = __cpu_to_le32(TARGET_RX_DECAP_MODE);
1340
1341         config.scan_max_pending_reqs =
1342                 __cpu_to_le32(TARGET_SCAN_MAX_PENDING_REQS);
1343
1344         config.bmiss_offload_max_vdev =
1345                 __cpu_to_le32(TARGET_BMISS_OFFLOAD_MAX_VDEV);
1346
1347         config.roam_offload_max_vdev =
1348                 __cpu_to_le32(TARGET_ROAM_OFFLOAD_MAX_VDEV);
1349
1350         config.roam_offload_max_ap_profiles =
1351                 __cpu_to_le32(TARGET_ROAM_OFFLOAD_MAX_AP_PROFILES);
1352
1353         config.num_mcast_groups = __cpu_to_le32(TARGET_NUM_MCAST_GROUPS);
1354         config.num_mcast_table_elems =
1355                 __cpu_to_le32(TARGET_NUM_MCAST_TABLE_ELEMS);
1356
1357         config.mcast2ucast_mode = __cpu_to_le32(TARGET_MCAST2UCAST_MODE);
1358         config.tx_dbg_log_size = __cpu_to_le32(TARGET_TX_DBG_LOG_SIZE);
1359         config.num_wds_entries = __cpu_to_le32(TARGET_NUM_WDS_ENTRIES);
1360         config.dma_burst_size = __cpu_to_le32(TARGET_DMA_BURST_SIZE);
1361         config.mac_aggr_delim = __cpu_to_le32(TARGET_MAC_AGGR_DELIM);
1362
1363         val = TARGET_RX_SKIP_DEFRAG_TIMEOUT_DUP_DETECTION_CHECK;
1364         config.rx_skip_defrag_timeout_dup_detection_check = __cpu_to_le32(val);
1365
1366         config.vow_config = __cpu_to_le32(TARGET_VOW_CONFIG);
1367
1368         config.gtk_offload_max_vdev =
1369                 __cpu_to_le32(TARGET_GTK_OFFLOAD_MAX_VDEV);
1370
1371         config.num_msdu_desc = __cpu_to_le32(TARGET_NUM_MSDU_DESC);
1372         config.max_frag_entries = __cpu_to_le32(TARGET_MAX_FRAG_ENTRIES);
1373
1374         buf = ath10k_wmi_alloc_skb(sizeof(*cmd));
1375         if (!buf)
1376                 return -ENOMEM;
1377
1378         cmd = (struct wmi_init_cmd *)buf->data;
1379         cmd->num_host_mem_chunks = 0;
1380         memcpy(&cmd->resource_config, &config, sizeof(config));
1381
1382         ath10k_dbg(ATH10K_DBG_WMI, "wmi init\n");
1383         return ath10k_wmi_cmd_send(ar, buf, WMI_INIT_CMDID);
1384 }
1385
1386 static int ath10k_wmi_start_scan_calc_len(const struct wmi_start_scan_arg *arg)
1387 {
1388         int len;
1389
1390         len = sizeof(struct wmi_start_scan_cmd);
1391
1392         if (arg->ie_len) {
1393                 if (!arg->ie)
1394                         return -EINVAL;
1395                 if (arg->ie_len > WLAN_SCAN_PARAMS_MAX_IE_LEN)
1396                         return -EINVAL;
1397
1398                 len += sizeof(struct wmi_ie_data);
1399                 len += roundup(arg->ie_len, 4);
1400         }
1401
1402         if (arg->n_channels) {
1403                 if (!arg->channels)
1404                         return -EINVAL;
1405                 if (arg->n_channels > ARRAY_SIZE(arg->channels))
1406                         return -EINVAL;
1407
1408                 len += sizeof(struct wmi_chan_list);
1409                 len += sizeof(__le32) * arg->n_channels;
1410         }
1411
1412         if (arg->n_ssids) {
1413                 if (!arg->ssids)
1414                         return -EINVAL;
1415                 if (arg->n_ssids > WLAN_SCAN_PARAMS_MAX_SSID)
1416                         return -EINVAL;
1417
1418                 len += sizeof(struct wmi_ssid_list);
1419                 len += sizeof(struct wmi_ssid) * arg->n_ssids;
1420         }
1421
1422         if (arg->n_bssids) {
1423                 if (!arg->bssids)
1424                         return -EINVAL;
1425                 if (arg->n_bssids > WLAN_SCAN_PARAMS_MAX_BSSID)
1426                         return -EINVAL;
1427
1428                 len += sizeof(struct wmi_bssid_list);
1429                 len += sizeof(struct wmi_mac_addr) * arg->n_bssids;
1430         }
1431
1432         return len;
1433 }
1434
1435 int ath10k_wmi_start_scan(struct ath10k *ar,
1436                           const struct wmi_start_scan_arg *arg)
1437 {
1438         struct wmi_start_scan_cmd *cmd;
1439         struct sk_buff *skb;
1440         struct wmi_ie_data *ie;
1441         struct wmi_chan_list *channels;
1442         struct wmi_ssid_list *ssids;
1443         struct wmi_bssid_list *bssids;
1444         u32 scan_id;
1445         u32 scan_req_id;
1446         int off;
1447         int len = 0;
1448         int i;
1449
1450         len = ath10k_wmi_start_scan_calc_len(arg);
1451         if (len < 0)
1452                 return len; /* len contains error code here */
1453
1454         skb = ath10k_wmi_alloc_skb(len);
1455         if (!skb)
1456                 return -ENOMEM;
1457
1458         scan_id  = WMI_HOST_SCAN_REQ_ID_PREFIX;
1459         scan_id |= arg->scan_id;
1460
1461         scan_req_id  = WMI_HOST_SCAN_REQUESTOR_ID_PREFIX;
1462         scan_req_id |= arg->scan_req_id;
1463
1464         cmd = (struct wmi_start_scan_cmd *)skb->data;
1465         cmd->scan_id            = __cpu_to_le32(scan_id);
1466         cmd->scan_req_id        = __cpu_to_le32(scan_req_id);
1467         cmd->vdev_id            = __cpu_to_le32(arg->vdev_id);
1468         cmd->scan_priority      = __cpu_to_le32(arg->scan_priority);
1469         cmd->notify_scan_events = __cpu_to_le32(arg->notify_scan_events);
1470         cmd->dwell_time_active  = __cpu_to_le32(arg->dwell_time_active);
1471         cmd->dwell_time_passive = __cpu_to_le32(arg->dwell_time_passive);
1472         cmd->min_rest_time      = __cpu_to_le32(arg->min_rest_time);
1473         cmd->max_rest_time      = __cpu_to_le32(arg->max_rest_time);
1474         cmd->repeat_probe_time  = __cpu_to_le32(arg->repeat_probe_time);
1475         cmd->probe_spacing_time = __cpu_to_le32(arg->probe_spacing_time);
1476         cmd->idle_time          = __cpu_to_le32(arg->idle_time);
1477         cmd->max_scan_time      = __cpu_to_le32(arg->max_scan_time);
1478         cmd->probe_delay        = __cpu_to_le32(arg->probe_delay);
1479         cmd->scan_ctrl_flags    = __cpu_to_le32(arg->scan_ctrl_flags);
1480
1481         /* TLV list starts after fields included in the struct */
1482         off = sizeof(*cmd);
1483
1484         if (arg->n_channels) {
1485                 channels = (void *)skb->data + off;
1486                 channels->tag = __cpu_to_le32(WMI_CHAN_LIST_TAG);
1487                 channels->num_chan = __cpu_to_le32(arg->n_channels);
1488
1489                 for (i = 0; i < arg->n_channels; i++)
1490                         channels->channel_list[i] =
1491                                 __cpu_to_le32(arg->channels[i]);
1492
1493                 off += sizeof(*channels);
1494                 off += sizeof(__le32) * arg->n_channels;
1495         }
1496
1497         if (arg->n_ssids) {
1498                 ssids = (void *)skb->data + off;
1499                 ssids->tag = __cpu_to_le32(WMI_SSID_LIST_TAG);
1500                 ssids->num_ssids = __cpu_to_le32(arg->n_ssids);
1501
1502                 for (i = 0; i < arg->n_ssids; i++) {
1503                         ssids->ssids[i].ssid_len =
1504                                 __cpu_to_le32(arg->ssids[i].len);
1505                         memcpy(&ssids->ssids[i].ssid,
1506                                arg->ssids[i].ssid,
1507                                arg->ssids[i].len);
1508                 }
1509
1510                 off += sizeof(*ssids);
1511                 off += sizeof(struct wmi_ssid) * arg->n_ssids;
1512         }
1513
1514         if (arg->n_bssids) {
1515                 bssids = (void *)skb->data + off;
1516                 bssids->tag = __cpu_to_le32(WMI_BSSID_LIST_TAG);
1517                 bssids->num_bssid = __cpu_to_le32(arg->n_bssids);
1518
1519                 for (i = 0; i < arg->n_bssids; i++)
1520                         memcpy(&bssids->bssid_list[i],
1521                                arg->bssids[i].bssid,
1522                                ETH_ALEN);
1523
1524                 off += sizeof(*bssids);
1525                 off += sizeof(struct wmi_mac_addr) * arg->n_bssids;
1526         }
1527
1528         if (arg->ie_len) {
1529                 ie = (void *)skb->data + off;
1530                 ie->tag = __cpu_to_le32(WMI_IE_TAG);
1531                 ie->ie_len = __cpu_to_le32(arg->ie_len);
1532                 memcpy(ie->ie_data, arg->ie, arg->ie_len);
1533
1534                 off += sizeof(*ie);
1535                 off += roundup(arg->ie_len, 4);
1536         }
1537
1538         if (off != skb->len) {
1539                 dev_kfree_skb(skb);
1540                 return -EINVAL;
1541         }
1542
1543         ath10k_dbg(ATH10K_DBG_WMI, "wmi start scan\n");
1544         return ath10k_wmi_cmd_send(ar, skb, WMI_START_SCAN_CMDID);
1545 }
1546
1547 void ath10k_wmi_start_scan_init(struct ath10k *ar,
1548                                 struct wmi_start_scan_arg *arg)
1549 {
1550         /* setup commonly used values */
1551         arg->scan_req_id = 1;
1552         arg->scan_priority = WMI_SCAN_PRIORITY_LOW;
1553         arg->dwell_time_active = 50;
1554         arg->dwell_time_passive = 150;
1555         arg->min_rest_time = 50;
1556         arg->max_rest_time = 500;
1557         arg->repeat_probe_time = 0;
1558         arg->probe_spacing_time = 0;
1559         arg->idle_time = 0;
1560         arg->max_scan_time = 5000;
1561         arg->probe_delay = 5;
1562         arg->notify_scan_events = WMI_SCAN_EVENT_STARTED
1563                 | WMI_SCAN_EVENT_COMPLETED
1564                 | WMI_SCAN_EVENT_BSS_CHANNEL
1565                 | WMI_SCAN_EVENT_FOREIGN_CHANNEL
1566                 | WMI_SCAN_EVENT_DEQUEUED;
1567         arg->scan_ctrl_flags |= WMI_SCAN_ADD_OFDM_RATES;
1568         arg->scan_ctrl_flags |= WMI_SCAN_CHAN_STAT_EVENT;
1569         arg->n_bssids = 1;
1570         arg->bssids[0].bssid = "\xFF\xFF\xFF\xFF\xFF\xFF";
1571 }
1572
1573 int ath10k_wmi_stop_scan(struct ath10k *ar, const struct wmi_stop_scan_arg *arg)
1574 {
1575         struct wmi_stop_scan_cmd *cmd;
1576         struct sk_buff *skb;
1577         u32 scan_id;
1578         u32 req_id;
1579
1580         if (arg->req_id > 0xFFF)
1581                 return -EINVAL;
1582         if (arg->req_type == WMI_SCAN_STOP_ONE && arg->u.scan_id > 0xFFF)
1583                 return -EINVAL;
1584
1585         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1586         if (!skb)
1587                 return -ENOMEM;
1588
1589         scan_id = arg->u.scan_id;
1590         scan_id |= WMI_HOST_SCAN_REQ_ID_PREFIX;
1591
1592         req_id = arg->req_id;
1593         req_id |= WMI_HOST_SCAN_REQUESTOR_ID_PREFIX;
1594
1595         cmd = (struct wmi_stop_scan_cmd *)skb->data;
1596         cmd->req_type    = __cpu_to_le32(arg->req_type);
1597         cmd->vdev_id     = __cpu_to_le32(arg->u.vdev_id);
1598         cmd->scan_id     = __cpu_to_le32(scan_id);
1599         cmd->scan_req_id = __cpu_to_le32(req_id);
1600
1601         ath10k_dbg(ATH10K_DBG_WMI,
1602                    "wmi stop scan reqid %d req_type %d vdev/scan_id %d\n",
1603                    arg->req_id, arg->req_type, arg->u.scan_id);
1604         return ath10k_wmi_cmd_send(ar, skb, WMI_STOP_SCAN_CMDID);
1605 }
1606
1607 int ath10k_wmi_vdev_create(struct ath10k *ar, u32 vdev_id,
1608                            enum wmi_vdev_type type,
1609                            enum wmi_vdev_subtype subtype,
1610                            const u8 macaddr[ETH_ALEN])
1611 {
1612         struct wmi_vdev_create_cmd *cmd;
1613         struct sk_buff *skb;
1614
1615         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1616         if (!skb)
1617                 return -ENOMEM;
1618
1619         cmd = (struct wmi_vdev_create_cmd *)skb->data;
1620         cmd->vdev_id      = __cpu_to_le32(vdev_id);
1621         cmd->vdev_type    = __cpu_to_le32(type);
1622         cmd->vdev_subtype = __cpu_to_le32(subtype);
1623         memcpy(cmd->vdev_macaddr.addr, macaddr, ETH_ALEN);
1624
1625         ath10k_dbg(ATH10K_DBG_WMI,
1626                    "WMI vdev create: id %d type %d subtype %d macaddr %pM\n",
1627                    vdev_id, type, subtype, macaddr);
1628
1629         return ath10k_wmi_cmd_send(ar, skb, WMI_VDEV_CREATE_CMDID);
1630 }
1631
1632 int ath10k_wmi_vdev_delete(struct ath10k *ar, u32 vdev_id)
1633 {
1634         struct wmi_vdev_delete_cmd *cmd;
1635         struct sk_buff *skb;
1636
1637         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1638         if (!skb)
1639                 return -ENOMEM;
1640
1641         cmd = (struct wmi_vdev_delete_cmd *)skb->data;
1642         cmd->vdev_id = __cpu_to_le32(vdev_id);
1643
1644         ath10k_dbg(ATH10K_DBG_WMI,
1645                    "WMI vdev delete id %d\n", vdev_id);
1646
1647         return ath10k_wmi_cmd_send(ar, skb, WMI_VDEV_DELETE_CMDID);
1648 }
1649
1650 static int ath10k_wmi_vdev_start_restart(struct ath10k *ar,
1651                                 const struct wmi_vdev_start_request_arg *arg,
1652                                 enum wmi_cmd_id cmd_id)
1653 {
1654         struct wmi_vdev_start_request_cmd *cmd;
1655         struct sk_buff *skb;
1656         const char *cmdname;
1657         u32 flags = 0;
1658
1659         if (cmd_id != WMI_VDEV_START_REQUEST_CMDID &&
1660             cmd_id != WMI_VDEV_RESTART_REQUEST_CMDID)
1661                 return -EINVAL;
1662         if (WARN_ON(arg->ssid && arg->ssid_len == 0))
1663                 return -EINVAL;
1664         if (WARN_ON(arg->hidden_ssid && !arg->ssid))
1665                 return -EINVAL;
1666         if (WARN_ON(arg->ssid_len > sizeof(cmd->ssid.ssid)))
1667                 return -EINVAL;
1668
1669         if (cmd_id == WMI_VDEV_START_REQUEST_CMDID)
1670                 cmdname = "start";
1671         else if (cmd_id == WMI_VDEV_RESTART_REQUEST_CMDID)
1672                 cmdname = "restart";
1673         else
1674                 return -EINVAL; /* should not happen, we already check cmd_id */
1675
1676         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1677         if (!skb)
1678                 return -ENOMEM;
1679
1680         if (arg->hidden_ssid)
1681                 flags |= WMI_VDEV_START_HIDDEN_SSID;
1682         if (arg->pmf_enabled)
1683                 flags |= WMI_VDEV_START_PMF_ENABLED;
1684
1685         cmd = (struct wmi_vdev_start_request_cmd *)skb->data;
1686         cmd->vdev_id         = __cpu_to_le32(arg->vdev_id);
1687         cmd->disable_hw_ack  = __cpu_to_le32(arg->disable_hw_ack);
1688         cmd->beacon_interval = __cpu_to_le32(arg->bcn_intval);
1689         cmd->dtim_period     = __cpu_to_le32(arg->dtim_period);
1690         cmd->flags           = __cpu_to_le32(flags);
1691         cmd->bcn_tx_rate     = __cpu_to_le32(arg->bcn_tx_rate);
1692         cmd->bcn_tx_power    = __cpu_to_le32(arg->bcn_tx_power);
1693
1694         if (arg->ssid) {
1695                 cmd->ssid.ssid_len = __cpu_to_le32(arg->ssid_len);
1696                 memcpy(cmd->ssid.ssid, arg->ssid, arg->ssid_len);
1697         }
1698
1699         cmd->chan.mhz = __cpu_to_le32(arg->channel.freq);
1700
1701         cmd->chan.band_center_freq1 =
1702                 __cpu_to_le32(arg->channel.band_center_freq1);
1703
1704         cmd->chan.mode = arg->channel.mode;
1705         cmd->chan.min_power = arg->channel.min_power;
1706         cmd->chan.max_power = arg->channel.max_power;
1707         cmd->chan.reg_power = arg->channel.max_reg_power;
1708         cmd->chan.reg_classid = arg->channel.reg_class_id;
1709         cmd->chan.antenna_max = arg->channel.max_antenna_gain;
1710
1711         ath10k_dbg(ATH10K_DBG_WMI,
1712                    "wmi vdev %s id 0x%x freq %d, mode %d, ch_flags: 0x%0X,"
1713                    "max_power: %d\n", cmdname, arg->vdev_id, arg->channel.freq,
1714                    arg->channel.mode, flags, arg->channel.max_power);
1715
1716         return ath10k_wmi_cmd_send(ar, skb, cmd_id);
1717 }
1718
1719 int ath10k_wmi_vdev_start(struct ath10k *ar,
1720                           const struct wmi_vdev_start_request_arg *arg)
1721 {
1722         return ath10k_wmi_vdev_start_restart(ar, arg,
1723                                              WMI_VDEV_START_REQUEST_CMDID);
1724 }
1725
1726 int ath10k_wmi_vdev_restart(struct ath10k *ar,
1727                      const struct wmi_vdev_start_request_arg *arg)
1728 {
1729         return ath10k_wmi_vdev_start_restart(ar, arg,
1730                                              WMI_VDEV_RESTART_REQUEST_CMDID);
1731 }
1732
1733 int ath10k_wmi_vdev_stop(struct ath10k *ar, u32 vdev_id)
1734 {
1735         struct wmi_vdev_stop_cmd *cmd;
1736         struct sk_buff *skb;
1737
1738         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1739         if (!skb)
1740                 return -ENOMEM;
1741
1742         cmd = (struct wmi_vdev_stop_cmd *)skb->data;
1743         cmd->vdev_id = __cpu_to_le32(vdev_id);
1744
1745         ath10k_dbg(ATH10K_DBG_WMI, "wmi vdev stop id 0x%x\n", vdev_id);
1746
1747         return ath10k_wmi_cmd_send(ar, skb, WMI_VDEV_STOP_CMDID);
1748 }
1749
1750 int ath10k_wmi_vdev_up(struct ath10k *ar, u32 vdev_id, u32 aid, const u8 *bssid)
1751 {
1752         struct wmi_vdev_up_cmd *cmd;
1753         struct sk_buff *skb;
1754
1755         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1756         if (!skb)
1757                 return -ENOMEM;
1758
1759         cmd = (struct wmi_vdev_up_cmd *)skb->data;
1760         cmd->vdev_id       = __cpu_to_le32(vdev_id);
1761         cmd->vdev_assoc_id = __cpu_to_le32(aid);
1762         memcpy(&cmd->vdev_bssid.addr, bssid, 6);
1763
1764         ath10k_dbg(ATH10K_DBG_WMI,
1765                    "wmi mgmt vdev up id 0x%x assoc id %d bssid %pM\n",
1766                    vdev_id, aid, bssid);
1767
1768         return ath10k_wmi_cmd_send(ar, skb, WMI_VDEV_UP_CMDID);
1769 }
1770
1771 int ath10k_wmi_vdev_down(struct ath10k *ar, u32 vdev_id)
1772 {
1773         struct wmi_vdev_down_cmd *cmd;
1774         struct sk_buff *skb;
1775
1776         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1777         if (!skb)
1778                 return -ENOMEM;
1779
1780         cmd = (struct wmi_vdev_down_cmd *)skb->data;
1781         cmd->vdev_id = __cpu_to_le32(vdev_id);
1782
1783         ath10k_dbg(ATH10K_DBG_WMI,
1784                    "wmi mgmt vdev down id 0x%x\n", vdev_id);
1785
1786         return ath10k_wmi_cmd_send(ar, skb, WMI_VDEV_DOWN_CMDID);
1787 }
1788
1789 int ath10k_wmi_vdev_set_param(struct ath10k *ar, u32 vdev_id,
1790                               enum wmi_vdev_param param_id, u32 param_value)
1791 {
1792         struct wmi_vdev_set_param_cmd *cmd;
1793         struct sk_buff *skb;
1794
1795         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1796         if (!skb)
1797                 return -ENOMEM;
1798
1799         cmd = (struct wmi_vdev_set_param_cmd *)skb->data;
1800         cmd->vdev_id     = __cpu_to_le32(vdev_id);
1801         cmd->param_id    = __cpu_to_le32(param_id);
1802         cmd->param_value = __cpu_to_le32(param_value);
1803
1804         ath10k_dbg(ATH10K_DBG_WMI,
1805                    "wmi vdev id 0x%x set param %d value %d\n",
1806                    vdev_id, param_id, param_value);
1807
1808         return ath10k_wmi_cmd_send(ar, skb, WMI_VDEV_SET_PARAM_CMDID);
1809 }
1810
1811 int ath10k_wmi_vdev_install_key(struct ath10k *ar,
1812                                 const struct wmi_vdev_install_key_arg *arg)
1813 {
1814         struct wmi_vdev_install_key_cmd *cmd;
1815         struct sk_buff *skb;
1816
1817         if (arg->key_cipher == WMI_CIPHER_NONE && arg->key_data != NULL)
1818                 return -EINVAL;
1819         if (arg->key_cipher != WMI_CIPHER_NONE && arg->key_data == NULL)
1820                 return -EINVAL;
1821
1822         skb = ath10k_wmi_alloc_skb(sizeof(*cmd) + arg->key_len);
1823         if (!skb)
1824                 return -ENOMEM;
1825
1826         cmd = (struct wmi_vdev_install_key_cmd *)skb->data;
1827         cmd->vdev_id       = __cpu_to_le32(arg->vdev_id);
1828         cmd->key_idx       = __cpu_to_le32(arg->key_idx);
1829         cmd->key_flags     = __cpu_to_le32(arg->key_flags);
1830         cmd->key_cipher    = __cpu_to_le32(arg->key_cipher);
1831         cmd->key_len       = __cpu_to_le32(arg->key_len);
1832         cmd->key_txmic_len = __cpu_to_le32(arg->key_txmic_len);
1833         cmd->key_rxmic_len = __cpu_to_le32(arg->key_rxmic_len);
1834
1835         if (arg->macaddr)
1836                 memcpy(cmd->peer_macaddr.addr, arg->macaddr, ETH_ALEN);
1837         if (arg->key_data)
1838                 memcpy(cmd->key_data, arg->key_data, arg->key_len);
1839
1840         ath10k_dbg(ATH10K_DBG_WMI,
1841                    "wmi vdev install key idx %d cipher %d len %d\n",
1842                    arg->key_idx, arg->key_cipher, arg->key_len);
1843         return ath10k_wmi_cmd_send(ar, skb, WMI_VDEV_INSTALL_KEY_CMDID);
1844 }
1845
1846 int ath10k_wmi_peer_create(struct ath10k *ar, u32 vdev_id,
1847                            const u8 peer_addr[ETH_ALEN])
1848 {
1849         struct wmi_peer_create_cmd *cmd;
1850         struct sk_buff *skb;
1851
1852         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1853         if (!skb)
1854                 return -ENOMEM;
1855
1856         cmd = (struct wmi_peer_create_cmd *)skb->data;
1857         cmd->vdev_id = __cpu_to_le32(vdev_id);
1858         memcpy(cmd->peer_macaddr.addr, peer_addr, ETH_ALEN);
1859
1860         ath10k_dbg(ATH10K_DBG_WMI,
1861                    "wmi peer create vdev_id %d peer_addr %pM\n",
1862                    vdev_id, peer_addr);
1863         return ath10k_wmi_cmd_send(ar, skb, WMI_PEER_CREATE_CMDID);
1864 }
1865
1866 int ath10k_wmi_peer_delete(struct ath10k *ar, u32 vdev_id,
1867                            const u8 peer_addr[ETH_ALEN])
1868 {
1869         struct wmi_peer_delete_cmd *cmd;
1870         struct sk_buff *skb;
1871
1872         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1873         if (!skb)
1874                 return -ENOMEM;
1875
1876         cmd = (struct wmi_peer_delete_cmd *)skb->data;
1877         cmd->vdev_id = __cpu_to_le32(vdev_id);
1878         memcpy(cmd->peer_macaddr.addr, peer_addr, ETH_ALEN);
1879
1880         ath10k_dbg(ATH10K_DBG_WMI,
1881                    "wmi peer delete vdev_id %d peer_addr %pM\n",
1882                    vdev_id, peer_addr);
1883         return ath10k_wmi_cmd_send(ar, skb, WMI_PEER_DELETE_CMDID);
1884 }
1885
1886 int ath10k_wmi_peer_flush(struct ath10k *ar, u32 vdev_id,
1887                           const u8 peer_addr[ETH_ALEN], u32 tid_bitmap)
1888 {
1889         struct wmi_peer_flush_tids_cmd *cmd;
1890         struct sk_buff *skb;
1891
1892         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1893         if (!skb)
1894                 return -ENOMEM;
1895
1896         cmd = (struct wmi_peer_flush_tids_cmd *)skb->data;
1897         cmd->vdev_id         = __cpu_to_le32(vdev_id);
1898         cmd->peer_tid_bitmap = __cpu_to_le32(tid_bitmap);
1899         memcpy(cmd->peer_macaddr.addr, peer_addr, ETH_ALEN);
1900
1901         ath10k_dbg(ATH10K_DBG_WMI,
1902                    "wmi peer flush vdev_id %d peer_addr %pM tids %08x\n",
1903                    vdev_id, peer_addr, tid_bitmap);
1904         return ath10k_wmi_cmd_send(ar, skb, WMI_PEER_FLUSH_TIDS_CMDID);
1905 }
1906
1907 int ath10k_wmi_peer_set_param(struct ath10k *ar, u32 vdev_id,
1908                               const u8 *peer_addr, enum wmi_peer_param param_id,
1909                               u32 param_value)
1910 {
1911         struct wmi_peer_set_param_cmd *cmd;
1912         struct sk_buff *skb;
1913
1914         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1915         if (!skb)
1916                 return -ENOMEM;
1917
1918         cmd = (struct wmi_peer_set_param_cmd *)skb->data;
1919         cmd->vdev_id     = __cpu_to_le32(vdev_id);
1920         cmd->param_id    = __cpu_to_le32(param_id);
1921         cmd->param_value = __cpu_to_le32(param_value);
1922         memcpy(&cmd->peer_macaddr.addr, peer_addr, 6);
1923
1924         ath10k_dbg(ATH10K_DBG_WMI,
1925                    "wmi vdev %d peer 0x%pM set param %d value %d\n",
1926                    vdev_id, peer_addr, param_id, param_value);
1927
1928         return ath10k_wmi_cmd_send(ar, skb, WMI_PEER_SET_PARAM_CMDID);
1929 }
1930
1931 int ath10k_wmi_set_psmode(struct ath10k *ar, u32 vdev_id,
1932                           enum wmi_sta_ps_mode psmode)
1933 {
1934         struct wmi_sta_powersave_mode_cmd *cmd;
1935         struct sk_buff *skb;
1936
1937         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1938         if (!skb)
1939                 return -ENOMEM;
1940
1941         cmd = (struct wmi_sta_powersave_mode_cmd *)skb->data;
1942         cmd->vdev_id     = __cpu_to_le32(vdev_id);
1943         cmd->sta_ps_mode = __cpu_to_le32(psmode);
1944
1945         ath10k_dbg(ATH10K_DBG_WMI,
1946                    "wmi set powersave id 0x%x mode %d\n",
1947                    vdev_id, psmode);
1948
1949         return ath10k_wmi_cmd_send(ar, skb, WMI_STA_POWERSAVE_MODE_CMDID);
1950 }
1951
1952 int ath10k_wmi_set_sta_ps_param(struct ath10k *ar, u32 vdev_id,
1953                                 enum wmi_sta_powersave_param param_id,
1954                                 u32 value)
1955 {
1956         struct wmi_sta_powersave_param_cmd *cmd;
1957         struct sk_buff *skb;
1958
1959         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1960         if (!skb)
1961                 return -ENOMEM;
1962
1963         cmd = (struct wmi_sta_powersave_param_cmd *)skb->data;
1964         cmd->vdev_id     = __cpu_to_le32(vdev_id);
1965         cmd->param_id    = __cpu_to_le32(param_id);
1966         cmd->param_value = __cpu_to_le32(value);
1967
1968         ath10k_dbg(ATH10K_DBG_WMI,
1969                    "wmi sta ps param vdev_id 0x%x param %d value %d\n",
1970                    vdev_id, param_id, value);
1971         return ath10k_wmi_cmd_send(ar, skb, WMI_STA_POWERSAVE_PARAM_CMDID);
1972 }
1973
1974 int ath10k_wmi_set_ap_ps_param(struct ath10k *ar, u32 vdev_id, const u8 *mac,
1975                                enum wmi_ap_ps_peer_param param_id, u32 value)
1976 {
1977         struct wmi_ap_ps_peer_cmd *cmd;
1978         struct sk_buff *skb;
1979
1980         if (!mac)
1981                 return -EINVAL;
1982
1983         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
1984         if (!skb)
1985                 return -ENOMEM;
1986
1987         cmd = (struct wmi_ap_ps_peer_cmd *)skb->data;
1988         cmd->vdev_id = __cpu_to_le32(vdev_id);
1989         cmd->param_id = __cpu_to_le32(param_id);
1990         cmd->param_value = __cpu_to_le32(value);
1991         memcpy(&cmd->peer_macaddr, mac, ETH_ALEN);
1992
1993         ath10k_dbg(ATH10K_DBG_WMI,
1994                    "wmi ap ps param vdev_id 0x%X param %d value %d mac_addr %pM\n",
1995                    vdev_id, param_id, value, mac);
1996
1997         return ath10k_wmi_cmd_send(ar, skb, WMI_AP_PS_PEER_PARAM_CMDID);
1998 }
1999
2000 int ath10k_wmi_scan_chan_list(struct ath10k *ar,
2001                               const struct wmi_scan_chan_list_arg *arg)
2002 {
2003         struct wmi_scan_chan_list_cmd *cmd;
2004         struct sk_buff *skb;
2005         struct wmi_channel_arg *ch;
2006         struct wmi_channel *ci;
2007         int len;
2008         int i;
2009
2010         len = sizeof(*cmd) + arg->n_channels * sizeof(struct wmi_channel);
2011
2012         skb = ath10k_wmi_alloc_skb(len);
2013         if (!skb)
2014                 return -EINVAL;
2015
2016         cmd = (struct wmi_scan_chan_list_cmd *)skb->data;
2017         cmd->num_scan_chans = __cpu_to_le32(arg->n_channels);
2018
2019         for (i = 0; i < arg->n_channels; i++) {
2020                 u32 flags = 0;
2021
2022                 ch = &arg->channels[i];
2023                 ci = &cmd->chan_info[i];
2024
2025                 if (ch->passive)
2026                         flags |= WMI_CHAN_FLAG_PASSIVE;
2027                 if (ch->allow_ibss)
2028                         flags |= WMI_CHAN_FLAG_ADHOC_ALLOWED;
2029                 if (ch->allow_ht)
2030                         flags |= WMI_CHAN_FLAG_ALLOW_HT;
2031                 if (ch->allow_vht)
2032                         flags |= WMI_CHAN_FLAG_ALLOW_VHT;
2033                 if (ch->ht40plus)
2034                         flags |= WMI_CHAN_FLAG_HT40_PLUS;
2035
2036                 ci->mhz               = __cpu_to_le32(ch->freq);
2037                 ci->band_center_freq1 = __cpu_to_le32(ch->freq);
2038                 ci->band_center_freq2 = 0;
2039                 ci->min_power         = ch->min_power;
2040                 ci->max_power         = ch->max_power;
2041                 ci->reg_power         = ch->max_reg_power;
2042                 ci->antenna_max       = ch->max_antenna_gain;
2043                 ci->antenna_max       = 0;
2044
2045                 /* mode & flags share storage */
2046                 ci->mode              = ch->mode;
2047                 ci->flags            |= __cpu_to_le32(flags);
2048         }
2049
2050         return ath10k_wmi_cmd_send(ar, skb, WMI_SCAN_CHAN_LIST_CMDID);
2051 }
2052
2053 int ath10k_wmi_peer_assoc(struct ath10k *ar,
2054                           const struct wmi_peer_assoc_complete_arg *arg)
2055 {
2056         struct wmi_peer_assoc_complete_cmd *cmd;
2057         struct sk_buff *skb;
2058
2059         if (arg->peer_mpdu_density > 16)
2060                 return -EINVAL;
2061         if (arg->peer_legacy_rates.num_rates > MAX_SUPPORTED_RATES)
2062                 return -EINVAL;
2063         if (arg->peer_ht_rates.num_rates > MAX_SUPPORTED_RATES)
2064                 return -EINVAL;
2065
2066         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
2067         if (!skb)
2068                 return -ENOMEM;
2069
2070         cmd = (struct wmi_peer_assoc_complete_cmd *)skb->data;
2071         cmd->vdev_id            = __cpu_to_le32(arg->vdev_id);
2072         cmd->peer_new_assoc     = __cpu_to_le32(arg->peer_reassoc ? 0 : 1);
2073         cmd->peer_associd       = __cpu_to_le32(arg->peer_aid);
2074         cmd->peer_flags         = __cpu_to_le32(arg->peer_flags);
2075         cmd->peer_caps          = __cpu_to_le32(arg->peer_caps);
2076         cmd->peer_listen_intval = __cpu_to_le32(arg->peer_listen_intval);
2077         cmd->peer_ht_caps       = __cpu_to_le32(arg->peer_ht_caps);
2078         cmd->peer_max_mpdu      = __cpu_to_le32(arg->peer_max_mpdu);
2079         cmd->peer_mpdu_density  = __cpu_to_le32(arg->peer_mpdu_density);
2080         cmd->peer_rate_caps     = __cpu_to_le32(arg->peer_rate_caps);
2081         cmd->peer_nss           = __cpu_to_le32(arg->peer_num_spatial_streams);
2082         cmd->peer_vht_caps      = __cpu_to_le32(arg->peer_vht_caps);
2083         cmd->peer_phymode       = __cpu_to_le32(arg->peer_phymode);
2084
2085         memcpy(cmd->peer_macaddr.addr, arg->addr, ETH_ALEN);
2086
2087         cmd->peer_legacy_rates.num_rates =
2088                 __cpu_to_le32(arg->peer_legacy_rates.num_rates);
2089         memcpy(cmd->peer_legacy_rates.rates, arg->peer_legacy_rates.rates,
2090                arg->peer_legacy_rates.num_rates);
2091
2092         cmd->peer_ht_rates.num_rates =
2093                 __cpu_to_le32(arg->peer_ht_rates.num_rates);
2094         memcpy(cmd->peer_ht_rates.rates, arg->peer_ht_rates.rates,
2095                arg->peer_ht_rates.num_rates);
2096
2097         cmd->peer_vht_rates.rx_max_rate =
2098                 __cpu_to_le32(arg->peer_vht_rates.rx_max_rate);
2099         cmd->peer_vht_rates.rx_mcs_set =
2100                 __cpu_to_le32(arg->peer_vht_rates.rx_mcs_set);
2101         cmd->peer_vht_rates.tx_max_rate =
2102                 __cpu_to_le32(arg->peer_vht_rates.tx_max_rate);
2103         cmd->peer_vht_rates.tx_mcs_set =
2104                 __cpu_to_le32(arg->peer_vht_rates.tx_mcs_set);
2105
2106         ath10k_dbg(ATH10K_DBG_WMI,
2107                    "wmi peer assoc vdev %d addr %pM\n",
2108                    arg->vdev_id, arg->addr);
2109         return ath10k_wmi_cmd_send(ar, skb, WMI_PEER_ASSOC_CMDID);
2110 }
2111
2112 int ath10k_wmi_beacon_send(struct ath10k *ar, const struct wmi_bcn_tx_arg *arg)
2113 {
2114         struct wmi_bcn_tx_cmd *cmd;
2115         struct sk_buff *skb;
2116
2117         skb = ath10k_wmi_alloc_skb(sizeof(*cmd) + arg->bcn_len);
2118         if (!skb)
2119                 return -ENOMEM;
2120
2121         cmd = (struct wmi_bcn_tx_cmd *)skb->data;
2122         cmd->hdr.vdev_id  = __cpu_to_le32(arg->vdev_id);
2123         cmd->hdr.tx_rate  = __cpu_to_le32(arg->tx_rate);
2124         cmd->hdr.tx_power = __cpu_to_le32(arg->tx_power);
2125         cmd->hdr.bcn_len  = __cpu_to_le32(arg->bcn_len);
2126         memcpy(cmd->bcn, arg->bcn, arg->bcn_len);
2127
2128         return ath10k_wmi_cmd_send(ar, skb, WMI_BCN_TX_CMDID);
2129 }
2130
2131 static void ath10k_wmi_pdev_set_wmm_param(struct wmi_wmm_params *params,
2132                                           const struct wmi_wmm_params_arg *arg)
2133 {
2134         params->cwmin  = __cpu_to_le32(arg->cwmin);
2135         params->cwmax  = __cpu_to_le32(arg->cwmax);
2136         params->aifs   = __cpu_to_le32(arg->aifs);
2137         params->txop   = __cpu_to_le32(arg->txop);
2138         params->acm    = __cpu_to_le32(arg->acm);
2139         params->no_ack = __cpu_to_le32(arg->no_ack);
2140 }
2141
2142 int ath10k_wmi_pdev_set_wmm_params(struct ath10k *ar,
2143                         const struct wmi_pdev_set_wmm_params_arg *arg)
2144 {
2145         struct wmi_pdev_set_wmm_params *cmd;
2146         struct sk_buff *skb;
2147
2148         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
2149         if (!skb)
2150                 return -ENOMEM;
2151
2152         cmd = (struct wmi_pdev_set_wmm_params *)skb->data;
2153         ath10k_wmi_pdev_set_wmm_param(&cmd->ac_be, &arg->ac_be);
2154         ath10k_wmi_pdev_set_wmm_param(&cmd->ac_bk, &arg->ac_bk);
2155         ath10k_wmi_pdev_set_wmm_param(&cmd->ac_vi, &arg->ac_vi);
2156         ath10k_wmi_pdev_set_wmm_param(&cmd->ac_vo, &arg->ac_vo);
2157
2158         ath10k_dbg(ATH10K_DBG_WMI, "wmi pdev set wmm params\n");
2159         return ath10k_wmi_cmd_send(ar, skb, WMI_PDEV_SET_WMM_PARAMS_CMDID);
2160 }
2161
2162 int ath10k_wmi_request_stats(struct ath10k *ar, enum wmi_stats_id stats_id)
2163 {
2164         struct wmi_request_stats_cmd *cmd;
2165         struct sk_buff *skb;
2166
2167         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
2168         if (!skb)
2169                 return -ENOMEM;
2170
2171         cmd = (struct wmi_request_stats_cmd *)skb->data;
2172         cmd->stats_id = __cpu_to_le32(stats_id);
2173
2174         ath10k_dbg(ATH10K_DBG_WMI, "wmi request stats %d\n", (int)stats_id);
2175         return ath10k_wmi_cmd_send(ar, skb, WMI_REQUEST_STATS_CMDID);
2176 }
2177
2178 int ath10k_wmi_force_fw_hang(struct ath10k *ar,
2179                              enum wmi_force_fw_hang_type type, u32 delay_ms)
2180 {
2181         struct wmi_force_fw_hang_cmd *cmd;
2182         struct sk_buff *skb;
2183
2184         skb = ath10k_wmi_alloc_skb(sizeof(*cmd));
2185         if (!skb)
2186                 return -ENOMEM;
2187
2188         cmd = (struct wmi_force_fw_hang_cmd *)skb->data;
2189         cmd->type = __cpu_to_le32(type);
2190         cmd->delay_ms = __cpu_to_le32(delay_ms);
2191
2192         ath10k_dbg(ATH10K_DBG_WMI, "wmi force fw hang %d delay %d\n",
2193                    type, delay_ms);
2194         return ath10k_wmi_cmd_send(ar, skb, WMI_FORCE_FW_HANG_CMDID);
2195 }