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 "wil6210.h"
182be7d22fSVladimir Kondratiev #include "wmi.h"
192be7d22fSVladimir Kondratiev 
202be7d22fSVladimir Kondratiev #define CHAN60G(_channel, _flags) {				\
212be7d22fSVladimir Kondratiev 	.band			= IEEE80211_BAND_60GHZ,		\
222be7d22fSVladimir Kondratiev 	.center_freq		= 56160 + (2160 * (_channel)),	\
232be7d22fSVladimir Kondratiev 	.hw_value		= (_channel),			\
242be7d22fSVladimir Kondratiev 	.flags			= (_flags),			\
252be7d22fSVladimir Kondratiev 	.max_antenna_gain	= 0,				\
262be7d22fSVladimir Kondratiev 	.max_power		= 40,				\
272be7d22fSVladimir Kondratiev }
282be7d22fSVladimir Kondratiev 
292be7d22fSVladimir Kondratiev static struct ieee80211_channel wil_60ghz_channels[] = {
302be7d22fSVladimir Kondratiev 	CHAN60G(1, 0),
312be7d22fSVladimir Kondratiev 	CHAN60G(2, 0),
322be7d22fSVladimir Kondratiev 	CHAN60G(3, 0),
332be7d22fSVladimir Kondratiev /* channel 4 not supported yet */
342be7d22fSVladimir Kondratiev };
352be7d22fSVladimir Kondratiev 
362be7d22fSVladimir Kondratiev static struct ieee80211_supported_band wil_band_60ghz = {
372be7d22fSVladimir Kondratiev 	.channels = wil_60ghz_channels,
382be7d22fSVladimir Kondratiev 	.n_channels = ARRAY_SIZE(wil_60ghz_channels),
392be7d22fSVladimir Kondratiev 	.ht_cap = {
402be7d22fSVladimir Kondratiev 		.ht_supported = true,
412be7d22fSVladimir Kondratiev 		.cap = 0, /* TODO */
422be7d22fSVladimir Kondratiev 		.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
432be7d22fSVladimir Kondratiev 		.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
442be7d22fSVladimir Kondratiev 		.mcs = {
452be7d22fSVladimir Kondratiev 				/* MCS 1..12 - SC PHY */
462be7d22fSVladimir Kondratiev 			.rx_mask = {0xfe, 0x1f}, /* 1..12 */
472be7d22fSVladimir Kondratiev 			.tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
482be7d22fSVladimir Kondratiev 		},
492be7d22fSVladimir Kondratiev 	},
502be7d22fSVladimir Kondratiev };
512be7d22fSVladimir Kondratiev 
522be7d22fSVladimir Kondratiev static const struct ieee80211_txrx_stypes
532be7d22fSVladimir Kondratiev wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
542be7d22fSVladimir Kondratiev 	[NL80211_IFTYPE_STATION] = {
552be7d22fSVladimir Kondratiev 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
562be7d22fSVladimir Kondratiev 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
572be7d22fSVladimir Kondratiev 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
582be7d22fSVladimir Kondratiev 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
592be7d22fSVladimir Kondratiev 	},
602be7d22fSVladimir Kondratiev 	[NL80211_IFTYPE_AP] = {
612be7d22fSVladimir Kondratiev 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
622be7d22fSVladimir Kondratiev 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
632be7d22fSVladimir Kondratiev 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
642be7d22fSVladimir Kondratiev 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
652be7d22fSVladimir Kondratiev 	},
662be7d22fSVladimir Kondratiev 	[NL80211_IFTYPE_P2P_CLIENT] = {
672be7d22fSVladimir Kondratiev 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
682be7d22fSVladimir Kondratiev 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
692be7d22fSVladimir Kondratiev 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
702be7d22fSVladimir Kondratiev 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
712be7d22fSVladimir Kondratiev 	},
722be7d22fSVladimir Kondratiev 	[NL80211_IFTYPE_P2P_GO] = {
732be7d22fSVladimir Kondratiev 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
742be7d22fSVladimir Kondratiev 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
752be7d22fSVladimir Kondratiev 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
762be7d22fSVladimir Kondratiev 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
772be7d22fSVladimir Kondratiev 	},
782be7d22fSVladimir Kondratiev };
792be7d22fSVladimir Kondratiev 
802be7d22fSVladimir Kondratiev static const u32 wil_cipher_suites[] = {
812be7d22fSVladimir Kondratiev 	WLAN_CIPHER_SUITE_GCMP,
822be7d22fSVladimir Kondratiev };
832be7d22fSVladimir Kondratiev 
842be7d22fSVladimir Kondratiev int wil_iftype_nl2wmi(enum nl80211_iftype type)
852be7d22fSVladimir Kondratiev {
862be7d22fSVladimir Kondratiev 	static const struct {
872be7d22fSVladimir Kondratiev 		enum nl80211_iftype nl;
882be7d22fSVladimir Kondratiev 		enum wmi_network_type wmi;
892be7d22fSVladimir Kondratiev 	} __nl2wmi[] = {
902be7d22fSVladimir Kondratiev 		{NL80211_IFTYPE_ADHOC,		WMI_NETTYPE_ADHOC},
912be7d22fSVladimir Kondratiev 		{NL80211_IFTYPE_STATION,	WMI_NETTYPE_INFRA},
922be7d22fSVladimir Kondratiev 		{NL80211_IFTYPE_AP,		WMI_NETTYPE_AP},
932be7d22fSVladimir Kondratiev 		{NL80211_IFTYPE_P2P_CLIENT,	WMI_NETTYPE_P2P},
942be7d22fSVladimir Kondratiev 		{NL80211_IFTYPE_P2P_GO,		WMI_NETTYPE_P2P},
952be7d22fSVladimir Kondratiev 		{NL80211_IFTYPE_MONITOR,	WMI_NETTYPE_ADHOC}, /* FIXME */
962be7d22fSVladimir Kondratiev 	};
972be7d22fSVladimir Kondratiev 	uint i;
982be7d22fSVladimir Kondratiev 
992be7d22fSVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
1002be7d22fSVladimir Kondratiev 		if (__nl2wmi[i].nl == type)
1012be7d22fSVladimir Kondratiev 			return __nl2wmi[i].wmi;
1022be7d22fSVladimir Kondratiev 	}
1032be7d22fSVladimir Kondratiev 
1042be7d22fSVladimir Kondratiev 	return -EOPNOTSUPP;
1052be7d22fSVladimir Kondratiev }
1062be7d22fSVladimir Kondratiev 
1072be7d22fSVladimir Kondratiev static int wil_cfg80211_get_station(struct wiphy *wiphy,
1082be7d22fSVladimir Kondratiev 				    struct net_device *ndev,
1092be7d22fSVladimir Kondratiev 				    u8 *mac, struct station_info *sinfo)
1102be7d22fSVladimir Kondratiev {
1112be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1122be7d22fSVladimir Kondratiev 	int rc;
1132be7d22fSVladimir Kondratiev 	struct wmi_notify_req_cmd cmd = {
1142be7d22fSVladimir Kondratiev 		.cid = 0,
1152be7d22fSVladimir Kondratiev 		.interval_usec = 0,
1162be7d22fSVladimir Kondratiev 	};
1172be7d22fSVladimir Kondratiev 
1182be7d22fSVladimir Kondratiev 	if (memcmp(mac, wil->dst_addr[0], ETH_ALEN))
1192be7d22fSVladimir Kondratiev 		return -ENOENT;
1202be7d22fSVladimir Kondratiev 
1212be7d22fSVladimir Kondratiev 	/* WMI_NOTIFY_REQ_DONE_EVENTID handler fills wil->stats.bf_mcs */
1222be7d22fSVladimir Kondratiev 	rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
1232be7d22fSVladimir Kondratiev 		      WMI_NOTIFY_REQ_DONE_EVENTID, NULL, 0, 20);
1242be7d22fSVladimir Kondratiev 	if (rc)
1252be7d22fSVladimir Kondratiev 		return rc;
1262be7d22fSVladimir Kondratiev 
1272be7d22fSVladimir Kondratiev 	sinfo->generation = wil->sinfo_gen;
1282be7d22fSVladimir Kondratiev 
1292be7d22fSVladimir Kondratiev 	sinfo->filled |= STATION_INFO_TX_BITRATE;
1302be7d22fSVladimir Kondratiev 	sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
1312be7d22fSVladimir Kondratiev 	sinfo->txrate.mcs = wil->stats.bf_mcs;
1322be7d22fSVladimir Kondratiev 	sinfo->filled |= STATION_INFO_RX_BITRATE;
1332be7d22fSVladimir Kondratiev 	sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
1342be7d22fSVladimir Kondratiev 	sinfo->rxrate.mcs = wil->stats.last_mcs_rx;
1352be7d22fSVladimir Kondratiev 
1362be7d22fSVladimir Kondratiev 	if (test_bit(wil_status_fwconnected, &wil->status)) {
1372be7d22fSVladimir Kondratiev 		sinfo->filled |= STATION_INFO_SIGNAL;
1382be7d22fSVladimir Kondratiev 		sinfo->signal = 12; /* TODO: provide real value */
1392be7d22fSVladimir Kondratiev 	}
1402be7d22fSVladimir Kondratiev 
1412be7d22fSVladimir Kondratiev 	return 0;
1422be7d22fSVladimir Kondratiev }
1432be7d22fSVladimir Kondratiev 
1442be7d22fSVladimir Kondratiev static int wil_cfg80211_change_iface(struct wiphy *wiphy,
1452be7d22fSVladimir Kondratiev 				     struct net_device *ndev,
1462be7d22fSVladimir Kondratiev 				     enum nl80211_iftype type, u32 *flags,
1472be7d22fSVladimir Kondratiev 				     struct vif_params *params)
1482be7d22fSVladimir Kondratiev {
1492be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1502be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
1512be7d22fSVladimir Kondratiev 
1522be7d22fSVladimir Kondratiev 	switch (type) {
1532be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_STATION:
1542be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_AP:
1552be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_P2P_CLIENT:
1562be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_P2P_GO:
1572be7d22fSVladimir Kondratiev 		break;
1582be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_MONITOR:
1592be7d22fSVladimir Kondratiev 		if (flags)
1602be7d22fSVladimir Kondratiev 			wil->monitor_flags = *flags;
1612be7d22fSVladimir Kondratiev 		else
1622be7d22fSVladimir Kondratiev 			wil->monitor_flags = 0;
1632be7d22fSVladimir Kondratiev 
1642be7d22fSVladimir Kondratiev 		break;
1652be7d22fSVladimir Kondratiev 	default:
1662be7d22fSVladimir Kondratiev 		return -EOPNOTSUPP;
1672be7d22fSVladimir Kondratiev 	}
1682be7d22fSVladimir Kondratiev 
1692be7d22fSVladimir Kondratiev 	wdev->iftype = type;
1702be7d22fSVladimir Kondratiev 
1712be7d22fSVladimir Kondratiev 	return 0;
1722be7d22fSVladimir Kondratiev }
1732be7d22fSVladimir Kondratiev 
1742be7d22fSVladimir Kondratiev static int wil_cfg80211_scan(struct wiphy *wiphy,
1752be7d22fSVladimir Kondratiev 			     struct cfg80211_scan_request *request)
1762be7d22fSVladimir Kondratiev {
1772be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1782be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
1792be7d22fSVladimir Kondratiev 	struct {
1802be7d22fSVladimir Kondratiev 		struct wmi_start_scan_cmd cmd;
1812be7d22fSVladimir Kondratiev 		u16 chnl[4];
1822be7d22fSVladimir Kondratiev 	} __packed cmd;
1832be7d22fSVladimir Kondratiev 	uint i, n;
1842be7d22fSVladimir Kondratiev 
1852be7d22fSVladimir Kondratiev 	if (wil->scan_request) {
1862be7d22fSVladimir Kondratiev 		wil_err(wil, "Already scanning\n");
1872be7d22fSVladimir Kondratiev 		return -EAGAIN;
1882be7d22fSVladimir Kondratiev 	}
1892be7d22fSVladimir Kondratiev 
1902be7d22fSVladimir Kondratiev 	/* check we are client side */
1912be7d22fSVladimir Kondratiev 	switch (wdev->iftype) {
1922be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_STATION:
1932be7d22fSVladimir Kondratiev 	case NL80211_IFTYPE_P2P_CLIENT:
1942be7d22fSVladimir Kondratiev 		break;
1952be7d22fSVladimir Kondratiev 	default:
1962be7d22fSVladimir Kondratiev 		return -EOPNOTSUPP;
1972be7d22fSVladimir Kondratiev 	}
1982be7d22fSVladimir Kondratiev 
1992be7d22fSVladimir Kondratiev 	/* FW don't support scan after connection attempt */
2002be7d22fSVladimir Kondratiev 	if (test_bit(wil_status_dontscan, &wil->status)) {
2012be7d22fSVladimir Kondratiev 		wil_err(wil, "Scan after connect attempt not supported\n");
2022be7d22fSVladimir Kondratiev 		return -EBUSY;
2032be7d22fSVladimir Kondratiev 	}
2042be7d22fSVladimir Kondratiev 
2052be7d22fSVladimir Kondratiev 	wil->scan_request = request;
2062be7d22fSVladimir Kondratiev 
2072be7d22fSVladimir Kondratiev 	memset(&cmd, 0, sizeof(cmd));
2082be7d22fSVladimir Kondratiev 	cmd.cmd.num_channels = 0;
2092be7d22fSVladimir Kondratiev 	n = min(request->n_channels, 4U);
2102be7d22fSVladimir Kondratiev 	for (i = 0; i < n; i++) {
2112be7d22fSVladimir Kondratiev 		int ch = request->channels[i]->hw_value;
2122be7d22fSVladimir Kondratiev 		if (ch == 0) {
2132be7d22fSVladimir Kondratiev 			wil_err(wil,
2142be7d22fSVladimir Kondratiev 				"Scan requested for unknown frequency %dMhz\n",
2152be7d22fSVladimir Kondratiev 				request->channels[i]->center_freq);
2162be7d22fSVladimir Kondratiev 			continue;
2172be7d22fSVladimir Kondratiev 		}
2182be7d22fSVladimir Kondratiev 		/* 0-based channel indexes */
2192be7d22fSVladimir Kondratiev 		cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
2207743882dSVladimir Kondratiev 		wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
2212be7d22fSVladimir Kondratiev 			     request->channels[i]->center_freq);
2222be7d22fSVladimir Kondratiev 	}
2232be7d22fSVladimir Kondratiev 
2242be7d22fSVladimir Kondratiev 	return wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
2252be7d22fSVladimir Kondratiev 			cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
2262be7d22fSVladimir Kondratiev }
2272be7d22fSVladimir Kondratiev 
2282be7d22fSVladimir Kondratiev static int wil_cfg80211_connect(struct wiphy *wiphy,
2292be7d22fSVladimir Kondratiev 				struct net_device *ndev,
2302be7d22fSVladimir Kondratiev 				struct cfg80211_connect_params *sme)
2312be7d22fSVladimir Kondratiev {
2322be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2332be7d22fSVladimir Kondratiev 	struct cfg80211_bss *bss;
2342be7d22fSVladimir Kondratiev 	struct wmi_connect_cmd conn;
2352be7d22fSVladimir Kondratiev 	const u8 *ssid_eid;
2362be7d22fSVladimir Kondratiev 	const u8 *rsn_eid;
2372be7d22fSVladimir Kondratiev 	int ch;
2382be7d22fSVladimir Kondratiev 	int rc = 0;
2392be7d22fSVladimir Kondratiev 
2402be7d22fSVladimir Kondratiev 	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
2412be7d22fSVladimir Kondratiev 			       sme->ssid, sme->ssid_len,
2422be7d22fSVladimir Kondratiev 			       WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
2432be7d22fSVladimir Kondratiev 	if (!bss) {
2442be7d22fSVladimir Kondratiev 		wil_err(wil, "Unable to find BSS\n");
2452be7d22fSVladimir Kondratiev 		return -ENOENT;
2462be7d22fSVladimir Kondratiev 	}
2472be7d22fSVladimir Kondratiev 
2482be7d22fSVladimir Kondratiev 	ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
2492be7d22fSVladimir Kondratiev 	if (!ssid_eid) {
2502be7d22fSVladimir Kondratiev 		wil_err(wil, "No SSID\n");
2512be7d22fSVladimir Kondratiev 		rc = -ENOENT;
2522be7d22fSVladimir Kondratiev 		goto out;
2532be7d22fSVladimir Kondratiev 	}
2542be7d22fSVladimir Kondratiev 
2552be7d22fSVladimir Kondratiev 	rsn_eid = sme->ie ?
2562be7d22fSVladimir Kondratiev 			cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
2572be7d22fSVladimir Kondratiev 			NULL;
2582be7d22fSVladimir Kondratiev 	if (rsn_eid) {
2592be7d22fSVladimir Kondratiev 		if (sme->ie_len > WMI_MAX_IE_LEN) {
2602be7d22fSVladimir Kondratiev 			rc = -ERANGE;
2612be7d22fSVladimir Kondratiev 			wil_err(wil, "IE too large (%td bytes)\n",
2622be7d22fSVladimir Kondratiev 				sme->ie_len);
2632be7d22fSVladimir Kondratiev 			goto out;
2642be7d22fSVladimir Kondratiev 		}
2652be7d22fSVladimir Kondratiev 		/*
2662be7d22fSVladimir Kondratiev 		 * For secure assoc, send:
2672be7d22fSVladimir Kondratiev 		 * (1) WMI_DELETE_CIPHER_KEY_CMD
2682be7d22fSVladimir Kondratiev 		 * (2) WMI_SET_APPIE_CMD
2692be7d22fSVladimir Kondratiev 		 */
2702be7d22fSVladimir Kondratiev 		rc = wmi_del_cipher_key(wil, 0, bss->bssid);
2712be7d22fSVladimir Kondratiev 		if (rc) {
2722be7d22fSVladimir Kondratiev 			wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD failed\n");
2732be7d22fSVladimir Kondratiev 			goto out;
2742be7d22fSVladimir Kondratiev 		}
2752be7d22fSVladimir Kondratiev 		/* WMI_SET_APPIE_CMD */
2762be7d22fSVladimir Kondratiev 		rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
2772be7d22fSVladimir Kondratiev 		if (rc) {
2782be7d22fSVladimir Kondratiev 			wil_err(wil, "WMI_SET_APPIE_CMD failed\n");
2792be7d22fSVladimir Kondratiev 			goto out;
2802be7d22fSVladimir Kondratiev 		}
2812be7d22fSVladimir Kondratiev 	}
2822be7d22fSVladimir Kondratiev 
2832be7d22fSVladimir Kondratiev 	/* WMI_CONNECT_CMD */
2842be7d22fSVladimir Kondratiev 	memset(&conn, 0, sizeof(conn));
285acc9780dSVladimir Kondratiev 	switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
2862be7d22fSVladimir Kondratiev 	case WLAN_CAPABILITY_DMG_TYPE_AP:
2872be7d22fSVladimir Kondratiev 		conn.network_type = WMI_NETTYPE_INFRA;
2882be7d22fSVladimir Kondratiev 		break;
2892be7d22fSVladimir Kondratiev 	case WLAN_CAPABILITY_DMG_TYPE_PBSS:
2902be7d22fSVladimir Kondratiev 		conn.network_type = WMI_NETTYPE_P2P;
2912be7d22fSVladimir Kondratiev 		break;
2922be7d22fSVladimir Kondratiev 	default:
2932be7d22fSVladimir Kondratiev 		wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
2942be7d22fSVladimir Kondratiev 			bss->capability);
2952be7d22fSVladimir Kondratiev 		goto out;
2962be7d22fSVladimir Kondratiev 	}
2972be7d22fSVladimir Kondratiev 	if (rsn_eid) {
2982be7d22fSVladimir Kondratiev 		conn.dot11_auth_mode = WMI_AUTH11_SHARED;
2992be7d22fSVladimir Kondratiev 		conn.auth_mode = WMI_AUTH_WPA2_PSK;
3002be7d22fSVladimir Kondratiev 		conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
3012be7d22fSVladimir Kondratiev 		conn.pairwise_crypto_len = 16;
3022be7d22fSVladimir Kondratiev 	} else {
3032be7d22fSVladimir Kondratiev 		conn.dot11_auth_mode = WMI_AUTH11_OPEN;
3042be7d22fSVladimir Kondratiev 		conn.auth_mode = WMI_AUTH_NONE;
3052be7d22fSVladimir Kondratiev 	}
3062be7d22fSVladimir Kondratiev 
3072be7d22fSVladimir Kondratiev 	conn.ssid_len = min_t(u8, ssid_eid[1], 32);
3082be7d22fSVladimir Kondratiev 	memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
3092be7d22fSVladimir Kondratiev 
3102be7d22fSVladimir Kondratiev 	ch = bss->channel->hw_value;
3112be7d22fSVladimir Kondratiev 	if (ch == 0) {
3122be7d22fSVladimir Kondratiev 		wil_err(wil, "BSS at unknown frequency %dMhz\n",
3132be7d22fSVladimir Kondratiev 			bss->channel->center_freq);
3142be7d22fSVladimir Kondratiev 		rc = -EOPNOTSUPP;
3152be7d22fSVladimir Kondratiev 		goto out;
3162be7d22fSVladimir Kondratiev 	}
3172be7d22fSVladimir Kondratiev 	conn.channel = ch - 1;
3182be7d22fSVladimir Kondratiev 
3192be7d22fSVladimir Kondratiev 	memcpy(conn.bssid, bss->bssid, 6);
3202be7d22fSVladimir Kondratiev 	memcpy(conn.dst_mac, bss->bssid, 6);
3212be7d22fSVladimir Kondratiev 	/*
3222be7d22fSVladimir Kondratiev 	 * FW don't support scan after connection attempt
3232be7d22fSVladimir Kondratiev 	 */
3242be7d22fSVladimir Kondratiev 	set_bit(wil_status_dontscan, &wil->status);
325b338f74eSVladimir Kondratiev 	set_bit(wil_status_fwconnecting, &wil->status);
3262be7d22fSVladimir Kondratiev 
3272be7d22fSVladimir Kondratiev 	rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
3282be7d22fSVladimir Kondratiev 	if (rc == 0) {
3292be7d22fSVladimir Kondratiev 		/* Connect can take lots of time */
3302be7d22fSVladimir Kondratiev 		mod_timer(&wil->connect_timer,
3312be7d22fSVladimir Kondratiev 			  jiffies + msecs_to_jiffies(2000));
332b338f74eSVladimir Kondratiev 	} else {
333b338f74eSVladimir Kondratiev 		clear_bit(wil_status_dontscan, &wil->status);
334b338f74eSVladimir Kondratiev 		clear_bit(wil_status_fwconnecting, &wil->status);
3352be7d22fSVladimir Kondratiev 	}
3362be7d22fSVladimir Kondratiev 
3372be7d22fSVladimir Kondratiev  out:
3385b112d3dSJohannes Berg 	cfg80211_put_bss(wiphy, bss);
3392be7d22fSVladimir Kondratiev 
3402be7d22fSVladimir Kondratiev 	return rc;
3412be7d22fSVladimir Kondratiev }
3422be7d22fSVladimir Kondratiev 
3432be7d22fSVladimir Kondratiev static int wil_cfg80211_disconnect(struct wiphy *wiphy,
3442be7d22fSVladimir Kondratiev 				   struct net_device *ndev,
3452be7d22fSVladimir Kondratiev 				   u16 reason_code)
3462be7d22fSVladimir Kondratiev {
3472be7d22fSVladimir Kondratiev 	int rc;
3482be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
3492be7d22fSVladimir Kondratiev 
3502be7d22fSVladimir Kondratiev 	rc = wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
3512be7d22fSVladimir Kondratiev 
3522be7d22fSVladimir Kondratiev 	return rc;
3532be7d22fSVladimir Kondratiev }
3542be7d22fSVladimir Kondratiev 
3552be7d22fSVladimir Kondratiev static int wil_cfg80211_set_channel(struct wiphy *wiphy,
3562be7d22fSVladimir Kondratiev 				    struct cfg80211_chan_def *chandef)
3572be7d22fSVladimir Kondratiev {
3582be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
3592be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
3602be7d22fSVladimir Kondratiev 
3612be7d22fSVladimir Kondratiev 	wdev->preset_chandef = *chandef;
3622be7d22fSVladimir Kondratiev 
3632be7d22fSVladimir Kondratiev 	return 0;
3642be7d22fSVladimir Kondratiev }
3652be7d22fSVladimir Kondratiev 
3662be7d22fSVladimir Kondratiev static int wil_cfg80211_add_key(struct wiphy *wiphy,
3672be7d22fSVladimir Kondratiev 				struct net_device *ndev,
3682be7d22fSVladimir Kondratiev 				u8 key_index, bool pairwise,
3692be7d22fSVladimir Kondratiev 				const u8 *mac_addr,
3702be7d22fSVladimir Kondratiev 				struct key_params *params)
3712be7d22fSVladimir Kondratiev {
3722be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
3732be7d22fSVladimir Kondratiev 
3742be7d22fSVladimir Kondratiev 	/* group key is not used */
3752be7d22fSVladimir Kondratiev 	if (!pairwise)
3762be7d22fSVladimir Kondratiev 		return 0;
3772be7d22fSVladimir Kondratiev 
3782be7d22fSVladimir Kondratiev 	return wmi_add_cipher_key(wil, key_index, mac_addr,
3792be7d22fSVladimir Kondratiev 				  params->key_len, params->key);
3802be7d22fSVladimir Kondratiev }
3812be7d22fSVladimir Kondratiev 
3822be7d22fSVladimir Kondratiev static int wil_cfg80211_del_key(struct wiphy *wiphy,
3832be7d22fSVladimir Kondratiev 				struct net_device *ndev,
3842be7d22fSVladimir Kondratiev 				u8 key_index, bool pairwise,
3852be7d22fSVladimir Kondratiev 				const u8 *mac_addr)
3862be7d22fSVladimir Kondratiev {
3872be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
3882be7d22fSVladimir Kondratiev 
3892be7d22fSVladimir Kondratiev 	/* group key is not used */
3902be7d22fSVladimir Kondratiev 	if (!pairwise)
3912be7d22fSVladimir Kondratiev 		return 0;
3922be7d22fSVladimir Kondratiev 
3932be7d22fSVladimir Kondratiev 	return wmi_del_cipher_key(wil, key_index, mac_addr);
3942be7d22fSVladimir Kondratiev }
3952be7d22fSVladimir Kondratiev 
3962be7d22fSVladimir Kondratiev /* Need to be present or wiphy_new() will WARN */
3972be7d22fSVladimir Kondratiev static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
3982be7d22fSVladimir Kondratiev 					struct net_device *ndev,
3992be7d22fSVladimir Kondratiev 					u8 key_index, bool unicast,
4002be7d22fSVladimir Kondratiev 					bool multicast)
4012be7d22fSVladimir Kondratiev {
4022be7d22fSVladimir Kondratiev 	return 0;
4032be7d22fSVladimir Kondratiev }
4042be7d22fSVladimir Kondratiev 
4052be7d22fSVladimir Kondratiev static int wil_cfg80211_start_ap(struct wiphy *wiphy,
4062be7d22fSVladimir Kondratiev 				 struct net_device *ndev,
4072be7d22fSVladimir Kondratiev 				 struct cfg80211_ap_settings *info)
4082be7d22fSVladimir Kondratiev {
4092be7d22fSVladimir Kondratiev 	int rc = 0;
4102be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
4112be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
4122be7d22fSVladimir Kondratiev 	struct ieee80211_channel *channel = info->chandef.chan;
4132be7d22fSVladimir Kondratiev 	struct cfg80211_beacon_data *bcon = &info->beacon;
4142be7d22fSVladimir Kondratiev 	u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
4152be7d22fSVladimir Kondratiev 
4162be7d22fSVladimir Kondratiev 	if (!channel) {
4172be7d22fSVladimir Kondratiev 		wil_err(wil, "AP: No channel???\n");
4182be7d22fSVladimir Kondratiev 		return -EINVAL;
4192be7d22fSVladimir Kondratiev 	}
4202be7d22fSVladimir Kondratiev 
4217743882dSVladimir Kondratiev 	wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
4222be7d22fSVladimir Kondratiev 		     channel->center_freq, info->privacy ? "secure" : "open");
4232be7d22fSVladimir Kondratiev 	print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
4242be7d22fSVladimir Kondratiev 			     info->ssid, info->ssid_len);
4252be7d22fSVladimir Kondratiev 
4262be7d22fSVladimir Kondratiev 	rc = wil_reset(wil);
4272be7d22fSVladimir Kondratiev 	if (rc)
4282be7d22fSVladimir Kondratiev 		return rc;
4292be7d22fSVladimir Kondratiev 
4302be7d22fSVladimir Kondratiev 	rc = wmi_set_ssid(wil, info->ssid_len, info->ssid);
4312be7d22fSVladimir Kondratiev 	if (rc)
4322be7d22fSVladimir Kondratiev 		return rc;
4332be7d22fSVladimir Kondratiev 
4342be7d22fSVladimir Kondratiev 	/* MAC address - pre-requisite for other commands */
4352be7d22fSVladimir Kondratiev 	wmi_set_mac_address(wil, ndev->dev_addr);
4362be7d22fSVladimir Kondratiev 
4372be7d22fSVladimir Kondratiev 	/* IE's */
4382be7d22fSVladimir Kondratiev 	/* bcon 'head IE's are not relevant for 60g band */
439b1defa4dSVladimir Kondratiev 	/*
440b1defa4dSVladimir Kondratiev 	 * FW do not form regular beacon, so bcon IE's are not set
441b1defa4dSVladimir Kondratiev 	 * For the DMG bcon, when it will be supported, bcon IE's will
442b1defa4dSVladimir Kondratiev 	 * be reused; add something like:
443b1defa4dSVladimir Kondratiev 	 * wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->beacon_ies_len,
444b1defa4dSVladimir Kondratiev 	 * bcon->beacon_ies);
445b1defa4dSVladimir Kondratiev 	 */
4462be7d22fSVladimir Kondratiev 	wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, bcon->proberesp_ies_len,
4472be7d22fSVladimir Kondratiev 		   bcon->proberesp_ies);
4482be7d22fSVladimir Kondratiev 	wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, bcon->assocresp_ies_len,
4492be7d22fSVladimir Kondratiev 		   bcon->assocresp_ies);
4502be7d22fSVladimir Kondratiev 
4512be7d22fSVladimir Kondratiev 	wil->secure_pcp = info->privacy;
4522be7d22fSVladimir Kondratiev 
453b8023177SVladimir Kondratiev 	rc = wmi_pcp_start(wil, info->beacon_interval, wmi_nettype,
454b8023177SVladimir Kondratiev 			   channel->hw_value);
4552be7d22fSVladimir Kondratiev 	if (rc)
4562be7d22fSVladimir Kondratiev 		return rc;
4572be7d22fSVladimir Kondratiev 
4582be7d22fSVladimir Kondratiev 	/* Rx VRING. After MAC and beacon */
4592be7d22fSVladimir Kondratiev 	rc = wil_rx_init(wil);
4602be7d22fSVladimir Kondratiev 
4612be7d22fSVladimir Kondratiev 	netif_carrier_on(ndev);
4622be7d22fSVladimir Kondratiev 
4632be7d22fSVladimir Kondratiev 	return rc;
4642be7d22fSVladimir Kondratiev }
4652be7d22fSVladimir Kondratiev 
4662be7d22fSVladimir Kondratiev static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
4672be7d22fSVladimir Kondratiev 				struct net_device *ndev)
4682be7d22fSVladimir Kondratiev {
4692be7d22fSVladimir Kondratiev 	int rc = 0;
4702be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
4712be7d22fSVladimir Kondratiev 
472b8023177SVladimir Kondratiev 	rc = wmi_pcp_stop(wil);
4732be7d22fSVladimir Kondratiev 
4742be7d22fSVladimir Kondratiev 	return rc;
4752be7d22fSVladimir Kondratiev }
4762be7d22fSVladimir Kondratiev 
4772be7d22fSVladimir Kondratiev static struct cfg80211_ops wil_cfg80211_ops = {
4782be7d22fSVladimir Kondratiev 	.scan = wil_cfg80211_scan,
4792be7d22fSVladimir Kondratiev 	.connect = wil_cfg80211_connect,
4802be7d22fSVladimir Kondratiev 	.disconnect = wil_cfg80211_disconnect,
4812be7d22fSVladimir Kondratiev 	.change_virtual_intf = wil_cfg80211_change_iface,
4822be7d22fSVladimir Kondratiev 	.get_station = wil_cfg80211_get_station,
4832be7d22fSVladimir Kondratiev 	.set_monitor_channel = wil_cfg80211_set_channel,
4842be7d22fSVladimir Kondratiev 	.add_key = wil_cfg80211_add_key,
4852be7d22fSVladimir Kondratiev 	.del_key = wil_cfg80211_del_key,
4862be7d22fSVladimir Kondratiev 	.set_default_key = wil_cfg80211_set_default_key,
4872be7d22fSVladimir Kondratiev 	/* AP mode */
4882be7d22fSVladimir Kondratiev 	.start_ap = wil_cfg80211_start_ap,
4892be7d22fSVladimir Kondratiev 	.stop_ap = wil_cfg80211_stop_ap,
4902be7d22fSVladimir Kondratiev };
4912be7d22fSVladimir Kondratiev 
4922be7d22fSVladimir Kondratiev static void wil_wiphy_init(struct wiphy *wiphy)
4932be7d22fSVladimir Kondratiev {
4942be7d22fSVladimir Kondratiev 	/* TODO: set real value */
4952be7d22fSVladimir Kondratiev 	wiphy->max_scan_ssids = 10;
4962be7d22fSVladimir Kondratiev 	wiphy->max_num_pmkids = 0 /* TODO: */;
4972be7d22fSVladimir Kondratiev 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
4982be7d22fSVladimir Kondratiev 				 BIT(NL80211_IFTYPE_AP) |
4992be7d22fSVladimir Kondratiev 				 BIT(NL80211_IFTYPE_MONITOR);
5002be7d22fSVladimir Kondratiev 	/* TODO: enable P2P when integrated with supplicant:
5012be7d22fSVladimir Kondratiev 	 * BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO)
5022be7d22fSVladimir Kondratiev 	 */
5032be7d22fSVladimir Kondratiev 	wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
5042be7d22fSVladimir Kondratiev 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5052be7d22fSVladimir Kondratiev 	dev_warn(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
5062be7d22fSVladimir Kondratiev 		 __func__, wiphy->flags);
5072be7d22fSVladimir Kondratiev 	wiphy->probe_resp_offload =
5082be7d22fSVladimir Kondratiev 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5092be7d22fSVladimir Kondratiev 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5102be7d22fSVladimir Kondratiev 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5112be7d22fSVladimir Kondratiev 
5122be7d22fSVladimir Kondratiev 	wiphy->bands[IEEE80211_BAND_60GHZ] = &wil_band_60ghz;
5132be7d22fSVladimir Kondratiev 
5142be7d22fSVladimir Kondratiev 	/* TODO: figure this out */
5152be7d22fSVladimir Kondratiev 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
5162be7d22fSVladimir Kondratiev 
5172be7d22fSVladimir Kondratiev 	wiphy->cipher_suites = wil_cipher_suites;
5182be7d22fSVladimir Kondratiev 	wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
5192be7d22fSVladimir Kondratiev 	wiphy->mgmt_stypes = wil_mgmt_stypes;
5202be7d22fSVladimir Kondratiev }
5212be7d22fSVladimir Kondratiev 
5222be7d22fSVladimir Kondratiev struct wireless_dev *wil_cfg80211_init(struct device *dev)
5232be7d22fSVladimir Kondratiev {
5242be7d22fSVladimir Kondratiev 	int rc = 0;
5252be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev;
5262be7d22fSVladimir Kondratiev 
5272be7d22fSVladimir Kondratiev 	wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
5282be7d22fSVladimir Kondratiev 	if (!wdev)
5292be7d22fSVladimir Kondratiev 		return ERR_PTR(-ENOMEM);
5302be7d22fSVladimir Kondratiev 
5312be7d22fSVladimir Kondratiev 	wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
5322be7d22fSVladimir Kondratiev 				sizeof(struct wil6210_priv));
5332be7d22fSVladimir Kondratiev 	if (!wdev->wiphy) {
5342be7d22fSVladimir Kondratiev 		rc = -ENOMEM;
5352be7d22fSVladimir Kondratiev 		goto out;
5362be7d22fSVladimir Kondratiev 	}
5372be7d22fSVladimir Kondratiev 
5382be7d22fSVladimir Kondratiev 	set_wiphy_dev(wdev->wiphy, dev);
5392be7d22fSVladimir Kondratiev 	wil_wiphy_init(wdev->wiphy);
5402be7d22fSVladimir Kondratiev 
5412be7d22fSVladimir Kondratiev 	rc = wiphy_register(wdev->wiphy);
5422be7d22fSVladimir Kondratiev 	if (rc < 0)
5432be7d22fSVladimir Kondratiev 		goto out_failed_reg;
5442be7d22fSVladimir Kondratiev 
5452be7d22fSVladimir Kondratiev 	return wdev;
5462be7d22fSVladimir Kondratiev 
5472be7d22fSVladimir Kondratiev out_failed_reg:
5482be7d22fSVladimir Kondratiev 	wiphy_free(wdev->wiphy);
5492be7d22fSVladimir Kondratiev out:
5502be7d22fSVladimir Kondratiev 	kfree(wdev);
5512be7d22fSVladimir Kondratiev 
5522be7d22fSVladimir Kondratiev 	return ERR_PTR(rc);
5532be7d22fSVladimir Kondratiev }
5542be7d22fSVladimir Kondratiev 
5552be7d22fSVladimir Kondratiev void wil_wdev_free(struct wil6210_priv *wil)
5562be7d22fSVladimir Kondratiev {
5572be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil_to_wdev(wil);
5582be7d22fSVladimir Kondratiev 
5592be7d22fSVladimir Kondratiev 	if (!wdev)
5602be7d22fSVladimir Kondratiev 		return;
5612be7d22fSVladimir Kondratiev 
5622be7d22fSVladimir Kondratiev 	wiphy_unregister(wdev->wiphy);
5632be7d22fSVladimir Kondratiev 	wiphy_free(wdev->wiphy);
5642be7d22fSVladimir Kondratiev 	kfree(wdev);
5652be7d22fSVladimir Kondratiev }
566