]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/wireless/ath/wil6210/wmi.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[mirror_ubuntu-jammy-kernel.git] / drivers / net / wireless / ath / wil6210 / wmi.c
CommitLineData
85630469 1// SPDX-License-Identifier: ISC
2be7d22f 2/*
849a564b 3 * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
387f3794 4 * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
2be7d22f
VK
5 */
6
1eb9d1e5 7#include <linux/moduleparam.h>
2be7d22f 8#include <linux/etherdevice.h>
47e19af9 9#include <linux/if_arp.h>
2be7d22f
VK
10
11#include "wil6210.h"
47e19af9 12#include "txrx.h"
2be7d22f 13#include "wmi.h"
98658095 14#include "trace.h"
2be7d22f 15
bf0353a6
AM
16/* set the default max assoc sta to max supported by driver */
17uint max_assoc_sta = WIL6210_MAX_CID;
18module_param(max_assoc_sta, uint, 0444);
1eb9d1e5
DL
19MODULE_PARM_DESC(max_assoc_sta, " Max number of stations associated to the AP");
20
3a3def8d 21int agg_wsize; /* = 0; */
78484c44 22module_param(agg_wsize, int, 0644);
3a3def8d
VK
23MODULE_PARM_DESC(agg_wsize, " Window size for Tx Block Ack after connect;"
24 " 0 - use default; < 0 - don't auto-establish");
25
10d599ad 26u8 led_id = WIL_LED_INVALID_ID;
78484c44 27module_param(led_id, byte, 0444);
10d599ad
ME
28MODULE_PARM_DESC(led_id,
29 " 60G device led enablement. Set the led ID (0-2) to enable");
30
fe9ee51e 31#define WIL_WAIT_FOR_SUSPEND_RESUME_COMP 200
5793fe9d 32#define WIL_WMI_PCP_STOP_TO_MS 5000
fe9ee51e 33
2be7d22f
VK
34/**
35 * WMI event receiving - theory of operations
36 *
37 * When firmware about to report WMI event, it fills memory area
38 * in the mailbox and raises misc. IRQ. Thread interrupt handler invoked for
39 * the misc IRQ, function @wmi_recv_cmd called by thread IRQ handler.
40 *
41 * @wmi_recv_cmd reads event, allocates memory chunk and attaches it to the
42 * event list @wil->pending_wmi_ev. Then, work queue @wil->wmi_wq wakes up
43 * and handles events within the @wmi_event_worker. Every event get detached
44 * from list, processed and deleted.
45 *
46 * Purpose for this mechanism is to release IRQ thread; otherwise,
47 * if WMI event handling involves another WMI command flow, this 2-nd flow
48 * won't be completed because of blocked IRQ thread.
49 */
50
51/**
52 * Addressing - theory of operations
53 *
54 * There are several buses present on the WIL6210 card.
55 * Same memory areas are visible at different address on
56 * the different busses. There are 3 main bus masters:
57 * - MAC CPU (ucode)
58 * - User CPU (firmware)
59 * - AHB (host)
60 *
61 * On the PCI bus, there is one BAR (BAR0) of 2Mb size, exposing
62 * AHB addresses starting from 0x880000
63 *
4276d771 64 * Internally, firmware uses addresses that allow faster access but
2be7d22f
VK
65 * are invisible from the host. To read from these addresses, alternative
66 * AHB address must be used.
2be7d22f
VK
67 */
68
69/**
4276d771 70 * @sparrow_fw_mapping provides memory remapping table for sparrow
b541d0a0
VK
71 *
72 * array size should be in sync with the declaration in the wil6210.h
4276d771
ME
73 *
74 * Sparrow memory mapping:
75 * Linker address PCI/Host address
76 * 0x880000 .. 0xa80000 2Mb BAR0
77 * 0x800000 .. 0x808000 0x900000 .. 0x908000 32k DCCM
78 * 0x840000 .. 0x860000 0x908000 .. 0x928000 128k PERIPH
2be7d22f 79 */
4276d771 80const struct fw_map sparrow_fw_mapping[] = {
61578820 81 /* FW code RAM 256k */
9a53d0b6 82 {0x000000, 0x040000, 0x8c0000, "fw_code", true, true},
61578820 83 /* FW data RAM 32k */
9a53d0b6 84 {0x800000, 0x808000, 0x900000, "fw_data", true, true},
61578820 85 /* periph data 128k */
9a53d0b6 86 {0x840000, 0x860000, 0x908000, "fw_peri", true, true},
61578820 87 /* various RGF 40k */
9a53d0b6 88 {0x880000, 0x88a000, 0x880000, "rgf", true, true},
61578820 89 /* AGC table 4k */
9a53d0b6 90 {0x88a000, 0x88b000, 0x88a000, "AGC_tbl", true, true},
61578820 91 /* Pcie_ext_rgf 4k */
9a53d0b6 92 {0x88b000, 0x88c000, 0x88b000, "rgf_ext", true, true},
61578820 93 /* mac_ext_rgf 512b */
9a53d0b6 94 {0x88c000, 0x88c200, 0x88c000, "mac_rgf_ext", true, true},
61578820 95 /* upper area 548k */
9a53d0b6 96 {0x8c0000, 0x949000, 0x8c0000, "upper", true, true},
61578820
LD
97 /* UCODE areas - accessible by debugfs blobs but not by
98 * wmi_addr_remap. UCODE areas MUST be added AFTER FW areas!
2be7d22f 99 */
61578820 100 /* ucode code RAM 128k */
9a53d0b6 101 {0x000000, 0x020000, 0x920000, "uc_code", false, false},
61578820 102 /* ucode data RAM 16k */
9a53d0b6 103 {0x800000, 0x804000, 0x940000, "uc_data", false, false},
2be7d22f
VK
104};
105
54fca595
LD
106/**
107 * @sparrow_d0_mac_rgf_ext - mac_rgf_ext section for Sparrow D0
108 * it is a bit larger to support extra features
109 */
110const struct fw_map sparrow_d0_mac_rgf_ext = {
9a53d0b6 111 0x88c000, 0x88c500, 0x88c000, "mac_rgf_ext", true, true
54fca595
LD
112};
113
4276d771
ME
114/**
115 * @talyn_fw_mapping provides memory remapping table for Talyn
116 *
117 * array size should be in sync with the declaration in the wil6210.h
118 *
119 * Talyn memory mapping:
120 * Linker address PCI/Host address
121 * 0x880000 .. 0xc80000 4Mb BAR0
122 * 0x800000 .. 0x820000 0xa00000 .. 0xa20000 128k DCCM
123 * 0x840000 .. 0x858000 0xa20000 .. 0xa38000 96k PERIPH
124 */
125const struct fw_map talyn_fw_mapping[] = {
126 /* FW code RAM 1M */
9a53d0b6 127 {0x000000, 0x100000, 0x900000, "fw_code", true, true},
4276d771 128 /* FW data RAM 128k */
9a53d0b6 129 {0x800000, 0x820000, 0xa00000, "fw_data", true, true},
4276d771 130 /* periph. data RAM 96k */
9a53d0b6 131 {0x840000, 0x858000, 0xa20000, "fw_peri", true, true},
4276d771 132 /* various RGF 40k */
9a53d0b6 133 {0x880000, 0x88a000, 0x880000, "rgf", true, true},
4276d771 134 /* AGC table 4k */
9a53d0b6 135 {0x88a000, 0x88b000, 0x88a000, "AGC_tbl", true, true},
4276d771 136 /* Pcie_ext_rgf 4k */
9a53d0b6 137 {0x88b000, 0x88c000, 0x88b000, "rgf_ext", true, true},
4276d771 138 /* mac_ext_rgf 1344b */
9a53d0b6 139 {0x88c000, 0x88c540, 0x88c000, "mac_rgf_ext", true, true},
4276d771 140 /* ext USER RGF 4k */
9a53d0b6 141 {0x88d000, 0x88e000, 0x88d000, "ext_user_rgf", true, true},
4276d771 142 /* OTP 4k */
9a53d0b6 143 {0x8a0000, 0x8a1000, 0x8a0000, "otp", true, false},
4276d771 144 /* DMA EXT RGF 64k */
9a53d0b6 145 {0x8b0000, 0x8c0000, 0x8b0000, "dma_ext_rgf", true, true},
485790d0 146 /* upper area 1536k */
9a53d0b6 147 {0x900000, 0xa80000, 0x900000, "upper", true, true},
485790d0
ME
148 /* UCODE areas - accessible by debugfs blobs but not by
149 * wmi_addr_remap. UCODE areas MUST be added AFTER FW areas!
150 */
151 /* ucode code RAM 256k */
9a53d0b6 152 {0x000000, 0x040000, 0xa38000, "uc_code", false, false},
485790d0 153 /* ucode data RAM 32k */
9a53d0b6 154 {0x800000, 0x808000, 0xa78000, "uc_data", false, false},
485790d0
ME
155};
156
157/**
158 * @talyn_mb_fw_mapping provides memory remapping table for Talyn-MB
159 *
160 * array size should be in sync with the declaration in the wil6210.h
161 *
162 * Talyn MB memory mapping:
163 * Linker address PCI/Host address
164 * 0x880000 .. 0xc80000 4Mb BAR0
165 * 0x800000 .. 0x820000 0xa00000 .. 0xa20000 128k DCCM
166 * 0x840000 .. 0x858000 0xa20000 .. 0xa38000 96k PERIPH
167 */
168const struct fw_map talyn_mb_fw_mapping[] = {
169 /* FW code RAM 768k */
9a53d0b6 170 {0x000000, 0x0c0000, 0x900000, "fw_code", true, true},
485790d0 171 /* FW data RAM 128k */
9a53d0b6 172 {0x800000, 0x820000, 0xa00000, "fw_data", true, true},
485790d0 173 /* periph. data RAM 96k */
9a53d0b6 174 {0x840000, 0x858000, 0xa20000, "fw_peri", true, true},
485790d0 175 /* various RGF 40k */
9a53d0b6 176 {0x880000, 0x88a000, 0x880000, "rgf", true, true},
485790d0 177 /* AGC table 4k */
9a53d0b6 178 {0x88a000, 0x88b000, 0x88a000, "AGC_tbl", true, true},
485790d0 179 /* Pcie_ext_rgf 4k */
9a53d0b6 180 {0x88b000, 0x88c000, 0x88b000, "rgf_ext", true, true},
485790d0 181 /* mac_ext_rgf 2256b */
9a53d0b6 182 {0x88c000, 0x88c8d0, 0x88c000, "mac_rgf_ext", true, true},
485790d0 183 /* ext USER RGF 4k */
9a53d0b6 184 {0x88d000, 0x88e000, 0x88d000, "ext_user_rgf", true, true},
485790d0 185 /* SEC PKA 16k */
9a53d0b6 186 {0x890000, 0x894000, 0x890000, "sec_pka", true, true},
485790d0 187 /* SEC KDF RGF 3096b */
9a53d0b6 188 {0x898000, 0x898c18, 0x898000, "sec_kdf_rgf", true, true},
485790d0 189 /* SEC MAIN 2124b */
9a53d0b6 190 {0x89a000, 0x89a84c, 0x89a000, "sec_main", true, true},
485790d0 191 /* OTP 4k */
9a53d0b6 192 {0x8a0000, 0x8a1000, 0x8a0000, "otp", true, false},
485790d0 193 /* DMA EXT RGF 64k */
9a53d0b6 194 {0x8b0000, 0x8c0000, 0x8b0000, "dma_ext_rgf", true, true},
485790d0 195 /* DUM USER RGF 528b */
9a53d0b6 196 {0x8c0000, 0x8c0210, 0x8c0000, "dum_user_rgf", true, true},
485790d0 197 /* DMA OFU 296b */
9a53d0b6 198 {0x8c2000, 0x8c2128, 0x8c2000, "dma_ofu", true, true},
485790d0 199 /* ucode debug 4k */
9a53d0b6 200 {0x8c3000, 0x8c4000, 0x8c3000, "ucode_debug", true, true},
4276d771 201 /* upper area 1536k */
9a53d0b6 202 {0x900000, 0xa80000, 0x900000, "upper", true, true},
4276d771
ME
203 /* UCODE areas - accessible by debugfs blobs but not by
204 * wmi_addr_remap. UCODE areas MUST be added AFTER FW areas!
205 */
206 /* ucode code RAM 256k */
9a53d0b6 207 {0x000000, 0x040000, 0xa38000, "uc_code", false, false},
4276d771 208 /* ucode data RAM 32k */
9a53d0b6 209 {0x800000, 0x808000, 0xa78000, "uc_data", false, false},
4276d771
ME
210};
211
212struct fw_map fw_mapping[MAX_FW_MAPPING_TABLE_SIZE];
213
10d599ad
ME
214struct blink_on_off_time led_blink_time[] = {
215 {WIL_LED_BLINK_ON_SLOW_MS, WIL_LED_BLINK_OFF_SLOW_MS},
216 {WIL_LED_BLINK_ON_MED_MS, WIL_LED_BLINK_OFF_MED_MS},
217 {WIL_LED_BLINK_ON_FAST_MS, WIL_LED_BLINK_OFF_FAST_MS},
218};
219
b9010f10
AM
220struct auth_no_hdr {
221 __le16 auth_alg;
222 __le16 auth_transaction;
223 __le16 status_code;
224 /* possibly followed by Challenge text */
225 u8 variable[0];
226} __packed;
227
10d599ad
ME
228u8 led_polarity = LED_POLARITY_LOW_ACTIVE;
229
2be7d22f 230/**
61578820 231 * return AHB address for given firmware internal (linker) address
2be7d22f
VK
232 * @x - internal address
233 * If address have no valid AHB mapping, return 0
234 */
235static u32 wmi_addr_remap(u32 x)
236{
237 uint i;
238
239 for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
61578820
LD
240 if (fw_mapping[i].fw &&
241 ((x >= fw_mapping[i].from) && (x < fw_mapping[i].to)))
2be7d22f
VK
242 return x + fw_mapping[i].host - fw_mapping[i].from;
243 }
244
245 return 0;
246}
247
70bcc658
LD
248/**
249 * find fw_mapping entry by section name
250 * @section - section name
251 *
252 * Return pointer to section or NULL if not found
253 */
254struct fw_map *wil_find_fw_mapping(const char *section)
255{
256 int i;
257
258 for (i = 0; i < ARRAY_SIZE(fw_mapping); i++)
259 if (fw_mapping[i].name &&
260 !strcmp(section, fw_mapping[i].name))
261 return &fw_mapping[i];
262
263 return NULL;
264}
265
2be7d22f
VK
266/**
267 * Check address validity for WMI buffer; remap if needed
268 * @ptr - internal (linker) fw/ucode address
705d2fde
LD
269 * @size - if non zero, validate the block does not
270 * exceed the device memory (bar)
2be7d22f
VK
271 *
272 * Valid buffer should be DWORD aligned
273 *
274 * return address for accessing buffer from the host;
275 * if buffer is not valid, return NULL.
276 */
705d2fde 277void __iomem *wmi_buffer_block(struct wil6210_priv *wil, __le32 ptr_, u32 size)
2be7d22f
VK
278{
279 u32 off;
280 u32 ptr = le32_to_cpu(ptr_);
281
282 if (ptr % 4)
283 return NULL;
284
285 ptr = wmi_addr_remap(ptr);
286 if (ptr < WIL6210_FW_HOST_OFF)
287 return NULL;
288
289 off = HOSTADDR(ptr);
d86d4716 290 if (off > wil->bar_size - 4)
2be7d22f 291 return NULL;
705d2fde
LD
292 if (size && ((off + size > wil->bar_size) || (off + size < off)))
293 return NULL;
2be7d22f
VK
294
295 return wil->csr + off;
296}
297
705d2fde
LD
298void __iomem *wmi_buffer(struct wil6210_priv *wil, __le32 ptr_)
299{
300 return wmi_buffer_block(wil, ptr_, 0);
301}
302
2be7d22f
VK
303/**
304 * Check address validity
305 */
306void __iomem *wmi_addr(struct wil6210_priv *wil, u32 ptr)
307{
308 u32 off;
309
310 if (ptr % 4)
311 return NULL;
312
313 if (ptr < WIL6210_FW_HOST_OFF)
314 return NULL;
315
316 off = HOSTADDR(ptr);
d86d4716 317 if (off > wil->bar_size - 4)
2be7d22f
VK
318 return NULL;
319
320 return wil->csr + off;
321}
322
323int wmi_read_hdr(struct wil6210_priv *wil, __le32 ptr,
324 struct wil6210_mbox_hdr *hdr)
325{
326 void __iomem *src = wmi_buffer(wil, ptr);
8fe59627 327
2be7d22f
VK
328 if (!src)
329 return -EINVAL;
330
331 wil_memcpy_fromio_32(hdr, src, sizeof(*hdr));
332
333 return 0;
334}
335
5fad7241
DL
336static const char *cmdid2name(u16 cmdid)
337{
338 switch (cmdid) {
339 case WMI_NOTIFY_REQ_CMDID:
340 return "WMI_NOTIFY_REQ_CMD";
341 case WMI_START_SCAN_CMDID:
342 return "WMI_START_SCAN_CMD";
343 case WMI_CONNECT_CMDID:
344 return "WMI_CONNECT_CMD";
345 case WMI_DISCONNECT_CMDID:
346 return "WMI_DISCONNECT_CMD";
347 case WMI_SW_TX_REQ_CMDID:
348 return "WMI_SW_TX_REQ_CMD";
349 case WMI_GET_RF_SECTOR_PARAMS_CMDID:
350 return "WMI_GET_RF_SECTOR_PARAMS_CMD";
351 case WMI_SET_RF_SECTOR_PARAMS_CMDID:
352 return "WMI_SET_RF_SECTOR_PARAMS_CMD";
353 case WMI_GET_SELECTED_RF_SECTOR_INDEX_CMDID:
354 return "WMI_GET_SELECTED_RF_SECTOR_INDEX_CMD";
355 case WMI_SET_SELECTED_RF_SECTOR_INDEX_CMDID:
356 return "WMI_SET_SELECTED_RF_SECTOR_INDEX_CMD";
357 case WMI_BRP_SET_ANT_LIMIT_CMDID:
358 return "WMI_BRP_SET_ANT_LIMIT_CMD";
359 case WMI_TOF_SESSION_START_CMDID:
360 return "WMI_TOF_SESSION_START_CMD";
361 case WMI_AOA_MEAS_CMDID:
362 return "WMI_AOA_MEAS_CMD";
363 case WMI_PMC_CMDID:
364 return "WMI_PMC_CMD";
365 case WMI_TOF_GET_TX_RX_OFFSET_CMDID:
366 return "WMI_TOF_GET_TX_RX_OFFSET_CMD";
367 case WMI_TOF_SET_TX_RX_OFFSET_CMDID:
368 return "WMI_TOF_SET_TX_RX_OFFSET_CMD";
369 case WMI_VRING_CFG_CMDID:
370 return "WMI_VRING_CFG_CMD";
371 case WMI_BCAST_VRING_CFG_CMDID:
372 return "WMI_BCAST_VRING_CFG_CMD";
373 case WMI_TRAFFIC_SUSPEND_CMDID:
374 return "WMI_TRAFFIC_SUSPEND_CMD";
375 case WMI_TRAFFIC_RESUME_CMDID:
376 return "WMI_TRAFFIC_RESUME_CMD";
377 case WMI_ECHO_CMDID:
378 return "WMI_ECHO_CMD";
379 case WMI_SET_MAC_ADDRESS_CMDID:
380 return "WMI_SET_MAC_ADDRESS_CMD";
381 case WMI_LED_CFG_CMDID:
382 return "WMI_LED_CFG_CMD";
383 case WMI_PCP_START_CMDID:
384 return "WMI_PCP_START_CMD";
385 case WMI_PCP_STOP_CMDID:
386 return "WMI_PCP_STOP_CMD";
387 case WMI_SET_SSID_CMDID:
388 return "WMI_SET_SSID_CMD";
389 case WMI_GET_SSID_CMDID:
390 return "WMI_GET_SSID_CMD";
391 case WMI_SET_PCP_CHANNEL_CMDID:
392 return "WMI_SET_PCP_CHANNEL_CMD";
393 case WMI_GET_PCP_CHANNEL_CMDID:
394 return "WMI_GET_PCP_CHANNEL_CMD";
395 case WMI_P2P_CFG_CMDID:
396 return "WMI_P2P_CFG_CMD";
3ada9314
LD
397 case WMI_PORT_ALLOCATE_CMDID:
398 return "WMI_PORT_ALLOCATE_CMD";
399 case WMI_PORT_DELETE_CMDID:
400 return "WMI_PORT_DELETE_CMD";
5fad7241
DL
401 case WMI_START_LISTEN_CMDID:
402 return "WMI_START_LISTEN_CMD";
403 case WMI_START_SEARCH_CMDID:
404 return "WMI_START_SEARCH_CMD";
405 case WMI_DISCOVERY_STOP_CMDID:
406 return "WMI_DISCOVERY_STOP_CMD";
407 case WMI_DELETE_CIPHER_KEY_CMDID:
408 return "WMI_DELETE_CIPHER_KEY_CMD";
409 case WMI_ADD_CIPHER_KEY_CMDID:
410 return "WMI_ADD_CIPHER_KEY_CMD";
411 case WMI_SET_APPIE_CMDID:
412 return "WMI_SET_APPIE_CMD";
413 case WMI_CFG_RX_CHAIN_CMDID:
414 return "WMI_CFG_RX_CHAIN_CMD";
415 case WMI_TEMP_SENSE_CMDID:
416 return "WMI_TEMP_SENSE_CMD";
417 case WMI_DEL_STA_CMDID:
418 return "WMI_DEL_STA_CMD";
419 case WMI_DISCONNECT_STA_CMDID:
420 return "WMI_DISCONNECT_STA_CMD";
96c93589
GS
421 case WMI_RING_BA_EN_CMDID:
422 return "WMI_RING_BA_EN_CMD";
423 case WMI_RING_BA_DIS_CMDID:
424 return "WMI_RING_BA_DIS_CMD";
5fad7241
DL
425 case WMI_RCP_DELBA_CMDID:
426 return "WMI_RCP_DELBA_CMD";
427 case WMI_RCP_ADDBA_RESP_CMDID:
428 return "WMI_RCP_ADDBA_RESP_CMD";
7be13fc3
GS
429 case WMI_RCP_ADDBA_RESP_EDMA_CMDID:
430 return "WMI_RCP_ADDBA_RESP_EDMA_CMD";
5fad7241
DL
431 case WMI_PS_DEV_PROFILE_CFG_CMDID:
432 return "WMI_PS_DEV_PROFILE_CFG_CMD";
433 case WMI_SET_MGMT_RETRY_LIMIT_CMDID:
434 return "WMI_SET_MGMT_RETRY_LIMIT_CMD";
435 case WMI_GET_MGMT_RETRY_LIMIT_CMDID:
436 return "WMI_GET_MGMT_RETRY_LIMIT_CMD";
437 case WMI_ABORT_SCAN_CMDID:
438 return "WMI_ABORT_SCAN_CMD";
439 case WMI_NEW_STA_CMDID:
440 return "WMI_NEW_STA_CMD";
441 case WMI_SET_THERMAL_THROTTLING_CFG_CMDID:
442 return "WMI_SET_THERMAL_THROTTLING_CFG_CMD";
443 case WMI_GET_THERMAL_THROTTLING_CFG_CMDID:
444 return "WMI_GET_THERMAL_THROTTLING_CFG_CMD";
445 case WMI_LINK_MAINTAIN_CFG_WRITE_CMDID:
446 return "WMI_LINK_MAINTAIN_CFG_WRITE_CMD";
447 case WMI_LO_POWER_CALIB_FROM_OTP_CMDID:
448 return "WMI_LO_POWER_CALIB_FROM_OTP_CMD";
a5dc6883
DL
449 case WMI_START_SCHED_SCAN_CMDID:
450 return "WMI_START_SCHED_SCAN_CMD";
451 case WMI_STOP_SCHED_SCAN_CMDID:
452 return "WMI_STOP_SCHED_SCAN_CMD";
96c93589
GS
453 case WMI_TX_STATUS_RING_ADD_CMDID:
454 return "WMI_TX_STATUS_RING_ADD_CMD";
455 case WMI_RX_STATUS_RING_ADD_CMDID:
456 return "WMI_RX_STATUS_RING_ADD_CMD";
457 case WMI_TX_DESC_RING_ADD_CMDID:
458 return "WMI_TX_DESC_RING_ADD_CMD";
459 case WMI_RX_DESC_RING_ADD_CMDID:
460 return "WMI_RX_DESC_RING_ADD_CMD";
461 case WMI_BCAST_DESC_RING_ADD_CMDID:
462 return "WMI_BCAST_DESC_RING_ADD_CMD";
463 case WMI_CFG_DEF_RX_OFFLOAD_CMDID:
464 return "WMI_CFG_DEF_RX_OFFLOAD_CMD";
0c936b3c
DL
465 case WMI_LINK_STATS_CMDID:
466 return "WMI_LINK_STATS_CMD";
b698e2df
AM
467 case WMI_SW_TX_REQ_EXT_CMDID:
468 return "WMI_SW_TX_REQ_EXT_CMDID";
b9010f10
AM
469 case WMI_FT_AUTH_CMDID:
470 return "WMI_FT_AUTH_CMD";
471 case WMI_FT_REASSOC_CMDID:
472 return "WMI_FT_REASSOC_CMD";
473 case WMI_UPDATE_FT_IES_CMDID:
474 return "WMI_UPDATE_FT_IES_CMD";
c5b3a658
AAL
475 case WMI_RBUFCAP_CFG_CMDID:
476 return "WMI_RBUFCAP_CFG_CMD";
9b586118
TS
477 case WMI_TEMP_SENSE_ALL_CMDID:
478 return "WMI_TEMP_SENSE_ALL_CMDID";
5fad7241
DL
479 default:
480 return "Untracked CMD";
481 }
482}
483
484static const char *eventid2name(u16 eventid)
485{
486 switch (eventid) {
487 case WMI_NOTIFY_REQ_DONE_EVENTID:
488 return "WMI_NOTIFY_REQ_DONE_EVENT";
489 case WMI_DISCONNECT_EVENTID:
490 return "WMI_DISCONNECT_EVENT";
491 case WMI_SW_TX_COMPLETE_EVENTID:
492 return "WMI_SW_TX_COMPLETE_EVENT";
493 case WMI_GET_RF_SECTOR_PARAMS_DONE_EVENTID:
494 return "WMI_GET_RF_SECTOR_PARAMS_DONE_EVENT";
495 case WMI_SET_RF_SECTOR_PARAMS_DONE_EVENTID:
496 return "WMI_SET_RF_SECTOR_PARAMS_DONE_EVENT";
497 case WMI_GET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID:
498 return "WMI_GET_SELECTED_RF_SECTOR_INDEX_DONE_EVENT";
499 case WMI_SET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID:
500 return "WMI_SET_SELECTED_RF_SECTOR_INDEX_DONE_EVENT";
501 case WMI_BRP_SET_ANT_LIMIT_EVENTID:
502 return "WMI_BRP_SET_ANT_LIMIT_EVENT";
503 case WMI_FW_READY_EVENTID:
504 return "WMI_FW_READY_EVENT";
505 case WMI_TRAFFIC_RESUME_EVENTID:
506 return "WMI_TRAFFIC_RESUME_EVENT";
507 case WMI_TOF_GET_TX_RX_OFFSET_EVENTID:
508 return "WMI_TOF_GET_TX_RX_OFFSET_EVENT";
509 case WMI_TOF_SET_TX_RX_OFFSET_EVENTID:
510 return "WMI_TOF_SET_TX_RX_OFFSET_EVENT";
511 case WMI_VRING_CFG_DONE_EVENTID:
512 return "WMI_VRING_CFG_DONE_EVENT";
513 case WMI_READY_EVENTID:
514 return "WMI_READY_EVENT";
515 case WMI_RX_MGMT_PACKET_EVENTID:
516 return "WMI_RX_MGMT_PACKET_EVENT";
517 case WMI_TX_MGMT_PACKET_EVENTID:
518 return "WMI_TX_MGMT_PACKET_EVENT";
519 case WMI_SCAN_COMPLETE_EVENTID:
520 return "WMI_SCAN_COMPLETE_EVENT";
521 case WMI_ACS_PASSIVE_SCAN_COMPLETE_EVENTID:
522 return "WMI_ACS_PASSIVE_SCAN_COMPLETE_EVENT";
523 case WMI_CONNECT_EVENTID:
524 return "WMI_CONNECT_EVENT";
525 case WMI_EAPOL_RX_EVENTID:
526 return "WMI_EAPOL_RX_EVENT";
527 case WMI_BA_STATUS_EVENTID:
528 return "WMI_BA_STATUS_EVENT";
529 case WMI_RCP_ADDBA_REQ_EVENTID:
530 return "WMI_RCP_ADDBA_REQ_EVENT";
531 case WMI_DELBA_EVENTID:
532 return "WMI_DELBA_EVENT";
96c93589
GS
533 case WMI_RING_EN_EVENTID:
534 return "WMI_RING_EN_EVENT";
5fad7241
DL
535 case WMI_DATA_PORT_OPEN_EVENTID:
536 return "WMI_DATA_PORT_OPEN_EVENT";
537 case WMI_AOA_MEAS_EVENTID:
538 return "WMI_AOA_MEAS_EVENT";
539 case WMI_TOF_SESSION_END_EVENTID:
540 return "WMI_TOF_SESSION_END_EVENT";
541 case WMI_TOF_GET_CAPABILITIES_EVENTID:
542 return "WMI_TOF_GET_CAPABILITIES_EVENT";
543 case WMI_TOF_SET_LCR_EVENTID:
544 return "WMI_TOF_SET_LCR_EVENT";
545 case WMI_TOF_SET_LCI_EVENTID:
546 return "WMI_TOF_SET_LCI_EVENT";
547 case WMI_TOF_FTM_PER_DEST_RES_EVENTID:
548 return "WMI_TOF_FTM_PER_DEST_RES_EVENT";
549 case WMI_TOF_CHANNEL_INFO_EVENTID:
550 return "WMI_TOF_CHANNEL_INFO_EVENT";
551 case WMI_TRAFFIC_SUSPEND_EVENTID:
552 return "WMI_TRAFFIC_SUSPEND_EVENT";
553 case WMI_ECHO_RSP_EVENTID:
554 return "WMI_ECHO_RSP_EVENT";
555 case WMI_LED_CFG_DONE_EVENTID:
556 return "WMI_LED_CFG_DONE_EVENT";
557 case WMI_PCP_STARTED_EVENTID:
558 return "WMI_PCP_STARTED_EVENT";
559 case WMI_PCP_STOPPED_EVENTID:
560 return "WMI_PCP_STOPPED_EVENT";
561 case WMI_GET_SSID_EVENTID:
562 return "WMI_GET_SSID_EVENT";
563 case WMI_GET_PCP_CHANNEL_EVENTID:
564 return "WMI_GET_PCP_CHANNEL_EVENT";
565 case WMI_P2P_CFG_DONE_EVENTID:
566 return "WMI_P2P_CFG_DONE_EVENT";
3ada9314
LD
567 case WMI_PORT_ALLOCATED_EVENTID:
568 return "WMI_PORT_ALLOCATED_EVENT";
569 case WMI_PORT_DELETED_EVENTID:
570 return "WMI_PORT_DELETED_EVENT";
5fad7241
DL
571 case WMI_LISTEN_STARTED_EVENTID:
572 return "WMI_LISTEN_STARTED_EVENT";
573 case WMI_SEARCH_STARTED_EVENTID:
574 return "WMI_SEARCH_STARTED_EVENT";
575 case WMI_DISCOVERY_STOPPED_EVENTID:
576 return "WMI_DISCOVERY_STOPPED_EVENT";
577 case WMI_CFG_RX_CHAIN_DONE_EVENTID:
578 return "WMI_CFG_RX_CHAIN_DONE_EVENT";
579 case WMI_TEMP_SENSE_DONE_EVENTID:
580 return "WMI_TEMP_SENSE_DONE_EVENT";
581 case WMI_RCP_ADDBA_RESP_SENT_EVENTID:
582 return "WMI_RCP_ADDBA_RESP_SENT_EVENT";
583 case WMI_PS_DEV_PROFILE_CFG_EVENTID:
584 return "WMI_PS_DEV_PROFILE_CFG_EVENT";
585 case WMI_SET_MGMT_RETRY_LIMIT_EVENTID:
586 return "WMI_SET_MGMT_RETRY_LIMIT_EVENT";
587 case WMI_GET_MGMT_RETRY_LIMIT_EVENTID:
588 return "WMI_GET_MGMT_RETRY_LIMIT_EVENT";
589 case WMI_SET_THERMAL_THROTTLING_CFG_EVENTID:
590 return "WMI_SET_THERMAL_THROTTLING_CFG_EVENT";
591 case WMI_GET_THERMAL_THROTTLING_CFG_EVENTID:
592 return "WMI_GET_THERMAL_THROTTLING_CFG_EVENT";
593 case WMI_LINK_MAINTAIN_CFG_WRITE_DONE_EVENTID:
594 return "WMI_LINK_MAINTAIN_CFG_WRITE_DONE_EVENT";
595 case WMI_LO_POWER_CALIB_FROM_OTP_EVENTID:
596 return "WMI_LO_POWER_CALIB_FROM_OTP_EVENT";
a5dc6883
DL
597 case WMI_START_SCHED_SCAN_EVENTID:
598 return "WMI_START_SCHED_SCAN_EVENT";
599 case WMI_STOP_SCHED_SCAN_EVENTID:
600 return "WMI_STOP_SCHED_SCAN_EVENT";
601 case WMI_SCHED_SCAN_RESULT_EVENTID:
602 return "WMI_SCHED_SCAN_RESULT_EVENT";
96c93589
GS
603 case WMI_TX_STATUS_RING_CFG_DONE_EVENTID:
604 return "WMI_TX_STATUS_RING_CFG_DONE_EVENT";
605 case WMI_RX_STATUS_RING_CFG_DONE_EVENTID:
606 return "WMI_RX_STATUS_RING_CFG_DONE_EVENT";
607 case WMI_TX_DESC_RING_CFG_DONE_EVENTID:
608 return "WMI_TX_DESC_RING_CFG_DONE_EVENT";
609 case WMI_RX_DESC_RING_CFG_DONE_EVENTID:
610 return "WMI_RX_DESC_RING_CFG_DONE_EVENT";
611 case WMI_CFG_DEF_RX_OFFLOAD_DONE_EVENTID:
612 return "WMI_CFG_DEF_RX_OFFLOAD_DONE_EVENT";
0c936b3c
DL
613 case WMI_LINK_STATS_CONFIG_DONE_EVENTID:
614 return "WMI_LINK_STATS_CONFIG_DONE_EVENT";
615 case WMI_LINK_STATS_EVENTID:
616 return "WMI_LINK_STATS_EVENT";
b9010f10
AM
617 case WMI_COMMAND_NOT_SUPPORTED_EVENTID:
618 return "WMI_COMMAND_NOT_SUPPORTED_EVENT";
619 case WMI_FT_AUTH_STATUS_EVENTID:
620 return "WMI_FT_AUTH_STATUS_EVENT";
621 case WMI_FT_REASSOC_STATUS_EVENTID:
622 return "WMI_FT_REASSOC_STATUS_EVENT";
c5b3a658
AAL
623 case WMI_RBUFCAP_CFG_EVENTID:
624 return "WMI_RBUFCAP_CFG_EVENT";
9b586118
TS
625 case WMI_TEMP_SENSE_ALL_DONE_EVENTID:
626 return "WMI_TEMP_SENSE_ALL_DONE_EVENTID";
5fad7241
DL
627 default:
628 return "Untracked EVENT";
629 }
630}
631
e00243fa
LD
632static int __wmi_send(struct wil6210_priv *wil, u16 cmdid, u8 mid,
633 void *buf, u16 len)
2be7d22f
VK
634{
635 struct {
636 struct wil6210_mbox_hdr hdr;
b874ddec 637 struct wmi_cmd_hdr wmi;
2be7d22f
VK
638 } __packed cmd = {
639 .hdr = {
640 .type = WIL_MBOX_HDR_TYPE_WMI,
641 .flags = 0,
642 .len = cpu_to_le16(sizeof(cmd.wmi) + len),
643 },
644 .wmi = {
e00243fa 645 .mid = mid,
b874ddec 646 .command_id = cpu_to_le16(cmdid),
2be7d22f
VK
647 },
648 };
649 struct wil6210_mbox_ring *r = &wil->mbox_ctl.tx;
650 struct wil6210_mbox_ring_desc d_head;
651 u32 next_head;
652 void __iomem *dst;
653 void __iomem *head = wmi_addr(wil, r->head);
654 uint retry;
349214c1 655 int rc = 0;
2be7d22f 656
26a6d527 657 if (len > r->entry_size - sizeof(cmd)) {
2be7d22f
VK
658 wil_err(wil, "WMI size too large: %d bytes, max is %d\n",
659 (int)(sizeof(cmd) + len), r->entry_size);
660 return -ERANGE;
2be7d22f
VK
661 }
662
663 might_sleep();
664
9419b6a2 665 if (!test_bit(wil_status_fwready, wil->status)) {
15e23124 666 wil_err(wil, "WMI: cannot send command while FW not ready\n");
2be7d22f
VK
667 return -EAGAIN;
668 }
669
fe9ee51e
ME
670 /* Allow sending only suspend / resume commands during susepnd flow */
671 if ((test_bit(wil_status_suspending, wil->status) ||
672 test_bit(wil_status_suspended, wil->status) ||
673 test_bit(wil_status_resuming, wil->status)) &&
674 ((cmdid != WMI_TRAFFIC_SUSPEND_CMDID) &&
675 (cmdid != WMI_TRAFFIC_RESUME_CMDID))) {
676 wil_err(wil, "WMI: reject send_command during suspend\n");
677 return -EINVAL;
678 }
679
2be7d22f
VK
680 if (!head) {
681 wil_err(wil, "WMI head is garbage: 0x%08x\n", r->head);
682 return -EINVAL;
683 }
349214c1
ME
684
685 wil_halp_vote(wil);
686
2be7d22f
VK
687 /* read Tx head till it is not busy */
688 for (retry = 5; retry > 0; retry--) {
689 wil_memcpy_fromio_32(&d_head, head, sizeof(d_head));
690 if (d_head.sync == 0)
691 break;
692 msleep(20);
693 }
694 if (d_head.sync != 0) {
695 wil_err(wil, "WMI head busy\n");
349214c1
ME
696 rc = -EBUSY;
697 goto out;
2be7d22f
VK
698 }
699 /* next head */
700 next_head = r->base + ((r->head - r->base + sizeof(d_head)) % r->size);
7743882d 701 wil_dbg_wmi(wil, "Head 0x%08x -> 0x%08x\n", r->head, next_head);
2be7d22f
VK
702 /* wait till FW finish with previous command */
703 for (retry = 5; retry > 0; retry--) {
452133a7
ME
704 if (!test_bit(wil_status_fwready, wil->status)) {
705 wil_err(wil, "WMI: cannot send command while FW not ready\n");
349214c1
ME
706 rc = -EAGAIN;
707 goto out;
452133a7 708 }
b9eeb512
VK
709 r->tail = wil_r(wil, RGF_MBOX +
710 offsetof(struct wil6210_mbox_ctl, tx.tail));
2be7d22f
VK
711 if (next_head != r->tail)
712 break;
713 msleep(20);
714 }
715 if (next_head == r->tail) {
716 wil_err(wil, "WMI ring full\n");
349214c1
ME
717 rc = -EBUSY;
718 goto out;
2be7d22f
VK
719 }
720 dst = wmi_buffer(wil, d_head.addr);
721 if (!dst) {
722 wil_err(wil, "invalid WMI buffer: 0x%08x\n",
723 le32_to_cpu(d_head.addr));
349214c1
ME
724 rc = -EAGAIN;
725 goto out;
2be7d22f
VK
726 }
727 cmd.hdr.seq = cpu_to_le16(++wil->wmi_seq);
728 /* set command */
e00243fa
LD
729 wil_dbg_wmi(wil, "sending %s (0x%04x) [%d] mid %d\n",
730 cmdid2name(cmdid), cmdid, len, mid);
7743882d 731 wil_hex_dump_wmi("Cmd ", DUMP_PREFIX_OFFSET, 16, 1, &cmd,
2be7d22f 732 sizeof(cmd), true);
7743882d 733 wil_hex_dump_wmi("cmd ", DUMP_PREFIX_OFFSET, 16, 1, buf,
2be7d22f
VK
734 len, true);
735 wil_memcpy_toio_32(dst, &cmd, sizeof(cmd));
736 wil_memcpy_toio_32(dst + sizeof(cmd), buf, len);
737 /* mark entry as full */
b9eeb512 738 wil_w(wil, r->head + offsetof(struct wil6210_mbox_ring_desc, sync), 1);
2be7d22f 739 /* advance next ptr */
b9eeb512
VK
740 wil_w(wil, RGF_MBOX + offsetof(struct wil6210_mbox_ctl, tx.head),
741 r->head = next_head);
2be7d22f 742
f988b23f 743 trace_wil6210_wmi_cmd(&cmd.wmi, buf, len);
98658095 744
2be7d22f 745 /* interrupt to FW */
b9eeb512
VK
746 wil_w(wil, RGF_USER_USER_ICR + offsetof(struct RGF_ICR, ICS),
747 SW_INT_MBOX);
2be7d22f 748
349214c1
ME
749out:
750 wil_halp_unvote(wil);
751 return rc;
2be7d22f
VK
752}
753
e00243fa 754int wmi_send(struct wil6210_priv *wil, u16 cmdid, u8 mid, void *buf, u16 len)
2be7d22f
VK
755{
756 int rc;
757
758 mutex_lock(&wil->wmi_mutex);
e00243fa 759 rc = __wmi_send(wil, cmdid, mid, buf, len);
2be7d22f
VK
760 mutex_unlock(&wil->wmi_mutex);
761
762 return rc;
763}
764
765/*=== Event handlers ===*/
e00243fa 766static void wmi_evt_ready(struct wil6210_vif *vif, int id, void *d, int len)
2be7d22f 767{
e00243fa
LD
768 struct wil6210_priv *wil = vif_to_wil(vif);
769 struct wiphy *wiphy = wil_to_wiphy(wil);
2be7d22f 770 struct wmi_ready_event *evt = d;
bf0353a6 771 u8 fw_max_assoc_sta;
8fe59627 772
13cd9f75
LD
773 wil_info(wil, "FW ver. %s(SW %d); MAC %pM; %d MID's\n",
774 wil->fw_version, le32_to_cpu(evt->sw_version),
4aebd3bd
LD
775 evt->mac, evt->numof_additional_mids);
776 if (evt->numof_additional_mids + 1 < wil->max_vifs) {
777 wil_err(wil, "FW does not support enough MIDs (need %d)",
778 wil->max_vifs - 1);
779 return; /* FW load will fail after timeout */
780 }
2cd0f021 781 /* ignore MAC address, we already have it from the boot loader */
e00243fa 782 strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
2be7d22f 783
eb4c0215
DL
784 if (len > offsetof(struct wmi_ready_event, rfc_read_calib_result)) {
785 wil_dbg_wmi(wil, "rfc calibration result %d\n",
786 evt->rfc_read_calib_result);
787 wil->fw_calib_result = evt->rfc_read_calib_result;
788 }
bf0353a6
AM
789
790 fw_max_assoc_sta = WIL6210_RX_DESC_MAX_CID;
791 if (len > offsetof(struct wmi_ready_event, max_assoc_sta) &&
792 evt->max_assoc_sta > 0) {
793 fw_max_assoc_sta = evt->max_assoc_sta;
794 wil_dbg_wmi(wil, "fw reported max assoc sta %d\n",
795 fw_max_assoc_sta);
796
797 if (fw_max_assoc_sta > WIL6210_MAX_CID) {
798 wil_dbg_wmi(wil,
799 "fw max assoc sta %d exceeds max driver supported %d\n",
800 fw_max_assoc_sta, WIL6210_MAX_CID);
801 fw_max_assoc_sta = WIL6210_MAX_CID;
802 }
803 }
804
ddf7afdd
AM
805 wil->max_assoc_sta = min_t(uint, max_assoc_sta, fw_max_assoc_sta);
806 wil_dbg_wmi(wil, "setting max assoc sta to %d\n", wil->max_assoc_sta);
bf0353a6 807
c33407a8 808 wil_set_recovery_state(wil, fw_recovery_idle);
9419b6a2 809 set_bit(wil_status_fwready, wil->status);
59502647 810 /* let the reset sequence continue */
2be7d22f
VK
811 complete(&wil->wmi_ready);
812}
813
e00243fa 814static void wmi_evt_rx_mgmt(struct wil6210_vif *vif, int id, void *d, int len)
2be7d22f 815{
e00243fa 816 struct wil6210_priv *wil = vif_to_wil(vif);
2be7d22f
VK
817 struct wmi_rx_mgmt_packet_event *data = d;
818 struct wiphy *wiphy = wil_to_wiphy(wil);
819 struct ieee80211_mgmt *rx_mgmt_frame =
820 (struct ieee80211_mgmt *)data->payload;
90d89e9a
VK
821 int flen = len - offsetof(struct wmi_rx_mgmt_packet_event, payload);
822 int ch_no;
823 u32 freq;
824 struct ieee80211_channel *channel;
825 s32 signal;
826 __le16 fc;
827 u32 d_len;
828 u16 d_status;
829
830 if (flen < 0) {
831 wil_err(wil, "MGMT Rx: short event, len %d\n", len);
832 return;
833 }
834
835 d_len = le32_to_cpu(data->info.len);
836 if (d_len != flen) {
837 wil_err(wil,
838 "MGMT Rx: length mismatch, d_len %d should be %d\n",
839 d_len, flen);
840 return;
841 }
842
843 ch_no = data->info.channel + 1;
57fbcce3 844 freq = ieee80211_channel_to_frequency(ch_no, NL80211_BAND_60GHZ);
90d89e9a 845 channel = ieee80211_get_channel(wiphy, freq);
30868f5d
DL
846 if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
847 signal = 100 * data->info.rssi;
848 else
849 signal = data->info.sqi;
90d89e9a
VK
850 d_status = le16_to_cpu(data->info.status);
851 fc = rx_mgmt_frame->frame_control;
852
30868f5d
DL
853 wil_dbg_wmi(wil, "MGMT Rx: channel %d MCS %d RSSI %d SQI %d%%\n",
854 data->info.channel, data->info.mcs, data->info.rssi,
b8b33a3a 855 data->info.sqi);
f27dbf78
VK
856 wil_dbg_wmi(wil, "status 0x%04x len %d fc 0x%04x\n", d_status, d_len,
857 le16_to_cpu(fc));
7743882d 858 wil_dbg_wmi(wil, "qid %d mid %d cid %d\n",
2be7d22f 859 data->info.qid, data->info.mid, data->info.cid);
90d89e9a
VK
860 wil_hex_dump_wmi("MGMT Rx ", DUMP_PREFIX_OFFSET, 16, 1, rx_mgmt_frame,
861 d_len, true);
2be7d22f
VK
862
863 if (!channel) {
864 wil_err(wil, "Frame on unsupported channel\n");
865 return;
866 }
867
868 if (ieee80211_is_beacon(fc) || ieee80211_is_probe_resp(fc)) {
869 struct cfg80211_bss *bss;
058b3f11
ME
870 struct cfg80211_inform_bss bss_data = {
871 .chan = channel,
872 .scan_width = NL80211_BSS_CHAN_WIDTH_20,
873 .signal = signal,
874 .boottime_ns = ktime_to_ns(ktime_get_boottime()),
875 };
8eea944a
VK
876 u64 tsf = le64_to_cpu(rx_mgmt_frame->u.beacon.timestamp);
877 u16 cap = le16_to_cpu(rx_mgmt_frame->u.beacon.capab_info);
878 u16 bi = le16_to_cpu(rx_mgmt_frame->u.beacon.beacon_int);
879 const u8 *ie_buf = rx_mgmt_frame->u.beacon.variable;
880 size_t ie_len = d_len - offsetof(struct ieee80211_mgmt,
881 u.beacon.variable);
882 wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
883 wil_dbg_wmi(wil, "TSF : 0x%016llx\n", tsf);
884 wil_dbg_wmi(wil, "Beacon interval : %d\n", bi);
885 wil_hex_dump_wmi("IE ", DUMP_PREFIX_OFFSET, 16, 1, ie_buf,
886 ie_len, true);
a0f7845b 887
e6d68341
DL
888 wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
889
058b3f11
ME
890 bss = cfg80211_inform_bss_frame_data(wiphy, &bss_data,
891 rx_mgmt_frame,
892 d_len, GFP_KERNEL);
2be7d22f 893 if (bss) {
7743882d 894 wil_dbg_wmi(wil, "Added BSS %pM\n",
2be7d22f 895 rx_mgmt_frame->bssid);
5b112d3d 896 cfg80211_put_bss(wiphy, bss);
2be7d22f 897 } else {
5bc8c1f2 898 wil_err(wil, "cfg80211_inform_bss_frame() failed\n");
2be7d22f 899 }
102b1d99 900 } else {
404bbb3c 901 mutex_lock(&wil->vif_mutex);
e00243fa 902 cfg80211_rx_mgmt(vif_to_radio_wdev(wil, vif), freq, signal,
970fdfa8 903 (void *)rx_mgmt_frame, d_len, 0);
404bbb3c 904 mutex_unlock(&wil->vif_mutex);
2be7d22f
VK
905 }
906}
907
e00243fa 908static void wmi_evt_tx_mgmt(struct wil6210_vif *vif, int id, void *d, int len)
90d89e9a
VK
909{
910 struct wmi_tx_mgmt_packet_event *data = d;
911 struct ieee80211_mgmt *mgmt_frame =
912 (struct ieee80211_mgmt *)data->payload;
913 int flen = len - offsetof(struct wmi_tx_mgmt_packet_event, payload);
914
915 wil_hex_dump_wmi("MGMT Tx ", DUMP_PREFIX_OFFSET, 16, 1, mgmt_frame,
916 flen, true);
917}
918
e00243fa 919static void wmi_evt_scan_complete(struct wil6210_vif *vif, int id,
2be7d22f
VK
920 void *d, int len)
921{
e00243fa
LD
922 struct wil6210_priv *wil = vif_to_wil(vif);
923
404bbb3c 924 mutex_lock(&wil->vif_mutex);
e00243fa 925 if (vif->scan_request) {
2be7d22f 926 struct wmi_scan_complete_event *data = d;
035859a5 927 int status = le32_to_cpu(data->status);
1d76250b 928 struct cfg80211_scan_info info = {
035859a5
ME
929 .aborted = ((status != WMI_SCAN_SUCCESS) &&
930 (status != WMI_SCAN_ABORT_REJECTED)),
1d76250b 931 };
2be7d22f 932
035859a5 933 wil_dbg_wmi(wil, "SCAN_COMPLETE(0x%08x)\n", status);
2a91d7d0 934 wil_dbg_misc(wil, "Complete scan_request 0x%p aborted %d\n",
e00243fa
LD
935 vif->scan_request, info.aborted);
936 del_timer_sync(&vif->scan_timer);
937 cfg80211_scan_done(vif->scan_request, &info);
938 if (vif->mid == 0)
939 wil->radio_wdev = wil->main_ndev->ieee80211_ptr;
940 vif->scan_request = NULL;
035859a5 941 wake_up_interruptible(&wil->wq);
e00243fa 942 if (vif->p2p.pending_listen_wdev) {
bb6743f7 943 wil_dbg_misc(wil, "Scheduling delayed listen\n");
e00243fa 944 schedule_work(&vif->p2p.delayed_listen_work);
bb6743f7 945 }
2be7d22f
VK
946 } else {
947 wil_err(wil, "SCAN_COMPLETE while not scanning\n");
948 }
404bbb3c 949 mutex_unlock(&wil->vif_mutex);
2be7d22f
VK
950}
951
e00243fa 952static void wmi_evt_connect(struct wil6210_vif *vif, int id, void *d, int len)
2be7d22f 953{
e00243fa
LD
954 struct wil6210_priv *wil = vif_to_wil(vif);
955 struct net_device *ndev = vif_to_ndev(vif);
956 struct wireless_dev *wdev = vif_to_wdev(vif);
2be7d22f
VK
957 struct wmi_connect_event *evt = d;
958 int ch; /* channel number */
41bd3d58 959 struct station_info *sinfo;
2be7d22f
VK
960 u8 *assoc_req_ie, *assoc_resp_ie;
961 size_t assoc_req_ielen, assoc_resp_ielen;
962 /* capinfo(u16) + listen_interval(u16) + IEs */
963 const size_t assoc_req_ie_offset = sizeof(u16) * 2;
964 /* capinfo(u16) + status_code(u16) + associd(u16) + IEs */
965 const size_t assoc_resp_ie_offset = sizeof(u16) * 3;
0916d9f2 966 int rc;
2be7d22f
VK
967
968 if (len < sizeof(*evt)) {
969 wil_err(wil, "Connect event too short : %d bytes\n", len);
970 return;
971 }
972 if (len != sizeof(*evt) + evt->beacon_ie_len + evt->assoc_req_len +
973 evt->assoc_resp_len) {
974 wil_err(wil,
975 "Connect event corrupted : %d != %d + %d + %d + %d\n",
976 len, (int)sizeof(*evt), evt->beacon_ie_len,
977 evt->assoc_req_len, evt->assoc_resp_len);
978 return;
979 }
ddf7afdd 980 if (evt->cid >= wil->max_assoc_sta) {
3df2cd36
VK
981 wil_err(wil, "Connect CID invalid : %d\n", evt->cid);
982 return;
983 }
984
2be7d22f 985 ch = evt->channel + 1;
9d865ee2
LD
986 wil_info(wil, "Connect %pM channel [%d] cid %d aid %d\n",
987 evt->bssid, ch, evt->cid, evt->aid);
7743882d 988 wil_hex_dump_wmi("connect AI : ", DUMP_PREFIX_OFFSET, 16, 1,
2be7d22f
VK
989 evt->assoc_info, len - sizeof(*evt), true);
990
991 /* figure out IE's */
992 assoc_req_ie = &evt->assoc_info[evt->beacon_ie_len +
993 assoc_req_ie_offset];
994 assoc_req_ielen = evt->assoc_req_len - assoc_req_ie_offset;
995 if (evt->assoc_req_len <= assoc_req_ie_offset) {
996 assoc_req_ie = NULL;
997 assoc_req_ielen = 0;
998 }
999
1000 assoc_resp_ie = &evt->assoc_info[evt->beacon_ie_len +
1001 evt->assoc_req_len +
1002 assoc_resp_ie_offset];
1003 assoc_resp_ielen = evt->assoc_resp_len - assoc_resp_ie_offset;
1004 if (evt->assoc_resp_len <= assoc_resp_ie_offset) {
1005 assoc_resp_ie = NULL;
1006 assoc_resp_ielen = 0;
1007 }
1008
0916d9f2
ME
1009 if (test_bit(wil_status_resetting, wil->status) ||
1010 !test_bit(wil_status_fwready, wil->status)) {
1011 wil_err(wil, "status_resetting, cancel connect event, CID %d\n",
1012 evt->cid);
0916d9f2
ME
1013 /* no need for cleanup, wil_reset will do that */
1014 return;
1015 }
1016
b819447d
HK
1017 mutex_lock(&wil->mutex);
1018
2be7d22f
VK
1019 if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
1020 (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
5bd60982 1021 if (!test_bit(wil_vif_fwconnecting, vif->status)) {
2be7d22f 1022 wil_err(wil, "Not in connecting state\n");
0916d9f2 1023 mutex_unlock(&wil->mutex);
2be7d22f
VK
1024 return;
1025 }
e00243fa 1026 del_timer_sync(&vif->connect_timer);
3d287fb3
ME
1027 } else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
1028 (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
1029 if (wil->sta[evt->cid].status != wil_sta_unused) {
af3db60a
LA
1030 wil_err(wil, "AP: Invalid status %d for CID %d\n",
1031 wil->sta[evt->cid].status, evt->cid);
3d287fb3
ME
1032 mutex_unlock(&wil->mutex);
1033 return;
1034 }
0916d9f2
ME
1035 }
1036
0916d9f2 1037 ether_addr_copy(wil->sta[evt->cid].addr, evt->bssid);
e00243fa 1038 wil->sta[evt->cid].mid = vif->mid;
0916d9f2 1039 wil->sta[evt->cid].status = wil_sta_conn_pending;
2be7d22f 1040
96c93589 1041 rc = wil_ring_init_tx(vif, evt->cid);
0916d9f2 1042 if (rc) {
af3db60a
LA
1043 wil_err(wil, "config tx vring failed for CID %d, rc (%d)\n",
1044 evt->cid, rc);
e00243fa 1045 wmi_disconnect_sta(vif, wil->sta[evt->cid].addr,
e1b43407 1046 WLAN_REASON_UNSPECIFIED, false);
0916d9f2 1047 } else {
af3db60a 1048 wil_info(wil, "successful connection to CID %d\n", evt->cid);
0916d9f2
ME
1049 }
1050
1051 if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
1052 (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
1053 if (rc) {
0916d9f2 1054 netif_carrier_off(ndev);
9953a782 1055 wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
af3db60a 1056 wil_err(wil, "cfg80211_connect_result with failure\n");
0916d9f2
ME
1057 cfg80211_connect_result(ndev, evt->bssid, NULL, 0,
1058 NULL, 0,
1059 WLAN_STATUS_UNSPECIFIED_FAILURE,
1060 GFP_KERNEL);
1061 goto out;
1062 } else {
bcdd49b0
DL
1063 struct wiphy *wiphy = wil_to_wiphy(wil);
1064
e00243fa
LD
1065 cfg80211_ref_bss(wiphy, vif->bss);
1066 cfg80211_connect_bss(ndev, evt->bssid, vif->bss,
bcdd49b0
DL
1067 assoc_req_ie, assoc_req_ielen,
1068 assoc_resp_ie, assoc_resp_ielen,
1069 WLAN_STATUS_SUCCESS, GFP_KERNEL,
1070 NL80211_TIMEOUT_UNSPECIFIED);
0916d9f2 1071 }
e00243fa 1072 vif->bss = NULL;
2be7d22f
VK
1073 } else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
1074 (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
41bd3d58 1075
849a564b
DL
1076 if (rc) {
1077 if (disable_ap_sme)
1078 /* notify new_sta has failed */
1079 cfg80211_del_sta(ndev, evt->bssid, GFP_KERNEL);
0916d9f2 1080 goto out;
849a564b 1081 }
0916d9f2 1082
41bd3d58
THJ
1083 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1084 if (!sinfo) {
1085 rc = -ENOMEM;
1086 goto out;
1087 }
2be7d22f 1088
41bd3d58 1089 sinfo->generation = wil->sinfo_gen++;
2be7d22f
VK
1090
1091 if (assoc_req_ie) {
41bd3d58
THJ
1092 sinfo->assoc_req_ies = assoc_req_ie;
1093 sinfo->assoc_req_ies_len = assoc_req_ielen;
2be7d22f
VK
1094 }
1095
41bd3d58
THJ
1096 cfg80211_new_sta(ndev, evt->bssid, sinfo, GFP_KERNEL);
1097
1098 kfree(sinfo);
0916d9f2 1099 } else {
af3db60a
LA
1100 wil_err(wil, "unhandled iftype %d for CID %d\n", wdev->iftype,
1101 evt->cid);
0916d9f2 1102 goto out;
2be7d22f 1103 }
2be7d22f 1104
0916d9f2 1105 wil->sta[evt->cid].status = wil_sta_connected;
9d865ee2 1106 wil->sta[evt->cid].aid = evt->aid;
5bd60982
LD
1107 if (!test_and_set_bit(wil_vif_fwconnected, vif->status))
1108 atomic_inc(&wil->connected_vifs);
1109 wil_update_net_queues_bh(wil, vif, NULL, false);
2be7d22f 1110
0916d9f2 1111out:
e00243fa 1112 if (rc) {
0916d9f2 1113 wil->sta[evt->cid].status = wil_sta_unused;
e00243fa
LD
1114 wil->sta[evt->cid].mid = U8_MAX;
1115 }
5bd60982 1116 clear_bit(wil_vif_fwconnecting, vif->status);
0916d9f2 1117 mutex_unlock(&wil->mutex);
2be7d22f
VK
1118}
1119
e00243fa 1120static void wmi_evt_disconnect(struct wil6210_vif *vif, int id,
2be7d22f
VK
1121 void *d, int len)
1122{
e00243fa 1123 struct wil6210_priv *wil = vif_to_wil(vif);
2be7d22f 1124 struct wmi_disconnect_event *evt = d;
4821e6d8 1125 u16 reason_code = le16_to_cpu(evt->protocol_reason_status);
2be7d22f 1126
0916d9f2
ME
1127 wil_info(wil, "Disconnect %pM reason [proto %d wmi %d]\n",
1128 evt->bssid, reason_code, evt->disconnect_reason);
2be7d22f
VK
1129
1130 wil->sinfo_gen++;
1131
b819447d
HK
1132 if (test_bit(wil_status_resetting, wil->status) ||
1133 !test_bit(wil_status_fwready, wil->status)) {
1134 wil_err(wil, "status_resetting, cancel disconnect event\n");
1135 /* no need for cleanup, wil_reset will do that */
1136 return;
1137 }
1138
097638a0 1139 mutex_lock(&wil->mutex);
e1b43407 1140 wil6210_disconnect_complete(vif, evt->bssid, reason_code);
b571e71b
AM
1141 if (disable_ap_sme) {
1142 struct wireless_dev *wdev = vif_to_wdev(vif);
1143 struct net_device *ndev = vif_to_ndev(vif);
1144
1145 /* disconnect event in disable_ap_sme mode means link loss */
1146 switch (wdev->iftype) {
1147 /* AP-like interface */
1148 case NL80211_IFTYPE_AP:
1149 case NL80211_IFTYPE_P2P_GO:
1150 /* notify hostapd about link loss */
1151 cfg80211_cqm_pktloss_notify(ndev, evt->bssid, 0,
1152 GFP_KERNEL);
1153 break;
1154 default:
1155 break;
1156 }
1157 }
097638a0 1158 mutex_unlock(&wil->mutex);
2be7d22f
VK
1159}
1160
2be7d22f
VK
1161/*
1162 * Firmware reports EAPOL frame using WME event.
1163 * Reconstruct Ethernet frame and deliver it via normal Rx
1164 */
e00243fa 1165static void wmi_evt_eapol_rx(struct wil6210_vif *vif, int id, void *d, int len)
2be7d22f 1166{
e00243fa
LD
1167 struct wil6210_priv *wil = vif_to_wil(vif);
1168 struct net_device *ndev = vif_to_ndev(vif);
2be7d22f
VK
1169 struct wmi_eapol_rx_event *evt = d;
1170 u16 eapol_len = le16_to_cpu(evt->eapol_len);
1171 int sz = eapol_len + ETH_HLEN;
1172 struct sk_buff *skb;
1173 struct ethhdr *eth;
c8b78b5f
VK
1174 int cid;
1175 struct wil_net_stats *stats = NULL;
2be7d22f 1176
e00243fa
LD
1177 wil_dbg_wmi(wil, "EAPOL len %d from %pM MID %d\n", eapol_len,
1178 evt->src_mac, vif->mid);
2be7d22f 1179
e00243fa 1180 cid = wil_find_cid(wil, vif->mid, evt->src_mac);
c8b78b5f
VK
1181 if (cid >= 0)
1182 stats = &wil->sta[cid].stats;
1183
2be7d22f
VK
1184 if (eapol_len > 196) { /* TODO: revisit size limit */
1185 wil_err(wil, "EAPOL too large\n");
1186 return;
1187 }
1188
1189 skb = alloc_skb(sz, GFP_KERNEL);
1190 if (!skb) {
1191 wil_err(wil, "Failed to allocate skb\n");
1192 return;
1193 }
c8b78b5f 1194
4df864c1 1195 eth = skb_put(skb, ETH_HLEN);
a82553bb
VK
1196 ether_addr_copy(eth->h_dest, ndev->dev_addr);
1197 ether_addr_copy(eth->h_source, evt->src_mac);
2be7d22f 1198 eth->h_proto = cpu_to_be16(ETH_P_PAE);
59ae1d12 1199 skb_put_data(skb, evt->eapol, eapol_len);
2be7d22f
VK
1200 skb->protocol = eth_type_trans(skb, ndev);
1201 if (likely(netif_rx_ni(skb) == NET_RX_SUCCESS)) {
1202 ndev->stats.rx_packets++;
c8b78b5f
VK
1203 ndev->stats.rx_bytes += sz;
1204 if (stats) {
1205 stats->rx_packets++;
1206 stats->rx_bytes += sz;
1207 }
2be7d22f
VK
1208 } else {
1209 ndev->stats.rx_dropped++;
c8b78b5f
VK
1210 if (stats)
1211 stats->rx_dropped++;
2be7d22f
VK
1212 }
1213}
1214
96c93589 1215static void wmi_evt_ring_en(struct wil6210_vif *vif, int id, void *d, int len)
3a124ed6 1216{
e00243fa 1217 struct wil6210_priv *wil = vif_to_wil(vif);
96c93589
GS
1218 struct wmi_ring_en_event *evt = d;
1219 u8 vri = evt->ring_index;
e00243fa 1220 struct wireless_dev *wdev = vif_to_wdev(vif);
b9010f10 1221 struct wil_sta_info *sta;
49925f24 1222 u8 cid;
b9010f10 1223 struct key_params params;
3a124ed6 1224
e00243fa 1225 wil_dbg_wmi(wil, "Enable vring %d MID %d\n", vri, vif->mid);
3a124ed6 1226
10590c6a 1227 if (vri >= ARRAY_SIZE(wil->ring_tx)) {
230d8442 1228 wil_err(wil, "Enable for invalid vring %d\n", vri);
e58c9f70
VK
1229 return;
1230 }
849a564b 1231
b9010f10
AM
1232 if (wdev->iftype != NL80211_IFTYPE_AP || !disable_ap_sme ||
1233 test_bit(wil_vif_ft_roam, vif->status))
1234 /* in AP mode with disable_ap_sme that is not FT,
1235 * this is done by wil_cfg80211_change_station()
849a564b 1236 */
10590c6a
GS
1237 wil->ring_tx_data[vri].dot1x_open = true;
1238 if (vri == vif->bcast_ring) /* no BA for bcast */
e58c9f70 1239 return;
b9010f10
AM
1240
1241 cid = wil->ring2cid_tid[vri][0];
ddf7afdd 1242 if (!wil_cid_valid(wil, cid)) {
b9010f10
AM
1243 wil_err(wil, "invalid cid %d for vring %d\n", cid, vri);
1244 return;
1245 }
1246
1247 /* In FT mode we get key but not store it as it is received
1248 * before WMI_CONNECT_EVENT received from FW.
1249 * wil_set_crypto_rx is called here to reset the security PN
1250 */
1251 sta = &wil->sta[cid];
1252 if (test_bit(wil_vif_ft_roam, vif->status)) {
1253 memset(&params, 0, sizeof(params));
1254 wil_set_crypto_rx(0, WMI_KEY_USE_PAIRWISE, sta, &params);
1255 if (wdev->iftype != NL80211_IFTYPE_AP)
1256 clear_bit(wil_vif_ft_roam, vif->status);
1257 }
1258
230d8442
VK
1259 if (agg_wsize >= 0)
1260 wil_addba_tx_request(wil, vri, agg_wsize);
3442a504
VK
1261}
1262
e00243fa
LD
1263static void wmi_evt_ba_status(struct wil6210_vif *vif, int id,
1264 void *d, int len)
249a382b 1265{
e00243fa 1266 struct wil6210_priv *wil = vif_to_wil(vif);
b874ddec 1267 struct wmi_ba_status_event *evt = d;
10590c6a 1268 struct wil_ring_tx_data *txdata;
249a382b 1269
cbcf5866 1270 wil_dbg_wmi(wil, "BACK[%d] %s {%d} timeout %d AMSDU%s\n",
7b05b0ab
VK
1271 evt->ringid,
1272 evt->status == WMI_BA_AGREED ? "OK" : "N/A",
cbcf5866
VK
1273 evt->agg_wsize, __le16_to_cpu(evt->ba_timeout),
1274 evt->amsdu ? "+" : "-");
7b05b0ab
VK
1275
1276 if (evt->ringid >= WIL6210_MAX_TX_RINGS) {
1277 wil_err(wil, "invalid ring id %d\n", evt->ringid);
1278 return;
b4490f42
VK
1279 }
1280
3277213f
VK
1281 if (evt->status != WMI_BA_AGREED) {
1282 evt->ba_timeout = 0;
1283 evt->agg_wsize = 0;
cbcf5866 1284 evt->amsdu = 0;
7b05b0ab
VK
1285 }
1286
10590c6a 1287 txdata = &wil->ring_tx_data[evt->ringid];
ec81b5ad 1288
3277213f
VK
1289 txdata->agg_timeout = le16_to_cpu(evt->ba_timeout);
1290 txdata->agg_wsize = evt->agg_wsize;
cbcf5866 1291 txdata->agg_amsdu = evt->amsdu;
3a124ed6 1292 txdata->addba_in_progress = false;
3277213f 1293}
ec81b5ad 1294
e00243fa
LD
1295static void wmi_evt_addba_rx_req(struct wil6210_vif *vif, int id,
1296 void *d, int len)
3277213f 1297{
e00243fa 1298 struct wil6210_priv *wil = vif_to_wil(vif);
bf0353a6 1299 u8 cid, tid;
3277213f 1300 struct wmi_rcp_addba_req_event *evt = d;
ec81b5ad 1301
bf0353a6
AM
1302 if (evt->cidxtid != CIDXTID_EXTENDED_CID_TID) {
1303 parse_cidxtid(evt->cidxtid, &cid, &tid);
1304 } else {
1305 cid = evt->cid;
1306 tid = evt->tid;
1307 }
1308 wil_addba_rx_request(wil, vif->mid, cid, tid, evt->dialog_token,
3277213f
VK
1309 evt->ba_param_set, evt->ba_timeout,
1310 evt->ba_seq_ctrl);
1311}
ec81b5ad 1312
e00243fa 1313static void wmi_evt_delba(struct wil6210_vif *vif, int id, void *d, int len)
bd33273b 1314__acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
3277213f 1315{
e00243fa 1316 struct wil6210_priv *wil = vif_to_wil(vif);
3277213f
VK
1317 struct wmi_delba_event *evt = d;
1318 u8 cid, tid;
1319 u16 reason = __le16_to_cpu(evt->reason);
1320 struct wil_sta_info *sta;
1321 struct wil_tid_ampdu_rx *r;
3277213f 1322
bd33273b 1323 might_sleep();
bf0353a6
AM
1324
1325 if (evt->cidxtid != CIDXTID_EXTENDED_CID_TID) {
1326 parse_cidxtid(evt->cidxtid, &cid, &tid);
1327 } else {
1328 cid = evt->cid;
1329 tid = evt->tid;
1330 }
e78975fc
AAL
1331
1332 if (!wil_cid_valid(wil, cid)) {
1333 wil_err(wil, "DELBA: Invalid CID %d\n", cid);
1334 return;
1335 }
1336
e00243fa
LD
1337 wil_dbg_wmi(wil, "DELBA MID %d CID %d TID %d from %s reason %d\n",
1338 vif->mid, cid, tid,
3277213f
VK
1339 evt->from_initiator ? "originator" : "recipient",
1340 reason);
1341 if (!evt->from_initiator) {
1342 int i;
1343 /* find Tx vring it belongs to */
10590c6a
GS
1344 for (i = 0; i < ARRAY_SIZE(wil->ring2cid_tid); i++) {
1345 if (wil->ring2cid_tid[i][0] == cid &&
1346 wil->ring2cid_tid[i][1] == tid) {
1347 struct wil_ring_tx_data *txdata =
1348 &wil->ring_tx_data[i];
3277213f
VK
1349
1350 wil_dbg_wmi(wil, "DELBA Tx vring %d\n", i);
1351 txdata->agg_timeout = 0;
1352 txdata->agg_wsize = 0;
3a124ed6 1353 txdata->addba_in_progress = false;
3277213f
VK
1354
1355 break; /* max. 1 matching ring */
1356 }
1357 }
10590c6a 1358 if (i >= ARRAY_SIZE(wil->ring2cid_tid))
3277213f
VK
1359 wil_err(wil, "DELBA: unable to find Tx vring\n");
1360 return;
7b05b0ab 1361 }
8c86f757 1362
3277213f
VK
1363 sta = &wil->sta[cid];
1364
bd33273b 1365 spin_lock_bh(&sta->tid_rx_lock);
3277213f
VK
1366
1367 r = sta->tid_rx[tid];
1368 sta->tid_rx[tid] = NULL;
1369 wil_tid_ampdu_rx_free(wil, r);
1370
bd33273b 1371 spin_unlock_bh(&sta->tid_rx_lock);
249a382b
VK
1372}
1373
a5dc6883 1374static void
e00243fa 1375wmi_evt_sched_scan_result(struct wil6210_vif *vif, int id, void *d, int len)
a5dc6883 1376{
e00243fa 1377 struct wil6210_priv *wil = vif_to_wil(vif);
a5dc6883
DL
1378 struct wmi_sched_scan_result_event *data = d;
1379 struct wiphy *wiphy = wil_to_wiphy(wil);
1380 struct ieee80211_mgmt *rx_mgmt_frame =
1381 (struct ieee80211_mgmt *)data->payload;
1382 int flen = len - offsetof(struct wmi_sched_scan_result_event, payload);
1383 int ch_no;
1384 u32 freq;
1385 struct ieee80211_channel *channel;
1386 s32 signal;
1387 __le16 fc;
1388 u32 d_len;
1389 struct cfg80211_bss *bss;
058b3f11
ME
1390 struct cfg80211_inform_bss bss_data = {
1391 .scan_width = NL80211_BSS_CHAN_WIDTH_20,
1392 .boottime_ns = ktime_to_ns(ktime_get_boottime()),
1393 };
a5dc6883
DL
1394
1395 if (flen < 0) {
1396 wil_err(wil, "sched scan result event too short, len %d\n",
1397 len);
1398 return;
1399 }
1400
1401 d_len = le32_to_cpu(data->info.len);
1402 if (d_len != flen) {
1403 wil_err(wil,
1404 "sched scan result length mismatch, d_len %d should be %d\n",
1405 d_len, flen);
1406 return;
1407 }
1408
1409 fc = rx_mgmt_frame->frame_control;
1410 if (!ieee80211_is_probe_resp(fc)) {
1411 wil_err(wil, "sched scan result invalid frame, fc 0x%04x\n",
1412 fc);
1413 return;
1414 }
1415
1416 ch_no = data->info.channel + 1;
1417 freq = ieee80211_channel_to_frequency(ch_no, NL80211_BAND_60GHZ);
1418 channel = ieee80211_get_channel(wiphy, freq);
1419 if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
1420 signal = 100 * data->info.rssi;
1421 else
1422 signal = data->info.sqi;
1423
1424 wil_dbg_wmi(wil, "sched scan result: channel %d MCS %d RSSI %d\n",
1425 data->info.channel, data->info.mcs, data->info.rssi);
1426 wil_dbg_wmi(wil, "len %d qid %d mid %d cid %d\n",
1427 d_len, data->info.qid, data->info.mid, data->info.cid);
1428 wil_hex_dump_wmi("PROBE ", DUMP_PREFIX_OFFSET, 16, 1, rx_mgmt_frame,
1429 d_len, true);
1430
1431 if (!channel) {
1432 wil_err(wil, "Frame on unsupported channel\n");
1433 return;
1434 }
1435
058b3f11
ME
1436 bss_data.signal = signal;
1437 bss_data.chan = channel;
1438 bss = cfg80211_inform_bss_frame_data(wiphy, &bss_data, rx_mgmt_frame,
1439 d_len, GFP_KERNEL);
a5dc6883
DL
1440 if (bss) {
1441 wil_dbg_wmi(wil, "Added BSS %pM\n", rx_mgmt_frame->bssid);
1442 cfg80211_put_bss(wiphy, bss);
1443 } else {
1444 wil_err(wil, "cfg80211_inform_bss_frame() failed\n");
1445 }
1446
1447 cfg80211_sched_scan_results(wiphy, 0);
1448}
1449
0c936b3c
DL
1450static void wil_link_stats_store_basic(struct wil6210_vif *vif,
1451 struct wmi_link_stats_basic *basic)
1452{
1453 struct wil6210_priv *wil = vif_to_wil(vif);
1454 u8 cid = basic->cid;
1455 struct wil_sta_info *sta;
1456
ddf7afdd 1457 if (cid < 0 || cid >= wil->max_assoc_sta) {
0c936b3c
DL
1458 wil_err(wil, "invalid cid %d\n", cid);
1459 return;
1460 }
1461
1462 sta = &wil->sta[cid];
1463 sta->fw_stats_basic = *basic;
1464}
1465
1466static void wil_link_stats_store_global(struct wil6210_vif *vif,
1467 struct wmi_link_stats_global *global)
1468{
1469 struct wil6210_priv *wil = vif_to_wil(vif);
1470
1471 wil->fw_stats_global.stats = *global;
1472}
1473
1474static void wmi_link_stats_parse(struct wil6210_vif *vif, u64 tsf,
1475 bool has_next, void *payload,
1476 size_t payload_size)
1477{
1478 struct wil6210_priv *wil = vif_to_wil(vif);
1479 size_t hdr_size = sizeof(struct wmi_link_stats_record);
1480 size_t stats_size, record_size, expected_size;
1481 struct wmi_link_stats_record *hdr;
1482
1483 if (payload_size < hdr_size) {
1484 wil_err(wil, "link stats wrong event size %zu\n", payload_size);
1485 return;
1486 }
1487
1488 while (payload_size >= hdr_size) {
1489 hdr = payload;
1490 stats_size = le16_to_cpu(hdr->record_size);
1491 record_size = hdr_size + stats_size;
1492
1493 if (payload_size < record_size) {
1494 wil_err(wil, "link stats payload ended unexpectedly, size %zu < %zu\n",
1495 payload_size, record_size);
1496 return;
1497 }
1498
1499 switch (hdr->record_type_id) {
1500 case WMI_LINK_STATS_TYPE_BASIC:
1501 expected_size = sizeof(struct wmi_link_stats_basic);
1502 if (stats_size < expected_size) {
1503 wil_err(wil, "link stats invalid basic record size %zu < %zu\n",
1504 stats_size, expected_size);
1505 return;
1506 }
1507 if (vif->fw_stats_ready) {
1508 /* clean old statistics */
1509 vif->fw_stats_tsf = 0;
1510 vif->fw_stats_ready = 0;
1511 }
1512
1513 wil_link_stats_store_basic(vif, payload + hdr_size);
1514
1515 if (!has_next) {
1516 vif->fw_stats_tsf = tsf;
1517 vif->fw_stats_ready = 1;
1518 }
1519
1520 break;
1521 case WMI_LINK_STATS_TYPE_GLOBAL:
1522 expected_size = sizeof(struct wmi_link_stats_global);
1523 if (stats_size < sizeof(struct wmi_link_stats_global)) {
1524 wil_err(wil, "link stats invalid global record size %zu < %zu\n",
1525 stats_size, expected_size);
1526 return;
1527 }
1528
1529 if (wil->fw_stats_global.ready) {
1530 /* clean old statistics */
1531 wil->fw_stats_global.tsf = 0;
1532 wil->fw_stats_global.ready = 0;
1533 }
1534
1535 wil_link_stats_store_global(vif, payload + hdr_size);
1536
1537 if (!has_next) {
1538 wil->fw_stats_global.tsf = tsf;
1539 wil->fw_stats_global.ready = 1;
1540 }
1541
1542 break;
1543 default:
1544 break;
1545 }
1546
1547 /* skip to next record */
1548 payload += record_size;
1549 payload_size -= record_size;
1550 }
1551}
1552
1553static void
1554wmi_evt_link_stats(struct wil6210_vif *vif, int id, void *d, int len)
1555{
1556 struct wil6210_priv *wil = vif_to_wil(vif);
1557 struct wmi_link_stats_event *evt = d;
1558 size_t payload_size;
1559
1560 if (len < offsetof(struct wmi_link_stats_event, payload)) {
1561 wil_err(wil, "stats event way too short %d\n", len);
1562 return;
1563 }
1564 payload_size = le16_to_cpu(evt->payload_size);
1565 if (len < sizeof(struct wmi_link_stats_event) + payload_size) {
1566 wil_err(wil, "stats event too short %d\n", len);
1567 return;
1568 }
1569
1570 wmi_link_stats_parse(vif, le64_to_cpu(evt->tsf), evt->has_next,
1571 evt->payload, payload_size);
1572}
1573
b9010f10
AM
1574/**
1575 * find cid and ringid for the station vif
1576 *
1577 * return error, if other interfaces are used or ring was not found
1578 */
1579static int wil_find_cid_ringid_sta(struct wil6210_priv *wil,
1580 struct wil6210_vif *vif,
1581 int *cid,
1582 int *ringid)
1583{
1584 struct wil_ring *ring;
1585 struct wil_ring_tx_data *txdata;
1586 int min_ring_id = wil_get_min_tx_ring_id(wil);
1587 int i;
1588 u8 lcid;
1589
1590 if (!(vif->wdev.iftype == NL80211_IFTYPE_STATION ||
1591 vif->wdev.iftype == NL80211_IFTYPE_P2P_CLIENT)) {
1592 wil_err(wil, "invalid interface type %d\n", vif->wdev.iftype);
1593 return -EINVAL;
1594 }
1595
1596 /* In the STA mode, it is expected to have only one ring
1597 * for the AP we are connected to.
1598 * find it and return the cid associated with it.
1599 */
1600 for (i = min_ring_id; i < WIL6210_MAX_TX_RINGS; i++) {
1601 ring = &wil->ring_tx[i];
1602 txdata = &wil->ring_tx_data[i];
1603 if (!ring->va || !txdata->enabled || txdata->mid != vif->mid)
1604 continue;
1605
1606 lcid = wil->ring2cid_tid[i][0];
ddf7afdd 1607 if (lcid >= wil->max_assoc_sta) /* skip BCAST */
b9010f10
AM
1608 continue;
1609
1610 wil_dbg_wmi(wil, "find sta -> ringid %d cid %d\n", i, lcid);
1611 *cid = lcid;
1612 *ringid = i;
1613 return 0;
1614 }
1615
1616 wil_dbg_wmi(wil, "find sta cid while no rings active?\n");
1617
1618 return -ENOENT;
1619}
1620
1621static void
1622wmi_evt_auth_status(struct wil6210_vif *vif, int id, void *d, int len)
1623{
1624 struct wil6210_priv *wil = vif_to_wil(vif);
1625 struct net_device *ndev = vif_to_ndev(vif);
1626 struct wmi_ft_auth_status_event *data = d;
1627 int ie_len = len - offsetof(struct wmi_ft_auth_status_event, ie_info);
1628 int rc, cid = 0, ringid = 0;
1629 struct cfg80211_ft_event_params ft;
1630 u16 d_len;
1631 /* auth_alg(u16) + auth_transaction(u16) + status_code(u16) */
1632 const size_t auth_ie_offset = sizeof(u16) * 3;
1633 struct auth_no_hdr *auth = (struct auth_no_hdr *)data->ie_info;
1634
1635 /* check the status */
1636 if (ie_len >= 0 && data->status != WMI_FW_STATUS_SUCCESS) {
1637 wil_err(wil, "FT: auth failed. status %d\n", data->status);
1638 goto fail;
1639 }
1640
1641 if (ie_len < auth_ie_offset) {
1642 wil_err(wil, "FT: auth event too short, len %d\n", len);
1643 goto fail;
1644 }
1645
1646 d_len = le16_to_cpu(data->ie_len);
1647 if (d_len != ie_len) {
1648 wil_err(wil,
1649 "FT: auth ie length mismatch, d_len %d should be %d\n",
1650 d_len, ie_len);
1651 goto fail;
1652 }
1653
1654 if (!test_bit(wil_vif_ft_roam, wil->status)) {
1655 wil_err(wil, "FT: Not in roaming state\n");
1656 goto fail;
1657 }
1658
1659 if (le16_to_cpu(auth->auth_transaction) != 2) {
1660 wil_err(wil, "FT: auth error. auth_transaction %d\n",
1661 le16_to_cpu(auth->auth_transaction));
1662 goto fail;
1663 }
1664
1665 if (le16_to_cpu(auth->auth_alg) != WLAN_AUTH_FT) {
1666 wil_err(wil, "FT: auth error. auth_alg %d\n",
1667 le16_to_cpu(auth->auth_alg));
1668 goto fail;
1669 }
1670
1671 wil_dbg_wmi(wil, "FT: Auth to %pM successfully\n", data->mac_addr);
1672 wil_hex_dump_wmi("FT Auth ies : ", DUMP_PREFIX_OFFSET, 16, 1,
1673 data->ie_info, d_len, true);
1674
1675 /* find cid and ringid */
1676 rc = wil_find_cid_ringid_sta(wil, vif, &cid, &ringid);
1677 if (rc) {
1678 wil_err(wil, "No valid cid found\n");
1679 goto fail;
1680 }
1681
1682 if (vif->privacy) {
1683 /* For secure assoc, remove old keys */
1684 rc = wmi_del_cipher_key(vif, 0, wil->sta[cid].addr,
1685 WMI_KEY_USE_PAIRWISE);
1686 if (rc) {
1687 wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(PTK) failed\n");
1688 goto fail;
1689 }
1690 rc = wmi_del_cipher_key(vif, 0, wil->sta[cid].addr,
1691 WMI_KEY_USE_RX_GROUP);
1692 if (rc) {
1693 wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(GTK) failed\n");
1694 goto fail;
1695 }
1696 }
1697
1698 memset(&ft, 0, sizeof(ft));
1699 ft.ies = data->ie_info + auth_ie_offset;
1700 ft.ies_len = d_len - auth_ie_offset;
1701 ft.target_ap = data->mac_addr;
1702 cfg80211_ft_event(ndev, &ft);
1703
1704 return;
1705
1706fail:
e1b43407 1707 wil6210_disconnect(vif, NULL, WLAN_REASON_PREV_AUTH_NOT_VALID);
b9010f10
AM
1708}
1709
1710static void
1711wmi_evt_reassoc_status(struct wil6210_vif *vif, int id, void *d, int len)
1712{
1713 struct wil6210_priv *wil = vif_to_wil(vif);
1714 struct net_device *ndev = vif_to_ndev(vif);
1715 struct wiphy *wiphy = wil_to_wiphy(wil);
1716 struct wmi_ft_reassoc_status_event *data = d;
1717 int ies_len = len - offsetof(struct wmi_ft_reassoc_status_event,
1718 ie_info);
1719 int rc = -ENOENT, cid = 0, ringid = 0;
1720 int ch; /* channel number (primary) */
1721 size_t assoc_req_ie_len = 0, assoc_resp_ie_len = 0;
1722 u8 *assoc_req_ie = NULL, *assoc_resp_ie = NULL;
1723 /* capinfo(u16) + listen_interval(u16) + current_ap mac addr + IEs */
1724 const size_t assoc_req_ie_offset = sizeof(u16) * 2 + ETH_ALEN;
1725 /* capinfo(u16) + status_code(u16) + associd(u16) + IEs */
1726 const size_t assoc_resp_ie_offset = sizeof(u16) * 3;
1727 u16 d_len;
1728 int freq;
1729 struct cfg80211_roam_info info;
1730
1731 if (ies_len < 0) {
1732 wil_err(wil, "ft reassoc event too short, len %d\n", len);
1733 goto fail;
1734 }
1735
1736 wil_dbg_wmi(wil, "Reasoc Status event: status=%d, aid=%d",
1737 data->status, data->aid);
1738 wil_dbg_wmi(wil, " mac_addr=%pM, beacon_ie_len=%d",
1739 data->mac_addr, data->beacon_ie_len);
1740 wil_dbg_wmi(wil, " reassoc_req_ie_len=%d, reassoc_resp_ie_len=%d",
1741 le16_to_cpu(data->reassoc_req_ie_len),
1742 le16_to_cpu(data->reassoc_resp_ie_len));
1743
1744 d_len = le16_to_cpu(data->beacon_ie_len) +
1745 le16_to_cpu(data->reassoc_req_ie_len) +
1746 le16_to_cpu(data->reassoc_resp_ie_len);
1747 if (d_len != ies_len) {
1748 wil_err(wil,
1749 "ft reassoc ie length mismatch, d_len %d should be %d\n",
1750 d_len, ies_len);
1751 goto fail;
1752 }
1753
1754 /* check the status */
1755 if (data->status != WMI_FW_STATUS_SUCCESS) {
1756 wil_err(wil, "ft reassoc failed. status %d\n", data->status);
1757 goto fail;
1758 }
1759
1760 /* find cid and ringid */
1761 rc = wil_find_cid_ringid_sta(wil, vif, &cid, &ringid);
1762 if (rc) {
1763 wil_err(wil, "No valid cid found\n");
1764 goto fail;
1765 }
1766
1767 ch = data->channel + 1;
1768 wil_info(wil, "FT: Roam %pM channel [%d] cid %d aid %d\n",
1769 data->mac_addr, ch, cid, data->aid);
1770
1771 wil_hex_dump_wmi("reassoc AI : ", DUMP_PREFIX_OFFSET, 16, 1,
1772 data->ie_info, len - sizeof(*data), true);
1773
1774 /* figure out IE's */
1775 if (le16_to_cpu(data->reassoc_req_ie_len) > assoc_req_ie_offset) {
1776 assoc_req_ie = &data->ie_info[assoc_req_ie_offset];
1777 assoc_req_ie_len = le16_to_cpu(data->reassoc_req_ie_len) -
1778 assoc_req_ie_offset;
1779 }
1780 if (le16_to_cpu(data->reassoc_resp_ie_len) <= assoc_resp_ie_offset) {
1781 wil_err(wil, "FT: reassoc resp ie len is too short, len %d\n",
1782 le16_to_cpu(data->reassoc_resp_ie_len));
1783 goto fail;
1784 }
1785
1786 assoc_resp_ie = &data->ie_info[le16_to_cpu(data->reassoc_req_ie_len) +
1787 assoc_resp_ie_offset];
1788 assoc_resp_ie_len = le16_to_cpu(data->reassoc_resp_ie_len) -
1789 assoc_resp_ie_offset;
1790
1791 if (test_bit(wil_status_resetting, wil->status) ||
1792 !test_bit(wil_status_fwready, wil->status)) {
1793 wil_err(wil, "FT: status_resetting, cancel reassoc event\n");
1794 /* no need for cleanup, wil_reset will do that */
1795 return;
1796 }
1797
1798 mutex_lock(&wil->mutex);
1799
1800 /* ring modify to set the ring for the roamed AP settings */
1801 wil_dbg_wmi(wil,
1802 "ft modify tx config for connection CID %d ring %d\n",
1803 cid, ringid);
1804
1805 rc = wil->txrx_ops.tx_ring_modify(vif, ringid, cid, 0);
1806 if (rc) {
1807 wil_err(wil, "modify TX for CID %d MID %d ring %d failed (%d)\n",
1808 cid, vif->mid, ringid, rc);
1809 mutex_unlock(&wil->mutex);
1810 goto fail;
1811 }
1812
1813 /* Update the driver STA members with the new bss */
1814 wil->sta[cid].aid = data->aid;
1815 wil->sta[cid].stats.ft_roams++;
1816 ether_addr_copy(wil->sta[cid].addr, vif->bss->bssid);
1817 mutex_unlock(&wil->mutex);
1818 del_timer_sync(&vif->connect_timer);
1819
1820 cfg80211_ref_bss(wiphy, vif->bss);
1821 freq = ieee80211_channel_to_frequency(ch, NL80211_BAND_60GHZ);
1822
1823 memset(&info, 0, sizeof(info));
1824 info.channel = ieee80211_get_channel(wiphy, freq);
1825 info.bss = vif->bss;
1826 info.req_ie = assoc_req_ie;
1827 info.req_ie_len = assoc_req_ie_len;
1828 info.resp_ie = assoc_resp_ie;
1829 info.resp_ie_len = assoc_resp_ie_len;
1830 cfg80211_roamed(ndev, &info, GFP_KERNEL);
1831 vif->bss = NULL;
1832
1833 return;
1834
1835fail:
e1b43407 1836 wil6210_disconnect(vif, NULL, WLAN_REASON_PREV_AUTH_NOT_VALID);
b9010f10
AM
1837}
1838
b03fbab0
VK
1839/**
1840 * Some events are ignored for purpose; and need not be interpreted as
1841 * "unhandled events"
1842 */
e00243fa 1843static void wmi_evt_ignore(struct wil6210_vif *vif, int id, void *d, int len)
b03fbab0 1844{
e00243fa
LD
1845 struct wil6210_priv *wil = vif_to_wil(vif);
1846
b03fbab0
VK
1847 wil_dbg_wmi(wil, "Ignore event 0x%04x len %d\n", id, len);
1848}
1849
2be7d22f
VK
1850static const struct {
1851 int eventid;
e00243fa
LD
1852 void (*handler)(struct wil6210_vif *vif,
1853 int eventid, void *data, int data_len);
2be7d22f
VK
1854} wmi_evt_handlers[] = {
1855 {WMI_READY_EVENTID, wmi_evt_ready},
817f1853 1856 {WMI_FW_READY_EVENTID, wmi_evt_ignore},
2be7d22f 1857 {WMI_RX_MGMT_PACKET_EVENTID, wmi_evt_rx_mgmt},
90d89e9a 1858 {WMI_TX_MGMT_PACKET_EVENTID, wmi_evt_tx_mgmt},
2be7d22f
VK
1859 {WMI_SCAN_COMPLETE_EVENTID, wmi_evt_scan_complete},
1860 {WMI_CONNECT_EVENTID, wmi_evt_connect},
1861 {WMI_DISCONNECT_EVENTID, wmi_evt_disconnect},
2be7d22f 1862 {WMI_EAPOL_RX_EVENTID, wmi_evt_eapol_rx},
249a382b 1863 {WMI_BA_STATUS_EVENTID, wmi_evt_ba_status},
3277213f
VK
1864 {WMI_RCP_ADDBA_REQ_EVENTID, wmi_evt_addba_rx_req},
1865 {WMI_DELBA_EVENTID, wmi_evt_delba},
96c93589 1866 {WMI_RING_EN_EVENTID, wmi_evt_ring_en},
b03fbab0 1867 {WMI_DATA_PORT_OPEN_EVENTID, wmi_evt_ignore},
a5dc6883 1868 {WMI_SCHED_SCAN_RESULT_EVENTID, wmi_evt_sched_scan_result},
0c936b3c 1869 {WMI_LINK_STATS_EVENTID, wmi_evt_link_stats},
b9010f10
AM
1870 {WMI_FT_AUTH_STATUS_EVENTID, wmi_evt_auth_status},
1871 {WMI_FT_REASSOC_STATUS_EVENTID, wmi_evt_reassoc_status},
2be7d22f
VK
1872};
1873
1874/*
1875 * Run in IRQ context
1876 * Extract WMI command from mailbox. Queue it to the @wil->pending_wmi_ev
1877 * that will be eventually handled by the @wmi_event_worker in the thread
1878 * context of thread "wil6210_wmi"
1879 */
1880void wmi_recv_cmd(struct wil6210_priv *wil)
1881{
1882 struct wil6210_mbox_ring_desc d_tail;
1883 struct wil6210_mbox_hdr hdr;
1884 struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
1885 struct pending_wmi_event *evt;
1886 u8 *cmd;
1887 void __iomem *src;
1888 ulong flags;
a715c7dd 1889 unsigned n;
0916d9f2 1890 unsigned int num_immed_reply = 0;
2be7d22f 1891
817f1853 1892 if (!test_bit(wil_status_mbox_ready, wil->status)) {
4cf99c93 1893 wil_err(wil, "Reset in progress. Cannot handle WMI event\n");
55f7acdd
VK
1894 return;
1895 }
1896
fe9ee51e
ME
1897 if (test_bit(wil_status_suspended, wil->status)) {
1898 wil_err(wil, "suspended. cannot handle WMI event\n");
1899 return;
1900 }
1901
a715c7dd 1902 for (n = 0;; n++) {
2be7d22f 1903 u16 len;
95266dc0 1904 bool q;
0916d9f2 1905 bool immed_reply = false;
2be7d22f 1906
b9eeb512
VK
1907 r->head = wil_r(wil, RGF_MBOX +
1908 offsetof(struct wil6210_mbox_ctl, rx.head));
95266dc0
VK
1909 if (r->tail == r->head)
1910 break;
2be7d22f 1911
a715c7dd
VK
1912 wil_dbg_wmi(wil, "Mbox head %08x tail %08x\n",
1913 r->head, r->tail);
1914 /* read cmd descriptor from tail */
2be7d22f
VK
1915 wil_memcpy_fromio_32(&d_tail, wil->csr + HOSTADDR(r->tail),
1916 sizeof(struct wil6210_mbox_ring_desc));
1917 if (d_tail.sync == 0) {
1918 wil_err(wil, "Mbox evt not owned by FW?\n");
95266dc0 1919 break;
2be7d22f
VK
1920 }
1921
a715c7dd 1922 /* read cmd header from descriptor */
2be7d22f
VK
1923 if (0 != wmi_read_hdr(wil, d_tail.addr, &hdr)) {
1924 wil_err(wil, "Mbox evt at 0x%08x?\n",
1925 le32_to_cpu(d_tail.addr));
95266dc0 1926 break;
2be7d22f 1927 }
2be7d22f 1928 len = le16_to_cpu(hdr.len);
a715c7dd
VK
1929 wil_dbg_wmi(wil, "Mbox evt %04x %04x %04x %02x\n",
1930 le16_to_cpu(hdr.seq), len, le16_to_cpu(hdr.type),
1931 hdr.flags);
1932
1933 /* read cmd buffer from descriptor */
2be7d22f
VK
1934 src = wmi_buffer(wil, d_tail.addr) +
1935 sizeof(struct wil6210_mbox_hdr);
1936 evt = kmalloc(ALIGN(offsetof(struct pending_wmi_event,
1937 event.wmi) + len, 4),
1938 GFP_KERNEL);
14f8dc49 1939 if (!evt)
95266dc0 1940 break;
14f8dc49 1941
2be7d22f
VK
1942 evt->event.hdr = hdr;
1943 cmd = (void *)&evt->event.wmi;
1944 wil_memcpy_fromio_32(cmd, src, len);
1945 /* mark entry as empty */
b9eeb512
VK
1946 wil_w(wil, r->tail +
1947 offsetof(struct wil6210_mbox_ring_desc, sync), 0);
2be7d22f 1948 /* indicate */
2be7d22f 1949 if ((hdr.type == WIL_MBOX_HDR_TYPE_WMI) &&
b874ddec
LD
1950 (len >= sizeof(struct wmi_cmd_hdr))) {
1951 struct wmi_cmd_hdr *wmi = &evt->event.wmi;
1952 u16 id = le16_to_cpu(wmi->command_id);
e00243fa 1953 u8 mid = wmi->mid;
b874ddec 1954 u32 tstamp = le32_to_cpu(wmi->fw_timestamp);
fe9ee51e
ME
1955 if (test_bit(wil_status_resuming, wil->status)) {
1956 if (id == WMI_TRAFFIC_RESUME_EVENTID)
1957 clear_bit(wil_status_resuming,
1958 wil->status);
1959 else
1960 wil_err(wil,
1961 "WMI evt %d while resuming\n",
1962 id);
1963 }
fe5c271e 1964 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
e00243fa
LD
1965 if (wil->reply_id && wil->reply_id == id &&
1966 wil->reply_mid == mid) {
0916d9f2
ME
1967 if (wil->reply_buf) {
1968 memcpy(wil->reply_buf, wmi,
1969 min(len, wil->reply_size));
1970 immed_reply = true;
1971 }
fe9ee51e
ME
1972 if (id == WMI_TRAFFIC_SUSPEND_EVENTID) {
1973 wil_dbg_wmi(wil,
1974 "set suspend_resp_rcvd\n");
1975 wil->suspend_resp_rcvd = true;
1976 }
0916d9f2 1977 }
fe5c271e 1978 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
8fe59627 1979
5fad7241
DL
1980 wil_dbg_wmi(wil, "recv %s (0x%04x) MID %d @%d msec\n",
1981 eventid2name(id), id, wmi->mid, tstamp);
f988b23f
VK
1982 trace_wil6210_wmi_event(wmi, &wmi[1],
1983 len - sizeof(*wmi));
2be7d22f 1984 }
7743882d 1985 wil_hex_dump_wmi("evt ", DUMP_PREFIX_OFFSET, 16, 1,
2be7d22f
VK
1986 &evt->event.hdr, sizeof(hdr) + len, true);
1987
1988 /* advance tail */
1989 r->tail = r->base + ((r->tail - r->base +
1990 sizeof(struct wil6210_mbox_ring_desc)) % r->size);
b9eeb512
VK
1991 wil_w(wil, RGF_MBOX +
1992 offsetof(struct wil6210_mbox_ctl, rx.tail), r->tail);
2be7d22f 1993
0916d9f2 1994 if (immed_reply) {
af3db60a
LA
1995 wil_dbg_wmi(wil, "recv_cmd: Complete WMI 0x%04x\n",
1996 wil->reply_id);
0916d9f2
ME
1997 kfree(evt);
1998 num_immed_reply++;
1999 complete(&wil->wmi_call);
2000 } else {
2001 /* add to the pending list */
2002 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
2003 list_add_tail(&evt->list, &wil->pending_wmi_ev);
2004 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
2005 q = queue_work(wil->wmi_wq, &wil->wmi_event_worker);
2006 wil_dbg_wmi(wil, "queue_work -> %d\n", q);
2007 }
2be7d22f 2008 }
95266dc0 2009 /* normally, 1 event per IRQ should be processed */
af3db60a 2010 wil_dbg_wmi(wil, "recv_cmd: -> %d events queued, %d completed\n",
0916d9f2 2011 n - num_immed_reply, num_immed_reply);
2be7d22f
VK
2012}
2013
e00243fa 2014int wmi_call(struct wil6210_priv *wil, u16 cmdid, u8 mid, void *buf, u16 len,
5f85c7e7 2015 u16 reply_id, void *reply, u16 reply_size, int to_msec)
2be7d22f
VK
2016{
2017 int rc;
f4bbb829 2018 unsigned long remain;
dc57731d 2019 ulong flags;
2be7d22f
VK
2020
2021 mutex_lock(&wil->wmi_mutex);
2022
dc57731d 2023 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
fe5c271e 2024 wil->reply_id = reply_id;
e00243fa 2025 wil->reply_mid = mid;
fe5c271e
ME
2026 wil->reply_buf = reply;
2027 wil->reply_size = reply_size;
4d4c4dc3 2028 reinit_completion(&wil->wmi_call);
dc57731d 2029 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
fe5c271e 2030
e00243fa 2031 rc = __wmi_send(wil, cmdid, mid, buf, len);
2be7d22f
VK
2032 if (rc)
2033 goto out;
2034
59502647
DL
2035 remain = wait_for_completion_timeout(&wil->wmi_call,
2036 msecs_to_jiffies(to_msec));
2be7d22f
VK
2037 if (0 == remain) {
2038 wil_err(wil, "wmi_call(0x%04x->0x%04x) timeout %d msec\n",
2039 cmdid, reply_id, to_msec);
2040 rc = -ETIME;
2041 } else {
7743882d 2042 wil_dbg_wmi(wil,
2be7d22f
VK
2043 "wmi_call(0x%04x->0x%04x) completed in %d msec\n",
2044 cmdid, reply_id,
2045 to_msec - jiffies_to_msecs(remain));
2046 }
fe5c271e
ME
2047
2048out:
dc57731d 2049 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
2be7d22f 2050 wil->reply_id = 0;
e00243fa 2051 wil->reply_mid = U8_MAX;
2be7d22f
VK
2052 wil->reply_buf = NULL;
2053 wil->reply_size = 0;
dc57731d 2054 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
fe5c271e 2055
2be7d22f
VK
2056 mutex_unlock(&wil->wmi_mutex);
2057
2058 return rc;
2059}
2060
2061int wmi_echo(struct wil6210_priv *wil)
2062{
e00243fa 2063 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2be7d22f
VK
2064 struct wmi_echo_cmd cmd = {
2065 .value = cpu_to_le32(0x12345678),
2066 };
2067
e00243fa 2068 return wmi_call(wil, WMI_ECHO_CMDID, vif->mid, &cmd, sizeof(cmd),
2a32c20b
AM
2069 WMI_ECHO_RSP_EVENTID, NULL, 0,
2070 WIL_WMI_CALL_GENERAL_TO_MS);
2be7d22f
VK
2071}
2072
2073int wmi_set_mac_address(struct wil6210_priv *wil, void *addr)
2074{
e00243fa 2075 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2be7d22f
VK
2076 struct wmi_set_mac_address_cmd cmd;
2077
a82553bb 2078 ether_addr_copy(cmd.mac, addr);
2be7d22f 2079
7743882d 2080 wil_dbg_wmi(wil, "Set MAC %pM\n", addr);
2be7d22f 2081
e00243fa
LD
2082 return wmi_send(wil, WMI_SET_MAC_ADDRESS_CMDID, vif->mid,
2083 &cmd, sizeof(cmd));
2be7d22f
VK
2084}
2085
10d599ad
ME
2086int wmi_led_cfg(struct wil6210_priv *wil, bool enable)
2087{
e00243fa 2088 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
10d599ad
ME
2089 int rc = 0;
2090 struct wmi_led_cfg_cmd cmd = {
2091 .led_mode = enable,
2092 .id = led_id,
2093 .slow_blink_cfg.blink_on =
2094 cpu_to_le32(led_blink_time[WIL_LED_TIME_SLOW].on_ms),
2095 .slow_blink_cfg.blink_off =
2096 cpu_to_le32(led_blink_time[WIL_LED_TIME_SLOW].off_ms),
2097 .medium_blink_cfg.blink_on =
2098 cpu_to_le32(led_blink_time[WIL_LED_TIME_MED].on_ms),
2099 .medium_blink_cfg.blink_off =
2100 cpu_to_le32(led_blink_time[WIL_LED_TIME_MED].off_ms),
2101 .fast_blink_cfg.blink_on =
2102 cpu_to_le32(led_blink_time[WIL_LED_TIME_FAST].on_ms),
2103 .fast_blink_cfg.blink_off =
2104 cpu_to_le32(led_blink_time[WIL_LED_TIME_FAST].off_ms),
2105 .led_polarity = led_polarity,
2106 };
2107 struct {
2108 struct wmi_cmd_hdr wmi;
2109 struct wmi_led_cfg_done_event evt;
807b0860
AAL
2110 } __packed reply = {
2111 .evt = {.status = cpu_to_le32(WMI_FW_STATUS_FAILURE)},
2112 };
10d599ad
ME
2113
2114 if (led_id == WIL_LED_INVALID_ID)
2115 goto out;
2116
2117 if (led_id > WIL_LED_MAX_ID) {
2118 wil_err(wil, "Invalid led id %d\n", led_id);
2119 rc = -EINVAL;
2120 goto out;
2121 }
2122
2123 wil_dbg_wmi(wil,
2124 "%s led %d\n",
2125 enable ? "enabling" : "disabling", led_id);
2126
e00243fa 2127 rc = wmi_call(wil, WMI_LED_CFG_CMDID, vif->mid, &cmd, sizeof(cmd),
10d599ad 2128 WMI_LED_CFG_DONE_EVENTID, &reply, sizeof(reply),
2a32c20b 2129 WIL_WMI_CALL_GENERAL_TO_MS);
10d599ad
ME
2130 if (rc)
2131 goto out;
2132
2133 if (reply.evt.status) {
2134 wil_err(wil, "led %d cfg failed with status %d\n",
2135 led_id, le32_to_cpu(reply.evt.status));
2136 rc = -EINVAL;
2137 }
2138
2139out:
2140 return rc;
2141}
2142
c5b3a658
AAL
2143int wmi_rbufcap_cfg(struct wil6210_priv *wil, bool enable, u16 threshold)
2144{
2145 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2146 int rc;
2147
2148 struct wmi_rbufcap_cfg_cmd cmd = {
2149 .enable = enable,
2150 .rx_desc_threshold = cpu_to_le16(threshold),
2151 };
2152 struct {
2153 struct wmi_cmd_hdr wmi;
2154 struct wmi_rbufcap_cfg_event evt;
2155 } __packed reply = {
2156 .evt = {.status = WMI_FW_STATUS_FAILURE},
2157 };
2158
2159 rc = wmi_call(wil, WMI_RBUFCAP_CFG_CMDID, vif->mid, &cmd, sizeof(cmd),
2160 WMI_RBUFCAP_CFG_EVENTID, &reply, sizeof(reply),
2161 WIL_WMI_CALL_GENERAL_TO_MS);
2162 if (rc)
2163 return rc;
2164
2165 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
2166 wil_err(wil, "RBUFCAP_CFG failed. status %d\n",
2167 reply.evt.status);
2168 rc = -EINVAL;
2169 }
2170
2171 return rc;
2172}
2173
9abe3e30
AAL
2174int wmi_pcp_start(struct wil6210_vif *vif, int bi, u8 wmi_nettype,
2175 u8 chan, u8 wmi_edmg_chan, u8 hidden_ssid, u8 is_go)
2be7d22f 2176{
e00243fa 2177 struct wil6210_priv *wil = vif_to_wil(vif);
b8023177
VK
2178 int rc;
2179
2180 struct wmi_pcp_start_cmd cmd = {
2be7d22f
VK
2181 .bcon_interval = cpu_to_le16(bi),
2182 .network_type = wmi_nettype,
2183 .disable_sec_offload = 1,
adc2d122 2184 .channel = chan - 1,
9abe3e30 2185 .edmg_channel = wmi_edmg_chan,
ddf7afdd 2186 .pcp_max_assoc_sta = wil->max_assoc_sta,
8e52fe30 2187 .hidden_ssid = hidden_ssid,
b4944f2c 2188 .is_go = is_go,
8a4fa214
AM
2189 .ap_sme_offload_mode = disable_ap_sme ?
2190 WMI_AP_SME_OFFLOAD_PARTIAL :
2191 WMI_AP_SME_OFFLOAD_FULL,
c3bfea05 2192 .abft_len = wil->abft_len,
2be7d22f 2193 };
b8023177 2194 struct {
b874ddec 2195 struct wmi_cmd_hdr wmi;
b8023177 2196 struct wmi_pcp_started_event evt;
807b0860
AAL
2197 } __packed reply = {
2198 .evt = {.status = WMI_FW_STATUS_FAILURE},
2199 };
2be7d22f 2200
e00243fa 2201 if (!vif->privacy)
2be7d22f
VK
2202 cmd.disable_sec = 1;
2203
1eb9d1e5
DL
2204 if ((cmd.pcp_max_assoc_sta > WIL6210_MAX_CID) ||
2205 (cmd.pcp_max_assoc_sta <= 0)) {
bf0353a6
AM
2206 wil_err(wil, "unexpected max_assoc_sta %d\n",
2207 cmd.pcp_max_assoc_sta);
2208 return -EOPNOTSUPP;
1eb9d1e5
DL
2209 }
2210
849a564b 2211 if (disable_ap_sme &&
8a4fa214 2212 !test_bit(WMI_FW_CAPABILITY_AP_SME_OFFLOAD_PARTIAL,
849a564b
DL
2213 wil->fw_capabilities)) {
2214 wil_err(wil, "disable_ap_sme not supported by FW\n");
2215 return -EOPNOTSUPP;
2216 }
2217
8b5c7f6c
VK
2218 /*
2219 * Processing time may be huge, in case of secure AP it takes about
2220 * 3500ms for FW to start AP
2221 */
e00243fa 2222 rc = wmi_call(wil, WMI_PCP_START_CMDID, vif->mid, &cmd, sizeof(cmd),
8b5c7f6c 2223 WMI_PCP_STARTED_EVENTID, &reply, sizeof(reply), 5000);
b8023177
VK
2224 if (rc)
2225 return rc;
2226
2227 if (reply.evt.status != WMI_FW_STATUS_SUCCESS)
2228 rc = -EINVAL;
2229
10d599ad
ME
2230 if (wmi_nettype != WMI_NETTYPE_P2P)
2231 /* Don't fail due to error in the led configuration */
2232 wmi_led_cfg(wil, true);
2233
b8023177
VK
2234 return rc;
2235}
2236
e00243fa 2237int wmi_pcp_stop(struct wil6210_vif *vif)
b8023177 2238{
e00243fa 2239 struct wil6210_priv *wil = vif_to_wil(vif);
10d599ad
ME
2240 int rc;
2241
2242 rc = wmi_led_cfg(wil, false);
2243 if (rc)
2244 return rc;
2245
e00243fa 2246 return wmi_call(wil, WMI_PCP_STOP_CMDID, vif->mid, NULL, 0,
5793fe9d
ME
2247 WMI_PCP_STOPPED_EVENTID, NULL, 0,
2248 WIL_WMI_PCP_STOP_TO_MS);
2be7d22f
VK
2249}
2250
e00243fa 2251int wmi_set_ssid(struct wil6210_vif *vif, u8 ssid_len, const void *ssid)
2be7d22f 2252{
e00243fa 2253 struct wil6210_priv *wil = vif_to_wil(vif);
2be7d22f
VK
2254 struct wmi_set_ssid_cmd cmd = {
2255 .ssid_len = cpu_to_le32(ssid_len),
2256 };
2257
2258 if (ssid_len > sizeof(cmd.ssid))
2259 return -EINVAL;
2260
2261 memcpy(cmd.ssid, ssid, ssid_len);
2262
e00243fa 2263 return wmi_send(wil, WMI_SET_SSID_CMDID, vif->mid, &cmd, sizeof(cmd));
2be7d22f
VK
2264}
2265
e00243fa 2266int wmi_get_ssid(struct wil6210_vif *vif, u8 *ssid_len, void *ssid)
2be7d22f 2267{
e00243fa 2268 struct wil6210_priv *wil = vif_to_wil(vif);
2be7d22f
VK
2269 int rc;
2270 struct {
b874ddec 2271 struct wmi_cmd_hdr wmi;
2be7d22f
VK
2272 struct wmi_set_ssid_cmd cmd;
2273 } __packed reply;
2274 int len; /* reply.cmd.ssid_len in CPU order */
2275
807b0860
AAL
2276 memset(&reply, 0, sizeof(reply));
2277
e00243fa 2278 rc = wmi_call(wil, WMI_GET_SSID_CMDID, vif->mid, NULL, 0,
2a32c20b
AM
2279 WMI_GET_SSID_EVENTID, &reply, sizeof(reply),
2280 WIL_WMI_CALL_GENERAL_TO_MS);
2be7d22f
VK
2281 if (rc)
2282 return rc;
2283
2284 len = le32_to_cpu(reply.cmd.ssid_len);
2285 if (len > sizeof(reply.cmd.ssid))
2286 return -EINVAL;
2287
2288 *ssid_len = len;
2289 memcpy(ssid, reply.cmd.ssid, len);
2290
2291 return 0;
2292}
2293
2294int wmi_set_channel(struct wil6210_priv *wil, int channel)
2295{
e00243fa 2296 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2be7d22f
VK
2297 struct wmi_set_pcp_channel_cmd cmd = {
2298 .channel = channel - 1,
2299 };
2300
e00243fa
LD
2301 return wmi_send(wil, WMI_SET_PCP_CHANNEL_CMDID, vif->mid,
2302 &cmd, sizeof(cmd));
2be7d22f
VK
2303}
2304
2305int wmi_get_channel(struct wil6210_priv *wil, int *channel)
2306{
e00243fa 2307 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2be7d22f
VK
2308 int rc;
2309 struct {
b874ddec 2310 struct wmi_cmd_hdr wmi;
2be7d22f
VK
2311 struct wmi_set_pcp_channel_cmd cmd;
2312 } __packed reply;
2313
807b0860
AAL
2314 memset(&reply, 0, sizeof(reply));
2315
e00243fa 2316 rc = wmi_call(wil, WMI_GET_PCP_CHANNEL_CMDID, vif->mid, NULL, 0,
2a32c20b
AM
2317 WMI_GET_PCP_CHANNEL_EVENTID, &reply, sizeof(reply),
2318 WIL_WMI_CALL_GENERAL_TO_MS);
2be7d22f
VK
2319 if (rc)
2320 return rc;
2321
2322 if (reply.cmd.channel > 3)
2323 return -EINVAL;
2324
2325 *channel = reply.cmd.channel + 1;
2326
2327 return 0;
2328}
2329
e00243fa 2330int wmi_p2p_cfg(struct wil6210_vif *vif, int channel, int bi)
b8023177 2331{
e00243fa 2332 struct wil6210_priv *wil = vif_to_wil(vif);
e6d68341 2333 int rc;
b8023177 2334 struct wmi_p2p_cfg_cmd cmd = {
e6d68341
DL
2335 .discovery_mode = WMI_DISCOVERY_MODE_PEER2PEER,
2336 .bcon_interval = cpu_to_le16(bi),
b8023177
VK
2337 .channel = channel - 1,
2338 };
e6d68341
DL
2339 struct {
2340 struct wmi_cmd_hdr wmi;
2341 struct wmi_p2p_cfg_done_event evt;
807b0860
AAL
2342 } __packed reply = {
2343 .evt = {.status = WMI_FW_STATUS_FAILURE},
2344 };
e6d68341
DL
2345
2346 wil_dbg_wmi(wil, "sending WMI_P2P_CFG_CMDID\n");
2347
e00243fa 2348 rc = wmi_call(wil, WMI_P2P_CFG_CMDID, vif->mid, &cmd, sizeof(cmd),
e6d68341
DL
2349 WMI_P2P_CFG_DONE_EVENTID, &reply, sizeof(reply), 300);
2350 if (!rc && reply.evt.status != WMI_FW_STATUS_SUCCESS) {
2351 wil_err(wil, "P2P_CFG failed. status %d\n", reply.evt.status);
2352 rc = -EINVAL;
2353 }
2354
2355 return rc;
2356}
2357
e00243fa 2358int wmi_start_listen(struct wil6210_vif *vif)
e6d68341 2359{
e00243fa 2360 struct wil6210_priv *wil = vif_to_wil(vif);
e6d68341
DL
2361 int rc;
2362 struct {
2363 struct wmi_cmd_hdr wmi;
2364 struct wmi_listen_started_event evt;
807b0860
AAL
2365 } __packed reply = {
2366 .evt = {.status = WMI_FW_STATUS_FAILURE},
2367 };
e6d68341
DL
2368
2369 wil_dbg_wmi(wil, "sending WMI_START_LISTEN_CMDID\n");
2370
e00243fa 2371 rc = wmi_call(wil, WMI_START_LISTEN_CMDID, vif->mid, NULL, 0,
e6d68341
DL
2372 WMI_LISTEN_STARTED_EVENTID, &reply, sizeof(reply), 300);
2373 if (!rc && reply.evt.status != WMI_FW_STATUS_SUCCESS) {
2374 wil_err(wil, "device failed to start listen. status %d\n",
2375 reply.evt.status);
2376 rc = -EINVAL;
2377 }
b8023177 2378
e6d68341
DL
2379 return rc;
2380}
2381
e00243fa 2382int wmi_start_search(struct wil6210_vif *vif)
e6d68341 2383{
e00243fa 2384 struct wil6210_priv *wil = vif_to_wil(vif);
e6d68341
DL
2385 int rc;
2386 struct {
2387 struct wmi_cmd_hdr wmi;
2388 struct wmi_search_started_event evt;
807b0860
AAL
2389 } __packed reply = {
2390 .evt = {.status = WMI_FW_STATUS_FAILURE},
2391 };
e6d68341
DL
2392
2393 wil_dbg_wmi(wil, "sending WMI_START_SEARCH_CMDID\n");
2394
e00243fa 2395 rc = wmi_call(wil, WMI_START_SEARCH_CMDID, vif->mid, NULL, 0,
e6d68341
DL
2396 WMI_SEARCH_STARTED_EVENTID, &reply, sizeof(reply), 300);
2397 if (!rc && reply.evt.status != WMI_FW_STATUS_SUCCESS) {
2398 wil_err(wil, "device failed to start search. status %d\n",
2399 reply.evt.status);
2400 rc = -EINVAL;
2401 }
2402
2403 return rc;
2404}
2405
e00243fa 2406int wmi_stop_discovery(struct wil6210_vif *vif)
e6d68341 2407{
e00243fa 2408 struct wil6210_priv *wil = vif_to_wil(vif);
e6d68341
DL
2409 int rc;
2410
2411 wil_dbg_wmi(wil, "sending WMI_DISCOVERY_STOP_CMDID\n");
2412
e00243fa 2413 rc = wmi_call(wil, WMI_DISCOVERY_STOP_CMDID, vif->mid, NULL, 0,
2a32c20b
AM
2414 WMI_DISCOVERY_STOPPED_EVENTID, NULL, 0,
2415 WIL_WMI_CALL_GENERAL_TO_MS);
e6d68341
DL
2416
2417 if (rc)
2418 wil_err(wil, "Failed to stop discovery\n");
2419
2420 return rc;
b8023177
VK
2421}
2422
e00243fa 2423int wmi_del_cipher_key(struct wil6210_vif *vif, u8 key_index,
230d8442 2424 const void *mac_addr, int key_usage)
2be7d22f 2425{
e00243fa 2426 struct wil6210_priv *wil = vif_to_wil(vif);
2be7d22f
VK
2427 struct wmi_delete_cipher_key_cmd cmd = {
2428 .key_index = key_index,
2429 };
2430
2431 if (mac_addr)
2432 memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
2433
e00243fa
LD
2434 return wmi_send(wil, WMI_DELETE_CIPHER_KEY_CMDID, vif->mid,
2435 &cmd, sizeof(cmd));
2be7d22f
VK
2436}
2437
e00243fa 2438int wmi_add_cipher_key(struct wil6210_vif *vif, u8 key_index,
230d8442
VK
2439 const void *mac_addr, int key_len, const void *key,
2440 int key_usage)
2be7d22f 2441{
e00243fa 2442 struct wil6210_priv *wil = vif_to_wil(vif);
2be7d22f
VK
2443 struct wmi_add_cipher_key_cmd cmd = {
2444 .key_index = key_index,
230d8442 2445 .key_usage = key_usage,
2be7d22f
VK
2446 .key_len = key_len,
2447 };
2448
42fe1e51 2449 if (key_len > sizeof(cmd.key))
2be7d22f
VK
2450 return -EINVAL;
2451
42fe1e51
AM
2452 /* key len = 0 is allowed only for usage of WMI_KEY_USE_APPLY */
2453 if ((key_len == 0 || !key) &&
2454 key_usage != WMI_KEY_USE_APPLY_PTK)
2455 return -EINVAL;
2456
2457 if (key)
2458 memcpy(cmd.key, key, key_len);
2459
2be7d22f
VK
2460 if (mac_addr)
2461 memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
2462
e00243fa
LD
2463 return wmi_send(wil, WMI_ADD_CIPHER_KEY_CMDID, vif->mid,
2464 &cmd, sizeof(cmd));
2be7d22f
VK
2465}
2466
e00243fa 2467int wmi_set_ie(struct wil6210_vif *vif, u8 type, u16 ie_len, const void *ie)
2be7d22f 2468{
e00243fa 2469 struct wil6210_priv *wil = vif_to_wil(vif);
5421bf0c
VK
2470 static const char *const names[] = {
2471 [WMI_FRAME_BEACON] = "BEACON",
2472 [WMI_FRAME_PROBE_REQ] = "PROBE_REQ",
2473 [WMI_FRAME_PROBE_RESP] = "WMI_FRAME_PROBE_RESP",
2474 [WMI_FRAME_ASSOC_REQ] = "WMI_FRAME_ASSOC_REQ",
2475 [WMI_FRAME_ASSOC_RESP] = "WMI_FRAME_ASSOC_RESP",
2476 };
2be7d22f
VK
2477 int rc;
2478 u16 len = sizeof(struct wmi_set_appie_cmd) + ie_len;
b5a8ffca 2479 struct wmi_set_appie_cmd *cmd;
8fe59627 2480
b5a8ffca
LD
2481 if (len < ie_len) {
2482 rc = -EINVAL;
2483 goto out;
2484 }
2485
2486 cmd = kzalloc(len, GFP_KERNEL);
5421bf0c
VK
2487 if (!cmd) {
2488 rc = -ENOMEM;
2489 goto out;
2490 }
ac4acdb7
VK
2491 if (!ie)
2492 ie_len = 0;
2be7d22f
VK
2493
2494 cmd->mgmt_frm_type = type;
2495 /* BUG: FW API define ieLen as u8. Will fix FW */
2496 cmd->ie_len = cpu_to_le16(ie_len);
2c840676
DE
2497 if (ie_len)
2498 memcpy(cmd->ie_info, ie, ie_len);
e00243fa 2499 rc = wmi_send(wil, WMI_SET_APPIE_CMDID, vif->mid, cmd, len);
2be7d22f 2500 kfree(cmd);
5421bf0c
VK
2501out:
2502 if (rc) {
2503 const char *name = type < ARRAY_SIZE(names) ?
2504 names[type] : "??";
2505 wil_err(wil, "set_ie(%d %s) failed : %d\n", type, name, rc);
2506 }
2be7d22f
VK
2507
2508 return rc;
2509}
2510
b9010f10
AM
2511int wmi_update_ft_ies(struct wil6210_vif *vif, u16 ie_len, const void *ie)
2512{
2513 struct wil6210_priv *wil = vif_to_wil(vif);
2514 u16 len;
2515 struct wmi_update_ft_ies_cmd *cmd;
2516 int rc;
2517
2518 if (!ie)
2519 ie_len = 0;
2520
2521 len = sizeof(struct wmi_update_ft_ies_cmd) + ie_len;
2522 if (len < ie_len) {
2523 wil_err(wil, "wraparound. ie len %d\n", ie_len);
2524 return -EINVAL;
2525 }
2526
2527 cmd = kzalloc(len, GFP_KERNEL);
2528 if (!cmd) {
2529 rc = -ENOMEM;
2530 goto out;
2531 }
2532
2533 cmd->ie_len = cpu_to_le16(ie_len);
2c840676
DE
2534 if (ie_len)
2535 memcpy(cmd->ie_info, ie, ie_len);
b9010f10
AM
2536 rc = wmi_send(wil, WMI_UPDATE_FT_IES_CMDID, vif->mid, cmd, len);
2537 kfree(cmd);
2538
2539out:
2540 if (rc)
2541 wil_err(wil, "update ft ies failed : %d\n", rc);
2542
2543 return rc;
2544}
2545
1647f12f
VK
2546/**
2547 * wmi_rxon - turn radio on/off
2548 * @on: turn on if true, off otherwise
2549 *
2550 * Only switch radio. Channel should be set separately.
2551 * No timeout for rxon - radio turned on forever unless some other call
2552 * turns it off
2553 */
2554int wmi_rxon(struct wil6210_priv *wil, bool on)
2555{
e00243fa 2556 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1647f12f
VK
2557 int rc;
2558 struct {
b874ddec 2559 struct wmi_cmd_hdr wmi;
1647f12f 2560 struct wmi_listen_started_event evt;
807b0860
AAL
2561 } __packed reply = {
2562 .evt = {.status = WMI_FW_STATUS_FAILURE},
2563 };
1647f12f 2564
af3db60a 2565 wil_info(wil, "(%s)\n", on ? "on" : "off");
1647f12f
VK
2566
2567 if (on) {
e00243fa 2568 rc = wmi_call(wil, WMI_START_LISTEN_CMDID, vif->mid, NULL, 0,
1647f12f 2569 WMI_LISTEN_STARTED_EVENTID,
2a32c20b
AM
2570 &reply, sizeof(reply),
2571 WIL_WMI_CALL_GENERAL_TO_MS);
1647f12f
VK
2572 if ((rc == 0) && (reply.evt.status != WMI_FW_STATUS_SUCCESS))
2573 rc = -EINVAL;
2574 } else {
e00243fa 2575 rc = wmi_call(wil, WMI_DISCOVERY_STOP_CMDID, vif->mid, NULL, 0,
2a32c20b
AM
2576 WMI_DISCOVERY_STOPPED_EVENTID, NULL, 0,
2577 WIL_WMI_CALL_GENERAL_TO_MS);
1647f12f
VK
2578 }
2579
2580 return rc;
2581}
2582
10590c6a 2583int wmi_rx_chain_add(struct wil6210_priv *wil, struct wil_ring *vring)
47e19af9 2584{
e00243fa
LD
2585 struct net_device *ndev = wil->main_ndev;
2586 struct wireless_dev *wdev = ndev->ieee80211_ptr;
2587 struct wil6210_vif *vif = ndev_to_vif(ndev);
47e19af9
VK
2588 struct wmi_cfg_rx_chain_cmd cmd = {
2589 .action = WMI_RX_CHAIN_ADD,
2590 .rx_sw_ring = {
52a45702
LD
2591 .max_mpdu_size = cpu_to_le16(
2592 wil_mtu2macbuf(wil->rx_buf_len)),
47e19af9
VK
2593 .ring_mem_base = cpu_to_le64(vring->pa),
2594 .ring_size = cpu_to_le16(vring->size),
2595 },
2596 .mid = 0, /* TODO - what is it? */
2597 .decap_trans_type = WMI_DECAP_TYPE_802_3,
b4490f42 2598 .reorder_type = WMI_RX_SW_REORDER,
ab954628 2599 .host_thrsh = cpu_to_le16(rx_ring_overflow_thrsh),
47e19af9
VK
2600 };
2601 struct {
b874ddec 2602 struct wmi_cmd_hdr wmi;
47e19af9
VK
2603 struct wmi_cfg_rx_chain_done_event evt;
2604 } __packed evt;
2605 int rc;
2606
807b0860
AAL
2607 memset(&evt, 0, sizeof(evt));
2608
47e19af9 2609 if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
7d3e4dbe 2610 struct ieee80211_channel *ch = wil->monitor_chandef.chan;
47e19af9
VK
2611
2612 cmd.sniffer_cfg.mode = cpu_to_le32(WMI_SNIFFER_ON);
2613 if (ch)
2614 cmd.sniffer_cfg.channel = ch->hw_value - 1;
2615 cmd.sniffer_cfg.phy_info_mode =
387f3794 2616 cpu_to_le32(WMI_SNIFFER_PHY_INFO_DISABLED);
47e19af9
VK
2617 cmd.sniffer_cfg.phy_support =
2618 cpu_to_le32((wil->monitor_flags & MONITOR_FLAG_CONTROL)
4765332d 2619 ? WMI_SNIFFER_CP : WMI_SNIFFER_BOTH_PHYS);
504937d4
KE
2620 } else {
2621 /* Initialize offload (in non-sniffer mode).
2622 * Linux IP stack always calculates IP checksum
2623 * HW always calculate TCP/UDP checksum
2624 */
2625 cmd.l3_l4_ctrl |= (1 << L3_L4_CTRL_TCPIP_CHECKSUM_EN_POS);
47e19af9 2626 }
c406ea7c
VK
2627
2628 if (rx_align_2)
2629 cmd.l2_802_3_offload_ctrl |=
2630 L2_802_3_OFFLOAD_CTRL_SNAP_KEEP_MSK;
2631
47e19af9 2632 /* typical time for secure PCP is 840ms */
e00243fa 2633 rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, vif->mid, &cmd, sizeof(cmd),
47e19af9
VK
2634 WMI_CFG_RX_CHAIN_DONE_EVENTID, &evt, sizeof(evt), 2000);
2635 if (rc)
2636 return rc;
2637
807b0860
AAL
2638 if (le32_to_cpu(evt.evt.status) != WMI_CFG_RX_CHAIN_SUCCESS)
2639 rc = -EINVAL;
2640
47e19af9
VK
2641 vring->hwtail = le32_to_cpu(evt.evt.rx_ring_tail_ptr);
2642
7743882d 2643 wil_dbg_misc(wil, "Rx init: status %d tail 0x%08x\n",
47e19af9
VK
2644 le32_to_cpu(evt.evt.status), vring->hwtail);
2645
47e19af9
VK
2646 return rc;
2647}
2648
8c679675 2649int wmi_get_temperature(struct wil6210_priv *wil, u32 *t_bb, u32 *t_rf)
1a2780e0 2650{
e00243fa 2651 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1a2780e0
VK
2652 int rc;
2653 struct wmi_temp_sense_cmd cmd = {
8c679675
VK
2654 .measure_baseband_en = cpu_to_le32(!!t_bb),
2655 .measure_rf_en = cpu_to_le32(!!t_rf),
2656 .measure_mode = cpu_to_le32(TEMPERATURE_MEASURE_NOW),
1a2780e0
VK
2657 };
2658 struct {
b874ddec 2659 struct wmi_cmd_hdr wmi;
1a2780e0
VK
2660 struct wmi_temp_sense_done_event evt;
2661 } __packed reply;
2662
807b0860
AAL
2663 memset(&reply, 0, sizeof(reply));
2664
e00243fa 2665 rc = wmi_call(wil, WMI_TEMP_SENSE_CMDID, vif->mid, &cmd, sizeof(cmd),
2a32c20b
AM
2666 WMI_TEMP_SENSE_DONE_EVENTID, &reply, sizeof(reply),
2667 WIL_WMI_CALL_GENERAL_TO_MS);
1a2780e0
VK
2668 if (rc)
2669 return rc;
2670
8c679675
VK
2671 if (t_bb)
2672 *t_bb = le32_to_cpu(reply.evt.baseband_t1000);
2673 if (t_rf)
2674 *t_rf = le32_to_cpu(reply.evt.rf_t1000);
1a2780e0
VK
2675
2676 return 0;
2677}
2678
9b586118
TS
2679int wmi_get_all_temperatures(struct wil6210_priv *wil,
2680 struct wmi_temp_sense_all_done_event
2681 *sense_all_evt)
2682{
2683 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2684 int rc;
2685 struct wmi_temp_sense_all_cmd cmd = {
2686 .measure_baseband_en = true,
2687 .measure_rf_en = true,
2688 .measure_mode = TEMPERATURE_MEASURE_NOW,
2689 };
2690 struct {
2691 struct wmi_cmd_hdr wmi;
2692 struct wmi_temp_sense_all_done_event evt;
2693 } __packed reply;
2694
2695 if (!sense_all_evt) {
2696 wil_err(wil, "Invalid sense_all_evt value\n");
2697 return -EINVAL;
2698 }
2699
2700 memset(&reply, 0, sizeof(reply));
2701 reply.evt.status = WMI_FW_STATUS_FAILURE;
2702 rc = wmi_call(wil, WMI_TEMP_SENSE_ALL_CMDID, vif->mid, &cmd,
2703 sizeof(cmd), WMI_TEMP_SENSE_ALL_DONE_EVENTID,
2704 &reply, sizeof(reply), WIL_WMI_CALL_GENERAL_TO_MS);
2705 if (rc)
2706 return rc;
2707
2708 if (reply.evt.status == WMI_FW_STATUS_FAILURE) {
80ce8ca7 2709 wil_err(wil, "Failed getting TEMP_SENSE_ALL\n");
9b586118
TS
2710 return -EINVAL;
2711 }
2712
2713 memcpy(sense_all_evt, &reply.evt, sizeof(reply.evt));
2714 return 0;
2715}
2716
e1b43407
AM
2717int wmi_disconnect_sta(struct wil6210_vif *vif, const u8 *mac, u16 reason,
2718 bool del_sta)
4d55a0a1 2719{
e00243fa 2720 struct wil6210_priv *wil = vif_to_wil(vif);
3e9191fc 2721 int rc;
849a564b
DL
2722 struct wmi_disconnect_sta_cmd disc_sta_cmd = {
2723 .disconnect_reason = cpu_to_le16(reason),
2724 };
2725 struct wmi_del_sta_cmd del_sta_cmd = {
4d55a0a1
VK
2726 .disconnect_reason = cpu_to_le16(reason),
2727 };
3e9191fc 2728 struct {
b874ddec 2729 struct wmi_cmd_hdr wmi;
3e9191fc
VK
2730 struct wmi_disconnect_event evt;
2731 } __packed reply;
a82553bb 2732
af3db60a 2733 wil_dbg_wmi(wil, "disconnect_sta: (%pM, reason %d)\n", mac, reason);
4d55a0a1 2734
807b0860 2735 memset(&reply, 0, sizeof(reply));
e00243fa 2736 vif->locally_generated_disc = true;
849a564b
DL
2737 if (del_sta) {
2738 ether_addr_copy(del_sta_cmd.dst_mac, mac);
e00243fa 2739 rc = wmi_call(wil, WMI_DEL_STA_CMDID, vif->mid, &del_sta_cmd,
849a564b
DL
2740 sizeof(del_sta_cmd), WMI_DISCONNECT_EVENTID,
2741 &reply, sizeof(reply), 1000);
2742 } else {
2743 ether_addr_copy(disc_sta_cmd.dst_mac, mac);
e00243fa
LD
2744 rc = wmi_call(wil, WMI_DISCONNECT_STA_CMDID, vif->mid,
2745 &disc_sta_cmd, sizeof(disc_sta_cmd),
2746 WMI_DISCONNECT_EVENTID,
849a564b
DL
2747 &reply, sizeof(reply), 1000);
2748 }
3e9191fc
VK
2749 /* failure to disconnect in reasonable time treated as FW error */
2750 if (rc) {
2751 wil_fw_error_recovery(wil);
2752 return rc;
2753 }
e1b43407 2754 wil->sinfo_gen++;
3e9191fc 2755
3e9191fc 2756 return 0;
4d55a0a1
VK
2757}
2758
e00243fa
LD
2759int wmi_addba(struct wil6210_priv *wil, u8 mid,
2760 u8 ringid, u8 size, u16 timeout)
3277213f 2761{
7be13fc3
GS
2762 u8 amsdu = wil->use_enhanced_dma_hw && wil->use_rx_hw_reordering &&
2763 test_bit(WMI_FW_CAPABILITY_AMSDU, wil->fw_capabilities) &&
2764 wil->amsdu_en;
96c93589
GS
2765 struct wmi_ring_ba_en_cmd cmd = {
2766 .ring_id = ringid,
3277213f
VK
2767 .agg_max_wsize = size,
2768 .ba_timeout = cpu_to_le16(timeout),
7be13fc3 2769 .amsdu = amsdu,
3277213f
VK
2770 };
2771
7be13fc3
GS
2772 wil_dbg_wmi(wil, "addba: (ring %d size %d timeout %d amsdu %d)\n",
2773 ringid, size, timeout, amsdu);
3277213f 2774
96c93589 2775 return wmi_send(wil, WMI_RING_BA_EN_CMDID, mid, &cmd, sizeof(cmd));
3277213f
VK
2776}
2777
e00243fa 2778int wmi_delba_tx(struct wil6210_priv *wil, u8 mid, u8 ringid, u16 reason)
3277213f 2779{
96c93589
GS
2780 struct wmi_ring_ba_dis_cmd cmd = {
2781 .ring_id = ringid,
3277213f
VK
2782 .reason = cpu_to_le16(reason),
2783 };
2784
af3db60a 2785 wil_dbg_wmi(wil, "delba_tx: (ring %d reason %d)\n", ringid, reason);
3277213f 2786
96c93589 2787 return wmi_send(wil, WMI_RING_BA_DIS_CMDID, mid, &cmd, sizeof(cmd));
3277213f
VK
2788}
2789
bf0353a6 2790int wmi_delba_rx(struct wil6210_priv *wil, u8 mid, u8 cid, u8 tid, u16 reason)
26a359d9
VK
2791{
2792 struct wmi_rcp_delba_cmd cmd = {
26a359d9
VK
2793 .reason = cpu_to_le16(reason),
2794 };
2795
bf0353a6
AM
2796 if (cid >= WIL6210_RX_DESC_MAX_CID) {
2797 cmd.cidxtid = CIDXTID_EXTENDED_CID_TID;
2798 cmd.cid = cid;
2799 cmd.tid = tid;
2800 } else {
2801 cmd.cidxtid = mk_cidxtid(cid, tid);
2802 }
2803
2804 wil_dbg_wmi(wil, "delba_rx: (CID %d TID %d reason %d)\n", cid,
2805 tid, reason);
26a359d9 2806
e00243fa 2807 return wmi_send(wil, WMI_RCP_DELBA_CMDID, mid, &cmd, sizeof(cmd));
26a359d9
VK
2808}
2809
e00243fa
LD
2810int wmi_addba_rx_resp(struct wil6210_priv *wil,
2811 u8 mid, u8 cid, u8 tid, u8 token,
3277213f
VK
2812 u16 status, bool amsdu, u16 agg_wsize, u16 timeout)
2813{
2814 int rc;
2815 struct wmi_rcp_addba_resp_cmd cmd = {
3277213f
VK
2816 .dialog_token = token,
2817 .status_code = cpu_to_le16(status),
2818 /* bit 0: A-MSDU supported
c5b3a658 2819 * bit 1: policy (controlled by FW)
3277213f
VK
2820 * bits 2..5: TID
2821 * bits 6..15: buffer size
2822 */
2823 .ba_param_set = cpu_to_le16((amsdu ? 1 : 0) | (tid << 2) |
2824 (agg_wsize << 6)),
2825 .ba_timeout = cpu_to_le16(timeout),
2826 };
2827 struct {
b874ddec 2828 struct wmi_cmd_hdr wmi;
3277213f 2829 struct wmi_rcp_addba_resp_sent_event evt;
807b0860
AAL
2830 } __packed reply = {
2831 .evt = {.status = cpu_to_le16(WMI_FW_STATUS_FAILURE)},
2832 };
3277213f 2833
bf0353a6
AM
2834 if (cid >= WIL6210_RX_DESC_MAX_CID) {
2835 cmd.cidxtid = CIDXTID_EXTENDED_CID_TID;
2836 cmd.cid = cid;
2837 cmd.tid = tid;
2838 } else {
2839 cmd.cidxtid = mk_cidxtid(cid, tid);
2840 }
2841
3277213f 2842 wil_dbg_wmi(wil,
e00243fa
LD
2843 "ADDBA response for MID %d CID %d TID %d size %d timeout %d status %d AMSDU%s\n",
2844 mid, cid, tid, agg_wsize,
2845 timeout, status, amsdu ? "+" : "-");
3277213f 2846
e00243fa 2847 rc = wmi_call(wil, WMI_RCP_ADDBA_RESP_CMDID, mid, &cmd, sizeof(cmd),
230d8442 2848 WMI_RCP_ADDBA_RESP_SENT_EVENTID, &reply, sizeof(reply),
2a32c20b 2849 WIL_WMI_CALL_GENERAL_TO_MS);
3277213f
VK
2850 if (rc)
2851 return rc;
2852
2853 if (reply.evt.status) {
2854 wil_err(wil, "ADDBA response failed with status %d\n",
2855 le16_to_cpu(reply.evt.status));
2856 rc = -EINVAL;
2857 }
2858
2859 return rc;
2860}
2861
7be13fc3
GS
2862int wmi_addba_rx_resp_edma(struct wil6210_priv *wil, u8 mid, u8 cid, u8 tid,
2863 u8 token, u16 status, bool amsdu, u16 agg_wsize,
2864 u16 timeout)
2865{
2866 int rc;
2867 struct wmi_rcp_addba_resp_edma_cmd cmd = {
2868 .cid = cid,
2869 .tid = tid,
2870 .dialog_token = token,
2871 .status_code = cpu_to_le16(status),
2872 /* bit 0: A-MSDU supported
c5b3a658 2873 * bit 1: policy (controlled by FW)
7be13fc3
GS
2874 * bits 2..5: TID
2875 * bits 6..15: buffer size
2876 */
2877 .ba_param_set = cpu_to_le16((amsdu ? 1 : 0) | (tid << 2) |
2878 (agg_wsize << 6)),
2879 .ba_timeout = cpu_to_le16(timeout),
2880 /* route all the connections to status ring 0 */
2881 .status_ring_id = WIL_DEFAULT_RX_STATUS_RING_ID,
2882 };
2883 struct {
2884 struct wmi_cmd_hdr wmi;
2885 struct wmi_rcp_addba_resp_sent_event evt;
2886 } __packed reply = {
2887 .evt = {.status = cpu_to_le16(WMI_FW_STATUS_FAILURE)},
2888 };
2889
2890 wil_dbg_wmi(wil,
2891 "ADDBA response for CID %d TID %d size %d timeout %d status %d AMSDU%s, sring_id %d\n",
2892 cid, tid, agg_wsize, timeout, status, amsdu ? "+" : "-",
2893 WIL_DEFAULT_RX_STATUS_RING_ID);
2894
2895 rc = wmi_call(wil, WMI_RCP_ADDBA_RESP_EDMA_CMDID, mid, &cmd,
2896 sizeof(cmd), WMI_RCP_ADDBA_RESP_SENT_EVENTID, &reply,
2897 sizeof(reply), WIL_WMI_CALL_GENERAL_TO_MS);
2898 if (rc)
2899 return rc;
2900
2901 if (reply.evt.status) {
2902 wil_err(wil, "ADDBA response failed with status %d\n",
2903 le16_to_cpu(reply.evt.status));
2904 rc = -EINVAL;
2905 }
2906
2907 return rc;
2908}
2909
2c207eb8
ME
2910int wmi_ps_dev_profile_cfg(struct wil6210_priv *wil,
2911 enum wmi_ps_profile_type ps_profile)
2912{
e00243fa 2913 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2c207eb8
ME
2914 int rc;
2915 struct wmi_ps_dev_profile_cfg_cmd cmd = {
2916 .ps_profile = ps_profile,
2917 };
2918 struct {
2919 struct wmi_cmd_hdr wmi;
2920 struct wmi_ps_dev_profile_cfg_event evt;
807b0860
AAL
2921 } __packed reply = {
2922 .evt = {.status = cpu_to_le32(WMI_PS_CFG_CMD_STATUS_ERROR)},
2923 };
2c207eb8
ME
2924 u32 status;
2925
2926 wil_dbg_wmi(wil, "Setting ps dev profile %d\n", ps_profile);
2927
e00243fa
LD
2928 rc = wmi_call(wil, WMI_PS_DEV_PROFILE_CFG_CMDID, vif->mid,
2929 &cmd, sizeof(cmd),
2c207eb8 2930 WMI_PS_DEV_PROFILE_CFG_EVENTID, &reply, sizeof(reply),
2a32c20b 2931 WIL_WMI_CALL_GENERAL_TO_MS);
2c207eb8
ME
2932 if (rc)
2933 return rc;
2934
2935 status = le32_to_cpu(reply.evt.status);
2936
2937 if (status != WMI_PS_CFG_CMD_STATUS_SUCCESS) {
2938 wil_err(wil, "ps dev profile cfg failed with status %d\n",
2939 status);
2940 rc = -EINVAL;
2941 }
2942
2943 return rc;
2944}
2945
3fea18d0
LD
2946int wmi_set_mgmt_retry(struct wil6210_priv *wil, u8 retry_short)
2947{
e00243fa 2948 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
3fea18d0
LD
2949 int rc;
2950 struct wmi_set_mgmt_retry_limit_cmd cmd = {
2951 .mgmt_retry_limit = retry_short,
2952 };
2953 struct {
2954 struct wmi_cmd_hdr wmi;
2955 struct wmi_set_mgmt_retry_limit_event evt;
807b0860
AAL
2956 } __packed reply = {
2957 .evt = {.status = WMI_FW_STATUS_FAILURE},
2958 };
3fea18d0
LD
2959
2960 wil_dbg_wmi(wil, "Setting mgmt retry short %d\n", retry_short);
2961
2962 if (!test_bit(WMI_FW_CAPABILITY_MGMT_RETRY_LIMIT, wil->fw_capabilities))
2963 return -ENOTSUPP;
2964
e00243fa
LD
2965 rc = wmi_call(wil, WMI_SET_MGMT_RETRY_LIMIT_CMDID, vif->mid,
2966 &cmd, sizeof(cmd),
3fea18d0 2967 WMI_SET_MGMT_RETRY_LIMIT_EVENTID, &reply, sizeof(reply),
2a32c20b 2968 WIL_WMI_CALL_GENERAL_TO_MS);
3fea18d0
LD
2969 if (rc)
2970 return rc;
2971
2972 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
2973 wil_err(wil, "set mgmt retry limit failed with status %d\n",
2974 reply.evt.status);
2975 rc = -EINVAL;
2976 }
2977
2978 return rc;
2979}
2980
2981int wmi_get_mgmt_retry(struct wil6210_priv *wil, u8 *retry_short)
2982{
e00243fa 2983 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
3fea18d0
LD
2984 int rc;
2985 struct {
2986 struct wmi_cmd_hdr wmi;
2987 struct wmi_get_mgmt_retry_limit_event evt;
2988 } __packed reply;
2989
2990 wil_dbg_wmi(wil, "getting mgmt retry short\n");
2991
2992 if (!test_bit(WMI_FW_CAPABILITY_MGMT_RETRY_LIMIT, wil->fw_capabilities))
2993 return -ENOTSUPP;
2994
807b0860 2995 memset(&reply, 0, sizeof(reply));
e00243fa 2996 rc = wmi_call(wil, WMI_GET_MGMT_RETRY_LIMIT_CMDID, vif->mid, NULL, 0,
3fea18d0 2997 WMI_GET_MGMT_RETRY_LIMIT_EVENTID, &reply, sizeof(reply),
2a32c20b 2998 WIL_WMI_CALL_GENERAL_TO_MS);
3fea18d0
LD
2999 if (rc)
3000 return rc;
3001
3002 if (retry_short)
3003 *retry_short = reply.evt.mgmt_retry_limit;
3004
3005 return 0;
3006}
3007
e00243fa 3008int wmi_abort_scan(struct wil6210_vif *vif)
035859a5 3009{
e00243fa 3010 struct wil6210_priv *wil = vif_to_wil(vif);
035859a5
ME
3011 int rc;
3012
3013 wil_dbg_wmi(wil, "sending WMI_ABORT_SCAN_CMDID\n");
3014
e00243fa 3015 rc = wmi_send(wil, WMI_ABORT_SCAN_CMDID, vif->mid, NULL, 0);
035859a5
ME
3016 if (rc)
3017 wil_err(wil, "Failed to abort scan (%d)\n", rc);
3018
3019 return rc;
3020}
3021
e00243fa 3022int wmi_new_sta(struct wil6210_vif *vif, const u8 *mac, u8 aid)
849a564b 3023{
e00243fa 3024 struct wil6210_priv *wil = vif_to_wil(vif);
849a564b
DL
3025 int rc;
3026 struct wmi_new_sta_cmd cmd = {
3027 .aid = aid,
3028 };
3029
3030 wil_dbg_wmi(wil, "new sta %pM, aid %d\n", mac, aid);
3031
3032 ether_addr_copy(cmd.dst_mac, mac);
3033
e00243fa 3034 rc = wmi_send(wil, WMI_NEW_STA_CMDID, vif->mid, &cmd, sizeof(cmd));
849a564b
DL
3035 if (rc)
3036 wil_err(wil, "Failed to send new sta (%d)\n", rc);
3037
3038 return rc;
3039}
3040
2be7d22f
VK
3041void wmi_event_flush(struct wil6210_priv *wil)
3042{
f6b29b65 3043 ulong flags;
2be7d22f
VK
3044 struct pending_wmi_event *evt, *t;
3045
af3db60a 3046 wil_dbg_wmi(wil, "event_flush\n");
2be7d22f 3047
f6b29b65
HK
3048 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
3049
2be7d22f
VK
3050 list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
3051 list_del(&evt->list);
3052 kfree(evt);
3053 }
f6b29b65
HK
3054
3055 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
2be7d22f
VK
3056}
3057
6804cd10
LA
3058static const char *suspend_status2name(u8 status)
3059{
3060 switch (status) {
3061 case WMI_TRAFFIC_SUSPEND_REJECTED_LINK_NOT_IDLE:
3062 return "LINK_NOT_IDLE";
73a7d1e3
AAL
3063 case WMI_TRAFFIC_SUSPEND_REJECTED_DISCONNECT:
3064 return "DISCONNECT";
3065 case WMI_TRAFFIC_SUSPEND_REJECTED_OTHER:
3066 return "OTHER";
6804cd10
LA
3067 default:
3068 return "Untracked status";
3069 }
3070}
3071
fe9ee51e
ME
3072int wmi_suspend(struct wil6210_priv *wil)
3073{
e00243fa 3074 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
fe9ee51e
ME
3075 int rc;
3076 struct wmi_traffic_suspend_cmd cmd = {
3077 .wakeup_trigger = wil->wakeup_trigger,
3078 };
3079 struct {
3080 struct wmi_cmd_hdr wmi;
3081 struct wmi_traffic_suspend_event evt;
807b0860
AAL
3082 } __packed reply = {
3083 .evt = {.status = WMI_TRAFFIC_SUSPEND_REJECTED_LINK_NOT_IDLE},
3084 };
3085
fe9ee51e
ME
3086 u32 suspend_to = WIL_WAIT_FOR_SUSPEND_RESUME_COMP;
3087
3088 wil->suspend_resp_rcvd = false;
3089 wil->suspend_resp_comp = false;
3090
e00243fa
LD
3091 rc = wmi_call(wil, WMI_TRAFFIC_SUSPEND_CMDID, vif->mid,
3092 &cmd, sizeof(cmd),
fe9ee51e
ME
3093 WMI_TRAFFIC_SUSPEND_EVENTID, &reply, sizeof(reply),
3094 suspend_to);
3095 if (rc) {
3096 wil_err(wil, "wmi_call for suspend req failed, rc=%d\n", rc);
3097 if (rc == -ETIME)
3098 /* wmi_call TO */
3099 wil->suspend_stats.rejected_by_device++;
3100 else
3101 wil->suspend_stats.rejected_by_host++;
3102 goto out;
3103 }
3104
3105 wil_dbg_wmi(wil, "waiting for suspend_response_completed\n");
3106
3107 rc = wait_event_interruptible_timeout(wil->wq,
3108 wil->suspend_resp_comp,
3109 msecs_to_jiffies(suspend_to));
3110 if (rc == 0) {
3111 wil_err(wil, "TO waiting for suspend_response_completed\n");
3112 if (wil->suspend_resp_rcvd)
3113 /* Device responded but we TO due to another reason */
3114 wil->suspend_stats.rejected_by_host++;
3115 else
3116 wil->suspend_stats.rejected_by_device++;
3117 rc = -EBUSY;
3118 goto out;
3119 }
3120
3121 wil_dbg_wmi(wil, "suspend_response_completed rcvd\n");
6804cd10
LA
3122 if (reply.evt.status != WMI_TRAFFIC_SUSPEND_APPROVED) {
3123 wil_dbg_pm(wil, "device rejected the suspend, %s\n",
3124 suspend_status2name(reply.evt.status));
fe9ee51e
ME
3125 wil->suspend_stats.rejected_by_device++;
3126 }
3127 rc = reply.evt.status;
3128
3129out:
3130 wil->suspend_resp_rcvd = false;
3131 wil->suspend_resp_comp = false;
3132
3133 return rc;
3134}
3135
6804cd10
LA
3136static void resume_triggers2string(u32 triggers, char *string, int str_size)
3137{
3138 string[0] = '\0';
3139
3140 if (!triggers) {
3141 strlcat(string, " UNKNOWN", str_size);
3142 return;
3143 }
3144
3145 if (triggers & WMI_RESUME_TRIGGER_HOST)
3146 strlcat(string, " HOST", str_size);
3147
3148 if (triggers & WMI_RESUME_TRIGGER_UCAST_RX)
3149 strlcat(string, " UCAST_RX", str_size);
3150
3151 if (triggers & WMI_RESUME_TRIGGER_BCAST_RX)
3152 strlcat(string, " BCAST_RX", str_size);
3153
3154 if (triggers & WMI_RESUME_TRIGGER_WMI_EVT)
3155 strlcat(string, " WMI_EVT", str_size);
73a7d1e3
AAL
3156
3157 if (triggers & WMI_RESUME_TRIGGER_DISCONNECT)
3158 strlcat(string, " DISCONNECT", str_size);
6804cd10
LA
3159}
3160
fe9ee51e
ME
3161int wmi_resume(struct wil6210_priv *wil)
3162{
e00243fa 3163 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
fe9ee51e 3164 int rc;
6804cd10 3165 char string[100];
fe9ee51e
ME
3166 struct {
3167 struct wmi_cmd_hdr wmi;
3168 struct wmi_traffic_resume_event evt;
807b0860
AAL
3169 } __packed reply = {
3170 .evt = {.status = WMI_TRAFFIC_RESUME_FAILED,
3171 .resume_triggers =
3172 cpu_to_le32(WMI_RESUME_TRIGGER_UNKNOWN)},
3173 };
fe9ee51e 3174
e00243fa 3175 rc = wmi_call(wil, WMI_TRAFFIC_RESUME_CMDID, vif->mid, NULL, 0,
fe9ee51e
ME
3176 WMI_TRAFFIC_RESUME_EVENTID, &reply, sizeof(reply),
3177 WIL_WAIT_FOR_SUSPEND_RESUME_COMP);
3178 if (rc)
3179 return rc;
6804cd10
LA
3180 resume_triggers2string(le32_to_cpu(reply.evt.resume_triggers), string,
3181 sizeof(string));
3182 wil_dbg_pm(wil, "device resume %s, resume triggers:%s (0x%x)\n",
3183 reply.evt.status ? "failed" : "passed", string,
3184 le32_to_cpu(reply.evt.resume_triggers));
fe9ee51e
ME
3185
3186 return reply.evt.status;
3187}
3188
4aebd3bd
LD
3189int wmi_port_allocate(struct wil6210_priv *wil, u8 mid,
3190 const u8 *mac, enum nl80211_iftype iftype)
3191{
3192 int rc;
3193 struct wmi_port_allocate_cmd cmd = {
3194 .mid = mid,
3195 };
3196 struct {
3197 struct wmi_cmd_hdr wmi;
3198 struct wmi_port_allocated_event evt;
807b0860
AAL
3199 } __packed reply = {
3200 .evt = {.status = WMI_FW_STATUS_FAILURE},
3201 };
4aebd3bd
LD
3202
3203 wil_dbg_misc(wil, "port allocate, mid %d iftype %d, mac %pM\n",
3204 mid, iftype, mac);
3205
3206 ether_addr_copy(cmd.mac, mac);
3207 switch (iftype) {
3208 case NL80211_IFTYPE_STATION:
3209 cmd.port_role = WMI_PORT_STA;
3210 break;
3211 case NL80211_IFTYPE_AP:
3212 cmd.port_role = WMI_PORT_AP;
3213 break;
3214 case NL80211_IFTYPE_P2P_CLIENT:
3215 cmd.port_role = WMI_PORT_P2P_CLIENT;
3216 break;
3217 case NL80211_IFTYPE_P2P_GO:
3218 cmd.port_role = WMI_PORT_P2P_GO;
3219 break;
3220 /* what about monitor??? */
3221 default:
3222 wil_err(wil, "unsupported iftype: %d\n", iftype);
3223 return -EINVAL;
3224 }
3225
4aebd3bd
LD
3226 rc = wmi_call(wil, WMI_PORT_ALLOCATE_CMDID, mid,
3227 &cmd, sizeof(cmd),
3228 WMI_PORT_ALLOCATED_EVENTID, &reply,
3229 sizeof(reply), 300);
3230 if (rc) {
3231 wil_err(wil, "failed to allocate port, status %d\n", rc);
3232 return rc;
3233 }
3234 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
3235 wil_err(wil, "WMI_PORT_ALLOCATE returned status %d\n",
3236 reply.evt.status);
3237 return -EINVAL;
3238 }
3239
3240 return 0;
3241}
3242
3243int wmi_port_delete(struct wil6210_priv *wil, u8 mid)
3244{
3245 int rc;
3246 struct wmi_port_delete_cmd cmd = {
3247 .mid = mid,
3248 };
3249 struct {
3250 struct wmi_cmd_hdr wmi;
3251 struct wmi_port_deleted_event evt;
807b0860
AAL
3252 } __packed reply = {
3253 .evt = {.status = WMI_FW_STATUS_FAILURE},
3254 };
4aebd3bd
LD
3255
3256 wil_dbg_misc(wil, "port delete, mid %d\n", mid);
3257
4aebd3bd
LD
3258 rc = wmi_call(wil, WMI_PORT_DELETE_CMDID, mid,
3259 &cmd, sizeof(cmd),
3260 WMI_PORT_DELETED_EVENTID, &reply,
3261 sizeof(reply), 2000);
3262 if (rc) {
3263 wil_err(wil, "failed to delete port, status %d\n", rc);
3264 return rc;
3265 }
3266 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
3267 wil_err(wil, "WMI_PORT_DELETE returned status %d\n",
3268 reply.evt.status);
3269 return -EINVAL;
3270 }
3271
3272 return 0;
3273}
3274
e00243fa 3275static bool wmi_evt_call_handler(struct wil6210_vif *vif, int id,
2be7d22f
VK
3276 void *d, int len)
3277{
3278 uint i;
3279
3280 for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
3281 if (wmi_evt_handlers[i].eventid == id) {
e00243fa 3282 wmi_evt_handlers[i].handler(vif, id, d, len);
2be7d22f
VK
3283 return true;
3284 }
3285 }
3286
3287 return false;
3288}
3289
3290static void wmi_event_handle(struct wil6210_priv *wil,
3291 struct wil6210_mbox_hdr *hdr)
3292{
3293 u16 len = le16_to_cpu(hdr->len);
4aebd3bd 3294 struct wil6210_vif *vif;
2be7d22f
VK
3295
3296 if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
b874ddec
LD
3297 (len >= sizeof(struct wmi_cmd_hdr))) {
3298 struct wmi_cmd_hdr *wmi = (void *)(&hdr[1]);
2be7d22f 3299 void *evt_data = (void *)(&wmi[1]);
b874ddec 3300 u16 id = le16_to_cpu(wmi->command_id);
e00243fa
LD
3301 u8 mid = wmi->mid;
3302
3303 wil_dbg_wmi(wil, "Handle %s (0x%04x) (reply_id 0x%04x,%d)\n",
3304 eventid2name(id), id, wil->reply_id,
3305 wil->reply_mid);
028e1836 3306
4aebd3bd
LD
3307 if (mid == MID_BROADCAST)
3308 mid = 0;
e4a29bdd 3309 if (mid >= GET_MAX_VIFS(wil)) {
4aebd3bd
LD
3310 wil_dbg_wmi(wil, "invalid mid %d, event skipped\n",
3311 mid);
3312 return;
3313 }
3314 vif = wil->vifs[mid];
3315 if (!vif) {
3316 wil_dbg_wmi(wil, "event for empty VIF(%d), skipped\n",
3317 mid);
3318 return;
3319 }
3320
2be7d22f 3321 /* check if someone waits for this event */
e00243fa
LD
3322 if (wil->reply_id && wil->reply_id == id &&
3323 wil->reply_mid == mid) {
1a276003
AM
3324 if (wil->reply_buf) {
3325 /* event received while wmi_call is waiting
3326 * with a buffer. Such event should be handled
3327 * in wmi_recv_cmd function. Handling the event
3328 * here means a previous wmi_call was timeout.
3329 * Drop the event and do not handle it.
3330 */
3331 wil_err(wil,
3332 "Old event (%d, %s) while wmi_call is waiting. Drop it and Continue waiting\n",
3333 id, eventid2name(id));
3334 return;
3335 }
e00243fa
LD
3336
3337 wmi_evt_call_handler(vif, id, evt_data,
0916d9f2 3338 len - sizeof(*wmi));
af3db60a
LA
3339 wil_dbg_wmi(wil, "event_handle: Complete WMI 0x%04x\n",
3340 id);
59502647 3341 complete(&wil->wmi_call);
2be7d22f
VK
3342 return;
3343 }
3344 /* unsolicited event */
3345 /* search for handler */
e00243fa 3346 if (!wmi_evt_call_handler(vif, id, evt_data,
2be7d22f 3347 len - sizeof(*wmi))) {
a3ce5ccd 3348 wil_info(wil, "Unhandled event 0x%04x\n", id);
2be7d22f
VK
3349 }
3350 } else {
3351 wil_err(wil, "Unknown event type\n");
3352 print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
3353 hdr, sizeof(*hdr) + len, true);
3354 }
3355}
3356
3357/*
3358 * Retrieve next WMI event from the pending list
3359 */
3360static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
3361{
3362 ulong flags;
3363 struct list_head *ret = NULL;
3364
3365 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
3366
3367 if (!list_empty(&wil->pending_wmi_ev)) {
3368 ret = wil->pending_wmi_ev.next;
3369 list_del(ret);
3370 }
3371
3372 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
3373
3374 return ret;
3375}
3376
3377/*
3378 * Handler for the WMI events
3379 */
3380void wmi_event_worker(struct work_struct *work)
3381{
3382 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
3383 wmi_event_worker);
3384 struct pending_wmi_event *evt;
3385 struct list_head *lh;
3386
af3db60a 3387 wil_dbg_wmi(wil, "event_worker: Start\n");
2be7d22f
VK
3388 while ((lh = next_wmi_ev(wil)) != NULL) {
3389 evt = list_entry(lh, struct pending_wmi_event, list);
3390 wmi_event_handle(wil, &evt->event.hdr);
3391 kfree(evt);
3392 }
af3db60a 3393 wil_dbg_wmi(wil, "event_worker: Finished\n");
2be7d22f 3394}
fe9ee51e
ME
3395
3396bool wil_is_wmi_idle(struct wil6210_priv *wil)
3397{
3398 ulong flags;
3399 struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
3400 bool rc = false;
3401
3402 spin_lock_irqsave(&wil->wmi_ev_lock, flags);
3403
3404 /* Check if there are pending WMI events in the events queue */
3405 if (!list_empty(&wil->pending_wmi_ev)) {
3406 wil_dbg_pm(wil, "Pending WMI events in queue\n");
3407 goto out;
3408 }
3409
3410 /* Check if there is a pending WMI call */
3411 if (wil->reply_id) {
3412 wil_dbg_pm(wil, "Pending WMI call\n");
3413 goto out;
3414 }
3415
3416 /* Check if there are pending RX events in mbox */
3417 r->head = wil_r(wil, RGF_MBOX +
3418 offsetof(struct wil6210_mbox_ctl, rx.head));
3419 if (r->tail != r->head)
3420 wil_dbg_pm(wil, "Pending WMI mbox events\n");
3421 else
3422 rc = true;
3423
3424out:
3425 spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
3426 return rc;
3427}
a5dc6883
DL
3428
3429static void
3430wmi_sched_scan_set_ssids(struct wil6210_priv *wil,
3431 struct wmi_start_sched_scan_cmd *cmd,
3432 struct cfg80211_ssid *ssids, int n_ssids,
3433 struct cfg80211_match_set *match_sets,
3434 int n_match_sets)
3435{
3436 int i;
3437
3438 if (n_match_sets > WMI_MAX_PNO_SSID_NUM) {
3439 wil_dbg_wmi(wil, "too many match sets (%d), use first %d\n",
3440 n_match_sets, WMI_MAX_PNO_SSID_NUM);
3441 n_match_sets = WMI_MAX_PNO_SSID_NUM;
3442 }
3443 cmd->num_of_ssids = n_match_sets;
3444
3445 for (i = 0; i < n_match_sets; i++) {
3446 struct wmi_sched_scan_ssid_match *wmi_match =
3447 &cmd->ssid_for_match[i];
3448 struct cfg80211_match_set *cfg_match = &match_sets[i];
3449 int j;
3450
3451 wmi_match->ssid_len = cfg_match->ssid.ssid_len;
3452 memcpy(wmi_match->ssid, cfg_match->ssid.ssid,
3453 min_t(u8, wmi_match->ssid_len, WMI_MAX_SSID_LEN));
3454 wmi_match->rssi_threshold = S8_MIN;
3455 if (cfg_match->rssi_thold >= S8_MIN &&
3456 cfg_match->rssi_thold <= S8_MAX)
3457 wmi_match->rssi_threshold = cfg_match->rssi_thold;
3458
3459 for (j = 0; j < n_ssids; j++)
3460 if (wmi_match->ssid_len == ssids[j].ssid_len &&
3461 memcmp(wmi_match->ssid, ssids[j].ssid,
3462 wmi_match->ssid_len) == 0)
3463 wmi_match->add_ssid_to_probe = true;
3464 }
3465}
3466
3467static void
3468wmi_sched_scan_set_channels(struct wil6210_priv *wil,
3469 struct wmi_start_sched_scan_cmd *cmd,
3470 u32 n_channels,
3471 struct ieee80211_channel **channels)
3472{
3473 int i;
3474
3475 if (n_channels > WMI_MAX_CHANNEL_NUM) {
3476 wil_dbg_wmi(wil, "too many channels (%d), use first %d\n",
3477 n_channels, WMI_MAX_CHANNEL_NUM);
3478 n_channels = WMI_MAX_CHANNEL_NUM;
3479 }
3480 cmd->num_of_channels = n_channels;
3481
3482 for (i = 0; i < n_channels; i++) {
3483 struct ieee80211_channel *cfg_chan = channels[i];
3484
3485 cmd->channel_list[i] = cfg_chan->hw_value - 1;
3486 }
3487}
3488
3489static void
3490wmi_sched_scan_set_plans(struct wil6210_priv *wil,
3491 struct wmi_start_sched_scan_cmd *cmd,
3492 struct cfg80211_sched_scan_plan *scan_plans,
3493 int n_scan_plans)
3494{
3495 int i;
3496
3497 if (n_scan_plans > WMI_MAX_PLANS_NUM) {
3498 wil_dbg_wmi(wil, "too many plans (%d), use first %d\n",
3499 n_scan_plans, WMI_MAX_PLANS_NUM);
3500 n_scan_plans = WMI_MAX_PLANS_NUM;
3501 }
3502
3503 for (i = 0; i < n_scan_plans; i++) {
3504 struct cfg80211_sched_scan_plan *cfg_plan = &scan_plans[i];
3505
3506 cmd->scan_plans[i].interval_sec =
3507 cpu_to_le16(cfg_plan->interval);
3508 cmd->scan_plans[i].num_of_iterations =
3509 cpu_to_le16(cfg_plan->iterations);
3510 }
3511}
3512
3513int wmi_start_sched_scan(struct wil6210_priv *wil,
3514 struct cfg80211_sched_scan_request *request)
3515{
e00243fa 3516 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
a5dc6883
DL
3517 int rc;
3518 struct wmi_start_sched_scan_cmd cmd = {
3519 .min_rssi_threshold = S8_MIN,
3520 .initial_delay_sec = cpu_to_le16(request->delay),
3521 };
3522 struct {
3523 struct wmi_cmd_hdr wmi;
3524 struct wmi_start_sched_scan_event evt;
807b0860
AAL
3525 } __packed reply = {
3526 .evt = {.result = WMI_PNO_REJECT},
3527 };
a5dc6883
DL
3528
3529 if (!test_bit(WMI_FW_CAPABILITY_PNO, wil->fw_capabilities))
3530 return -ENOTSUPP;
3531
3532 if (request->min_rssi_thold >= S8_MIN &&
3533 request->min_rssi_thold <= S8_MAX)
3534 cmd.min_rssi_threshold = request->min_rssi_thold;
3535
3536 wmi_sched_scan_set_ssids(wil, &cmd, request->ssids, request->n_ssids,
3537 request->match_sets, request->n_match_sets);
3538 wmi_sched_scan_set_channels(wil, &cmd,
3539 request->n_channels, request->channels);
3540 wmi_sched_scan_set_plans(wil, &cmd,
3541 request->scan_plans, request->n_scan_plans);
3542
e00243fa
LD
3543 rc = wmi_call(wil, WMI_START_SCHED_SCAN_CMDID, vif->mid,
3544 &cmd, sizeof(cmd),
a5dc6883
DL
3545 WMI_START_SCHED_SCAN_EVENTID, &reply, sizeof(reply),
3546 WIL_WMI_CALL_GENERAL_TO_MS);
3547 if (rc)
3548 return rc;
3549
3550 if (reply.evt.result != WMI_PNO_SUCCESS) {
3551 wil_err(wil, "start sched scan failed, result %d\n",
3552 reply.evt.result);
3553 return -EINVAL;
3554 }
3555
3556 return 0;
3557}
3558
3559int wmi_stop_sched_scan(struct wil6210_priv *wil)
3560{
e00243fa 3561 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
a5dc6883
DL
3562 int rc;
3563 struct {
3564 struct wmi_cmd_hdr wmi;
3565 struct wmi_stop_sched_scan_event evt;
807b0860
AAL
3566 } __packed reply = {
3567 .evt = {.result = WMI_PNO_REJECT},
3568 };
a5dc6883
DL
3569
3570 if (!test_bit(WMI_FW_CAPABILITY_PNO, wil->fw_capabilities))
3571 return -ENOTSUPP;
3572
e00243fa 3573 rc = wmi_call(wil, WMI_STOP_SCHED_SCAN_CMDID, vif->mid, NULL, 0,
a5dc6883
DL
3574 WMI_STOP_SCHED_SCAN_EVENTID, &reply, sizeof(reply),
3575 WIL_WMI_CALL_GENERAL_TO_MS);
3576 if (rc)
3577 return rc;
3578
3579 if (reply.evt.result != WMI_PNO_SUCCESS) {
3580 wil_err(wil, "stop sched scan failed, result %d\n",
3581 reply.evt.result);
3582 return -EINVAL;
3583 }
3584
3585 return 0;
3586}
1c21cc5f
DL
3587
3588int wmi_mgmt_tx(struct wil6210_vif *vif, const u8 *buf, size_t len)
3589{
3590 size_t total;
3591 struct wil6210_priv *wil = vif_to_wil(vif);
3592 struct ieee80211_mgmt *mgmt_frame = (void *)buf;
3593 struct wmi_sw_tx_req_cmd *cmd;
3594 struct {
3595 struct wmi_cmd_hdr wmi;
3596 struct wmi_sw_tx_complete_event evt;
3597 } __packed evt = {
3598 .evt = {.status = WMI_FW_STATUS_FAILURE},
3599 };
3600 int rc;
3601
3602 wil_dbg_misc(wil, "mgmt_tx mid %d\n", vif->mid);
3603 wil_hex_dump_misc("mgmt tx frame ", DUMP_PREFIX_OFFSET, 16, 1, buf,
3604 len, true);
3605
3606 if (len < sizeof(struct ieee80211_hdr_3addr))
3607 return -EINVAL;
3608
3609 total = sizeof(*cmd) + len;
3610 if (total < len) {
3611 wil_err(wil, "mgmt_tx invalid len %zu\n", len);
3612 return -EINVAL;
3613 }
3614
3615 cmd = kmalloc(total, GFP_KERNEL);
3616 if (!cmd)
3617 return -ENOMEM;
3618
3619 memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
3620 cmd->len = cpu_to_le16(len);
3621 memcpy(cmd->payload, buf, len);
3622
3623 rc = wmi_call(wil, WMI_SW_TX_REQ_CMDID, vif->mid, cmd, total,
3624 WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
3625 if (!rc && evt.evt.status != WMI_FW_STATUS_SUCCESS) {
49122ec4
LD
3626 wil_dbg_wmi(wil, "mgmt_tx failed with status %d\n",
3627 evt.evt.status);
3628 rc = -EAGAIN;
1c21cc5f
DL
3629 }
3630
3631 kfree(cmd);
3632
3633 return rc;
3634}
96c93589 3635
b698e2df
AM
3636int wmi_mgmt_tx_ext(struct wil6210_vif *vif, const u8 *buf, size_t len,
3637 u8 channel, u16 duration_ms)
3638{
3639 size_t total;
3640 struct wil6210_priv *wil = vif_to_wil(vif);
3641 struct ieee80211_mgmt *mgmt_frame = (void *)buf;
3642 struct wmi_sw_tx_req_ext_cmd *cmd;
3643 struct {
3644 struct wmi_cmd_hdr wmi;
3645 struct wmi_sw_tx_complete_event evt;
3646 } __packed evt = {
3647 .evt = {.status = WMI_FW_STATUS_FAILURE},
3648 };
3649 int rc;
3650
3651 wil_dbg_wmi(wil, "mgmt_tx_ext mid %d channel %d duration %d\n",
3652 vif->mid, channel, duration_ms);
3653 wil_hex_dump_wmi("mgmt_tx_ext frame ", DUMP_PREFIX_OFFSET, 16, 1, buf,
3654 len, true);
3655
3656 if (len < sizeof(struct ieee80211_hdr_3addr)) {
3657 wil_err(wil, "short frame. len %zu\n", len);
3658 return -EINVAL;
3659 }
3660
3661 total = sizeof(*cmd) + len;
3662 if (total < len) {
3663 wil_err(wil, "mgmt_tx_ext invalid len %zu\n", len);
3664 return -EINVAL;
3665 }
3666
3667 cmd = kzalloc(total, GFP_KERNEL);
3668 if (!cmd)
3669 return -ENOMEM;
3670
3671 memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
3672 cmd->len = cpu_to_le16(len);
3673 memcpy(cmd->payload, buf, len);
3674 cmd->channel = channel - 1;
3675 cmd->duration_ms = cpu_to_le16(duration_ms);
3676
3677 rc = wmi_call(wil, WMI_SW_TX_REQ_EXT_CMDID, vif->mid, cmd, total,
3678 WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
3679 if (!rc && evt.evt.status != WMI_FW_STATUS_SUCCESS) {
49122ec4
LD
3680 wil_dbg_wmi(wil, "mgmt_tx_ext failed with status %d\n",
3681 evt.evt.status);
3682 rc = -EAGAIN;
b698e2df
AM
3683 }
3684
3685 kfree(cmd);
3686
3687 return rc;
3688}
3689
96c93589
GS
3690int wil_wmi_tx_sring_cfg(struct wil6210_priv *wil, int ring_id)
3691{
3692 int rc;
3693 struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
3694 struct wil_status_ring *sring = &wil->srings[ring_id];
3695 struct wmi_tx_status_ring_add_cmd cmd = {
3696 .ring_cfg = {
3697 .ring_size = cpu_to_le16(sring->size),
3698 },
3699 .irq_index = WIL_TX_STATUS_IRQ_IDX
3700 };
3701 struct {
3702 struct wmi_cmd_hdr hdr;
3703 struct wmi_tx_status_ring_cfg_done_event evt;
3704 } __packed reply = {
3705 .evt = {.status = WMI_FW_STATUS_FAILURE},
3706 };
3707
3708 cmd.ring_cfg.ring_id = ring_id;
3709
3710 cmd.ring_cfg.ring_mem_base = cpu_to_le64(sring->pa);
3711 rc = wmi_call(wil, WMI_TX_STATUS_RING_ADD_CMDID, vif->mid, &cmd,
3712 sizeof(cmd), WMI_TX_STATUS_RING_CFG_DONE_EVENTID,
3713 &reply, sizeof(reply), WIL_WMI_CALL_GENERAL_TO_MS);
3714 if (rc) {
3715 wil_err(wil, "TX_STATUS_RING_ADD_CMD failed, rc %d\n", rc);
3716 return rc;
3717 }
3718
3719 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
3720 wil_err(wil, "TX_STATUS_RING_ADD_CMD failed, status %d\n",
3721 reply.evt.status);
3722 return -EINVAL;
3723 }
3724
3725 sring->hwtail = le32_to_cpu(reply.evt.ring_tail_ptr);
3726
3727 return 0;
3728}
3729
3730int wil_wmi_cfg_def_rx_offload(struct wil6210_priv *wil, u16 max_rx_pl_per_desc)
3731{
3732 struct net_device *ndev = wil->main_ndev;
3733 struct wil6210_vif *vif = ndev_to_vif(ndev);
3734 int rc;
3735 struct wmi_cfg_def_rx_offload_cmd cmd = {
3736 .max_msdu_size = cpu_to_le16(wil_mtu2macbuf(WIL_MAX_ETH_MTU)),
3737 .max_rx_pl_per_desc = cpu_to_le16(max_rx_pl_per_desc),
3738 .decap_trans_type = WMI_DECAP_TYPE_802_3,
3739 .l2_802_3_offload_ctrl = 0,
3740 .l3_l4_ctrl = 1 << L3_L4_CTRL_TCPIP_CHECKSUM_EN_POS,
3741 };
3742 struct {
3743 struct wmi_cmd_hdr hdr;
3744 struct wmi_cfg_def_rx_offload_done_event evt;
3745 } __packed reply = {
3746 .evt = {.status = WMI_FW_STATUS_FAILURE},
3747 };
3748
3749 rc = wmi_call(wil, WMI_CFG_DEF_RX_OFFLOAD_CMDID, vif->mid, &cmd,
3750 sizeof(cmd), WMI_CFG_DEF_RX_OFFLOAD_DONE_EVENTID, &reply,
3751 sizeof(reply), WIL_WMI_CALL_GENERAL_TO_MS);
3752 if (rc) {
3753 wil_err(wil, "WMI_CFG_DEF_RX_OFFLOAD_CMD failed, rc %d\n", rc);
3754 return rc;
3755 }
3756
3757 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
3758 wil_err(wil, "WMI_CFG_DEF_RX_OFFLOAD_CMD failed, status %d\n",
3759 reply.evt.status);
3760 return -EINVAL;
3761 }
3762
3763 return 0;
3764}
3765
3766int wil_wmi_rx_sring_add(struct wil6210_priv *wil, u16 ring_id)
3767{
3768 struct net_device *ndev = wil->main_ndev;
3769 struct wil6210_vif *vif = ndev_to_vif(ndev);
3770 struct wil_status_ring *sring = &wil->srings[ring_id];
3771 int rc;
3772 struct wmi_rx_status_ring_add_cmd cmd = {
3773 .ring_cfg = {
3774 .ring_size = cpu_to_le16(sring->size),
3775 .ring_id = ring_id,
3776 },
3777 .rx_msg_type = wil->use_compressed_rx_status ?
3778 WMI_RX_MSG_TYPE_COMPRESSED :
3779 WMI_RX_MSG_TYPE_EXTENDED,
3780 .irq_index = WIL_RX_STATUS_IRQ_IDX,
3781 };
3782 struct {
3783 struct wmi_cmd_hdr hdr;
3784 struct wmi_rx_status_ring_cfg_done_event evt;
3785 } __packed reply = {
3786 .evt = {.status = WMI_FW_STATUS_FAILURE},
3787 };
3788
3789 cmd.ring_cfg.ring_mem_base = cpu_to_le64(sring->pa);
3790 rc = wmi_call(wil, WMI_RX_STATUS_RING_ADD_CMDID, vif->mid, &cmd,
3791 sizeof(cmd), WMI_RX_STATUS_RING_CFG_DONE_EVENTID, &reply,
3792 sizeof(reply), WIL_WMI_CALL_GENERAL_TO_MS);
3793 if (rc) {
3794 wil_err(wil, "RX_STATUS_RING_ADD_CMD failed, rc %d\n", rc);
3795 return rc;
3796 }
3797
3798 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
3799 wil_err(wil, "RX_STATUS_RING_ADD_CMD failed, status %d\n",
3800 reply.evt.status);
3801 return -EINVAL;
3802 }
3803
3804 sring->hwtail = le32_to_cpu(reply.evt.ring_tail_ptr);
3805
3806 return 0;
3807}
3808
3809int wil_wmi_rx_desc_ring_add(struct wil6210_priv *wil, int status_ring_id)
3810{
3811 struct net_device *ndev = wil->main_ndev;
3812 struct wil6210_vif *vif = ndev_to_vif(ndev);
3813 struct wil_ring *ring = &wil->ring_rx;
3814 int rc;
3815 struct wmi_rx_desc_ring_add_cmd cmd = {
3816 .ring_cfg = {
3817 .ring_size = cpu_to_le16(ring->size),
3818 .ring_id = WIL_RX_DESC_RING_ID,
3819 },
3820 .status_ring_id = status_ring_id,
3821 .irq_index = WIL_RX_STATUS_IRQ_IDX,
3822 };
3823 struct {
3824 struct wmi_cmd_hdr hdr;
3825 struct wmi_rx_desc_ring_cfg_done_event evt;
3826 } __packed reply = {
3827 .evt = {.status = WMI_FW_STATUS_FAILURE},
3828 };
3829
3830 cmd.ring_cfg.ring_mem_base = cpu_to_le64(ring->pa);
3831 cmd.sw_tail_host_addr = cpu_to_le64(ring->edma_rx_swtail.pa);
3832 rc = wmi_call(wil, WMI_RX_DESC_RING_ADD_CMDID, vif->mid, &cmd,
3833 sizeof(cmd), WMI_RX_DESC_RING_CFG_DONE_EVENTID, &reply,
3834 sizeof(reply), WIL_WMI_CALL_GENERAL_TO_MS);
3835 if (rc) {
3836 wil_err(wil, "WMI_RX_DESC_RING_ADD_CMD failed, rc %d\n", rc);
3837 return rc;
3838 }
3839
3840 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
3841 wil_err(wil, "WMI_RX_DESC_RING_ADD_CMD failed, status %d\n",
3842 reply.evt.status);
3843 return -EINVAL;
3844 }
3845
3846 ring->hwtail = le32_to_cpu(reply.evt.ring_tail_ptr);
3847
3848 return 0;
3849}
3850
3851int wil_wmi_tx_desc_ring_add(struct wil6210_vif *vif, int ring_id, int cid,
3852 int tid)
3853{
3854 struct wil6210_priv *wil = vif_to_wil(vif);
3855 int sring_id = wil->tx_sring_idx; /* there is only one TX sring */
3856 int rc;
3857 struct wil_ring *ring = &wil->ring_tx[ring_id];
3858 struct wil_ring_tx_data *txdata = &wil->ring_tx_data[ring_id];
3859 struct wmi_tx_desc_ring_add_cmd cmd = {
3860 .ring_cfg = {
3861 .ring_size = cpu_to_le16(ring->size),
3862 .ring_id = ring_id,
3863 },
3864 .status_ring_id = sring_id,
3865 .cid = cid,
3866 .tid = tid,
3867 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
3868 .max_msdu_size = cpu_to_le16(wil_mtu2macbuf(mtu_max)),
3869 .schd_params = {
3870 .priority = cpu_to_le16(0),
3871 .timeslot_us = cpu_to_le16(0xfff),
3872 }
3873 };
3874 struct {
3875 struct wmi_cmd_hdr hdr;
3876 struct wmi_tx_desc_ring_cfg_done_event evt;
3877 } __packed reply = {
3878 .evt = {.status = WMI_FW_STATUS_FAILURE},
3879 };
3880
3881 cmd.ring_cfg.ring_mem_base = cpu_to_le64(ring->pa);
3882 rc = wmi_call(wil, WMI_TX_DESC_RING_ADD_CMDID, vif->mid, &cmd,
3883 sizeof(cmd), WMI_TX_DESC_RING_CFG_DONE_EVENTID, &reply,
3884 sizeof(reply), WIL_WMI_CALL_GENERAL_TO_MS);
3885 if (rc) {
3886 wil_err(wil, "WMI_TX_DESC_RING_ADD_CMD failed, rc %d\n", rc);
3887 return rc;
3888 }
3889
3890 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
3891 wil_err(wil, "WMI_TX_DESC_RING_ADD_CMD failed, status %d\n",
3892 reply.evt.status);
3893 return -EINVAL;
3894 }
3895
3896 spin_lock_bh(&txdata->lock);
3897 ring->hwtail = le32_to_cpu(reply.evt.ring_tail_ptr);
3898 txdata->mid = vif->mid;
3899 txdata->enabled = 1;
3900 spin_unlock_bh(&txdata->lock);
3901
3902 return 0;
3903}
3904
3905int wil_wmi_bcast_desc_ring_add(struct wil6210_vif *vif, int ring_id)
3906{
3907 struct wil6210_priv *wil = vif_to_wil(vif);
3908 struct wil_ring *ring = &wil->ring_tx[ring_id];
3909 int rc;
3910 struct wmi_bcast_desc_ring_add_cmd cmd = {
3911 .ring_cfg = {
3912 .ring_size = cpu_to_le16(ring->size),
3913 .ring_id = ring_id,
3914 },
3e7ee09d 3915 .max_msdu_size = cpu_to_le16(wil_mtu2macbuf(mtu_max)),
96c93589
GS
3916 .status_ring_id = wil->tx_sring_idx,
3917 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
3918 };
3919 struct {
3920 struct wmi_cmd_hdr hdr;
3921 struct wmi_rx_desc_ring_cfg_done_event evt;
3922 } __packed reply = {
3923 .evt = {.status = WMI_FW_STATUS_FAILURE},
3924 };
3925 struct wil_ring_tx_data *txdata = &wil->ring_tx_data[ring_id];
3926
3927 cmd.ring_cfg.ring_mem_base = cpu_to_le64(ring->pa);
3928 rc = wmi_call(wil, WMI_BCAST_DESC_RING_ADD_CMDID, vif->mid, &cmd,
3929 sizeof(cmd), WMI_TX_DESC_RING_CFG_DONE_EVENTID, &reply,
3930 sizeof(reply), WIL_WMI_CALL_GENERAL_TO_MS);
3931 if (rc) {
3932 wil_err(wil, "WMI_BCAST_DESC_RING_ADD_CMD failed, rc %d\n", rc);
3933 return rc;
3934 }
3935
3936 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
3937 wil_err(wil, "Broadcast Tx config failed, status %d\n",
3938 reply.evt.status);
3939 return -EINVAL;
3940 }
3941
3942 spin_lock_bh(&txdata->lock);
3943 ring->hwtail = le32_to_cpu(reply.evt.ring_tail_ptr);
3944 txdata->mid = vif->mid;
3945 txdata->enabled = 1;
3946 spin_unlock_bh(&txdata->lock);
3947
3948 return 0;
3949}
0c936b3c
DL
3950
3951int wmi_link_stats_cfg(struct wil6210_vif *vif, u32 type, u8 cid, u32 interval)
3952{
3953 struct wil6210_priv *wil = vif_to_wil(vif);
3954 struct wmi_link_stats_cmd cmd = {
3955 .record_type_mask = cpu_to_le32(type),
3956 .cid = cid,
3957 .action = WMI_LINK_STATS_SNAPSHOT,
3958 .interval_msec = cpu_to_le32(interval),
3959 };
3960 struct {
3961 struct wmi_cmd_hdr wmi;
3962 struct wmi_link_stats_config_done_event evt;
3963 } __packed reply = {
3964 .evt = {.status = WMI_FW_STATUS_FAILURE},
3965 };
3966 int rc;
3967
3968 rc = wmi_call(wil, WMI_LINK_STATS_CMDID, vif->mid, &cmd, sizeof(cmd),
3969 WMI_LINK_STATS_CONFIG_DONE_EVENTID, &reply,
3970 sizeof(reply), WIL_WMI_CALL_GENERAL_TO_MS);
3971 if (rc) {
3972 wil_err(wil, "WMI_LINK_STATS_CMDID failed, rc %d\n", rc);
3973 return rc;
3974 }
3975
3976 if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
3977 wil_err(wil, "Link statistics config failed, status %d\n",
3978 reply.evt.status);
3979 return -EINVAL;
3980 }
3981
3982 return 0;
3983}