1 /*
2  * Copyright (c) 2012-2015 Qualcomm Atheros, Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include "wil6210.h"
18 #include "wmi.h"
19 
20 #define CHAN60G(_channel, _flags) {				\
21 	.band			= IEEE80211_BAND_60GHZ,		\
22 	.center_freq		= 56160 + (2160 * (_channel)),	\
23 	.hw_value		= (_channel),			\
24 	.flags			= (_flags),			\
25 	.max_antenna_gain	= 0,				\
26 	.max_power		= 40,				\
27 }
28 
29 static struct ieee80211_channel wil_60ghz_channels[] = {
30 	CHAN60G(1, 0),
31 	CHAN60G(2, 0),
32 	CHAN60G(3, 0),
33 /* channel 4 not supported yet */
34 };
35 
36 static struct ieee80211_supported_band wil_band_60ghz = {
37 	.channels = wil_60ghz_channels,
38 	.n_channels = ARRAY_SIZE(wil_60ghz_channels),
39 	.ht_cap = {
40 		.ht_supported = true,
41 		.cap = 0, /* TODO */
42 		.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
43 		.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
44 		.mcs = {
45 				/* MCS 1..12 - SC PHY */
46 			.rx_mask = {0xfe, 0x1f}, /* 1..12 */
47 			.tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
48 		},
49 	},
50 };
51 
52 static const struct ieee80211_txrx_stypes
53 wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
54 	[NL80211_IFTYPE_STATION] = {
55 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
56 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
57 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
58 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
59 	},
60 	[NL80211_IFTYPE_AP] = {
61 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
62 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
63 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
64 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
65 	},
66 	[NL80211_IFTYPE_P2P_CLIENT] = {
67 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
68 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
69 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
70 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
71 	},
72 	[NL80211_IFTYPE_P2P_GO] = {
73 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
74 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
75 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
76 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
77 	},
78 };
79 
80 static const u32 wil_cipher_suites[] = {
81 	WLAN_CIPHER_SUITE_GCMP,
82 };
83 
84 int wil_iftype_nl2wmi(enum nl80211_iftype type)
85 {
86 	static const struct {
87 		enum nl80211_iftype nl;
88 		enum wmi_network_type wmi;
89 	} __nl2wmi[] = {
90 		{NL80211_IFTYPE_ADHOC,		WMI_NETTYPE_ADHOC},
91 		{NL80211_IFTYPE_STATION,	WMI_NETTYPE_INFRA},
92 		{NL80211_IFTYPE_AP,		WMI_NETTYPE_AP},
93 		{NL80211_IFTYPE_P2P_CLIENT,	WMI_NETTYPE_P2P},
94 		{NL80211_IFTYPE_P2P_GO,		WMI_NETTYPE_P2P},
95 		{NL80211_IFTYPE_MONITOR,	WMI_NETTYPE_ADHOC}, /* FIXME */
96 	};
97 	uint i;
98 
99 	for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
100 		if (__nl2wmi[i].nl == type)
101 			return __nl2wmi[i].wmi;
102 	}
103 
104 	return -EOPNOTSUPP;
105 }
106 
107 int wil_cid_fill_sinfo(struct wil6210_priv *wil, int cid,
108 		       struct station_info *sinfo)
109 {
110 	struct wmi_notify_req_cmd cmd = {
111 		.cid = cid,
112 		.interval_usec = 0,
113 	};
114 	struct {
115 		struct wil6210_mbox_hdr_wmi wmi;
116 		struct wmi_notify_req_done_event evt;
117 	} __packed reply;
118 	struct wil_net_stats *stats = &wil->sta[cid].stats;
119 	int rc;
120 
121 	rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
122 		      WMI_NOTIFY_REQ_DONE_EVENTID, &reply, sizeof(reply), 20);
123 	if (rc)
124 		return rc;
125 
126 	wil_dbg_wmi(wil, "Link status for CID %d: {\n"
127 		    "  MCS %d TSF 0x%016llx\n"
128 		    "  BF status 0x%08x SNR 0x%08x SQI %d%%\n"
129 		    "  Tx Tpt %d goodput %d Rx goodput %d\n"
130 		    "  Sectors(rx:tx) my %d:%d peer %d:%d\n""}\n",
131 		    cid, le16_to_cpu(reply.evt.bf_mcs),
132 		    le64_to_cpu(reply.evt.tsf), reply.evt.status,
133 		    le32_to_cpu(reply.evt.snr_val),
134 		    reply.evt.sqi,
135 		    le32_to_cpu(reply.evt.tx_tpt),
136 		    le32_to_cpu(reply.evt.tx_goodput),
137 		    le32_to_cpu(reply.evt.rx_goodput),
138 		    le16_to_cpu(reply.evt.my_rx_sector),
139 		    le16_to_cpu(reply.evt.my_tx_sector),
140 		    le16_to_cpu(reply.evt.other_rx_sector),
141 		    le16_to_cpu(reply.evt.other_tx_sector));
142 
143 	sinfo->generation = wil->sinfo_gen;
144 
145 	sinfo->filled = BIT(NL80211_STA_INFO_RX_BYTES) |
146 			BIT(NL80211_STA_INFO_TX_BYTES) |
147 			BIT(NL80211_STA_INFO_RX_PACKETS) |
148 			BIT(NL80211_STA_INFO_TX_PACKETS) |
149 			BIT(NL80211_STA_INFO_RX_BITRATE) |
150 			BIT(NL80211_STA_INFO_TX_BITRATE) |
151 			BIT(NL80211_STA_INFO_RX_DROP_MISC) |
152 			BIT(NL80211_STA_INFO_TX_FAILED);
153 
154 	sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
155 	sinfo->txrate.mcs = le16_to_cpu(reply.evt.bf_mcs);
156 	sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
157 	sinfo->rxrate.mcs = stats->last_mcs_rx;
158 	sinfo->rx_bytes = stats->rx_bytes;
159 	sinfo->rx_packets = stats->rx_packets;
160 	sinfo->rx_dropped_misc = stats->rx_dropped;
161 	sinfo->tx_bytes = stats->tx_bytes;
162 	sinfo->tx_packets = stats->tx_packets;
163 	sinfo->tx_failed = stats->tx_errors;
164 
165 	if (test_bit(wil_status_fwconnected, wil->status)) {
166 		sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
167 		sinfo->signal = reply.evt.sqi;
168 	}
169 
170 	return rc;
171 }
172 
173 static int wil_cfg80211_get_station(struct wiphy *wiphy,
174 				    struct net_device *ndev,
175 				    const u8 *mac, struct station_info *sinfo)
176 {
177 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
178 	int rc;
179 
180 	int cid = wil_find_cid(wil, mac);
181 
182 	wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
183 	if (cid < 0)
184 		return cid;
185 
186 	rc = wil_cid_fill_sinfo(wil, cid, sinfo);
187 
188 	return rc;
189 }
190 
191 /*
192  * Find @idx-th active STA for station dump.
193  */
194 static int wil_find_cid_by_idx(struct wil6210_priv *wil, int idx)
195 {
196 	int i;
197 
198 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
199 		if (wil->sta[i].status == wil_sta_unused)
200 			continue;
201 		if (idx == 0)
202 			return i;
203 		idx--;
204 	}
205 
206 	return -ENOENT;
207 }
208 
209 static int wil_cfg80211_dump_station(struct wiphy *wiphy,
210 				     struct net_device *dev, int idx,
211 				     u8 *mac, struct station_info *sinfo)
212 {
213 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
214 	int rc;
215 	int cid = wil_find_cid_by_idx(wil, idx);
216 
217 	if (cid < 0)
218 		return -ENOENT;
219 
220 	memcpy(mac, wil->sta[cid].addr, ETH_ALEN);
221 	wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
222 
223 	rc = wil_cid_fill_sinfo(wil, cid, sinfo);
224 
225 	return rc;
226 }
227 
228 static int wil_cfg80211_change_iface(struct wiphy *wiphy,
229 				     struct net_device *ndev,
230 				     enum nl80211_iftype type, u32 *flags,
231 				     struct vif_params *params)
232 {
233 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
234 	struct wireless_dev *wdev = wil->wdev;
235 
236 	switch (type) {
237 	case NL80211_IFTYPE_STATION:
238 	case NL80211_IFTYPE_AP:
239 	case NL80211_IFTYPE_P2P_CLIENT:
240 	case NL80211_IFTYPE_P2P_GO:
241 		break;
242 	case NL80211_IFTYPE_MONITOR:
243 		if (flags)
244 			wil->monitor_flags = *flags;
245 		else
246 			wil->monitor_flags = 0;
247 
248 		break;
249 	default:
250 		return -EOPNOTSUPP;
251 	}
252 
253 	wdev->iftype = type;
254 
255 	return 0;
256 }
257 
258 static int wil_cfg80211_scan(struct wiphy *wiphy,
259 			     struct cfg80211_scan_request *request)
260 {
261 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
262 	struct wireless_dev *wdev = wil->wdev;
263 	struct {
264 		struct wmi_start_scan_cmd cmd;
265 		u16 chnl[4];
266 	} __packed cmd;
267 	uint i, n;
268 	int rc;
269 
270 	if (wil->scan_request) {
271 		wil_err(wil, "Already scanning\n");
272 		return -EAGAIN;
273 	}
274 
275 	/* check we are client side */
276 	switch (wdev->iftype) {
277 	case NL80211_IFTYPE_STATION:
278 	case NL80211_IFTYPE_P2P_CLIENT:
279 		break;
280 	default:
281 		return -EOPNOTSUPP;
282 	}
283 
284 	/* FW don't support scan after connection attempt */
285 	if (test_bit(wil_status_dontscan, wil->status)) {
286 		wil_err(wil, "Can't scan now\n");
287 		return -EBUSY;
288 	}
289 
290 	wil_dbg_misc(wil, "Start scan_request 0x%p\n", request);
291 	wil->scan_request = request;
292 	mod_timer(&wil->scan_timer, jiffies + WIL6210_SCAN_TO);
293 
294 	memset(&cmd, 0, sizeof(cmd));
295 	cmd.cmd.num_channels = 0;
296 	n = min(request->n_channels, 4U);
297 	for (i = 0; i < n; i++) {
298 		int ch = request->channels[i]->hw_value;
299 
300 		if (ch == 0) {
301 			wil_err(wil,
302 				"Scan requested for unknown frequency %dMhz\n",
303 				request->channels[i]->center_freq);
304 			continue;
305 		}
306 		/* 0-based channel indexes */
307 		cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
308 		wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
309 			     request->channels[i]->center_freq);
310 	}
311 
312 	if (request->ie_len)
313 		print_hex_dump_bytes("Scan IE ", DUMP_PREFIX_OFFSET,
314 				     request->ie, request->ie_len);
315 	else
316 		wil_dbg_misc(wil, "Scan has no IE's\n");
317 
318 	rc = wmi_set_ie(wil, WMI_FRAME_PROBE_REQ, request->ie_len,
319 			request->ie);
320 	if (rc) {
321 		wil_err(wil, "Aborting scan, set_ie failed: %d\n", rc);
322 		goto out;
323 	}
324 
325 	rc = wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
326 			cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
327 
328 out:
329 	if (rc) {
330 		del_timer_sync(&wil->scan_timer);
331 		wil->scan_request = NULL;
332 	}
333 
334 	return rc;
335 }
336 
337 static void wil_print_crypto(struct wil6210_priv *wil,
338 			     struct cfg80211_crypto_settings *c)
339 {
340 	int i, n;
341 
342 	wil_dbg_misc(wil, "WPA versions: 0x%08x cipher group 0x%08x\n",
343 		     c->wpa_versions, c->cipher_group);
344 	wil_dbg_misc(wil, "Pairwise ciphers [%d] {\n", c->n_ciphers_pairwise);
345 	n = min_t(int, c->n_ciphers_pairwise, ARRAY_SIZE(c->ciphers_pairwise));
346 	for (i = 0; i < n; i++)
347 		wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
348 			     c->ciphers_pairwise[i]);
349 	wil_dbg_misc(wil, "}\n");
350 	wil_dbg_misc(wil, "AKM suites [%d] {\n", c->n_akm_suites);
351 	n = min_t(int, c->n_akm_suites, ARRAY_SIZE(c->akm_suites));
352 	for (i = 0; i < n; i++)
353 		wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
354 			     c->akm_suites[i]);
355 	wil_dbg_misc(wil, "}\n");
356 	wil_dbg_misc(wil, "Control port : %d, eth_type 0x%04x no_encrypt %d\n",
357 		     c->control_port, be16_to_cpu(c->control_port_ethertype),
358 		     c->control_port_no_encrypt);
359 }
360 
361 static void wil_print_connect_params(struct wil6210_priv *wil,
362 				     struct cfg80211_connect_params *sme)
363 {
364 	wil_info(wil, "Connecting to:\n");
365 	if (sme->channel) {
366 		wil_info(wil, "  Channel: %d freq %d\n",
367 			 sme->channel->hw_value, sme->channel->center_freq);
368 	}
369 	if (sme->bssid)
370 		wil_info(wil, "  BSSID: %pM\n", sme->bssid);
371 	if (sme->ssid)
372 		print_hex_dump(KERN_INFO, "  SSID: ", DUMP_PREFIX_OFFSET,
373 			       16, 1, sme->ssid, sme->ssid_len, true);
374 	wil_info(wil, "  Privacy: %s\n", sme->privacy ? "secure" : "open");
375 	wil_print_crypto(wil, &sme->crypto);
376 }
377 
378 static int wil_cfg80211_connect(struct wiphy *wiphy,
379 				struct net_device *ndev,
380 				struct cfg80211_connect_params *sme)
381 {
382 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
383 	struct cfg80211_bss *bss;
384 	struct wmi_connect_cmd conn;
385 	const u8 *ssid_eid;
386 	const u8 *rsn_eid;
387 	int ch;
388 	int rc = 0;
389 
390 	if (test_bit(wil_status_fwconnecting, wil->status) ||
391 	    test_bit(wil_status_fwconnected, wil->status))
392 		return -EALREADY;
393 
394 	wil_print_connect_params(wil, sme);
395 
396 	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
397 			       sme->ssid, sme->ssid_len,
398 			       WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
399 	if (!bss) {
400 		wil_err(wil, "Unable to find BSS\n");
401 		return -ENOENT;
402 	}
403 
404 	ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
405 	if (!ssid_eid) {
406 		wil_err(wil, "No SSID\n");
407 		rc = -ENOENT;
408 		goto out;
409 	}
410 
411 	rsn_eid = sme->ie ?
412 			cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
413 			NULL;
414 	if (rsn_eid) {
415 		if (sme->ie_len > WMI_MAX_IE_LEN) {
416 			rc = -ERANGE;
417 			wil_err(wil, "IE too large (%td bytes)\n",
418 				sme->ie_len);
419 			goto out;
420 		}
421 		/* For secure assoc, send WMI_DELETE_CIPHER_KEY_CMD */
422 		rc = wmi_del_cipher_key(wil, 0, bss->bssid);
423 		if (rc) {
424 			wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD failed\n");
425 			goto out;
426 		}
427 	}
428 
429 	/* WMI_SET_APPIE_CMD. ie may contain rsn info as well as other info
430 	 * elements. Send it also in case it's empty, to erase previously set
431 	 * ies in FW.
432 	 */
433 	rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
434 	if (rc) {
435 		wil_err(wil, "WMI_SET_APPIE_CMD failed\n");
436 		goto out;
437 	}
438 
439 	/* WMI_CONNECT_CMD */
440 	memset(&conn, 0, sizeof(conn));
441 	switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
442 	case WLAN_CAPABILITY_DMG_TYPE_AP:
443 		conn.network_type = WMI_NETTYPE_INFRA;
444 		break;
445 	case WLAN_CAPABILITY_DMG_TYPE_PBSS:
446 		conn.network_type = WMI_NETTYPE_P2P;
447 		break;
448 	default:
449 		wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
450 			bss->capability);
451 		goto out;
452 	}
453 	if (rsn_eid) {
454 		conn.dot11_auth_mode = WMI_AUTH11_SHARED;
455 		conn.auth_mode = WMI_AUTH_WPA2_PSK;
456 		conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
457 		conn.pairwise_crypto_len = 16;
458 	} else {
459 		conn.dot11_auth_mode = WMI_AUTH11_OPEN;
460 		conn.auth_mode = WMI_AUTH_NONE;
461 	}
462 
463 	conn.ssid_len = min_t(u8, ssid_eid[1], 32);
464 	memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
465 
466 	ch = bss->channel->hw_value;
467 	if (ch == 0) {
468 		wil_err(wil, "BSS at unknown frequency %dMhz\n",
469 			bss->channel->center_freq);
470 		rc = -EOPNOTSUPP;
471 		goto out;
472 	}
473 	conn.channel = ch - 1;
474 
475 	memcpy(conn.bssid, bss->bssid, ETH_ALEN);
476 	memcpy(conn.dst_mac, bss->bssid, ETH_ALEN);
477 
478 	set_bit(wil_status_fwconnecting, wil->status);
479 
480 	rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
481 	if (rc == 0) {
482 		netif_carrier_on(ndev);
483 		/* Connect can take lots of time */
484 		mod_timer(&wil->connect_timer,
485 			  jiffies + msecs_to_jiffies(2000));
486 	} else {
487 		clear_bit(wil_status_fwconnecting, wil->status);
488 	}
489 
490  out:
491 	cfg80211_put_bss(wiphy, bss);
492 
493 	return rc;
494 }
495 
496 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
497 				   struct net_device *ndev,
498 				   u16 reason_code)
499 {
500 	int rc;
501 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
502 
503 	rc = wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
504 
505 	return rc;
506 }
507 
508 int wil_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
509 			 struct cfg80211_mgmt_tx_params *params,
510 			 u64 *cookie)
511 {
512 	const u8 *buf = params->buf;
513 	size_t len = params->len;
514 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
515 	int rc;
516 	bool tx_status = false;
517 	struct ieee80211_mgmt *mgmt_frame = (void *)buf;
518 	struct wmi_sw_tx_req_cmd *cmd;
519 	struct {
520 		struct wil6210_mbox_hdr_wmi wmi;
521 		struct wmi_sw_tx_complete_event evt;
522 	} __packed evt;
523 
524 	cmd = kmalloc(sizeof(*cmd) + len, GFP_KERNEL);
525 	if (!cmd) {
526 		rc = -ENOMEM;
527 		goto out;
528 	}
529 
530 	memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
531 	cmd->len = cpu_to_le16(len);
532 	memcpy(cmd->payload, buf, len);
533 
534 	rc = wmi_call(wil, WMI_SW_TX_REQ_CMDID, cmd, sizeof(*cmd) + len,
535 		      WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
536 	if (rc == 0)
537 		tx_status = !evt.evt.status;
538 
539 	kfree(cmd);
540  out:
541 	cfg80211_mgmt_tx_status(wdev, cookie ? *cookie : 0, buf, len,
542 				tx_status, GFP_KERNEL);
543 	return rc;
544 }
545 
546 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
547 				    struct cfg80211_chan_def *chandef)
548 {
549 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
550 	struct wireless_dev *wdev = wil->wdev;
551 
552 	wdev->preset_chandef = *chandef;
553 
554 	return 0;
555 }
556 
557 static int wil_cfg80211_add_key(struct wiphy *wiphy,
558 				struct net_device *ndev,
559 				u8 key_index, bool pairwise,
560 				const u8 *mac_addr,
561 				struct key_params *params)
562 {
563 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
564 
565 	/* group key is not used */
566 	if (!pairwise)
567 		return 0;
568 
569 	return wmi_add_cipher_key(wil, key_index, mac_addr,
570 				  params->key_len, params->key);
571 }
572 
573 static int wil_cfg80211_del_key(struct wiphy *wiphy,
574 				struct net_device *ndev,
575 				u8 key_index, bool pairwise,
576 				const u8 *mac_addr)
577 {
578 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
579 
580 	/* group key is not used */
581 	if (!pairwise)
582 		return 0;
583 
584 	return wmi_del_cipher_key(wil, key_index, mac_addr);
585 }
586 
587 /* Need to be present or wiphy_new() will WARN */
588 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
589 					struct net_device *ndev,
590 					u8 key_index, bool unicast,
591 					bool multicast)
592 {
593 	return 0;
594 }
595 
596 static int wil_remain_on_channel(struct wiphy *wiphy,
597 				 struct wireless_dev *wdev,
598 				 struct ieee80211_channel *chan,
599 				 unsigned int duration,
600 				 u64 *cookie)
601 {
602 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
603 	int rc;
604 
605 	/* TODO: handle duration */
606 	wil_info(wil, "%s(%d, %d ms)\n", __func__, chan->center_freq, duration);
607 
608 	rc = wmi_set_channel(wil, chan->hw_value);
609 	if (rc)
610 		return rc;
611 
612 	rc = wmi_rxon(wil, true);
613 
614 	return rc;
615 }
616 
617 static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
618 					struct wireless_dev *wdev,
619 					u64 cookie)
620 {
621 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
622 	int rc;
623 
624 	wil_info(wil, "%s()\n", __func__);
625 
626 	rc = wmi_rxon(wil, false);
627 
628 	return rc;
629 }
630 
631 static void wil_print_bcon_data(struct cfg80211_beacon_data *b)
632 {
633 	print_hex_dump_bytes("head     ", DUMP_PREFIX_OFFSET,
634 			     b->head, b->head_len);
635 	print_hex_dump_bytes("tail     ", DUMP_PREFIX_OFFSET,
636 			     b->tail, b->tail_len);
637 	print_hex_dump_bytes("BCON IE  ", DUMP_PREFIX_OFFSET,
638 			     b->beacon_ies, b->beacon_ies_len);
639 	print_hex_dump_bytes("PROBE    ", DUMP_PREFIX_OFFSET,
640 			     b->probe_resp, b->probe_resp_len);
641 	print_hex_dump_bytes("PROBE IE ", DUMP_PREFIX_OFFSET,
642 			     b->proberesp_ies, b->proberesp_ies_len);
643 	print_hex_dump_bytes("ASSOC IE ", DUMP_PREFIX_OFFSET,
644 			     b->assocresp_ies, b->assocresp_ies_len);
645 }
646 
647 static int wil_fix_bcon(struct wil6210_priv *wil,
648 			struct cfg80211_beacon_data *bcon)
649 {
650 	struct ieee80211_mgmt *f = (struct ieee80211_mgmt *)bcon->probe_resp;
651 	size_t hlen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
652 	int rc = 0;
653 
654 	if (bcon->probe_resp_len <= hlen)
655 		return 0;
656 
657 	if (!bcon->proberesp_ies) {
658 		bcon->proberesp_ies = f->u.probe_resp.variable;
659 		bcon->proberesp_ies_len = bcon->probe_resp_len - hlen;
660 		rc = 1;
661 	}
662 	if (!bcon->assocresp_ies) {
663 		bcon->assocresp_ies = f->u.probe_resp.variable;
664 		bcon->assocresp_ies_len = bcon->probe_resp_len - hlen;
665 		rc = 1;
666 	}
667 
668 	return rc;
669 }
670 
671 static int wil_cfg80211_change_beacon(struct wiphy *wiphy,
672 				      struct net_device *ndev,
673 				      struct cfg80211_beacon_data *bcon)
674 {
675 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
676 	int rc;
677 
678 	wil_dbg_misc(wil, "%s()\n", __func__);
679 
680 	if (wil_fix_bcon(wil, bcon)) {
681 		wil_dbg_misc(wil, "Fixed bcon\n");
682 		wil_print_bcon_data(bcon);
683 	}
684 
685 	/* FW do not form regular beacon, so bcon IE's are not set
686 	 * For the DMG bcon, when it will be supported, bcon IE's will
687 	 * be reused; add something like:
688 	 * wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->beacon_ies_len,
689 	 * bcon->beacon_ies);
690 	 */
691 	rc = wmi_set_ie(wil, WMI_FRAME_PROBE_RESP,
692 			bcon->proberesp_ies_len,
693 			bcon->proberesp_ies);
694 	if (rc) {
695 		wil_err(wil, "set_ie(PROBE_RESP) failed\n");
696 		return rc;
697 	}
698 
699 	rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP,
700 			bcon->assocresp_ies_len,
701 			bcon->assocresp_ies);
702 	if (rc) {
703 		wil_err(wil, "set_ie(ASSOC_RESP) failed\n");
704 		return rc;
705 	}
706 
707 	return 0;
708 }
709 
710 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
711 				 struct net_device *ndev,
712 				 struct cfg80211_ap_settings *info)
713 {
714 	int rc = 0;
715 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
716 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
717 	struct ieee80211_channel *channel = info->chandef.chan;
718 	struct cfg80211_beacon_data *bcon = &info->beacon;
719 	struct cfg80211_crypto_settings *crypto = &info->crypto;
720 	u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
721 
722 	wil_dbg_misc(wil, "%s()\n", __func__);
723 
724 	if (!channel) {
725 		wil_err(wil, "AP: No channel???\n");
726 		return -EINVAL;
727 	}
728 
729 	wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
730 		     channel->center_freq, info->privacy ? "secure" : "open");
731 	wil_dbg_misc(wil, "Privacy: %d auth_type %d\n",
732 		     info->privacy, info->auth_type);
733 	wil_dbg_misc(wil, "BI %d DTIM %d\n", info->beacon_interval,
734 		     info->dtim_period);
735 	print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
736 			     info->ssid, info->ssid_len);
737 	wil_print_bcon_data(bcon);
738 	wil_print_crypto(wil, crypto);
739 
740 	if (wil_fix_bcon(wil, bcon)) {
741 		wil_dbg_misc(wil, "Fixed bcon\n");
742 		wil_print_bcon_data(bcon);
743 	}
744 
745 	wil_set_recovery_state(wil, fw_recovery_idle);
746 
747 	mutex_lock(&wil->mutex);
748 
749 	__wil_down(wil);
750 	rc = __wil_up(wil);
751 	if (rc)
752 		goto out;
753 
754 	rc = wmi_set_ssid(wil, info->ssid_len, info->ssid);
755 	if (rc)
756 		goto out;
757 
758 	/* IE's */
759 	/* bcon 'head IE's are not relevant for 60g band */
760 	/*
761 	 * FW do not form regular beacon, so bcon IE's are not set
762 	 * For the DMG bcon, when it will be supported, bcon IE's will
763 	 * be reused; add something like:
764 	 * wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->beacon_ies_len,
765 	 * bcon->beacon_ies);
766 	 */
767 	wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, bcon->proberesp_ies_len,
768 		   bcon->proberesp_ies);
769 	wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, bcon->assocresp_ies_len,
770 		   bcon->assocresp_ies);
771 
772 	wil->secure_pcp = info->privacy;
773 
774 	netif_carrier_on(ndev);
775 
776 	rc = wmi_pcp_start(wil, info->beacon_interval, wmi_nettype,
777 			   channel->hw_value);
778 	if (rc)
779 		netif_carrier_off(ndev);
780 
781 out:
782 	mutex_unlock(&wil->mutex);
783 	return rc;
784 }
785 
786 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
787 				struct net_device *ndev)
788 {
789 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
790 
791 	wil_dbg_misc(wil, "%s()\n", __func__);
792 
793 	netif_carrier_off(ndev);
794 	wil_set_recovery_state(wil, fw_recovery_idle);
795 
796 	mutex_lock(&wil->mutex);
797 
798 	wmi_pcp_stop(wil);
799 
800 	__wil_down(wil);
801 	__wil_up(wil);
802 
803 	mutex_unlock(&wil->mutex);
804 
805 	/* some functions above might fail (e.g. __wil_up). Nevertheless, we
806 	 * return success because AP has stopped
807 	 */
808 	return 0;
809 }
810 
811 static int wil_cfg80211_del_station(struct wiphy *wiphy,
812 				    struct net_device *dev,
813 				    struct station_del_parameters *params)
814 {
815 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
816 
817 	mutex_lock(&wil->mutex);
818 	wil6210_disconnect(wil, params->mac, params->reason_code, false);
819 	mutex_unlock(&wil->mutex);
820 
821 	return 0;
822 }
823 
824 /* probe_client handling */
825 static void wil_probe_client_handle(struct wil6210_priv *wil,
826 				    struct wil_probe_client_req *req)
827 {
828 	struct net_device *ndev = wil_to_ndev(wil);
829 	struct wil_sta_info *sta = &wil->sta[req->cid];
830 	/* assume STA is alive if it is still connected,
831 	 * else FW will disconnect it
832 	 */
833 	bool alive = (sta->status == wil_sta_connected);
834 
835 	cfg80211_probe_status(ndev, sta->addr, req->cookie, alive, GFP_KERNEL);
836 }
837 
838 static struct list_head *next_probe_client(struct wil6210_priv *wil)
839 {
840 	struct list_head *ret = NULL;
841 
842 	mutex_lock(&wil->probe_client_mutex);
843 
844 	if (!list_empty(&wil->probe_client_pending)) {
845 		ret = wil->probe_client_pending.next;
846 		list_del(ret);
847 	}
848 
849 	mutex_unlock(&wil->probe_client_mutex);
850 
851 	return ret;
852 }
853 
854 void wil_probe_client_worker(struct work_struct *work)
855 {
856 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
857 						probe_client_worker);
858 	struct wil_probe_client_req *req;
859 	struct list_head *lh;
860 
861 	while ((lh = next_probe_client(wil)) != NULL) {
862 		req = list_entry(lh, struct wil_probe_client_req, list);
863 
864 		wil_probe_client_handle(wil, req);
865 		kfree(req);
866 	}
867 }
868 
869 void wil_probe_client_flush(struct wil6210_priv *wil)
870 {
871 	struct wil_probe_client_req *req, *t;
872 
873 	wil_dbg_misc(wil, "%s()\n", __func__);
874 
875 	mutex_lock(&wil->probe_client_mutex);
876 
877 	list_for_each_entry_safe(req, t, &wil->probe_client_pending, list) {
878 		list_del(&req->list);
879 		kfree(req);
880 	}
881 
882 	mutex_unlock(&wil->probe_client_mutex);
883 }
884 
885 static int wil_cfg80211_probe_client(struct wiphy *wiphy,
886 				     struct net_device *dev,
887 				     const u8 *peer, u64 *cookie)
888 {
889 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
890 	struct wil_probe_client_req *req;
891 	int cid = wil_find_cid(wil, peer);
892 
893 	wil_dbg_misc(wil, "%s(%pM => CID %d)\n", __func__, peer, cid);
894 
895 	if (cid < 0)
896 		return -ENOLINK;
897 
898 	req = kzalloc(sizeof(*req), GFP_KERNEL);
899 	if (!req)
900 		return -ENOMEM;
901 
902 	req->cid = cid;
903 	req->cookie = cid;
904 
905 	mutex_lock(&wil->probe_client_mutex);
906 	list_add_tail(&req->list, &wil->probe_client_pending);
907 	mutex_unlock(&wil->probe_client_mutex);
908 
909 	*cookie = req->cookie;
910 	queue_work(wil->wq_service, &wil->probe_client_worker);
911 	return 0;
912 }
913 
914 static struct cfg80211_ops wil_cfg80211_ops = {
915 	.scan = wil_cfg80211_scan,
916 	.connect = wil_cfg80211_connect,
917 	.disconnect = wil_cfg80211_disconnect,
918 	.change_virtual_intf = wil_cfg80211_change_iface,
919 	.get_station = wil_cfg80211_get_station,
920 	.dump_station = wil_cfg80211_dump_station,
921 	.remain_on_channel = wil_remain_on_channel,
922 	.cancel_remain_on_channel = wil_cancel_remain_on_channel,
923 	.mgmt_tx = wil_cfg80211_mgmt_tx,
924 	.set_monitor_channel = wil_cfg80211_set_channel,
925 	.add_key = wil_cfg80211_add_key,
926 	.del_key = wil_cfg80211_del_key,
927 	.set_default_key = wil_cfg80211_set_default_key,
928 	/* AP mode */
929 	.change_beacon = wil_cfg80211_change_beacon,
930 	.start_ap = wil_cfg80211_start_ap,
931 	.stop_ap = wil_cfg80211_stop_ap,
932 	.del_station = wil_cfg80211_del_station,
933 	.probe_client = wil_cfg80211_probe_client,
934 };
935 
936 static void wil_wiphy_init(struct wiphy *wiphy)
937 {
938 	/* TODO: set real value */
939 	wiphy->max_scan_ssids = 10;
940 	wiphy->max_scan_ie_len = WMI_MAX_IE_LEN;
941 	wiphy->max_num_pmkids = 0 /* TODO: */;
942 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
943 				 BIT(NL80211_IFTYPE_AP) |
944 				 BIT(NL80211_IFTYPE_MONITOR);
945 	/* TODO: enable P2P when integrated with supplicant:
946 	 * BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO)
947 	 */
948 	wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
949 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
950 	dev_dbg(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
951 		__func__, wiphy->flags);
952 	wiphy->probe_resp_offload =
953 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
954 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
955 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
956 
957 	wiphy->bands[IEEE80211_BAND_60GHZ] = &wil_band_60ghz;
958 
959 	/* TODO: figure this out */
960 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
961 
962 	wiphy->cipher_suites = wil_cipher_suites;
963 	wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
964 	wiphy->mgmt_stypes = wil_mgmt_stypes;
965 	wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
966 }
967 
968 struct wireless_dev *wil_cfg80211_init(struct device *dev)
969 {
970 	int rc = 0;
971 	struct wireless_dev *wdev;
972 
973 	dev_dbg(dev, "%s()\n", __func__);
974 
975 	wdev = kzalloc(sizeof(*wdev), GFP_KERNEL);
976 	if (!wdev)
977 		return ERR_PTR(-ENOMEM);
978 
979 	wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
980 				sizeof(struct wil6210_priv));
981 	if (!wdev->wiphy) {
982 		rc = -ENOMEM;
983 		goto out;
984 	}
985 
986 	set_wiphy_dev(wdev->wiphy, dev);
987 	wil_wiphy_init(wdev->wiphy);
988 
989 	rc = wiphy_register(wdev->wiphy);
990 	if (rc < 0)
991 		goto out_failed_reg;
992 
993 	return wdev;
994 
995 out_failed_reg:
996 	wiphy_free(wdev->wiphy);
997 out:
998 	kfree(wdev);
999 
1000 	return ERR_PTR(rc);
1001 }
1002 
1003 void wil_wdev_free(struct wil6210_priv *wil)
1004 {
1005 	struct wireless_dev *wdev = wil_to_wdev(wil);
1006 
1007 	dev_dbg(wil_to_dev(wil), "%s()\n", __func__);
1008 
1009 	if (!wdev)
1010 		return;
1011 
1012 	wiphy_unregister(wdev->wiphy);
1013 	wiphy_free(wdev->wiphy);
1014 	kfree(wdev);
1015 }
1016