12be7d22fSVladimir Kondratiev /*
2230d8442SVladimir Kondratiev  * Copyright (c) 2012-2015 Qualcomm Atheros, Inc.
32be7d22fSVladimir Kondratiev  *
42be7d22fSVladimir Kondratiev  * Permission to use, copy, modify, and/or distribute this software for any
52be7d22fSVladimir Kondratiev  * purpose with or without fee is hereby granted, provided that the above
62be7d22fSVladimir Kondratiev  * copyright notice and this permission notice appear in all copies.
72be7d22fSVladimir Kondratiev  *
82be7d22fSVladimir Kondratiev  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
92be7d22fSVladimir Kondratiev  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
102be7d22fSVladimir Kondratiev  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
112be7d22fSVladimir Kondratiev  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
122be7d22fSVladimir Kondratiev  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
132be7d22fSVladimir Kondratiev  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
142be7d22fSVladimir Kondratiev  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
152be7d22fSVladimir Kondratiev  */
162be7d22fSVladimir Kondratiev 
171eb9d1e5SDedy Lansky #include <linux/moduleparam.h>
182be7d22fSVladimir Kondratiev #include <linux/etherdevice.h>
1947e19af9SVladimir Kondratiev #include <linux/if_arp.h>
202be7d22fSVladimir Kondratiev 
212be7d22fSVladimir Kondratiev #include "wil6210.h"
2247e19af9SVladimir Kondratiev #include "txrx.h"
232be7d22fSVladimir Kondratiev #include "wmi.h"
2498658095SVladimir Kondratiev #include "trace.h"
252be7d22fSVladimir Kondratiev 
26327755fbSVladimir Kondratiev static uint max_assoc_sta = WIL6210_MAX_CID;
271eb9d1e5SDedy Lansky module_param(max_assoc_sta, uint, S_IRUGO | S_IWUSR);
281eb9d1e5SDedy Lansky MODULE_PARM_DESC(max_assoc_sta, " Max number of stations associated to the AP");
291eb9d1e5SDedy Lansky 
303a3def8dSVladimir Kondratiev int agg_wsize; /* = 0; */
313a3def8dSVladimir Kondratiev module_param(agg_wsize, int, S_IRUGO | S_IWUSR);
323a3def8dSVladimir Kondratiev MODULE_PARM_DESC(agg_wsize, " Window size for Tx Block Ack after connect;"
333a3def8dSVladimir Kondratiev 		 " 0 - use default; < 0 - don't auto-establish");
343a3def8dSVladimir Kondratiev 
352be7d22fSVladimir Kondratiev /**
362be7d22fSVladimir Kondratiev  * WMI event receiving - theory of operations
372be7d22fSVladimir Kondratiev  *
382be7d22fSVladimir Kondratiev  * When firmware about to report WMI event, it fills memory area
392be7d22fSVladimir Kondratiev  * in the mailbox and raises misc. IRQ. Thread interrupt handler invoked for
402be7d22fSVladimir Kondratiev  * the misc IRQ, function @wmi_recv_cmd called by thread IRQ handler.
412be7d22fSVladimir Kondratiev  *
422be7d22fSVladimir Kondratiev  * @wmi_recv_cmd reads event, allocates memory chunk  and attaches it to the
432be7d22fSVladimir Kondratiev  * event list @wil->pending_wmi_ev. Then, work queue @wil->wmi_wq wakes up
442be7d22fSVladimir Kondratiev  * and handles events within the @wmi_event_worker. Every event get detached
452be7d22fSVladimir Kondratiev  * from list, processed and deleted.
462be7d22fSVladimir Kondratiev  *
472be7d22fSVladimir Kondratiev  * Purpose for this mechanism is to release IRQ thread; otherwise,
482be7d22fSVladimir Kondratiev  * if WMI event handling involves another WMI command flow, this 2-nd flow
492be7d22fSVladimir Kondratiev  * won't be completed because of blocked IRQ thread.
502be7d22fSVladimir Kondratiev  */
512be7d22fSVladimir Kondratiev 
522be7d22fSVladimir Kondratiev /**
532be7d22fSVladimir Kondratiev  * Addressing - theory of operations
542be7d22fSVladimir Kondratiev  *
552be7d22fSVladimir Kondratiev  * There are several buses present on the WIL6210 card.
562be7d22fSVladimir Kondratiev  * Same memory areas are visible at different address on
572be7d22fSVladimir Kondratiev  * the different busses. There are 3 main bus masters:
582be7d22fSVladimir Kondratiev  *  - MAC CPU (ucode)
592be7d22fSVladimir Kondratiev  *  - User CPU (firmware)
602be7d22fSVladimir Kondratiev  *  - AHB (host)
612be7d22fSVladimir Kondratiev  *
622be7d22fSVladimir Kondratiev  * On the PCI bus, there is one BAR (BAR0) of 2Mb size, exposing
632be7d22fSVladimir Kondratiev  * AHB addresses starting from 0x880000
642be7d22fSVladimir Kondratiev  *
652be7d22fSVladimir Kondratiev  * Internally, firmware uses addresses that allows faster access but
662be7d22fSVladimir Kondratiev  * are invisible from the host. To read from these addresses, alternative
672be7d22fSVladimir Kondratiev  * AHB address must be used.
682be7d22fSVladimir Kondratiev  *
692be7d22fSVladimir Kondratiev  * Memory mapping
702be7d22fSVladimir Kondratiev  * Linker address         PCI/Host address
712be7d22fSVladimir Kondratiev  *                        0x880000 .. 0xa80000  2Mb BAR0
722be7d22fSVladimir Kondratiev  * 0x800000 .. 0x807000   0x900000 .. 0x907000  28k DCCM
732be7d22fSVladimir Kondratiev  * 0x840000 .. 0x857000   0x908000 .. 0x91f000  92k PERIPH
742be7d22fSVladimir Kondratiev  */
752be7d22fSVladimir Kondratiev 
762be7d22fSVladimir Kondratiev /**
772be7d22fSVladimir Kondratiev  * @fw_mapping provides memory remapping table
78b541d0a0SVladimir Kondratiev  *
79b541d0a0SVladimir Kondratiev  * array size should be in sync with the declaration in the wil6210.h
802be7d22fSVladimir Kondratiev  */
81b541d0a0SVladimir Kondratiev const struct fw_map fw_mapping[] = {
82b541d0a0SVladimir Kondratiev 	{0x000000, 0x040000, 0x8c0000, "fw_code"}, /* FW code RAM      256k */
83b541d0a0SVladimir Kondratiev 	{0x800000, 0x808000, 0x900000, "fw_data"}, /* FW data RAM       32k */
84b541d0a0SVladimir Kondratiev 	{0x840000, 0x860000, 0x908000, "fw_peri"}, /* periph. data RAM 128k */
85b541d0a0SVladimir Kondratiev 	{0x880000, 0x88a000, 0x880000, "rgf"},     /* various RGF       40k */
8672269146SVladimir Kondratiev 	{0x88a000, 0x88b000, 0x88a000, "AGC_tbl"}, /* AGC table          4k */
87b541d0a0SVladimir Kondratiev 	{0x88b000, 0x88c000, 0x88b000, "rgf_ext"}, /* Pcie_ext_rgf       4k */
88b541d0a0SVladimir Kondratiev 	{0x8c0000, 0x949000, 0x8c0000, "upper"},   /* upper area       548k */
892be7d22fSVladimir Kondratiev 	/*
902be7d22fSVladimir Kondratiev 	 * 920000..930000 ucode code RAM
912be7d22fSVladimir Kondratiev 	 * 930000..932000 ucode data RAM
92af6b48dbSVladimir Kondratiev 	 * 932000..949000 back-door debug data
932be7d22fSVladimir Kondratiev 	 */
942be7d22fSVladimir Kondratiev };
952be7d22fSVladimir Kondratiev 
962be7d22fSVladimir Kondratiev /**
972be7d22fSVladimir Kondratiev  * return AHB address for given firmware/ucode internal (linker) address
982be7d22fSVladimir Kondratiev  * @x - internal address
992be7d22fSVladimir Kondratiev  * If address have no valid AHB mapping, return 0
1002be7d22fSVladimir Kondratiev  */
1012be7d22fSVladimir Kondratiev static u32 wmi_addr_remap(u32 x)
1022be7d22fSVladimir Kondratiev {
1032be7d22fSVladimir Kondratiev 	uint i;
1042be7d22fSVladimir Kondratiev 
1052be7d22fSVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
1062be7d22fSVladimir Kondratiev 		if ((x >= fw_mapping[i].from) && (x < fw_mapping[i].to))
1072be7d22fSVladimir Kondratiev 			return x + fw_mapping[i].host - fw_mapping[i].from;
1082be7d22fSVladimir Kondratiev 	}
1092be7d22fSVladimir Kondratiev 
1102be7d22fSVladimir Kondratiev 	return 0;
1112be7d22fSVladimir Kondratiev }
1122be7d22fSVladimir Kondratiev 
1132be7d22fSVladimir Kondratiev /**
1142be7d22fSVladimir Kondratiev  * Check address validity for WMI buffer; remap if needed
1152be7d22fSVladimir Kondratiev  * @ptr - internal (linker) fw/ucode address
1162be7d22fSVladimir Kondratiev  *
1172be7d22fSVladimir Kondratiev  * Valid buffer should be DWORD aligned
1182be7d22fSVladimir Kondratiev  *
1192be7d22fSVladimir Kondratiev  * return address for accessing buffer from the host;
1202be7d22fSVladimir Kondratiev  * if buffer is not valid, return NULL.
1212be7d22fSVladimir Kondratiev  */
1222be7d22fSVladimir Kondratiev void __iomem *wmi_buffer(struct wil6210_priv *wil, __le32 ptr_)
1232be7d22fSVladimir Kondratiev {
1242be7d22fSVladimir Kondratiev 	u32 off;
1252be7d22fSVladimir Kondratiev 	u32 ptr = le32_to_cpu(ptr_);
1262be7d22fSVladimir Kondratiev 
1272be7d22fSVladimir Kondratiev 	if (ptr % 4)
1282be7d22fSVladimir Kondratiev 		return NULL;
1292be7d22fSVladimir Kondratiev 
1302be7d22fSVladimir Kondratiev 	ptr = wmi_addr_remap(ptr);
1312be7d22fSVladimir Kondratiev 	if (ptr < WIL6210_FW_HOST_OFF)
1322be7d22fSVladimir Kondratiev 		return NULL;
1332be7d22fSVladimir Kondratiev 
1342be7d22fSVladimir Kondratiev 	off = HOSTADDR(ptr);
1352be7d22fSVladimir Kondratiev 	if (off > WIL6210_MEM_SIZE - 4)
1362be7d22fSVladimir Kondratiev 		return NULL;
1372be7d22fSVladimir Kondratiev 
1382be7d22fSVladimir Kondratiev 	return wil->csr + off;
1392be7d22fSVladimir Kondratiev }
1402be7d22fSVladimir Kondratiev 
1412be7d22fSVladimir Kondratiev /**
1422be7d22fSVladimir Kondratiev  * Check address validity
1432be7d22fSVladimir Kondratiev  */
1442be7d22fSVladimir Kondratiev void __iomem *wmi_addr(struct wil6210_priv *wil, u32 ptr)
1452be7d22fSVladimir Kondratiev {
1462be7d22fSVladimir Kondratiev 	u32 off;
1472be7d22fSVladimir Kondratiev 
1482be7d22fSVladimir Kondratiev 	if (ptr % 4)
1492be7d22fSVladimir Kondratiev 		return NULL;
1502be7d22fSVladimir Kondratiev 
1512be7d22fSVladimir Kondratiev 	if (ptr < WIL6210_FW_HOST_OFF)
1522be7d22fSVladimir Kondratiev 		return NULL;
1532be7d22fSVladimir Kondratiev 
1542be7d22fSVladimir Kondratiev 	off = HOSTADDR(ptr);
1552be7d22fSVladimir Kondratiev 	if (off > WIL6210_MEM_SIZE - 4)
1562be7d22fSVladimir Kondratiev 		return NULL;
1572be7d22fSVladimir Kondratiev 
1582be7d22fSVladimir Kondratiev 	return wil->csr + off;
1592be7d22fSVladimir Kondratiev }
1602be7d22fSVladimir Kondratiev 
1612be7d22fSVladimir Kondratiev int wmi_read_hdr(struct wil6210_priv *wil, __le32 ptr,
1622be7d22fSVladimir Kondratiev 		 struct wil6210_mbox_hdr *hdr)
1632be7d22fSVladimir Kondratiev {
1642be7d22fSVladimir Kondratiev 	void __iomem *src = wmi_buffer(wil, ptr);
1658fe59627SVladimir Kondratiev 
1662be7d22fSVladimir Kondratiev 	if (!src)
1672be7d22fSVladimir Kondratiev 		return -EINVAL;
1682be7d22fSVladimir Kondratiev 
1692be7d22fSVladimir Kondratiev 	wil_memcpy_fromio_32(hdr, src, sizeof(*hdr));
1702be7d22fSVladimir Kondratiev 
1712be7d22fSVladimir Kondratiev 	return 0;
1722be7d22fSVladimir Kondratiev }
1732be7d22fSVladimir Kondratiev 
1742be7d22fSVladimir Kondratiev static int __wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
1752be7d22fSVladimir Kondratiev {
1762be7d22fSVladimir Kondratiev 	struct {
1772be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr hdr;
1782be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
1792be7d22fSVladimir Kondratiev 	} __packed cmd = {
1802be7d22fSVladimir Kondratiev 		.hdr = {
1812be7d22fSVladimir Kondratiev 			.type = WIL_MBOX_HDR_TYPE_WMI,
1822be7d22fSVladimir Kondratiev 			.flags = 0,
1832be7d22fSVladimir Kondratiev 			.len = cpu_to_le16(sizeof(cmd.wmi) + len),
1842be7d22fSVladimir Kondratiev 		},
1852be7d22fSVladimir Kondratiev 		.wmi = {
186f988b23fSVladimir Kondratiev 			.mid = 0,
1872be7d22fSVladimir Kondratiev 			.id = cpu_to_le16(cmdid),
1882be7d22fSVladimir Kondratiev 		},
1892be7d22fSVladimir Kondratiev 	};
1902be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring *r = &wil->mbox_ctl.tx;
1912be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring_desc d_head;
1922be7d22fSVladimir Kondratiev 	u32 next_head;
1932be7d22fSVladimir Kondratiev 	void __iomem *dst;
1942be7d22fSVladimir Kondratiev 	void __iomem *head = wmi_addr(wil, r->head);
1952be7d22fSVladimir Kondratiev 	uint retry;
1962be7d22fSVladimir Kondratiev 
1972be7d22fSVladimir Kondratiev 	if (sizeof(cmd) + len > r->entry_size) {
1982be7d22fSVladimir Kondratiev 		wil_err(wil, "WMI size too large: %d bytes, max is %d\n",
1992be7d22fSVladimir Kondratiev 			(int)(sizeof(cmd) + len), r->entry_size);
2002be7d22fSVladimir Kondratiev 		return -ERANGE;
2012be7d22fSVladimir Kondratiev 	}
2022be7d22fSVladimir Kondratiev 
2032be7d22fSVladimir Kondratiev 	might_sleep();
2042be7d22fSVladimir Kondratiev 
2059419b6a2SVladimir Kondratiev 	if (!test_bit(wil_status_fwready, wil->status)) {
20615e23124SVladimir Kondratiev 		wil_err(wil, "WMI: cannot send command while FW not ready\n");
2072be7d22fSVladimir Kondratiev 		return -EAGAIN;
2082be7d22fSVladimir Kondratiev 	}
2092be7d22fSVladimir Kondratiev 
2102be7d22fSVladimir Kondratiev 	if (!head) {
2112be7d22fSVladimir Kondratiev 		wil_err(wil, "WMI head is garbage: 0x%08x\n", r->head);
2122be7d22fSVladimir Kondratiev 		return -EINVAL;
2132be7d22fSVladimir Kondratiev 	}
2142be7d22fSVladimir Kondratiev 	/* read Tx head till it is not busy */
2152be7d22fSVladimir Kondratiev 	for (retry = 5; retry > 0; retry--) {
2162be7d22fSVladimir Kondratiev 		wil_memcpy_fromio_32(&d_head, head, sizeof(d_head));
2172be7d22fSVladimir Kondratiev 		if (d_head.sync == 0)
2182be7d22fSVladimir Kondratiev 			break;
2192be7d22fSVladimir Kondratiev 		msleep(20);
2202be7d22fSVladimir Kondratiev 	}
2212be7d22fSVladimir Kondratiev 	if (d_head.sync != 0) {
2222be7d22fSVladimir Kondratiev 		wil_err(wil, "WMI head busy\n");
2232be7d22fSVladimir Kondratiev 		return -EBUSY;
2242be7d22fSVladimir Kondratiev 	}
2252be7d22fSVladimir Kondratiev 	/* next head */
2262be7d22fSVladimir Kondratiev 	next_head = r->base + ((r->head - r->base + sizeof(d_head)) % r->size);
2277743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "Head 0x%08x -> 0x%08x\n", r->head, next_head);
2282be7d22fSVladimir Kondratiev 	/* wait till FW finish with previous command */
2292be7d22fSVladimir Kondratiev 	for (retry = 5; retry > 0; retry--) {
2302be7d22fSVladimir Kondratiev 		r->tail = ioread32(wil->csr + HOST_MBOX +
2312be7d22fSVladimir Kondratiev 				   offsetof(struct wil6210_mbox_ctl, tx.tail));
2322be7d22fSVladimir Kondratiev 		if (next_head != r->tail)
2332be7d22fSVladimir Kondratiev 			break;
2342be7d22fSVladimir Kondratiev 		msleep(20);
2352be7d22fSVladimir Kondratiev 	}
2362be7d22fSVladimir Kondratiev 	if (next_head == r->tail) {
2372be7d22fSVladimir Kondratiev 		wil_err(wil, "WMI ring full\n");
2382be7d22fSVladimir Kondratiev 		return -EBUSY;
2392be7d22fSVladimir Kondratiev 	}
2402be7d22fSVladimir Kondratiev 	dst = wmi_buffer(wil, d_head.addr);
2412be7d22fSVladimir Kondratiev 	if (!dst) {
2422be7d22fSVladimir Kondratiev 		wil_err(wil, "invalid WMI buffer: 0x%08x\n",
2432be7d22fSVladimir Kondratiev 			le32_to_cpu(d_head.addr));
2442be7d22fSVladimir Kondratiev 		return -EINVAL;
2452be7d22fSVladimir Kondratiev 	}
2462be7d22fSVladimir Kondratiev 	cmd.hdr.seq = cpu_to_le16(++wil->wmi_seq);
2472be7d22fSVladimir Kondratiev 	/* set command */
2487743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "WMI command 0x%04x [%d]\n", cmdid, len);
2497743882dSVladimir Kondratiev 	wil_hex_dump_wmi("Cmd ", DUMP_PREFIX_OFFSET, 16, 1, &cmd,
2502be7d22fSVladimir Kondratiev 			 sizeof(cmd), true);
2517743882dSVladimir Kondratiev 	wil_hex_dump_wmi("cmd ", DUMP_PREFIX_OFFSET, 16, 1, buf,
2522be7d22fSVladimir Kondratiev 			 len, true);
2532be7d22fSVladimir Kondratiev 	wil_memcpy_toio_32(dst, &cmd, sizeof(cmd));
2542be7d22fSVladimir Kondratiev 	wil_memcpy_toio_32(dst + sizeof(cmd), buf, len);
2552be7d22fSVladimir Kondratiev 	/* mark entry as full */
2562be7d22fSVladimir Kondratiev 	iowrite32(1, wil->csr + HOSTADDR(r->head) +
2572be7d22fSVladimir Kondratiev 		  offsetof(struct wil6210_mbox_ring_desc, sync));
2582be7d22fSVladimir Kondratiev 	/* advance next ptr */
2592be7d22fSVladimir Kondratiev 	iowrite32(r->head = next_head, wil->csr + HOST_MBOX +
2602be7d22fSVladimir Kondratiev 		  offsetof(struct wil6210_mbox_ctl, tx.head));
2612be7d22fSVladimir Kondratiev 
262f988b23fSVladimir Kondratiev 	trace_wil6210_wmi_cmd(&cmd.wmi, buf, len);
26398658095SVladimir Kondratiev 
2642be7d22fSVladimir Kondratiev 	/* interrupt to FW */
2652be7d22fSVladimir Kondratiev 	iowrite32(SW_INT_MBOX, wil->csr + HOST_SW_INT);
2662be7d22fSVladimir Kondratiev 
2672be7d22fSVladimir Kondratiev 	return 0;
2682be7d22fSVladimir Kondratiev }
2692be7d22fSVladimir Kondratiev 
2702be7d22fSVladimir Kondratiev int wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
2712be7d22fSVladimir Kondratiev {
2722be7d22fSVladimir Kondratiev 	int rc;
2732be7d22fSVladimir Kondratiev 
2742be7d22fSVladimir Kondratiev 	mutex_lock(&wil->wmi_mutex);
2752be7d22fSVladimir Kondratiev 	rc = __wmi_send(wil, cmdid, buf, len);
2762be7d22fSVladimir Kondratiev 	mutex_unlock(&wil->wmi_mutex);
2772be7d22fSVladimir Kondratiev 
2782be7d22fSVladimir Kondratiev 	return rc;
2792be7d22fSVladimir Kondratiev }
2802be7d22fSVladimir Kondratiev 
2812be7d22fSVladimir Kondratiev /*=== Event handlers ===*/
2822be7d22fSVladimir Kondratiev static void wmi_evt_ready(struct wil6210_priv *wil, int id, void *d, int len)
2832be7d22fSVladimir Kondratiev {
2842be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
2852be7d22fSVladimir Kondratiev 	struct wmi_ready_event *evt = d;
2868fe59627SVladimir Kondratiev 
287b8023177SVladimir Kondratiev 	wil->fw_version = le32_to_cpu(evt->sw_version);
288b8023177SVladimir Kondratiev 	wil->n_mids = evt->numof_additional_mids;
2892be7d22fSVladimir Kondratiev 
29015e23124SVladimir Kondratiev 	wil_info(wil, "FW ver. %d; MAC %pM; %d MID's\n", wil->fw_version,
291b8023177SVladimir Kondratiev 		 evt->mac, wil->n_mids);
2922cd0f021SVladimir Kondratiev 	/* ignore MAC address, we already have it from the boot loader */
2932be7d22fSVladimir Kondratiev 	snprintf(wdev->wiphy->fw_version, sizeof(wdev->wiphy->fw_version),
294b8023177SVladimir Kondratiev 		 "%d", wil->fw_version);
2952be7d22fSVladimir Kondratiev }
2962be7d22fSVladimir Kondratiev 
2972be7d22fSVladimir Kondratiev static void wmi_evt_fw_ready(struct wil6210_priv *wil, int id, void *d,
2982be7d22fSVladimir Kondratiev 			     int len)
2992be7d22fSVladimir Kondratiev {
30015e23124SVladimir Kondratiev 	wil_dbg_wmi(wil, "WMI: got FW ready event\n");
3012be7d22fSVladimir Kondratiev 
302c33407a8SVladimir Kondratiev 	wil_set_recovery_state(wil, fw_recovery_idle);
3039419b6a2SVladimir Kondratiev 	set_bit(wil_status_fwready, wil->status);
30459502647SDedy Lansky 	/* let the reset sequence continue */
3052be7d22fSVladimir Kondratiev 	complete(&wil->wmi_ready);
3062be7d22fSVladimir Kondratiev }
3072be7d22fSVladimir Kondratiev 
3082be7d22fSVladimir Kondratiev static void wmi_evt_rx_mgmt(struct wil6210_priv *wil, int id, void *d, int len)
3092be7d22fSVladimir Kondratiev {
3102be7d22fSVladimir Kondratiev 	struct wmi_rx_mgmt_packet_event *data = d;
3112be7d22fSVladimir Kondratiev 	struct wiphy *wiphy = wil_to_wiphy(wil);
3122be7d22fSVladimir Kondratiev 	struct ieee80211_mgmt *rx_mgmt_frame =
3132be7d22fSVladimir Kondratiev 			(struct ieee80211_mgmt *)data->payload;
3142be7d22fSVladimir Kondratiev 	int ch_no = data->info.channel+1;
3152be7d22fSVladimir Kondratiev 	u32 freq = ieee80211_channel_to_frequency(ch_no,
3162be7d22fSVladimir Kondratiev 			IEEE80211_BAND_60GHZ);
3172be7d22fSVladimir Kondratiev 	struct ieee80211_channel *channel = ieee80211_get_channel(wiphy, freq);
318b8b33a3aSVladimir Kondratiev 	s32 signal = data->info.sqi;
3192be7d22fSVladimir Kondratiev 	__le16 fc = rx_mgmt_frame->frame_control;
3202be7d22fSVladimir Kondratiev 	u32 d_len = le32_to_cpu(data->info.len);
3212be7d22fSVladimir Kondratiev 	u16 d_status = le16_to_cpu(data->info.status);
3222be7d22fSVladimir Kondratiev 
323b8b33a3aSVladimir Kondratiev 	wil_dbg_wmi(wil, "MGMT: channel %d MCS %d SNR %d SQI %d%%\n",
324b8b33a3aSVladimir Kondratiev 		    data->info.channel, data->info.mcs, data->info.snr,
325b8b33a3aSVladimir Kondratiev 		    data->info.sqi);
326f27dbf78SVladimir Kondratiev 	wil_dbg_wmi(wil, "status 0x%04x len %d fc 0x%04x\n", d_status, d_len,
327f27dbf78SVladimir Kondratiev 		    le16_to_cpu(fc));
3287743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "qid %d mid %d cid %d\n",
3292be7d22fSVladimir Kondratiev 		    data->info.qid, data->info.mid, data->info.cid);
3302be7d22fSVladimir Kondratiev 
3312be7d22fSVladimir Kondratiev 	if (!channel) {
3322be7d22fSVladimir Kondratiev 		wil_err(wil, "Frame on unsupported channel\n");
3332be7d22fSVladimir Kondratiev 		return;
3342be7d22fSVladimir Kondratiev 	}
3352be7d22fSVladimir Kondratiev 
3362be7d22fSVladimir Kondratiev 	if (ieee80211_is_beacon(fc) || ieee80211_is_probe_resp(fc)) {
3372be7d22fSVladimir Kondratiev 		struct cfg80211_bss *bss;
3388eea944aSVladimir Kondratiev 		u64 tsf = le64_to_cpu(rx_mgmt_frame->u.beacon.timestamp);
3398eea944aSVladimir Kondratiev 		u16 cap = le16_to_cpu(rx_mgmt_frame->u.beacon.capab_info);
3408eea944aSVladimir Kondratiev 		u16 bi = le16_to_cpu(rx_mgmt_frame->u.beacon.beacon_int);
3418eea944aSVladimir Kondratiev 		const u8 *ie_buf = rx_mgmt_frame->u.beacon.variable;
3428eea944aSVladimir Kondratiev 		size_t ie_len = d_len - offsetof(struct ieee80211_mgmt,
3438eea944aSVladimir Kondratiev 						 u.beacon.variable);
3448eea944aSVladimir Kondratiev 		wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
3458eea944aSVladimir Kondratiev 		wil_dbg_wmi(wil, "TSF : 0x%016llx\n", tsf);
3468eea944aSVladimir Kondratiev 		wil_dbg_wmi(wil, "Beacon interval : %d\n", bi);
3478eea944aSVladimir Kondratiev 		wil_hex_dump_wmi("IE ", DUMP_PREFIX_OFFSET, 16, 1, ie_buf,
3488eea944aSVladimir Kondratiev 				 ie_len, true);
3492be7d22fSVladimir Kondratiev 
350a0f7845bSVladimir Kondratiev 		bss = cfg80211_inform_bss_frame(wiphy, channel, rx_mgmt_frame,
351a0f7845bSVladimir Kondratiev 						d_len, signal, GFP_KERNEL);
3522be7d22fSVladimir Kondratiev 		if (bss) {
3537743882dSVladimir Kondratiev 			wil_dbg_wmi(wil, "Added BSS %pM\n",
3542be7d22fSVladimir Kondratiev 				    rx_mgmt_frame->bssid);
3555b112d3dSJohannes Berg 			cfg80211_put_bss(wiphy, bss);
3562be7d22fSVladimir Kondratiev 		} else {
3575bc8c1f2SJohannes Berg 			wil_err(wil, "cfg80211_inform_bss_frame() failed\n");
3582be7d22fSVladimir Kondratiev 		}
359102b1d99SVladimir Kondratiev 	} else {
360102b1d99SVladimir Kondratiev 		cfg80211_rx_mgmt(wil->wdev, freq, signal,
361970fdfa8SVladimir Kondratiev 				 (void *)rx_mgmt_frame, d_len, 0);
3622be7d22fSVladimir Kondratiev 	}
3632be7d22fSVladimir Kondratiev }
3642be7d22fSVladimir Kondratiev 
3652be7d22fSVladimir Kondratiev static void wmi_evt_scan_complete(struct wil6210_priv *wil, int id,
3662be7d22fSVladimir Kondratiev 				  void *d, int len)
3672be7d22fSVladimir Kondratiev {
3682be7d22fSVladimir Kondratiev 	if (wil->scan_request) {
3692be7d22fSVladimir Kondratiev 		struct wmi_scan_complete_event *data = d;
3706c2faf09SVladimir Kondratiev 		bool aborted = (data->status != WMI_SCAN_SUCCESS);
3712be7d22fSVladimir Kondratiev 
3727743882dSVladimir Kondratiev 		wil_dbg_wmi(wil, "SCAN_COMPLETE(0x%08x)\n", data->status);
3732a91d7d0SVladimir Kondratiev 		wil_dbg_misc(wil, "Complete scan_request 0x%p aborted %d\n",
3742a91d7d0SVladimir Kondratiev 			     wil->scan_request, aborted);
3752a91d7d0SVladimir Kondratiev 
376047e5d74SVladimir Kondratiev 		del_timer_sync(&wil->scan_timer);
3772be7d22fSVladimir Kondratiev 		cfg80211_scan_done(wil->scan_request, aborted);
3782be7d22fSVladimir Kondratiev 		wil->scan_request = NULL;
3792be7d22fSVladimir Kondratiev 	} else {
3802be7d22fSVladimir Kondratiev 		wil_err(wil, "SCAN_COMPLETE while not scanning\n");
3812be7d22fSVladimir Kondratiev 	}
3822be7d22fSVladimir Kondratiev }
3832be7d22fSVladimir Kondratiev 
3842be7d22fSVladimir Kondratiev static void wmi_evt_connect(struct wil6210_priv *wil, int id, void *d, int len)
3852be7d22fSVladimir Kondratiev {
3862be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
3872be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
3882be7d22fSVladimir Kondratiev 	struct wmi_connect_event *evt = d;
3892be7d22fSVladimir Kondratiev 	int ch; /* channel number */
3902be7d22fSVladimir Kondratiev 	struct station_info sinfo;
3912be7d22fSVladimir Kondratiev 	u8 *assoc_req_ie, *assoc_resp_ie;
3922be7d22fSVladimir Kondratiev 	size_t assoc_req_ielen, assoc_resp_ielen;
3932be7d22fSVladimir Kondratiev 	/* capinfo(u16) + listen_interval(u16) + IEs */
3942be7d22fSVladimir Kondratiev 	const size_t assoc_req_ie_offset = sizeof(u16) * 2;
3952be7d22fSVladimir Kondratiev 	/* capinfo(u16) + status_code(u16) + associd(u16) + IEs */
3962be7d22fSVladimir Kondratiev 	const size_t assoc_resp_ie_offset = sizeof(u16) * 3;
3972be7d22fSVladimir Kondratiev 
3982be7d22fSVladimir Kondratiev 	if (len < sizeof(*evt)) {
3992be7d22fSVladimir Kondratiev 		wil_err(wil, "Connect event too short : %d bytes\n", len);
4002be7d22fSVladimir Kondratiev 		return;
4012be7d22fSVladimir Kondratiev 	}
4022be7d22fSVladimir Kondratiev 	if (len != sizeof(*evt) + evt->beacon_ie_len + evt->assoc_req_len +
4032be7d22fSVladimir Kondratiev 		   evt->assoc_resp_len) {
4042be7d22fSVladimir Kondratiev 		wil_err(wil,
4052be7d22fSVladimir Kondratiev 			"Connect event corrupted : %d != %d + %d + %d + %d\n",
4062be7d22fSVladimir Kondratiev 			len, (int)sizeof(*evt), evt->beacon_ie_len,
4072be7d22fSVladimir Kondratiev 			evt->assoc_req_len, evt->assoc_resp_len);
4082be7d22fSVladimir Kondratiev 		return;
4092be7d22fSVladimir Kondratiev 	}
4103df2cd36SVladimir Kondratiev 	if (evt->cid >= WIL6210_MAX_CID) {
4113df2cd36SVladimir Kondratiev 		wil_err(wil, "Connect CID invalid : %d\n", evt->cid);
4123df2cd36SVladimir Kondratiev 		return;
4133df2cd36SVladimir Kondratiev 	}
4143df2cd36SVladimir Kondratiev 
4152be7d22fSVladimir Kondratiev 	ch = evt->channel + 1;
4167743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "Connect %pM channel [%d] cid %d\n",
4172be7d22fSVladimir Kondratiev 		    evt->bssid, ch, evt->cid);
4187743882dSVladimir Kondratiev 	wil_hex_dump_wmi("connect AI : ", DUMP_PREFIX_OFFSET, 16, 1,
4192be7d22fSVladimir Kondratiev 			 evt->assoc_info, len - sizeof(*evt), true);
4202be7d22fSVladimir Kondratiev 
4212be7d22fSVladimir Kondratiev 	/* figure out IE's */
4222be7d22fSVladimir Kondratiev 	assoc_req_ie = &evt->assoc_info[evt->beacon_ie_len +
4232be7d22fSVladimir Kondratiev 					assoc_req_ie_offset];
4242be7d22fSVladimir Kondratiev 	assoc_req_ielen = evt->assoc_req_len - assoc_req_ie_offset;
4252be7d22fSVladimir Kondratiev 	if (evt->assoc_req_len <= assoc_req_ie_offset) {
4262be7d22fSVladimir Kondratiev 		assoc_req_ie = NULL;
4272be7d22fSVladimir Kondratiev 		assoc_req_ielen = 0;
4282be7d22fSVladimir Kondratiev 	}
4292be7d22fSVladimir Kondratiev 
4302be7d22fSVladimir Kondratiev 	assoc_resp_ie = &evt->assoc_info[evt->beacon_ie_len +
4312be7d22fSVladimir Kondratiev 					 evt->assoc_req_len +
4322be7d22fSVladimir Kondratiev 					 assoc_resp_ie_offset];
4332be7d22fSVladimir Kondratiev 	assoc_resp_ielen = evt->assoc_resp_len - assoc_resp_ie_offset;
4342be7d22fSVladimir Kondratiev 	if (evt->assoc_resp_len <= assoc_resp_ie_offset) {
4352be7d22fSVladimir Kondratiev 		assoc_resp_ie = NULL;
4362be7d22fSVladimir Kondratiev 		assoc_resp_ielen = 0;
4372be7d22fSVladimir Kondratiev 	}
4382be7d22fSVladimir Kondratiev 
4392be7d22fSVladimir Kondratiev 	if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
4402be7d22fSVladimir Kondratiev 	    (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
4419419b6a2SVladimir Kondratiev 		if (!test_bit(wil_status_fwconnecting, wil->status)) {
4422be7d22fSVladimir Kondratiev 			wil_err(wil, "Not in connecting state\n");
4432be7d22fSVladimir Kondratiev 			return;
4442be7d22fSVladimir Kondratiev 		}
4452be7d22fSVladimir Kondratiev 		del_timer_sync(&wil->connect_timer);
4462be7d22fSVladimir Kondratiev 		cfg80211_connect_result(ndev, evt->bssid,
4472be7d22fSVladimir Kondratiev 					assoc_req_ie, assoc_req_ielen,
4482be7d22fSVladimir Kondratiev 					assoc_resp_ie, assoc_resp_ielen,
4492be7d22fSVladimir Kondratiev 					WLAN_STATUS_SUCCESS, GFP_KERNEL);
4502be7d22fSVladimir Kondratiev 
4512be7d22fSVladimir Kondratiev 	} else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
4522be7d22fSVladimir Kondratiev 		   (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
4532be7d22fSVladimir Kondratiev 		memset(&sinfo, 0, sizeof(sinfo));
4542be7d22fSVladimir Kondratiev 
4552be7d22fSVladimir Kondratiev 		sinfo.generation = wil->sinfo_gen++;
4562be7d22fSVladimir Kondratiev 
4572be7d22fSVladimir Kondratiev 		if (assoc_req_ie) {
4582be7d22fSVladimir Kondratiev 			sinfo.assoc_req_ies = assoc_req_ie;
4592be7d22fSVladimir Kondratiev 			sinfo.assoc_req_ies_len = assoc_req_ielen;
4602be7d22fSVladimir Kondratiev 		}
4612be7d22fSVladimir Kondratiev 
4622be7d22fSVladimir Kondratiev 		cfg80211_new_sta(ndev, evt->bssid, &sinfo, GFP_KERNEL);
4632be7d22fSVladimir Kondratiev 	}
4649419b6a2SVladimir Kondratiev 	clear_bit(wil_status_fwconnecting, wil->status);
4659419b6a2SVladimir Kondratiev 	set_bit(wil_status_fwconnected, wil->status);
4662be7d22fSVladimir Kondratiev 
4672be7d22fSVladimir Kondratiev 	/* FIXME FW can transmit only ucast frames to peer */
4682be7d22fSVladimir Kondratiev 	/* FIXME real ring_id instead of hard coded 0 */
469a82553bbSVladimir Kondratiev 	ether_addr_copy(wil->sta[evt->cid].addr, evt->bssid);
4703df2cd36SVladimir Kondratiev 	wil->sta[evt->cid].status = wil_sta_conn_pending;
4712be7d22fSVladimir Kondratiev 
4722be7d22fSVladimir Kondratiev 	wil->pending_connect_cid = evt->cid;
4733277213fSVladimir Kondratiev 	queue_work(wil->wq_service, &wil->connect_worker);
4742be7d22fSVladimir Kondratiev }
4752be7d22fSVladimir Kondratiev 
4762be7d22fSVladimir Kondratiev static void wmi_evt_disconnect(struct wil6210_priv *wil, int id,
4772be7d22fSVladimir Kondratiev 			       void *d, int len)
4782be7d22fSVladimir Kondratiev {
4792be7d22fSVladimir Kondratiev 	struct wmi_disconnect_event *evt = d;
4804821e6d8SVladimir Kondratiev 	u16 reason_code = le16_to_cpu(evt->protocol_reason_status);
4812be7d22fSVladimir Kondratiev 
4824821e6d8SVladimir Kondratiev 	wil_dbg_wmi(wil, "Disconnect %pM reason [proto %d wmi %d]\n",
4834821e6d8SVladimir Kondratiev 		    evt->bssid, reason_code, evt->disconnect_reason);
4842be7d22fSVladimir Kondratiev 
4852be7d22fSVladimir Kondratiev 	wil->sinfo_gen++;
4862be7d22fSVladimir Kondratiev 
487097638a0SVladimir Kondratiev 	mutex_lock(&wil->mutex);
4884821e6d8SVladimir Kondratiev 	wil6210_disconnect(wil, evt->bssid, reason_code, true);
489097638a0SVladimir Kondratiev 	mutex_unlock(&wil->mutex);
4902be7d22fSVladimir Kondratiev }
4912be7d22fSVladimir Kondratiev 
4922be7d22fSVladimir Kondratiev /*
4932be7d22fSVladimir Kondratiev  * Firmware reports EAPOL frame using WME event.
4942be7d22fSVladimir Kondratiev  * Reconstruct Ethernet frame and deliver it via normal Rx
4952be7d22fSVladimir Kondratiev  */
4962be7d22fSVladimir Kondratiev static void wmi_evt_eapol_rx(struct wil6210_priv *wil, int id,
4972be7d22fSVladimir Kondratiev 			     void *d, int len)
4982be7d22fSVladimir Kondratiev {
4992be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
5002be7d22fSVladimir Kondratiev 	struct wmi_eapol_rx_event *evt = d;
5012be7d22fSVladimir Kondratiev 	u16 eapol_len = le16_to_cpu(evt->eapol_len);
5022be7d22fSVladimir Kondratiev 	int sz = eapol_len + ETH_HLEN;
5032be7d22fSVladimir Kondratiev 	struct sk_buff *skb;
5042be7d22fSVladimir Kondratiev 	struct ethhdr *eth;
505c8b78b5fSVladimir Kondratiev 	int cid;
506c8b78b5fSVladimir Kondratiev 	struct wil_net_stats *stats = NULL;
5072be7d22fSVladimir Kondratiev 
5087743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "EAPOL len %d from %pM\n", eapol_len,
5092be7d22fSVladimir Kondratiev 		    evt->src_mac);
5102be7d22fSVladimir Kondratiev 
511c8b78b5fSVladimir Kondratiev 	cid = wil_find_cid(wil, evt->src_mac);
512c8b78b5fSVladimir Kondratiev 	if (cid >= 0)
513c8b78b5fSVladimir Kondratiev 		stats = &wil->sta[cid].stats;
514c8b78b5fSVladimir Kondratiev 
5152be7d22fSVladimir Kondratiev 	if (eapol_len > 196) { /* TODO: revisit size limit */
5162be7d22fSVladimir Kondratiev 		wil_err(wil, "EAPOL too large\n");
5172be7d22fSVladimir Kondratiev 		return;
5182be7d22fSVladimir Kondratiev 	}
5192be7d22fSVladimir Kondratiev 
5202be7d22fSVladimir Kondratiev 	skb = alloc_skb(sz, GFP_KERNEL);
5212be7d22fSVladimir Kondratiev 	if (!skb) {
5222be7d22fSVladimir Kondratiev 		wil_err(wil, "Failed to allocate skb\n");
5232be7d22fSVladimir Kondratiev 		return;
5242be7d22fSVladimir Kondratiev 	}
525c8b78b5fSVladimir Kondratiev 
5262be7d22fSVladimir Kondratiev 	eth = (struct ethhdr *)skb_put(skb, ETH_HLEN);
527a82553bbSVladimir Kondratiev 	ether_addr_copy(eth->h_dest, ndev->dev_addr);
528a82553bbSVladimir Kondratiev 	ether_addr_copy(eth->h_source, evt->src_mac);
5292be7d22fSVladimir Kondratiev 	eth->h_proto = cpu_to_be16(ETH_P_PAE);
5302be7d22fSVladimir Kondratiev 	memcpy(skb_put(skb, eapol_len), evt->eapol, eapol_len);
5312be7d22fSVladimir Kondratiev 	skb->protocol = eth_type_trans(skb, ndev);
5322be7d22fSVladimir Kondratiev 	if (likely(netif_rx_ni(skb) == NET_RX_SUCCESS)) {
5332be7d22fSVladimir Kondratiev 		ndev->stats.rx_packets++;
534c8b78b5fSVladimir Kondratiev 		ndev->stats.rx_bytes += sz;
535c8b78b5fSVladimir Kondratiev 		if (stats) {
536c8b78b5fSVladimir Kondratiev 			stats->rx_packets++;
537c8b78b5fSVladimir Kondratiev 			stats->rx_bytes += sz;
538c8b78b5fSVladimir Kondratiev 		}
5392be7d22fSVladimir Kondratiev 	} else {
5402be7d22fSVladimir Kondratiev 		ndev->stats.rx_dropped++;
541c8b78b5fSVladimir Kondratiev 		if (stats)
542c8b78b5fSVladimir Kondratiev 			stats->rx_dropped++;
5432be7d22fSVladimir Kondratiev 	}
5442be7d22fSVladimir Kondratiev }
5452be7d22fSVladimir Kondratiev 
546230d8442SVladimir Kondratiev static void wmi_evt_vring_en(struct wil6210_priv *wil, int id, void *d, int len)
5473a124ed6SVladimir Kondratiev {
548230d8442SVladimir Kondratiev 	struct wmi_vring_en_event *evt = d;
549230d8442SVladimir Kondratiev 	u8 vri = evt->vring_index;
5503a124ed6SVladimir Kondratiev 
551230d8442SVladimir Kondratiev 	wil_dbg_wmi(wil, "Enable vring %d\n", vri);
5523a124ed6SVladimir Kondratiev 
553230d8442SVladimir Kondratiev 	if (vri >= ARRAY_SIZE(wil->vring_tx)) {
554230d8442SVladimir Kondratiev 		wil_err(wil, "Enable for invalid vring %d\n", vri);
555e58c9f70SVladimir Kondratiev 		return;
556e58c9f70SVladimir Kondratiev 	}
557230d8442SVladimir Kondratiev 	wil->vring_tx_data[vri].dot1x_open = true;
558230d8442SVladimir Kondratiev 	if (vri == wil->bcast_vring) /* no BA for bcast */
559230d8442SVladimir Kondratiev 		return;
5603a3def8dSVladimir Kondratiev 	if (agg_wsize >= 0)
561230d8442SVladimir Kondratiev 		wil_addba_tx_request(wil, vri, agg_wsize);
5623442a504SVladimir Kondratiev }
5633442a504SVladimir Kondratiev 
564249a382bSVladimir Kondratiev static void wmi_evt_ba_status(struct wil6210_priv *wil, int id, void *d,
565249a382bSVladimir Kondratiev 			      int len)
566249a382bSVladimir Kondratiev {
567249a382bSVladimir Kondratiev 	struct wmi_vring_ba_status_event *evt = d;
5683277213fSVladimir Kondratiev 	struct vring_tx_data *txdata;
569249a382bSVladimir Kondratiev 
570cbcf5866SVladimir Kondratiev 	wil_dbg_wmi(wil, "BACK[%d] %s {%d} timeout %d AMSDU%s\n",
5717b05b0abSVladimir Kondratiev 		    evt->ringid,
5727b05b0abSVladimir Kondratiev 		    evt->status == WMI_BA_AGREED ? "OK" : "N/A",
573cbcf5866SVladimir Kondratiev 		    evt->agg_wsize, __le16_to_cpu(evt->ba_timeout),
574cbcf5866SVladimir Kondratiev 		    evt->amsdu ? "+" : "-");
575b4490f42SVladimir Kondratiev 
5767b05b0abSVladimir Kondratiev 	if (evt->ringid >= WIL6210_MAX_TX_RINGS) {
5777b05b0abSVladimir Kondratiev 		wil_err(wil, "invalid ring id %d\n", evt->ringid);
5787b05b0abSVladimir Kondratiev 		return;
5797b05b0abSVladimir Kondratiev 	}
5807b05b0abSVladimir Kondratiev 
5813277213fSVladimir Kondratiev 	if (evt->status != WMI_BA_AGREED) {
5823277213fSVladimir Kondratiev 		evt->ba_timeout = 0;
5833277213fSVladimir Kondratiev 		evt->agg_wsize = 0;
584cbcf5866SVladimir Kondratiev 		evt->amsdu = 0;
5857b05b0abSVladimir Kondratiev 	}
5867b05b0abSVladimir Kondratiev 
5873277213fSVladimir Kondratiev 	txdata = &wil->vring_tx_data[evt->ringid];
5883277213fSVladimir Kondratiev 
5893277213fSVladimir Kondratiev 	txdata->agg_timeout = le16_to_cpu(evt->ba_timeout);
5903277213fSVladimir Kondratiev 	txdata->agg_wsize = evt->agg_wsize;
591cbcf5866SVladimir Kondratiev 	txdata->agg_amsdu = evt->amsdu;
5923a124ed6SVladimir Kondratiev 	txdata->addba_in_progress = false;
5937b05b0abSVladimir Kondratiev }
5947b05b0abSVladimir Kondratiev 
5953277213fSVladimir Kondratiev static void wmi_evt_addba_rx_req(struct wil6210_priv *wil, int id, void *d,
5963277213fSVladimir Kondratiev 				 int len)
5973277213fSVladimir Kondratiev {
5983277213fSVladimir Kondratiev 	struct wmi_rcp_addba_req_event *evt = d;
5993277213fSVladimir Kondratiev 
6003277213fSVladimir Kondratiev 	wil_addba_rx_request(wil, evt->cidxtid, evt->dialog_token,
6013277213fSVladimir Kondratiev 			     evt->ba_param_set, evt->ba_timeout,
6023277213fSVladimir Kondratiev 			     evt->ba_seq_ctrl);
6033277213fSVladimir Kondratiev }
6043277213fSVladimir Kondratiev 
6053277213fSVladimir Kondratiev static void wmi_evt_delba(struct wil6210_priv *wil, int id, void *d, int len)
606bd33273bSVladimir Kondratiev __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
6073277213fSVladimir Kondratiev {
6083277213fSVladimir Kondratiev 	struct wmi_delba_event *evt = d;
6093277213fSVladimir Kondratiev 	u8 cid, tid;
6103277213fSVladimir Kondratiev 	u16 reason = __le16_to_cpu(evt->reason);
6113277213fSVladimir Kondratiev 	struct wil_sta_info *sta;
612ec81b5adSDedy Lansky 	struct wil_tid_ampdu_rx *r;
613ec81b5adSDedy Lansky 
614bd33273bSVladimir Kondratiev 	might_sleep();
6153277213fSVladimir Kondratiev 	parse_cidxtid(evt->cidxtid, &cid, &tid);
6163277213fSVladimir Kondratiev 	wil_dbg_wmi(wil, "DELBA CID %d TID %d from %s reason %d\n",
6173277213fSVladimir Kondratiev 		    cid, tid,
6183277213fSVladimir Kondratiev 		    evt->from_initiator ? "originator" : "recipient",
6193277213fSVladimir Kondratiev 		    reason);
6203277213fSVladimir Kondratiev 	if (!evt->from_initiator) {
6213277213fSVladimir Kondratiev 		int i;
6223277213fSVladimir Kondratiev 		/* find Tx vring it belongs to */
6233277213fSVladimir Kondratiev 		for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
6243277213fSVladimir Kondratiev 			if ((wil->vring2cid_tid[i][0] == cid) &&
6253277213fSVladimir Kondratiev 			    (wil->vring2cid_tid[i][1] == tid)) {
6263277213fSVladimir Kondratiev 				struct vring_tx_data *txdata =
6273277213fSVladimir Kondratiev 					&wil->vring_tx_data[i];
6283277213fSVladimir Kondratiev 
6293277213fSVladimir Kondratiev 				wil_dbg_wmi(wil, "DELBA Tx vring %d\n", i);
6303277213fSVladimir Kondratiev 				txdata->agg_timeout = 0;
6313277213fSVladimir Kondratiev 				txdata->agg_wsize = 0;
6323a124ed6SVladimir Kondratiev 				txdata->addba_in_progress = false;
6333277213fSVladimir Kondratiev 
6343277213fSVladimir Kondratiev 				break; /* max. 1 matching ring */
6353277213fSVladimir Kondratiev 			}
6363277213fSVladimir Kondratiev 		}
6373277213fSVladimir Kondratiev 		if (i >= ARRAY_SIZE(wil->vring2cid_tid))
6383277213fSVladimir Kondratiev 			wil_err(wil, "DELBA: unable to find Tx vring\n");
6393277213fSVladimir Kondratiev 		return;
6403277213fSVladimir Kondratiev 	}
6413277213fSVladimir Kondratiev 
6423277213fSVladimir Kondratiev 	sta = &wil->sta[cid];
6433277213fSVladimir Kondratiev 
644bd33273bSVladimir Kondratiev 	spin_lock_bh(&sta->tid_rx_lock);
645ec81b5adSDedy Lansky 
6463277213fSVladimir Kondratiev 	r = sta->tid_rx[tid];
6473277213fSVladimir Kondratiev 	sta->tid_rx[tid] = NULL;
648b4490f42SVladimir Kondratiev 	wil_tid_ampdu_rx_free(wil, r);
649ec81b5adSDedy Lansky 
650bd33273bSVladimir Kondratiev 	spin_unlock_bh(&sta->tid_rx_lock);
651b4490f42SVladimir Kondratiev }
652b4490f42SVladimir Kondratiev 
6532be7d22fSVladimir Kondratiev static const struct {
6542be7d22fSVladimir Kondratiev 	int eventid;
6552be7d22fSVladimir Kondratiev 	void (*handler)(struct wil6210_priv *wil, int eventid,
6562be7d22fSVladimir Kondratiev 			void *data, int data_len);
6572be7d22fSVladimir Kondratiev } wmi_evt_handlers[] = {
6582be7d22fSVladimir Kondratiev 	{WMI_READY_EVENTID,		wmi_evt_ready},
6592be7d22fSVladimir Kondratiev 	{WMI_FW_READY_EVENTID,		wmi_evt_fw_ready},
6602be7d22fSVladimir Kondratiev 	{WMI_RX_MGMT_PACKET_EVENTID,	wmi_evt_rx_mgmt},
6612be7d22fSVladimir Kondratiev 	{WMI_SCAN_COMPLETE_EVENTID,	wmi_evt_scan_complete},
6622be7d22fSVladimir Kondratiev 	{WMI_CONNECT_EVENTID,		wmi_evt_connect},
6632be7d22fSVladimir Kondratiev 	{WMI_DISCONNECT_EVENTID,	wmi_evt_disconnect},
6642be7d22fSVladimir Kondratiev 	{WMI_EAPOL_RX_EVENTID,		wmi_evt_eapol_rx},
665249a382bSVladimir Kondratiev 	{WMI_BA_STATUS_EVENTID,		wmi_evt_ba_status},
6663277213fSVladimir Kondratiev 	{WMI_RCP_ADDBA_REQ_EVENTID,	wmi_evt_addba_rx_req},
6673277213fSVladimir Kondratiev 	{WMI_DELBA_EVENTID,		wmi_evt_delba},
668230d8442SVladimir Kondratiev 	{WMI_VRING_EN_EVENTID,		wmi_evt_vring_en},
6692be7d22fSVladimir Kondratiev };
6702be7d22fSVladimir Kondratiev 
6712be7d22fSVladimir Kondratiev /*
6722be7d22fSVladimir Kondratiev  * Run in IRQ context
6732be7d22fSVladimir Kondratiev  * Extract WMI command from mailbox. Queue it to the @wil->pending_wmi_ev
6742be7d22fSVladimir Kondratiev  * that will be eventually handled by the @wmi_event_worker in the thread
6752be7d22fSVladimir Kondratiev  * context of thread "wil6210_wmi"
6762be7d22fSVladimir Kondratiev  */
6772be7d22fSVladimir Kondratiev void wmi_recv_cmd(struct wil6210_priv *wil)
6782be7d22fSVladimir Kondratiev {
6792be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring_desc d_tail;
6802be7d22fSVladimir Kondratiev 	struct wil6210_mbox_hdr hdr;
6812be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
6822be7d22fSVladimir Kondratiev 	struct pending_wmi_event *evt;
6832be7d22fSVladimir Kondratiev 	u8 *cmd;
6842be7d22fSVladimir Kondratiev 	void __iomem *src;
6852be7d22fSVladimir Kondratiev 	ulong flags;
686a715c7ddSVladimir Kondratiev 	unsigned n;
6872be7d22fSVladimir Kondratiev 
6889419b6a2SVladimir Kondratiev 	if (!test_bit(wil_status_reset_done, wil->status)) {
6894cf99c93SDedy Lansky 		wil_err(wil, "Reset in progress. Cannot handle WMI event\n");
69055f7acddSVladimir Kondratiev 		return;
69155f7acddSVladimir Kondratiev 	}
69255f7acddSVladimir Kondratiev 
693a715c7ddSVladimir Kondratiev 	for (n = 0;; n++) {
6942be7d22fSVladimir Kondratiev 		u16 len;
69595266dc0SVladimir Kondratiev 		bool q;
6962be7d22fSVladimir Kondratiev 
6972be7d22fSVladimir Kondratiev 		r->head = ioread32(wil->csr + HOST_MBOX +
6982be7d22fSVladimir Kondratiev 				   offsetof(struct wil6210_mbox_ctl, rx.head));
69995266dc0SVladimir Kondratiev 		if (r->tail == r->head)
70095266dc0SVladimir Kondratiev 			break;
7012be7d22fSVladimir Kondratiev 
702a715c7ddSVladimir Kondratiev 		wil_dbg_wmi(wil, "Mbox head %08x tail %08x\n",
703a715c7ddSVladimir Kondratiev 			    r->head, r->tail);
704a715c7ddSVladimir Kondratiev 		/* read cmd descriptor from tail */
7052be7d22fSVladimir Kondratiev 		wil_memcpy_fromio_32(&d_tail, wil->csr + HOSTADDR(r->tail),
7062be7d22fSVladimir Kondratiev 				     sizeof(struct wil6210_mbox_ring_desc));
7072be7d22fSVladimir Kondratiev 		if (d_tail.sync == 0) {
7082be7d22fSVladimir Kondratiev 			wil_err(wil, "Mbox evt not owned by FW?\n");
70995266dc0SVladimir Kondratiev 			break;
7102be7d22fSVladimir Kondratiev 		}
7112be7d22fSVladimir Kondratiev 
712a715c7ddSVladimir Kondratiev 		/* read cmd header from descriptor */
7132be7d22fSVladimir Kondratiev 		if (0 != wmi_read_hdr(wil, d_tail.addr, &hdr)) {
7142be7d22fSVladimir Kondratiev 			wil_err(wil, "Mbox evt at 0x%08x?\n",
7152be7d22fSVladimir Kondratiev 				le32_to_cpu(d_tail.addr));
71695266dc0SVladimir Kondratiev 			break;
7172be7d22fSVladimir Kondratiev 		}
7182be7d22fSVladimir Kondratiev 		len = le16_to_cpu(hdr.len);
719a715c7ddSVladimir Kondratiev 		wil_dbg_wmi(wil, "Mbox evt %04x %04x %04x %02x\n",
720a715c7ddSVladimir Kondratiev 			    le16_to_cpu(hdr.seq), len, le16_to_cpu(hdr.type),
721a715c7ddSVladimir Kondratiev 			    hdr.flags);
722a715c7ddSVladimir Kondratiev 
723a715c7ddSVladimir Kondratiev 		/* read cmd buffer from descriptor */
7242be7d22fSVladimir Kondratiev 		src = wmi_buffer(wil, d_tail.addr) +
7252be7d22fSVladimir Kondratiev 		      sizeof(struct wil6210_mbox_hdr);
7262be7d22fSVladimir Kondratiev 		evt = kmalloc(ALIGN(offsetof(struct pending_wmi_event,
7272be7d22fSVladimir Kondratiev 					     event.wmi) + len, 4),
7282be7d22fSVladimir Kondratiev 			      GFP_KERNEL);
72914f8dc49SJoe Perches 		if (!evt)
73095266dc0SVladimir Kondratiev 			break;
73114f8dc49SJoe Perches 
7322be7d22fSVladimir Kondratiev 		evt->event.hdr = hdr;
7332be7d22fSVladimir Kondratiev 		cmd = (void *)&evt->event.wmi;
7342be7d22fSVladimir Kondratiev 		wil_memcpy_fromio_32(cmd, src, len);
7352be7d22fSVladimir Kondratiev 		/* mark entry as empty */
7362be7d22fSVladimir Kondratiev 		iowrite32(0, wil->csr + HOSTADDR(r->tail) +
7372be7d22fSVladimir Kondratiev 			  offsetof(struct wil6210_mbox_ring_desc, sync));
7382be7d22fSVladimir Kondratiev 		/* indicate */
7392be7d22fSVladimir Kondratiev 		if ((hdr.type == WIL_MBOX_HDR_TYPE_WMI) &&
7402be7d22fSVladimir Kondratiev 		    (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
741f988b23fSVladimir Kondratiev 			struct wil6210_mbox_hdr_wmi *wmi = &evt->event.wmi;
742f988b23fSVladimir Kondratiev 			u16 id = le16_to_cpu(wmi->id);
743f988b23fSVladimir Kondratiev 			u32 tstamp = le32_to_cpu(wmi->timestamp);
7448fe59627SVladimir Kondratiev 
745f988b23fSVladimir Kondratiev 			wil_dbg_wmi(wil, "WMI event 0x%04x MID %d @%d msec\n",
746f988b23fSVladimir Kondratiev 				    id, wmi->mid, tstamp);
747f988b23fSVladimir Kondratiev 			trace_wil6210_wmi_event(wmi, &wmi[1],
748f988b23fSVladimir Kondratiev 						len - sizeof(*wmi));
7492be7d22fSVladimir Kondratiev 		}
7507743882dSVladimir Kondratiev 		wil_hex_dump_wmi("evt ", DUMP_PREFIX_OFFSET, 16, 1,
7512be7d22fSVladimir Kondratiev 				 &evt->event.hdr, sizeof(hdr) + len, true);
7522be7d22fSVladimir Kondratiev 
7532be7d22fSVladimir Kondratiev 		/* advance tail */
7542be7d22fSVladimir Kondratiev 		r->tail = r->base + ((r->tail - r->base +
7552be7d22fSVladimir Kondratiev 			  sizeof(struct wil6210_mbox_ring_desc)) % r->size);
7562be7d22fSVladimir Kondratiev 		iowrite32(r->tail, wil->csr + HOST_MBOX +
7572be7d22fSVladimir Kondratiev 			  offsetof(struct wil6210_mbox_ctl, rx.tail));
7582be7d22fSVladimir Kondratiev 
7592be7d22fSVladimir Kondratiev 		/* add to the pending list */
7602be7d22fSVladimir Kondratiev 		spin_lock_irqsave(&wil->wmi_ev_lock, flags);
7612be7d22fSVladimir Kondratiev 		list_add_tail(&evt->list, &wil->pending_wmi_ev);
7622be7d22fSVladimir Kondratiev 		spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
76395266dc0SVladimir Kondratiev 		q = queue_work(wil->wmi_wq, &wil->wmi_event_worker);
7647743882dSVladimir Kondratiev 		wil_dbg_wmi(wil, "queue_work -> %d\n", q);
7652be7d22fSVladimir Kondratiev 	}
76695266dc0SVladimir Kondratiev 	/* normally, 1 event per IRQ should be processed */
76795266dc0SVladimir Kondratiev 	wil_dbg_wmi(wil, "%s -> %d events queued\n", __func__, n);
7682be7d22fSVladimir Kondratiev }
7692be7d22fSVladimir Kondratiev 
7702be7d22fSVladimir Kondratiev int wmi_call(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len,
7712be7d22fSVladimir Kondratiev 	     u16 reply_id, void *reply, u8 reply_size, int to_msec)
7722be7d22fSVladimir Kondratiev {
7732be7d22fSVladimir Kondratiev 	int rc;
7742be7d22fSVladimir Kondratiev 	int remain;
7752be7d22fSVladimir Kondratiev 
7762be7d22fSVladimir Kondratiev 	mutex_lock(&wil->wmi_mutex);
7772be7d22fSVladimir Kondratiev 
7782be7d22fSVladimir Kondratiev 	rc = __wmi_send(wil, cmdid, buf, len);
7792be7d22fSVladimir Kondratiev 	if (rc)
7802be7d22fSVladimir Kondratiev 		goto out;
7812be7d22fSVladimir Kondratiev 
7822be7d22fSVladimir Kondratiev 	wil->reply_id = reply_id;
7832be7d22fSVladimir Kondratiev 	wil->reply_buf = reply;
7842be7d22fSVladimir Kondratiev 	wil->reply_size = reply_size;
78559502647SDedy Lansky 	remain = wait_for_completion_timeout(&wil->wmi_call,
7862be7d22fSVladimir Kondratiev 					     msecs_to_jiffies(to_msec));
7872be7d22fSVladimir Kondratiev 	if (0 == remain) {
7882be7d22fSVladimir Kondratiev 		wil_err(wil, "wmi_call(0x%04x->0x%04x) timeout %d msec\n",
7892be7d22fSVladimir Kondratiev 			cmdid, reply_id, to_msec);
7902be7d22fSVladimir Kondratiev 		rc = -ETIME;
7912be7d22fSVladimir Kondratiev 	} else {
7927743882dSVladimir Kondratiev 		wil_dbg_wmi(wil,
7932be7d22fSVladimir Kondratiev 			    "wmi_call(0x%04x->0x%04x) completed in %d msec\n",
7942be7d22fSVladimir Kondratiev 			    cmdid, reply_id,
7952be7d22fSVladimir Kondratiev 			    to_msec - jiffies_to_msecs(remain));
7962be7d22fSVladimir Kondratiev 	}
7972be7d22fSVladimir Kondratiev 	wil->reply_id = 0;
7982be7d22fSVladimir Kondratiev 	wil->reply_buf = NULL;
7992be7d22fSVladimir Kondratiev 	wil->reply_size = 0;
8002be7d22fSVladimir Kondratiev  out:
8012be7d22fSVladimir Kondratiev 	mutex_unlock(&wil->wmi_mutex);
8022be7d22fSVladimir Kondratiev 
8032be7d22fSVladimir Kondratiev 	return rc;
8042be7d22fSVladimir Kondratiev }
8052be7d22fSVladimir Kondratiev 
8062be7d22fSVladimir Kondratiev int wmi_echo(struct wil6210_priv *wil)
8072be7d22fSVladimir Kondratiev {
8082be7d22fSVladimir Kondratiev 	struct wmi_echo_cmd cmd = {
8092be7d22fSVladimir Kondratiev 		.value = cpu_to_le32(0x12345678),
8102be7d22fSVladimir Kondratiev 	};
8112be7d22fSVladimir Kondratiev 
8122be7d22fSVladimir Kondratiev 	return wmi_call(wil, WMI_ECHO_CMDID, &cmd, sizeof(cmd),
813a54a40daSVladimir Kondratiev 			WMI_ECHO_RSP_EVENTID, NULL, 0, 50);
8142be7d22fSVladimir Kondratiev }
8152be7d22fSVladimir Kondratiev 
8162be7d22fSVladimir Kondratiev int wmi_set_mac_address(struct wil6210_priv *wil, void *addr)
8172be7d22fSVladimir Kondratiev {
8182be7d22fSVladimir Kondratiev 	struct wmi_set_mac_address_cmd cmd;
8192be7d22fSVladimir Kondratiev 
820a82553bbSVladimir Kondratiev 	ether_addr_copy(cmd.mac, addr);
8212be7d22fSVladimir Kondratiev 
8227743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "Set MAC %pM\n", addr);
8232be7d22fSVladimir Kondratiev 
8242be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_SET_MAC_ADDRESS_CMDID, &cmd, sizeof(cmd));
8252be7d22fSVladimir Kondratiev }
8262be7d22fSVladimir Kondratiev 
827b8023177SVladimir Kondratiev int wmi_pcp_start(struct wil6210_priv *wil, int bi, u8 wmi_nettype, u8 chan)
8282be7d22fSVladimir Kondratiev {
829b8023177SVladimir Kondratiev 	int rc;
830b8023177SVladimir Kondratiev 
831b8023177SVladimir Kondratiev 	struct wmi_pcp_start_cmd cmd = {
8322be7d22fSVladimir Kondratiev 		.bcon_interval = cpu_to_le16(bi),
8332be7d22fSVladimir Kondratiev 		.network_type = wmi_nettype,
8342be7d22fSVladimir Kondratiev 		.disable_sec_offload = 1,
835adc2d122SVladimir Kondratiev 		.channel = chan - 1,
8361eb9d1e5SDedy Lansky 		.pcp_max_assoc_sta = max_assoc_sta,
8372be7d22fSVladimir Kondratiev 	};
838b8023177SVladimir Kondratiev 	struct {
839b8023177SVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
840b8023177SVladimir Kondratiev 		struct wmi_pcp_started_event evt;
841b8023177SVladimir Kondratiev 	} __packed reply;
8422be7d22fSVladimir Kondratiev 
843774974e5SVladimir Kondratiev 	if (!wil->privacy)
8442be7d22fSVladimir Kondratiev 		cmd.disable_sec = 1;
8452be7d22fSVladimir Kondratiev 
8461eb9d1e5SDedy Lansky 	if ((cmd.pcp_max_assoc_sta > WIL6210_MAX_CID) ||
8471eb9d1e5SDedy Lansky 	    (cmd.pcp_max_assoc_sta <= 0)) {
8481eb9d1e5SDedy Lansky 		wil_info(wil,
8491eb9d1e5SDedy Lansky 			 "Requested connection limit %u, valid values are 1 - %d. Setting to %d\n",
8501eb9d1e5SDedy Lansky 			 max_assoc_sta, WIL6210_MAX_CID, WIL6210_MAX_CID);
8511eb9d1e5SDedy Lansky 		cmd.pcp_max_assoc_sta = WIL6210_MAX_CID;
8521eb9d1e5SDedy Lansky 	}
8531eb9d1e5SDedy Lansky 
8548b5c7f6cSVladimir Kondratiev 	/*
8558b5c7f6cSVladimir Kondratiev 	 * Processing time may be huge, in case of secure AP it takes about
8568b5c7f6cSVladimir Kondratiev 	 * 3500ms for FW to start AP
8578b5c7f6cSVladimir Kondratiev 	 */
858b8023177SVladimir Kondratiev 	rc = wmi_call(wil, WMI_PCP_START_CMDID, &cmd, sizeof(cmd),
8598b5c7f6cSVladimir Kondratiev 		      WMI_PCP_STARTED_EVENTID, &reply, sizeof(reply), 5000);
860b8023177SVladimir Kondratiev 	if (rc)
861b8023177SVladimir Kondratiev 		return rc;
862b8023177SVladimir Kondratiev 
863b8023177SVladimir Kondratiev 	if (reply.evt.status != WMI_FW_STATUS_SUCCESS)
864b8023177SVladimir Kondratiev 		rc = -EINVAL;
865b8023177SVladimir Kondratiev 
866b8023177SVladimir Kondratiev 	return rc;
867b8023177SVladimir Kondratiev }
868b8023177SVladimir Kondratiev 
869b8023177SVladimir Kondratiev int wmi_pcp_stop(struct wil6210_priv *wil)
870b8023177SVladimir Kondratiev {
871b8023177SVladimir Kondratiev 	return wmi_call(wil, WMI_PCP_STOP_CMDID, NULL, 0,
872b8023177SVladimir Kondratiev 			WMI_PCP_STOPPED_EVENTID, NULL, 0, 20);
8732be7d22fSVladimir Kondratiev }
8742be7d22fSVladimir Kondratiev 
8752be7d22fSVladimir Kondratiev int wmi_set_ssid(struct wil6210_priv *wil, u8 ssid_len, const void *ssid)
8762be7d22fSVladimir Kondratiev {
8772be7d22fSVladimir Kondratiev 	struct wmi_set_ssid_cmd cmd = {
8782be7d22fSVladimir Kondratiev 		.ssid_len = cpu_to_le32(ssid_len),
8792be7d22fSVladimir Kondratiev 	};
8802be7d22fSVladimir Kondratiev 
8812be7d22fSVladimir Kondratiev 	if (ssid_len > sizeof(cmd.ssid))
8822be7d22fSVladimir Kondratiev 		return -EINVAL;
8832be7d22fSVladimir Kondratiev 
8842be7d22fSVladimir Kondratiev 	memcpy(cmd.ssid, ssid, ssid_len);
8852be7d22fSVladimir Kondratiev 
8862be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_SET_SSID_CMDID, &cmd, sizeof(cmd));
8872be7d22fSVladimir Kondratiev }
8882be7d22fSVladimir Kondratiev 
8892be7d22fSVladimir Kondratiev int wmi_get_ssid(struct wil6210_priv *wil, u8 *ssid_len, void *ssid)
8902be7d22fSVladimir Kondratiev {
8912be7d22fSVladimir Kondratiev 	int rc;
8922be7d22fSVladimir Kondratiev 	struct {
8932be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
8942be7d22fSVladimir Kondratiev 		struct wmi_set_ssid_cmd cmd;
8952be7d22fSVladimir Kondratiev 	} __packed reply;
8962be7d22fSVladimir Kondratiev 	int len; /* reply.cmd.ssid_len in CPU order */
8972be7d22fSVladimir Kondratiev 
8982be7d22fSVladimir Kondratiev 	rc = wmi_call(wil, WMI_GET_SSID_CMDID, NULL, 0, WMI_GET_SSID_EVENTID,
8992be7d22fSVladimir Kondratiev 		      &reply, sizeof(reply), 20);
9002be7d22fSVladimir Kondratiev 	if (rc)
9012be7d22fSVladimir Kondratiev 		return rc;
9022be7d22fSVladimir Kondratiev 
9032be7d22fSVladimir Kondratiev 	len = le32_to_cpu(reply.cmd.ssid_len);
9042be7d22fSVladimir Kondratiev 	if (len > sizeof(reply.cmd.ssid))
9052be7d22fSVladimir Kondratiev 		return -EINVAL;
9062be7d22fSVladimir Kondratiev 
9072be7d22fSVladimir Kondratiev 	*ssid_len = len;
9082be7d22fSVladimir Kondratiev 	memcpy(ssid, reply.cmd.ssid, len);
9092be7d22fSVladimir Kondratiev 
9102be7d22fSVladimir Kondratiev 	return 0;
9112be7d22fSVladimir Kondratiev }
9122be7d22fSVladimir Kondratiev 
9132be7d22fSVladimir Kondratiev int wmi_set_channel(struct wil6210_priv *wil, int channel)
9142be7d22fSVladimir Kondratiev {
9152be7d22fSVladimir Kondratiev 	struct wmi_set_pcp_channel_cmd cmd = {
9162be7d22fSVladimir Kondratiev 		.channel = channel - 1,
9172be7d22fSVladimir Kondratiev 	};
9182be7d22fSVladimir Kondratiev 
9192be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_SET_PCP_CHANNEL_CMDID, &cmd, sizeof(cmd));
9202be7d22fSVladimir Kondratiev }
9212be7d22fSVladimir Kondratiev 
9222be7d22fSVladimir Kondratiev int wmi_get_channel(struct wil6210_priv *wil, int *channel)
9232be7d22fSVladimir Kondratiev {
9242be7d22fSVladimir Kondratiev 	int rc;
9252be7d22fSVladimir Kondratiev 	struct {
9262be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
9272be7d22fSVladimir Kondratiev 		struct wmi_set_pcp_channel_cmd cmd;
9282be7d22fSVladimir Kondratiev 	} __packed reply;
9292be7d22fSVladimir Kondratiev 
9302be7d22fSVladimir Kondratiev 	rc = wmi_call(wil, WMI_GET_PCP_CHANNEL_CMDID, NULL, 0,
9312be7d22fSVladimir Kondratiev 		      WMI_GET_PCP_CHANNEL_EVENTID, &reply, sizeof(reply), 20);
9322be7d22fSVladimir Kondratiev 	if (rc)
9332be7d22fSVladimir Kondratiev 		return rc;
9342be7d22fSVladimir Kondratiev 
9352be7d22fSVladimir Kondratiev 	if (reply.cmd.channel > 3)
9362be7d22fSVladimir Kondratiev 		return -EINVAL;
9372be7d22fSVladimir Kondratiev 
9382be7d22fSVladimir Kondratiev 	*channel = reply.cmd.channel + 1;
9392be7d22fSVladimir Kondratiev 
9402be7d22fSVladimir Kondratiev 	return 0;
9412be7d22fSVladimir Kondratiev }
9422be7d22fSVladimir Kondratiev 
943b8023177SVladimir Kondratiev int wmi_p2p_cfg(struct wil6210_priv *wil, int channel)
944b8023177SVladimir Kondratiev {
945b8023177SVladimir Kondratiev 	struct wmi_p2p_cfg_cmd cmd = {
946b8023177SVladimir Kondratiev 		.discovery_mode = WMI_DISCOVERY_MODE_NON_OFFLOAD,
947b8023177SVladimir Kondratiev 		.channel = channel - 1,
948b8023177SVladimir Kondratiev 	};
949b8023177SVladimir Kondratiev 
950b8023177SVladimir Kondratiev 	return wmi_send(wil, WMI_P2P_CFG_CMDID, &cmd, sizeof(cmd));
951b8023177SVladimir Kondratiev }
952b8023177SVladimir Kondratiev 
9532be7d22fSVladimir Kondratiev int wmi_del_cipher_key(struct wil6210_priv *wil, u8 key_index,
954230d8442SVladimir Kondratiev 		       const void *mac_addr, int key_usage)
9552be7d22fSVladimir Kondratiev {
9562be7d22fSVladimir Kondratiev 	struct wmi_delete_cipher_key_cmd cmd = {
9572be7d22fSVladimir Kondratiev 		.key_index = key_index,
9582be7d22fSVladimir Kondratiev 	};
9592be7d22fSVladimir Kondratiev 
9602be7d22fSVladimir Kondratiev 	if (mac_addr)
9612be7d22fSVladimir Kondratiev 		memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
9622be7d22fSVladimir Kondratiev 
9632be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_DELETE_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
9642be7d22fSVladimir Kondratiev }
9652be7d22fSVladimir Kondratiev 
9662be7d22fSVladimir Kondratiev int wmi_add_cipher_key(struct wil6210_priv *wil, u8 key_index,
967230d8442SVladimir Kondratiev 		       const void *mac_addr, int key_len, const void *key,
968230d8442SVladimir Kondratiev 		       int key_usage)
9692be7d22fSVladimir Kondratiev {
9702be7d22fSVladimir Kondratiev 	struct wmi_add_cipher_key_cmd cmd = {
9712be7d22fSVladimir Kondratiev 		.key_index = key_index,
972230d8442SVladimir Kondratiev 		.key_usage = key_usage,
9732be7d22fSVladimir Kondratiev 		.key_len = key_len,
9742be7d22fSVladimir Kondratiev 	};
9752be7d22fSVladimir Kondratiev 
9762be7d22fSVladimir Kondratiev 	if (!key || (key_len > sizeof(cmd.key)))
9772be7d22fSVladimir Kondratiev 		return -EINVAL;
9782be7d22fSVladimir Kondratiev 
9792be7d22fSVladimir Kondratiev 	memcpy(cmd.key, key, key_len);
9802be7d22fSVladimir Kondratiev 	if (mac_addr)
9812be7d22fSVladimir Kondratiev 		memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
9822be7d22fSVladimir Kondratiev 
9832be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_ADD_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
9842be7d22fSVladimir Kondratiev }
9852be7d22fSVladimir Kondratiev 
9862be7d22fSVladimir Kondratiev int wmi_set_ie(struct wil6210_priv *wil, u8 type, u16 ie_len, const void *ie)
9872be7d22fSVladimir Kondratiev {
9882be7d22fSVladimir Kondratiev 	int rc;
9892be7d22fSVladimir Kondratiev 	u16 len = sizeof(struct wmi_set_appie_cmd) + ie_len;
9902be7d22fSVladimir Kondratiev 	struct wmi_set_appie_cmd *cmd = kzalloc(len, GFP_KERNEL);
9918fe59627SVladimir Kondratiev 
99214f8dc49SJoe Perches 	if (!cmd)
9932be7d22fSVladimir Kondratiev 		return -ENOMEM;
994ac4acdb7SVladimir Kondratiev 	if (!ie)
995ac4acdb7SVladimir Kondratiev 		ie_len = 0;
9962be7d22fSVladimir Kondratiev 
9972be7d22fSVladimir Kondratiev 	cmd->mgmt_frm_type = type;
9982be7d22fSVladimir Kondratiev 	/* BUG: FW API define ieLen as u8. Will fix FW */
9992be7d22fSVladimir Kondratiev 	cmd->ie_len = cpu_to_le16(ie_len);
10002be7d22fSVladimir Kondratiev 	memcpy(cmd->ie_info, ie, ie_len);
100103866e7dSVladimir Kondratiev 	rc = wmi_send(wil, WMI_SET_APPIE_CMDID, cmd, len);
10022be7d22fSVladimir Kondratiev 	kfree(cmd);
10032be7d22fSVladimir Kondratiev 
10042be7d22fSVladimir Kondratiev 	return rc;
10052be7d22fSVladimir Kondratiev }
10062be7d22fSVladimir Kondratiev 
10071647f12fSVladimir Kondratiev /**
10081647f12fSVladimir Kondratiev  * wmi_rxon - turn radio on/off
10091647f12fSVladimir Kondratiev  * @on:		turn on if true, off otherwise
10101647f12fSVladimir Kondratiev  *
10111647f12fSVladimir Kondratiev  * Only switch radio. Channel should be set separately.
10121647f12fSVladimir Kondratiev  * No timeout for rxon - radio turned on forever unless some other call
10131647f12fSVladimir Kondratiev  * turns it off
10141647f12fSVladimir Kondratiev  */
10151647f12fSVladimir Kondratiev int wmi_rxon(struct wil6210_priv *wil, bool on)
10161647f12fSVladimir Kondratiev {
10171647f12fSVladimir Kondratiev 	int rc;
10181647f12fSVladimir Kondratiev 	struct {
10191647f12fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
10201647f12fSVladimir Kondratiev 		struct wmi_listen_started_event evt;
10211647f12fSVladimir Kondratiev 	} __packed reply;
10221647f12fSVladimir Kondratiev 
10231647f12fSVladimir Kondratiev 	wil_info(wil, "%s(%s)\n", __func__, on ? "on" : "off");
10241647f12fSVladimir Kondratiev 
10251647f12fSVladimir Kondratiev 	if (on) {
10261647f12fSVladimir Kondratiev 		rc = wmi_call(wil, WMI_START_LISTEN_CMDID, NULL, 0,
10271647f12fSVladimir Kondratiev 			      WMI_LISTEN_STARTED_EVENTID,
10281647f12fSVladimir Kondratiev 			      &reply, sizeof(reply), 100);
10291647f12fSVladimir Kondratiev 		if ((rc == 0) && (reply.evt.status != WMI_FW_STATUS_SUCCESS))
10301647f12fSVladimir Kondratiev 			rc = -EINVAL;
10311647f12fSVladimir Kondratiev 	} else {
10321647f12fSVladimir Kondratiev 		rc = wmi_call(wil, WMI_DISCOVERY_STOP_CMDID, NULL, 0,
10331647f12fSVladimir Kondratiev 			      WMI_DISCOVERY_STOPPED_EVENTID, NULL, 0, 20);
10341647f12fSVladimir Kondratiev 	}
10351647f12fSVladimir Kondratiev 
10361647f12fSVladimir Kondratiev 	return rc;
10371647f12fSVladimir Kondratiev }
10381647f12fSVladimir Kondratiev 
103947e19af9SVladimir Kondratiev int wmi_rx_chain_add(struct wil6210_priv *wil, struct vring *vring)
104047e19af9SVladimir Kondratiev {
104147e19af9SVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
104247e19af9SVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
104347e19af9SVladimir Kondratiev 	struct wmi_cfg_rx_chain_cmd cmd = {
104447e19af9SVladimir Kondratiev 		.action = WMI_RX_CHAIN_ADD,
104547e19af9SVladimir Kondratiev 		.rx_sw_ring = {
1046c44690a1SVladimir Kondratiev 			.max_mpdu_size = cpu_to_le16(wil_mtu2macbuf(mtu_max)),
104747e19af9SVladimir Kondratiev 			.ring_mem_base = cpu_to_le64(vring->pa),
104847e19af9SVladimir Kondratiev 			.ring_size = cpu_to_le16(vring->size),
104947e19af9SVladimir Kondratiev 		},
105047e19af9SVladimir Kondratiev 		.mid = 0, /* TODO - what is it? */
105147e19af9SVladimir Kondratiev 		.decap_trans_type = WMI_DECAP_TYPE_802_3,
1052b4490f42SVladimir Kondratiev 		.reorder_type = WMI_RX_SW_REORDER,
1053ab954628SVladimir Kondratiev 		.host_thrsh = cpu_to_le16(rx_ring_overflow_thrsh),
105447e19af9SVladimir Kondratiev 	};
105547e19af9SVladimir Kondratiev 	struct {
105647e19af9SVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
105747e19af9SVladimir Kondratiev 		struct wmi_cfg_rx_chain_done_event evt;
105847e19af9SVladimir Kondratiev 	} __packed evt;
105947e19af9SVladimir Kondratiev 	int rc;
106047e19af9SVladimir Kondratiev 
106147e19af9SVladimir Kondratiev 	if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
106247e19af9SVladimir Kondratiev 		struct ieee80211_channel *ch = wdev->preset_chandef.chan;
106347e19af9SVladimir Kondratiev 
106447e19af9SVladimir Kondratiev 		cmd.sniffer_cfg.mode = cpu_to_le32(WMI_SNIFFER_ON);
106547e19af9SVladimir Kondratiev 		if (ch)
106647e19af9SVladimir Kondratiev 			cmd.sniffer_cfg.channel = ch->hw_value - 1;
106747e19af9SVladimir Kondratiev 		cmd.sniffer_cfg.phy_info_mode =
106847e19af9SVladimir Kondratiev 			cpu_to_le32(ndev->type == ARPHRD_IEEE80211_RADIOTAP);
106947e19af9SVladimir Kondratiev 		cmd.sniffer_cfg.phy_support =
107047e19af9SVladimir Kondratiev 			cpu_to_le32((wil->monitor_flags & MONITOR_FLAG_CONTROL)
107147e19af9SVladimir Kondratiev 				    ? WMI_SNIFFER_CP : WMI_SNIFFER_DP);
1072504937d4SKirshenbaum Erez 	} else {
1073504937d4SKirshenbaum Erez 		/* Initialize offload (in non-sniffer mode).
1074504937d4SKirshenbaum Erez 		 * Linux IP stack always calculates IP checksum
1075504937d4SKirshenbaum Erez 		 * HW always calculate TCP/UDP checksum
1076504937d4SKirshenbaum Erez 		 */
1077504937d4SKirshenbaum Erez 		cmd.l3_l4_ctrl |= (1 << L3_L4_CTRL_TCPIP_CHECKSUM_EN_POS);
107847e19af9SVladimir Kondratiev 	}
1079c406ea7cSVladimir Kondratiev 
1080c406ea7cSVladimir Kondratiev 	if (rx_align_2)
1081c406ea7cSVladimir Kondratiev 		cmd.l2_802_3_offload_ctrl |=
1082c406ea7cSVladimir Kondratiev 				L2_802_3_OFFLOAD_CTRL_SNAP_KEEP_MSK;
1083c406ea7cSVladimir Kondratiev 
108447e19af9SVladimir Kondratiev 	/* typical time for secure PCP is 840ms */
108547e19af9SVladimir Kondratiev 	rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, &cmd, sizeof(cmd),
108647e19af9SVladimir Kondratiev 		      WMI_CFG_RX_CHAIN_DONE_EVENTID, &evt, sizeof(evt), 2000);
108747e19af9SVladimir Kondratiev 	if (rc)
108847e19af9SVladimir Kondratiev 		return rc;
108947e19af9SVladimir Kondratiev 
109047e19af9SVladimir Kondratiev 	vring->hwtail = le32_to_cpu(evt.evt.rx_ring_tail_ptr);
109147e19af9SVladimir Kondratiev 
10927743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "Rx init: status %d tail 0x%08x\n",
109347e19af9SVladimir Kondratiev 		     le32_to_cpu(evt.evt.status), vring->hwtail);
109447e19af9SVladimir Kondratiev 
109547e19af9SVladimir Kondratiev 	if (le32_to_cpu(evt.evt.status) != WMI_CFG_RX_CHAIN_SUCCESS)
109647e19af9SVladimir Kondratiev 		rc = -EINVAL;
109747e19af9SVladimir Kondratiev 
109847e19af9SVladimir Kondratiev 	return rc;
109947e19af9SVladimir Kondratiev }
110047e19af9SVladimir Kondratiev 
11018c679675SVladimir Kondratiev int wmi_get_temperature(struct wil6210_priv *wil, u32 *t_bb, u32 *t_rf)
11021a2780e0SVladimir Kondratiev {
11031a2780e0SVladimir Kondratiev 	int rc;
11041a2780e0SVladimir Kondratiev 	struct wmi_temp_sense_cmd cmd = {
11058c679675SVladimir Kondratiev 		.measure_baseband_en = cpu_to_le32(!!t_bb),
11068c679675SVladimir Kondratiev 		.measure_rf_en = cpu_to_le32(!!t_rf),
11078c679675SVladimir Kondratiev 		.measure_mode = cpu_to_le32(TEMPERATURE_MEASURE_NOW),
11081a2780e0SVladimir Kondratiev 	};
11091a2780e0SVladimir Kondratiev 	struct {
11101a2780e0SVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
11111a2780e0SVladimir Kondratiev 		struct wmi_temp_sense_done_event evt;
11121a2780e0SVladimir Kondratiev 	} __packed reply;
11131a2780e0SVladimir Kondratiev 
11141a2780e0SVladimir Kondratiev 	rc = wmi_call(wil, WMI_TEMP_SENSE_CMDID, &cmd, sizeof(cmd),
11151a2780e0SVladimir Kondratiev 		      WMI_TEMP_SENSE_DONE_EVENTID, &reply, sizeof(reply), 100);
11161a2780e0SVladimir Kondratiev 	if (rc)
11171a2780e0SVladimir Kondratiev 		return rc;
11181a2780e0SVladimir Kondratiev 
11198c679675SVladimir Kondratiev 	if (t_bb)
11208c679675SVladimir Kondratiev 		*t_bb = le32_to_cpu(reply.evt.baseband_t1000);
11218c679675SVladimir Kondratiev 	if (t_rf)
11228c679675SVladimir Kondratiev 		*t_rf = le32_to_cpu(reply.evt.rf_t1000);
11231a2780e0SVladimir Kondratiev 
11241a2780e0SVladimir Kondratiev 	return 0;
11251a2780e0SVladimir Kondratiev }
11261a2780e0SVladimir Kondratiev 
11274d55a0a1SVladimir Kondratiev int wmi_disconnect_sta(struct wil6210_priv *wil, const u8 *mac, u16 reason)
11284d55a0a1SVladimir Kondratiev {
11294d55a0a1SVladimir Kondratiev 	struct wmi_disconnect_sta_cmd cmd = {
11304d55a0a1SVladimir Kondratiev 		.disconnect_reason = cpu_to_le16(reason),
11314d55a0a1SVladimir Kondratiev 	};
1132a82553bbSVladimir Kondratiev 
1133a82553bbSVladimir Kondratiev 	ether_addr_copy(cmd.dst_mac, mac);
11344d55a0a1SVladimir Kondratiev 
11354d55a0a1SVladimir Kondratiev 	wil_dbg_wmi(wil, "%s(%pM, reason %d)\n", __func__, mac, reason);
11364d55a0a1SVladimir Kondratiev 
11374d55a0a1SVladimir Kondratiev 	return wmi_send(wil, WMI_DISCONNECT_STA_CMDID, &cmd, sizeof(cmd));
11384d55a0a1SVladimir Kondratiev }
11394d55a0a1SVladimir Kondratiev 
11403277213fSVladimir Kondratiev int wmi_addba(struct wil6210_priv *wil, u8 ringid, u8 size, u16 timeout)
11413277213fSVladimir Kondratiev {
11423277213fSVladimir Kondratiev 	struct wmi_vring_ba_en_cmd cmd = {
11433277213fSVladimir Kondratiev 		.ringid = ringid,
11443277213fSVladimir Kondratiev 		.agg_max_wsize = size,
11453277213fSVladimir Kondratiev 		.ba_timeout = cpu_to_le16(timeout),
1146cbcf5866SVladimir Kondratiev 		.amsdu = 0,
11473277213fSVladimir Kondratiev 	};
11483277213fSVladimir Kondratiev 
11493277213fSVladimir Kondratiev 	wil_dbg_wmi(wil, "%s(ring %d size %d timeout %d)\n", __func__,
11503277213fSVladimir Kondratiev 		    ringid, size, timeout);
11513277213fSVladimir Kondratiev 
11523277213fSVladimir Kondratiev 	return wmi_send(wil, WMI_VRING_BA_EN_CMDID, &cmd, sizeof(cmd));
11533277213fSVladimir Kondratiev }
11543277213fSVladimir Kondratiev 
115526a359d9SVladimir Kondratiev int wmi_delba_tx(struct wil6210_priv *wil, u8 ringid, u16 reason)
11563277213fSVladimir Kondratiev {
11573277213fSVladimir Kondratiev 	struct wmi_vring_ba_dis_cmd cmd = {
11583277213fSVladimir Kondratiev 		.ringid = ringid,
11593277213fSVladimir Kondratiev 		.reason = cpu_to_le16(reason),
11603277213fSVladimir Kondratiev 	};
11613277213fSVladimir Kondratiev 
11623277213fSVladimir Kondratiev 	wil_dbg_wmi(wil, "%s(ring %d reason %d)\n", __func__,
11633277213fSVladimir Kondratiev 		    ringid, reason);
11643277213fSVladimir Kondratiev 
11653277213fSVladimir Kondratiev 	return wmi_send(wil, WMI_VRING_BA_DIS_CMDID, &cmd, sizeof(cmd));
11663277213fSVladimir Kondratiev }
11673277213fSVladimir Kondratiev 
116826a359d9SVladimir Kondratiev int wmi_delba_rx(struct wil6210_priv *wil, u8 cidxtid, u16 reason)
116926a359d9SVladimir Kondratiev {
117026a359d9SVladimir Kondratiev 	struct wmi_rcp_delba_cmd cmd = {
117126a359d9SVladimir Kondratiev 		.cidxtid = cidxtid,
117226a359d9SVladimir Kondratiev 		.reason = cpu_to_le16(reason),
117326a359d9SVladimir Kondratiev 	};
117426a359d9SVladimir Kondratiev 
117526a359d9SVladimir Kondratiev 	wil_dbg_wmi(wil, "%s(CID %d TID %d reason %d)\n", __func__,
117626a359d9SVladimir Kondratiev 		    cidxtid & 0xf, (cidxtid >> 4) & 0xf, reason);
117726a359d9SVladimir Kondratiev 
117826a359d9SVladimir Kondratiev 	return wmi_send(wil, WMI_RCP_DELBA_CMDID, &cmd, sizeof(cmd));
117926a359d9SVladimir Kondratiev }
118026a359d9SVladimir Kondratiev 
11813277213fSVladimir Kondratiev int wmi_addba_rx_resp(struct wil6210_priv *wil, u8 cid, u8 tid, u8 token,
11823277213fSVladimir Kondratiev 		      u16 status, bool amsdu, u16 agg_wsize, u16 timeout)
11833277213fSVladimir Kondratiev {
11843277213fSVladimir Kondratiev 	int rc;
11853277213fSVladimir Kondratiev 	struct wmi_rcp_addba_resp_cmd cmd = {
11863277213fSVladimir Kondratiev 		.cidxtid = mk_cidxtid(cid, tid),
11873277213fSVladimir Kondratiev 		.dialog_token = token,
11883277213fSVladimir Kondratiev 		.status_code = cpu_to_le16(status),
11893277213fSVladimir Kondratiev 		/* bit 0: A-MSDU supported
11903277213fSVladimir Kondratiev 		 * bit 1: policy (should be 0 for us)
11913277213fSVladimir Kondratiev 		 * bits 2..5: TID
11923277213fSVladimir Kondratiev 		 * bits 6..15: buffer size
11933277213fSVladimir Kondratiev 		 */
11943277213fSVladimir Kondratiev 		.ba_param_set = cpu_to_le16((amsdu ? 1 : 0) | (tid << 2) |
11953277213fSVladimir Kondratiev 					    (agg_wsize << 6)),
11963277213fSVladimir Kondratiev 		.ba_timeout = cpu_to_le16(timeout),
11973277213fSVladimir Kondratiev 	};
11983277213fSVladimir Kondratiev 	struct {
11993277213fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
12003277213fSVladimir Kondratiev 		struct wmi_rcp_addba_resp_sent_event evt;
12013277213fSVladimir Kondratiev 	} __packed reply;
12023277213fSVladimir Kondratiev 
12033277213fSVladimir Kondratiev 	wil_dbg_wmi(wil,
12043277213fSVladimir Kondratiev 		    "ADDBA response for CID %d TID %d size %d timeout %d status %d AMSDU%s\n",
12053277213fSVladimir Kondratiev 		    cid, tid, agg_wsize, timeout, status, amsdu ? "+" : "-");
12063277213fSVladimir Kondratiev 
12073277213fSVladimir Kondratiev 	rc = wmi_call(wil, WMI_RCP_ADDBA_RESP_CMDID, &cmd, sizeof(cmd),
1208230d8442SVladimir Kondratiev 		      WMI_RCP_ADDBA_RESP_SENT_EVENTID, &reply, sizeof(reply),
1209230d8442SVladimir Kondratiev 		      100);
12103277213fSVladimir Kondratiev 	if (rc)
12113277213fSVladimir Kondratiev 		return rc;
12123277213fSVladimir Kondratiev 
12133277213fSVladimir Kondratiev 	if (reply.evt.status) {
12143277213fSVladimir Kondratiev 		wil_err(wil, "ADDBA response failed with status %d\n",
12153277213fSVladimir Kondratiev 			le16_to_cpu(reply.evt.status));
12163277213fSVladimir Kondratiev 		rc = -EINVAL;
12173277213fSVladimir Kondratiev 	}
12183277213fSVladimir Kondratiev 
12193277213fSVladimir Kondratiev 	return rc;
12203277213fSVladimir Kondratiev }
12213277213fSVladimir Kondratiev 
12222be7d22fSVladimir Kondratiev void wmi_event_flush(struct wil6210_priv *wil)
12232be7d22fSVladimir Kondratiev {
12242be7d22fSVladimir Kondratiev 	struct pending_wmi_event *evt, *t;
12252be7d22fSVladimir Kondratiev 
12267743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "%s()\n", __func__);
12272be7d22fSVladimir Kondratiev 
12282be7d22fSVladimir Kondratiev 	list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
12292be7d22fSVladimir Kondratiev 		list_del(&evt->list);
12302be7d22fSVladimir Kondratiev 		kfree(evt);
12312be7d22fSVladimir Kondratiev 	}
12322be7d22fSVladimir Kondratiev }
12332be7d22fSVladimir Kondratiev 
12342be7d22fSVladimir Kondratiev static bool wmi_evt_call_handler(struct wil6210_priv *wil, int id,
12352be7d22fSVladimir Kondratiev 				 void *d, int len)
12362be7d22fSVladimir Kondratiev {
12372be7d22fSVladimir Kondratiev 	uint i;
12382be7d22fSVladimir Kondratiev 
12392be7d22fSVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
12402be7d22fSVladimir Kondratiev 		if (wmi_evt_handlers[i].eventid == id) {
12412be7d22fSVladimir Kondratiev 			wmi_evt_handlers[i].handler(wil, id, d, len);
12422be7d22fSVladimir Kondratiev 			return true;
12432be7d22fSVladimir Kondratiev 		}
12442be7d22fSVladimir Kondratiev 	}
12452be7d22fSVladimir Kondratiev 
12462be7d22fSVladimir Kondratiev 	return false;
12472be7d22fSVladimir Kondratiev }
12482be7d22fSVladimir Kondratiev 
12492be7d22fSVladimir Kondratiev static void wmi_event_handle(struct wil6210_priv *wil,
12502be7d22fSVladimir Kondratiev 			     struct wil6210_mbox_hdr *hdr)
12512be7d22fSVladimir Kondratiev {
12522be7d22fSVladimir Kondratiev 	u16 len = le16_to_cpu(hdr->len);
12532be7d22fSVladimir Kondratiev 
12542be7d22fSVladimir Kondratiev 	if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
12552be7d22fSVladimir Kondratiev 	    (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
12562be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi *wmi = (void *)(&hdr[1]);
12572be7d22fSVladimir Kondratiev 		void *evt_data = (void *)(&wmi[1]);
12582be7d22fSVladimir Kondratiev 		u16 id = le16_to_cpu(wmi->id);
1259028e1836SVladimir Kondratiev 
1260028e1836SVladimir Kondratiev 		wil_dbg_wmi(wil, "Handle WMI 0x%04x (reply_id 0x%04x)\n",
1261028e1836SVladimir Kondratiev 			    id, wil->reply_id);
12622be7d22fSVladimir Kondratiev 		/* check if someone waits for this event */
12632be7d22fSVladimir Kondratiev 		if (wil->reply_id && wil->reply_id == id) {
12642be7d22fSVladimir Kondratiev 			if (wil->reply_buf) {
12652be7d22fSVladimir Kondratiev 				memcpy(wil->reply_buf, wmi,
12662be7d22fSVladimir Kondratiev 				       min(len, wil->reply_size));
12672be7d22fSVladimir Kondratiev 			} else {
12682be7d22fSVladimir Kondratiev 				wmi_evt_call_handler(wil, id, evt_data,
12692be7d22fSVladimir Kondratiev 						     len - sizeof(*wmi));
12702be7d22fSVladimir Kondratiev 			}
12717743882dSVladimir Kondratiev 			wil_dbg_wmi(wil, "Complete WMI 0x%04x\n", id);
127259502647SDedy Lansky 			complete(&wil->wmi_call);
12732be7d22fSVladimir Kondratiev 			return;
12742be7d22fSVladimir Kondratiev 		}
12752be7d22fSVladimir Kondratiev 		/* unsolicited event */
12762be7d22fSVladimir Kondratiev 		/* search for handler */
12772be7d22fSVladimir Kondratiev 		if (!wmi_evt_call_handler(wil, id, evt_data,
12782be7d22fSVladimir Kondratiev 					  len - sizeof(*wmi))) {
12792be7d22fSVladimir Kondratiev 			wil_err(wil, "Unhandled event 0x%04x\n", id);
12802be7d22fSVladimir Kondratiev 		}
12812be7d22fSVladimir Kondratiev 	} else {
12822be7d22fSVladimir Kondratiev 		wil_err(wil, "Unknown event type\n");
12832be7d22fSVladimir Kondratiev 		print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
12842be7d22fSVladimir Kondratiev 			       hdr, sizeof(*hdr) + len, true);
12852be7d22fSVladimir Kondratiev 	}
12862be7d22fSVladimir Kondratiev }
12872be7d22fSVladimir Kondratiev 
12882be7d22fSVladimir Kondratiev /*
12892be7d22fSVladimir Kondratiev  * Retrieve next WMI event from the pending list
12902be7d22fSVladimir Kondratiev  */
12912be7d22fSVladimir Kondratiev static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
12922be7d22fSVladimir Kondratiev {
12932be7d22fSVladimir Kondratiev 	ulong flags;
12942be7d22fSVladimir Kondratiev 	struct list_head *ret = NULL;
12952be7d22fSVladimir Kondratiev 
12962be7d22fSVladimir Kondratiev 	spin_lock_irqsave(&wil->wmi_ev_lock, flags);
12972be7d22fSVladimir Kondratiev 
12982be7d22fSVladimir Kondratiev 	if (!list_empty(&wil->pending_wmi_ev)) {
12992be7d22fSVladimir Kondratiev 		ret = wil->pending_wmi_ev.next;
13002be7d22fSVladimir Kondratiev 		list_del(ret);
13012be7d22fSVladimir Kondratiev 	}
13022be7d22fSVladimir Kondratiev 
13032be7d22fSVladimir Kondratiev 	spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
13042be7d22fSVladimir Kondratiev 
13052be7d22fSVladimir Kondratiev 	return ret;
13062be7d22fSVladimir Kondratiev }
13072be7d22fSVladimir Kondratiev 
13082be7d22fSVladimir Kondratiev /*
13092be7d22fSVladimir Kondratiev  * Handler for the WMI events
13102be7d22fSVladimir Kondratiev  */
13112be7d22fSVladimir Kondratiev void wmi_event_worker(struct work_struct *work)
13122be7d22fSVladimir Kondratiev {
13132be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
13142be7d22fSVladimir Kondratiev 						 wmi_event_worker);
13152be7d22fSVladimir Kondratiev 	struct pending_wmi_event *evt;
13162be7d22fSVladimir Kondratiev 	struct list_head *lh;
13172be7d22fSVladimir Kondratiev 
1318028e1836SVladimir Kondratiev 	wil_dbg_wmi(wil, "Start %s\n", __func__);
13192be7d22fSVladimir Kondratiev 	while ((lh = next_wmi_ev(wil)) != NULL) {
13202be7d22fSVladimir Kondratiev 		evt = list_entry(lh, struct pending_wmi_event, list);
13212be7d22fSVladimir Kondratiev 		wmi_event_handle(wil, &evt->event.hdr);
13222be7d22fSVladimir Kondratiev 		kfree(evt);
13232be7d22fSVladimir Kondratiev 	}
1324028e1836SVladimir Kondratiev 	wil_dbg_wmi(wil, "Finished %s\n", __func__);
13252be7d22fSVladimir Kondratiev }
1326