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