]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/ath/wil6210/wmi.c
networking: make skb_put & friends return void pointers
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / ath / wil6210 / wmi.c
CommitLineData
2be7d22f 1/*
849a564b 2 * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
2be7d22f
VK
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
1eb9d1e5 17#include <linux/moduleparam.h>
2be7d22f 18#include <linux/etherdevice.h>
47e19af9 19#include <linux/if_arp.h>
2be7d22f
VK
20
21#include "wil6210.h"
47e19af9 22#include "txrx.h"
2be7d22f 23#include "wmi.h"
98658095 24#include "trace.h"
2be7d22f 25
327755fb 26static uint max_assoc_sta = WIL6210_MAX_CID;
78484c44 27module_param(max_assoc_sta, uint, 0644);
1eb9d1e5
DL
28MODULE_PARM_DESC(max_assoc_sta, " Max number of stations associated to the AP");
29
3a3def8d 30int agg_wsize; /* = 0; */
78484c44 31module_param(agg_wsize, int, 0644);
3a3def8d
VK
32MODULE_PARM_DESC(agg_wsize, " Window size for Tx Block Ack after connect;"
33 " 0 - use default; < 0 - don't auto-establish");
34
10d599ad 35u8 led_id = WIL_LED_INVALID_ID;
78484c44 36module_param(led_id, byte, 0444);
10d599ad
ME
37MODULE_PARM_DESC(led_id,
38 " 60G device led enablement. Set the led ID (0-2) to enable");
39
2be7d22f
VK
40/**
41 * WMI event receiving - theory of operations
42 *
43 * When firmware about to report WMI event, it fills memory area
44 * in the mailbox and raises misc. IRQ. Thread interrupt handler invoked for
45 * the misc IRQ, function @wmi_recv_cmd called by thread IRQ handler.
46 *
47 * @wmi_recv_cmd reads event, allocates memory chunk and attaches it to the
48 * event list @wil->pending_wmi_ev. Then, work queue @wil->wmi_wq wakes up
49 * and handles events within the @wmi_event_worker. Every event get detached
50 * from list, processed and deleted.
51 *
52 * Purpose for this mechanism is to release IRQ thread; otherwise,
53 * if WMI event handling involves another WMI command flow, this 2-nd flow
54 * won't be completed because of blocked IRQ thread.
55 */
56
57/**
58 * Addressing - theory of operations
59 *
60 * There are several buses present on the WIL6210 card.
61 * Same memory areas are visible at different address on
62 * the different busses. There are 3 main bus masters:
63 * - MAC CPU (ucode)
64 * - User CPU (firmware)
65 * - AHB (host)
66 *
67 * On the PCI bus, there is one BAR (BAR0) of 2Mb size, exposing
68 * AHB addresses starting from 0x880000
69 *
70 * Internally, firmware uses addresses that allows faster access but
71 * are invisible from the host. To read from these addresses, alternative
72 * AHB address must be used.
73 *
74 * Memory mapping
75 * Linker address PCI/Host address
76 * 0x880000 .. 0xa80000 2Mb BAR0
77 * 0x800000 .. 0x807000 0x900000 .. 0x907000 28k DCCM
78 * 0x840000 .. 0x857000 0x908000 .. 0x91f000 92k PERIPH
79 */
80
81/**
82 * @fw_mapping provides memory remapping table
b541d0a0
VK
83 *
84 * array size should be in sync with the declaration in the wil6210.h
2be7d22f 85 */
b541d0a0 86const struct fw_map fw_mapping[] = {
61578820
LD
87 /* FW code RAM 256k */
88 {0x000000, 0x040000, 0x8c0000, "fw_code", true},
89 /* FW data RAM 32k */
90 {0x800000, 0x808000, 0x900000, "fw_data", true},
91 /* periph data 128k */
92 {0x840000, 0x860000, 0x908000, "fw_peri", true},
93 /* various RGF 40k */
94 {0x880000, 0x88a000, 0x880000, "rgf", true},
95 /* AGC table 4k */
96 {0x88a000, 0x88b000, 0x88a000, "AGC_tbl", true},
97 /* Pcie_ext_rgf 4k */
98 {0x88b000, 0x88c000, 0x88b000, "rgf_ext", true},
99 /* mac_ext_rgf 512b */
100 {0x88c000, 0x88c200, 0x88c000, "mac_rgf_ext", true},
101 /* upper area 548k */
102 {0x8c0000, 0x949000, 0x8c0000, "upper", true},
103 /* UCODE areas - accessible by debugfs blobs but not by
104 * wmi_addr_remap. UCODE areas MUST be added AFTER FW areas!
2be7d22f 105 */
61578820
LD
106 /* ucode code RAM 128k */
107 {0x000000, 0x020000, 0x920000, "uc_code", false},
108 /* ucode data RAM 16k */
109 {0x800000, 0x804000, 0x940000, "uc_data", false},
2be7d22f
VK
110};
111
10d599ad
ME
112struct blink_on_off_time led_blink_time[] = {
113 {WIL_LED_BLINK_ON_SLOW_MS, WIL_LED_BLINK_OFF_SLOW_MS},
114 {WIL_LED_BLINK_ON_MED_MS, WIL_LED_BLINK_OFF_MED_MS},
115 {WIL_LED_BLINK_ON_FAST_MS, WIL_LED_BLINK_OFF_FAST_MS},
116};
117
118u8 led_polarity = LED_POLARITY_LOW_ACTIVE;
119
2be7d22f 120/**
61578820 121 * return AHB address for given firmware internal (linker) address
2be7d22f
VK
122 * @x - internal address
123 * If address have no valid AHB mapping, return 0
124 */
125static u32 wmi_addr_remap(u32 x)
126{
127 uint i;
128
129 for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
61578820
LD
130 if (fw_mapping[i].fw &&
131 ((x >= fw_mapping[i].from) && (x < fw_mapping[i].to)))
2be7d22f
VK
132 return x + fw_mapping[i].host - fw_mapping[i].from;
133 }
134
135 return 0;
136}
137
138/**
139 * Check address validity for WMI buffer; remap if needed
140 * @ptr - internal (linker) fw/ucode address
141 *
142 * Valid buffer should be DWORD aligned
143 *
144 * return address for accessing buffer from the host;
145 * if buffer is not valid, return NULL.
146 */
147void __iomem *wmi_buffer(struct wil6210_priv *wil, __le32 ptr_)
148{
149 u32 off;
150 u32 ptr = le32_to_cpu(ptr_);
151
152 if (ptr % 4)
153 return NULL;
154
155 ptr = wmi_addr_remap(ptr);
156 if (ptr < WIL6210_FW_HOST_OFF)
157 return NULL;
158
159 off = HOSTADDR(ptr);
160 if (off > WIL6210_MEM_SIZE - 4)
161 return NULL;
162
163 return wil->csr + off;
164}
165
166/**
167 * Check address validity
168 */
169void __iomem *wmi_addr(struct wil6210_priv *wil, u32 ptr)
170{
171 u32 off;
172
173 if (ptr % 4)
174 return NULL;
175
176 if (ptr < WIL6210_FW_HOST_OFF)
177 return NULL;
178
179 off = HOSTADDR(ptr);
180 if (off > WIL6210_MEM_SIZE - 4)
181 return NULL;
182
183 return wil->csr + off;
184}
185
186int wmi_read_hdr(struct wil6210_priv *wil, __le32 ptr,
187 struct wil6210_mbox_hdr *hdr)
188{
189 void __iomem *src = wmi_buffer(wil, ptr);
8fe59627 190
2be7d22f
VK
191 if (!src)
192 return -EINVAL;
193
194 wil_memcpy_fromio_32(hdr, src, sizeof(*hdr));
195
196 return 0;
197}
198
199static int __wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
200{
201 struct {
202 struct wil6210_mbox_hdr hdr;
b874ddec 203 struct wmi_cmd_hdr wmi;
2be7d22f
VK
204 } __packed cmd = {
205 .hdr = {
206 .type = WIL_MBOX_HDR_TYPE_WMI,
207 .flags = 0,
208 .len = cpu_to_le16(sizeof(cmd.wmi) + len),
209 },
210 .wmi = {
f988b23f 211 .mid = 0,
b874ddec 212 .command_id = cpu_to_le16(cmdid),
2be7d22f
VK
213 },
214 };
215 struct wil6210_mbox_ring *r = &wil->mbox_ctl.tx;
216 struct wil6210_mbox_ring_desc d_head;
217 u32 next_head;
218 void __iomem *dst;
219 void __iomem *head = wmi_addr(wil, r->head);
220 uint retry;
349214c1 221 int rc = 0;
2be7d22f
VK
222
223 if (sizeof(cmd) + len > r->entry_size) {
224 wil_err(wil, "WMI size too large: %d bytes, max is %d\n",
225 (int)(sizeof(cmd) + len), r->entry_size);
226 return -ERANGE;
2be7d22f
VK
227 }
228
229 might_sleep();
230
9419b6a2 231 if (!test_bit(wil_status_fwready, wil->status)) {
15e23124 232 wil_err(wil, "WMI: cannot send command while FW not ready\n");
2be7d22f
VK
233 return -EAGAIN;
234 }
235
236 if (!head) {
237 wil_err(wil, "WMI head is garbage: 0x%08x\n", r->head);
238 return -EINVAL;
239 }
349214c1
ME
240
241 wil_halp_vote(wil);
242
2be7d22f
VK
243 /* read Tx head till it is not busy */
244 for (retry = 5; retry > 0; retry--) {
245 wil_memcpy_fromio_32(&d_head, head, sizeof(d_head));
246 if (d_head.sync == 0)
247 break;
248 msleep(20);
249 }
250 if (d_head.sync != 0) {
251 wil_err(wil, "WMI head busy\n");
349214c1
ME
252 rc = -EBUSY;
253 goto out;
2be7d22f
VK
254 }
255 /* next head */
256 next_head = r->base + ((r->head - r->base + sizeof(d_head)) % r->size);
7743882d 257 wil_dbg_wmi(wil, "Head 0x%08x -> 0x%08x\n", r->head, next_head);
2be7d22f
VK
258 /* wait till FW finish with previous command */
259 for (retry = 5; retry > 0; retry--) {
452133a7
ME
260 if (!test_bit(wil_status_fwready, wil->status)) {
261 wil_err(wil, "WMI: cannot send command while FW not ready\n");
349214c1
ME
262 rc = -EAGAIN;
263 goto out;
452133a7 264 }
b9eeb512
VK
265 r->tail = wil_r(wil, RGF_MBOX +
266 offsetof(struct wil6210_mbox_ctl, tx.tail));
2be7d22f
VK
267 if (next_head != r->tail)
268 break;
269 msleep(20);
270 }
271 if (next_head == r->tail) {
272 wil_err(wil, "WMI ring full\n");
349214c1
ME
273 rc = -EBUSY;
274 goto out;
2be7d22f
VK
275 }
276 dst = wmi_buffer(wil, d_head.addr);
277 if (!dst) {
278 wil_err(wil, "invalid WMI buffer: 0x%08x\n",
279 le32_to_cpu(d_head.addr));
349214c1
ME
280 rc = -EAGAIN;
281 goto out;
2be7d22f
VK
282 }
283 cmd.hdr.seq = cpu_to_le16(++wil->wmi_seq);
284 /* set command */
7743882d
VK
285 wil_dbg_wmi(wil, "WMI command 0x%04x [%d]\n", cmdid, len);
286 wil_hex_dump_wmi("Cmd ", DUMP_PREFIX_OFFSET, 16, 1, &cmd,
2be7d22f 287 sizeof(cmd), true);
7743882d 288 wil_hex_dump_wmi("cmd ", DUMP_PREFIX_OFFSET, 16, 1, buf,
2be7d22f
VK
289 len, true);
290 wil_memcpy_toio_32(dst, &cmd, sizeof(cmd));
291 wil_memcpy_toio_32(dst + sizeof(cmd), buf, len);
292 /* mark entry as full */
b9eeb512 293 wil_w(wil, r->head + offsetof(struct wil6210_mbox_ring_desc, sync), 1);
2be7d22f 294 /* advance next ptr */
b9eeb512
VK
295 wil_w(wil, RGF_MBOX + offsetof(struct wil6210_mbox_ctl, tx.head),
296 r->head = next_head);
2be7d22f 297
f988b23f 298 trace_wil6210_wmi_cmd(&cmd.wmi, buf, len);
98658095 299
2be7d22f 300 /* interrupt to FW */
b9eeb512
VK
301 wil_w(wil, RGF_USER_USER_ICR + offsetof(struct RGF_ICR, ICS),
302 SW_INT_MBOX);
2be7d22f 303
349214c1
ME
304out:
305 wil_halp_unvote(wil);
306 return rc;
2be7d22f
VK
307}
308
309int wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
310{
311 int rc;
312
313 mutex_lock(&wil->wmi_mutex);
314 rc = __wmi_send(wil, cmdid, buf, len);
315 mutex_unlock(&wil->wmi_mutex);
316
317 return rc;
318}
319
320/*=== Event handlers ===*/
321static void wmi_evt_ready(struct wil6210_priv *wil, int id, void *d, int len)
322{
2be7d22f
VK
323 struct wireless_dev *wdev = wil->wdev;
324 struct wmi_ready_event *evt = d;
8fe59627 325
b8023177 326 wil->n_mids = evt->numof_additional_mids;
2be7d22f 327
13cd9f75
LD
328 wil_info(wil, "FW ver. %s(SW %d); MAC %pM; %d MID's\n",
329 wil->fw_version, le32_to_cpu(evt->sw_version),
15e23124 330 evt->mac, wil->n_mids);
2cd0f021 331 /* ignore MAC address, we already have it from the boot loader */
13cd9f75
LD
332 strlcpy(wdev->wiphy->fw_version, wil->fw_version,
333 sizeof(wdev->wiphy->fw_version));
2be7d22f 334
c33407a8 335 wil_set_recovery_state(wil, fw_recovery_idle);
9419b6a2 336 set_bit(wil_status_fwready, wil->status);
59502647 337 /* let the reset sequence continue */
2be7d22f
VK
338 complete(&wil->wmi_ready);
339}
340
341static void wmi_evt_rx_mgmt(struct wil6210_priv *wil, int id, void *d, int len)
342{
343 struct wmi_rx_mgmt_packet_event *data = d;
344 struct wiphy *wiphy = wil_to_wiphy(wil);
345 struct ieee80211_mgmt *rx_mgmt_frame =
346 (struct ieee80211_mgmt *)data->payload;
90d89e9a
VK
347 int flen = len - offsetof(struct wmi_rx_mgmt_packet_event, payload);
348 int ch_no;
349 u32 freq;
350 struct ieee80211_channel *channel;
351 s32 signal;
352 __le16 fc;
353 u32 d_len;
354 u16 d_status;
355
356 if (flen < 0) {
357 wil_err(wil, "MGMT Rx: short event, len %d\n", len);
358 return;
359 }
360
361 d_len = le32_to_cpu(data->info.len);
362 if (d_len != flen) {
363 wil_err(wil,
364 "MGMT Rx: length mismatch, d_len %d should be %d\n",
365 d_len, flen);
366 return;
367 }
368
369 ch_no = data->info.channel + 1;
57fbcce3 370 freq = ieee80211_channel_to_frequency(ch_no, NL80211_BAND_60GHZ);
90d89e9a
VK
371 channel = ieee80211_get_channel(wiphy, freq);
372 signal = data->info.sqi;
373 d_status = le16_to_cpu(data->info.status);
374 fc = rx_mgmt_frame->frame_control;
375
376 wil_dbg_wmi(wil, "MGMT Rx: channel %d MCS %d SNR %d SQI %d%%\n",
b8b33a3a
VK
377 data->info.channel, data->info.mcs, data->info.snr,
378 data->info.sqi);
f27dbf78
VK
379 wil_dbg_wmi(wil, "status 0x%04x len %d fc 0x%04x\n", d_status, d_len,
380 le16_to_cpu(fc));
7743882d 381 wil_dbg_wmi(wil, "qid %d mid %d cid %d\n",
2be7d22f 382 data->info.qid, data->info.mid, data->info.cid);
90d89e9a
VK
383 wil_hex_dump_wmi("MGMT Rx ", DUMP_PREFIX_OFFSET, 16, 1, rx_mgmt_frame,
384 d_len, true);
2be7d22f
VK
385
386 if (!channel) {
387 wil_err(wil, "Frame on unsupported channel\n");
388 return;
389 }
390
391 if (ieee80211_is_beacon(fc) || ieee80211_is_probe_resp(fc)) {
392 struct cfg80211_bss *bss;
8eea944a
VK
393 u64 tsf = le64_to_cpu(rx_mgmt_frame->u.beacon.timestamp);
394 u16 cap = le16_to_cpu(rx_mgmt_frame->u.beacon.capab_info);
395 u16 bi = le16_to_cpu(rx_mgmt_frame->u.beacon.beacon_int);
396 const u8 *ie_buf = rx_mgmt_frame->u.beacon.variable;
397 size_t ie_len = d_len - offsetof(struct ieee80211_mgmt,
398 u.beacon.variable);
399 wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
400 wil_dbg_wmi(wil, "TSF : 0x%016llx\n", tsf);
401 wil_dbg_wmi(wil, "Beacon interval : %d\n", bi);
402 wil_hex_dump_wmi("IE ", DUMP_PREFIX_OFFSET, 16, 1, ie_buf,
403 ie_len, true);
a0f7845b 404
e6d68341
DL
405 wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
406
a0f7845b
VK
407 bss = cfg80211_inform_bss_frame(wiphy, channel, rx_mgmt_frame,
408 d_len, signal, GFP_KERNEL);
2be7d22f 409 if (bss) {
7743882d 410 wil_dbg_wmi(wil, "Added BSS %pM\n",
2be7d22f 411 rx_mgmt_frame->bssid);
5b112d3d 412 cfg80211_put_bss(wiphy, bss);
2be7d22f 413 } else {
5bc8c1f2 414 wil_err(wil, "cfg80211_inform_bss_frame() failed\n");
2be7d22f 415 }
102b1d99 416 } else {
4332cac1
LD
417 mutex_lock(&wil->p2p_wdev_mutex);
418 cfg80211_rx_mgmt(wil->radio_wdev, freq, signal,
970fdfa8 419 (void *)rx_mgmt_frame, d_len, 0);
4332cac1 420 mutex_unlock(&wil->p2p_wdev_mutex);
2be7d22f
VK
421 }
422}
423
90d89e9a
VK
424static void wmi_evt_tx_mgmt(struct wil6210_priv *wil, int id, void *d, int len)
425{
426 struct wmi_tx_mgmt_packet_event *data = d;
427 struct ieee80211_mgmt *mgmt_frame =
428 (struct ieee80211_mgmt *)data->payload;
429 int flen = len - offsetof(struct wmi_tx_mgmt_packet_event, payload);
430
431 wil_hex_dump_wmi("MGMT Tx ", DUMP_PREFIX_OFFSET, 16, 1, mgmt_frame,
432 flen, true);
433}
434
2be7d22f
VK
435static void wmi_evt_scan_complete(struct wil6210_priv *wil, int id,
436 void *d, int len)
437{
5ffae432 438 mutex_lock(&wil->p2p_wdev_mutex);
2be7d22f
VK
439 if (wil->scan_request) {
440 struct wmi_scan_complete_event *data = d;
035859a5 441 int status = le32_to_cpu(data->status);
1d76250b 442 struct cfg80211_scan_info info = {
035859a5
ME
443 .aborted = ((status != WMI_SCAN_SUCCESS) &&
444 (status != WMI_SCAN_ABORT_REJECTED)),
1d76250b 445 };
2be7d22f 446
035859a5 447 wil_dbg_wmi(wil, "SCAN_COMPLETE(0x%08x)\n", status);
2a91d7d0 448 wil_dbg_misc(wil, "Complete scan_request 0x%p aborted %d\n",
1d76250b 449 wil->scan_request, info.aborted);
047e5d74 450 del_timer_sync(&wil->scan_timer);
1d76250b 451 cfg80211_scan_done(wil->scan_request, &info);
4332cac1 452 wil->radio_wdev = wil->wdev;
2be7d22f 453 wil->scan_request = NULL;
035859a5 454 wake_up_interruptible(&wil->wq);
bb6743f7
LD
455 if (wil->p2p.pending_listen_wdev) {
456 wil_dbg_misc(wil, "Scheduling delayed listen\n");
457 schedule_work(&wil->p2p.delayed_listen_work);
458 }
2be7d22f
VK
459 } else {
460 wil_err(wil, "SCAN_COMPLETE while not scanning\n");
461 }
5ffae432 462 mutex_unlock(&wil->p2p_wdev_mutex);
2be7d22f
VK
463}
464
465static void wmi_evt_connect(struct wil6210_priv *wil, int id, void *d, int len)
466{
467 struct net_device *ndev = wil_to_ndev(wil);
468 struct wireless_dev *wdev = wil->wdev;
469 struct wmi_connect_event *evt = d;
470 int ch; /* channel number */
471 struct station_info sinfo;
472 u8 *assoc_req_ie, *assoc_resp_ie;
473 size_t assoc_req_ielen, assoc_resp_ielen;
474 /* capinfo(u16) + listen_interval(u16) + IEs */
475 const size_t assoc_req_ie_offset = sizeof(u16) * 2;
476 /* capinfo(u16) + status_code(u16) + associd(u16) + IEs */
477 const size_t assoc_resp_ie_offset = sizeof(u16) * 3;
0916d9f2 478 int rc;
2be7d22f
VK
479
480 if (len < sizeof(*evt)) {
481 wil_err(wil, "Connect event too short : %d bytes\n", len);
482 return;
483 }
484 if (len != sizeof(*evt) + evt->beacon_ie_len + evt->assoc_req_len +
485 evt->assoc_resp_len) {
486 wil_err(wil,
487 "Connect event corrupted : %d != %d + %d + %d + %d\n",
488 len, (int)sizeof(*evt), evt->beacon_ie_len,
489 evt->assoc_req_len, evt->assoc_resp_len);
490 return;
491 }
3df2cd36
VK
492 if (evt->cid >= WIL6210_MAX_CID) {
493 wil_err(wil, "Connect CID invalid : %d\n", evt->cid);
494 return;
495 }
496
2be7d22f 497 ch = evt->channel + 1;
9d865ee2
LD
498 wil_info(wil, "Connect %pM channel [%d] cid %d aid %d\n",
499 evt->bssid, ch, evt->cid, evt->aid);
7743882d 500 wil_hex_dump_wmi("connect AI : ", DUMP_PREFIX_OFFSET, 16, 1,
2be7d22f
VK
501 evt->assoc_info, len - sizeof(*evt), true);
502
503 /* figure out IE's */
504 assoc_req_ie = &evt->assoc_info[evt->beacon_ie_len +
505 assoc_req_ie_offset];
506 assoc_req_ielen = evt->assoc_req_len - assoc_req_ie_offset;
507 if (evt->assoc_req_len <= assoc_req_ie_offset) {
508 assoc_req_ie = NULL;
509 assoc_req_ielen = 0;
510 }
511
512 assoc_resp_ie = &evt->assoc_info[evt->beacon_ie_len +
513 evt->assoc_req_len +
514 assoc_resp_ie_offset];
515 assoc_resp_ielen = evt->assoc_resp_len - assoc_resp_ie_offset;
516 if (evt->assoc_resp_len <= assoc_resp_ie_offset) {
517 assoc_resp_ie = NULL;
518 assoc_resp_ielen = 0;
519 }
520
0916d9f2
ME
521 if (test_bit(wil_status_resetting, wil->status) ||
522 !test_bit(wil_status_fwready, wil->status)) {
523 wil_err(wil, "status_resetting, cancel connect event, CID %d\n",
524 evt->cid);
0916d9f2
ME
525 /* no need for cleanup, wil_reset will do that */
526 return;
527 }
528
b819447d
HK
529 mutex_lock(&wil->mutex);
530
2be7d22f
VK
531 if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
532 (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
9419b6a2 533 if (!test_bit(wil_status_fwconnecting, wil->status)) {
2be7d22f 534 wil_err(wil, "Not in connecting state\n");
0916d9f2 535 mutex_unlock(&wil->mutex);
2be7d22f
VK
536 return;
537 }
538 del_timer_sync(&wil->connect_timer);
3d287fb3
ME
539 } else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
540 (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
541 if (wil->sta[evt->cid].status != wil_sta_unused) {
af3db60a
LA
542 wil_err(wil, "AP: Invalid status %d for CID %d\n",
543 wil->sta[evt->cid].status, evt->cid);
3d287fb3
ME
544 mutex_unlock(&wil->mutex);
545 return;
546 }
0916d9f2
ME
547 }
548
549 /* FIXME FW can transmit only ucast frames to peer */
550 /* FIXME real ring_id instead of hard coded 0 */
551 ether_addr_copy(wil->sta[evt->cid].addr, evt->bssid);
552 wil->sta[evt->cid].status = wil_sta_conn_pending;
2be7d22f 553
0916d9f2
ME
554 rc = wil_tx_init(wil, evt->cid);
555 if (rc) {
af3db60a
LA
556 wil_err(wil, "config tx vring failed for CID %d, rc (%d)\n",
557 evt->cid, rc);
0916d9f2 558 wmi_disconnect_sta(wil, wil->sta[evt->cid].addr,
849a564b 559 WLAN_REASON_UNSPECIFIED, false, false);
0916d9f2 560 } else {
af3db60a 561 wil_info(wil, "successful connection to CID %d\n", evt->cid);
0916d9f2
ME
562 }
563
564 if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
565 (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
566 if (rc) {
0916d9f2 567 netif_carrier_off(ndev);
9953a782 568 wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
af3db60a 569 wil_err(wil, "cfg80211_connect_result with failure\n");
0916d9f2
ME
570 cfg80211_connect_result(ndev, evt->bssid, NULL, 0,
571 NULL, 0,
572 WLAN_STATUS_UNSPECIFIED_FAILURE,
573 GFP_KERNEL);
574 goto out;
575 } else {
bcdd49b0
DL
576 struct wiphy *wiphy = wil_to_wiphy(wil);
577
578 cfg80211_ref_bss(wiphy, wil->bss);
579 cfg80211_connect_bss(ndev, evt->bssid, wil->bss,
580 assoc_req_ie, assoc_req_ielen,
581 assoc_resp_ie, assoc_resp_ielen,
582 WLAN_STATUS_SUCCESS, GFP_KERNEL,
583 NL80211_TIMEOUT_UNSPECIFIED);
0916d9f2 584 }
bcdd49b0 585 wil->bss = NULL;
2be7d22f
VK
586 } else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
587 (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
849a564b
DL
588 if (rc) {
589 if (disable_ap_sme)
590 /* notify new_sta has failed */
591 cfg80211_del_sta(ndev, evt->bssid, GFP_KERNEL);
0916d9f2 592 goto out;
849a564b 593 }
0916d9f2 594
2be7d22f
VK
595 memset(&sinfo, 0, sizeof(sinfo));
596
597 sinfo.generation = wil->sinfo_gen++;
598
599 if (assoc_req_ie) {
600 sinfo.assoc_req_ies = assoc_req_ie;
601 sinfo.assoc_req_ies_len = assoc_req_ielen;
2be7d22f
VK
602 }
603
604 cfg80211_new_sta(ndev, evt->bssid, &sinfo, GFP_KERNEL);
0916d9f2 605 } else {
af3db60a
LA
606 wil_err(wil, "unhandled iftype %d for CID %d\n", wdev->iftype,
607 evt->cid);
0916d9f2 608 goto out;
2be7d22f 609 }
2be7d22f 610
0916d9f2 611 wil->sta[evt->cid].status = wil_sta_connected;
9d865ee2 612 wil->sta[evt->cid].aid = evt->aid;
0916d9f2 613 set_bit(wil_status_fwconnected, wil->status);
f9e3033f 614 wil_update_net_queues_bh(wil, NULL, false);
2be7d22f 615
0916d9f2
ME
616out:
617 if (rc)
618 wil->sta[evt->cid].status = wil_sta_unused;
619 clear_bit(wil_status_fwconnecting, wil->status);
620 mutex_unlock(&wil->mutex);
2be7d22f
VK
621}
622
623static void wmi_evt_disconnect(struct wil6210_priv *wil, int id,
624 void *d, int len)
625{
626 struct wmi_disconnect_event *evt = d;
4821e6d8 627 u16 reason_code = le16_to_cpu(evt->protocol_reason_status);
2be7d22f 628
0916d9f2
ME
629 wil_info(wil, "Disconnect %pM reason [proto %d wmi %d]\n",
630 evt->bssid, reason_code, evt->disconnect_reason);
2be7d22f
VK
631
632 wil->sinfo_gen++;
633
b819447d
HK
634 if (test_bit(wil_status_resetting, wil->status) ||
635 !test_bit(wil_status_fwready, wil->status)) {
636 wil_err(wil, "status_resetting, cancel disconnect event\n");
637 /* no need for cleanup, wil_reset will do that */
638 return;
639 }
640
097638a0 641 mutex_lock(&wil->mutex);
4821e6d8 642 wil6210_disconnect(wil, evt->bssid, reason_code, true);
097638a0 643 mutex_unlock(&wil->mutex);
2be7d22f
VK
644}
645
2be7d22f
VK
646/*
647 * Firmware reports EAPOL frame using WME event.
648 * Reconstruct Ethernet frame and deliver it via normal Rx
649 */
650static void wmi_evt_eapol_rx(struct wil6210_priv *wil, int id,
651 void *d, int len)
652{
653 struct net_device *ndev = wil_to_ndev(wil);
654 struct wmi_eapol_rx_event *evt = d;
655 u16 eapol_len = le16_to_cpu(evt->eapol_len);
656 int sz = eapol_len + ETH_HLEN;
657 struct sk_buff *skb;
658 struct ethhdr *eth;
c8b78b5f
VK
659 int cid;
660 struct wil_net_stats *stats = NULL;
2be7d22f 661
7743882d 662 wil_dbg_wmi(wil, "EAPOL len %d from %pM\n", eapol_len,
2be7d22f
VK
663 evt->src_mac);
664
c8b78b5f
VK
665 cid = wil_find_cid(wil, evt->src_mac);
666 if (cid >= 0)
667 stats = &wil->sta[cid].stats;
668
2be7d22f
VK
669 if (eapol_len > 196) { /* TODO: revisit size limit */
670 wil_err(wil, "EAPOL too large\n");
671 return;
672 }
673
674 skb = alloc_skb(sz, GFP_KERNEL);
675 if (!skb) {
676 wil_err(wil, "Failed to allocate skb\n");
677 return;
678 }
c8b78b5f 679
4df864c1 680 eth = skb_put(skb, ETH_HLEN);
a82553bb
VK
681 ether_addr_copy(eth->h_dest, ndev->dev_addr);
682 ether_addr_copy(eth->h_source, evt->src_mac);
2be7d22f 683 eth->h_proto = cpu_to_be16(ETH_P_PAE);
59ae1d12 684 skb_put_data(skb, evt->eapol, eapol_len);
2be7d22f
VK
685 skb->protocol = eth_type_trans(skb, ndev);
686 if (likely(netif_rx_ni(skb) == NET_RX_SUCCESS)) {
687 ndev->stats.rx_packets++;
c8b78b5f
VK
688 ndev->stats.rx_bytes += sz;
689 if (stats) {
690 stats->rx_packets++;
691 stats->rx_bytes += sz;
692 }
2be7d22f
VK
693 } else {
694 ndev->stats.rx_dropped++;
c8b78b5f
VK
695 if (stats)
696 stats->rx_dropped++;
2be7d22f
VK
697 }
698}
699
230d8442 700static void wmi_evt_vring_en(struct wil6210_priv *wil, int id, void *d, int len)
3a124ed6 701{
230d8442
VK
702 struct wmi_vring_en_event *evt = d;
703 u8 vri = evt->vring_index;
849a564b 704 struct wireless_dev *wdev = wil_to_wdev(wil);
3a124ed6 705
230d8442 706 wil_dbg_wmi(wil, "Enable vring %d\n", vri);
3a124ed6 707
230d8442
VK
708 if (vri >= ARRAY_SIZE(wil->vring_tx)) {
709 wil_err(wil, "Enable for invalid vring %d\n", vri);
e58c9f70
VK
710 return;
711 }
849a564b
DL
712
713 if (wdev->iftype != NL80211_IFTYPE_AP || !disable_ap_sme)
714 /* in AP mode with disable_ap_sme, this is done by
715 * wil_cfg80211_change_station()
716 */
717 wil->vring_tx_data[vri].dot1x_open = true;
230d8442 718 if (vri == wil->bcast_vring) /* no BA for bcast */
e58c9f70 719 return;
230d8442
VK
720 if (agg_wsize >= 0)
721 wil_addba_tx_request(wil, vri, agg_wsize);
3442a504
VK
722}
723
249a382b
VK
724static void wmi_evt_ba_status(struct wil6210_priv *wil, int id, void *d,
725 int len)
726{
b874ddec 727 struct wmi_ba_status_event *evt = d;
3277213f 728 struct vring_tx_data *txdata;
249a382b 729
cbcf5866 730 wil_dbg_wmi(wil, "BACK[%d] %s {%d} timeout %d AMSDU%s\n",
7b05b0ab
VK
731 evt->ringid,
732 evt->status == WMI_BA_AGREED ? "OK" : "N/A",
cbcf5866
VK
733 evt->agg_wsize, __le16_to_cpu(evt->ba_timeout),
734 evt->amsdu ? "+" : "-");
7b05b0ab
VK
735
736 if (evt->ringid >= WIL6210_MAX_TX_RINGS) {
737 wil_err(wil, "invalid ring id %d\n", evt->ringid);
738 return;
b4490f42
VK
739 }
740
3277213f
VK
741 if (evt->status != WMI_BA_AGREED) {
742 evt->ba_timeout = 0;
743 evt->agg_wsize = 0;
cbcf5866 744 evt->amsdu = 0;
7b05b0ab
VK
745 }
746
3277213f 747 txdata = &wil->vring_tx_data[evt->ringid];
ec81b5ad 748
3277213f
VK
749 txdata->agg_timeout = le16_to_cpu(evt->ba_timeout);
750 txdata->agg_wsize = evt->agg_wsize;
cbcf5866 751 txdata->agg_amsdu = evt->amsdu;
3a124ed6 752 txdata->addba_in_progress = false;
3277213f 753}
ec81b5ad 754
3277213f
VK
755static void wmi_evt_addba_rx_req(struct wil6210_priv *wil, int id, void *d,
756 int len)
757{
758 struct wmi_rcp_addba_req_event *evt = d;
ec81b5ad 759
3277213f
VK
760 wil_addba_rx_request(wil, evt->cidxtid, evt->dialog_token,
761 evt->ba_param_set, evt->ba_timeout,
762 evt->ba_seq_ctrl);
763}
ec81b5ad 764
3277213f 765static void wmi_evt_delba(struct wil6210_priv *wil, int id, void *d, int len)
bd33273b 766__acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
3277213f
VK
767{
768 struct wmi_delba_event *evt = d;
769 u8 cid, tid;
770 u16 reason = __le16_to_cpu(evt->reason);
771 struct wil_sta_info *sta;
772 struct wil_tid_ampdu_rx *r;
3277213f 773
bd33273b 774 might_sleep();
3277213f
VK
775 parse_cidxtid(evt->cidxtid, &cid, &tid);
776 wil_dbg_wmi(wil, "DELBA CID %d TID %d from %s reason %d\n",
777 cid, tid,
778 evt->from_initiator ? "originator" : "recipient",
779 reason);
780 if (!evt->from_initiator) {
781 int i;
782 /* find Tx vring it belongs to */
783 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
784 if ((wil->vring2cid_tid[i][0] == cid) &&
785 (wil->vring2cid_tid[i][1] == tid)) {
786 struct vring_tx_data *txdata =
787 &wil->vring_tx_data[i];
788
789 wil_dbg_wmi(wil, "DELBA Tx vring %d\n", i);
790 txdata->agg_timeout = 0;
791 txdata->agg_wsize = 0;
3a124ed6 792 txdata->addba_in_progress = false;
3277213f
VK
793
794 break; /* max. 1 matching ring */
795 }
796 }
797 if (i >= ARRAY_SIZE(wil->vring2cid_tid))
798 wil_err(wil, "DELBA: unable to find Tx vring\n");
799 return;
7b05b0ab 800 }
8c86f757 801
3277213f
VK
802 sta = &wil->sta[cid];
803
bd33273b 804 spin_lock_bh(&sta->tid_rx_lock);
3277213f
VK
805
806 r = sta->tid_rx[tid];
807 sta->tid_rx[tid] = NULL;
808 wil_tid_ampdu_rx_free(wil, r);
809
bd33273b 810 spin_unlock_bh(&sta->tid_rx_lock);
249a382b
VK
811}
812
b03fbab0
VK
813/**
814 * Some events are ignored for purpose; and need not be interpreted as
815 * "unhandled events"
816 */
817static void wmi_evt_ignore(struct wil6210_priv *wil, int id, void *d, int len)
818{
819 wil_dbg_wmi(wil, "Ignore event 0x%04x len %d\n", id, len);
820}
821
2be7d22f
VK
822static const struct {
823 int eventid;
824 void (*handler)(struct wil6210_priv *wil, int eventid,
825 void *data, int data_len);
826} wmi_evt_handlers[] = {
827 {WMI_READY_EVENTID, wmi_evt_ready},
817f1853 828 {WMI_FW_READY_EVENTID, wmi_evt_ignore},
2be7d22f 829 {WMI_RX_MGMT_PACKET_EVENTID, wmi_evt_rx_mgmt},
90d89e9a 830 {WMI_TX_MGMT_PACKET_EVENTID, wmi_evt_tx_mgmt},
2be7d22f
VK
831 {WMI_SCAN_COMPLETE_EVENTID, wmi_evt_scan_complete},
832 {WMI_CONNECT_EVENTID, wmi_evt_connect},
833 {WMI_DISCONNECT_EVENTID, wmi_evt_disconnect},
2be7d22f 834 {WMI_EAPOL_RX_EVENTID, wmi_evt_eapol_rx},
249a382b 835 {WMI_BA_STATUS_EVENTID, wmi_evt_ba_status},
3277213f
VK
836 {WMI_RCP_ADDBA_REQ_EVENTID, wmi_evt_addba_rx_req},
837 {WMI_DELBA_EVENTID, wmi_evt_delba},
230d8442 838 {WMI_VRING_EN_EVENTID, wmi_evt_vring_en},
b03fbab0 839 {WMI_DATA_PORT_OPEN_EVENTID, wmi_evt_ignore},
2be7d22f
VK
840};
841
842/*
843 * Run in IRQ context
844 * Extract WMI command from mailbox. Queue it to the @wil->pending_wmi_ev
845 * that will be eventually handled by the @wmi_event_worker in the thread
846 * context of thread "wil6210_wmi"
847 */
848void wmi_recv_cmd(struct wil6210_priv *wil)
849{
850 struct wil6210_mbox_ring_desc d_tail;
851 struct wil6210_mbox_hdr hdr;
852 struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
853 struct pending_wmi_event *evt;
854 u8 *cmd;
855 void __iomem *src;
856 ulong flags;
a715c7dd 857 unsigned n;
0916d9f2 858 unsigned int num_immed_reply = 0;
2be7d22f 859
817f1853 860 if (!test_bit(wil_status_mbox_ready, wil->status)) {
4cf99c93 861 wil_err(wil, "Reset in progress. Cannot handle WMI event\n");
55f7acdd
VK
862 return;
863 }
864
a715c7dd 865 for (n = 0;; n++) {
2be7d22f 866 u16 len;
95266dc0 867 bool q;
0916d9f2 868 bool immed_reply = false;
2be7d22f 869
b9eeb512
VK
870 r->head = wil_r(wil, RGF_MBOX +
871 offsetof(struct wil6210_mbox_ctl, rx.head));
95266dc0
VK
872 if (r->tail == r->head)
873 break;
2be7d22f 874
a715c7dd
VK
875 wil_dbg_wmi(wil, "Mbox head %08x tail %08x\n",
876 r->head, r->tail);
877 /* read cmd descriptor from tail */
2be7d22f
VK
878 wil_memcpy_fromio_32(&d_tail, wil->csr + HOSTADDR(r->tail),
879 sizeof(struct wil6210_mbox_ring_desc));
880 if (d_tail.sync == 0) {
881 wil_err(wil, "Mbox evt not owned by FW?\n");
95266dc0 882 break;
2be7d22f
VK
883 }
884
a715c7dd 885 /* read cmd header from descriptor */
2be7d22f
VK
886 if (0 != wmi_read_hdr(wil, d_tail.addr, &hdr)) {
887 wil_err(wil, "Mbox evt at 0x%08x?\n",
888 le32_to_cpu(d_tail.addr));
95266dc0 889 break;
2be7d22f 890 }
2be7d22f 891 len = le16_to_cpu(hdr.len);
a715c7dd
VK
892 wil_dbg_wmi(wil, "Mbox evt %04x %04x %04x %02x\n",
893 le16_to_cpu(hdr.seq), len, le16_to_cpu(hdr.type),
894 hdr.flags);
895
896 /* read cmd buffer from descriptor */
2be7d22f
VK
897 src = wmi_buffer(wil, d_tail.addr) +
898 sizeof(struct wil6210_mbox_hdr);
899 evt = kmalloc(ALIGN(offsetof(struct pending_wmi_event,
900 event.wmi) + len, 4),
901 GFP_KERNEL);
14f8dc49 902 if (!evt)
95266dc0 903 break;
14f8dc49 904
2be7d22f
VK
905 evt->event.hdr = hdr;
906 cmd = (void *)&evt->event.wmi;
907 wil_memcpy_fromio_32(cmd, src, len);
908 /* mark entry as empty */
b9eeb512
VK
909 wil_w(wil, r->tail +
910 offsetof(struct wil6210_mbox_ring_desc, sync), 0);
2be7d22f 911 /* indicate */
2be7d22f 912 if ((hdr.type == WIL_MBOX_HDR_TYPE_WMI) &&
b874ddec
LD
913 (len >= sizeof(struct wmi_cmd_hdr))) {
914 struct wmi_cmd_hdr *wmi = &evt->event.wmi;
915 u16 id = le16_to_cpu(wmi->command_id);
916 u32 tstamp = le32_to_cpu(wmi->fw_timestamp);
fe5c271e 917 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
0916d9f2
ME
918 if (wil->reply_id && wil->reply_id == id) {
919 if (wil->reply_buf) {
920 memcpy(wil->reply_buf, wmi,
921 min(len, wil->reply_size));
922 immed_reply = true;
923 }
924 }
fe5c271e 925 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
8fe59627 926
f988b23f
VK
927 wil_dbg_wmi(wil, "WMI event 0x%04x MID %d @%d msec\n",
928 id, wmi->mid, tstamp);
929 trace_wil6210_wmi_event(wmi, &wmi[1],
930 len - sizeof(*wmi));
2be7d22f 931 }
7743882d 932 wil_hex_dump_wmi("evt ", DUMP_PREFIX_OFFSET, 16, 1,
2be7d22f
VK
933 &evt->event.hdr, sizeof(hdr) + len, true);
934
935 /* advance tail */
936 r->tail = r->base + ((r->tail - r->base +
937 sizeof(struct wil6210_mbox_ring_desc)) % r->size);
b9eeb512
VK
938 wil_w(wil, RGF_MBOX +
939 offsetof(struct wil6210_mbox_ctl, rx.tail), r->tail);
2be7d22f 940
0916d9f2 941 if (immed_reply) {
af3db60a
LA
942 wil_dbg_wmi(wil, "recv_cmd: Complete WMI 0x%04x\n",
943 wil->reply_id);
0916d9f2
ME
944 kfree(evt);
945 num_immed_reply++;
946 complete(&wil->wmi_call);
947 } else {
948 /* add to the pending list */
949 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
950 list_add_tail(&evt->list, &wil->pending_wmi_ev);
951 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
952 q = queue_work(wil->wmi_wq, &wil->wmi_event_worker);
953 wil_dbg_wmi(wil, "queue_work -> %d\n", q);
954 }
2be7d22f 955 }
95266dc0 956 /* normally, 1 event per IRQ should be processed */
af3db60a 957 wil_dbg_wmi(wil, "recv_cmd: -> %d events queued, %d completed\n",
0916d9f2 958 n - num_immed_reply, num_immed_reply);
2be7d22f
VK
959}
960
961int wmi_call(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len,
962 u16 reply_id, void *reply, u8 reply_size, int to_msec)
963{
964 int rc;
f4bbb829 965 unsigned long remain;
2be7d22f
VK
966
967 mutex_lock(&wil->wmi_mutex);
968
fe5c271e
ME
969 spin_lock(&wil->wmi_ev_lock);
970 wil->reply_id = reply_id;
971 wil->reply_buf = reply;
972 wil->reply_size = reply_size;
4d4c4dc3 973 reinit_completion(&wil->wmi_call);
fe5c271e
ME
974 spin_unlock(&wil->wmi_ev_lock);
975
2be7d22f
VK
976 rc = __wmi_send(wil, cmdid, buf, len);
977 if (rc)
978 goto out;
979
59502647
DL
980 remain = wait_for_completion_timeout(&wil->wmi_call,
981 msecs_to_jiffies(to_msec));
2be7d22f
VK
982 if (0 == remain) {
983 wil_err(wil, "wmi_call(0x%04x->0x%04x) timeout %d msec\n",
984 cmdid, reply_id, to_msec);
985 rc = -ETIME;
986 } else {
7743882d 987 wil_dbg_wmi(wil,
2be7d22f
VK
988 "wmi_call(0x%04x->0x%04x) completed in %d msec\n",
989 cmdid, reply_id,
990 to_msec - jiffies_to_msecs(remain));
991 }
fe5c271e
ME
992
993out:
994 spin_lock(&wil->wmi_ev_lock);
2be7d22f
VK
995 wil->reply_id = 0;
996 wil->reply_buf = NULL;
997 wil->reply_size = 0;
fe5c271e
ME
998 spin_unlock(&wil->wmi_ev_lock);
999
2be7d22f
VK
1000 mutex_unlock(&wil->wmi_mutex);
1001
1002 return rc;
1003}
1004
1005int wmi_echo(struct wil6210_priv *wil)
1006{
1007 struct wmi_echo_cmd cmd = {
1008 .value = cpu_to_le32(0x12345678),
1009 };
1010
1011 return wmi_call(wil, WMI_ECHO_CMDID, &cmd, sizeof(cmd),
a54a40da 1012 WMI_ECHO_RSP_EVENTID, NULL, 0, 50);
2be7d22f
VK
1013}
1014
1015int wmi_set_mac_address(struct wil6210_priv *wil, void *addr)
1016{
1017 struct wmi_set_mac_address_cmd cmd;
1018
a82553bb 1019 ether_addr_copy(cmd.mac, addr);
2be7d22f 1020
7743882d 1021 wil_dbg_wmi(wil, "Set MAC %pM\n", addr);
2be7d22f
VK
1022
1023 return wmi_send(wil, WMI_SET_MAC_ADDRESS_CMDID, &cmd, sizeof(cmd));
1024}
1025
10d599ad
ME
1026int wmi_led_cfg(struct wil6210_priv *wil, bool enable)
1027{
1028 int rc = 0;
1029 struct wmi_led_cfg_cmd cmd = {
1030 .led_mode = enable,
1031 .id = led_id,
1032 .slow_blink_cfg.blink_on =
1033 cpu_to_le32(led_blink_time[WIL_LED_TIME_SLOW].on_ms),
1034 .slow_blink_cfg.blink_off =
1035 cpu_to_le32(led_blink_time[WIL_LED_TIME_SLOW].off_ms),
1036 .medium_blink_cfg.blink_on =
1037 cpu_to_le32(led_blink_time[WIL_LED_TIME_MED].on_ms),
1038 .medium_blink_cfg.blink_off =
1039 cpu_to_le32(led_blink_time[WIL_LED_TIME_MED].off_ms),
1040 .fast_blink_cfg.blink_on =
1041 cpu_to_le32(led_blink_time[WIL_LED_TIME_FAST].on_ms),
1042 .fast_blink_cfg.blink_off =
1043 cpu_to_le32(led_blink_time[WIL_LED_TIME_FAST].off_ms),
1044 .led_polarity = led_polarity,
1045 };
1046 struct {
1047 struct wmi_cmd_hdr wmi;
1048 struct wmi_led_cfg_done_event evt;
1049 } __packed reply;
1050
1051 if (led_id == WIL_LED_INVALID_ID)
1052 goto out;
1053
1054 if (led_id > WIL_LED_MAX_ID) {
1055 wil_err(wil, "Invalid led id %d\n", led_id);
1056 rc = -EINVAL;
1057 goto out;
1058 }
1059
1060 wil_dbg_wmi(wil,
1061 "%s led %d\n",
1062 enable ? "enabling" : "disabling", led_id);
1063
1064 rc = wmi_call(wil, WMI_LED_CFG_CMDID, &cmd, sizeof(cmd),
1065 WMI_LED_CFG_DONE_EVENTID, &reply, sizeof(reply),
1066 100);
1067 if (rc)
1068 goto out;
1069
1070 if (reply.evt.status) {
1071 wil_err(wil, "led %d cfg failed with status %d\n",
1072 led_id, le32_to_cpu(reply.evt.status));
1073 rc = -EINVAL;
1074 }
1075
1076out:
1077 return rc;
1078}
1079
8e52fe30 1080int wmi_pcp_start(struct wil6210_priv *wil, int bi, u8 wmi_nettype,
b4944f2c 1081 u8 chan, u8 hidden_ssid, u8 is_go)
2be7d22f 1082{
b8023177
VK
1083 int rc;
1084
1085 struct wmi_pcp_start_cmd cmd = {
2be7d22f
VK
1086 .bcon_interval = cpu_to_le16(bi),
1087 .network_type = wmi_nettype,
1088 .disable_sec_offload = 1,
adc2d122 1089 .channel = chan - 1,
1eb9d1e5 1090 .pcp_max_assoc_sta = max_assoc_sta,
8e52fe30 1091 .hidden_ssid = hidden_ssid,
b4944f2c 1092 .is_go = is_go,
849a564b 1093 .disable_ap_sme = disable_ap_sme,
c3bfea05 1094 .abft_len = wil->abft_len,
2be7d22f 1095 };
b8023177 1096 struct {
b874ddec 1097 struct wmi_cmd_hdr wmi;
b8023177
VK
1098 struct wmi_pcp_started_event evt;
1099 } __packed reply;
2be7d22f 1100
774974e5 1101 if (!wil->privacy)
2be7d22f
VK
1102 cmd.disable_sec = 1;
1103
1eb9d1e5
DL
1104 if ((cmd.pcp_max_assoc_sta > WIL6210_MAX_CID) ||
1105 (cmd.pcp_max_assoc_sta <= 0)) {
1106 wil_info(wil,
1107 "Requested connection limit %u, valid values are 1 - %d. Setting to %d\n",
1108 max_assoc_sta, WIL6210_MAX_CID, WIL6210_MAX_CID);
1109 cmd.pcp_max_assoc_sta = WIL6210_MAX_CID;
1110 }
1111
849a564b
DL
1112 if (disable_ap_sme &&
1113 !test_bit(WMI_FW_CAPABILITY_DISABLE_AP_SME,
1114 wil->fw_capabilities)) {
1115 wil_err(wil, "disable_ap_sme not supported by FW\n");
1116 return -EOPNOTSUPP;
1117 }
1118
8b5c7f6c
VK
1119 /*
1120 * Processing time may be huge, in case of secure AP it takes about
1121 * 3500ms for FW to start AP
1122 */
b8023177 1123 rc = wmi_call(wil, WMI_PCP_START_CMDID, &cmd, sizeof(cmd),
8b5c7f6c 1124 WMI_PCP_STARTED_EVENTID, &reply, sizeof(reply), 5000);
b8023177
VK
1125 if (rc)
1126 return rc;
1127
1128 if (reply.evt.status != WMI_FW_STATUS_SUCCESS)
1129 rc = -EINVAL;
1130
10d599ad
ME
1131 if (wmi_nettype != WMI_NETTYPE_P2P)
1132 /* Don't fail due to error in the led configuration */
1133 wmi_led_cfg(wil, true);
1134
b8023177
VK
1135 return rc;
1136}
1137
1138int wmi_pcp_stop(struct wil6210_priv *wil)
1139{
10d599ad
ME
1140 int rc;
1141
1142 rc = wmi_led_cfg(wil, false);
1143 if (rc)
1144 return rc;
1145
b8023177
VK
1146 return wmi_call(wil, WMI_PCP_STOP_CMDID, NULL, 0,
1147 WMI_PCP_STOPPED_EVENTID, NULL, 0, 20);
2be7d22f
VK
1148}
1149
1150int wmi_set_ssid(struct wil6210_priv *wil, u8 ssid_len, const void *ssid)
1151{
1152 struct wmi_set_ssid_cmd cmd = {
1153 .ssid_len = cpu_to_le32(ssid_len),
1154 };
1155
1156 if (ssid_len > sizeof(cmd.ssid))
1157 return -EINVAL;
1158
1159 memcpy(cmd.ssid, ssid, ssid_len);
1160
1161 return wmi_send(wil, WMI_SET_SSID_CMDID, &cmd, sizeof(cmd));
1162}
1163
1164int wmi_get_ssid(struct wil6210_priv *wil, u8 *ssid_len, void *ssid)
1165{
1166 int rc;
1167 struct {
b874ddec 1168 struct wmi_cmd_hdr wmi;
2be7d22f
VK
1169 struct wmi_set_ssid_cmd cmd;
1170 } __packed reply;
1171 int len; /* reply.cmd.ssid_len in CPU order */
1172
1173 rc = wmi_call(wil, WMI_GET_SSID_CMDID, NULL, 0, WMI_GET_SSID_EVENTID,
1174 &reply, sizeof(reply), 20);
1175 if (rc)
1176 return rc;
1177
1178 len = le32_to_cpu(reply.cmd.ssid_len);
1179 if (len > sizeof(reply.cmd.ssid))
1180 return -EINVAL;
1181
1182 *ssid_len = len;
1183 memcpy(ssid, reply.cmd.ssid, len);
1184
1185 return 0;
1186}
1187
1188int wmi_set_channel(struct wil6210_priv *wil, int channel)
1189{
1190 struct wmi_set_pcp_channel_cmd cmd = {
1191 .channel = channel - 1,
1192 };
1193
1194 return wmi_send(wil, WMI_SET_PCP_CHANNEL_CMDID, &cmd, sizeof(cmd));
1195}
1196
1197int wmi_get_channel(struct wil6210_priv *wil, int *channel)
1198{
1199 int rc;
1200 struct {
b874ddec 1201 struct wmi_cmd_hdr wmi;
2be7d22f
VK
1202 struct wmi_set_pcp_channel_cmd cmd;
1203 } __packed reply;
1204
1205 rc = wmi_call(wil, WMI_GET_PCP_CHANNEL_CMDID, NULL, 0,
1206 WMI_GET_PCP_CHANNEL_EVENTID, &reply, sizeof(reply), 20);
1207 if (rc)
1208 return rc;
1209
1210 if (reply.cmd.channel > 3)
1211 return -EINVAL;
1212
1213 *channel = reply.cmd.channel + 1;
1214
1215 return 0;
1216}
1217
e6d68341 1218int wmi_p2p_cfg(struct wil6210_priv *wil, int channel, int bi)
b8023177 1219{
e6d68341 1220 int rc;
b8023177 1221 struct wmi_p2p_cfg_cmd cmd = {
e6d68341
DL
1222 .discovery_mode = WMI_DISCOVERY_MODE_PEER2PEER,
1223 .bcon_interval = cpu_to_le16(bi),
b8023177
VK
1224 .channel = channel - 1,
1225 };
e6d68341
DL
1226 struct {
1227 struct wmi_cmd_hdr wmi;
1228 struct wmi_p2p_cfg_done_event evt;
1229 } __packed reply;
1230
1231 wil_dbg_wmi(wil, "sending WMI_P2P_CFG_CMDID\n");
1232
1233 rc = wmi_call(wil, WMI_P2P_CFG_CMDID, &cmd, sizeof(cmd),
1234 WMI_P2P_CFG_DONE_EVENTID, &reply, sizeof(reply), 300);
1235 if (!rc && reply.evt.status != WMI_FW_STATUS_SUCCESS) {
1236 wil_err(wil, "P2P_CFG failed. status %d\n", reply.evt.status);
1237 rc = -EINVAL;
1238 }
1239
1240 return rc;
1241}
1242
1243int wmi_start_listen(struct wil6210_priv *wil)
1244{
1245 int rc;
1246 struct {
1247 struct wmi_cmd_hdr wmi;
1248 struct wmi_listen_started_event evt;
1249 } __packed reply;
1250
1251 wil_dbg_wmi(wil, "sending WMI_START_LISTEN_CMDID\n");
1252
1253 rc = wmi_call(wil, WMI_START_LISTEN_CMDID, NULL, 0,
1254 WMI_LISTEN_STARTED_EVENTID, &reply, sizeof(reply), 300);
1255 if (!rc && reply.evt.status != WMI_FW_STATUS_SUCCESS) {
1256 wil_err(wil, "device failed to start listen. status %d\n",
1257 reply.evt.status);
1258 rc = -EINVAL;
1259 }
b8023177 1260
e6d68341
DL
1261 return rc;
1262}
1263
1264int wmi_start_search(struct wil6210_priv *wil)
1265{
1266 int rc;
1267 struct {
1268 struct wmi_cmd_hdr wmi;
1269 struct wmi_search_started_event evt;
1270 } __packed reply;
1271
1272 wil_dbg_wmi(wil, "sending WMI_START_SEARCH_CMDID\n");
1273
1274 rc = wmi_call(wil, WMI_START_SEARCH_CMDID, NULL, 0,
1275 WMI_SEARCH_STARTED_EVENTID, &reply, sizeof(reply), 300);
1276 if (!rc && reply.evt.status != WMI_FW_STATUS_SUCCESS) {
1277 wil_err(wil, "device failed to start search. status %d\n",
1278 reply.evt.status);
1279 rc = -EINVAL;
1280 }
1281
1282 return rc;
1283}
1284
1285int wmi_stop_discovery(struct wil6210_priv *wil)
1286{
1287 int rc;
1288
1289 wil_dbg_wmi(wil, "sending WMI_DISCOVERY_STOP_CMDID\n");
1290
1291 rc = wmi_call(wil, WMI_DISCOVERY_STOP_CMDID, NULL, 0,
1292 WMI_DISCOVERY_STOPPED_EVENTID, NULL, 0, 100);
1293
1294 if (rc)
1295 wil_err(wil, "Failed to stop discovery\n");
1296
1297 return rc;
b8023177
VK
1298}
1299
2be7d22f 1300int wmi_del_cipher_key(struct wil6210_priv *wil, u8 key_index,
230d8442 1301 const void *mac_addr, int key_usage)
2be7d22f
VK
1302{
1303 struct wmi_delete_cipher_key_cmd cmd = {
1304 .key_index = key_index,
1305 };
1306
1307 if (mac_addr)
1308 memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
1309
1310 return wmi_send(wil, WMI_DELETE_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
1311}
1312
1313int wmi_add_cipher_key(struct wil6210_priv *wil, u8 key_index,
230d8442
VK
1314 const void *mac_addr, int key_len, const void *key,
1315 int key_usage)
2be7d22f
VK
1316{
1317 struct wmi_add_cipher_key_cmd cmd = {
1318 .key_index = key_index,
230d8442 1319 .key_usage = key_usage,
2be7d22f
VK
1320 .key_len = key_len,
1321 };
1322
1323 if (!key || (key_len > sizeof(cmd.key)))
1324 return -EINVAL;
1325
1326 memcpy(cmd.key, key, key_len);
1327 if (mac_addr)
1328 memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
1329
1330 return wmi_send(wil, WMI_ADD_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
1331}
1332
1333int wmi_set_ie(struct wil6210_priv *wil, u8 type, u16 ie_len, const void *ie)
1334{
5421bf0c
VK
1335 static const char *const names[] = {
1336 [WMI_FRAME_BEACON] = "BEACON",
1337 [WMI_FRAME_PROBE_REQ] = "PROBE_REQ",
1338 [WMI_FRAME_PROBE_RESP] = "WMI_FRAME_PROBE_RESP",
1339 [WMI_FRAME_ASSOC_REQ] = "WMI_FRAME_ASSOC_REQ",
1340 [WMI_FRAME_ASSOC_RESP] = "WMI_FRAME_ASSOC_RESP",
1341 };
2be7d22f
VK
1342 int rc;
1343 u16 len = sizeof(struct wmi_set_appie_cmd) + ie_len;
1344 struct wmi_set_appie_cmd *cmd = kzalloc(len, GFP_KERNEL);
8fe59627 1345
5421bf0c
VK
1346 if (!cmd) {
1347 rc = -ENOMEM;
1348 goto out;
1349 }
ac4acdb7
VK
1350 if (!ie)
1351 ie_len = 0;
2be7d22f
VK
1352
1353 cmd->mgmt_frm_type = type;
1354 /* BUG: FW API define ieLen as u8. Will fix FW */
1355 cmd->ie_len = cpu_to_le16(ie_len);
1356 memcpy(cmd->ie_info, ie, ie_len);
03866e7d 1357 rc = wmi_send(wil, WMI_SET_APPIE_CMDID, cmd, len);
2be7d22f 1358 kfree(cmd);
5421bf0c
VK
1359out:
1360 if (rc) {
1361 const char *name = type < ARRAY_SIZE(names) ?
1362 names[type] : "??";
1363 wil_err(wil, "set_ie(%d %s) failed : %d\n", type, name, rc);
1364 }
2be7d22f
VK
1365
1366 return rc;
1367}
1368
1647f12f
VK
1369/**
1370 * wmi_rxon - turn radio on/off
1371 * @on: turn on if true, off otherwise
1372 *
1373 * Only switch radio. Channel should be set separately.
1374 * No timeout for rxon - radio turned on forever unless some other call
1375 * turns it off
1376 */
1377int wmi_rxon(struct wil6210_priv *wil, bool on)
1378{
1379 int rc;
1380 struct {
b874ddec 1381 struct wmi_cmd_hdr wmi;
1647f12f
VK
1382 struct wmi_listen_started_event evt;
1383 } __packed reply;
1384
af3db60a 1385 wil_info(wil, "(%s)\n", on ? "on" : "off");
1647f12f
VK
1386
1387 if (on) {
1388 rc = wmi_call(wil, WMI_START_LISTEN_CMDID, NULL, 0,
1389 WMI_LISTEN_STARTED_EVENTID,
1390 &reply, sizeof(reply), 100);
1391 if ((rc == 0) && (reply.evt.status != WMI_FW_STATUS_SUCCESS))
1392 rc = -EINVAL;
1393 } else {
1394 rc = wmi_call(wil, WMI_DISCOVERY_STOP_CMDID, NULL, 0,
1395 WMI_DISCOVERY_STOPPED_EVENTID, NULL, 0, 20);
1396 }
1397
1398 return rc;
1399}
1400
47e19af9
VK
1401int wmi_rx_chain_add(struct wil6210_priv *wil, struct vring *vring)
1402{
1403 struct wireless_dev *wdev = wil->wdev;
1404 struct net_device *ndev = wil_to_ndev(wil);
1405 struct wmi_cfg_rx_chain_cmd cmd = {
1406 .action = WMI_RX_CHAIN_ADD,
1407 .rx_sw_ring = {
52a45702
LD
1408 .max_mpdu_size = cpu_to_le16(
1409 wil_mtu2macbuf(wil->rx_buf_len)),
47e19af9
VK
1410 .ring_mem_base = cpu_to_le64(vring->pa),
1411 .ring_size = cpu_to_le16(vring->size),
1412 },
1413 .mid = 0, /* TODO - what is it? */
1414 .decap_trans_type = WMI_DECAP_TYPE_802_3,
b4490f42 1415 .reorder_type = WMI_RX_SW_REORDER,
ab954628 1416 .host_thrsh = cpu_to_le16(rx_ring_overflow_thrsh),
47e19af9
VK
1417 };
1418 struct {
b874ddec 1419 struct wmi_cmd_hdr wmi;
47e19af9
VK
1420 struct wmi_cfg_rx_chain_done_event evt;
1421 } __packed evt;
1422 int rc;
1423
1424 if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
1425 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
1426
1427 cmd.sniffer_cfg.mode = cpu_to_le32(WMI_SNIFFER_ON);
1428 if (ch)
1429 cmd.sniffer_cfg.channel = ch->hw_value - 1;
1430 cmd.sniffer_cfg.phy_info_mode =
1431 cpu_to_le32(ndev->type == ARPHRD_IEEE80211_RADIOTAP);
1432 cmd.sniffer_cfg.phy_support =
1433 cpu_to_le32((wil->monitor_flags & MONITOR_FLAG_CONTROL)
4765332d 1434 ? WMI_SNIFFER_CP : WMI_SNIFFER_BOTH_PHYS);
504937d4
KE
1435 } else {
1436 /* Initialize offload (in non-sniffer mode).
1437 * Linux IP stack always calculates IP checksum
1438 * HW always calculate TCP/UDP checksum
1439 */
1440 cmd.l3_l4_ctrl |= (1 << L3_L4_CTRL_TCPIP_CHECKSUM_EN_POS);
47e19af9 1441 }
c406ea7c
VK
1442
1443 if (rx_align_2)
1444 cmd.l2_802_3_offload_ctrl |=
1445 L2_802_3_OFFLOAD_CTRL_SNAP_KEEP_MSK;
1446
47e19af9
VK
1447 /* typical time for secure PCP is 840ms */
1448 rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, &cmd, sizeof(cmd),
1449 WMI_CFG_RX_CHAIN_DONE_EVENTID, &evt, sizeof(evt), 2000);
1450 if (rc)
1451 return rc;
1452
1453 vring->hwtail = le32_to_cpu(evt.evt.rx_ring_tail_ptr);
1454
7743882d 1455 wil_dbg_misc(wil, "Rx init: status %d tail 0x%08x\n",
47e19af9
VK
1456 le32_to_cpu(evt.evt.status), vring->hwtail);
1457
1458 if (le32_to_cpu(evt.evt.status) != WMI_CFG_RX_CHAIN_SUCCESS)
1459 rc = -EINVAL;
1460
1461 return rc;
1462}
1463
8c679675 1464int wmi_get_temperature(struct wil6210_priv *wil, u32 *t_bb, u32 *t_rf)
1a2780e0
VK
1465{
1466 int rc;
1467 struct wmi_temp_sense_cmd cmd = {
8c679675
VK
1468 .measure_baseband_en = cpu_to_le32(!!t_bb),
1469 .measure_rf_en = cpu_to_le32(!!t_rf),
1470 .measure_mode = cpu_to_le32(TEMPERATURE_MEASURE_NOW),
1a2780e0
VK
1471 };
1472 struct {
b874ddec 1473 struct wmi_cmd_hdr wmi;
1a2780e0
VK
1474 struct wmi_temp_sense_done_event evt;
1475 } __packed reply;
1476
1477 rc = wmi_call(wil, WMI_TEMP_SENSE_CMDID, &cmd, sizeof(cmd),
1478 WMI_TEMP_SENSE_DONE_EVENTID, &reply, sizeof(reply), 100);
1479 if (rc)
1480 return rc;
1481
8c679675
VK
1482 if (t_bb)
1483 *t_bb = le32_to_cpu(reply.evt.baseband_t1000);
1484 if (t_rf)
1485 *t_rf = le32_to_cpu(reply.evt.rf_t1000);
1a2780e0
VK
1486
1487 return 0;
1488}
1489
849a564b
DL
1490int wmi_disconnect_sta(struct wil6210_priv *wil, const u8 *mac,
1491 u16 reason, bool full_disconnect, bool del_sta)
4d55a0a1 1492{
3e9191fc
VK
1493 int rc;
1494 u16 reason_code;
849a564b
DL
1495 struct wmi_disconnect_sta_cmd disc_sta_cmd = {
1496 .disconnect_reason = cpu_to_le16(reason),
1497 };
1498 struct wmi_del_sta_cmd del_sta_cmd = {
4d55a0a1
VK
1499 .disconnect_reason = cpu_to_le16(reason),
1500 };
3e9191fc 1501 struct {
b874ddec 1502 struct wmi_cmd_hdr wmi;
3e9191fc
VK
1503 struct wmi_disconnect_event evt;
1504 } __packed reply;
a82553bb 1505
af3db60a 1506 wil_dbg_wmi(wil, "disconnect_sta: (%pM, reason %d)\n", mac, reason);
4d55a0a1 1507
3b56c15f 1508 wil->locally_generated_disc = true;
849a564b
DL
1509 if (del_sta) {
1510 ether_addr_copy(del_sta_cmd.dst_mac, mac);
1511 rc = wmi_call(wil, WMI_DEL_STA_CMDID, &del_sta_cmd,
1512 sizeof(del_sta_cmd), WMI_DISCONNECT_EVENTID,
1513 &reply, sizeof(reply), 1000);
1514 } else {
1515 ether_addr_copy(disc_sta_cmd.dst_mac, mac);
1516 rc = wmi_call(wil, WMI_DISCONNECT_STA_CMDID, &disc_sta_cmd,
1517 sizeof(disc_sta_cmd), WMI_DISCONNECT_EVENTID,
1518 &reply, sizeof(reply), 1000);
1519 }
3e9191fc
VK
1520 /* failure to disconnect in reasonable time treated as FW error */
1521 if (rc) {
1522 wil_fw_error_recovery(wil);
1523 return rc;
1524 }
1525
0916d9f2
ME
1526 if (full_disconnect) {
1527 /* call event handler manually after processing wmi_call,
1528 * to avoid deadlock - disconnect event handler acquires
1529 * wil->mutex while it is already held here
1530 */
1531 reason_code = le16_to_cpu(reply.evt.protocol_reason_status);
3e9191fc 1532
0916d9f2
ME
1533 wil_dbg_wmi(wil, "Disconnect %pM reason [proto %d wmi %d]\n",
1534 reply.evt.bssid, reason_code,
1535 reply.evt.disconnect_reason);
3e9191fc 1536
0916d9f2
ME
1537 wil->sinfo_gen++;
1538 wil6210_disconnect(wil, reply.evt.bssid, reason_code, true);
1539 }
3e9191fc 1540 return 0;
4d55a0a1
VK
1541}
1542
3277213f
VK
1543int wmi_addba(struct wil6210_priv *wil, u8 ringid, u8 size, u16 timeout)
1544{
1545 struct wmi_vring_ba_en_cmd cmd = {
1546 .ringid = ringid,
1547 .agg_max_wsize = size,
1548 .ba_timeout = cpu_to_le16(timeout),
cbcf5866 1549 .amsdu = 0,
3277213f
VK
1550 };
1551
af3db60a
LA
1552 wil_dbg_wmi(wil, "addba: (ring %d size %d timeout %d)\n", ringid, size,
1553 timeout);
3277213f
VK
1554
1555 return wmi_send(wil, WMI_VRING_BA_EN_CMDID, &cmd, sizeof(cmd));
1556}
1557
26a359d9 1558int wmi_delba_tx(struct wil6210_priv *wil, u8 ringid, u16 reason)
3277213f
VK
1559{
1560 struct wmi_vring_ba_dis_cmd cmd = {
1561 .ringid = ringid,
1562 .reason = cpu_to_le16(reason),
1563 };
1564
af3db60a 1565 wil_dbg_wmi(wil, "delba_tx: (ring %d reason %d)\n", ringid, reason);
3277213f
VK
1566
1567 return wmi_send(wil, WMI_VRING_BA_DIS_CMDID, &cmd, sizeof(cmd));
1568}
1569
26a359d9
VK
1570int wmi_delba_rx(struct wil6210_priv *wil, u8 cidxtid, u16 reason)
1571{
1572 struct wmi_rcp_delba_cmd cmd = {
1573 .cidxtid = cidxtid,
1574 .reason = cpu_to_le16(reason),
1575 };
1576
af3db60a
LA
1577 wil_dbg_wmi(wil, "delba_rx: (CID %d TID %d reason %d)\n", cidxtid & 0xf,
1578 (cidxtid >> 4) & 0xf, reason);
26a359d9
VK
1579
1580 return wmi_send(wil, WMI_RCP_DELBA_CMDID, &cmd, sizeof(cmd));
1581}
1582
3277213f
VK
1583int wmi_addba_rx_resp(struct wil6210_priv *wil, u8 cid, u8 tid, u8 token,
1584 u16 status, bool amsdu, u16 agg_wsize, u16 timeout)
1585{
1586 int rc;
1587 struct wmi_rcp_addba_resp_cmd cmd = {
1588 .cidxtid = mk_cidxtid(cid, tid),
1589 .dialog_token = token,
1590 .status_code = cpu_to_le16(status),
1591 /* bit 0: A-MSDU supported
1592 * bit 1: policy (should be 0 for us)
1593 * bits 2..5: TID
1594 * bits 6..15: buffer size
1595 */
1596 .ba_param_set = cpu_to_le16((amsdu ? 1 : 0) | (tid << 2) |
1597 (agg_wsize << 6)),
1598 .ba_timeout = cpu_to_le16(timeout),
1599 };
1600 struct {
b874ddec 1601 struct wmi_cmd_hdr wmi;
3277213f
VK
1602 struct wmi_rcp_addba_resp_sent_event evt;
1603 } __packed reply;
1604
1605 wil_dbg_wmi(wil,
1606 "ADDBA response for CID %d TID %d size %d timeout %d status %d AMSDU%s\n",
1607 cid, tid, agg_wsize, timeout, status, amsdu ? "+" : "-");
1608
1609 rc = wmi_call(wil, WMI_RCP_ADDBA_RESP_CMDID, &cmd, sizeof(cmd),
230d8442
VK
1610 WMI_RCP_ADDBA_RESP_SENT_EVENTID, &reply, sizeof(reply),
1611 100);
3277213f
VK
1612 if (rc)
1613 return rc;
1614
1615 if (reply.evt.status) {
1616 wil_err(wil, "ADDBA response failed with status %d\n",
1617 le16_to_cpu(reply.evt.status));
1618 rc = -EINVAL;
1619 }
1620
1621 return rc;
1622}
1623
2c207eb8
ME
1624int wmi_ps_dev_profile_cfg(struct wil6210_priv *wil,
1625 enum wmi_ps_profile_type ps_profile)
1626{
1627 int rc;
1628 struct wmi_ps_dev_profile_cfg_cmd cmd = {
1629 .ps_profile = ps_profile,
1630 };
1631 struct {
1632 struct wmi_cmd_hdr wmi;
1633 struct wmi_ps_dev_profile_cfg_event evt;
1634 } __packed reply;
1635 u32 status;
1636
1637 wil_dbg_wmi(wil, "Setting ps dev profile %d\n", ps_profile);
1638
1639 reply.evt.status = cpu_to_le32(WMI_PS_CFG_CMD_STATUS_ERROR);
1640
1641 rc = wmi_call(wil, WMI_PS_DEV_PROFILE_CFG_CMDID, &cmd, sizeof(cmd),
1642 WMI_PS_DEV_PROFILE_CFG_EVENTID, &reply, sizeof(reply),
1643 100);
1644 if (rc)
1645 return rc;
1646
1647 status = le32_to_cpu(reply.evt.status);
1648
1649 if (status != WMI_PS_CFG_CMD_STATUS_SUCCESS) {
1650 wil_err(wil, "ps dev profile cfg failed with status %d\n",
1651 status);
1652 rc = -EINVAL;
1653 }
1654
1655 return rc;
1656}
1657
3fea18d0
LD
1658int wmi_set_mgmt_retry(struct wil6210_priv *wil, u8 retry_short)
1659{
1660 int rc;
1661 struct wmi_set_mgmt_retry_limit_cmd cmd = {
1662 .mgmt_retry_limit = retry_short,
1663 };
1664 struct {
1665 struct wmi_cmd_hdr wmi;
1666 struct wmi_set_mgmt_retry_limit_event evt;
1667 } __packed reply;
1668
1669 wil_dbg_wmi(wil, "Setting mgmt retry short %d\n", retry_short);
1670
1671 if (!test_bit(WMI_FW_CAPABILITY_MGMT_RETRY_LIMIT, wil->fw_capabilities))
1672 return -ENOTSUPP;
1673
1674 reply.evt.status = WMI_FW_STATUS_FAILURE;
1675
1676 rc = wmi_call(wil, WMI_SET_MGMT_RETRY_LIMIT_CMDID, &cmd, sizeof(cmd),
1677 WMI_SET_MGMT_RETRY_LIMIT_EVENTID, &reply, sizeof(reply),
1678 100);
1679 if (rc)
1680 return rc;
1681
1682 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
1683 wil_err(wil, "set mgmt retry limit failed with status %d\n",
1684 reply.evt.status);
1685 rc = -EINVAL;
1686 }
1687
1688 return rc;
1689}
1690
1691int wmi_get_mgmt_retry(struct wil6210_priv *wil, u8 *retry_short)
1692{
1693 int rc;
1694 struct {
1695 struct wmi_cmd_hdr wmi;
1696 struct wmi_get_mgmt_retry_limit_event evt;
1697 } __packed reply;
1698
1699 wil_dbg_wmi(wil, "getting mgmt retry short\n");
1700
1701 if (!test_bit(WMI_FW_CAPABILITY_MGMT_RETRY_LIMIT, wil->fw_capabilities))
1702 return -ENOTSUPP;
1703
1704 reply.evt.mgmt_retry_limit = 0;
1705 rc = wmi_call(wil, WMI_GET_MGMT_RETRY_LIMIT_CMDID, NULL, 0,
1706 WMI_GET_MGMT_RETRY_LIMIT_EVENTID, &reply, sizeof(reply),
1707 100);
1708 if (rc)
1709 return rc;
1710
1711 if (retry_short)
1712 *retry_short = reply.evt.mgmt_retry_limit;
1713
1714 return 0;
1715}
1716
035859a5
ME
1717int wmi_abort_scan(struct wil6210_priv *wil)
1718{
1719 int rc;
1720
1721 wil_dbg_wmi(wil, "sending WMI_ABORT_SCAN_CMDID\n");
1722
1723 rc = wmi_send(wil, WMI_ABORT_SCAN_CMDID, NULL, 0);
1724 if (rc)
1725 wil_err(wil, "Failed to abort scan (%d)\n", rc);
1726
1727 return rc;
1728}
1729
849a564b
DL
1730int wmi_new_sta(struct wil6210_priv *wil, const u8 *mac, u8 aid)
1731{
1732 int rc;
1733 struct wmi_new_sta_cmd cmd = {
1734 .aid = aid,
1735 };
1736
1737 wil_dbg_wmi(wil, "new sta %pM, aid %d\n", mac, aid);
1738
1739 ether_addr_copy(cmd.dst_mac, mac);
1740
1741 rc = wmi_send(wil, WMI_NEW_STA_CMDID, &cmd, sizeof(cmd));
1742 if (rc)
1743 wil_err(wil, "Failed to send new sta (%d)\n", rc);
1744
1745 return rc;
1746}
1747
2be7d22f
VK
1748void wmi_event_flush(struct wil6210_priv *wil)
1749{
f6b29b65 1750 ulong flags;
2be7d22f
VK
1751 struct pending_wmi_event *evt, *t;
1752
af3db60a 1753 wil_dbg_wmi(wil, "event_flush\n");
2be7d22f 1754
f6b29b65
HK
1755 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
1756
2be7d22f
VK
1757 list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
1758 list_del(&evt->list);
1759 kfree(evt);
1760 }
f6b29b65
HK
1761
1762 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
2be7d22f
VK
1763}
1764
1765static bool wmi_evt_call_handler(struct wil6210_priv *wil, int id,
1766 void *d, int len)
1767{
1768 uint i;
1769
1770 for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
1771 if (wmi_evt_handlers[i].eventid == id) {
1772 wmi_evt_handlers[i].handler(wil, id, d, len);
1773 return true;
1774 }
1775 }
1776
1777 return false;
1778}
1779
1780static void wmi_event_handle(struct wil6210_priv *wil,
1781 struct wil6210_mbox_hdr *hdr)
1782{
1783 u16 len = le16_to_cpu(hdr->len);
1784
1785 if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
b874ddec
LD
1786 (len >= sizeof(struct wmi_cmd_hdr))) {
1787 struct wmi_cmd_hdr *wmi = (void *)(&hdr[1]);
2be7d22f 1788 void *evt_data = (void *)(&wmi[1]);
b874ddec 1789 u16 id = le16_to_cpu(wmi->command_id);
028e1836
VK
1790
1791 wil_dbg_wmi(wil, "Handle WMI 0x%04x (reply_id 0x%04x)\n",
1792 id, wil->reply_id);
2be7d22f
VK
1793 /* check if someone waits for this event */
1794 if (wil->reply_id && wil->reply_id == id) {
0916d9f2
ME
1795 WARN_ON(wil->reply_buf);
1796 wmi_evt_call_handler(wil, id, evt_data,
1797 len - sizeof(*wmi));
af3db60a
LA
1798 wil_dbg_wmi(wil, "event_handle: Complete WMI 0x%04x\n",
1799 id);
59502647 1800 complete(&wil->wmi_call);
2be7d22f
VK
1801 return;
1802 }
1803 /* unsolicited event */
1804 /* search for handler */
1805 if (!wmi_evt_call_handler(wil, id, evt_data,
1806 len - sizeof(*wmi))) {
a3ce5ccd 1807 wil_info(wil, "Unhandled event 0x%04x\n", id);
2be7d22f
VK
1808 }
1809 } else {
1810 wil_err(wil, "Unknown event type\n");
1811 print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
1812 hdr, sizeof(*hdr) + len, true);
1813 }
1814}
1815
1816/*
1817 * Retrieve next WMI event from the pending list
1818 */
1819static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
1820{
1821 ulong flags;
1822 struct list_head *ret = NULL;
1823
1824 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
1825
1826 if (!list_empty(&wil->pending_wmi_ev)) {
1827 ret = wil->pending_wmi_ev.next;
1828 list_del(ret);
1829 }
1830
1831 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
1832
1833 return ret;
1834}
1835
1836/*
1837 * Handler for the WMI events
1838 */
1839void wmi_event_worker(struct work_struct *work)
1840{
1841 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
1842 wmi_event_worker);
1843 struct pending_wmi_event *evt;
1844 struct list_head *lh;
1845
af3db60a 1846 wil_dbg_wmi(wil, "event_worker: Start\n");
2be7d22f
VK
1847 while ((lh = next_wmi_ev(wil)) != NULL) {
1848 evt = list_entry(lh, struct pending_wmi_event, list);
1849 wmi_event_handle(wil, &evt->event.hdr);
1850 kfree(evt);
1851 }
af3db60a 1852 wil_dbg_wmi(wil, "event_worker: Finished\n");
2be7d22f 1853}