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},
93 	/* FW data RAM 32k */
94 	{0x800000, 0x808000, 0x900000, "fw_data", true},
95 	/* periph data 128k */
96 	{0x840000, 0x860000, 0x908000, "fw_peri", true},
97 	/* various RGF 40k */
98 	{0x880000, 0x88a000, 0x880000, "rgf", true},
99 	/* AGC table   4k */
100 	{0x88a000, 0x88b000, 0x88a000, "AGC_tbl", true},
101 	/* Pcie_ext_rgf 4k */
102 	{0x88b000, 0x88c000, 0x88b000, "rgf_ext", true},
103 	/* mac_ext_rgf 512b */
104 	{0x88c000, 0x88c200, 0x88c000, "mac_rgf_ext", true},
105 	/* upper area 548k */
106 	{0x8c0000, 0x949000, 0x8c0000, "upper", 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},
112 	/* ucode data RAM 16k */
113 	{0x800000, 0x804000, 0x940000, "uc_data", 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
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},
138 	/* FW data RAM 128k */
139 	{0x800000, 0x820000, 0xa00000, "fw_data", true},
140 	/* periph. data RAM 96k */
141 	{0x840000, 0x858000, 0xa20000, "fw_peri", true},
142 	/* various RGF 40k */
143 	{0x880000, 0x88a000, 0x880000, "rgf", true},
144 	/* AGC table 4k */
145 	{0x88a000, 0x88b000, 0x88a000, "AGC_tbl", true},
146 	/* Pcie_ext_rgf 4k */
147 	{0x88b000, 0x88c000, 0x88b000, "rgf_ext", true},
148 	/* mac_ext_rgf 1344b */
149 	{0x88c000, 0x88c540, 0x88c000, "mac_rgf_ext", true},
150 	/* ext USER RGF 4k */
151 	{0x88d000, 0x88e000, 0x88d000, "ext_user_rgf", true},
152 	/* OTP 4k */
153 	{0x8a0000, 0x8a1000, 0x8a0000, "otp", true},
154 	/* DMA EXT RGF 64k */
155 	{0x8b0000, 0x8c0000, 0x8b0000, "dma_ext_rgf", true},
156 	/* upper area 1536k */
157 	{0x900000, 0xa80000, 0x900000, "upper", 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},
163 	/* ucode data RAM 32k */
164 	{0x800000, 0x808000, 0xa78000, "uc_data", false},
165 };
166 
167 struct fw_map fw_mapping[MAX_FW_MAPPING_TABLE_SIZE];
168 
169 struct blink_on_off_time led_blink_time[] = {
170 	{WIL_LED_BLINK_ON_SLOW_MS, WIL_LED_BLINK_OFF_SLOW_MS},
171 	{WIL_LED_BLINK_ON_MED_MS, WIL_LED_BLINK_OFF_MED_MS},
172 	{WIL_LED_BLINK_ON_FAST_MS, WIL_LED_BLINK_OFF_FAST_MS},
173 };
174 
175 u8 led_polarity = LED_POLARITY_LOW_ACTIVE;
176 
177 /**
178  * return AHB address for given firmware internal (linker) address
179  * @x - internal address
180  * If address have no valid AHB mapping, return 0
181  */
182 static u32 wmi_addr_remap(u32 x)
183 {
184 	uint i;
185 
186 	for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
187 		if (fw_mapping[i].fw &&
188 		    ((x >= fw_mapping[i].from) && (x < fw_mapping[i].to)))
189 			return x + fw_mapping[i].host - fw_mapping[i].from;
190 	}
191 
192 	return 0;
193 }
194 
195 /**
196  * find fw_mapping entry by section name
197  * @section - section name
198  *
199  * Return pointer to section or NULL if not found
200  */
201 struct fw_map *wil_find_fw_mapping(const char *section)
202 {
203 	int i;
204 
205 	for (i = 0; i < ARRAY_SIZE(fw_mapping); i++)
206 		if (fw_mapping[i].name &&
207 		    !strcmp(section, fw_mapping[i].name))
208 			return &fw_mapping[i];
209 
210 	return NULL;
211 }
212 
213 /**
214  * Check address validity for WMI buffer; remap if needed
215  * @ptr - internal (linker) fw/ucode address
216  * @size - if non zero, validate the block does not
217  *  exceed the device memory (bar)
218  *
219  * Valid buffer should be DWORD aligned
220  *
221  * return address for accessing buffer from the host;
222  * if buffer is not valid, return NULL.
223  */
224 void __iomem *wmi_buffer_block(struct wil6210_priv *wil, __le32 ptr_, u32 size)
225 {
226 	u32 off;
227 	u32 ptr = le32_to_cpu(ptr_);
228 
229 	if (ptr % 4)
230 		return NULL;
231 
232 	ptr = wmi_addr_remap(ptr);
233 	if (ptr < WIL6210_FW_HOST_OFF)
234 		return NULL;
235 
236 	off = HOSTADDR(ptr);
237 	if (off > wil->bar_size - 4)
238 		return NULL;
239 	if (size && ((off + size > wil->bar_size) || (off + size < off)))
240 		return NULL;
241 
242 	return wil->csr + off;
243 }
244 
245 void __iomem *wmi_buffer(struct wil6210_priv *wil, __le32 ptr_)
246 {
247 	return wmi_buffer_block(wil, ptr_, 0);
248 }
249 
250 /**
251  * Check address validity
252  */
253 void __iomem *wmi_addr(struct wil6210_priv *wil, u32 ptr)
254 {
255 	u32 off;
256 
257 	if (ptr % 4)
258 		return NULL;
259 
260 	if (ptr < WIL6210_FW_HOST_OFF)
261 		return NULL;
262 
263 	off = HOSTADDR(ptr);
264 	if (off > wil->bar_size - 4)
265 		return NULL;
266 
267 	return wil->csr + off;
268 }
269 
270 int wmi_read_hdr(struct wil6210_priv *wil, __le32 ptr,
271 		 struct wil6210_mbox_hdr *hdr)
272 {
273 	void __iomem *src = wmi_buffer(wil, ptr);
274 
275 	if (!src)
276 		return -EINVAL;
277 
278 	wil_memcpy_fromio_32(hdr, src, sizeof(*hdr));
279 
280 	return 0;
281 }
282 
283 static const char *cmdid2name(u16 cmdid)
284 {
285 	switch (cmdid) {
286 	case WMI_NOTIFY_REQ_CMDID:
287 		return "WMI_NOTIFY_REQ_CMD";
288 	case WMI_START_SCAN_CMDID:
289 		return "WMI_START_SCAN_CMD";
290 	case WMI_CONNECT_CMDID:
291 		return "WMI_CONNECT_CMD";
292 	case WMI_DISCONNECT_CMDID:
293 		return "WMI_DISCONNECT_CMD";
294 	case WMI_SW_TX_REQ_CMDID:
295 		return "WMI_SW_TX_REQ_CMD";
296 	case WMI_GET_RF_SECTOR_PARAMS_CMDID:
297 		return "WMI_GET_RF_SECTOR_PARAMS_CMD";
298 	case WMI_SET_RF_SECTOR_PARAMS_CMDID:
299 		return "WMI_SET_RF_SECTOR_PARAMS_CMD";
300 	case WMI_GET_SELECTED_RF_SECTOR_INDEX_CMDID:
301 		return "WMI_GET_SELECTED_RF_SECTOR_INDEX_CMD";
302 	case WMI_SET_SELECTED_RF_SECTOR_INDEX_CMDID:
303 		return "WMI_SET_SELECTED_RF_SECTOR_INDEX_CMD";
304 	case WMI_BRP_SET_ANT_LIMIT_CMDID:
305 		return "WMI_BRP_SET_ANT_LIMIT_CMD";
306 	case WMI_TOF_SESSION_START_CMDID:
307 		return "WMI_TOF_SESSION_START_CMD";
308 	case WMI_AOA_MEAS_CMDID:
309 		return "WMI_AOA_MEAS_CMD";
310 	case WMI_PMC_CMDID:
311 		return "WMI_PMC_CMD";
312 	case WMI_TOF_GET_TX_RX_OFFSET_CMDID:
313 		return "WMI_TOF_GET_TX_RX_OFFSET_CMD";
314 	case WMI_TOF_SET_TX_RX_OFFSET_CMDID:
315 		return "WMI_TOF_SET_TX_RX_OFFSET_CMD";
316 	case WMI_VRING_CFG_CMDID:
317 		return "WMI_VRING_CFG_CMD";
318 	case WMI_BCAST_VRING_CFG_CMDID:
319 		return "WMI_BCAST_VRING_CFG_CMD";
320 	case WMI_TRAFFIC_SUSPEND_CMDID:
321 		return "WMI_TRAFFIC_SUSPEND_CMD";
322 	case WMI_TRAFFIC_RESUME_CMDID:
323 		return "WMI_TRAFFIC_RESUME_CMD";
324 	case WMI_ECHO_CMDID:
325 		return "WMI_ECHO_CMD";
326 	case WMI_SET_MAC_ADDRESS_CMDID:
327 		return "WMI_SET_MAC_ADDRESS_CMD";
328 	case WMI_LED_CFG_CMDID:
329 		return "WMI_LED_CFG_CMD";
330 	case WMI_PCP_START_CMDID:
331 		return "WMI_PCP_START_CMD";
332 	case WMI_PCP_STOP_CMDID:
333 		return "WMI_PCP_STOP_CMD";
334 	case WMI_SET_SSID_CMDID:
335 		return "WMI_SET_SSID_CMD";
336 	case WMI_GET_SSID_CMDID:
337 		return "WMI_GET_SSID_CMD";
338 	case WMI_SET_PCP_CHANNEL_CMDID:
339 		return "WMI_SET_PCP_CHANNEL_CMD";
340 	case WMI_GET_PCP_CHANNEL_CMDID:
341 		return "WMI_GET_PCP_CHANNEL_CMD";
342 	case WMI_P2P_CFG_CMDID:
343 		return "WMI_P2P_CFG_CMD";
344 	case WMI_PORT_ALLOCATE_CMDID:
345 		return "WMI_PORT_ALLOCATE_CMD";
346 	case WMI_PORT_DELETE_CMDID:
347 		return "WMI_PORT_DELETE_CMD";
348 	case WMI_START_LISTEN_CMDID:
349 		return "WMI_START_LISTEN_CMD";
350 	case WMI_START_SEARCH_CMDID:
351 		return "WMI_START_SEARCH_CMD";
352 	case WMI_DISCOVERY_STOP_CMDID:
353 		return "WMI_DISCOVERY_STOP_CMD";
354 	case WMI_DELETE_CIPHER_KEY_CMDID:
355 		return "WMI_DELETE_CIPHER_KEY_CMD";
356 	case WMI_ADD_CIPHER_KEY_CMDID:
357 		return "WMI_ADD_CIPHER_KEY_CMD";
358 	case WMI_SET_APPIE_CMDID:
359 		return "WMI_SET_APPIE_CMD";
360 	case WMI_CFG_RX_CHAIN_CMDID:
361 		return "WMI_CFG_RX_CHAIN_CMD";
362 	case WMI_TEMP_SENSE_CMDID:
363 		return "WMI_TEMP_SENSE_CMD";
364 	case WMI_DEL_STA_CMDID:
365 		return "WMI_DEL_STA_CMD";
366 	case WMI_DISCONNECT_STA_CMDID:
367 		return "WMI_DISCONNECT_STA_CMD";
368 	case WMI_VRING_BA_EN_CMDID:
369 		return "WMI_VRING_BA_EN_CMD";
370 	case WMI_VRING_BA_DIS_CMDID:
371 		return "WMI_VRING_BA_DIS_CMD";
372 	case WMI_RCP_DELBA_CMDID:
373 		return "WMI_RCP_DELBA_CMD";
374 	case WMI_RCP_ADDBA_RESP_CMDID:
375 		return "WMI_RCP_ADDBA_RESP_CMD";
376 	case WMI_PS_DEV_PROFILE_CFG_CMDID:
377 		return "WMI_PS_DEV_PROFILE_CFG_CMD";
378 	case WMI_SET_MGMT_RETRY_LIMIT_CMDID:
379 		return "WMI_SET_MGMT_RETRY_LIMIT_CMD";
380 	case WMI_GET_MGMT_RETRY_LIMIT_CMDID:
381 		return "WMI_GET_MGMT_RETRY_LIMIT_CMD";
382 	case WMI_ABORT_SCAN_CMDID:
383 		return "WMI_ABORT_SCAN_CMD";
384 	case WMI_NEW_STA_CMDID:
385 		return "WMI_NEW_STA_CMD";
386 	case WMI_SET_THERMAL_THROTTLING_CFG_CMDID:
387 		return "WMI_SET_THERMAL_THROTTLING_CFG_CMD";
388 	case WMI_GET_THERMAL_THROTTLING_CFG_CMDID:
389 		return "WMI_GET_THERMAL_THROTTLING_CFG_CMD";
390 	case WMI_LINK_MAINTAIN_CFG_WRITE_CMDID:
391 		return "WMI_LINK_MAINTAIN_CFG_WRITE_CMD";
392 	case WMI_LO_POWER_CALIB_FROM_OTP_CMDID:
393 		return "WMI_LO_POWER_CALIB_FROM_OTP_CMD";
394 	case WMI_START_SCHED_SCAN_CMDID:
395 		return "WMI_START_SCHED_SCAN_CMD";
396 	case WMI_STOP_SCHED_SCAN_CMDID:
397 		return "WMI_STOP_SCHED_SCAN_CMD";
398 	default:
399 		return "Untracked CMD";
400 	}
401 }
402 
403 static const char *eventid2name(u16 eventid)
404 {
405 	switch (eventid) {
406 	case WMI_NOTIFY_REQ_DONE_EVENTID:
407 		return "WMI_NOTIFY_REQ_DONE_EVENT";
408 	case WMI_DISCONNECT_EVENTID:
409 		return "WMI_DISCONNECT_EVENT";
410 	case WMI_SW_TX_COMPLETE_EVENTID:
411 		return "WMI_SW_TX_COMPLETE_EVENT";
412 	case WMI_GET_RF_SECTOR_PARAMS_DONE_EVENTID:
413 		return "WMI_GET_RF_SECTOR_PARAMS_DONE_EVENT";
414 	case WMI_SET_RF_SECTOR_PARAMS_DONE_EVENTID:
415 		return "WMI_SET_RF_SECTOR_PARAMS_DONE_EVENT";
416 	case WMI_GET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID:
417 		return "WMI_GET_SELECTED_RF_SECTOR_INDEX_DONE_EVENT";
418 	case WMI_SET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID:
419 		return "WMI_SET_SELECTED_RF_SECTOR_INDEX_DONE_EVENT";
420 	case WMI_BRP_SET_ANT_LIMIT_EVENTID:
421 		return "WMI_BRP_SET_ANT_LIMIT_EVENT";
422 	case WMI_FW_READY_EVENTID:
423 		return "WMI_FW_READY_EVENT";
424 	case WMI_TRAFFIC_RESUME_EVENTID:
425 		return "WMI_TRAFFIC_RESUME_EVENT";
426 	case WMI_TOF_GET_TX_RX_OFFSET_EVENTID:
427 		return "WMI_TOF_GET_TX_RX_OFFSET_EVENT";
428 	case WMI_TOF_SET_TX_RX_OFFSET_EVENTID:
429 		return "WMI_TOF_SET_TX_RX_OFFSET_EVENT";
430 	case WMI_VRING_CFG_DONE_EVENTID:
431 		return "WMI_VRING_CFG_DONE_EVENT";
432 	case WMI_READY_EVENTID:
433 		return "WMI_READY_EVENT";
434 	case WMI_RX_MGMT_PACKET_EVENTID:
435 		return "WMI_RX_MGMT_PACKET_EVENT";
436 	case WMI_TX_MGMT_PACKET_EVENTID:
437 		return "WMI_TX_MGMT_PACKET_EVENT";
438 	case WMI_SCAN_COMPLETE_EVENTID:
439 		return "WMI_SCAN_COMPLETE_EVENT";
440 	case WMI_ACS_PASSIVE_SCAN_COMPLETE_EVENTID:
441 		return "WMI_ACS_PASSIVE_SCAN_COMPLETE_EVENT";
442 	case WMI_CONNECT_EVENTID:
443 		return "WMI_CONNECT_EVENT";
444 	case WMI_EAPOL_RX_EVENTID:
445 		return "WMI_EAPOL_RX_EVENT";
446 	case WMI_BA_STATUS_EVENTID:
447 		return "WMI_BA_STATUS_EVENT";
448 	case WMI_RCP_ADDBA_REQ_EVENTID:
449 		return "WMI_RCP_ADDBA_REQ_EVENT";
450 	case WMI_DELBA_EVENTID:
451 		return "WMI_DELBA_EVENT";
452 	case WMI_VRING_EN_EVENTID:
453 		return "WMI_VRING_EN_EVENT";
454 	case WMI_DATA_PORT_OPEN_EVENTID:
455 		return "WMI_DATA_PORT_OPEN_EVENT";
456 	case WMI_AOA_MEAS_EVENTID:
457 		return "WMI_AOA_MEAS_EVENT";
458 	case WMI_TOF_SESSION_END_EVENTID:
459 		return "WMI_TOF_SESSION_END_EVENT";
460 	case WMI_TOF_GET_CAPABILITIES_EVENTID:
461 		return "WMI_TOF_GET_CAPABILITIES_EVENT";
462 	case WMI_TOF_SET_LCR_EVENTID:
463 		return "WMI_TOF_SET_LCR_EVENT";
464 	case WMI_TOF_SET_LCI_EVENTID:
465 		return "WMI_TOF_SET_LCI_EVENT";
466 	case WMI_TOF_FTM_PER_DEST_RES_EVENTID:
467 		return "WMI_TOF_FTM_PER_DEST_RES_EVENT";
468 	case WMI_TOF_CHANNEL_INFO_EVENTID:
469 		return "WMI_TOF_CHANNEL_INFO_EVENT";
470 	case WMI_TRAFFIC_SUSPEND_EVENTID:
471 		return "WMI_TRAFFIC_SUSPEND_EVENT";
472 	case WMI_ECHO_RSP_EVENTID:
473 		return "WMI_ECHO_RSP_EVENT";
474 	case WMI_LED_CFG_DONE_EVENTID:
475 		return "WMI_LED_CFG_DONE_EVENT";
476 	case WMI_PCP_STARTED_EVENTID:
477 		return "WMI_PCP_STARTED_EVENT";
478 	case WMI_PCP_STOPPED_EVENTID:
479 		return "WMI_PCP_STOPPED_EVENT";
480 	case WMI_GET_SSID_EVENTID:
481 		return "WMI_GET_SSID_EVENT";
482 	case WMI_GET_PCP_CHANNEL_EVENTID:
483 		return "WMI_GET_PCP_CHANNEL_EVENT";
484 	case WMI_P2P_CFG_DONE_EVENTID:
485 		return "WMI_P2P_CFG_DONE_EVENT";
486 	case WMI_PORT_ALLOCATED_EVENTID:
487 		return "WMI_PORT_ALLOCATED_EVENT";
488 	case WMI_PORT_DELETED_EVENTID:
489 		return "WMI_PORT_DELETED_EVENT";
490 	case WMI_LISTEN_STARTED_EVENTID:
491 		return "WMI_LISTEN_STARTED_EVENT";
492 	case WMI_SEARCH_STARTED_EVENTID:
493 		return "WMI_SEARCH_STARTED_EVENT";
494 	case WMI_DISCOVERY_STOPPED_EVENTID:
495 		return "WMI_DISCOVERY_STOPPED_EVENT";
496 	case WMI_CFG_RX_CHAIN_DONE_EVENTID:
497 		return "WMI_CFG_RX_CHAIN_DONE_EVENT";
498 	case WMI_TEMP_SENSE_DONE_EVENTID:
499 		return "WMI_TEMP_SENSE_DONE_EVENT";
500 	case WMI_RCP_ADDBA_RESP_SENT_EVENTID:
501 		return "WMI_RCP_ADDBA_RESP_SENT_EVENT";
502 	case WMI_PS_DEV_PROFILE_CFG_EVENTID:
503 		return "WMI_PS_DEV_PROFILE_CFG_EVENT";
504 	case WMI_SET_MGMT_RETRY_LIMIT_EVENTID:
505 		return "WMI_SET_MGMT_RETRY_LIMIT_EVENT";
506 	case WMI_GET_MGMT_RETRY_LIMIT_EVENTID:
507 		return "WMI_GET_MGMT_RETRY_LIMIT_EVENT";
508 	case WMI_SET_THERMAL_THROTTLING_CFG_EVENTID:
509 		return "WMI_SET_THERMAL_THROTTLING_CFG_EVENT";
510 	case WMI_GET_THERMAL_THROTTLING_CFG_EVENTID:
511 		return "WMI_GET_THERMAL_THROTTLING_CFG_EVENT";
512 	case WMI_LINK_MAINTAIN_CFG_WRITE_DONE_EVENTID:
513 		return "WMI_LINK_MAINTAIN_CFG_WRITE_DONE_EVENT";
514 	case WMI_LO_POWER_CALIB_FROM_OTP_EVENTID:
515 		return "WMI_LO_POWER_CALIB_FROM_OTP_EVENT";
516 	case WMI_START_SCHED_SCAN_EVENTID:
517 		return "WMI_START_SCHED_SCAN_EVENT";
518 	case WMI_STOP_SCHED_SCAN_EVENTID:
519 		return "WMI_STOP_SCHED_SCAN_EVENT";
520 	case WMI_SCHED_SCAN_RESULT_EVENTID:
521 		return "WMI_SCHED_SCAN_RESULT_EVENT";
522 	default:
523 		return "Untracked EVENT";
524 	}
525 }
526 
527 static int __wmi_send(struct wil6210_priv *wil, u16 cmdid, u8 mid,
528 		      void *buf, u16 len)
529 {
530 	struct {
531 		struct wil6210_mbox_hdr hdr;
532 		struct wmi_cmd_hdr wmi;
533 	} __packed cmd = {
534 		.hdr = {
535 			.type = WIL_MBOX_HDR_TYPE_WMI,
536 			.flags = 0,
537 			.len = cpu_to_le16(sizeof(cmd.wmi) + len),
538 		},
539 		.wmi = {
540 			.mid = mid,
541 			.command_id = cpu_to_le16(cmdid),
542 		},
543 	};
544 	struct wil6210_mbox_ring *r = &wil->mbox_ctl.tx;
545 	struct wil6210_mbox_ring_desc d_head;
546 	u32 next_head;
547 	void __iomem *dst;
548 	void __iomem *head = wmi_addr(wil, r->head);
549 	uint retry;
550 	int rc = 0;
551 
552 	if (len > r->entry_size - sizeof(cmd)) {
553 		wil_err(wil, "WMI size too large: %d bytes, max is %d\n",
554 			(int)(sizeof(cmd) + len), r->entry_size);
555 		return -ERANGE;
556 	}
557 
558 	might_sleep();
559 
560 	if (!test_bit(wil_status_fwready, wil->status)) {
561 		wil_err(wil, "WMI: cannot send command while FW not ready\n");
562 		return -EAGAIN;
563 	}
564 
565 	/* Allow sending only suspend / resume commands during susepnd flow */
566 	if ((test_bit(wil_status_suspending, wil->status) ||
567 	     test_bit(wil_status_suspended, wil->status) ||
568 	     test_bit(wil_status_resuming, wil->status)) &&
569 	     ((cmdid != WMI_TRAFFIC_SUSPEND_CMDID) &&
570 	      (cmdid != WMI_TRAFFIC_RESUME_CMDID))) {
571 		wil_err(wil, "WMI: reject send_command during suspend\n");
572 		return -EINVAL;
573 	}
574 
575 	if (!head) {
576 		wil_err(wil, "WMI head is garbage: 0x%08x\n", r->head);
577 		return -EINVAL;
578 	}
579 
580 	wil_halp_vote(wil);
581 
582 	/* read Tx head till it is not busy */
583 	for (retry = 5; retry > 0; retry--) {
584 		wil_memcpy_fromio_32(&d_head, head, sizeof(d_head));
585 		if (d_head.sync == 0)
586 			break;
587 		msleep(20);
588 	}
589 	if (d_head.sync != 0) {
590 		wil_err(wil, "WMI head busy\n");
591 		rc = -EBUSY;
592 		goto out;
593 	}
594 	/* next head */
595 	next_head = r->base + ((r->head - r->base + sizeof(d_head)) % r->size);
596 	wil_dbg_wmi(wil, "Head 0x%08x -> 0x%08x\n", r->head, next_head);
597 	/* wait till FW finish with previous command */
598 	for (retry = 5; retry > 0; retry--) {
599 		if (!test_bit(wil_status_fwready, wil->status)) {
600 			wil_err(wil, "WMI: cannot send command while FW not ready\n");
601 			rc = -EAGAIN;
602 			goto out;
603 		}
604 		r->tail = wil_r(wil, RGF_MBOX +
605 				offsetof(struct wil6210_mbox_ctl, tx.tail));
606 		if (next_head != r->tail)
607 			break;
608 		msleep(20);
609 	}
610 	if (next_head == r->tail) {
611 		wil_err(wil, "WMI ring full\n");
612 		rc = -EBUSY;
613 		goto out;
614 	}
615 	dst = wmi_buffer(wil, d_head.addr);
616 	if (!dst) {
617 		wil_err(wil, "invalid WMI buffer: 0x%08x\n",
618 			le32_to_cpu(d_head.addr));
619 		rc = -EAGAIN;
620 		goto out;
621 	}
622 	cmd.hdr.seq = cpu_to_le16(++wil->wmi_seq);
623 	/* set command */
624 	wil_dbg_wmi(wil, "sending %s (0x%04x) [%d] mid %d\n",
625 		    cmdid2name(cmdid), cmdid, len, mid);
626 	wil_hex_dump_wmi("Cmd ", DUMP_PREFIX_OFFSET, 16, 1, &cmd,
627 			 sizeof(cmd), true);
628 	wil_hex_dump_wmi("cmd ", DUMP_PREFIX_OFFSET, 16, 1, buf,
629 			 len, true);
630 	wil_memcpy_toio_32(dst, &cmd, sizeof(cmd));
631 	wil_memcpy_toio_32(dst + sizeof(cmd), buf, len);
632 	/* mark entry as full */
633 	wil_w(wil, r->head + offsetof(struct wil6210_mbox_ring_desc, sync), 1);
634 	/* advance next ptr */
635 	wil_w(wil, RGF_MBOX + offsetof(struct wil6210_mbox_ctl, tx.head),
636 	      r->head = next_head);
637 
638 	trace_wil6210_wmi_cmd(&cmd.wmi, buf, len);
639 
640 	/* interrupt to FW */
641 	wil_w(wil, RGF_USER_USER_ICR + offsetof(struct RGF_ICR, ICS),
642 	      SW_INT_MBOX);
643 
644 out:
645 	wil_halp_unvote(wil);
646 	return rc;
647 }
648 
649 int wmi_send(struct wil6210_priv *wil, u16 cmdid, u8 mid, void *buf, u16 len)
650 {
651 	int rc;
652 
653 	mutex_lock(&wil->wmi_mutex);
654 	rc = __wmi_send(wil, cmdid, mid, buf, len);
655 	mutex_unlock(&wil->wmi_mutex);
656 
657 	return rc;
658 }
659 
660 /*=== Event handlers ===*/
661 static void wmi_evt_ready(struct wil6210_vif *vif, int id, void *d, int len)
662 {
663 	struct wil6210_priv *wil = vif_to_wil(vif);
664 	struct wiphy *wiphy = wil_to_wiphy(wil);
665 	struct wmi_ready_event *evt = d;
666 
667 	wil_info(wil, "FW ver. %s(SW %d); MAC %pM; %d MID's\n",
668 		 wil->fw_version, le32_to_cpu(evt->sw_version),
669 		 evt->mac, evt->numof_additional_mids);
670 	if (evt->numof_additional_mids + 1 < wil->max_vifs) {
671 		wil_err(wil, "FW does not support enough MIDs (need %d)",
672 			wil->max_vifs - 1);
673 		return; /* FW load will fail after timeout */
674 	}
675 	/* ignore MAC address, we already have it from the boot loader */
676 	strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
677 
678 	if (len > offsetof(struct wmi_ready_event, rfc_read_calib_result)) {
679 		wil_dbg_wmi(wil, "rfc calibration result %d\n",
680 			    evt->rfc_read_calib_result);
681 		wil->fw_calib_result = evt->rfc_read_calib_result;
682 	}
683 	wil_set_recovery_state(wil, fw_recovery_idle);
684 	set_bit(wil_status_fwready, wil->status);
685 	/* let the reset sequence continue */
686 	complete(&wil->wmi_ready);
687 }
688 
689 static void wmi_evt_rx_mgmt(struct wil6210_vif *vif, int id, void *d, int len)
690 {
691 	struct wil6210_priv *wil = vif_to_wil(vif);
692 	struct wmi_rx_mgmt_packet_event *data = d;
693 	struct wiphy *wiphy = wil_to_wiphy(wil);
694 	struct ieee80211_mgmt *rx_mgmt_frame =
695 			(struct ieee80211_mgmt *)data->payload;
696 	int flen = len - offsetof(struct wmi_rx_mgmt_packet_event, payload);
697 	int ch_no;
698 	u32 freq;
699 	struct ieee80211_channel *channel;
700 	s32 signal;
701 	__le16 fc;
702 	u32 d_len;
703 	u16 d_status;
704 
705 	if (flen < 0) {
706 		wil_err(wil, "MGMT Rx: short event, len %d\n", len);
707 		return;
708 	}
709 
710 	d_len = le32_to_cpu(data->info.len);
711 	if (d_len != flen) {
712 		wil_err(wil,
713 			"MGMT Rx: length mismatch, d_len %d should be %d\n",
714 			d_len, flen);
715 		return;
716 	}
717 
718 	ch_no = data->info.channel + 1;
719 	freq = ieee80211_channel_to_frequency(ch_no, NL80211_BAND_60GHZ);
720 	channel = ieee80211_get_channel(wiphy, freq);
721 	if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
722 		signal = 100 * data->info.rssi;
723 	else
724 		signal = data->info.sqi;
725 	d_status = le16_to_cpu(data->info.status);
726 	fc = rx_mgmt_frame->frame_control;
727 
728 	wil_dbg_wmi(wil, "MGMT Rx: channel %d MCS %d RSSI %d SQI %d%%\n",
729 		    data->info.channel, data->info.mcs, data->info.rssi,
730 		    data->info.sqi);
731 	wil_dbg_wmi(wil, "status 0x%04x len %d fc 0x%04x\n", d_status, d_len,
732 		    le16_to_cpu(fc));
733 	wil_dbg_wmi(wil, "qid %d mid %d cid %d\n",
734 		    data->info.qid, data->info.mid, data->info.cid);
735 	wil_hex_dump_wmi("MGMT Rx ", DUMP_PREFIX_OFFSET, 16, 1, rx_mgmt_frame,
736 			 d_len, true);
737 
738 	if (!channel) {
739 		wil_err(wil, "Frame on unsupported channel\n");
740 		return;
741 	}
742 
743 	if (ieee80211_is_beacon(fc) || ieee80211_is_probe_resp(fc)) {
744 		struct cfg80211_bss *bss;
745 		u64 tsf = le64_to_cpu(rx_mgmt_frame->u.beacon.timestamp);
746 		u16 cap = le16_to_cpu(rx_mgmt_frame->u.beacon.capab_info);
747 		u16 bi = le16_to_cpu(rx_mgmt_frame->u.beacon.beacon_int);
748 		const u8 *ie_buf = rx_mgmt_frame->u.beacon.variable;
749 		size_t ie_len = d_len - offsetof(struct ieee80211_mgmt,
750 						 u.beacon.variable);
751 		wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
752 		wil_dbg_wmi(wil, "TSF : 0x%016llx\n", tsf);
753 		wil_dbg_wmi(wil, "Beacon interval : %d\n", bi);
754 		wil_hex_dump_wmi("IE ", DUMP_PREFIX_OFFSET, 16, 1, ie_buf,
755 				 ie_len, true);
756 
757 		wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
758 
759 		bss = cfg80211_inform_bss_frame(wiphy, channel, rx_mgmt_frame,
760 						d_len, signal, GFP_KERNEL);
761 		if (bss) {
762 			wil_dbg_wmi(wil, "Added BSS %pM\n",
763 				    rx_mgmt_frame->bssid);
764 			cfg80211_put_bss(wiphy, bss);
765 		} else {
766 			wil_err(wil, "cfg80211_inform_bss_frame() failed\n");
767 		}
768 	} else {
769 		mutex_lock(&wil->vif_mutex);
770 		cfg80211_rx_mgmt(vif_to_radio_wdev(wil, vif), freq, signal,
771 				 (void *)rx_mgmt_frame, d_len, 0);
772 		mutex_unlock(&wil->vif_mutex);
773 	}
774 }
775 
776 static void wmi_evt_tx_mgmt(struct wil6210_vif *vif, int id, void *d, int len)
777 {
778 	struct wmi_tx_mgmt_packet_event *data = d;
779 	struct ieee80211_mgmt *mgmt_frame =
780 			(struct ieee80211_mgmt *)data->payload;
781 	int flen = len - offsetof(struct wmi_tx_mgmt_packet_event, payload);
782 
783 	wil_hex_dump_wmi("MGMT Tx ", DUMP_PREFIX_OFFSET, 16, 1, mgmt_frame,
784 			 flen, true);
785 }
786 
787 static void wmi_evt_scan_complete(struct wil6210_vif *vif, int id,
788 				  void *d, int len)
789 {
790 	struct wil6210_priv *wil = vif_to_wil(vif);
791 
792 	mutex_lock(&wil->vif_mutex);
793 	if (vif->scan_request) {
794 		struct wmi_scan_complete_event *data = d;
795 		int status = le32_to_cpu(data->status);
796 		struct cfg80211_scan_info info = {
797 			.aborted = ((status != WMI_SCAN_SUCCESS) &&
798 				(status != WMI_SCAN_ABORT_REJECTED)),
799 		};
800 
801 		wil_dbg_wmi(wil, "SCAN_COMPLETE(0x%08x)\n", status);
802 		wil_dbg_misc(wil, "Complete scan_request 0x%p aborted %d\n",
803 			     vif->scan_request, info.aborted);
804 		del_timer_sync(&vif->scan_timer);
805 		cfg80211_scan_done(vif->scan_request, &info);
806 		if (vif->mid == 0)
807 			wil->radio_wdev = wil->main_ndev->ieee80211_ptr;
808 		vif->scan_request = NULL;
809 		wake_up_interruptible(&wil->wq);
810 		if (vif->p2p.pending_listen_wdev) {
811 			wil_dbg_misc(wil, "Scheduling delayed listen\n");
812 			schedule_work(&vif->p2p.delayed_listen_work);
813 		}
814 	} else {
815 		wil_err(wil, "SCAN_COMPLETE while not scanning\n");
816 	}
817 	mutex_unlock(&wil->vif_mutex);
818 }
819 
820 static void wmi_evt_connect(struct wil6210_vif *vif, int id, void *d, int len)
821 {
822 	struct wil6210_priv *wil = vif_to_wil(vif);
823 	struct net_device *ndev = vif_to_ndev(vif);
824 	struct wireless_dev *wdev = vif_to_wdev(vif);
825 	struct wmi_connect_event *evt = d;
826 	int ch; /* channel number */
827 	struct station_info *sinfo;
828 	u8 *assoc_req_ie, *assoc_resp_ie;
829 	size_t assoc_req_ielen, assoc_resp_ielen;
830 	/* capinfo(u16) + listen_interval(u16) + IEs */
831 	const size_t assoc_req_ie_offset = sizeof(u16) * 2;
832 	/* capinfo(u16) + status_code(u16) + associd(u16) + IEs */
833 	const size_t assoc_resp_ie_offset = sizeof(u16) * 3;
834 	int rc;
835 
836 	if (len < sizeof(*evt)) {
837 		wil_err(wil, "Connect event too short : %d bytes\n", len);
838 		return;
839 	}
840 	if (len != sizeof(*evt) + evt->beacon_ie_len + evt->assoc_req_len +
841 		   evt->assoc_resp_len) {
842 		wil_err(wil,
843 			"Connect event corrupted : %d != %d + %d + %d + %d\n",
844 			len, (int)sizeof(*evt), evt->beacon_ie_len,
845 			evt->assoc_req_len, evt->assoc_resp_len);
846 		return;
847 	}
848 	if (evt->cid >= WIL6210_MAX_CID) {
849 		wil_err(wil, "Connect CID invalid : %d\n", evt->cid);
850 		return;
851 	}
852 
853 	ch = evt->channel + 1;
854 	wil_info(wil, "Connect %pM channel [%d] cid %d aid %d\n",
855 		 evt->bssid, ch, evt->cid, evt->aid);
856 	wil_hex_dump_wmi("connect AI : ", DUMP_PREFIX_OFFSET, 16, 1,
857 			 evt->assoc_info, len - sizeof(*evt), true);
858 
859 	/* figure out IE's */
860 	assoc_req_ie = &evt->assoc_info[evt->beacon_ie_len +
861 					assoc_req_ie_offset];
862 	assoc_req_ielen = evt->assoc_req_len - assoc_req_ie_offset;
863 	if (evt->assoc_req_len <= assoc_req_ie_offset) {
864 		assoc_req_ie = NULL;
865 		assoc_req_ielen = 0;
866 	}
867 
868 	assoc_resp_ie = &evt->assoc_info[evt->beacon_ie_len +
869 					 evt->assoc_req_len +
870 					 assoc_resp_ie_offset];
871 	assoc_resp_ielen = evt->assoc_resp_len - assoc_resp_ie_offset;
872 	if (evt->assoc_resp_len <= assoc_resp_ie_offset) {
873 		assoc_resp_ie = NULL;
874 		assoc_resp_ielen = 0;
875 	}
876 
877 	if (test_bit(wil_status_resetting, wil->status) ||
878 	    !test_bit(wil_status_fwready, wil->status)) {
879 		wil_err(wil, "status_resetting, cancel connect event, CID %d\n",
880 			evt->cid);
881 		/* no need for cleanup, wil_reset will do that */
882 		return;
883 	}
884 
885 	mutex_lock(&wil->mutex);
886 
887 	if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
888 	    (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
889 		if (!test_bit(wil_vif_fwconnecting, vif->status)) {
890 			wil_err(wil, "Not in connecting state\n");
891 			mutex_unlock(&wil->mutex);
892 			return;
893 		}
894 		del_timer_sync(&vif->connect_timer);
895 	} else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
896 		   (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
897 		if (wil->sta[evt->cid].status != wil_sta_unused) {
898 			wil_err(wil, "AP: Invalid status %d for CID %d\n",
899 				wil->sta[evt->cid].status, evt->cid);
900 			mutex_unlock(&wil->mutex);
901 			return;
902 		}
903 	}
904 
905 	ether_addr_copy(wil->sta[evt->cid].addr, evt->bssid);
906 	wil->sta[evt->cid].mid = vif->mid;
907 	wil->sta[evt->cid].status = wil_sta_conn_pending;
908 
909 	rc = wil_tx_init(vif, evt->cid);
910 	if (rc) {
911 		wil_err(wil, "config tx vring failed for CID %d, rc (%d)\n",
912 			evt->cid, rc);
913 		wmi_disconnect_sta(vif, wil->sta[evt->cid].addr,
914 				   WLAN_REASON_UNSPECIFIED, false, false);
915 	} else {
916 		wil_info(wil, "successful connection to CID %d\n", evt->cid);
917 	}
918 
919 	if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
920 	    (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
921 		if (rc) {
922 			netif_carrier_off(ndev);
923 			wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
924 			wil_err(wil, "cfg80211_connect_result with failure\n");
925 			cfg80211_connect_result(ndev, evt->bssid, NULL, 0,
926 						NULL, 0,
927 						WLAN_STATUS_UNSPECIFIED_FAILURE,
928 						GFP_KERNEL);
929 			goto out;
930 		} else {
931 			struct wiphy *wiphy = wil_to_wiphy(wil);
932 
933 			cfg80211_ref_bss(wiphy, vif->bss);
934 			cfg80211_connect_bss(ndev, evt->bssid, vif->bss,
935 					     assoc_req_ie, assoc_req_ielen,
936 					     assoc_resp_ie, assoc_resp_ielen,
937 					     WLAN_STATUS_SUCCESS, GFP_KERNEL,
938 					     NL80211_TIMEOUT_UNSPECIFIED);
939 		}
940 		vif->bss = NULL;
941 	} else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
942 		   (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
943 
944 		if (rc) {
945 			if (disable_ap_sme)
946 				/* notify new_sta has failed */
947 				cfg80211_del_sta(ndev, evt->bssid, GFP_KERNEL);
948 			goto out;
949 		}
950 
951 		sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
952 		if (!sinfo) {
953 			rc = -ENOMEM;
954 			goto out;
955 		}
956 
957 		sinfo->generation = wil->sinfo_gen++;
958 
959 		if (assoc_req_ie) {
960 			sinfo->assoc_req_ies = assoc_req_ie;
961 			sinfo->assoc_req_ies_len = assoc_req_ielen;
962 		}
963 
964 		cfg80211_new_sta(ndev, evt->bssid, sinfo, GFP_KERNEL);
965 
966 		kfree(sinfo);
967 	} else {
968 		wil_err(wil, "unhandled iftype %d for CID %d\n", wdev->iftype,
969 			evt->cid);
970 		goto out;
971 	}
972 
973 	wil->sta[evt->cid].status = wil_sta_connected;
974 	wil->sta[evt->cid].aid = evt->aid;
975 	if (!test_and_set_bit(wil_vif_fwconnected, vif->status))
976 		atomic_inc(&wil->connected_vifs);
977 	wil_update_net_queues_bh(wil, vif, NULL, false);
978 
979 out:
980 	if (rc) {
981 		wil->sta[evt->cid].status = wil_sta_unused;
982 		wil->sta[evt->cid].mid = U8_MAX;
983 	}
984 	clear_bit(wil_vif_fwconnecting, vif->status);
985 	mutex_unlock(&wil->mutex);
986 }
987 
988 static void wmi_evt_disconnect(struct wil6210_vif *vif, int id,
989 			       void *d, int len)
990 {
991 	struct wil6210_priv *wil = vif_to_wil(vif);
992 	struct wmi_disconnect_event *evt = d;
993 	u16 reason_code = le16_to_cpu(evt->protocol_reason_status);
994 
995 	wil_info(wil, "Disconnect %pM reason [proto %d wmi %d]\n",
996 		 evt->bssid, reason_code, evt->disconnect_reason);
997 
998 	wil->sinfo_gen++;
999 
1000 	if (test_bit(wil_status_resetting, wil->status) ||
1001 	    !test_bit(wil_status_fwready, wil->status)) {
1002 		wil_err(wil, "status_resetting, cancel disconnect event\n");
1003 		/* no need for cleanup, wil_reset will do that */
1004 		return;
1005 	}
1006 
1007 	mutex_lock(&wil->mutex);
1008 	wil6210_disconnect(vif, evt->bssid, reason_code, true);
1009 	mutex_unlock(&wil->mutex);
1010 }
1011 
1012 /*
1013  * Firmware reports EAPOL frame using WME event.
1014  * Reconstruct Ethernet frame and deliver it via normal Rx
1015  */
1016 static void wmi_evt_eapol_rx(struct wil6210_vif *vif, int id, void *d, int len)
1017 {
1018 	struct wil6210_priv *wil = vif_to_wil(vif);
1019 	struct net_device *ndev = vif_to_ndev(vif);
1020 	struct wmi_eapol_rx_event *evt = d;
1021 	u16 eapol_len = le16_to_cpu(evt->eapol_len);
1022 	int sz = eapol_len + ETH_HLEN;
1023 	struct sk_buff *skb;
1024 	struct ethhdr *eth;
1025 	int cid;
1026 	struct wil_net_stats *stats = NULL;
1027 
1028 	wil_dbg_wmi(wil, "EAPOL len %d from %pM MID %d\n", eapol_len,
1029 		    evt->src_mac, vif->mid);
1030 
1031 	cid = wil_find_cid(wil, vif->mid, evt->src_mac);
1032 	if (cid >= 0)
1033 		stats = &wil->sta[cid].stats;
1034 
1035 	if (eapol_len > 196) { /* TODO: revisit size limit */
1036 		wil_err(wil, "EAPOL too large\n");
1037 		return;
1038 	}
1039 
1040 	skb = alloc_skb(sz, GFP_KERNEL);
1041 	if (!skb) {
1042 		wil_err(wil, "Failed to allocate skb\n");
1043 		return;
1044 	}
1045 
1046 	eth = skb_put(skb, ETH_HLEN);
1047 	ether_addr_copy(eth->h_dest, ndev->dev_addr);
1048 	ether_addr_copy(eth->h_source, evt->src_mac);
1049 	eth->h_proto = cpu_to_be16(ETH_P_PAE);
1050 	skb_put_data(skb, evt->eapol, eapol_len);
1051 	skb->protocol = eth_type_trans(skb, ndev);
1052 	if (likely(netif_rx_ni(skb) == NET_RX_SUCCESS)) {
1053 		ndev->stats.rx_packets++;
1054 		ndev->stats.rx_bytes += sz;
1055 		if (stats) {
1056 			stats->rx_packets++;
1057 			stats->rx_bytes += sz;
1058 		}
1059 	} else {
1060 		ndev->stats.rx_dropped++;
1061 		if (stats)
1062 			stats->rx_dropped++;
1063 	}
1064 }
1065 
1066 static void wmi_evt_vring_en(struct wil6210_vif *vif, int id, void *d, int len)
1067 {
1068 	struct wil6210_priv *wil = vif_to_wil(vif);
1069 	struct wmi_vring_en_event *evt = d;
1070 	u8 vri = evt->vring_index;
1071 	struct wireless_dev *wdev = vif_to_wdev(vif);
1072 
1073 	wil_dbg_wmi(wil, "Enable vring %d MID %d\n", vri, vif->mid);
1074 
1075 	if (vri >= ARRAY_SIZE(wil->vring_tx)) {
1076 		wil_err(wil, "Enable for invalid vring %d\n", vri);
1077 		return;
1078 	}
1079 
1080 	if (wdev->iftype != NL80211_IFTYPE_AP || !disable_ap_sme)
1081 		/* in AP mode with disable_ap_sme, this is done by
1082 		 * wil_cfg80211_change_station()
1083 		 */
1084 		wil->vring_tx_data[vri].dot1x_open = true;
1085 	if (vri == vif->bcast_vring) /* no BA for bcast */
1086 		return;
1087 	if (agg_wsize >= 0)
1088 		wil_addba_tx_request(wil, vri, agg_wsize);
1089 }
1090 
1091 static void wmi_evt_ba_status(struct wil6210_vif *vif, int id,
1092 			      void *d, int len)
1093 {
1094 	struct wil6210_priv *wil = vif_to_wil(vif);
1095 	struct wmi_ba_status_event *evt = d;
1096 	struct vring_tx_data *txdata;
1097 
1098 	wil_dbg_wmi(wil, "BACK[%d] %s {%d} timeout %d AMSDU%s\n",
1099 		    evt->ringid,
1100 		    evt->status == WMI_BA_AGREED ? "OK" : "N/A",
1101 		    evt->agg_wsize, __le16_to_cpu(evt->ba_timeout),
1102 		    evt->amsdu ? "+" : "-");
1103 
1104 	if (evt->ringid >= WIL6210_MAX_TX_RINGS) {
1105 		wil_err(wil, "invalid ring id %d\n", evt->ringid);
1106 		return;
1107 	}
1108 
1109 	if (evt->status != WMI_BA_AGREED) {
1110 		evt->ba_timeout = 0;
1111 		evt->agg_wsize = 0;
1112 		evt->amsdu = 0;
1113 	}
1114 
1115 	txdata = &wil->vring_tx_data[evt->ringid];
1116 
1117 	txdata->agg_timeout = le16_to_cpu(evt->ba_timeout);
1118 	txdata->agg_wsize = evt->agg_wsize;
1119 	txdata->agg_amsdu = evt->amsdu;
1120 	txdata->addba_in_progress = false;
1121 }
1122 
1123 static void wmi_evt_addba_rx_req(struct wil6210_vif *vif, int id,
1124 				 void *d, int len)
1125 {
1126 	struct wil6210_priv *wil = vif_to_wil(vif);
1127 	struct wmi_rcp_addba_req_event *evt = d;
1128 
1129 	wil_addba_rx_request(wil, vif->mid, evt->cidxtid, evt->dialog_token,
1130 			     evt->ba_param_set, evt->ba_timeout,
1131 			     evt->ba_seq_ctrl);
1132 }
1133 
1134 static void wmi_evt_delba(struct wil6210_vif *vif, int id, void *d, int len)
1135 __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
1136 {
1137 	struct wil6210_priv *wil = vif_to_wil(vif);
1138 	struct wmi_delba_event *evt = d;
1139 	u8 cid, tid;
1140 	u16 reason = __le16_to_cpu(evt->reason);
1141 	struct wil_sta_info *sta;
1142 	struct wil_tid_ampdu_rx *r;
1143 
1144 	might_sleep();
1145 	parse_cidxtid(evt->cidxtid, &cid, &tid);
1146 	wil_dbg_wmi(wil, "DELBA MID %d CID %d TID %d from %s reason %d\n",
1147 		    vif->mid, cid, tid,
1148 		    evt->from_initiator ? "originator" : "recipient",
1149 		    reason);
1150 	if (!evt->from_initiator) {
1151 		int i;
1152 		/* find Tx vring it belongs to */
1153 		for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
1154 			if ((wil->vring2cid_tid[i][0] == cid) &&
1155 			    (wil->vring2cid_tid[i][1] == tid)) {
1156 				struct vring_tx_data *txdata =
1157 					&wil->vring_tx_data[i];
1158 
1159 				wil_dbg_wmi(wil, "DELBA Tx vring %d\n", i);
1160 				txdata->agg_timeout = 0;
1161 				txdata->agg_wsize = 0;
1162 				txdata->addba_in_progress = false;
1163 
1164 				break; /* max. 1 matching ring */
1165 			}
1166 		}
1167 		if (i >= ARRAY_SIZE(wil->vring2cid_tid))
1168 			wil_err(wil, "DELBA: unable to find Tx vring\n");
1169 		return;
1170 	}
1171 
1172 	sta = &wil->sta[cid];
1173 
1174 	spin_lock_bh(&sta->tid_rx_lock);
1175 
1176 	r = sta->tid_rx[tid];
1177 	sta->tid_rx[tid] = NULL;
1178 	wil_tid_ampdu_rx_free(wil, r);
1179 
1180 	spin_unlock_bh(&sta->tid_rx_lock);
1181 }
1182 
1183 static void
1184 wmi_evt_sched_scan_result(struct wil6210_vif *vif, int id, void *d, int len)
1185 {
1186 	struct wil6210_priv *wil = vif_to_wil(vif);
1187 	struct wmi_sched_scan_result_event *data = d;
1188 	struct wiphy *wiphy = wil_to_wiphy(wil);
1189 	struct ieee80211_mgmt *rx_mgmt_frame =
1190 		(struct ieee80211_mgmt *)data->payload;
1191 	int flen = len - offsetof(struct wmi_sched_scan_result_event, payload);
1192 	int ch_no;
1193 	u32 freq;
1194 	struct ieee80211_channel *channel;
1195 	s32 signal;
1196 	__le16 fc;
1197 	u32 d_len;
1198 	struct cfg80211_bss *bss;
1199 
1200 	if (flen < 0) {
1201 		wil_err(wil, "sched scan result event too short, len %d\n",
1202 			len);
1203 		return;
1204 	}
1205 
1206 	d_len = le32_to_cpu(data->info.len);
1207 	if (d_len != flen) {
1208 		wil_err(wil,
1209 			"sched scan result length mismatch, d_len %d should be %d\n",
1210 			d_len, flen);
1211 		return;
1212 	}
1213 
1214 	fc = rx_mgmt_frame->frame_control;
1215 	if (!ieee80211_is_probe_resp(fc)) {
1216 		wil_err(wil, "sched scan result invalid frame, fc 0x%04x\n",
1217 			fc);
1218 		return;
1219 	}
1220 
1221 	ch_no = data->info.channel + 1;
1222 	freq = ieee80211_channel_to_frequency(ch_no, NL80211_BAND_60GHZ);
1223 	channel = ieee80211_get_channel(wiphy, freq);
1224 	if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
1225 		signal = 100 * data->info.rssi;
1226 	else
1227 		signal = data->info.sqi;
1228 
1229 	wil_dbg_wmi(wil, "sched scan result: channel %d MCS %d RSSI %d\n",
1230 		    data->info.channel, data->info.mcs, data->info.rssi);
1231 	wil_dbg_wmi(wil, "len %d qid %d mid %d cid %d\n",
1232 		    d_len, data->info.qid, data->info.mid, data->info.cid);
1233 	wil_hex_dump_wmi("PROBE ", DUMP_PREFIX_OFFSET, 16, 1, rx_mgmt_frame,
1234 			 d_len, true);
1235 
1236 	if (!channel) {
1237 		wil_err(wil, "Frame on unsupported channel\n");
1238 		return;
1239 	}
1240 
1241 	bss = cfg80211_inform_bss_frame(wiphy, channel, rx_mgmt_frame,
1242 					d_len, signal, GFP_KERNEL);
1243 	if (bss) {
1244 		wil_dbg_wmi(wil, "Added BSS %pM\n", rx_mgmt_frame->bssid);
1245 		cfg80211_put_bss(wiphy, bss);
1246 	} else {
1247 		wil_err(wil, "cfg80211_inform_bss_frame() failed\n");
1248 	}
1249 
1250 	cfg80211_sched_scan_results(wiphy, 0);
1251 }
1252 
1253 /**
1254  * Some events are ignored for purpose; and need not be interpreted as
1255  * "unhandled events"
1256  */
1257 static void wmi_evt_ignore(struct wil6210_vif *vif, int id, void *d, int len)
1258 {
1259 	struct wil6210_priv *wil = vif_to_wil(vif);
1260 
1261 	wil_dbg_wmi(wil, "Ignore event 0x%04x len %d\n", id, len);
1262 }
1263 
1264 static const struct {
1265 	int eventid;
1266 	void (*handler)(struct wil6210_vif *vif,
1267 			int eventid, void *data, int data_len);
1268 } wmi_evt_handlers[] = {
1269 	{WMI_READY_EVENTID,		wmi_evt_ready},
1270 	{WMI_FW_READY_EVENTID,			wmi_evt_ignore},
1271 	{WMI_RX_MGMT_PACKET_EVENTID,	wmi_evt_rx_mgmt},
1272 	{WMI_TX_MGMT_PACKET_EVENTID,		wmi_evt_tx_mgmt},
1273 	{WMI_SCAN_COMPLETE_EVENTID,	wmi_evt_scan_complete},
1274 	{WMI_CONNECT_EVENTID,		wmi_evt_connect},
1275 	{WMI_DISCONNECT_EVENTID,	wmi_evt_disconnect},
1276 	{WMI_EAPOL_RX_EVENTID,		wmi_evt_eapol_rx},
1277 	{WMI_BA_STATUS_EVENTID,		wmi_evt_ba_status},
1278 	{WMI_RCP_ADDBA_REQ_EVENTID,	wmi_evt_addba_rx_req},
1279 	{WMI_DELBA_EVENTID,		wmi_evt_delba},
1280 	{WMI_VRING_EN_EVENTID,		wmi_evt_vring_en},
1281 	{WMI_DATA_PORT_OPEN_EVENTID,		wmi_evt_ignore},
1282 	{WMI_SCHED_SCAN_RESULT_EVENTID,		wmi_evt_sched_scan_result},
1283 };
1284 
1285 /*
1286  * Run in IRQ context
1287  * Extract WMI command from mailbox. Queue it to the @wil->pending_wmi_ev
1288  * that will be eventually handled by the @wmi_event_worker in the thread
1289  * context of thread "wil6210_wmi"
1290  */
1291 void wmi_recv_cmd(struct wil6210_priv *wil)
1292 {
1293 	struct wil6210_mbox_ring_desc d_tail;
1294 	struct wil6210_mbox_hdr hdr;
1295 	struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
1296 	struct pending_wmi_event *evt;
1297 	u8 *cmd;
1298 	void __iomem *src;
1299 	ulong flags;
1300 	unsigned n;
1301 	unsigned int num_immed_reply = 0;
1302 
1303 	if (!test_bit(wil_status_mbox_ready, wil->status)) {
1304 		wil_err(wil, "Reset in progress. Cannot handle WMI event\n");
1305 		return;
1306 	}
1307 
1308 	if (test_bit(wil_status_suspended, wil->status)) {
1309 		wil_err(wil, "suspended. cannot handle WMI event\n");
1310 		return;
1311 	}
1312 
1313 	for (n = 0;; n++) {
1314 		u16 len;
1315 		bool q;
1316 		bool immed_reply = false;
1317 
1318 		r->head = wil_r(wil, RGF_MBOX +
1319 				offsetof(struct wil6210_mbox_ctl, rx.head));
1320 		if (r->tail == r->head)
1321 			break;
1322 
1323 		wil_dbg_wmi(wil, "Mbox head %08x tail %08x\n",
1324 			    r->head, r->tail);
1325 		/* read cmd descriptor from tail */
1326 		wil_memcpy_fromio_32(&d_tail, wil->csr + HOSTADDR(r->tail),
1327 				     sizeof(struct wil6210_mbox_ring_desc));
1328 		if (d_tail.sync == 0) {
1329 			wil_err(wil, "Mbox evt not owned by FW?\n");
1330 			break;
1331 		}
1332 
1333 		/* read cmd header from descriptor */
1334 		if (0 != wmi_read_hdr(wil, d_tail.addr, &hdr)) {
1335 			wil_err(wil, "Mbox evt at 0x%08x?\n",
1336 				le32_to_cpu(d_tail.addr));
1337 			break;
1338 		}
1339 		len = le16_to_cpu(hdr.len);
1340 		wil_dbg_wmi(wil, "Mbox evt %04x %04x %04x %02x\n",
1341 			    le16_to_cpu(hdr.seq), len, le16_to_cpu(hdr.type),
1342 			    hdr.flags);
1343 
1344 		/* read cmd buffer from descriptor */
1345 		src = wmi_buffer(wil, d_tail.addr) +
1346 		      sizeof(struct wil6210_mbox_hdr);
1347 		evt = kmalloc(ALIGN(offsetof(struct pending_wmi_event,
1348 					     event.wmi) + len, 4),
1349 			      GFP_KERNEL);
1350 		if (!evt)
1351 			break;
1352 
1353 		evt->event.hdr = hdr;
1354 		cmd = (void *)&evt->event.wmi;
1355 		wil_memcpy_fromio_32(cmd, src, len);
1356 		/* mark entry as empty */
1357 		wil_w(wil, r->tail +
1358 		      offsetof(struct wil6210_mbox_ring_desc, sync), 0);
1359 		/* indicate */
1360 		if ((hdr.type == WIL_MBOX_HDR_TYPE_WMI) &&
1361 		    (len >= sizeof(struct wmi_cmd_hdr))) {
1362 			struct wmi_cmd_hdr *wmi = &evt->event.wmi;
1363 			u16 id = le16_to_cpu(wmi->command_id);
1364 			u8 mid = wmi->mid;
1365 			u32 tstamp = le32_to_cpu(wmi->fw_timestamp);
1366 			if (test_bit(wil_status_resuming, wil->status)) {
1367 				if (id == WMI_TRAFFIC_RESUME_EVENTID)
1368 					clear_bit(wil_status_resuming,
1369 						  wil->status);
1370 				else
1371 					wil_err(wil,
1372 						"WMI evt %d while resuming\n",
1373 						id);
1374 			}
1375 			spin_lock_irqsave(&wil->wmi_ev_lock, flags);
1376 			if (wil->reply_id && wil->reply_id == id &&
1377 			    wil->reply_mid == mid) {
1378 				if (wil->reply_buf) {
1379 					memcpy(wil->reply_buf, wmi,
1380 					       min(len, wil->reply_size));
1381 					immed_reply = true;
1382 				}
1383 				if (id == WMI_TRAFFIC_SUSPEND_EVENTID) {
1384 					wil_dbg_wmi(wil,
1385 						    "set suspend_resp_rcvd\n");
1386 					wil->suspend_resp_rcvd = true;
1387 				}
1388 			}
1389 			spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
1390 
1391 			wil_dbg_wmi(wil, "recv %s (0x%04x) MID %d @%d msec\n",
1392 				    eventid2name(id), id, wmi->mid, tstamp);
1393 			trace_wil6210_wmi_event(wmi, &wmi[1],
1394 						len - sizeof(*wmi));
1395 		}
1396 		wil_hex_dump_wmi("evt ", DUMP_PREFIX_OFFSET, 16, 1,
1397 				 &evt->event.hdr, sizeof(hdr) + len, true);
1398 
1399 		/* advance tail */
1400 		r->tail = r->base + ((r->tail - r->base +
1401 			  sizeof(struct wil6210_mbox_ring_desc)) % r->size);
1402 		wil_w(wil, RGF_MBOX +
1403 		      offsetof(struct wil6210_mbox_ctl, rx.tail), r->tail);
1404 
1405 		if (immed_reply) {
1406 			wil_dbg_wmi(wil, "recv_cmd: Complete WMI 0x%04x\n",
1407 				    wil->reply_id);
1408 			kfree(evt);
1409 			num_immed_reply++;
1410 			complete(&wil->wmi_call);
1411 		} else {
1412 			/* add to the pending list */
1413 			spin_lock_irqsave(&wil->wmi_ev_lock, flags);
1414 			list_add_tail(&evt->list, &wil->pending_wmi_ev);
1415 			spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
1416 			q = queue_work(wil->wmi_wq, &wil->wmi_event_worker);
1417 			wil_dbg_wmi(wil, "queue_work -> %d\n", q);
1418 		}
1419 	}
1420 	/* normally, 1 event per IRQ should be processed */
1421 	wil_dbg_wmi(wil, "recv_cmd: -> %d events queued, %d completed\n",
1422 		    n - num_immed_reply, num_immed_reply);
1423 }
1424 
1425 int wmi_call(struct wil6210_priv *wil, u16 cmdid, u8 mid, void *buf, u16 len,
1426 	     u16 reply_id, void *reply, u16 reply_size, int to_msec)
1427 {
1428 	int rc;
1429 	unsigned long remain;
1430 
1431 	mutex_lock(&wil->wmi_mutex);
1432 
1433 	spin_lock(&wil->wmi_ev_lock);
1434 	wil->reply_id = reply_id;
1435 	wil->reply_mid = mid;
1436 	wil->reply_buf = reply;
1437 	wil->reply_size = reply_size;
1438 	reinit_completion(&wil->wmi_call);
1439 	spin_unlock(&wil->wmi_ev_lock);
1440 
1441 	rc = __wmi_send(wil, cmdid, mid, buf, len);
1442 	if (rc)
1443 		goto out;
1444 
1445 	remain = wait_for_completion_timeout(&wil->wmi_call,
1446 					     msecs_to_jiffies(to_msec));
1447 	if (0 == remain) {
1448 		wil_err(wil, "wmi_call(0x%04x->0x%04x) timeout %d msec\n",
1449 			cmdid, reply_id, to_msec);
1450 		rc = -ETIME;
1451 	} else {
1452 		wil_dbg_wmi(wil,
1453 			    "wmi_call(0x%04x->0x%04x) completed in %d msec\n",
1454 			    cmdid, reply_id,
1455 			    to_msec - jiffies_to_msecs(remain));
1456 	}
1457 
1458 out:
1459 	spin_lock(&wil->wmi_ev_lock);
1460 	wil->reply_id = 0;
1461 	wil->reply_mid = U8_MAX;
1462 	wil->reply_buf = NULL;
1463 	wil->reply_size = 0;
1464 	spin_unlock(&wil->wmi_ev_lock);
1465 
1466 	mutex_unlock(&wil->wmi_mutex);
1467 
1468 	return rc;
1469 }
1470 
1471 int wmi_echo(struct wil6210_priv *wil)
1472 {
1473 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1474 	struct wmi_echo_cmd cmd = {
1475 		.value = cpu_to_le32(0x12345678),
1476 	};
1477 
1478 	return wmi_call(wil, WMI_ECHO_CMDID, vif->mid, &cmd, sizeof(cmd),
1479 			WMI_ECHO_RSP_EVENTID, NULL, 0, 50);
1480 }
1481 
1482 int wmi_set_mac_address(struct wil6210_priv *wil, void *addr)
1483 {
1484 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1485 	struct wmi_set_mac_address_cmd cmd;
1486 
1487 	ether_addr_copy(cmd.mac, addr);
1488 
1489 	wil_dbg_wmi(wil, "Set MAC %pM\n", addr);
1490 
1491 	return wmi_send(wil, WMI_SET_MAC_ADDRESS_CMDID, vif->mid,
1492 			&cmd, sizeof(cmd));
1493 }
1494 
1495 int wmi_led_cfg(struct wil6210_priv *wil, bool enable)
1496 {
1497 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1498 	int rc = 0;
1499 	struct wmi_led_cfg_cmd cmd = {
1500 		.led_mode = enable,
1501 		.id = led_id,
1502 		.slow_blink_cfg.blink_on =
1503 			cpu_to_le32(led_blink_time[WIL_LED_TIME_SLOW].on_ms),
1504 		.slow_blink_cfg.blink_off =
1505 			cpu_to_le32(led_blink_time[WIL_LED_TIME_SLOW].off_ms),
1506 		.medium_blink_cfg.blink_on =
1507 			cpu_to_le32(led_blink_time[WIL_LED_TIME_MED].on_ms),
1508 		.medium_blink_cfg.blink_off =
1509 			cpu_to_le32(led_blink_time[WIL_LED_TIME_MED].off_ms),
1510 		.fast_blink_cfg.blink_on =
1511 			cpu_to_le32(led_blink_time[WIL_LED_TIME_FAST].on_ms),
1512 		.fast_blink_cfg.blink_off =
1513 			cpu_to_le32(led_blink_time[WIL_LED_TIME_FAST].off_ms),
1514 		.led_polarity = led_polarity,
1515 	};
1516 	struct {
1517 		struct wmi_cmd_hdr wmi;
1518 		struct wmi_led_cfg_done_event evt;
1519 	} __packed reply = {
1520 		.evt = {.status = cpu_to_le32(WMI_FW_STATUS_FAILURE)},
1521 	};
1522 
1523 	if (led_id == WIL_LED_INVALID_ID)
1524 		goto out;
1525 
1526 	if (led_id > WIL_LED_MAX_ID) {
1527 		wil_err(wil, "Invalid led id %d\n", led_id);
1528 		rc = -EINVAL;
1529 		goto out;
1530 	}
1531 
1532 	wil_dbg_wmi(wil,
1533 		    "%s led %d\n",
1534 		    enable ? "enabling" : "disabling", led_id);
1535 
1536 	rc = wmi_call(wil, WMI_LED_CFG_CMDID, vif->mid, &cmd, sizeof(cmd),
1537 		      WMI_LED_CFG_DONE_EVENTID, &reply, sizeof(reply),
1538 		      100);
1539 	if (rc)
1540 		goto out;
1541 
1542 	if (reply.evt.status) {
1543 		wil_err(wil, "led %d cfg failed with status %d\n",
1544 			led_id, le32_to_cpu(reply.evt.status));
1545 		rc = -EINVAL;
1546 	}
1547 
1548 out:
1549 	return rc;
1550 }
1551 
1552 int wmi_pcp_start(struct wil6210_vif *vif,
1553 		  int bi, u8 wmi_nettype, u8 chan, u8 hidden_ssid, u8 is_go)
1554 {
1555 	struct wil6210_priv *wil = vif_to_wil(vif);
1556 	int rc;
1557 
1558 	struct wmi_pcp_start_cmd cmd = {
1559 		.bcon_interval = cpu_to_le16(bi),
1560 		.network_type = wmi_nettype,
1561 		.disable_sec_offload = 1,
1562 		.channel = chan - 1,
1563 		.pcp_max_assoc_sta = max_assoc_sta,
1564 		.hidden_ssid = hidden_ssid,
1565 		.is_go = is_go,
1566 		.ap_sme_offload_mode = disable_ap_sme ?
1567 				       WMI_AP_SME_OFFLOAD_PARTIAL :
1568 				       WMI_AP_SME_OFFLOAD_FULL,
1569 		.abft_len = wil->abft_len,
1570 	};
1571 	struct {
1572 		struct wmi_cmd_hdr wmi;
1573 		struct wmi_pcp_started_event evt;
1574 	} __packed reply = {
1575 		.evt = {.status = WMI_FW_STATUS_FAILURE},
1576 	};
1577 
1578 	if (!vif->privacy)
1579 		cmd.disable_sec = 1;
1580 
1581 	if ((cmd.pcp_max_assoc_sta > WIL6210_MAX_CID) ||
1582 	    (cmd.pcp_max_assoc_sta <= 0)) {
1583 		wil_info(wil,
1584 			 "Requested connection limit %u, valid values are 1 - %d. Setting to %d\n",
1585 			 max_assoc_sta, WIL6210_MAX_CID, WIL6210_MAX_CID);
1586 		cmd.pcp_max_assoc_sta = WIL6210_MAX_CID;
1587 	}
1588 
1589 	if (disable_ap_sme &&
1590 	    !test_bit(WMI_FW_CAPABILITY_AP_SME_OFFLOAD_PARTIAL,
1591 		      wil->fw_capabilities)) {
1592 		wil_err(wil, "disable_ap_sme not supported by FW\n");
1593 		return -EOPNOTSUPP;
1594 	}
1595 
1596 	/*
1597 	 * Processing time may be huge, in case of secure AP it takes about
1598 	 * 3500ms for FW to start AP
1599 	 */
1600 	rc = wmi_call(wil, WMI_PCP_START_CMDID, vif->mid, &cmd, sizeof(cmd),
1601 		      WMI_PCP_STARTED_EVENTID, &reply, sizeof(reply), 5000);
1602 	if (rc)
1603 		return rc;
1604 
1605 	if (reply.evt.status != WMI_FW_STATUS_SUCCESS)
1606 		rc = -EINVAL;
1607 
1608 	if (wmi_nettype != WMI_NETTYPE_P2P)
1609 		/* Don't fail due to error in the led configuration */
1610 		wmi_led_cfg(wil, true);
1611 
1612 	return rc;
1613 }
1614 
1615 int wmi_pcp_stop(struct wil6210_vif *vif)
1616 {
1617 	struct wil6210_priv *wil = vif_to_wil(vif);
1618 	int rc;
1619 
1620 	rc = wmi_led_cfg(wil, false);
1621 	if (rc)
1622 		return rc;
1623 
1624 	return wmi_call(wil, WMI_PCP_STOP_CMDID, vif->mid, NULL, 0,
1625 			WMI_PCP_STOPPED_EVENTID, NULL, 0, 20);
1626 }
1627 
1628 int wmi_set_ssid(struct wil6210_vif *vif, u8 ssid_len, const void *ssid)
1629 {
1630 	struct wil6210_priv *wil = vif_to_wil(vif);
1631 	struct wmi_set_ssid_cmd cmd = {
1632 		.ssid_len = cpu_to_le32(ssid_len),
1633 	};
1634 
1635 	if (ssid_len > sizeof(cmd.ssid))
1636 		return -EINVAL;
1637 
1638 	memcpy(cmd.ssid, ssid, ssid_len);
1639 
1640 	return wmi_send(wil, WMI_SET_SSID_CMDID, vif->mid, &cmd, sizeof(cmd));
1641 }
1642 
1643 int wmi_get_ssid(struct wil6210_vif *vif, u8 *ssid_len, void *ssid)
1644 {
1645 	struct wil6210_priv *wil = vif_to_wil(vif);
1646 	int rc;
1647 	struct {
1648 		struct wmi_cmd_hdr wmi;
1649 		struct wmi_set_ssid_cmd cmd;
1650 	} __packed reply;
1651 	int len; /* reply.cmd.ssid_len in CPU order */
1652 
1653 	memset(&reply, 0, sizeof(reply));
1654 
1655 	rc = wmi_call(wil, WMI_GET_SSID_CMDID, vif->mid, NULL, 0,
1656 		      WMI_GET_SSID_EVENTID, &reply, sizeof(reply), 20);
1657 	if (rc)
1658 		return rc;
1659 
1660 	len = le32_to_cpu(reply.cmd.ssid_len);
1661 	if (len > sizeof(reply.cmd.ssid))
1662 		return -EINVAL;
1663 
1664 	*ssid_len = len;
1665 	memcpy(ssid, reply.cmd.ssid, len);
1666 
1667 	return 0;
1668 }
1669 
1670 int wmi_set_channel(struct wil6210_priv *wil, int channel)
1671 {
1672 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1673 	struct wmi_set_pcp_channel_cmd cmd = {
1674 		.channel = channel - 1,
1675 	};
1676 
1677 	return wmi_send(wil, WMI_SET_PCP_CHANNEL_CMDID, vif->mid,
1678 			&cmd, sizeof(cmd));
1679 }
1680 
1681 int wmi_get_channel(struct wil6210_priv *wil, int *channel)
1682 {
1683 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1684 	int rc;
1685 	struct {
1686 		struct wmi_cmd_hdr wmi;
1687 		struct wmi_set_pcp_channel_cmd cmd;
1688 	} __packed reply;
1689 
1690 	memset(&reply, 0, sizeof(reply));
1691 
1692 	rc = wmi_call(wil, WMI_GET_PCP_CHANNEL_CMDID, vif->mid, NULL, 0,
1693 		      WMI_GET_PCP_CHANNEL_EVENTID, &reply, sizeof(reply), 20);
1694 	if (rc)
1695 		return rc;
1696 
1697 	if (reply.cmd.channel > 3)
1698 		return -EINVAL;
1699 
1700 	*channel = reply.cmd.channel + 1;
1701 
1702 	return 0;
1703 }
1704 
1705 int wmi_p2p_cfg(struct wil6210_vif *vif, int channel, int bi)
1706 {
1707 	struct wil6210_priv *wil = vif_to_wil(vif);
1708 	int rc;
1709 	struct wmi_p2p_cfg_cmd cmd = {
1710 		.discovery_mode = WMI_DISCOVERY_MODE_PEER2PEER,
1711 		.bcon_interval = cpu_to_le16(bi),
1712 		.channel = channel - 1,
1713 	};
1714 	struct {
1715 		struct wmi_cmd_hdr wmi;
1716 		struct wmi_p2p_cfg_done_event evt;
1717 	} __packed reply = {
1718 		.evt = {.status = WMI_FW_STATUS_FAILURE},
1719 	};
1720 
1721 	wil_dbg_wmi(wil, "sending WMI_P2P_CFG_CMDID\n");
1722 
1723 	rc = wmi_call(wil, WMI_P2P_CFG_CMDID, vif->mid, &cmd, sizeof(cmd),
1724 		      WMI_P2P_CFG_DONE_EVENTID, &reply, sizeof(reply), 300);
1725 	if (!rc && reply.evt.status != WMI_FW_STATUS_SUCCESS) {
1726 		wil_err(wil, "P2P_CFG failed. status %d\n", reply.evt.status);
1727 		rc = -EINVAL;
1728 	}
1729 
1730 	return rc;
1731 }
1732 
1733 int wmi_start_listen(struct wil6210_vif *vif)
1734 {
1735 	struct wil6210_priv *wil = vif_to_wil(vif);
1736 	int rc;
1737 	struct {
1738 		struct wmi_cmd_hdr wmi;
1739 		struct wmi_listen_started_event evt;
1740 	} __packed reply = {
1741 		.evt = {.status = WMI_FW_STATUS_FAILURE},
1742 	};
1743 
1744 	wil_dbg_wmi(wil, "sending WMI_START_LISTEN_CMDID\n");
1745 
1746 	rc = wmi_call(wil, WMI_START_LISTEN_CMDID, vif->mid, NULL, 0,
1747 		      WMI_LISTEN_STARTED_EVENTID, &reply, sizeof(reply), 300);
1748 	if (!rc && reply.evt.status != WMI_FW_STATUS_SUCCESS) {
1749 		wil_err(wil, "device failed to start listen. status %d\n",
1750 			reply.evt.status);
1751 		rc = -EINVAL;
1752 	}
1753 
1754 	return rc;
1755 }
1756 
1757 int wmi_start_search(struct wil6210_vif *vif)
1758 {
1759 	struct wil6210_priv *wil = vif_to_wil(vif);
1760 	int rc;
1761 	struct {
1762 		struct wmi_cmd_hdr wmi;
1763 		struct wmi_search_started_event evt;
1764 	} __packed reply = {
1765 		.evt = {.status = WMI_FW_STATUS_FAILURE},
1766 	};
1767 
1768 	wil_dbg_wmi(wil, "sending WMI_START_SEARCH_CMDID\n");
1769 
1770 	rc = wmi_call(wil, WMI_START_SEARCH_CMDID, vif->mid, NULL, 0,
1771 		      WMI_SEARCH_STARTED_EVENTID, &reply, sizeof(reply), 300);
1772 	if (!rc && reply.evt.status != WMI_FW_STATUS_SUCCESS) {
1773 		wil_err(wil, "device failed to start search. status %d\n",
1774 			reply.evt.status);
1775 		rc = -EINVAL;
1776 	}
1777 
1778 	return rc;
1779 }
1780 
1781 int wmi_stop_discovery(struct wil6210_vif *vif)
1782 {
1783 	struct wil6210_priv *wil = vif_to_wil(vif);
1784 	int rc;
1785 
1786 	wil_dbg_wmi(wil, "sending WMI_DISCOVERY_STOP_CMDID\n");
1787 
1788 	rc = wmi_call(wil, WMI_DISCOVERY_STOP_CMDID, vif->mid, NULL, 0,
1789 		      WMI_DISCOVERY_STOPPED_EVENTID, NULL, 0, 100);
1790 
1791 	if (rc)
1792 		wil_err(wil, "Failed to stop discovery\n");
1793 
1794 	return rc;
1795 }
1796 
1797 int wmi_del_cipher_key(struct wil6210_vif *vif, u8 key_index,
1798 		       const void *mac_addr, int key_usage)
1799 {
1800 	struct wil6210_priv *wil = vif_to_wil(vif);
1801 	struct wmi_delete_cipher_key_cmd cmd = {
1802 		.key_index = key_index,
1803 	};
1804 
1805 	if (mac_addr)
1806 		memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
1807 
1808 	return wmi_send(wil, WMI_DELETE_CIPHER_KEY_CMDID, vif->mid,
1809 			&cmd, sizeof(cmd));
1810 }
1811 
1812 int wmi_add_cipher_key(struct wil6210_vif *vif, u8 key_index,
1813 		       const void *mac_addr, int key_len, const void *key,
1814 		       int key_usage)
1815 {
1816 	struct wil6210_priv *wil = vif_to_wil(vif);
1817 	struct wmi_add_cipher_key_cmd cmd = {
1818 		.key_index = key_index,
1819 		.key_usage = key_usage,
1820 		.key_len = key_len,
1821 	};
1822 
1823 	if (!key || (key_len > sizeof(cmd.key)))
1824 		return -EINVAL;
1825 
1826 	memcpy(cmd.key, key, key_len);
1827 	if (mac_addr)
1828 		memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
1829 
1830 	return wmi_send(wil, WMI_ADD_CIPHER_KEY_CMDID, vif->mid,
1831 			&cmd, sizeof(cmd));
1832 }
1833 
1834 int wmi_set_ie(struct wil6210_vif *vif, u8 type, u16 ie_len, const void *ie)
1835 {
1836 	struct wil6210_priv *wil = vif_to_wil(vif);
1837 	static const char *const names[] = {
1838 		[WMI_FRAME_BEACON]	= "BEACON",
1839 		[WMI_FRAME_PROBE_REQ]	= "PROBE_REQ",
1840 		[WMI_FRAME_PROBE_RESP]	= "WMI_FRAME_PROBE_RESP",
1841 		[WMI_FRAME_ASSOC_REQ]	= "WMI_FRAME_ASSOC_REQ",
1842 		[WMI_FRAME_ASSOC_RESP]	= "WMI_FRAME_ASSOC_RESP",
1843 	};
1844 	int rc;
1845 	u16 len = sizeof(struct wmi_set_appie_cmd) + ie_len;
1846 	struct wmi_set_appie_cmd *cmd;
1847 
1848 	if (len < ie_len) {
1849 		rc = -EINVAL;
1850 		goto out;
1851 	}
1852 
1853 	cmd = kzalloc(len, GFP_KERNEL);
1854 	if (!cmd) {
1855 		rc = -ENOMEM;
1856 		goto out;
1857 	}
1858 	if (!ie)
1859 		ie_len = 0;
1860 
1861 	cmd->mgmt_frm_type = type;
1862 	/* BUG: FW API define ieLen as u8. Will fix FW */
1863 	cmd->ie_len = cpu_to_le16(ie_len);
1864 	memcpy(cmd->ie_info, ie, ie_len);
1865 	rc = wmi_send(wil, WMI_SET_APPIE_CMDID, vif->mid, cmd, len);
1866 	kfree(cmd);
1867 out:
1868 	if (rc) {
1869 		const char *name = type < ARRAY_SIZE(names) ?
1870 				   names[type] : "??";
1871 		wil_err(wil, "set_ie(%d %s) failed : %d\n", type, name, rc);
1872 	}
1873 
1874 	return rc;
1875 }
1876 
1877 /**
1878  * wmi_rxon - turn radio on/off
1879  * @on:		turn on if true, off otherwise
1880  *
1881  * Only switch radio. Channel should be set separately.
1882  * No timeout for rxon - radio turned on forever unless some other call
1883  * turns it off
1884  */
1885 int wmi_rxon(struct wil6210_priv *wil, bool on)
1886 {
1887 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1888 	int rc;
1889 	struct {
1890 		struct wmi_cmd_hdr wmi;
1891 		struct wmi_listen_started_event evt;
1892 	} __packed reply = {
1893 		.evt = {.status = WMI_FW_STATUS_FAILURE},
1894 	};
1895 
1896 	wil_info(wil, "(%s)\n", on ? "on" : "off");
1897 
1898 	if (on) {
1899 		rc = wmi_call(wil, WMI_START_LISTEN_CMDID, vif->mid, NULL, 0,
1900 			      WMI_LISTEN_STARTED_EVENTID,
1901 			      &reply, sizeof(reply), 100);
1902 		if ((rc == 0) && (reply.evt.status != WMI_FW_STATUS_SUCCESS))
1903 			rc = -EINVAL;
1904 	} else {
1905 		rc = wmi_call(wil, WMI_DISCOVERY_STOP_CMDID, vif->mid, NULL, 0,
1906 			      WMI_DISCOVERY_STOPPED_EVENTID, NULL, 0, 20);
1907 	}
1908 
1909 	return rc;
1910 }
1911 
1912 int wmi_rx_chain_add(struct wil6210_priv *wil, struct vring *vring)
1913 {
1914 	struct net_device *ndev = wil->main_ndev;
1915 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
1916 	struct wil6210_vif *vif = ndev_to_vif(ndev);
1917 	struct wmi_cfg_rx_chain_cmd cmd = {
1918 		.action = WMI_RX_CHAIN_ADD,
1919 		.rx_sw_ring = {
1920 			.max_mpdu_size = cpu_to_le16(
1921 				wil_mtu2macbuf(wil->rx_buf_len)),
1922 			.ring_mem_base = cpu_to_le64(vring->pa),
1923 			.ring_size = cpu_to_le16(vring->size),
1924 		},
1925 		.mid = 0, /* TODO - what is it? */
1926 		.decap_trans_type = WMI_DECAP_TYPE_802_3,
1927 		.reorder_type = WMI_RX_SW_REORDER,
1928 		.host_thrsh = cpu_to_le16(rx_ring_overflow_thrsh),
1929 	};
1930 	struct {
1931 		struct wmi_cmd_hdr wmi;
1932 		struct wmi_cfg_rx_chain_done_event evt;
1933 	} __packed evt;
1934 	int rc;
1935 
1936 	memset(&evt, 0, sizeof(evt));
1937 
1938 	if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
1939 		struct ieee80211_channel *ch = wil->monitor_chandef.chan;
1940 
1941 		cmd.sniffer_cfg.mode = cpu_to_le32(WMI_SNIFFER_ON);
1942 		if (ch)
1943 			cmd.sniffer_cfg.channel = ch->hw_value - 1;
1944 		cmd.sniffer_cfg.phy_info_mode =
1945 			cpu_to_le32(ndev->type == ARPHRD_IEEE80211_RADIOTAP);
1946 		cmd.sniffer_cfg.phy_support =
1947 			cpu_to_le32((wil->monitor_flags & MONITOR_FLAG_CONTROL)
1948 				    ? WMI_SNIFFER_CP : WMI_SNIFFER_BOTH_PHYS);
1949 	} else {
1950 		/* Initialize offload (in non-sniffer mode).
1951 		 * Linux IP stack always calculates IP checksum
1952 		 * HW always calculate TCP/UDP checksum
1953 		 */
1954 		cmd.l3_l4_ctrl |= (1 << L3_L4_CTRL_TCPIP_CHECKSUM_EN_POS);
1955 	}
1956 
1957 	if (rx_align_2)
1958 		cmd.l2_802_3_offload_ctrl |=
1959 				L2_802_3_OFFLOAD_CTRL_SNAP_KEEP_MSK;
1960 
1961 	/* typical time for secure PCP is 840ms */
1962 	rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, vif->mid, &cmd, sizeof(cmd),
1963 		      WMI_CFG_RX_CHAIN_DONE_EVENTID, &evt, sizeof(evt), 2000);
1964 	if (rc)
1965 		return rc;
1966 
1967 	if (le32_to_cpu(evt.evt.status) != WMI_CFG_RX_CHAIN_SUCCESS)
1968 		rc = -EINVAL;
1969 
1970 	vring->hwtail = le32_to_cpu(evt.evt.rx_ring_tail_ptr);
1971 
1972 	wil_dbg_misc(wil, "Rx init: status %d tail 0x%08x\n",
1973 		     le32_to_cpu(evt.evt.status), vring->hwtail);
1974 
1975 	return rc;
1976 }
1977 
1978 int wmi_get_temperature(struct wil6210_priv *wil, u32 *t_bb, u32 *t_rf)
1979 {
1980 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
1981 	int rc;
1982 	struct wmi_temp_sense_cmd cmd = {
1983 		.measure_baseband_en = cpu_to_le32(!!t_bb),
1984 		.measure_rf_en = cpu_to_le32(!!t_rf),
1985 		.measure_mode = cpu_to_le32(TEMPERATURE_MEASURE_NOW),
1986 	};
1987 	struct {
1988 		struct wmi_cmd_hdr wmi;
1989 		struct wmi_temp_sense_done_event evt;
1990 	} __packed reply;
1991 
1992 	memset(&reply, 0, sizeof(reply));
1993 
1994 	rc = wmi_call(wil, WMI_TEMP_SENSE_CMDID, vif->mid, &cmd, sizeof(cmd),
1995 		      WMI_TEMP_SENSE_DONE_EVENTID, &reply, sizeof(reply), 100);
1996 	if (rc)
1997 		return rc;
1998 
1999 	if (t_bb)
2000 		*t_bb = le32_to_cpu(reply.evt.baseband_t1000);
2001 	if (t_rf)
2002 		*t_rf = le32_to_cpu(reply.evt.rf_t1000);
2003 
2004 	return 0;
2005 }
2006 
2007 int wmi_disconnect_sta(struct wil6210_vif *vif, const u8 *mac,
2008 		       u16 reason, bool full_disconnect, bool del_sta)
2009 {
2010 	struct wil6210_priv *wil = vif_to_wil(vif);
2011 	int rc;
2012 	u16 reason_code;
2013 	struct wmi_disconnect_sta_cmd disc_sta_cmd = {
2014 		.disconnect_reason = cpu_to_le16(reason),
2015 	};
2016 	struct wmi_del_sta_cmd del_sta_cmd = {
2017 		.disconnect_reason = cpu_to_le16(reason),
2018 	};
2019 	struct {
2020 		struct wmi_cmd_hdr wmi;
2021 		struct wmi_disconnect_event evt;
2022 	} __packed reply;
2023 
2024 	wil_dbg_wmi(wil, "disconnect_sta: (%pM, reason %d)\n", mac, reason);
2025 
2026 	memset(&reply, 0, sizeof(reply));
2027 	vif->locally_generated_disc = true;
2028 	if (del_sta) {
2029 		ether_addr_copy(del_sta_cmd.dst_mac, mac);
2030 		rc = wmi_call(wil, WMI_DEL_STA_CMDID, vif->mid, &del_sta_cmd,
2031 			      sizeof(del_sta_cmd), WMI_DISCONNECT_EVENTID,
2032 			      &reply, sizeof(reply), 1000);
2033 	} else {
2034 		ether_addr_copy(disc_sta_cmd.dst_mac, mac);
2035 		rc = wmi_call(wil, WMI_DISCONNECT_STA_CMDID, vif->mid,
2036 			      &disc_sta_cmd, sizeof(disc_sta_cmd),
2037 			      WMI_DISCONNECT_EVENTID,
2038 			      &reply, sizeof(reply), 1000);
2039 	}
2040 	/* failure to disconnect in reasonable time treated as FW error */
2041 	if (rc) {
2042 		wil_fw_error_recovery(wil);
2043 		return rc;
2044 	}
2045 
2046 	if (full_disconnect) {
2047 		/* call event handler manually after processing wmi_call,
2048 		 * to avoid deadlock - disconnect event handler acquires
2049 		 * wil->mutex while it is already held here
2050 		 */
2051 		reason_code = le16_to_cpu(reply.evt.protocol_reason_status);
2052 
2053 		wil_dbg_wmi(wil, "Disconnect %pM reason [proto %d wmi %d]\n",
2054 			    reply.evt.bssid, reason_code,
2055 			    reply.evt.disconnect_reason);
2056 
2057 		wil->sinfo_gen++;
2058 		wil6210_disconnect(vif, reply.evt.bssid, reason_code, true);
2059 	}
2060 	return 0;
2061 }
2062 
2063 int wmi_addba(struct wil6210_priv *wil, u8 mid,
2064 	      u8 ringid, u8 size, u16 timeout)
2065 {
2066 	struct wmi_vring_ba_en_cmd cmd = {
2067 		.ringid = ringid,
2068 		.agg_max_wsize = size,
2069 		.ba_timeout = cpu_to_le16(timeout),
2070 		.amsdu = 0,
2071 	};
2072 
2073 	wil_dbg_wmi(wil, "addba: (ring %d size %d timeout %d)\n", ringid, size,
2074 		    timeout);
2075 
2076 	return wmi_send(wil, WMI_VRING_BA_EN_CMDID, mid, &cmd, sizeof(cmd));
2077 }
2078 
2079 int wmi_delba_tx(struct wil6210_priv *wil, u8 mid, u8 ringid, u16 reason)
2080 {
2081 	struct wmi_vring_ba_dis_cmd cmd = {
2082 		.ringid = ringid,
2083 		.reason = cpu_to_le16(reason),
2084 	};
2085 
2086 	wil_dbg_wmi(wil, "delba_tx: (ring %d reason %d)\n", ringid, reason);
2087 
2088 	return wmi_send(wil, WMI_VRING_BA_DIS_CMDID, mid, &cmd, sizeof(cmd));
2089 }
2090 
2091 int wmi_delba_rx(struct wil6210_priv *wil, u8 mid, u8 cidxtid, u16 reason)
2092 {
2093 	struct wmi_rcp_delba_cmd cmd = {
2094 		.cidxtid = cidxtid,
2095 		.reason = cpu_to_le16(reason),
2096 	};
2097 
2098 	wil_dbg_wmi(wil, "delba_rx: (CID %d TID %d reason %d)\n", cidxtid & 0xf,
2099 		    (cidxtid >> 4) & 0xf, reason);
2100 
2101 	return wmi_send(wil, WMI_RCP_DELBA_CMDID, mid, &cmd, sizeof(cmd));
2102 }
2103 
2104 int wmi_addba_rx_resp(struct wil6210_priv *wil,
2105 		      u8 mid, u8 cid, u8 tid, u8 token,
2106 		      u16 status, bool amsdu, u16 agg_wsize, u16 timeout)
2107 {
2108 	int rc;
2109 	struct wmi_rcp_addba_resp_cmd cmd = {
2110 		.cidxtid = mk_cidxtid(cid, tid),
2111 		.dialog_token = token,
2112 		.status_code = cpu_to_le16(status),
2113 		/* bit 0: A-MSDU supported
2114 		 * bit 1: policy (should be 0 for us)
2115 		 * bits 2..5: TID
2116 		 * bits 6..15: buffer size
2117 		 */
2118 		.ba_param_set = cpu_to_le16((amsdu ? 1 : 0) | (tid << 2) |
2119 					    (agg_wsize << 6)),
2120 		.ba_timeout = cpu_to_le16(timeout),
2121 	};
2122 	struct {
2123 		struct wmi_cmd_hdr wmi;
2124 		struct wmi_rcp_addba_resp_sent_event evt;
2125 	} __packed reply = {
2126 		.evt = {.status = cpu_to_le16(WMI_FW_STATUS_FAILURE)},
2127 	};
2128 
2129 	wil_dbg_wmi(wil,
2130 		    "ADDBA response for MID %d CID %d TID %d size %d timeout %d status %d AMSDU%s\n",
2131 		    mid, cid, tid, agg_wsize,
2132 		    timeout, status, amsdu ? "+" : "-");
2133 
2134 	rc = wmi_call(wil, WMI_RCP_ADDBA_RESP_CMDID, mid, &cmd, sizeof(cmd),
2135 		      WMI_RCP_ADDBA_RESP_SENT_EVENTID, &reply, sizeof(reply),
2136 		      100);
2137 	if (rc)
2138 		return rc;
2139 
2140 	if (reply.evt.status) {
2141 		wil_err(wil, "ADDBA response failed with status %d\n",
2142 			le16_to_cpu(reply.evt.status));
2143 		rc = -EINVAL;
2144 	}
2145 
2146 	return rc;
2147 }
2148 
2149 int wmi_ps_dev_profile_cfg(struct wil6210_priv *wil,
2150 			   enum wmi_ps_profile_type ps_profile)
2151 {
2152 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2153 	int rc;
2154 	struct wmi_ps_dev_profile_cfg_cmd cmd = {
2155 		.ps_profile = ps_profile,
2156 	};
2157 	struct {
2158 		struct wmi_cmd_hdr wmi;
2159 		struct wmi_ps_dev_profile_cfg_event evt;
2160 	} __packed reply = {
2161 		.evt = {.status = cpu_to_le32(WMI_PS_CFG_CMD_STATUS_ERROR)},
2162 	};
2163 	u32 status;
2164 
2165 	wil_dbg_wmi(wil, "Setting ps dev profile %d\n", ps_profile);
2166 
2167 	rc = wmi_call(wil, WMI_PS_DEV_PROFILE_CFG_CMDID, vif->mid,
2168 		      &cmd, sizeof(cmd),
2169 		      WMI_PS_DEV_PROFILE_CFG_EVENTID, &reply, sizeof(reply),
2170 		      100);
2171 	if (rc)
2172 		return rc;
2173 
2174 	status = le32_to_cpu(reply.evt.status);
2175 
2176 	if (status != WMI_PS_CFG_CMD_STATUS_SUCCESS) {
2177 		wil_err(wil, "ps dev profile cfg failed with status %d\n",
2178 			status);
2179 		rc = -EINVAL;
2180 	}
2181 
2182 	return rc;
2183 }
2184 
2185 int wmi_set_mgmt_retry(struct wil6210_priv *wil, u8 retry_short)
2186 {
2187 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2188 	int rc;
2189 	struct wmi_set_mgmt_retry_limit_cmd cmd = {
2190 		.mgmt_retry_limit = retry_short,
2191 	};
2192 	struct {
2193 		struct wmi_cmd_hdr wmi;
2194 		struct wmi_set_mgmt_retry_limit_event evt;
2195 	} __packed reply = {
2196 		.evt = {.status = WMI_FW_STATUS_FAILURE},
2197 	};
2198 
2199 	wil_dbg_wmi(wil, "Setting mgmt retry short %d\n", retry_short);
2200 
2201 	if (!test_bit(WMI_FW_CAPABILITY_MGMT_RETRY_LIMIT, wil->fw_capabilities))
2202 		return -ENOTSUPP;
2203 
2204 	rc = wmi_call(wil, WMI_SET_MGMT_RETRY_LIMIT_CMDID, vif->mid,
2205 		      &cmd, sizeof(cmd),
2206 		      WMI_SET_MGMT_RETRY_LIMIT_EVENTID, &reply, sizeof(reply),
2207 		      100);
2208 	if (rc)
2209 		return rc;
2210 
2211 	if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
2212 		wil_err(wil, "set mgmt retry limit failed with status %d\n",
2213 			reply.evt.status);
2214 		rc = -EINVAL;
2215 	}
2216 
2217 	return rc;
2218 }
2219 
2220 int wmi_get_mgmt_retry(struct wil6210_priv *wil, u8 *retry_short)
2221 {
2222 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2223 	int rc;
2224 	struct {
2225 		struct wmi_cmd_hdr wmi;
2226 		struct wmi_get_mgmt_retry_limit_event evt;
2227 	} __packed reply;
2228 
2229 	wil_dbg_wmi(wil, "getting mgmt retry short\n");
2230 
2231 	if (!test_bit(WMI_FW_CAPABILITY_MGMT_RETRY_LIMIT, wil->fw_capabilities))
2232 		return -ENOTSUPP;
2233 
2234 	memset(&reply, 0, sizeof(reply));
2235 	rc = wmi_call(wil, WMI_GET_MGMT_RETRY_LIMIT_CMDID, vif->mid, NULL, 0,
2236 		      WMI_GET_MGMT_RETRY_LIMIT_EVENTID, &reply, sizeof(reply),
2237 		      100);
2238 	if (rc)
2239 		return rc;
2240 
2241 	if (retry_short)
2242 		*retry_short = reply.evt.mgmt_retry_limit;
2243 
2244 	return 0;
2245 }
2246 
2247 int wmi_abort_scan(struct wil6210_vif *vif)
2248 {
2249 	struct wil6210_priv *wil = vif_to_wil(vif);
2250 	int rc;
2251 
2252 	wil_dbg_wmi(wil, "sending WMI_ABORT_SCAN_CMDID\n");
2253 
2254 	rc = wmi_send(wil, WMI_ABORT_SCAN_CMDID, vif->mid, NULL, 0);
2255 	if (rc)
2256 		wil_err(wil, "Failed to abort scan (%d)\n", rc);
2257 
2258 	return rc;
2259 }
2260 
2261 int wmi_new_sta(struct wil6210_vif *vif, const u8 *mac, u8 aid)
2262 {
2263 	struct wil6210_priv *wil = vif_to_wil(vif);
2264 	int rc;
2265 	struct wmi_new_sta_cmd cmd = {
2266 		.aid = aid,
2267 	};
2268 
2269 	wil_dbg_wmi(wil, "new sta %pM, aid %d\n", mac, aid);
2270 
2271 	ether_addr_copy(cmd.dst_mac, mac);
2272 
2273 	rc = wmi_send(wil, WMI_NEW_STA_CMDID, vif->mid, &cmd, sizeof(cmd));
2274 	if (rc)
2275 		wil_err(wil, "Failed to send new sta (%d)\n", rc);
2276 
2277 	return rc;
2278 }
2279 
2280 void wmi_event_flush(struct wil6210_priv *wil)
2281 {
2282 	ulong flags;
2283 	struct pending_wmi_event *evt, *t;
2284 
2285 	wil_dbg_wmi(wil, "event_flush\n");
2286 
2287 	spin_lock_irqsave(&wil->wmi_ev_lock, flags);
2288 
2289 	list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
2290 		list_del(&evt->list);
2291 		kfree(evt);
2292 	}
2293 
2294 	spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
2295 }
2296 
2297 static const char *suspend_status2name(u8 status)
2298 {
2299 	switch (status) {
2300 	case WMI_TRAFFIC_SUSPEND_REJECTED_LINK_NOT_IDLE:
2301 		return "LINK_NOT_IDLE";
2302 	default:
2303 		return "Untracked status";
2304 	}
2305 }
2306 
2307 int wmi_suspend(struct wil6210_priv *wil)
2308 {
2309 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2310 	int rc;
2311 	struct wmi_traffic_suspend_cmd cmd = {
2312 		.wakeup_trigger = wil->wakeup_trigger,
2313 	};
2314 	struct {
2315 		struct wmi_cmd_hdr wmi;
2316 		struct wmi_traffic_suspend_event evt;
2317 	} __packed reply = {
2318 		.evt = {.status = WMI_TRAFFIC_SUSPEND_REJECTED_LINK_NOT_IDLE},
2319 	};
2320 
2321 	u32 suspend_to = WIL_WAIT_FOR_SUSPEND_RESUME_COMP;
2322 
2323 	wil->suspend_resp_rcvd = false;
2324 	wil->suspend_resp_comp = false;
2325 
2326 	rc = wmi_call(wil, WMI_TRAFFIC_SUSPEND_CMDID, vif->mid,
2327 		      &cmd, sizeof(cmd),
2328 		      WMI_TRAFFIC_SUSPEND_EVENTID, &reply, sizeof(reply),
2329 		      suspend_to);
2330 	if (rc) {
2331 		wil_err(wil, "wmi_call for suspend req failed, rc=%d\n", rc);
2332 		if (rc == -ETIME)
2333 			/* wmi_call TO */
2334 			wil->suspend_stats.rejected_by_device++;
2335 		else
2336 			wil->suspend_stats.rejected_by_host++;
2337 		goto out;
2338 	}
2339 
2340 	wil_dbg_wmi(wil, "waiting for suspend_response_completed\n");
2341 
2342 	rc = wait_event_interruptible_timeout(wil->wq,
2343 					      wil->suspend_resp_comp,
2344 					      msecs_to_jiffies(suspend_to));
2345 	if (rc == 0) {
2346 		wil_err(wil, "TO waiting for suspend_response_completed\n");
2347 		if (wil->suspend_resp_rcvd)
2348 			/* Device responded but we TO due to another reason */
2349 			wil->suspend_stats.rejected_by_host++;
2350 		else
2351 			wil->suspend_stats.rejected_by_device++;
2352 		rc = -EBUSY;
2353 		goto out;
2354 	}
2355 
2356 	wil_dbg_wmi(wil, "suspend_response_completed rcvd\n");
2357 	if (reply.evt.status != WMI_TRAFFIC_SUSPEND_APPROVED) {
2358 		wil_dbg_pm(wil, "device rejected the suspend, %s\n",
2359 			   suspend_status2name(reply.evt.status));
2360 		wil->suspend_stats.rejected_by_device++;
2361 	}
2362 	rc = reply.evt.status;
2363 
2364 out:
2365 	wil->suspend_resp_rcvd = false;
2366 	wil->suspend_resp_comp = false;
2367 
2368 	return rc;
2369 }
2370 
2371 static void resume_triggers2string(u32 triggers, char *string, int str_size)
2372 {
2373 	string[0] = '\0';
2374 
2375 	if (!triggers) {
2376 		strlcat(string, " UNKNOWN", str_size);
2377 		return;
2378 	}
2379 
2380 	if (triggers & WMI_RESUME_TRIGGER_HOST)
2381 		strlcat(string, " HOST", str_size);
2382 
2383 	if (triggers & WMI_RESUME_TRIGGER_UCAST_RX)
2384 		strlcat(string, " UCAST_RX", str_size);
2385 
2386 	if (triggers & WMI_RESUME_TRIGGER_BCAST_RX)
2387 		strlcat(string, " BCAST_RX", str_size);
2388 
2389 	if (triggers & WMI_RESUME_TRIGGER_WMI_EVT)
2390 		strlcat(string, " WMI_EVT", str_size);
2391 }
2392 
2393 int wmi_resume(struct wil6210_priv *wil)
2394 {
2395 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2396 	int rc;
2397 	char string[100];
2398 	struct {
2399 		struct wmi_cmd_hdr wmi;
2400 		struct wmi_traffic_resume_event evt;
2401 	} __packed reply = {
2402 		.evt = {.status = WMI_TRAFFIC_RESUME_FAILED,
2403 			.resume_triggers =
2404 				cpu_to_le32(WMI_RESUME_TRIGGER_UNKNOWN)},
2405 	};
2406 
2407 	rc = wmi_call(wil, WMI_TRAFFIC_RESUME_CMDID, vif->mid, NULL, 0,
2408 		      WMI_TRAFFIC_RESUME_EVENTID, &reply, sizeof(reply),
2409 		      WIL_WAIT_FOR_SUSPEND_RESUME_COMP);
2410 	if (rc)
2411 		return rc;
2412 	resume_triggers2string(le32_to_cpu(reply.evt.resume_triggers), string,
2413 			       sizeof(string));
2414 	wil_dbg_pm(wil, "device resume %s, resume triggers:%s (0x%x)\n",
2415 		   reply.evt.status ? "failed" : "passed", string,
2416 		   le32_to_cpu(reply.evt.resume_triggers));
2417 
2418 	return reply.evt.status;
2419 }
2420 
2421 int wmi_port_allocate(struct wil6210_priv *wil, u8 mid,
2422 		      const u8 *mac, enum nl80211_iftype iftype)
2423 {
2424 	int rc;
2425 	struct wmi_port_allocate_cmd cmd = {
2426 		.mid = mid,
2427 	};
2428 	struct {
2429 		struct wmi_cmd_hdr wmi;
2430 		struct wmi_port_allocated_event evt;
2431 	} __packed reply = {
2432 		.evt = {.status = WMI_FW_STATUS_FAILURE},
2433 	};
2434 
2435 	wil_dbg_misc(wil, "port allocate, mid %d iftype %d, mac %pM\n",
2436 		     mid, iftype, mac);
2437 
2438 	ether_addr_copy(cmd.mac, mac);
2439 	switch (iftype) {
2440 	case NL80211_IFTYPE_STATION:
2441 		cmd.port_role = WMI_PORT_STA;
2442 		break;
2443 	case NL80211_IFTYPE_AP:
2444 		cmd.port_role = WMI_PORT_AP;
2445 		break;
2446 	case NL80211_IFTYPE_P2P_CLIENT:
2447 		cmd.port_role = WMI_PORT_P2P_CLIENT;
2448 		break;
2449 	case NL80211_IFTYPE_P2P_GO:
2450 		cmd.port_role = WMI_PORT_P2P_GO;
2451 		break;
2452 	/* what about monitor??? */
2453 	default:
2454 		wil_err(wil, "unsupported iftype: %d\n", iftype);
2455 		return -EINVAL;
2456 	}
2457 
2458 	rc = wmi_call(wil, WMI_PORT_ALLOCATE_CMDID, mid,
2459 		      &cmd, sizeof(cmd),
2460 		      WMI_PORT_ALLOCATED_EVENTID, &reply,
2461 		      sizeof(reply), 300);
2462 	if (rc) {
2463 		wil_err(wil, "failed to allocate port, status %d\n", rc);
2464 		return rc;
2465 	}
2466 	if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
2467 		wil_err(wil, "WMI_PORT_ALLOCATE returned status %d\n",
2468 			reply.evt.status);
2469 		return -EINVAL;
2470 	}
2471 
2472 	return 0;
2473 }
2474 
2475 int wmi_port_delete(struct wil6210_priv *wil, u8 mid)
2476 {
2477 	int rc;
2478 	struct wmi_port_delete_cmd cmd = {
2479 		.mid = mid,
2480 	};
2481 	struct {
2482 		struct wmi_cmd_hdr wmi;
2483 		struct wmi_port_deleted_event evt;
2484 	} __packed reply = {
2485 		.evt = {.status = WMI_FW_STATUS_FAILURE},
2486 	};
2487 
2488 	wil_dbg_misc(wil, "port delete, mid %d\n", mid);
2489 
2490 	rc = wmi_call(wil, WMI_PORT_DELETE_CMDID, mid,
2491 		      &cmd, sizeof(cmd),
2492 		      WMI_PORT_DELETED_EVENTID, &reply,
2493 		      sizeof(reply), 2000);
2494 	if (rc) {
2495 		wil_err(wil, "failed to delete port, status %d\n", rc);
2496 		return rc;
2497 	}
2498 	if (reply.evt.status != WMI_FW_STATUS_SUCCESS) {
2499 		wil_err(wil, "WMI_PORT_DELETE returned status %d\n",
2500 			reply.evt.status);
2501 		return -EINVAL;
2502 	}
2503 
2504 	return 0;
2505 }
2506 
2507 static bool wmi_evt_call_handler(struct wil6210_vif *vif, int id,
2508 				 void *d, int len)
2509 {
2510 	uint i;
2511 
2512 	for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
2513 		if (wmi_evt_handlers[i].eventid == id) {
2514 			wmi_evt_handlers[i].handler(vif, id, d, len);
2515 			return true;
2516 		}
2517 	}
2518 
2519 	return false;
2520 }
2521 
2522 static void wmi_event_handle(struct wil6210_priv *wil,
2523 			     struct wil6210_mbox_hdr *hdr)
2524 {
2525 	u16 len = le16_to_cpu(hdr->len);
2526 	struct wil6210_vif *vif;
2527 
2528 	if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
2529 	    (len >= sizeof(struct wmi_cmd_hdr))) {
2530 		struct wmi_cmd_hdr *wmi = (void *)(&hdr[1]);
2531 		void *evt_data = (void *)(&wmi[1]);
2532 		u16 id = le16_to_cpu(wmi->command_id);
2533 		u8 mid = wmi->mid;
2534 
2535 		wil_dbg_wmi(wil, "Handle %s (0x%04x) (reply_id 0x%04x,%d)\n",
2536 			    eventid2name(id), id, wil->reply_id,
2537 			    wil->reply_mid);
2538 
2539 		if (mid == MID_BROADCAST)
2540 			mid = 0;
2541 		if (mid >= wil->max_vifs) {
2542 			wil_dbg_wmi(wil, "invalid mid %d, event skipped\n",
2543 				    mid);
2544 			return;
2545 		}
2546 		vif = wil->vifs[mid];
2547 		if (!vif) {
2548 			wil_dbg_wmi(wil, "event for empty VIF(%d), skipped\n",
2549 				    mid);
2550 			return;
2551 		}
2552 
2553 		/* check if someone waits for this event */
2554 		if (wil->reply_id && wil->reply_id == id &&
2555 		    wil->reply_mid == mid) {
2556 			WARN_ON(wil->reply_buf);
2557 
2558 			wmi_evt_call_handler(vif, id, evt_data,
2559 					     len - sizeof(*wmi));
2560 			wil_dbg_wmi(wil, "event_handle: Complete WMI 0x%04x\n",
2561 				    id);
2562 			complete(&wil->wmi_call);
2563 			return;
2564 		}
2565 		/* unsolicited event */
2566 		/* search for handler */
2567 		if (!wmi_evt_call_handler(vif, id, evt_data,
2568 					  len - sizeof(*wmi))) {
2569 			wil_info(wil, "Unhandled event 0x%04x\n", id);
2570 		}
2571 	} else {
2572 		wil_err(wil, "Unknown event type\n");
2573 		print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
2574 			       hdr, sizeof(*hdr) + len, true);
2575 	}
2576 }
2577 
2578 /*
2579  * Retrieve next WMI event from the pending list
2580  */
2581 static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
2582 {
2583 	ulong flags;
2584 	struct list_head *ret = NULL;
2585 
2586 	spin_lock_irqsave(&wil->wmi_ev_lock, flags);
2587 
2588 	if (!list_empty(&wil->pending_wmi_ev)) {
2589 		ret = wil->pending_wmi_ev.next;
2590 		list_del(ret);
2591 	}
2592 
2593 	spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
2594 
2595 	return ret;
2596 }
2597 
2598 /*
2599  * Handler for the WMI events
2600  */
2601 void wmi_event_worker(struct work_struct *work)
2602 {
2603 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
2604 						 wmi_event_worker);
2605 	struct pending_wmi_event *evt;
2606 	struct list_head *lh;
2607 
2608 	wil_dbg_wmi(wil, "event_worker: Start\n");
2609 	while ((lh = next_wmi_ev(wil)) != NULL) {
2610 		evt = list_entry(lh, struct pending_wmi_event, list);
2611 		wmi_event_handle(wil, &evt->event.hdr);
2612 		kfree(evt);
2613 	}
2614 	wil_dbg_wmi(wil, "event_worker: Finished\n");
2615 }
2616 
2617 bool wil_is_wmi_idle(struct wil6210_priv *wil)
2618 {
2619 	ulong flags;
2620 	struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
2621 	bool rc = false;
2622 
2623 	spin_lock_irqsave(&wil->wmi_ev_lock, flags);
2624 
2625 	/* Check if there are pending WMI events in the events queue */
2626 	if (!list_empty(&wil->pending_wmi_ev)) {
2627 		wil_dbg_pm(wil, "Pending WMI events in queue\n");
2628 		goto out;
2629 	}
2630 
2631 	/* Check if there is a pending WMI call */
2632 	if (wil->reply_id) {
2633 		wil_dbg_pm(wil, "Pending WMI call\n");
2634 		goto out;
2635 	}
2636 
2637 	/* Check if there are pending RX events in mbox */
2638 	r->head = wil_r(wil, RGF_MBOX +
2639 			offsetof(struct wil6210_mbox_ctl, rx.head));
2640 	if (r->tail != r->head)
2641 		wil_dbg_pm(wil, "Pending WMI mbox events\n");
2642 	else
2643 		rc = true;
2644 
2645 out:
2646 	spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
2647 	return rc;
2648 }
2649 
2650 static void
2651 wmi_sched_scan_set_ssids(struct wil6210_priv *wil,
2652 			 struct wmi_start_sched_scan_cmd *cmd,
2653 			 struct cfg80211_ssid *ssids, int n_ssids,
2654 			 struct cfg80211_match_set *match_sets,
2655 			 int n_match_sets)
2656 {
2657 	int i;
2658 
2659 	if (n_match_sets > WMI_MAX_PNO_SSID_NUM) {
2660 		wil_dbg_wmi(wil, "too many match sets (%d), use first %d\n",
2661 			    n_match_sets, WMI_MAX_PNO_SSID_NUM);
2662 		n_match_sets = WMI_MAX_PNO_SSID_NUM;
2663 	}
2664 	cmd->num_of_ssids = n_match_sets;
2665 
2666 	for (i = 0; i < n_match_sets; i++) {
2667 		struct wmi_sched_scan_ssid_match *wmi_match =
2668 			&cmd->ssid_for_match[i];
2669 		struct cfg80211_match_set *cfg_match = &match_sets[i];
2670 		int j;
2671 
2672 		wmi_match->ssid_len = cfg_match->ssid.ssid_len;
2673 		memcpy(wmi_match->ssid, cfg_match->ssid.ssid,
2674 		       min_t(u8, wmi_match->ssid_len, WMI_MAX_SSID_LEN));
2675 		wmi_match->rssi_threshold = S8_MIN;
2676 		if (cfg_match->rssi_thold >= S8_MIN &&
2677 		    cfg_match->rssi_thold <= S8_MAX)
2678 			wmi_match->rssi_threshold = cfg_match->rssi_thold;
2679 
2680 		for (j = 0; j < n_ssids; j++)
2681 			if (wmi_match->ssid_len == ssids[j].ssid_len &&
2682 			    memcmp(wmi_match->ssid, ssids[j].ssid,
2683 				   wmi_match->ssid_len) == 0)
2684 				wmi_match->add_ssid_to_probe = true;
2685 	}
2686 }
2687 
2688 static void
2689 wmi_sched_scan_set_channels(struct wil6210_priv *wil,
2690 			    struct wmi_start_sched_scan_cmd *cmd,
2691 			    u32 n_channels,
2692 			    struct ieee80211_channel **channels)
2693 {
2694 	int i;
2695 
2696 	if (n_channels > WMI_MAX_CHANNEL_NUM) {
2697 		wil_dbg_wmi(wil, "too many channels (%d), use first %d\n",
2698 			    n_channels, WMI_MAX_CHANNEL_NUM);
2699 		n_channels = WMI_MAX_CHANNEL_NUM;
2700 	}
2701 	cmd->num_of_channels = n_channels;
2702 
2703 	for (i = 0; i < n_channels; i++) {
2704 		struct ieee80211_channel *cfg_chan = channels[i];
2705 
2706 		cmd->channel_list[i] = cfg_chan->hw_value - 1;
2707 	}
2708 }
2709 
2710 static void
2711 wmi_sched_scan_set_plans(struct wil6210_priv *wil,
2712 			 struct wmi_start_sched_scan_cmd *cmd,
2713 			 struct cfg80211_sched_scan_plan *scan_plans,
2714 			 int n_scan_plans)
2715 {
2716 	int i;
2717 
2718 	if (n_scan_plans > WMI_MAX_PLANS_NUM) {
2719 		wil_dbg_wmi(wil, "too many plans (%d), use first %d\n",
2720 			    n_scan_plans, WMI_MAX_PLANS_NUM);
2721 		n_scan_plans = WMI_MAX_PLANS_NUM;
2722 	}
2723 
2724 	for (i = 0; i < n_scan_plans; i++) {
2725 		struct cfg80211_sched_scan_plan *cfg_plan = &scan_plans[i];
2726 
2727 		cmd->scan_plans[i].interval_sec =
2728 			cpu_to_le16(cfg_plan->interval);
2729 		cmd->scan_plans[i].num_of_iterations =
2730 			cpu_to_le16(cfg_plan->iterations);
2731 	}
2732 }
2733 
2734 int wmi_start_sched_scan(struct wil6210_priv *wil,
2735 			 struct cfg80211_sched_scan_request *request)
2736 {
2737 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2738 	int rc;
2739 	struct wmi_start_sched_scan_cmd cmd = {
2740 		.min_rssi_threshold = S8_MIN,
2741 		.initial_delay_sec = cpu_to_le16(request->delay),
2742 	};
2743 	struct {
2744 		struct wmi_cmd_hdr wmi;
2745 		struct wmi_start_sched_scan_event evt;
2746 	} __packed reply = {
2747 		.evt = {.result = WMI_PNO_REJECT},
2748 	};
2749 
2750 	if (!test_bit(WMI_FW_CAPABILITY_PNO, wil->fw_capabilities))
2751 		return -ENOTSUPP;
2752 
2753 	if (request->min_rssi_thold >= S8_MIN &&
2754 	    request->min_rssi_thold <= S8_MAX)
2755 		cmd.min_rssi_threshold = request->min_rssi_thold;
2756 
2757 	wmi_sched_scan_set_ssids(wil, &cmd, request->ssids, request->n_ssids,
2758 				 request->match_sets, request->n_match_sets);
2759 	wmi_sched_scan_set_channels(wil, &cmd,
2760 				    request->n_channels, request->channels);
2761 	wmi_sched_scan_set_plans(wil, &cmd,
2762 				 request->scan_plans, request->n_scan_plans);
2763 
2764 	rc = wmi_call(wil, WMI_START_SCHED_SCAN_CMDID, vif->mid,
2765 		      &cmd, sizeof(cmd),
2766 		      WMI_START_SCHED_SCAN_EVENTID, &reply, sizeof(reply),
2767 		      WIL_WMI_CALL_GENERAL_TO_MS);
2768 	if (rc)
2769 		return rc;
2770 
2771 	if (reply.evt.result != WMI_PNO_SUCCESS) {
2772 		wil_err(wil, "start sched scan failed, result %d\n",
2773 			reply.evt.result);
2774 		return -EINVAL;
2775 	}
2776 
2777 	return 0;
2778 }
2779 
2780 int wmi_stop_sched_scan(struct wil6210_priv *wil)
2781 {
2782 	struct wil6210_vif *vif = ndev_to_vif(wil->main_ndev);
2783 	int rc;
2784 	struct {
2785 		struct wmi_cmd_hdr wmi;
2786 		struct wmi_stop_sched_scan_event evt;
2787 	} __packed reply = {
2788 		.evt = {.result = WMI_PNO_REJECT},
2789 	};
2790 
2791 	if (!test_bit(WMI_FW_CAPABILITY_PNO, wil->fw_capabilities))
2792 		return -ENOTSUPP;
2793 
2794 	rc = wmi_call(wil, WMI_STOP_SCHED_SCAN_CMDID, vif->mid, NULL, 0,
2795 		      WMI_STOP_SCHED_SCAN_EVENTID, &reply, sizeof(reply),
2796 		      WIL_WMI_CALL_GENERAL_TO_MS);
2797 	if (rc)
2798 		return rc;
2799 
2800 	if (reply.evt.result != WMI_PNO_SUCCESS) {
2801 		wil_err(wil, "stop sched scan failed, result %d\n",
2802 			reply.evt.result);
2803 		return -EINVAL;
2804 	}
2805 
2806 	return 0;
2807 }
2808 
2809 int wmi_mgmt_tx(struct wil6210_vif *vif, const u8 *buf, size_t len)
2810 {
2811 	size_t total;
2812 	struct wil6210_priv *wil = vif_to_wil(vif);
2813 	struct ieee80211_mgmt *mgmt_frame = (void *)buf;
2814 	struct wmi_sw_tx_req_cmd *cmd;
2815 	struct {
2816 		struct wmi_cmd_hdr wmi;
2817 		struct wmi_sw_tx_complete_event evt;
2818 	} __packed evt = {
2819 		.evt = {.status = WMI_FW_STATUS_FAILURE},
2820 	};
2821 	int rc;
2822 
2823 	wil_dbg_misc(wil, "mgmt_tx mid %d\n", vif->mid);
2824 	wil_hex_dump_misc("mgmt tx frame ", DUMP_PREFIX_OFFSET, 16, 1, buf,
2825 			  len, true);
2826 
2827 	if (len < sizeof(struct ieee80211_hdr_3addr))
2828 		return -EINVAL;
2829 
2830 	total = sizeof(*cmd) + len;
2831 	if (total < len) {
2832 		wil_err(wil, "mgmt_tx invalid len %zu\n", len);
2833 		return -EINVAL;
2834 	}
2835 
2836 	cmd = kmalloc(total, GFP_KERNEL);
2837 	if (!cmd)
2838 		return -ENOMEM;
2839 
2840 	memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
2841 	cmd->len = cpu_to_le16(len);
2842 	memcpy(cmd->payload, buf, len);
2843 
2844 	rc = wmi_call(wil, WMI_SW_TX_REQ_CMDID, vif->mid, cmd, total,
2845 		      WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
2846 	if (!rc && evt.evt.status != WMI_FW_STATUS_SUCCESS) {
2847 		wil_err(wil, "mgmt_tx failed with status %d\n", evt.evt.status);
2848 		rc = -EINVAL;
2849 	}
2850 
2851 	kfree(cmd);
2852 
2853 	return rc;
2854 }
2855