12be7d22fSVladimir Kondratiev /*
20916d9f2SMaya Erez  * Copyright (c) 2012-2016 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 */
880fd37ff8SVladimir Kondratiev 	{0x88c000, 0x88c200, 0x88c000, "mac_rgf_ext"}, /* mac_ext_rgf  512b */
89b541d0a0SVladimir Kondratiev 	{0x8c0000, 0x949000, 0x8c0000, "upper"},   /* upper area       548k */
902be7d22fSVladimir Kondratiev 	/*
912be7d22fSVladimir Kondratiev 	 * 920000..930000 ucode code RAM
922be7d22fSVladimir Kondratiev 	 * 930000..932000 ucode data RAM
93af6b48dbSVladimir Kondratiev 	 * 932000..949000 back-door debug data
942be7d22fSVladimir Kondratiev 	 */
952be7d22fSVladimir Kondratiev };
962be7d22fSVladimir Kondratiev 
972be7d22fSVladimir Kondratiev /**
982be7d22fSVladimir Kondratiev  * return AHB address for given firmware/ucode internal (linker) address
992be7d22fSVladimir Kondratiev  * @x - internal address
1002be7d22fSVladimir Kondratiev  * If address have no valid AHB mapping, return 0
1012be7d22fSVladimir Kondratiev  */
1022be7d22fSVladimir Kondratiev static u32 wmi_addr_remap(u32 x)
1032be7d22fSVladimir Kondratiev {
1042be7d22fSVladimir Kondratiev 	uint i;
1052be7d22fSVladimir Kondratiev 
1062be7d22fSVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
1072be7d22fSVladimir Kondratiev 		if ((x >= fw_mapping[i].from) && (x < fw_mapping[i].to))
1082be7d22fSVladimir Kondratiev 			return x + fw_mapping[i].host - fw_mapping[i].from;
1092be7d22fSVladimir Kondratiev 	}
1102be7d22fSVladimir Kondratiev 
1112be7d22fSVladimir Kondratiev 	return 0;
1122be7d22fSVladimir Kondratiev }
1132be7d22fSVladimir Kondratiev 
1142be7d22fSVladimir Kondratiev /**
1152be7d22fSVladimir Kondratiev  * Check address validity for WMI buffer; remap if needed
1162be7d22fSVladimir Kondratiev  * @ptr - internal (linker) fw/ucode address
1172be7d22fSVladimir Kondratiev  *
1182be7d22fSVladimir Kondratiev  * Valid buffer should be DWORD aligned
1192be7d22fSVladimir Kondratiev  *
1202be7d22fSVladimir Kondratiev  * return address for accessing buffer from the host;
1212be7d22fSVladimir Kondratiev  * if buffer is not valid, return NULL.
1222be7d22fSVladimir Kondratiev  */
1232be7d22fSVladimir Kondratiev void __iomem *wmi_buffer(struct wil6210_priv *wil, __le32 ptr_)
1242be7d22fSVladimir Kondratiev {
1252be7d22fSVladimir Kondratiev 	u32 off;
1262be7d22fSVladimir Kondratiev 	u32 ptr = le32_to_cpu(ptr_);
1272be7d22fSVladimir Kondratiev 
1282be7d22fSVladimir Kondratiev 	if (ptr % 4)
1292be7d22fSVladimir Kondratiev 		return NULL;
1302be7d22fSVladimir Kondratiev 
1312be7d22fSVladimir Kondratiev 	ptr = wmi_addr_remap(ptr);
1322be7d22fSVladimir Kondratiev 	if (ptr < WIL6210_FW_HOST_OFF)
1332be7d22fSVladimir Kondratiev 		return NULL;
1342be7d22fSVladimir Kondratiev 
1352be7d22fSVladimir Kondratiev 	off = HOSTADDR(ptr);
1362be7d22fSVladimir Kondratiev 	if (off > WIL6210_MEM_SIZE - 4)
1372be7d22fSVladimir Kondratiev 		return NULL;
1382be7d22fSVladimir Kondratiev 
1392be7d22fSVladimir Kondratiev 	return wil->csr + off;
1402be7d22fSVladimir Kondratiev }
1412be7d22fSVladimir Kondratiev 
1422be7d22fSVladimir Kondratiev /**
1432be7d22fSVladimir Kondratiev  * Check address validity
1442be7d22fSVladimir Kondratiev  */
1452be7d22fSVladimir Kondratiev void __iomem *wmi_addr(struct wil6210_priv *wil, u32 ptr)
1462be7d22fSVladimir Kondratiev {
1472be7d22fSVladimir Kondratiev 	u32 off;
1482be7d22fSVladimir Kondratiev 
1492be7d22fSVladimir Kondratiev 	if (ptr % 4)
1502be7d22fSVladimir Kondratiev 		return NULL;
1512be7d22fSVladimir Kondratiev 
1522be7d22fSVladimir Kondratiev 	if (ptr < WIL6210_FW_HOST_OFF)
1532be7d22fSVladimir Kondratiev 		return NULL;
1542be7d22fSVladimir Kondratiev 
1552be7d22fSVladimir Kondratiev 	off = HOSTADDR(ptr);
1562be7d22fSVladimir Kondratiev 	if (off > WIL6210_MEM_SIZE - 4)
1572be7d22fSVladimir Kondratiev 		return NULL;
1582be7d22fSVladimir Kondratiev 
1592be7d22fSVladimir Kondratiev 	return wil->csr + off;
1602be7d22fSVladimir Kondratiev }
1612be7d22fSVladimir Kondratiev 
1622be7d22fSVladimir Kondratiev int wmi_read_hdr(struct wil6210_priv *wil, __le32 ptr,
1632be7d22fSVladimir Kondratiev 		 struct wil6210_mbox_hdr *hdr)
1642be7d22fSVladimir Kondratiev {
1652be7d22fSVladimir Kondratiev 	void __iomem *src = wmi_buffer(wil, ptr);
1668fe59627SVladimir Kondratiev 
1672be7d22fSVladimir Kondratiev 	if (!src)
1682be7d22fSVladimir Kondratiev 		return -EINVAL;
1692be7d22fSVladimir Kondratiev 
1702be7d22fSVladimir Kondratiev 	wil_memcpy_fromio_32(hdr, src, sizeof(*hdr));
1712be7d22fSVladimir Kondratiev 
1722be7d22fSVladimir Kondratiev 	return 0;
1732be7d22fSVladimir Kondratiev }
1742be7d22fSVladimir Kondratiev 
1752be7d22fSVladimir Kondratiev static int __wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
1762be7d22fSVladimir Kondratiev {
1772be7d22fSVladimir Kondratiev 	struct {
1782be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr hdr;
1792be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
1802be7d22fSVladimir Kondratiev 	} __packed cmd = {
1812be7d22fSVladimir Kondratiev 		.hdr = {
1822be7d22fSVladimir Kondratiev 			.type = WIL_MBOX_HDR_TYPE_WMI,
1832be7d22fSVladimir Kondratiev 			.flags = 0,
1842be7d22fSVladimir Kondratiev 			.len = cpu_to_le16(sizeof(cmd.wmi) + len),
1852be7d22fSVladimir Kondratiev 		},
1862be7d22fSVladimir Kondratiev 		.wmi = {
187f988b23fSVladimir Kondratiev 			.mid = 0,
1882be7d22fSVladimir Kondratiev 			.id = cpu_to_le16(cmdid),
1892be7d22fSVladimir Kondratiev 		},
1902be7d22fSVladimir Kondratiev 	};
1912be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring *r = &wil->mbox_ctl.tx;
1922be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring_desc d_head;
1932be7d22fSVladimir Kondratiev 	u32 next_head;
1942be7d22fSVladimir Kondratiev 	void __iomem *dst;
1952be7d22fSVladimir Kondratiev 	void __iomem *head = wmi_addr(wil, r->head);
1962be7d22fSVladimir Kondratiev 	uint retry;
1972be7d22fSVladimir Kondratiev 
1982be7d22fSVladimir Kondratiev 	if (sizeof(cmd) + len > r->entry_size) {
1992be7d22fSVladimir Kondratiev 		wil_err(wil, "WMI size too large: %d bytes, max is %d\n",
2002be7d22fSVladimir Kondratiev 			(int)(sizeof(cmd) + len), r->entry_size);
2012be7d22fSVladimir Kondratiev 		return -ERANGE;
2022be7d22fSVladimir Kondratiev 	}
2032be7d22fSVladimir Kondratiev 
2042be7d22fSVladimir Kondratiev 	might_sleep();
2052be7d22fSVladimir Kondratiev 
2069419b6a2SVladimir Kondratiev 	if (!test_bit(wil_status_fwready, wil->status)) {
20715e23124SVladimir Kondratiev 		wil_err(wil, "WMI: cannot send command while FW not ready\n");
2082be7d22fSVladimir Kondratiev 		return -EAGAIN;
2092be7d22fSVladimir Kondratiev 	}
2102be7d22fSVladimir Kondratiev 
2112be7d22fSVladimir Kondratiev 	if (!head) {
2122be7d22fSVladimir Kondratiev 		wil_err(wil, "WMI head is garbage: 0x%08x\n", r->head);
2132be7d22fSVladimir Kondratiev 		return -EINVAL;
2142be7d22fSVladimir Kondratiev 	}
2152be7d22fSVladimir Kondratiev 	/* read Tx head till it is not busy */
2162be7d22fSVladimir Kondratiev 	for (retry = 5; retry > 0; retry--) {
2172be7d22fSVladimir Kondratiev 		wil_memcpy_fromio_32(&d_head, head, sizeof(d_head));
2182be7d22fSVladimir Kondratiev 		if (d_head.sync == 0)
2192be7d22fSVladimir Kondratiev 			break;
2202be7d22fSVladimir Kondratiev 		msleep(20);
2212be7d22fSVladimir Kondratiev 	}
2222be7d22fSVladimir Kondratiev 	if (d_head.sync != 0) {
2232be7d22fSVladimir Kondratiev 		wil_err(wil, "WMI head busy\n");
2242be7d22fSVladimir Kondratiev 		return -EBUSY;
2252be7d22fSVladimir Kondratiev 	}
2262be7d22fSVladimir Kondratiev 	/* next head */
2272be7d22fSVladimir Kondratiev 	next_head = r->base + ((r->head - r->base + sizeof(d_head)) % r->size);
2287743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "Head 0x%08x -> 0x%08x\n", r->head, next_head);
2292be7d22fSVladimir Kondratiev 	/* wait till FW finish with previous command */
2302be7d22fSVladimir Kondratiev 	for (retry = 5; retry > 0; retry--) {
231452133a7SMaya Erez 		if (!test_bit(wil_status_fwready, wil->status)) {
232452133a7SMaya Erez 			wil_err(wil, "WMI: cannot send command while FW not ready\n");
233452133a7SMaya Erez 			return -EAGAIN;
234452133a7SMaya Erez 		}
235b9eeb512SVladimir Kondratiev 		r->tail = wil_r(wil, RGF_MBOX +
2362be7d22fSVladimir Kondratiev 				offsetof(struct wil6210_mbox_ctl, tx.tail));
2372be7d22fSVladimir Kondratiev 		if (next_head != r->tail)
2382be7d22fSVladimir Kondratiev 			break;
2392be7d22fSVladimir Kondratiev 		msleep(20);
2402be7d22fSVladimir Kondratiev 	}
2412be7d22fSVladimir Kondratiev 	if (next_head == r->tail) {
2422be7d22fSVladimir Kondratiev 		wil_err(wil, "WMI ring full\n");
2432be7d22fSVladimir Kondratiev 		return -EBUSY;
2442be7d22fSVladimir Kondratiev 	}
2452be7d22fSVladimir Kondratiev 	dst = wmi_buffer(wil, d_head.addr);
2462be7d22fSVladimir Kondratiev 	if (!dst) {
2472be7d22fSVladimir Kondratiev 		wil_err(wil, "invalid WMI buffer: 0x%08x\n",
2482be7d22fSVladimir Kondratiev 			le32_to_cpu(d_head.addr));
2492be7d22fSVladimir Kondratiev 		return -EINVAL;
2502be7d22fSVladimir Kondratiev 	}
2512be7d22fSVladimir Kondratiev 	cmd.hdr.seq = cpu_to_le16(++wil->wmi_seq);
2522be7d22fSVladimir Kondratiev 	/* set command */
2537743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "WMI command 0x%04x [%d]\n", cmdid, len);
2547743882dSVladimir Kondratiev 	wil_hex_dump_wmi("Cmd ", DUMP_PREFIX_OFFSET, 16, 1, &cmd,
2552be7d22fSVladimir Kondratiev 			 sizeof(cmd), true);
2567743882dSVladimir Kondratiev 	wil_hex_dump_wmi("cmd ", DUMP_PREFIX_OFFSET, 16, 1, buf,
2572be7d22fSVladimir Kondratiev 			 len, true);
2582be7d22fSVladimir Kondratiev 	wil_memcpy_toio_32(dst, &cmd, sizeof(cmd));
2592be7d22fSVladimir Kondratiev 	wil_memcpy_toio_32(dst + sizeof(cmd), buf, len);
2602be7d22fSVladimir Kondratiev 	/* mark entry as full */
261b9eeb512SVladimir Kondratiev 	wil_w(wil, r->head + offsetof(struct wil6210_mbox_ring_desc, sync), 1);
2622be7d22fSVladimir Kondratiev 	/* advance next ptr */
263b9eeb512SVladimir Kondratiev 	wil_w(wil, RGF_MBOX + offsetof(struct wil6210_mbox_ctl, tx.head),
264b9eeb512SVladimir Kondratiev 	      r->head = next_head);
2652be7d22fSVladimir Kondratiev 
266f988b23fSVladimir Kondratiev 	trace_wil6210_wmi_cmd(&cmd.wmi, buf, len);
26798658095SVladimir Kondratiev 
2682be7d22fSVladimir Kondratiev 	/* interrupt to FW */
269b9eeb512SVladimir Kondratiev 	wil_w(wil, RGF_USER_USER_ICR + offsetof(struct RGF_ICR, ICS),
270b9eeb512SVladimir Kondratiev 	      SW_INT_MBOX);
2712be7d22fSVladimir Kondratiev 
2722be7d22fSVladimir Kondratiev 	return 0;
2732be7d22fSVladimir Kondratiev }
2742be7d22fSVladimir Kondratiev 
2752be7d22fSVladimir Kondratiev int wmi_send(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len)
2762be7d22fSVladimir Kondratiev {
2772be7d22fSVladimir Kondratiev 	int rc;
2782be7d22fSVladimir Kondratiev 
2792be7d22fSVladimir Kondratiev 	mutex_lock(&wil->wmi_mutex);
2802be7d22fSVladimir Kondratiev 	rc = __wmi_send(wil, cmdid, buf, len);
2812be7d22fSVladimir Kondratiev 	mutex_unlock(&wil->wmi_mutex);
2822be7d22fSVladimir Kondratiev 
2832be7d22fSVladimir Kondratiev 	return rc;
2842be7d22fSVladimir Kondratiev }
2852be7d22fSVladimir Kondratiev 
2862be7d22fSVladimir Kondratiev /*=== Event handlers ===*/
2872be7d22fSVladimir Kondratiev static void wmi_evt_ready(struct wil6210_priv *wil, int id, void *d, int len)
2882be7d22fSVladimir Kondratiev {
2892be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
2902be7d22fSVladimir Kondratiev 	struct wmi_ready_event *evt = d;
2918fe59627SVladimir Kondratiev 
292b8023177SVladimir Kondratiev 	wil->fw_version = le32_to_cpu(evt->sw_version);
293b8023177SVladimir Kondratiev 	wil->n_mids = evt->numof_additional_mids;
2942be7d22fSVladimir Kondratiev 
29515e23124SVladimir Kondratiev 	wil_info(wil, "FW ver. %d; MAC %pM; %d MID's\n", wil->fw_version,
296b8023177SVladimir Kondratiev 		 evt->mac, wil->n_mids);
2972cd0f021SVladimir Kondratiev 	/* ignore MAC address, we already have it from the boot loader */
2982be7d22fSVladimir Kondratiev 	snprintf(wdev->wiphy->fw_version, sizeof(wdev->wiphy->fw_version),
299b8023177SVladimir Kondratiev 		 "%d", wil->fw_version);
3002be7d22fSVladimir Kondratiev 
301c33407a8SVladimir Kondratiev 	wil_set_recovery_state(wil, fw_recovery_idle);
3029419b6a2SVladimir Kondratiev 	set_bit(wil_status_fwready, wil->status);
30359502647SDedy Lansky 	/* let the reset sequence continue */
3042be7d22fSVladimir Kondratiev 	complete(&wil->wmi_ready);
3052be7d22fSVladimir Kondratiev }
3062be7d22fSVladimir Kondratiev 
3072be7d22fSVladimir Kondratiev static void wmi_evt_rx_mgmt(struct wil6210_priv *wil, int id, void *d, int len)
3082be7d22fSVladimir Kondratiev {
3092be7d22fSVladimir Kondratiev 	struct wmi_rx_mgmt_packet_event *data = d;
3102be7d22fSVladimir Kondratiev 	struct wiphy *wiphy = wil_to_wiphy(wil);
3112be7d22fSVladimir Kondratiev 	struct ieee80211_mgmt *rx_mgmt_frame =
3122be7d22fSVladimir Kondratiev 			(struct ieee80211_mgmt *)data->payload;
31390d89e9aSVladimir Kondratiev 	int flen = len - offsetof(struct wmi_rx_mgmt_packet_event, payload);
31490d89e9aSVladimir Kondratiev 	int ch_no;
31590d89e9aSVladimir Kondratiev 	u32 freq;
31690d89e9aSVladimir Kondratiev 	struct ieee80211_channel *channel;
31790d89e9aSVladimir Kondratiev 	s32 signal;
31890d89e9aSVladimir Kondratiev 	__le16 fc;
31990d89e9aSVladimir Kondratiev 	u32 d_len;
32090d89e9aSVladimir Kondratiev 	u16 d_status;
3212be7d22fSVladimir Kondratiev 
32290d89e9aSVladimir Kondratiev 	if (flen < 0) {
32390d89e9aSVladimir Kondratiev 		wil_err(wil, "MGMT Rx: short event, len %d\n", len);
32490d89e9aSVladimir Kondratiev 		return;
32590d89e9aSVladimir Kondratiev 	}
32690d89e9aSVladimir Kondratiev 
32790d89e9aSVladimir Kondratiev 	d_len = le32_to_cpu(data->info.len);
32890d89e9aSVladimir Kondratiev 	if (d_len != flen) {
32990d89e9aSVladimir Kondratiev 		wil_err(wil,
33090d89e9aSVladimir Kondratiev 			"MGMT Rx: length mismatch, d_len %d should be %d\n",
33190d89e9aSVladimir Kondratiev 			d_len, flen);
33290d89e9aSVladimir Kondratiev 		return;
33390d89e9aSVladimir Kondratiev 	}
33490d89e9aSVladimir Kondratiev 
33590d89e9aSVladimir Kondratiev 	ch_no = data->info.channel + 1;
33690d89e9aSVladimir Kondratiev 	freq = ieee80211_channel_to_frequency(ch_no, IEEE80211_BAND_60GHZ);
33790d89e9aSVladimir Kondratiev 	channel = ieee80211_get_channel(wiphy, freq);
33890d89e9aSVladimir Kondratiev 	signal = data->info.sqi;
33990d89e9aSVladimir Kondratiev 	d_status = le16_to_cpu(data->info.status);
34090d89e9aSVladimir Kondratiev 	fc = rx_mgmt_frame->frame_control;
34190d89e9aSVladimir Kondratiev 
34290d89e9aSVladimir Kondratiev 	wil_dbg_wmi(wil, "MGMT Rx: channel %d MCS %d SNR %d SQI %d%%\n",
343b8b33a3aSVladimir Kondratiev 		    data->info.channel, data->info.mcs, data->info.snr,
344b8b33a3aSVladimir Kondratiev 		    data->info.sqi);
345f27dbf78SVladimir Kondratiev 	wil_dbg_wmi(wil, "status 0x%04x len %d fc 0x%04x\n", d_status, d_len,
346f27dbf78SVladimir Kondratiev 		    le16_to_cpu(fc));
3477743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "qid %d mid %d cid %d\n",
3482be7d22fSVladimir Kondratiev 		    data->info.qid, data->info.mid, data->info.cid);
34990d89e9aSVladimir Kondratiev 	wil_hex_dump_wmi("MGMT Rx ", DUMP_PREFIX_OFFSET, 16, 1, rx_mgmt_frame,
35090d89e9aSVladimir Kondratiev 			 d_len, true);
3512be7d22fSVladimir Kondratiev 
3522be7d22fSVladimir Kondratiev 	if (!channel) {
3532be7d22fSVladimir Kondratiev 		wil_err(wil, "Frame on unsupported channel\n");
3542be7d22fSVladimir Kondratiev 		return;
3552be7d22fSVladimir Kondratiev 	}
3562be7d22fSVladimir Kondratiev 
3572be7d22fSVladimir Kondratiev 	if (ieee80211_is_beacon(fc) || ieee80211_is_probe_resp(fc)) {
3582be7d22fSVladimir Kondratiev 		struct cfg80211_bss *bss;
3598eea944aSVladimir Kondratiev 		u64 tsf = le64_to_cpu(rx_mgmt_frame->u.beacon.timestamp);
3608eea944aSVladimir Kondratiev 		u16 cap = le16_to_cpu(rx_mgmt_frame->u.beacon.capab_info);
3618eea944aSVladimir Kondratiev 		u16 bi = le16_to_cpu(rx_mgmt_frame->u.beacon.beacon_int);
3628eea944aSVladimir Kondratiev 		const u8 *ie_buf = rx_mgmt_frame->u.beacon.variable;
3638eea944aSVladimir Kondratiev 		size_t ie_len = d_len - offsetof(struct ieee80211_mgmt,
3648eea944aSVladimir Kondratiev 						 u.beacon.variable);
3658eea944aSVladimir Kondratiev 		wil_dbg_wmi(wil, "Capability info : 0x%04x\n", cap);
3668eea944aSVladimir Kondratiev 		wil_dbg_wmi(wil, "TSF : 0x%016llx\n", tsf);
3678eea944aSVladimir Kondratiev 		wil_dbg_wmi(wil, "Beacon interval : %d\n", bi);
3688eea944aSVladimir Kondratiev 		wil_hex_dump_wmi("IE ", DUMP_PREFIX_OFFSET, 16, 1, ie_buf,
3698eea944aSVladimir Kondratiev 				 ie_len, true);
3702be7d22fSVladimir Kondratiev 
371a0f7845bSVladimir Kondratiev 		bss = cfg80211_inform_bss_frame(wiphy, channel, rx_mgmt_frame,
372a0f7845bSVladimir Kondratiev 						d_len, signal, GFP_KERNEL);
3732be7d22fSVladimir Kondratiev 		if (bss) {
3747743882dSVladimir Kondratiev 			wil_dbg_wmi(wil, "Added BSS %pM\n",
3752be7d22fSVladimir Kondratiev 				    rx_mgmt_frame->bssid);
3765b112d3dSJohannes Berg 			cfg80211_put_bss(wiphy, bss);
3772be7d22fSVladimir Kondratiev 		} else {
3785bc8c1f2SJohannes Berg 			wil_err(wil, "cfg80211_inform_bss_frame() failed\n");
3792be7d22fSVladimir Kondratiev 		}
380102b1d99SVladimir Kondratiev 	} else {
381102b1d99SVladimir Kondratiev 		cfg80211_rx_mgmt(wil->wdev, freq, signal,
382970fdfa8SVladimir Kondratiev 				 (void *)rx_mgmt_frame, d_len, 0);
3832be7d22fSVladimir Kondratiev 	}
3842be7d22fSVladimir Kondratiev }
3852be7d22fSVladimir Kondratiev 
38690d89e9aSVladimir Kondratiev static void wmi_evt_tx_mgmt(struct wil6210_priv *wil, int id, void *d, int len)
38790d89e9aSVladimir Kondratiev {
38890d89e9aSVladimir Kondratiev 	struct wmi_tx_mgmt_packet_event *data = d;
38990d89e9aSVladimir Kondratiev 	struct ieee80211_mgmt *mgmt_frame =
39090d89e9aSVladimir Kondratiev 			(struct ieee80211_mgmt *)data->payload;
39190d89e9aSVladimir Kondratiev 	int flen = len - offsetof(struct wmi_tx_mgmt_packet_event, payload);
39290d89e9aSVladimir Kondratiev 
39390d89e9aSVladimir Kondratiev 	wil_hex_dump_wmi("MGMT Tx ", DUMP_PREFIX_OFFSET, 16, 1, mgmt_frame,
39490d89e9aSVladimir Kondratiev 			 flen, true);
39590d89e9aSVladimir Kondratiev }
39690d89e9aSVladimir Kondratiev 
3972be7d22fSVladimir Kondratiev static void wmi_evt_scan_complete(struct wil6210_priv *wil, int id,
3982be7d22fSVladimir Kondratiev 				  void *d, int len)
3992be7d22fSVladimir Kondratiev {
4002be7d22fSVladimir Kondratiev 	if (wil->scan_request) {
4012be7d22fSVladimir Kondratiev 		struct wmi_scan_complete_event *data = d;
4026c2faf09SVladimir Kondratiev 		bool aborted = (data->status != WMI_SCAN_SUCCESS);
4032be7d22fSVladimir Kondratiev 
4047743882dSVladimir Kondratiev 		wil_dbg_wmi(wil, "SCAN_COMPLETE(0x%08x)\n", data->status);
4052a91d7d0SVladimir Kondratiev 		wil_dbg_misc(wil, "Complete scan_request 0x%p aborted %d\n",
4062a91d7d0SVladimir Kondratiev 			     wil->scan_request, aborted);
4072a91d7d0SVladimir Kondratiev 
408047e5d74SVladimir Kondratiev 		del_timer_sync(&wil->scan_timer);
4092be7d22fSVladimir Kondratiev 		cfg80211_scan_done(wil->scan_request, aborted);
4102be7d22fSVladimir Kondratiev 		wil->scan_request = NULL;
4112be7d22fSVladimir Kondratiev 	} else {
4122be7d22fSVladimir Kondratiev 		wil_err(wil, "SCAN_COMPLETE while not scanning\n");
4132be7d22fSVladimir Kondratiev 	}
4142be7d22fSVladimir Kondratiev }
4152be7d22fSVladimir Kondratiev 
4162be7d22fSVladimir Kondratiev static void wmi_evt_connect(struct wil6210_priv *wil, int id, void *d, int len)
4172be7d22fSVladimir Kondratiev {
4182be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
4192be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
4202be7d22fSVladimir Kondratiev 	struct wmi_connect_event *evt = d;
4212be7d22fSVladimir Kondratiev 	int ch; /* channel number */
4222be7d22fSVladimir Kondratiev 	struct station_info sinfo;
4232be7d22fSVladimir Kondratiev 	u8 *assoc_req_ie, *assoc_resp_ie;
4242be7d22fSVladimir Kondratiev 	size_t assoc_req_ielen, assoc_resp_ielen;
4252be7d22fSVladimir Kondratiev 	/* capinfo(u16) + listen_interval(u16) + IEs */
4262be7d22fSVladimir Kondratiev 	const size_t assoc_req_ie_offset = sizeof(u16) * 2;
4272be7d22fSVladimir Kondratiev 	/* capinfo(u16) + status_code(u16) + associd(u16) + IEs */
4282be7d22fSVladimir Kondratiev 	const size_t assoc_resp_ie_offset = sizeof(u16) * 3;
4290916d9f2SMaya Erez 	int rc;
4302be7d22fSVladimir Kondratiev 
4312be7d22fSVladimir Kondratiev 	if (len < sizeof(*evt)) {
4322be7d22fSVladimir Kondratiev 		wil_err(wil, "Connect event too short : %d bytes\n", len);
4332be7d22fSVladimir Kondratiev 		return;
4342be7d22fSVladimir Kondratiev 	}
4352be7d22fSVladimir Kondratiev 	if (len != sizeof(*evt) + evt->beacon_ie_len + evt->assoc_req_len +
4362be7d22fSVladimir Kondratiev 		   evt->assoc_resp_len) {
4372be7d22fSVladimir Kondratiev 		wil_err(wil,
4382be7d22fSVladimir Kondratiev 			"Connect event corrupted : %d != %d + %d + %d + %d\n",
4392be7d22fSVladimir Kondratiev 			len, (int)sizeof(*evt), evt->beacon_ie_len,
4402be7d22fSVladimir Kondratiev 			evt->assoc_req_len, evt->assoc_resp_len);
4412be7d22fSVladimir Kondratiev 		return;
4422be7d22fSVladimir Kondratiev 	}
4433df2cd36SVladimir Kondratiev 	if (evt->cid >= WIL6210_MAX_CID) {
4443df2cd36SVladimir Kondratiev 		wil_err(wil, "Connect CID invalid : %d\n", evt->cid);
4453df2cd36SVladimir Kondratiev 		return;
4463df2cd36SVladimir Kondratiev 	}
4473df2cd36SVladimir Kondratiev 
4482be7d22fSVladimir Kondratiev 	ch = evt->channel + 1;
4490916d9f2SMaya Erez 	wil_info(wil, "Connect %pM channel [%d] cid %d\n",
4502be7d22fSVladimir Kondratiev 		 evt->bssid, ch, evt->cid);
4517743882dSVladimir Kondratiev 	wil_hex_dump_wmi("connect AI : ", DUMP_PREFIX_OFFSET, 16, 1,
4522be7d22fSVladimir Kondratiev 			 evt->assoc_info, len - sizeof(*evt), true);
4532be7d22fSVladimir Kondratiev 
4542be7d22fSVladimir Kondratiev 	/* figure out IE's */
4552be7d22fSVladimir Kondratiev 	assoc_req_ie = &evt->assoc_info[evt->beacon_ie_len +
4562be7d22fSVladimir Kondratiev 					assoc_req_ie_offset];
4572be7d22fSVladimir Kondratiev 	assoc_req_ielen = evt->assoc_req_len - assoc_req_ie_offset;
4582be7d22fSVladimir Kondratiev 	if (evt->assoc_req_len <= assoc_req_ie_offset) {
4592be7d22fSVladimir Kondratiev 		assoc_req_ie = NULL;
4602be7d22fSVladimir Kondratiev 		assoc_req_ielen = 0;
4612be7d22fSVladimir Kondratiev 	}
4622be7d22fSVladimir Kondratiev 
4632be7d22fSVladimir Kondratiev 	assoc_resp_ie = &evt->assoc_info[evt->beacon_ie_len +
4642be7d22fSVladimir Kondratiev 					 evt->assoc_req_len +
4652be7d22fSVladimir Kondratiev 					 assoc_resp_ie_offset];
4662be7d22fSVladimir Kondratiev 	assoc_resp_ielen = evt->assoc_resp_len - assoc_resp_ie_offset;
4672be7d22fSVladimir Kondratiev 	if (evt->assoc_resp_len <= assoc_resp_ie_offset) {
4682be7d22fSVladimir Kondratiev 		assoc_resp_ie = NULL;
4692be7d22fSVladimir Kondratiev 		assoc_resp_ielen = 0;
4702be7d22fSVladimir Kondratiev 	}
4712be7d22fSVladimir Kondratiev 
4720916d9f2SMaya Erez 	mutex_lock(&wil->mutex);
4730916d9f2SMaya Erez 	if (test_bit(wil_status_resetting, wil->status) ||
4740916d9f2SMaya Erez 	    !test_bit(wil_status_fwready, wil->status)) {
4750916d9f2SMaya Erez 		wil_err(wil, "status_resetting, cancel connect event, CID %d\n",
4760916d9f2SMaya Erez 			evt->cid);
4770916d9f2SMaya Erez 		mutex_unlock(&wil->mutex);
4780916d9f2SMaya Erez 		/* no need for cleanup, wil_reset will do that */
4790916d9f2SMaya Erez 		return;
4800916d9f2SMaya Erez 	}
4810916d9f2SMaya Erez 
4822be7d22fSVladimir Kondratiev 	if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
4832be7d22fSVladimir Kondratiev 	    (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
4849419b6a2SVladimir Kondratiev 		if (!test_bit(wil_status_fwconnecting, wil->status)) {
4852be7d22fSVladimir Kondratiev 			wil_err(wil, "Not in connecting state\n");
4860916d9f2SMaya Erez 			mutex_unlock(&wil->mutex);
4872be7d22fSVladimir Kondratiev 			return;
4882be7d22fSVladimir Kondratiev 		}
4892be7d22fSVladimir Kondratiev 		del_timer_sync(&wil->connect_timer);
4900916d9f2SMaya Erez 	}
4910916d9f2SMaya Erez 
4920916d9f2SMaya Erez 	/* FIXME FW can transmit only ucast frames to peer */
4930916d9f2SMaya Erez 	/* FIXME real ring_id instead of hard coded 0 */
4940916d9f2SMaya Erez 	ether_addr_copy(wil->sta[evt->cid].addr, evt->bssid);
4950916d9f2SMaya Erez 	wil->sta[evt->cid].status = wil_sta_conn_pending;
4960916d9f2SMaya Erez 
4970916d9f2SMaya Erez 	rc = wil_tx_init(wil, evt->cid);
4980916d9f2SMaya Erez 	if (rc) {
4990916d9f2SMaya Erez 		wil_err(wil, "%s: config tx vring failed for CID %d, rc (%d)\n",
5000916d9f2SMaya Erez 			__func__, evt->cid, rc);
5010916d9f2SMaya Erez 		wmi_disconnect_sta(wil, wil->sta[evt->cid].addr,
5020916d9f2SMaya Erez 				   WLAN_REASON_UNSPECIFIED, false);
5030916d9f2SMaya Erez 	} else {
5040916d9f2SMaya Erez 		wil_info(wil, "%s: successful connection to CID %d\n",
5050916d9f2SMaya Erez 			 __func__, evt->cid);
5060916d9f2SMaya Erez 	}
5070916d9f2SMaya Erez 
5080916d9f2SMaya Erez 	if ((wdev->iftype == NL80211_IFTYPE_STATION) ||
5090916d9f2SMaya Erez 	    (wdev->iftype == NL80211_IFTYPE_P2P_CLIENT)) {
5100916d9f2SMaya Erez 		if (rc) {
5110916d9f2SMaya Erez 			netif_tx_stop_all_queues(ndev);
5120916d9f2SMaya Erez 			netif_carrier_off(ndev);
5130916d9f2SMaya Erez 			wil_err(wil,
5140916d9f2SMaya Erez 				"%s: cfg80211_connect_result with failure\n",
5150916d9f2SMaya Erez 				__func__);
5160916d9f2SMaya Erez 			cfg80211_connect_result(ndev, evt->bssid, NULL, 0,
5170916d9f2SMaya Erez 						NULL, 0,
5180916d9f2SMaya Erez 						WLAN_STATUS_UNSPECIFIED_FAILURE,
5190916d9f2SMaya Erez 						GFP_KERNEL);
5200916d9f2SMaya Erez 			goto out;
5210916d9f2SMaya Erez 		} else {
5222be7d22fSVladimir Kondratiev 			cfg80211_connect_result(ndev, evt->bssid,
5232be7d22fSVladimir Kondratiev 						assoc_req_ie, assoc_req_ielen,
5242be7d22fSVladimir Kondratiev 						assoc_resp_ie, assoc_resp_ielen,
5250916d9f2SMaya Erez 						WLAN_STATUS_SUCCESS,
5260916d9f2SMaya Erez 						GFP_KERNEL);
5270916d9f2SMaya Erez 		}
5282be7d22fSVladimir Kondratiev 	} else if ((wdev->iftype == NL80211_IFTYPE_AP) ||
5292be7d22fSVladimir Kondratiev 		   (wdev->iftype == NL80211_IFTYPE_P2P_GO)) {
5300916d9f2SMaya Erez 		if (rc)
5310916d9f2SMaya Erez 			goto out;
5320916d9f2SMaya Erez 
5332be7d22fSVladimir Kondratiev 		memset(&sinfo, 0, sizeof(sinfo));
5342be7d22fSVladimir Kondratiev 
5352be7d22fSVladimir Kondratiev 		sinfo.generation = wil->sinfo_gen++;
5362be7d22fSVladimir Kondratiev 
5372be7d22fSVladimir Kondratiev 		if (assoc_req_ie) {
5382be7d22fSVladimir Kondratiev 			sinfo.assoc_req_ies = assoc_req_ie;
5392be7d22fSVladimir Kondratiev 			sinfo.assoc_req_ies_len = assoc_req_ielen;
5402be7d22fSVladimir Kondratiev 		}
5412be7d22fSVladimir Kondratiev 
5422be7d22fSVladimir Kondratiev 		cfg80211_new_sta(ndev, evt->bssid, &sinfo, GFP_KERNEL);
5430916d9f2SMaya Erez 	} else {
5440916d9f2SMaya Erez 		wil_err(wil, "%s: unhandled iftype %d for CID %d\n",
5450916d9f2SMaya Erez 			__func__, wdev->iftype, evt->cid);
5460916d9f2SMaya Erez 		goto out;
5472be7d22fSVladimir Kondratiev 	}
5480916d9f2SMaya Erez 
5490916d9f2SMaya Erez 	wil->sta[evt->cid].status = wil_sta_connected;
5509419b6a2SVladimir Kondratiev 	set_bit(wil_status_fwconnected, wil->status);
5510916d9f2SMaya Erez 	netif_tx_wake_all_queues(ndev);
5522be7d22fSVladimir Kondratiev 
5530916d9f2SMaya Erez out:
5540916d9f2SMaya Erez 	if (rc)
5550916d9f2SMaya Erez 		wil->sta[evt->cid].status = wil_sta_unused;
5560916d9f2SMaya Erez 	clear_bit(wil_status_fwconnecting, wil->status);
5570916d9f2SMaya Erez 	mutex_unlock(&wil->mutex);
5582be7d22fSVladimir Kondratiev }
5592be7d22fSVladimir Kondratiev 
5602be7d22fSVladimir Kondratiev static void wmi_evt_disconnect(struct wil6210_priv *wil, int id,
5612be7d22fSVladimir Kondratiev 			       void *d, int len)
5622be7d22fSVladimir Kondratiev {
5632be7d22fSVladimir Kondratiev 	struct wmi_disconnect_event *evt = d;
5644821e6d8SVladimir Kondratiev 	u16 reason_code = le16_to_cpu(evt->protocol_reason_status);
5652be7d22fSVladimir Kondratiev 
5660916d9f2SMaya Erez 	wil_info(wil, "Disconnect %pM reason [proto %d wmi %d]\n",
5674821e6d8SVladimir Kondratiev 		 evt->bssid, reason_code, evt->disconnect_reason);
5682be7d22fSVladimir Kondratiev 
5692be7d22fSVladimir Kondratiev 	wil->sinfo_gen++;
5702be7d22fSVladimir Kondratiev 
571097638a0SVladimir Kondratiev 	mutex_lock(&wil->mutex);
5724821e6d8SVladimir Kondratiev 	wil6210_disconnect(wil, evt->bssid, reason_code, true);
573097638a0SVladimir Kondratiev 	mutex_unlock(&wil->mutex);
5742be7d22fSVladimir Kondratiev }
5752be7d22fSVladimir Kondratiev 
5762be7d22fSVladimir Kondratiev /*
5772be7d22fSVladimir Kondratiev  * Firmware reports EAPOL frame using WME event.
5782be7d22fSVladimir Kondratiev  * Reconstruct Ethernet frame and deliver it via normal Rx
5792be7d22fSVladimir Kondratiev  */
5802be7d22fSVladimir Kondratiev static void wmi_evt_eapol_rx(struct wil6210_priv *wil, int id,
5812be7d22fSVladimir Kondratiev 			     void *d, int len)
5822be7d22fSVladimir Kondratiev {
5832be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
5842be7d22fSVladimir Kondratiev 	struct wmi_eapol_rx_event *evt = d;
5852be7d22fSVladimir Kondratiev 	u16 eapol_len = le16_to_cpu(evt->eapol_len);
5862be7d22fSVladimir Kondratiev 	int sz = eapol_len + ETH_HLEN;
5872be7d22fSVladimir Kondratiev 	struct sk_buff *skb;
5882be7d22fSVladimir Kondratiev 	struct ethhdr *eth;
589c8b78b5fSVladimir Kondratiev 	int cid;
590c8b78b5fSVladimir Kondratiev 	struct wil_net_stats *stats = NULL;
5912be7d22fSVladimir Kondratiev 
5927743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "EAPOL len %d from %pM\n", eapol_len,
5932be7d22fSVladimir Kondratiev 		    evt->src_mac);
5942be7d22fSVladimir Kondratiev 
595c8b78b5fSVladimir Kondratiev 	cid = wil_find_cid(wil, evt->src_mac);
596c8b78b5fSVladimir Kondratiev 	if (cid >= 0)
597c8b78b5fSVladimir Kondratiev 		stats = &wil->sta[cid].stats;
598c8b78b5fSVladimir Kondratiev 
5992be7d22fSVladimir Kondratiev 	if (eapol_len > 196) { /* TODO: revisit size limit */
6002be7d22fSVladimir Kondratiev 		wil_err(wil, "EAPOL too large\n");
6012be7d22fSVladimir Kondratiev 		return;
6022be7d22fSVladimir Kondratiev 	}
6032be7d22fSVladimir Kondratiev 
6042be7d22fSVladimir Kondratiev 	skb = alloc_skb(sz, GFP_KERNEL);
6052be7d22fSVladimir Kondratiev 	if (!skb) {
6062be7d22fSVladimir Kondratiev 		wil_err(wil, "Failed to allocate skb\n");
6072be7d22fSVladimir Kondratiev 		return;
6082be7d22fSVladimir Kondratiev 	}
609c8b78b5fSVladimir Kondratiev 
6102be7d22fSVladimir Kondratiev 	eth = (struct ethhdr *)skb_put(skb, ETH_HLEN);
611a82553bbSVladimir Kondratiev 	ether_addr_copy(eth->h_dest, ndev->dev_addr);
612a82553bbSVladimir Kondratiev 	ether_addr_copy(eth->h_source, evt->src_mac);
6132be7d22fSVladimir Kondratiev 	eth->h_proto = cpu_to_be16(ETH_P_PAE);
6142be7d22fSVladimir Kondratiev 	memcpy(skb_put(skb, eapol_len), evt->eapol, eapol_len);
6152be7d22fSVladimir Kondratiev 	skb->protocol = eth_type_trans(skb, ndev);
6162be7d22fSVladimir Kondratiev 	if (likely(netif_rx_ni(skb) == NET_RX_SUCCESS)) {
6172be7d22fSVladimir Kondratiev 		ndev->stats.rx_packets++;
618c8b78b5fSVladimir Kondratiev 		ndev->stats.rx_bytes += sz;
619c8b78b5fSVladimir Kondratiev 		if (stats) {
620c8b78b5fSVladimir Kondratiev 			stats->rx_packets++;
621c8b78b5fSVladimir Kondratiev 			stats->rx_bytes += sz;
622c8b78b5fSVladimir Kondratiev 		}
6232be7d22fSVladimir Kondratiev 	} else {
6242be7d22fSVladimir Kondratiev 		ndev->stats.rx_dropped++;
625c8b78b5fSVladimir Kondratiev 		if (stats)
626c8b78b5fSVladimir Kondratiev 			stats->rx_dropped++;
6272be7d22fSVladimir Kondratiev 	}
6282be7d22fSVladimir Kondratiev }
6292be7d22fSVladimir Kondratiev 
630230d8442SVladimir Kondratiev static void wmi_evt_vring_en(struct wil6210_priv *wil, int id, void *d, int len)
6313a124ed6SVladimir Kondratiev {
632230d8442SVladimir Kondratiev 	struct wmi_vring_en_event *evt = d;
633230d8442SVladimir Kondratiev 	u8 vri = evt->vring_index;
6343a124ed6SVladimir Kondratiev 
635230d8442SVladimir Kondratiev 	wil_dbg_wmi(wil, "Enable vring %d\n", vri);
6363a124ed6SVladimir Kondratiev 
637230d8442SVladimir Kondratiev 	if (vri >= ARRAY_SIZE(wil->vring_tx)) {
638230d8442SVladimir Kondratiev 		wil_err(wil, "Enable for invalid vring %d\n", vri);
639e58c9f70SVladimir Kondratiev 		return;
640e58c9f70SVladimir Kondratiev 	}
641230d8442SVladimir Kondratiev 	wil->vring_tx_data[vri].dot1x_open = true;
642230d8442SVladimir Kondratiev 	if (vri == wil->bcast_vring) /* no BA for bcast */
643230d8442SVladimir Kondratiev 		return;
6443a3def8dSVladimir Kondratiev 	if (agg_wsize >= 0)
645230d8442SVladimir Kondratiev 		wil_addba_tx_request(wil, vri, agg_wsize);
6463442a504SVladimir Kondratiev }
6473442a504SVladimir Kondratiev 
648249a382bSVladimir Kondratiev static void wmi_evt_ba_status(struct wil6210_priv *wil, int id, void *d,
649249a382bSVladimir Kondratiev 			      int len)
650249a382bSVladimir Kondratiev {
651249a382bSVladimir Kondratiev 	struct wmi_vring_ba_status_event *evt = d;
6523277213fSVladimir Kondratiev 	struct vring_tx_data *txdata;
653249a382bSVladimir Kondratiev 
654cbcf5866SVladimir Kondratiev 	wil_dbg_wmi(wil, "BACK[%d] %s {%d} timeout %d AMSDU%s\n",
6557b05b0abSVladimir Kondratiev 		    evt->ringid,
6567b05b0abSVladimir Kondratiev 		    evt->status == WMI_BA_AGREED ? "OK" : "N/A",
657cbcf5866SVladimir Kondratiev 		    evt->agg_wsize, __le16_to_cpu(evt->ba_timeout),
658cbcf5866SVladimir Kondratiev 		    evt->amsdu ? "+" : "-");
659b4490f42SVladimir Kondratiev 
6607b05b0abSVladimir Kondratiev 	if (evt->ringid >= WIL6210_MAX_TX_RINGS) {
6617b05b0abSVladimir Kondratiev 		wil_err(wil, "invalid ring id %d\n", evt->ringid);
6627b05b0abSVladimir Kondratiev 		return;
6637b05b0abSVladimir Kondratiev 	}
6647b05b0abSVladimir Kondratiev 
6653277213fSVladimir Kondratiev 	if (evt->status != WMI_BA_AGREED) {
6663277213fSVladimir Kondratiev 		evt->ba_timeout = 0;
6673277213fSVladimir Kondratiev 		evt->agg_wsize = 0;
668cbcf5866SVladimir Kondratiev 		evt->amsdu = 0;
6697b05b0abSVladimir Kondratiev 	}
6707b05b0abSVladimir Kondratiev 
6713277213fSVladimir Kondratiev 	txdata = &wil->vring_tx_data[evt->ringid];
6723277213fSVladimir Kondratiev 
6733277213fSVladimir Kondratiev 	txdata->agg_timeout = le16_to_cpu(evt->ba_timeout);
6743277213fSVladimir Kondratiev 	txdata->agg_wsize = evt->agg_wsize;
675cbcf5866SVladimir Kondratiev 	txdata->agg_amsdu = evt->amsdu;
6763a124ed6SVladimir Kondratiev 	txdata->addba_in_progress = false;
6777b05b0abSVladimir Kondratiev }
6787b05b0abSVladimir Kondratiev 
6793277213fSVladimir Kondratiev static void wmi_evt_addba_rx_req(struct wil6210_priv *wil, int id, void *d,
6803277213fSVladimir Kondratiev 				 int len)
6813277213fSVladimir Kondratiev {
6823277213fSVladimir Kondratiev 	struct wmi_rcp_addba_req_event *evt = d;
6833277213fSVladimir Kondratiev 
6843277213fSVladimir Kondratiev 	wil_addba_rx_request(wil, evt->cidxtid, evt->dialog_token,
6853277213fSVladimir Kondratiev 			     evt->ba_param_set, evt->ba_timeout,
6863277213fSVladimir Kondratiev 			     evt->ba_seq_ctrl);
6873277213fSVladimir Kondratiev }
6883277213fSVladimir Kondratiev 
6893277213fSVladimir Kondratiev static void wmi_evt_delba(struct wil6210_priv *wil, int id, void *d, int len)
690bd33273bSVladimir Kondratiev __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
6913277213fSVladimir Kondratiev {
6923277213fSVladimir Kondratiev 	struct wmi_delba_event *evt = d;
6933277213fSVladimir Kondratiev 	u8 cid, tid;
6943277213fSVladimir Kondratiev 	u16 reason = __le16_to_cpu(evt->reason);
6953277213fSVladimir Kondratiev 	struct wil_sta_info *sta;
696ec81b5adSDedy Lansky 	struct wil_tid_ampdu_rx *r;
697ec81b5adSDedy Lansky 
698bd33273bSVladimir Kondratiev 	might_sleep();
6993277213fSVladimir Kondratiev 	parse_cidxtid(evt->cidxtid, &cid, &tid);
7003277213fSVladimir Kondratiev 	wil_dbg_wmi(wil, "DELBA CID %d TID %d from %s reason %d\n",
7013277213fSVladimir Kondratiev 		    cid, tid,
7023277213fSVladimir Kondratiev 		    evt->from_initiator ? "originator" : "recipient",
7033277213fSVladimir Kondratiev 		    reason);
7043277213fSVladimir Kondratiev 	if (!evt->from_initiator) {
7053277213fSVladimir Kondratiev 		int i;
7063277213fSVladimir Kondratiev 		/* find Tx vring it belongs to */
7073277213fSVladimir Kondratiev 		for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
7083277213fSVladimir Kondratiev 			if ((wil->vring2cid_tid[i][0] == cid) &&
7093277213fSVladimir Kondratiev 			    (wil->vring2cid_tid[i][1] == tid)) {
7103277213fSVladimir Kondratiev 				struct vring_tx_data *txdata =
7113277213fSVladimir Kondratiev 					&wil->vring_tx_data[i];
7123277213fSVladimir Kondratiev 
7133277213fSVladimir Kondratiev 				wil_dbg_wmi(wil, "DELBA Tx vring %d\n", i);
7143277213fSVladimir Kondratiev 				txdata->agg_timeout = 0;
7153277213fSVladimir Kondratiev 				txdata->agg_wsize = 0;
7163a124ed6SVladimir Kondratiev 				txdata->addba_in_progress = false;
7173277213fSVladimir Kondratiev 
7183277213fSVladimir Kondratiev 				break; /* max. 1 matching ring */
7193277213fSVladimir Kondratiev 			}
7203277213fSVladimir Kondratiev 		}
7213277213fSVladimir Kondratiev 		if (i >= ARRAY_SIZE(wil->vring2cid_tid))
7223277213fSVladimir Kondratiev 			wil_err(wil, "DELBA: unable to find Tx vring\n");
7233277213fSVladimir Kondratiev 		return;
7243277213fSVladimir Kondratiev 	}
7253277213fSVladimir Kondratiev 
7263277213fSVladimir Kondratiev 	sta = &wil->sta[cid];
7273277213fSVladimir Kondratiev 
728bd33273bSVladimir Kondratiev 	spin_lock_bh(&sta->tid_rx_lock);
729ec81b5adSDedy Lansky 
7303277213fSVladimir Kondratiev 	r = sta->tid_rx[tid];
7313277213fSVladimir Kondratiev 	sta->tid_rx[tid] = NULL;
732b4490f42SVladimir Kondratiev 	wil_tid_ampdu_rx_free(wil, r);
733ec81b5adSDedy Lansky 
734bd33273bSVladimir Kondratiev 	spin_unlock_bh(&sta->tid_rx_lock);
735b4490f42SVladimir Kondratiev }
736b4490f42SVladimir Kondratiev 
737b03fbab0SVladimir Kondratiev /**
738b03fbab0SVladimir Kondratiev  * Some events are ignored for purpose; and need not be interpreted as
739b03fbab0SVladimir Kondratiev  * "unhandled events"
740b03fbab0SVladimir Kondratiev  */
741b03fbab0SVladimir Kondratiev static void wmi_evt_ignore(struct wil6210_priv *wil, int id, void *d, int len)
742b03fbab0SVladimir Kondratiev {
743b03fbab0SVladimir Kondratiev 	wil_dbg_wmi(wil, "Ignore event 0x%04x len %d\n", id, len);
744b03fbab0SVladimir Kondratiev }
745b03fbab0SVladimir Kondratiev 
7462be7d22fSVladimir Kondratiev static const struct {
7472be7d22fSVladimir Kondratiev 	int eventid;
7482be7d22fSVladimir Kondratiev 	void (*handler)(struct wil6210_priv *wil, int eventid,
7492be7d22fSVladimir Kondratiev 			void *data, int data_len);
7502be7d22fSVladimir Kondratiev } wmi_evt_handlers[] = {
7512be7d22fSVladimir Kondratiev 	{WMI_READY_EVENTID,		wmi_evt_ready},
752817f1853SVladimir Kondratiev 	{WMI_FW_READY_EVENTID,			wmi_evt_ignore},
7532be7d22fSVladimir Kondratiev 	{WMI_RX_MGMT_PACKET_EVENTID,	wmi_evt_rx_mgmt},
75490d89e9aSVladimir Kondratiev 	{WMI_TX_MGMT_PACKET_EVENTID,		wmi_evt_tx_mgmt},
7552be7d22fSVladimir Kondratiev 	{WMI_SCAN_COMPLETE_EVENTID,	wmi_evt_scan_complete},
7562be7d22fSVladimir Kondratiev 	{WMI_CONNECT_EVENTID,		wmi_evt_connect},
7572be7d22fSVladimir Kondratiev 	{WMI_DISCONNECT_EVENTID,	wmi_evt_disconnect},
7582be7d22fSVladimir Kondratiev 	{WMI_EAPOL_RX_EVENTID,		wmi_evt_eapol_rx},
759249a382bSVladimir Kondratiev 	{WMI_BA_STATUS_EVENTID,		wmi_evt_ba_status},
7603277213fSVladimir Kondratiev 	{WMI_RCP_ADDBA_REQ_EVENTID,	wmi_evt_addba_rx_req},
7613277213fSVladimir Kondratiev 	{WMI_DELBA_EVENTID,		wmi_evt_delba},
762230d8442SVladimir Kondratiev 	{WMI_VRING_EN_EVENTID,		wmi_evt_vring_en},
763b03fbab0SVladimir Kondratiev 	{WMI_DATA_PORT_OPEN_EVENTID,		wmi_evt_ignore},
7642be7d22fSVladimir Kondratiev };
7652be7d22fSVladimir Kondratiev 
7662be7d22fSVladimir Kondratiev /*
7672be7d22fSVladimir Kondratiev  * Run in IRQ context
7682be7d22fSVladimir Kondratiev  * Extract WMI command from mailbox. Queue it to the @wil->pending_wmi_ev
7692be7d22fSVladimir Kondratiev  * that will be eventually handled by the @wmi_event_worker in the thread
7702be7d22fSVladimir Kondratiev  * context of thread "wil6210_wmi"
7712be7d22fSVladimir Kondratiev  */
7722be7d22fSVladimir Kondratiev void wmi_recv_cmd(struct wil6210_priv *wil)
7732be7d22fSVladimir Kondratiev {
7742be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring_desc d_tail;
7752be7d22fSVladimir Kondratiev 	struct wil6210_mbox_hdr hdr;
7762be7d22fSVladimir Kondratiev 	struct wil6210_mbox_ring *r = &wil->mbox_ctl.rx;
7772be7d22fSVladimir Kondratiev 	struct pending_wmi_event *evt;
7782be7d22fSVladimir Kondratiev 	u8 *cmd;
7792be7d22fSVladimir Kondratiev 	void __iomem *src;
7802be7d22fSVladimir Kondratiev 	ulong flags;
781a715c7ddSVladimir Kondratiev 	unsigned n;
7820916d9f2SMaya Erez 	unsigned int num_immed_reply = 0;
7832be7d22fSVladimir Kondratiev 
784817f1853SVladimir Kondratiev 	if (!test_bit(wil_status_mbox_ready, wil->status)) {
7854cf99c93SDedy Lansky 		wil_err(wil, "Reset in progress. Cannot handle WMI event\n");
78655f7acddSVladimir Kondratiev 		return;
78755f7acddSVladimir Kondratiev 	}
78855f7acddSVladimir Kondratiev 
789a715c7ddSVladimir Kondratiev 	for (n = 0;; n++) {
7902be7d22fSVladimir Kondratiev 		u16 len;
79195266dc0SVladimir Kondratiev 		bool q;
7920916d9f2SMaya Erez 		bool immed_reply = false;
7932be7d22fSVladimir Kondratiev 
794b9eeb512SVladimir Kondratiev 		r->head = wil_r(wil, RGF_MBOX +
7952be7d22fSVladimir Kondratiev 				offsetof(struct wil6210_mbox_ctl, rx.head));
79695266dc0SVladimir Kondratiev 		if (r->tail == r->head)
79795266dc0SVladimir Kondratiev 			break;
7982be7d22fSVladimir Kondratiev 
799a715c7ddSVladimir Kondratiev 		wil_dbg_wmi(wil, "Mbox head %08x tail %08x\n",
800a715c7ddSVladimir Kondratiev 			    r->head, r->tail);
801a715c7ddSVladimir Kondratiev 		/* read cmd descriptor from tail */
8022be7d22fSVladimir Kondratiev 		wil_memcpy_fromio_32(&d_tail, wil->csr + HOSTADDR(r->tail),
8032be7d22fSVladimir Kondratiev 				     sizeof(struct wil6210_mbox_ring_desc));
8042be7d22fSVladimir Kondratiev 		if (d_tail.sync == 0) {
8052be7d22fSVladimir Kondratiev 			wil_err(wil, "Mbox evt not owned by FW?\n");
80695266dc0SVladimir Kondratiev 			break;
8072be7d22fSVladimir Kondratiev 		}
8082be7d22fSVladimir Kondratiev 
809a715c7ddSVladimir Kondratiev 		/* read cmd header from descriptor */
8102be7d22fSVladimir Kondratiev 		if (0 != wmi_read_hdr(wil, d_tail.addr, &hdr)) {
8112be7d22fSVladimir Kondratiev 			wil_err(wil, "Mbox evt at 0x%08x?\n",
8122be7d22fSVladimir Kondratiev 				le32_to_cpu(d_tail.addr));
81395266dc0SVladimir Kondratiev 			break;
8142be7d22fSVladimir Kondratiev 		}
8152be7d22fSVladimir Kondratiev 		len = le16_to_cpu(hdr.len);
816a715c7ddSVladimir Kondratiev 		wil_dbg_wmi(wil, "Mbox evt %04x %04x %04x %02x\n",
817a715c7ddSVladimir Kondratiev 			    le16_to_cpu(hdr.seq), len, le16_to_cpu(hdr.type),
818a715c7ddSVladimir Kondratiev 			    hdr.flags);
819a715c7ddSVladimir Kondratiev 
820a715c7ddSVladimir Kondratiev 		/* read cmd buffer from descriptor */
8212be7d22fSVladimir Kondratiev 		src = wmi_buffer(wil, d_tail.addr) +
8222be7d22fSVladimir Kondratiev 		      sizeof(struct wil6210_mbox_hdr);
8232be7d22fSVladimir Kondratiev 		evt = kmalloc(ALIGN(offsetof(struct pending_wmi_event,
8242be7d22fSVladimir Kondratiev 					     event.wmi) + len, 4),
8252be7d22fSVladimir Kondratiev 			      GFP_KERNEL);
82614f8dc49SJoe Perches 		if (!evt)
82795266dc0SVladimir Kondratiev 			break;
82814f8dc49SJoe Perches 
8292be7d22fSVladimir Kondratiev 		evt->event.hdr = hdr;
8302be7d22fSVladimir Kondratiev 		cmd = (void *)&evt->event.wmi;
8312be7d22fSVladimir Kondratiev 		wil_memcpy_fromio_32(cmd, src, len);
8322be7d22fSVladimir Kondratiev 		/* mark entry as empty */
833b9eeb512SVladimir Kondratiev 		wil_w(wil, r->tail +
834b9eeb512SVladimir Kondratiev 		      offsetof(struct wil6210_mbox_ring_desc, sync), 0);
8352be7d22fSVladimir Kondratiev 		/* indicate */
8362be7d22fSVladimir Kondratiev 		if ((hdr.type == WIL_MBOX_HDR_TYPE_WMI) &&
8372be7d22fSVladimir Kondratiev 		    (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
838f988b23fSVladimir Kondratiev 			struct wil6210_mbox_hdr_wmi *wmi = &evt->event.wmi;
839f988b23fSVladimir Kondratiev 			u16 id = le16_to_cpu(wmi->id);
840f988b23fSVladimir Kondratiev 			u32 tstamp = le32_to_cpu(wmi->timestamp);
841fe5c271eSMaya Erez 			spin_lock_irqsave(&wil->wmi_ev_lock, flags);
8420916d9f2SMaya Erez 			if (wil->reply_id && wil->reply_id == id) {
8430916d9f2SMaya Erez 				if (wil->reply_buf) {
8440916d9f2SMaya Erez 					memcpy(wil->reply_buf, wmi,
8450916d9f2SMaya Erez 					       min(len, wil->reply_size));
8460916d9f2SMaya Erez 					immed_reply = true;
8470916d9f2SMaya Erez 				}
8480916d9f2SMaya Erez 			}
849fe5c271eSMaya Erez 			spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
8508fe59627SVladimir Kondratiev 
851f988b23fSVladimir Kondratiev 			wil_dbg_wmi(wil, "WMI event 0x%04x MID %d @%d msec\n",
852f988b23fSVladimir Kondratiev 				    id, wmi->mid, tstamp);
853f988b23fSVladimir Kondratiev 			trace_wil6210_wmi_event(wmi, &wmi[1],
854f988b23fSVladimir Kondratiev 						len - sizeof(*wmi));
8552be7d22fSVladimir Kondratiev 		}
8567743882dSVladimir Kondratiev 		wil_hex_dump_wmi("evt ", DUMP_PREFIX_OFFSET, 16, 1,
8572be7d22fSVladimir Kondratiev 				 &evt->event.hdr, sizeof(hdr) + len, true);
8582be7d22fSVladimir Kondratiev 
8592be7d22fSVladimir Kondratiev 		/* advance tail */
8602be7d22fSVladimir Kondratiev 		r->tail = r->base + ((r->tail - r->base +
8612be7d22fSVladimir Kondratiev 			  sizeof(struct wil6210_mbox_ring_desc)) % r->size);
862b9eeb512SVladimir Kondratiev 		wil_w(wil, RGF_MBOX +
863b9eeb512SVladimir Kondratiev 		      offsetof(struct wil6210_mbox_ctl, rx.tail), r->tail);
8642be7d22fSVladimir Kondratiev 
8650916d9f2SMaya Erez 		if (immed_reply) {
8660916d9f2SMaya Erez 			wil_dbg_wmi(wil, "%s: Complete WMI 0x%04x\n",
8670916d9f2SMaya Erez 				    __func__, wil->reply_id);
8680916d9f2SMaya Erez 			kfree(evt);
8690916d9f2SMaya Erez 			num_immed_reply++;
8700916d9f2SMaya Erez 			complete(&wil->wmi_call);
8710916d9f2SMaya Erez 		} else {
8722be7d22fSVladimir Kondratiev 			/* add to the pending list */
8732be7d22fSVladimir Kondratiev 			spin_lock_irqsave(&wil->wmi_ev_lock, flags);
8742be7d22fSVladimir Kondratiev 			list_add_tail(&evt->list, &wil->pending_wmi_ev);
8752be7d22fSVladimir Kondratiev 			spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
87695266dc0SVladimir Kondratiev 			q = queue_work(wil->wmi_wq, &wil->wmi_event_worker);
8777743882dSVladimir Kondratiev 			wil_dbg_wmi(wil, "queue_work -> %d\n", q);
8782be7d22fSVladimir Kondratiev 		}
8790916d9f2SMaya Erez 	}
88095266dc0SVladimir Kondratiev 	/* normally, 1 event per IRQ should be processed */
8810916d9f2SMaya Erez 	wil_dbg_wmi(wil, "%s -> %d events queued, %d completed\n", __func__,
8820916d9f2SMaya Erez 		    n - num_immed_reply, num_immed_reply);
8832be7d22fSVladimir Kondratiev }
8842be7d22fSVladimir Kondratiev 
8852be7d22fSVladimir Kondratiev int wmi_call(struct wil6210_priv *wil, u16 cmdid, void *buf, u16 len,
8862be7d22fSVladimir Kondratiev 	     u16 reply_id, void *reply, u8 reply_size, int to_msec)
8872be7d22fSVladimir Kondratiev {
8882be7d22fSVladimir Kondratiev 	int rc;
889f4bbb829SNicholas Mc Guire 	unsigned long remain;
8902be7d22fSVladimir Kondratiev 
8912be7d22fSVladimir Kondratiev 	mutex_lock(&wil->wmi_mutex);
8922be7d22fSVladimir Kondratiev 
893fe5c271eSMaya Erez 	spin_lock(&wil->wmi_ev_lock);
894fe5c271eSMaya Erez 	wil->reply_id = reply_id;
895fe5c271eSMaya Erez 	wil->reply_buf = reply;
896fe5c271eSMaya Erez 	wil->reply_size = reply_size;
897fe5c271eSMaya Erez 	spin_unlock(&wil->wmi_ev_lock);
898fe5c271eSMaya Erez 
8992be7d22fSVladimir Kondratiev 	rc = __wmi_send(wil, cmdid, buf, len);
9002be7d22fSVladimir Kondratiev 	if (rc)
9012be7d22fSVladimir Kondratiev 		goto out;
9022be7d22fSVladimir Kondratiev 
90359502647SDedy Lansky 	remain = wait_for_completion_timeout(&wil->wmi_call,
9042be7d22fSVladimir Kondratiev 					     msecs_to_jiffies(to_msec));
9052be7d22fSVladimir Kondratiev 	if (0 == remain) {
9062be7d22fSVladimir Kondratiev 		wil_err(wil, "wmi_call(0x%04x->0x%04x) timeout %d msec\n",
9072be7d22fSVladimir Kondratiev 			cmdid, reply_id, to_msec);
9082be7d22fSVladimir Kondratiev 		rc = -ETIME;
9092be7d22fSVladimir Kondratiev 	} else {
9107743882dSVladimir Kondratiev 		wil_dbg_wmi(wil,
9112be7d22fSVladimir Kondratiev 			    "wmi_call(0x%04x->0x%04x) completed in %d msec\n",
9122be7d22fSVladimir Kondratiev 			    cmdid, reply_id,
9132be7d22fSVladimir Kondratiev 			    to_msec - jiffies_to_msecs(remain));
9142be7d22fSVladimir Kondratiev 	}
915fe5c271eSMaya Erez 
916fe5c271eSMaya Erez out:
917fe5c271eSMaya Erez 	spin_lock(&wil->wmi_ev_lock);
9182be7d22fSVladimir Kondratiev 	wil->reply_id = 0;
9192be7d22fSVladimir Kondratiev 	wil->reply_buf = NULL;
9202be7d22fSVladimir Kondratiev 	wil->reply_size = 0;
921fe5c271eSMaya Erez 	spin_unlock(&wil->wmi_ev_lock);
922fe5c271eSMaya Erez 
9232be7d22fSVladimir Kondratiev 	mutex_unlock(&wil->wmi_mutex);
9242be7d22fSVladimir Kondratiev 
9252be7d22fSVladimir Kondratiev 	return rc;
9262be7d22fSVladimir Kondratiev }
9272be7d22fSVladimir Kondratiev 
9282be7d22fSVladimir Kondratiev int wmi_echo(struct wil6210_priv *wil)
9292be7d22fSVladimir Kondratiev {
9302be7d22fSVladimir Kondratiev 	struct wmi_echo_cmd cmd = {
9312be7d22fSVladimir Kondratiev 		.value = cpu_to_le32(0x12345678),
9322be7d22fSVladimir Kondratiev 	};
9332be7d22fSVladimir Kondratiev 
9342be7d22fSVladimir Kondratiev 	return wmi_call(wil, WMI_ECHO_CMDID, &cmd, sizeof(cmd),
935a54a40daSVladimir Kondratiev 			WMI_ECHO_RSP_EVENTID, NULL, 0, 50);
9362be7d22fSVladimir Kondratiev }
9372be7d22fSVladimir Kondratiev 
9382be7d22fSVladimir Kondratiev int wmi_set_mac_address(struct wil6210_priv *wil, void *addr)
9392be7d22fSVladimir Kondratiev {
9402be7d22fSVladimir Kondratiev 	struct wmi_set_mac_address_cmd cmd;
9412be7d22fSVladimir Kondratiev 
942a82553bbSVladimir Kondratiev 	ether_addr_copy(cmd.mac, addr);
9432be7d22fSVladimir Kondratiev 
9447743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "Set MAC %pM\n", addr);
9452be7d22fSVladimir Kondratiev 
9462be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_SET_MAC_ADDRESS_CMDID, &cmd, sizeof(cmd));
9472be7d22fSVladimir Kondratiev }
9482be7d22fSVladimir Kondratiev 
9498e52fe30SHamad Kadmany int wmi_pcp_start(struct wil6210_priv *wil, int bi, u8 wmi_nettype,
9508e52fe30SHamad Kadmany 		  u8 chan, u8 hidden_ssid)
9512be7d22fSVladimir Kondratiev {
952b8023177SVladimir Kondratiev 	int rc;
953b8023177SVladimir Kondratiev 
954b8023177SVladimir Kondratiev 	struct wmi_pcp_start_cmd cmd = {
9552be7d22fSVladimir Kondratiev 		.bcon_interval = cpu_to_le16(bi),
9562be7d22fSVladimir Kondratiev 		.network_type = wmi_nettype,
9572be7d22fSVladimir Kondratiev 		.disable_sec_offload = 1,
958adc2d122SVladimir Kondratiev 		.channel = chan - 1,
9591eb9d1e5SDedy Lansky 		.pcp_max_assoc_sta = max_assoc_sta,
9608e52fe30SHamad Kadmany 		.hidden_ssid = hidden_ssid,
9612be7d22fSVladimir Kondratiev 	};
962b8023177SVladimir Kondratiev 	struct {
963b8023177SVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
964b8023177SVladimir Kondratiev 		struct wmi_pcp_started_event evt;
965b8023177SVladimir Kondratiev 	} __packed reply;
9662be7d22fSVladimir Kondratiev 
967774974e5SVladimir Kondratiev 	if (!wil->privacy)
9682be7d22fSVladimir Kondratiev 		cmd.disable_sec = 1;
9692be7d22fSVladimir Kondratiev 
9701eb9d1e5SDedy Lansky 	if ((cmd.pcp_max_assoc_sta > WIL6210_MAX_CID) ||
9711eb9d1e5SDedy Lansky 	    (cmd.pcp_max_assoc_sta <= 0)) {
9721eb9d1e5SDedy Lansky 		wil_info(wil,
9731eb9d1e5SDedy Lansky 			 "Requested connection limit %u, valid values are 1 - %d. Setting to %d\n",
9741eb9d1e5SDedy Lansky 			 max_assoc_sta, WIL6210_MAX_CID, WIL6210_MAX_CID);
9751eb9d1e5SDedy Lansky 		cmd.pcp_max_assoc_sta = WIL6210_MAX_CID;
9761eb9d1e5SDedy Lansky 	}
9771eb9d1e5SDedy Lansky 
9788b5c7f6cSVladimir Kondratiev 	/*
9798b5c7f6cSVladimir Kondratiev 	 * Processing time may be huge, in case of secure AP it takes about
9808b5c7f6cSVladimir Kondratiev 	 * 3500ms for FW to start AP
9818b5c7f6cSVladimir Kondratiev 	 */
982b8023177SVladimir Kondratiev 	rc = wmi_call(wil, WMI_PCP_START_CMDID, &cmd, sizeof(cmd),
9838b5c7f6cSVladimir Kondratiev 		      WMI_PCP_STARTED_EVENTID, &reply, sizeof(reply), 5000);
984b8023177SVladimir Kondratiev 	if (rc)
985b8023177SVladimir Kondratiev 		return rc;
986b8023177SVladimir Kondratiev 
987b8023177SVladimir Kondratiev 	if (reply.evt.status != WMI_FW_STATUS_SUCCESS)
988b8023177SVladimir Kondratiev 		rc = -EINVAL;
989b8023177SVladimir Kondratiev 
990b8023177SVladimir Kondratiev 	return rc;
991b8023177SVladimir Kondratiev }
992b8023177SVladimir Kondratiev 
993b8023177SVladimir Kondratiev int wmi_pcp_stop(struct wil6210_priv *wil)
994b8023177SVladimir Kondratiev {
995b8023177SVladimir Kondratiev 	return wmi_call(wil, WMI_PCP_STOP_CMDID, NULL, 0,
996b8023177SVladimir Kondratiev 			WMI_PCP_STOPPED_EVENTID, NULL, 0, 20);
9972be7d22fSVladimir Kondratiev }
9982be7d22fSVladimir Kondratiev 
9992be7d22fSVladimir Kondratiev int wmi_set_ssid(struct wil6210_priv *wil, u8 ssid_len, const void *ssid)
10002be7d22fSVladimir Kondratiev {
10012be7d22fSVladimir Kondratiev 	struct wmi_set_ssid_cmd cmd = {
10022be7d22fSVladimir Kondratiev 		.ssid_len = cpu_to_le32(ssid_len),
10032be7d22fSVladimir Kondratiev 	};
10042be7d22fSVladimir Kondratiev 
10052be7d22fSVladimir Kondratiev 	if (ssid_len > sizeof(cmd.ssid))
10062be7d22fSVladimir Kondratiev 		return -EINVAL;
10072be7d22fSVladimir Kondratiev 
10082be7d22fSVladimir Kondratiev 	memcpy(cmd.ssid, ssid, ssid_len);
10092be7d22fSVladimir Kondratiev 
10102be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_SET_SSID_CMDID, &cmd, sizeof(cmd));
10112be7d22fSVladimir Kondratiev }
10122be7d22fSVladimir Kondratiev 
10132be7d22fSVladimir Kondratiev int wmi_get_ssid(struct wil6210_priv *wil, u8 *ssid_len, void *ssid)
10142be7d22fSVladimir Kondratiev {
10152be7d22fSVladimir Kondratiev 	int rc;
10162be7d22fSVladimir Kondratiev 	struct {
10172be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
10182be7d22fSVladimir Kondratiev 		struct wmi_set_ssid_cmd cmd;
10192be7d22fSVladimir Kondratiev 	} __packed reply;
10202be7d22fSVladimir Kondratiev 	int len; /* reply.cmd.ssid_len in CPU order */
10212be7d22fSVladimir Kondratiev 
10222be7d22fSVladimir Kondratiev 	rc = wmi_call(wil, WMI_GET_SSID_CMDID, NULL, 0, WMI_GET_SSID_EVENTID,
10232be7d22fSVladimir Kondratiev 		      &reply, sizeof(reply), 20);
10242be7d22fSVladimir Kondratiev 	if (rc)
10252be7d22fSVladimir Kondratiev 		return rc;
10262be7d22fSVladimir Kondratiev 
10272be7d22fSVladimir Kondratiev 	len = le32_to_cpu(reply.cmd.ssid_len);
10282be7d22fSVladimir Kondratiev 	if (len > sizeof(reply.cmd.ssid))
10292be7d22fSVladimir Kondratiev 		return -EINVAL;
10302be7d22fSVladimir Kondratiev 
10312be7d22fSVladimir Kondratiev 	*ssid_len = len;
10322be7d22fSVladimir Kondratiev 	memcpy(ssid, reply.cmd.ssid, len);
10332be7d22fSVladimir Kondratiev 
10342be7d22fSVladimir Kondratiev 	return 0;
10352be7d22fSVladimir Kondratiev }
10362be7d22fSVladimir Kondratiev 
10372be7d22fSVladimir Kondratiev int wmi_set_channel(struct wil6210_priv *wil, int channel)
10382be7d22fSVladimir Kondratiev {
10392be7d22fSVladimir Kondratiev 	struct wmi_set_pcp_channel_cmd cmd = {
10402be7d22fSVladimir Kondratiev 		.channel = channel - 1,
10412be7d22fSVladimir Kondratiev 	};
10422be7d22fSVladimir Kondratiev 
10432be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_SET_PCP_CHANNEL_CMDID, &cmd, sizeof(cmd));
10442be7d22fSVladimir Kondratiev }
10452be7d22fSVladimir Kondratiev 
10462be7d22fSVladimir Kondratiev int wmi_get_channel(struct wil6210_priv *wil, int *channel)
10472be7d22fSVladimir Kondratiev {
10482be7d22fSVladimir Kondratiev 	int rc;
10492be7d22fSVladimir Kondratiev 	struct {
10502be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
10512be7d22fSVladimir Kondratiev 		struct wmi_set_pcp_channel_cmd cmd;
10522be7d22fSVladimir Kondratiev 	} __packed reply;
10532be7d22fSVladimir Kondratiev 
10542be7d22fSVladimir Kondratiev 	rc = wmi_call(wil, WMI_GET_PCP_CHANNEL_CMDID, NULL, 0,
10552be7d22fSVladimir Kondratiev 		      WMI_GET_PCP_CHANNEL_EVENTID, &reply, sizeof(reply), 20);
10562be7d22fSVladimir Kondratiev 	if (rc)
10572be7d22fSVladimir Kondratiev 		return rc;
10582be7d22fSVladimir Kondratiev 
10592be7d22fSVladimir Kondratiev 	if (reply.cmd.channel > 3)
10602be7d22fSVladimir Kondratiev 		return -EINVAL;
10612be7d22fSVladimir Kondratiev 
10622be7d22fSVladimir Kondratiev 	*channel = reply.cmd.channel + 1;
10632be7d22fSVladimir Kondratiev 
10642be7d22fSVladimir Kondratiev 	return 0;
10652be7d22fSVladimir Kondratiev }
10662be7d22fSVladimir Kondratiev 
1067b8023177SVladimir Kondratiev int wmi_p2p_cfg(struct wil6210_priv *wil, int channel)
1068b8023177SVladimir Kondratiev {
1069b8023177SVladimir Kondratiev 	struct wmi_p2p_cfg_cmd cmd = {
1070b8023177SVladimir Kondratiev 		.discovery_mode = WMI_DISCOVERY_MODE_NON_OFFLOAD,
1071b8023177SVladimir Kondratiev 		.channel = channel - 1,
1072b8023177SVladimir Kondratiev 	};
1073b8023177SVladimir Kondratiev 
1074b8023177SVladimir Kondratiev 	return wmi_send(wil, WMI_P2P_CFG_CMDID, &cmd, sizeof(cmd));
1075b8023177SVladimir Kondratiev }
1076b8023177SVladimir Kondratiev 
10772be7d22fSVladimir Kondratiev int wmi_del_cipher_key(struct wil6210_priv *wil, u8 key_index,
1078230d8442SVladimir Kondratiev 		       const void *mac_addr, int key_usage)
10792be7d22fSVladimir Kondratiev {
10802be7d22fSVladimir Kondratiev 	struct wmi_delete_cipher_key_cmd cmd = {
10812be7d22fSVladimir Kondratiev 		.key_index = key_index,
10822be7d22fSVladimir Kondratiev 	};
10832be7d22fSVladimir Kondratiev 
10842be7d22fSVladimir Kondratiev 	if (mac_addr)
10852be7d22fSVladimir Kondratiev 		memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
10862be7d22fSVladimir Kondratiev 
10872be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_DELETE_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
10882be7d22fSVladimir Kondratiev }
10892be7d22fSVladimir Kondratiev 
10902be7d22fSVladimir Kondratiev int wmi_add_cipher_key(struct wil6210_priv *wil, u8 key_index,
1091230d8442SVladimir Kondratiev 		       const void *mac_addr, int key_len, const void *key,
1092230d8442SVladimir Kondratiev 		       int key_usage)
10932be7d22fSVladimir Kondratiev {
10942be7d22fSVladimir Kondratiev 	struct wmi_add_cipher_key_cmd cmd = {
10952be7d22fSVladimir Kondratiev 		.key_index = key_index,
1096230d8442SVladimir Kondratiev 		.key_usage = key_usage,
10972be7d22fSVladimir Kondratiev 		.key_len = key_len,
10982be7d22fSVladimir Kondratiev 	};
10992be7d22fSVladimir Kondratiev 
11002be7d22fSVladimir Kondratiev 	if (!key || (key_len > sizeof(cmd.key)))
11012be7d22fSVladimir Kondratiev 		return -EINVAL;
11022be7d22fSVladimir Kondratiev 
11032be7d22fSVladimir Kondratiev 	memcpy(cmd.key, key, key_len);
11042be7d22fSVladimir Kondratiev 	if (mac_addr)
11052be7d22fSVladimir Kondratiev 		memcpy(cmd.mac, mac_addr, WMI_MAC_LEN);
11062be7d22fSVladimir Kondratiev 
11072be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_ADD_CIPHER_KEY_CMDID, &cmd, sizeof(cmd));
11082be7d22fSVladimir Kondratiev }
11092be7d22fSVladimir Kondratiev 
11102be7d22fSVladimir Kondratiev int wmi_set_ie(struct wil6210_priv *wil, u8 type, u16 ie_len, const void *ie)
11112be7d22fSVladimir Kondratiev {
11125421bf0cSVladimir Kondratiev 	static const char *const names[] = {
11135421bf0cSVladimir Kondratiev 		[WMI_FRAME_BEACON]	= "BEACON",
11145421bf0cSVladimir Kondratiev 		[WMI_FRAME_PROBE_REQ]	= "PROBE_REQ",
11155421bf0cSVladimir Kondratiev 		[WMI_FRAME_PROBE_RESP]	= "WMI_FRAME_PROBE_RESP",
11165421bf0cSVladimir Kondratiev 		[WMI_FRAME_ASSOC_REQ]	= "WMI_FRAME_ASSOC_REQ",
11175421bf0cSVladimir Kondratiev 		[WMI_FRAME_ASSOC_RESP]	= "WMI_FRAME_ASSOC_RESP",
11185421bf0cSVladimir Kondratiev 	};
11192be7d22fSVladimir Kondratiev 	int rc;
11202be7d22fSVladimir Kondratiev 	u16 len = sizeof(struct wmi_set_appie_cmd) + ie_len;
11212be7d22fSVladimir Kondratiev 	struct wmi_set_appie_cmd *cmd = kzalloc(len, GFP_KERNEL);
11228fe59627SVladimir Kondratiev 
11235421bf0cSVladimir Kondratiev 	if (!cmd) {
11245421bf0cSVladimir Kondratiev 		rc = -ENOMEM;
11255421bf0cSVladimir Kondratiev 		goto out;
11265421bf0cSVladimir Kondratiev 	}
1127ac4acdb7SVladimir Kondratiev 	if (!ie)
1128ac4acdb7SVladimir Kondratiev 		ie_len = 0;
11292be7d22fSVladimir Kondratiev 
11302be7d22fSVladimir Kondratiev 	cmd->mgmt_frm_type = type;
11312be7d22fSVladimir Kondratiev 	/* BUG: FW API define ieLen as u8. Will fix FW */
11322be7d22fSVladimir Kondratiev 	cmd->ie_len = cpu_to_le16(ie_len);
11332be7d22fSVladimir Kondratiev 	memcpy(cmd->ie_info, ie, ie_len);
113403866e7dSVladimir Kondratiev 	rc = wmi_send(wil, WMI_SET_APPIE_CMDID, cmd, len);
11352be7d22fSVladimir Kondratiev 	kfree(cmd);
11365421bf0cSVladimir Kondratiev out:
11375421bf0cSVladimir Kondratiev 	if (rc) {
11385421bf0cSVladimir Kondratiev 		const char *name = type < ARRAY_SIZE(names) ?
11395421bf0cSVladimir Kondratiev 				   names[type] : "??";
11405421bf0cSVladimir Kondratiev 		wil_err(wil, "set_ie(%d %s) failed : %d\n", type, name, rc);
11415421bf0cSVladimir Kondratiev 	}
11422be7d22fSVladimir Kondratiev 
11432be7d22fSVladimir Kondratiev 	return rc;
11442be7d22fSVladimir Kondratiev }
11452be7d22fSVladimir Kondratiev 
11461647f12fSVladimir Kondratiev /**
11471647f12fSVladimir Kondratiev  * wmi_rxon - turn radio on/off
11481647f12fSVladimir Kondratiev  * @on:		turn on if true, off otherwise
11491647f12fSVladimir Kondratiev  *
11501647f12fSVladimir Kondratiev  * Only switch radio. Channel should be set separately.
11511647f12fSVladimir Kondratiev  * No timeout for rxon - radio turned on forever unless some other call
11521647f12fSVladimir Kondratiev  * turns it off
11531647f12fSVladimir Kondratiev  */
11541647f12fSVladimir Kondratiev int wmi_rxon(struct wil6210_priv *wil, bool on)
11551647f12fSVladimir Kondratiev {
11561647f12fSVladimir Kondratiev 	int rc;
11571647f12fSVladimir Kondratiev 	struct {
11581647f12fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
11591647f12fSVladimir Kondratiev 		struct wmi_listen_started_event evt;
11601647f12fSVladimir Kondratiev 	} __packed reply;
11611647f12fSVladimir Kondratiev 
11621647f12fSVladimir Kondratiev 	wil_info(wil, "%s(%s)\n", __func__, on ? "on" : "off");
11631647f12fSVladimir Kondratiev 
11641647f12fSVladimir Kondratiev 	if (on) {
11651647f12fSVladimir Kondratiev 		rc = wmi_call(wil, WMI_START_LISTEN_CMDID, NULL, 0,
11661647f12fSVladimir Kondratiev 			      WMI_LISTEN_STARTED_EVENTID,
11671647f12fSVladimir Kondratiev 			      &reply, sizeof(reply), 100);
11681647f12fSVladimir Kondratiev 		if ((rc == 0) && (reply.evt.status != WMI_FW_STATUS_SUCCESS))
11691647f12fSVladimir Kondratiev 			rc = -EINVAL;
11701647f12fSVladimir Kondratiev 	} else {
11711647f12fSVladimir Kondratiev 		rc = wmi_call(wil, WMI_DISCOVERY_STOP_CMDID, NULL, 0,
11721647f12fSVladimir Kondratiev 			      WMI_DISCOVERY_STOPPED_EVENTID, NULL, 0, 20);
11731647f12fSVladimir Kondratiev 	}
11741647f12fSVladimir Kondratiev 
11751647f12fSVladimir Kondratiev 	return rc;
11761647f12fSVladimir Kondratiev }
11771647f12fSVladimir Kondratiev 
117847e19af9SVladimir Kondratiev int wmi_rx_chain_add(struct wil6210_priv *wil, struct vring *vring)
117947e19af9SVladimir Kondratiev {
118047e19af9SVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
118147e19af9SVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
118247e19af9SVladimir Kondratiev 	struct wmi_cfg_rx_chain_cmd cmd = {
118347e19af9SVladimir Kondratiev 		.action = WMI_RX_CHAIN_ADD,
118447e19af9SVladimir Kondratiev 		.rx_sw_ring = {
1185c44690a1SVladimir Kondratiev 			.max_mpdu_size = cpu_to_le16(wil_mtu2macbuf(mtu_max)),
118647e19af9SVladimir Kondratiev 			.ring_mem_base = cpu_to_le64(vring->pa),
118747e19af9SVladimir Kondratiev 			.ring_size = cpu_to_le16(vring->size),
118847e19af9SVladimir Kondratiev 		},
118947e19af9SVladimir Kondratiev 		.mid = 0, /* TODO - what is it? */
119047e19af9SVladimir Kondratiev 		.decap_trans_type = WMI_DECAP_TYPE_802_3,
1191b4490f42SVladimir Kondratiev 		.reorder_type = WMI_RX_SW_REORDER,
1192ab954628SVladimir Kondratiev 		.host_thrsh = cpu_to_le16(rx_ring_overflow_thrsh),
119347e19af9SVladimir Kondratiev 	};
119447e19af9SVladimir Kondratiev 	struct {
119547e19af9SVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
119647e19af9SVladimir Kondratiev 		struct wmi_cfg_rx_chain_done_event evt;
119747e19af9SVladimir Kondratiev 	} __packed evt;
119847e19af9SVladimir Kondratiev 	int rc;
119947e19af9SVladimir Kondratiev 
120047e19af9SVladimir Kondratiev 	if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
120147e19af9SVladimir Kondratiev 		struct ieee80211_channel *ch = wdev->preset_chandef.chan;
120247e19af9SVladimir Kondratiev 
120347e19af9SVladimir Kondratiev 		cmd.sniffer_cfg.mode = cpu_to_le32(WMI_SNIFFER_ON);
120447e19af9SVladimir Kondratiev 		if (ch)
120547e19af9SVladimir Kondratiev 			cmd.sniffer_cfg.channel = ch->hw_value - 1;
120647e19af9SVladimir Kondratiev 		cmd.sniffer_cfg.phy_info_mode =
120747e19af9SVladimir Kondratiev 			cpu_to_le32(ndev->type == ARPHRD_IEEE80211_RADIOTAP);
120847e19af9SVladimir Kondratiev 		cmd.sniffer_cfg.phy_support =
120947e19af9SVladimir Kondratiev 			cpu_to_le32((wil->monitor_flags & MONITOR_FLAG_CONTROL)
12104765332dSVladimir Kondratiev 				    ? WMI_SNIFFER_CP : WMI_SNIFFER_BOTH_PHYS);
1211504937d4SKirshenbaum Erez 	} else {
1212504937d4SKirshenbaum Erez 		/* Initialize offload (in non-sniffer mode).
1213504937d4SKirshenbaum Erez 		 * Linux IP stack always calculates IP checksum
1214504937d4SKirshenbaum Erez 		 * HW always calculate TCP/UDP checksum
1215504937d4SKirshenbaum Erez 		 */
1216504937d4SKirshenbaum Erez 		cmd.l3_l4_ctrl |= (1 << L3_L4_CTRL_TCPIP_CHECKSUM_EN_POS);
121747e19af9SVladimir Kondratiev 	}
1218c406ea7cSVladimir Kondratiev 
1219c406ea7cSVladimir Kondratiev 	if (rx_align_2)
1220c406ea7cSVladimir Kondratiev 		cmd.l2_802_3_offload_ctrl |=
1221c406ea7cSVladimir Kondratiev 				L2_802_3_OFFLOAD_CTRL_SNAP_KEEP_MSK;
1222c406ea7cSVladimir Kondratiev 
122347e19af9SVladimir Kondratiev 	/* typical time for secure PCP is 840ms */
122447e19af9SVladimir Kondratiev 	rc = wmi_call(wil, WMI_CFG_RX_CHAIN_CMDID, &cmd, sizeof(cmd),
122547e19af9SVladimir Kondratiev 		      WMI_CFG_RX_CHAIN_DONE_EVENTID, &evt, sizeof(evt), 2000);
122647e19af9SVladimir Kondratiev 	if (rc)
122747e19af9SVladimir Kondratiev 		return rc;
122847e19af9SVladimir Kondratiev 
122947e19af9SVladimir Kondratiev 	vring->hwtail = le32_to_cpu(evt.evt.rx_ring_tail_ptr);
123047e19af9SVladimir Kondratiev 
12317743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "Rx init: status %d tail 0x%08x\n",
123247e19af9SVladimir Kondratiev 		     le32_to_cpu(evt.evt.status), vring->hwtail);
123347e19af9SVladimir Kondratiev 
123447e19af9SVladimir Kondratiev 	if (le32_to_cpu(evt.evt.status) != WMI_CFG_RX_CHAIN_SUCCESS)
123547e19af9SVladimir Kondratiev 		rc = -EINVAL;
123647e19af9SVladimir Kondratiev 
123747e19af9SVladimir Kondratiev 	return rc;
123847e19af9SVladimir Kondratiev }
123947e19af9SVladimir Kondratiev 
12408c679675SVladimir Kondratiev int wmi_get_temperature(struct wil6210_priv *wil, u32 *t_bb, u32 *t_rf)
12411a2780e0SVladimir Kondratiev {
12421a2780e0SVladimir Kondratiev 	int rc;
12431a2780e0SVladimir Kondratiev 	struct wmi_temp_sense_cmd cmd = {
12448c679675SVladimir Kondratiev 		.measure_baseband_en = cpu_to_le32(!!t_bb),
12458c679675SVladimir Kondratiev 		.measure_rf_en = cpu_to_le32(!!t_rf),
12468c679675SVladimir Kondratiev 		.measure_mode = cpu_to_le32(TEMPERATURE_MEASURE_NOW),
12471a2780e0SVladimir Kondratiev 	};
12481a2780e0SVladimir Kondratiev 	struct {
12491a2780e0SVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
12501a2780e0SVladimir Kondratiev 		struct wmi_temp_sense_done_event evt;
12511a2780e0SVladimir Kondratiev 	} __packed reply;
12521a2780e0SVladimir Kondratiev 
12531a2780e0SVladimir Kondratiev 	rc = wmi_call(wil, WMI_TEMP_SENSE_CMDID, &cmd, sizeof(cmd),
12541a2780e0SVladimir Kondratiev 		      WMI_TEMP_SENSE_DONE_EVENTID, &reply, sizeof(reply), 100);
12551a2780e0SVladimir Kondratiev 	if (rc)
12561a2780e0SVladimir Kondratiev 		return rc;
12571a2780e0SVladimir Kondratiev 
12588c679675SVladimir Kondratiev 	if (t_bb)
12598c679675SVladimir Kondratiev 		*t_bb = le32_to_cpu(reply.evt.baseband_t1000);
12608c679675SVladimir Kondratiev 	if (t_rf)
12618c679675SVladimir Kondratiev 		*t_rf = le32_to_cpu(reply.evt.rf_t1000);
12621a2780e0SVladimir Kondratiev 
12631a2780e0SVladimir Kondratiev 	return 0;
12641a2780e0SVladimir Kondratiev }
12651a2780e0SVladimir Kondratiev 
12660916d9f2SMaya Erez int wmi_disconnect_sta(struct wil6210_priv *wil, const u8 *mac, u16 reason,
12670916d9f2SMaya Erez 		       bool full_disconnect)
12684d55a0a1SVladimir Kondratiev {
12693e9191fcSVladimir Kondratiev 	int rc;
12703e9191fcSVladimir Kondratiev 	u16 reason_code;
12714d55a0a1SVladimir Kondratiev 	struct wmi_disconnect_sta_cmd cmd = {
12724d55a0a1SVladimir Kondratiev 		.disconnect_reason = cpu_to_le16(reason),
12734d55a0a1SVladimir Kondratiev 	};
12743e9191fcSVladimir Kondratiev 	struct {
12753e9191fcSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
12763e9191fcSVladimir Kondratiev 		struct wmi_disconnect_event evt;
12773e9191fcSVladimir Kondratiev 	} __packed reply;
1278a82553bbSVladimir Kondratiev 
1279a82553bbSVladimir Kondratiev 	ether_addr_copy(cmd.dst_mac, mac);
12804d55a0a1SVladimir Kondratiev 
12814d55a0a1SVladimir Kondratiev 	wil_dbg_wmi(wil, "%s(%pM, reason %d)\n", __func__, mac, reason);
12824d55a0a1SVladimir Kondratiev 
12833e9191fcSVladimir Kondratiev 	rc = wmi_call(wil, WMI_DISCONNECT_STA_CMDID, &cmd, sizeof(cmd),
12843e9191fcSVladimir Kondratiev 		      WMI_DISCONNECT_EVENTID, &reply, sizeof(reply), 1000);
12853e9191fcSVladimir Kondratiev 	/* failure to disconnect in reasonable time treated as FW error */
12863e9191fcSVladimir Kondratiev 	if (rc) {
12873e9191fcSVladimir Kondratiev 		wil_fw_error_recovery(wil);
12883e9191fcSVladimir Kondratiev 		return rc;
12893e9191fcSVladimir Kondratiev 	}
12903e9191fcSVladimir Kondratiev 
12910916d9f2SMaya Erez 	if (full_disconnect) {
12923e9191fcSVladimir Kondratiev 		/* call event handler manually after processing wmi_call,
12930916d9f2SMaya Erez 		 * to avoid deadlock - disconnect event handler acquires
12940916d9f2SMaya Erez 		 * wil->mutex while it is already held here
12953e9191fcSVladimir Kondratiev 		 */
12963e9191fcSVladimir Kondratiev 		reason_code = le16_to_cpu(reply.evt.protocol_reason_status);
12973e9191fcSVladimir Kondratiev 
12983e9191fcSVladimir Kondratiev 		wil_dbg_wmi(wil, "Disconnect %pM reason [proto %d wmi %d]\n",
12993e9191fcSVladimir Kondratiev 			    reply.evt.bssid, reason_code,
13003e9191fcSVladimir Kondratiev 			    reply.evt.disconnect_reason);
13013e9191fcSVladimir Kondratiev 
13023e9191fcSVladimir Kondratiev 		wil->sinfo_gen++;
13033e9191fcSVladimir Kondratiev 		wil6210_disconnect(wil, reply.evt.bssid, reason_code, true);
13040916d9f2SMaya Erez 	}
13053e9191fcSVladimir Kondratiev 	return 0;
13064d55a0a1SVladimir Kondratiev }
13074d55a0a1SVladimir Kondratiev 
13083277213fSVladimir Kondratiev int wmi_addba(struct wil6210_priv *wil, u8 ringid, u8 size, u16 timeout)
13093277213fSVladimir Kondratiev {
13103277213fSVladimir Kondratiev 	struct wmi_vring_ba_en_cmd cmd = {
13113277213fSVladimir Kondratiev 		.ringid = ringid,
13123277213fSVladimir Kondratiev 		.agg_max_wsize = size,
13133277213fSVladimir Kondratiev 		.ba_timeout = cpu_to_le16(timeout),
1314cbcf5866SVladimir Kondratiev 		.amsdu = 0,
13153277213fSVladimir Kondratiev 	};
13163277213fSVladimir Kondratiev 
13173277213fSVladimir Kondratiev 	wil_dbg_wmi(wil, "%s(ring %d size %d timeout %d)\n", __func__,
13183277213fSVladimir Kondratiev 		    ringid, size, timeout);
13193277213fSVladimir Kondratiev 
13203277213fSVladimir Kondratiev 	return wmi_send(wil, WMI_VRING_BA_EN_CMDID, &cmd, sizeof(cmd));
13213277213fSVladimir Kondratiev }
13223277213fSVladimir Kondratiev 
132326a359d9SVladimir Kondratiev int wmi_delba_tx(struct wil6210_priv *wil, u8 ringid, u16 reason)
13243277213fSVladimir Kondratiev {
13253277213fSVladimir Kondratiev 	struct wmi_vring_ba_dis_cmd cmd = {
13263277213fSVladimir Kondratiev 		.ringid = ringid,
13273277213fSVladimir Kondratiev 		.reason = cpu_to_le16(reason),
13283277213fSVladimir Kondratiev 	};
13293277213fSVladimir Kondratiev 
13303277213fSVladimir Kondratiev 	wil_dbg_wmi(wil, "%s(ring %d reason %d)\n", __func__,
13313277213fSVladimir Kondratiev 		    ringid, reason);
13323277213fSVladimir Kondratiev 
13333277213fSVladimir Kondratiev 	return wmi_send(wil, WMI_VRING_BA_DIS_CMDID, &cmd, sizeof(cmd));
13343277213fSVladimir Kondratiev }
13353277213fSVladimir Kondratiev 
133626a359d9SVladimir Kondratiev int wmi_delba_rx(struct wil6210_priv *wil, u8 cidxtid, u16 reason)
133726a359d9SVladimir Kondratiev {
133826a359d9SVladimir Kondratiev 	struct wmi_rcp_delba_cmd cmd = {
133926a359d9SVladimir Kondratiev 		.cidxtid = cidxtid,
134026a359d9SVladimir Kondratiev 		.reason = cpu_to_le16(reason),
134126a359d9SVladimir Kondratiev 	};
134226a359d9SVladimir Kondratiev 
134326a359d9SVladimir Kondratiev 	wil_dbg_wmi(wil, "%s(CID %d TID %d reason %d)\n", __func__,
134426a359d9SVladimir Kondratiev 		    cidxtid & 0xf, (cidxtid >> 4) & 0xf, reason);
134526a359d9SVladimir Kondratiev 
134626a359d9SVladimir Kondratiev 	return wmi_send(wil, WMI_RCP_DELBA_CMDID, &cmd, sizeof(cmd));
134726a359d9SVladimir Kondratiev }
134826a359d9SVladimir Kondratiev 
13493277213fSVladimir Kondratiev int wmi_addba_rx_resp(struct wil6210_priv *wil, u8 cid, u8 tid, u8 token,
13503277213fSVladimir Kondratiev 		      u16 status, bool amsdu, u16 agg_wsize, u16 timeout)
13513277213fSVladimir Kondratiev {
13523277213fSVladimir Kondratiev 	int rc;
13533277213fSVladimir Kondratiev 	struct wmi_rcp_addba_resp_cmd cmd = {
13543277213fSVladimir Kondratiev 		.cidxtid = mk_cidxtid(cid, tid),
13553277213fSVladimir Kondratiev 		.dialog_token = token,
13563277213fSVladimir Kondratiev 		.status_code = cpu_to_le16(status),
13573277213fSVladimir Kondratiev 		/* bit 0: A-MSDU supported
13583277213fSVladimir Kondratiev 		 * bit 1: policy (should be 0 for us)
13593277213fSVladimir Kondratiev 		 * bits 2..5: TID
13603277213fSVladimir Kondratiev 		 * bits 6..15: buffer size
13613277213fSVladimir Kondratiev 		 */
13623277213fSVladimir Kondratiev 		.ba_param_set = cpu_to_le16((amsdu ? 1 : 0) | (tid << 2) |
13633277213fSVladimir Kondratiev 					    (agg_wsize << 6)),
13643277213fSVladimir Kondratiev 		.ba_timeout = cpu_to_le16(timeout),
13653277213fSVladimir Kondratiev 	};
13663277213fSVladimir Kondratiev 	struct {
13673277213fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
13683277213fSVladimir Kondratiev 		struct wmi_rcp_addba_resp_sent_event evt;
13693277213fSVladimir Kondratiev 	} __packed reply;
13703277213fSVladimir Kondratiev 
13713277213fSVladimir Kondratiev 	wil_dbg_wmi(wil,
13723277213fSVladimir Kondratiev 		    "ADDBA response for CID %d TID %d size %d timeout %d status %d AMSDU%s\n",
13733277213fSVladimir Kondratiev 		    cid, tid, agg_wsize, timeout, status, amsdu ? "+" : "-");
13743277213fSVladimir Kondratiev 
13753277213fSVladimir Kondratiev 	rc = wmi_call(wil, WMI_RCP_ADDBA_RESP_CMDID, &cmd, sizeof(cmd),
1376230d8442SVladimir Kondratiev 		      WMI_RCP_ADDBA_RESP_SENT_EVENTID, &reply, sizeof(reply),
1377230d8442SVladimir Kondratiev 		      100);
13783277213fSVladimir Kondratiev 	if (rc)
13793277213fSVladimir Kondratiev 		return rc;
13803277213fSVladimir Kondratiev 
13813277213fSVladimir Kondratiev 	if (reply.evt.status) {
13823277213fSVladimir Kondratiev 		wil_err(wil, "ADDBA response failed with status %d\n",
13833277213fSVladimir Kondratiev 			le16_to_cpu(reply.evt.status));
13843277213fSVladimir Kondratiev 		rc = -EINVAL;
13853277213fSVladimir Kondratiev 	}
13863277213fSVladimir Kondratiev 
13873277213fSVladimir Kondratiev 	return rc;
13883277213fSVladimir Kondratiev }
13893277213fSVladimir Kondratiev 
13902be7d22fSVladimir Kondratiev void wmi_event_flush(struct wil6210_priv *wil)
13912be7d22fSVladimir Kondratiev {
13922be7d22fSVladimir Kondratiev 	struct pending_wmi_event *evt, *t;
13932be7d22fSVladimir Kondratiev 
13947743882dSVladimir Kondratiev 	wil_dbg_wmi(wil, "%s()\n", __func__);
13952be7d22fSVladimir Kondratiev 
13962be7d22fSVladimir Kondratiev 	list_for_each_entry_safe(evt, t, &wil->pending_wmi_ev, list) {
13972be7d22fSVladimir Kondratiev 		list_del(&evt->list);
13982be7d22fSVladimir Kondratiev 		kfree(evt);
13992be7d22fSVladimir Kondratiev 	}
14002be7d22fSVladimir Kondratiev }
14012be7d22fSVladimir Kondratiev 
14022be7d22fSVladimir Kondratiev static bool wmi_evt_call_handler(struct wil6210_priv *wil, int id,
14032be7d22fSVladimir Kondratiev 				 void *d, int len)
14042be7d22fSVladimir Kondratiev {
14052be7d22fSVladimir Kondratiev 	uint i;
14062be7d22fSVladimir Kondratiev 
14072be7d22fSVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wmi_evt_handlers); i++) {
14082be7d22fSVladimir Kondratiev 		if (wmi_evt_handlers[i].eventid == id) {
14092be7d22fSVladimir Kondratiev 			wmi_evt_handlers[i].handler(wil, id, d, len);
14102be7d22fSVladimir Kondratiev 			return true;
14112be7d22fSVladimir Kondratiev 		}
14122be7d22fSVladimir Kondratiev 	}
14132be7d22fSVladimir Kondratiev 
14142be7d22fSVladimir Kondratiev 	return false;
14152be7d22fSVladimir Kondratiev }
14162be7d22fSVladimir Kondratiev 
14172be7d22fSVladimir Kondratiev static void wmi_event_handle(struct wil6210_priv *wil,
14182be7d22fSVladimir Kondratiev 			     struct wil6210_mbox_hdr *hdr)
14192be7d22fSVladimir Kondratiev {
14202be7d22fSVladimir Kondratiev 	u16 len = le16_to_cpu(hdr->len);
14212be7d22fSVladimir Kondratiev 
14222be7d22fSVladimir Kondratiev 	if ((hdr->type == WIL_MBOX_HDR_TYPE_WMI) &&
14232be7d22fSVladimir Kondratiev 	    (len >= sizeof(struct wil6210_mbox_hdr_wmi))) {
14242be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi *wmi = (void *)(&hdr[1]);
14252be7d22fSVladimir Kondratiev 		void *evt_data = (void *)(&wmi[1]);
14262be7d22fSVladimir Kondratiev 		u16 id = le16_to_cpu(wmi->id);
1427028e1836SVladimir Kondratiev 
1428028e1836SVladimir Kondratiev 		wil_dbg_wmi(wil, "Handle WMI 0x%04x (reply_id 0x%04x)\n",
1429028e1836SVladimir Kondratiev 			    id, wil->reply_id);
14302be7d22fSVladimir Kondratiev 		/* check if someone waits for this event */
14312be7d22fSVladimir Kondratiev 		if (wil->reply_id && wil->reply_id == id) {
14320916d9f2SMaya Erez 			WARN_ON(wil->reply_buf);
14332be7d22fSVladimir Kondratiev 			wmi_evt_call_handler(wil, id, evt_data,
14342be7d22fSVladimir Kondratiev 					     len - sizeof(*wmi));
14350916d9f2SMaya Erez 			wil_dbg_wmi(wil, "%s: Complete WMI 0x%04x\n",
14360916d9f2SMaya Erez 				    __func__, id);
143759502647SDedy Lansky 			complete(&wil->wmi_call);
14382be7d22fSVladimir Kondratiev 			return;
14392be7d22fSVladimir Kondratiev 		}
14402be7d22fSVladimir Kondratiev 		/* unsolicited event */
14412be7d22fSVladimir Kondratiev 		/* search for handler */
14422be7d22fSVladimir Kondratiev 		if (!wmi_evt_call_handler(wil, id, evt_data,
14432be7d22fSVladimir Kondratiev 					  len - sizeof(*wmi))) {
1444a3ce5ccdSDedy Lansky 			wil_info(wil, "Unhandled event 0x%04x\n", id);
14452be7d22fSVladimir Kondratiev 		}
14462be7d22fSVladimir Kondratiev 	} else {
14472be7d22fSVladimir Kondratiev 		wil_err(wil, "Unknown event type\n");
14482be7d22fSVladimir Kondratiev 		print_hex_dump(KERN_ERR, "evt?? ", DUMP_PREFIX_OFFSET, 16, 1,
14492be7d22fSVladimir Kondratiev 			       hdr, sizeof(*hdr) + len, true);
14502be7d22fSVladimir Kondratiev 	}
14512be7d22fSVladimir Kondratiev }
14522be7d22fSVladimir Kondratiev 
14532be7d22fSVladimir Kondratiev /*
14542be7d22fSVladimir Kondratiev  * Retrieve next WMI event from the pending list
14552be7d22fSVladimir Kondratiev  */
14562be7d22fSVladimir Kondratiev static struct list_head *next_wmi_ev(struct wil6210_priv *wil)
14572be7d22fSVladimir Kondratiev {
14582be7d22fSVladimir Kondratiev 	ulong flags;
14592be7d22fSVladimir Kondratiev 	struct list_head *ret = NULL;
14602be7d22fSVladimir Kondratiev 
14612be7d22fSVladimir Kondratiev 	spin_lock_irqsave(&wil->wmi_ev_lock, flags);
14622be7d22fSVladimir Kondratiev 
14632be7d22fSVladimir Kondratiev 	if (!list_empty(&wil->pending_wmi_ev)) {
14642be7d22fSVladimir Kondratiev 		ret = wil->pending_wmi_ev.next;
14652be7d22fSVladimir Kondratiev 		list_del(ret);
14662be7d22fSVladimir Kondratiev 	}
14672be7d22fSVladimir Kondratiev 
14682be7d22fSVladimir Kondratiev 	spin_unlock_irqrestore(&wil->wmi_ev_lock, flags);
14692be7d22fSVladimir Kondratiev 
14702be7d22fSVladimir Kondratiev 	return ret;
14712be7d22fSVladimir Kondratiev }
14722be7d22fSVladimir Kondratiev 
14732be7d22fSVladimir Kondratiev /*
14742be7d22fSVladimir Kondratiev  * Handler for the WMI events
14752be7d22fSVladimir Kondratiev  */
14762be7d22fSVladimir Kondratiev void wmi_event_worker(struct work_struct *work)
14772be7d22fSVladimir Kondratiev {
14782be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
14792be7d22fSVladimir Kondratiev 						 wmi_event_worker);
14802be7d22fSVladimir Kondratiev 	struct pending_wmi_event *evt;
14812be7d22fSVladimir Kondratiev 	struct list_head *lh;
14822be7d22fSVladimir Kondratiev 
1483028e1836SVladimir Kondratiev 	wil_dbg_wmi(wil, "Start %s\n", __func__);
14842be7d22fSVladimir Kondratiev 	while ((lh = next_wmi_ev(wil)) != NULL) {
14852be7d22fSVladimir Kondratiev 		evt = list_entry(lh, struct pending_wmi_event, list);
14862be7d22fSVladimir Kondratiev 		wmi_event_handle(wil, &evt->event.hdr);
14872be7d22fSVladimir Kondratiev 		kfree(evt);
14882be7d22fSVladimir Kondratiev 	}
1489028e1836SVladimir Kondratiev 	wil_dbg_wmi(wil, "Finished %s\n", __func__);
14902be7d22fSVladimir Kondratiev }
1491