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