12be7d22fSVladimir Kondratiev /*
22be7d22fSVladimir Kondratiev  * Copyright (c) 2012 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 
172be7d22fSVladimir Kondratiev #include <linux/moduleparam.h>
182be7d22fSVladimir Kondratiev #include <linux/if_arp.h>
192be7d22fSVladimir Kondratiev 
202be7d22fSVladimir Kondratiev #include "wil6210.h"
212be7d22fSVladimir Kondratiev 
222be7d22fSVladimir Kondratiev /*
232be7d22fSVladimir Kondratiev  * Due to a hardware issue,
242be7d22fSVladimir Kondratiev  * one has to read/write to/from NIC in 32-bit chunks;
252be7d22fSVladimir Kondratiev  * regular memcpy_fromio and siblings will
262be7d22fSVladimir Kondratiev  * not work on 64-bit platform - it uses 64-bit transactions
272be7d22fSVladimir Kondratiev  *
282be7d22fSVladimir Kondratiev  * Force 32-bit transactions to enable NIC on 64-bit platforms
292be7d22fSVladimir Kondratiev  *
302be7d22fSVladimir Kondratiev  * To avoid byte swap on big endian host, __raw_{read|write}l
312be7d22fSVladimir Kondratiev  * should be used - {read|write}l would swap bytes to provide
322be7d22fSVladimir Kondratiev  * little endian on PCI value in host endianness.
332be7d22fSVladimir Kondratiev  */
342be7d22fSVladimir Kondratiev void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
352be7d22fSVladimir Kondratiev 			  size_t count)
362be7d22fSVladimir Kondratiev {
372be7d22fSVladimir Kondratiev 	u32 *d = dst;
382be7d22fSVladimir Kondratiev 	const volatile u32 __iomem *s = src;
392be7d22fSVladimir Kondratiev 
402be7d22fSVladimir Kondratiev 	/* size_t is unsigned, if (count%4 != 0) it will wrap */
412be7d22fSVladimir Kondratiev 	for (count += 4; count > 4; count -= 4)
422be7d22fSVladimir Kondratiev 		*d++ = __raw_readl(s++);
432be7d22fSVladimir Kondratiev }
442be7d22fSVladimir Kondratiev 
452be7d22fSVladimir Kondratiev void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
462be7d22fSVladimir Kondratiev 			size_t count)
472be7d22fSVladimir Kondratiev {
482be7d22fSVladimir Kondratiev 	volatile u32 __iomem *d = dst;
492be7d22fSVladimir Kondratiev 	const u32 *s = src;
502be7d22fSVladimir Kondratiev 
512be7d22fSVladimir Kondratiev 	for (count += 4; count > 4; count -= 4)
522be7d22fSVladimir Kondratiev 		__raw_writel(*s++, d++);
532be7d22fSVladimir Kondratiev }
542be7d22fSVladimir Kondratiev 
552be7d22fSVladimir Kondratiev static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
562be7d22fSVladimir Kondratiev {
572be7d22fSVladimir Kondratiev 	uint i;
582be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
592be7d22fSVladimir Kondratiev 
607743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "%s()\n", __func__);
612be7d22fSVladimir Kondratiev 
622be7d22fSVladimir Kondratiev 	wil_link_off(wil);
63b338f74eSVladimir Kondratiev 	if (test_bit(wil_status_fwconnected, &wil->status)) {
642be7d22fSVladimir Kondratiev 		clear_bit(wil_status_fwconnected, &wil->status);
65b338f74eSVladimir Kondratiev 		cfg80211_disconnected(ndev,
66b338f74eSVladimir Kondratiev 				      WLAN_STATUS_UNSPECIFIED_FAILURE,
672be7d22fSVladimir Kondratiev 				      NULL, 0, GFP_KERNEL);
68b338f74eSVladimir Kondratiev 	} else if (test_bit(wil_status_fwconnecting, &wil->status)) {
692be7d22fSVladimir Kondratiev 		cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
702be7d22fSVladimir Kondratiev 					WLAN_STATUS_UNSPECIFIED_FAILURE,
712be7d22fSVladimir Kondratiev 					GFP_KERNEL);
722be7d22fSVladimir Kondratiev 	}
73b338f74eSVladimir Kondratiev 	clear_bit(wil_status_fwconnecting, &wil->status);
742be7d22fSVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++)
752be7d22fSVladimir Kondratiev 		wil_vring_fini_tx(wil, i);
76b98917d7SVladimir Kondratiev 
77b98917d7SVladimir Kondratiev 	clear_bit(wil_status_dontscan, &wil->status);
782be7d22fSVladimir Kondratiev }
792be7d22fSVladimir Kondratiev 
802be7d22fSVladimir Kondratiev static void wil_disconnect_worker(struct work_struct *work)
812be7d22fSVladimir Kondratiev {
822be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = container_of(work,
832be7d22fSVladimir Kondratiev 			struct wil6210_priv, disconnect_worker);
842be7d22fSVladimir Kondratiev 
852be7d22fSVladimir Kondratiev 	_wil6210_disconnect(wil, NULL);
862be7d22fSVladimir Kondratiev }
872be7d22fSVladimir Kondratiev 
882be7d22fSVladimir Kondratiev static void wil_connect_timer_fn(ulong x)
892be7d22fSVladimir Kondratiev {
902be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = (void *)x;
912be7d22fSVladimir Kondratiev 
927743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "Connect timeout\n");
932be7d22fSVladimir Kondratiev 
942be7d22fSVladimir Kondratiev 	/* reschedule to thread context - disconnect won't
952be7d22fSVladimir Kondratiev 	 * run from atomic context
962be7d22fSVladimir Kondratiev 	 */
972be7d22fSVladimir Kondratiev 	schedule_work(&wil->disconnect_worker);
982be7d22fSVladimir Kondratiev }
992be7d22fSVladimir Kondratiev 
100d81079f1SVladimir Kondratiev static void wil_connect_worker(struct work_struct *work)
101d81079f1SVladimir Kondratiev {
102d81079f1SVladimir Kondratiev 	int rc;
103d81079f1SVladimir Kondratiev 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
104d81079f1SVladimir Kondratiev 						connect_worker);
105d81079f1SVladimir Kondratiev 	int cid = wil->pending_connect_cid;
106d81079f1SVladimir Kondratiev 
107d81079f1SVladimir Kondratiev 	if (cid < 0) {
108d81079f1SVladimir Kondratiev 		wil_err(wil, "No connection pending\n");
109d81079f1SVladimir Kondratiev 		return;
110d81079f1SVladimir Kondratiev 	}
111d81079f1SVladimir Kondratiev 
112d81079f1SVladimir Kondratiev 	wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
113d81079f1SVladimir Kondratiev 
114d81079f1SVladimir Kondratiev 	rc = wil_vring_init_tx(wil, 0, WIL6210_TX_RING_SIZE, cid, 0);
115d81079f1SVladimir Kondratiev 	wil->pending_connect_cid = -1;
116d81079f1SVladimir Kondratiev 	if (rc == 0)
117d81079f1SVladimir Kondratiev 		wil_link_on(wil);
118d81079f1SVladimir Kondratiev }
119d81079f1SVladimir Kondratiev 
1202be7d22fSVladimir Kondratiev int wil_priv_init(struct wil6210_priv *wil)
1212be7d22fSVladimir Kondratiev {
1227743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "%s()\n", __func__);
1232be7d22fSVladimir Kondratiev 
1242be7d22fSVladimir Kondratiev 	mutex_init(&wil->mutex);
1252be7d22fSVladimir Kondratiev 	mutex_init(&wil->wmi_mutex);
1262be7d22fSVladimir Kondratiev 
1272be7d22fSVladimir Kondratiev 	init_completion(&wil->wmi_ready);
1282be7d22fSVladimir Kondratiev 
1292be7d22fSVladimir Kondratiev 	wil->pending_connect_cid = -1;
1302be7d22fSVladimir Kondratiev 	setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
1312be7d22fSVladimir Kondratiev 
132d81079f1SVladimir Kondratiev 	INIT_WORK(&wil->connect_worker, wil_connect_worker);
1332be7d22fSVladimir Kondratiev 	INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
1342be7d22fSVladimir Kondratiev 	INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
1352be7d22fSVladimir Kondratiev 
1362be7d22fSVladimir Kondratiev 	INIT_LIST_HEAD(&wil->pending_wmi_ev);
1372be7d22fSVladimir Kondratiev 	spin_lock_init(&wil->wmi_ev_lock);
1382be7d22fSVladimir Kondratiev 
1392be7d22fSVladimir Kondratiev 	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
1402be7d22fSVladimir Kondratiev 	if (!wil->wmi_wq)
1412be7d22fSVladimir Kondratiev 		return -EAGAIN;
1422be7d22fSVladimir Kondratiev 
1432be7d22fSVladimir Kondratiev 	wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
1442be7d22fSVladimir Kondratiev 	if (!wil->wmi_wq_conn) {
1452be7d22fSVladimir Kondratiev 		destroy_workqueue(wil->wmi_wq);
1462be7d22fSVladimir Kondratiev 		return -EAGAIN;
1472be7d22fSVladimir Kondratiev 	}
1482be7d22fSVladimir Kondratiev 
1492be7d22fSVladimir Kondratiev 	return 0;
1502be7d22fSVladimir Kondratiev }
1512be7d22fSVladimir Kondratiev 
1522be7d22fSVladimir Kondratiev void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
1532be7d22fSVladimir Kondratiev {
1542be7d22fSVladimir Kondratiev 	del_timer_sync(&wil->connect_timer);
1552be7d22fSVladimir Kondratiev 	_wil6210_disconnect(wil, bssid);
1562be7d22fSVladimir Kondratiev }
1572be7d22fSVladimir Kondratiev 
1582be7d22fSVladimir Kondratiev void wil_priv_deinit(struct wil6210_priv *wil)
1592be7d22fSVladimir Kondratiev {
1602be7d22fSVladimir Kondratiev 	cancel_work_sync(&wil->disconnect_worker);
1612be7d22fSVladimir Kondratiev 	wil6210_disconnect(wil, NULL);
1622be7d22fSVladimir Kondratiev 	wmi_event_flush(wil);
1632be7d22fSVladimir Kondratiev 	destroy_workqueue(wil->wmi_wq_conn);
1642be7d22fSVladimir Kondratiev 	destroy_workqueue(wil->wmi_wq);
1652be7d22fSVladimir Kondratiev }
1662be7d22fSVladimir Kondratiev 
1672be7d22fSVladimir Kondratiev static void wil_target_reset(struct wil6210_priv *wil)
1682be7d22fSVladimir Kondratiev {
1697743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "Resetting...\n");
1702be7d22fSVladimir Kondratiev 
1712be7d22fSVladimir Kondratiev 	/* register write */
1722be7d22fSVladimir Kondratiev #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
1732be7d22fSVladimir Kondratiev 	/* register set = read, OR, write */
1742be7d22fSVladimir Kondratiev #define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
1752be7d22fSVladimir Kondratiev 		wil->csr + HOSTADDR(a))
1762be7d22fSVladimir Kondratiev 
1772be7d22fSVladimir Kondratiev 	/* hpal_perst_from_pad_src_n_mask */
1782be7d22fSVladimir Kondratiev 	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
1792be7d22fSVladimir Kondratiev 	/* car_perst_rst_src_n_mask */
1802be7d22fSVladimir Kondratiev 	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
1812be7d22fSVladimir Kondratiev 
1822be7d22fSVladimir Kondratiev 	W(RGF_USER_MAC_CPU_0,  BIT(1)); /* mac_cpu_man_rst */
1832be7d22fSVladimir Kondratiev 	W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
1842be7d22fSVladimir Kondratiev 
1852be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
1862be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
1872be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
1882be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
1892be7d22fSVladimir Kondratiev 
1902be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
1912be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
1922be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
1932be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
1942be7d22fSVladimir Kondratiev 
1952be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
1962be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
1972be7d22fSVladimir Kondratiev 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
1982be7d22fSVladimir Kondratiev 
1997743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "Reset completed\n");
2002be7d22fSVladimir Kondratiev 
2012be7d22fSVladimir Kondratiev #undef W
2022be7d22fSVladimir Kondratiev #undef S
2032be7d22fSVladimir Kondratiev }
2042be7d22fSVladimir Kondratiev 
2052be7d22fSVladimir Kondratiev void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
2062be7d22fSVladimir Kondratiev {
2072be7d22fSVladimir Kondratiev 	le32_to_cpus(&r->base);
2082be7d22fSVladimir Kondratiev 	le16_to_cpus(&r->entry_size);
2092be7d22fSVladimir Kondratiev 	le16_to_cpus(&r->size);
2102be7d22fSVladimir Kondratiev 	le32_to_cpus(&r->tail);
2112be7d22fSVladimir Kondratiev 	le32_to_cpus(&r->head);
2122be7d22fSVladimir Kondratiev }
2132be7d22fSVladimir Kondratiev 
2142be7d22fSVladimir Kondratiev static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
2152be7d22fSVladimir Kondratiev {
2162be7d22fSVladimir Kondratiev 	ulong to = msecs_to_jiffies(1000);
2172be7d22fSVladimir Kondratiev 	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
2182be7d22fSVladimir Kondratiev 	if (0 == left) {
2192be7d22fSVladimir Kondratiev 		wil_err(wil, "Firmware not ready\n");
2202be7d22fSVladimir Kondratiev 		return -ETIME;
2212be7d22fSVladimir Kondratiev 	} else {
2227743882dSVladimir Kondratiev 		wil_dbg_misc(wil, "FW ready after %d ms\n",
2232be7d22fSVladimir Kondratiev 			     jiffies_to_msecs(to-left));
2242be7d22fSVladimir Kondratiev 	}
2252be7d22fSVladimir Kondratiev 	return 0;
2262be7d22fSVladimir Kondratiev }
2272be7d22fSVladimir Kondratiev 
2282be7d22fSVladimir Kondratiev /*
2292be7d22fSVladimir Kondratiev  * We reset all the structures, and we reset the UMAC.
2302be7d22fSVladimir Kondratiev  * After calling this routine, you're expected to reload
2312be7d22fSVladimir Kondratiev  * the firmware.
2322be7d22fSVladimir Kondratiev  */
2332be7d22fSVladimir Kondratiev int wil_reset(struct wil6210_priv *wil)
2342be7d22fSVladimir Kondratiev {
2352be7d22fSVladimir Kondratiev 	int rc;
2362be7d22fSVladimir Kondratiev 
2372be7d22fSVladimir Kondratiev 	cancel_work_sync(&wil->disconnect_worker);
2382be7d22fSVladimir Kondratiev 	wil6210_disconnect(wil, NULL);
2392be7d22fSVladimir Kondratiev 
2402be7d22fSVladimir Kondratiev 	wil6210_disable_irq(wil);
2412be7d22fSVladimir Kondratiev 	wil->status = 0;
2422be7d22fSVladimir Kondratiev 
243e08b5906SVladimir Kondratiev 	wmi_event_flush(wil);
244e08b5906SVladimir Kondratiev 
245e08b5906SVladimir Kondratiev 	flush_workqueue(wil->wmi_wq_conn);
246e08b5906SVladimir Kondratiev 	flush_workqueue(wil->wmi_wq);
247e08b5906SVladimir Kondratiev 
2482be7d22fSVladimir Kondratiev 	/* TODO: put MAC in reset */
2492be7d22fSVladimir Kondratiev 	wil_target_reset(wil);
2502be7d22fSVladimir Kondratiev 
2512be7d22fSVladimir Kondratiev 	/* init after reset */
2522be7d22fSVladimir Kondratiev 	wil->pending_connect_cid = -1;
2532be7d22fSVladimir Kondratiev 	INIT_COMPLETION(wil->wmi_ready);
2542be7d22fSVladimir Kondratiev 
2552be7d22fSVladimir Kondratiev 	/* TODO: release MAC reset */
2562be7d22fSVladimir Kondratiev 	wil6210_enable_irq(wil);
2572be7d22fSVladimir Kondratiev 
2582be7d22fSVladimir Kondratiev 	/* we just started MAC, wait for FW ready */
2592be7d22fSVladimir Kondratiev 	rc = wil_wait_for_fw_ready(wil);
2602be7d22fSVladimir Kondratiev 
2612be7d22fSVladimir Kondratiev 	return rc;
2622be7d22fSVladimir Kondratiev }
2632be7d22fSVladimir Kondratiev 
2642be7d22fSVladimir Kondratiev 
2652be7d22fSVladimir Kondratiev void wil_link_on(struct wil6210_priv *wil)
2662be7d22fSVladimir Kondratiev {
2672be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
2682be7d22fSVladimir Kondratiev 
2697743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "%s()\n", __func__);
2702be7d22fSVladimir Kondratiev 
2712be7d22fSVladimir Kondratiev 	netif_carrier_on(ndev);
2722be7d22fSVladimir Kondratiev 	netif_tx_wake_all_queues(ndev);
2732be7d22fSVladimir Kondratiev }
2742be7d22fSVladimir Kondratiev 
2752be7d22fSVladimir Kondratiev void wil_link_off(struct wil6210_priv *wil)
2762be7d22fSVladimir Kondratiev {
2772be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
2782be7d22fSVladimir Kondratiev 
2797743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "%s()\n", __func__);
2802be7d22fSVladimir Kondratiev 
2812be7d22fSVladimir Kondratiev 	netif_tx_stop_all_queues(ndev);
2822be7d22fSVladimir Kondratiev 	netif_carrier_off(ndev);
2832be7d22fSVladimir Kondratiev }
2842be7d22fSVladimir Kondratiev 
2852be7d22fSVladimir Kondratiev static int __wil_up(struct wil6210_priv *wil)
2862be7d22fSVladimir Kondratiev {
2872be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
2882be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
2892be7d22fSVladimir Kondratiev 	struct ieee80211_channel *channel = wdev->preset_chandef.chan;
2902be7d22fSVladimir Kondratiev 	int rc;
2912be7d22fSVladimir Kondratiev 	int bi;
2922be7d22fSVladimir Kondratiev 	u16 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
2932be7d22fSVladimir Kondratiev 
2942be7d22fSVladimir Kondratiev 	rc = wil_reset(wil);
2952be7d22fSVladimir Kondratiev 	if (rc)
2962be7d22fSVladimir Kondratiev 		return rc;
2972be7d22fSVladimir Kondratiev 
298e31b2562SVladimir Kondratiev 	/* Rx VRING. After MAC and beacon */
299e31b2562SVladimir Kondratiev 	rc = wil_rx_init(wil);
300e31b2562SVladimir Kondratiev 	if (rc)
301e31b2562SVladimir Kondratiev 		return rc;
302e31b2562SVladimir Kondratiev 
3032be7d22fSVladimir Kondratiev 	/* FIXME Firmware works now in PBSS mode(ToDS=0, FromDS=0) */
3042be7d22fSVladimir Kondratiev 	wmi_nettype = wil_iftype_nl2wmi(NL80211_IFTYPE_ADHOC);
3052be7d22fSVladimir Kondratiev 	switch (wdev->iftype) {
3062be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_STATION:
3077743882dSVladimir Kondratiev 		wil_dbg_misc(wil, "type: STATION\n");
3082be7d22fSVladimir Kondratiev 		bi = 0;
3092be7d22fSVladimir Kondratiev 		ndev->type = ARPHRD_ETHER;
3102be7d22fSVladimir Kondratiev 		break;
3112be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_AP:
3127743882dSVladimir Kondratiev 		wil_dbg_misc(wil, "type: AP\n");
3132be7d22fSVladimir Kondratiev 		bi = 100;
3142be7d22fSVladimir Kondratiev 		ndev->type = ARPHRD_ETHER;
3152be7d22fSVladimir Kondratiev 		break;
3162be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_P2P_CLIENT:
3177743882dSVladimir Kondratiev 		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
3182be7d22fSVladimir Kondratiev 		bi = 0;
3192be7d22fSVladimir Kondratiev 		ndev->type = ARPHRD_ETHER;
3202be7d22fSVladimir Kondratiev 		break;
3212be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_P2P_GO:
3227743882dSVladimir Kondratiev 		wil_dbg_misc(wil, "type: P2P_GO\n");
3232be7d22fSVladimir Kondratiev 		bi = 100;
3242be7d22fSVladimir Kondratiev 		ndev->type = ARPHRD_ETHER;
3252be7d22fSVladimir Kondratiev 		break;
3262be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_MONITOR:
3277743882dSVladimir Kondratiev 		wil_dbg_misc(wil, "type: Monitor\n");
3282be7d22fSVladimir Kondratiev 		bi = 0;
3292be7d22fSVladimir Kondratiev 		ndev->type = ARPHRD_IEEE80211_RADIOTAP;
3302be7d22fSVladimir Kondratiev 		/* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
3312be7d22fSVladimir Kondratiev 		break;
3322be7d22fSVladimir Kondratiev 	default:
3332be7d22fSVladimir Kondratiev 		return -EOPNOTSUPP;
3342be7d22fSVladimir Kondratiev 	}
3352be7d22fSVladimir Kondratiev 
3362be7d22fSVladimir Kondratiev 	/* Apply profile in the following order: */
3372be7d22fSVladimir Kondratiev 	/* SSID and channel for the AP */
3382be7d22fSVladimir Kondratiev 	switch (wdev->iftype) {
3392be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_AP:
3402be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_P2P_GO:
3412be7d22fSVladimir Kondratiev 		if (wdev->ssid_len == 0) {
3422be7d22fSVladimir Kondratiev 			wil_err(wil, "SSID not set\n");
3432be7d22fSVladimir Kondratiev 			return -EINVAL;
3442be7d22fSVladimir Kondratiev 		}
345b8023177SVladimir Kondratiev 		rc = wmi_set_ssid(wil, wdev->ssid_len, wdev->ssid);
346b8023177SVladimir Kondratiev 		if (rc)
347b8023177SVladimir Kondratiev 			return rc;
3482be7d22fSVladimir Kondratiev 		break;
3492be7d22fSVladimir Kondratiev 	default:
350afda8bb5SVladimir Kondratiev 		break;
3512be7d22fSVladimir Kondratiev 	}
3522be7d22fSVladimir Kondratiev 
3532be7d22fSVladimir Kondratiev 	/* MAC address - pre-requisite for other commands */
3542be7d22fSVladimir Kondratiev 	wmi_set_mac_address(wil, ndev->dev_addr);
3552be7d22fSVladimir Kondratiev 
3562be7d22fSVladimir Kondratiev 	/* Set up beaconing if required. */
357b8023177SVladimir Kondratiev 	if (bi > 0) {
358b8023177SVladimir Kondratiev 		rc = wmi_pcp_start(wil, bi, wmi_nettype,
359b8023177SVladimir Kondratiev 				   (channel ? channel->hw_value : 0));
3602be7d22fSVladimir Kondratiev 		if (rc)
3612be7d22fSVladimir Kondratiev 			return rc;
362b8023177SVladimir Kondratiev 	}
3632be7d22fSVladimir Kondratiev 
364e0287c4aSVladimir Kondratiev 	napi_enable(&wil->napi_rx);
365e0287c4aSVladimir Kondratiev 	napi_enable(&wil->napi_tx);
366e0287c4aSVladimir Kondratiev 
3672be7d22fSVladimir Kondratiev 	return 0;
3682be7d22fSVladimir Kondratiev }
3692be7d22fSVladimir Kondratiev 
3702be7d22fSVladimir Kondratiev int wil_up(struct wil6210_priv *wil)
3712be7d22fSVladimir Kondratiev {
3722be7d22fSVladimir Kondratiev 	int rc;
3732be7d22fSVladimir Kondratiev 
3742be7d22fSVladimir Kondratiev 	mutex_lock(&wil->mutex);
3752be7d22fSVladimir Kondratiev 	rc = __wil_up(wil);
3762be7d22fSVladimir Kondratiev 	mutex_unlock(&wil->mutex);
3772be7d22fSVladimir Kondratiev 
3782be7d22fSVladimir Kondratiev 	return rc;
3792be7d22fSVladimir Kondratiev }
3802be7d22fSVladimir Kondratiev 
3812be7d22fSVladimir Kondratiev static int __wil_down(struct wil6210_priv *wil)
3822be7d22fSVladimir Kondratiev {
383e0287c4aSVladimir Kondratiev 	napi_disable(&wil->napi_rx);
384e0287c4aSVladimir Kondratiev 	napi_disable(&wil->napi_tx);
385e0287c4aSVladimir Kondratiev 
3862be7d22fSVladimir Kondratiev 	if (wil->scan_request) {
3872be7d22fSVladimir Kondratiev 		cfg80211_scan_done(wil->scan_request, true);
3882be7d22fSVladimir Kondratiev 		wil->scan_request = NULL;
3892be7d22fSVladimir Kondratiev 	}
3902be7d22fSVladimir Kondratiev 
3912be7d22fSVladimir Kondratiev 	wil6210_disconnect(wil, NULL);
3922be7d22fSVladimir Kondratiev 	wil_rx_fini(wil);
3932be7d22fSVladimir Kondratiev 
3942be7d22fSVladimir Kondratiev 	return 0;
3952be7d22fSVladimir Kondratiev }
3962be7d22fSVladimir Kondratiev 
3972be7d22fSVladimir Kondratiev int wil_down(struct wil6210_priv *wil)
3982be7d22fSVladimir Kondratiev {
3992be7d22fSVladimir Kondratiev 	int rc;
4002be7d22fSVladimir Kondratiev 
4012be7d22fSVladimir Kondratiev 	mutex_lock(&wil->mutex);
4022be7d22fSVladimir Kondratiev 	rc = __wil_down(wil);
4032be7d22fSVladimir Kondratiev 	mutex_unlock(&wil->mutex);
4042be7d22fSVladimir Kondratiev 
4052be7d22fSVladimir Kondratiev 	return rc;
4062be7d22fSVladimir Kondratiev }
407