1 /*
2  * Copyright (c) 2012-2017 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 <linux/etherdevice.h>
18 #include <linux/moduleparam.h>
19 #include "wil6210.h"
20 #include "wmi.h"
21 
22 #define WIL_MAX_ROC_DURATION_MS 5000
23 
24 bool disable_ap_sme;
25 module_param(disable_ap_sme, bool, 0444);
26 MODULE_PARM_DESC(disable_ap_sme, " let user space handle AP mode SME");
27 
28 #define CHAN60G(_channel, _flags) {				\
29 	.band			= NL80211_BAND_60GHZ,		\
30 	.center_freq		= 56160 + (2160 * (_channel)),	\
31 	.hw_value		= (_channel),			\
32 	.flags			= (_flags),			\
33 	.max_antenna_gain	= 0,				\
34 	.max_power		= 40,				\
35 }
36 
37 static struct ieee80211_channel wil_60ghz_channels[] = {
38 	CHAN60G(1, 0),
39 	CHAN60G(2, 0),
40 	CHAN60G(3, 0),
41 /* channel 4 not supported yet */
42 };
43 
44 static struct ieee80211_supported_band wil_band_60ghz = {
45 	.channels = wil_60ghz_channels,
46 	.n_channels = ARRAY_SIZE(wil_60ghz_channels),
47 	.ht_cap = {
48 		.ht_supported = true,
49 		.cap = 0, /* TODO */
50 		.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
51 		.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
52 		.mcs = {
53 				/* MCS 1..12 - SC PHY */
54 			.rx_mask = {0xfe, 0x1f}, /* 1..12 */
55 			.tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
56 		},
57 	},
58 };
59 
60 static const struct ieee80211_txrx_stypes
61 wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
62 	[NL80211_IFTYPE_STATION] = {
63 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
64 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
65 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
66 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
67 	},
68 	[NL80211_IFTYPE_AP] = {
69 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
70 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4) |
71 		BIT(IEEE80211_STYPE_ASSOC_RESP >> 4) |
72 		BIT(IEEE80211_STYPE_DISASSOC >> 4),
73 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
74 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
75 		BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
76 		BIT(IEEE80211_STYPE_DISASSOC >> 4) |
77 		BIT(IEEE80211_STYPE_AUTH >> 4) |
78 		BIT(IEEE80211_STYPE_DEAUTH >> 4) |
79 		BIT(IEEE80211_STYPE_REASSOC_REQ >> 4)
80 	},
81 	[NL80211_IFTYPE_P2P_CLIENT] = {
82 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
83 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
84 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
85 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
86 	},
87 	[NL80211_IFTYPE_P2P_GO] = {
88 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
89 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
90 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
91 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
92 	},
93 	[NL80211_IFTYPE_P2P_DEVICE] = {
94 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
95 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
96 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
97 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
98 	},
99 };
100 
101 static const u32 wil_cipher_suites[] = {
102 	WLAN_CIPHER_SUITE_GCMP,
103 };
104 
105 static const char * const key_usage_str[] = {
106 	[WMI_KEY_USE_PAIRWISE]	= "PTK",
107 	[WMI_KEY_USE_RX_GROUP]	= "RX_GTK",
108 	[WMI_KEY_USE_TX_GROUP]	= "TX_GTK",
109 };
110 
111 int wil_iftype_nl2wmi(enum nl80211_iftype type)
112 {
113 	static const struct {
114 		enum nl80211_iftype nl;
115 		enum wmi_network_type wmi;
116 	} __nl2wmi[] = {
117 		{NL80211_IFTYPE_ADHOC,		WMI_NETTYPE_ADHOC},
118 		{NL80211_IFTYPE_STATION,	WMI_NETTYPE_INFRA},
119 		{NL80211_IFTYPE_AP,		WMI_NETTYPE_AP},
120 		{NL80211_IFTYPE_P2P_CLIENT,	WMI_NETTYPE_P2P},
121 		{NL80211_IFTYPE_P2P_GO,		WMI_NETTYPE_P2P},
122 		{NL80211_IFTYPE_MONITOR,	WMI_NETTYPE_ADHOC}, /* FIXME */
123 	};
124 	uint i;
125 
126 	for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
127 		if (__nl2wmi[i].nl == type)
128 			return __nl2wmi[i].wmi;
129 	}
130 
131 	return -EOPNOTSUPP;
132 }
133 
134 int wil_cid_fill_sinfo(struct wil6210_priv *wil, int cid,
135 		       struct station_info *sinfo)
136 {
137 	struct wmi_notify_req_cmd cmd = {
138 		.cid = cid,
139 		.interval_usec = 0,
140 	};
141 	struct {
142 		struct wmi_cmd_hdr wmi;
143 		struct wmi_notify_req_done_event evt;
144 	} __packed reply;
145 	struct wil_net_stats *stats = &wil->sta[cid].stats;
146 	int rc;
147 
148 	rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
149 		      WMI_NOTIFY_REQ_DONE_EVENTID, &reply, sizeof(reply), 20);
150 	if (rc)
151 		return rc;
152 
153 	wil_dbg_wmi(wil, "Link status for CID %d: {\n"
154 		    "  MCS %d TSF 0x%016llx\n"
155 		    "  BF status 0x%08x SNR 0x%08x SQI %d%%\n"
156 		    "  Tx Tpt %d goodput %d Rx goodput %d\n"
157 		    "  Sectors(rx:tx) my %d:%d peer %d:%d\n""}\n",
158 		    cid, le16_to_cpu(reply.evt.bf_mcs),
159 		    le64_to_cpu(reply.evt.tsf), reply.evt.status,
160 		    le32_to_cpu(reply.evt.snr_val),
161 		    reply.evt.sqi,
162 		    le32_to_cpu(reply.evt.tx_tpt),
163 		    le32_to_cpu(reply.evt.tx_goodput),
164 		    le32_to_cpu(reply.evt.rx_goodput),
165 		    le16_to_cpu(reply.evt.my_rx_sector),
166 		    le16_to_cpu(reply.evt.my_tx_sector),
167 		    le16_to_cpu(reply.evt.other_rx_sector),
168 		    le16_to_cpu(reply.evt.other_tx_sector));
169 
170 	sinfo->generation = wil->sinfo_gen;
171 
172 	sinfo->filled = BIT(NL80211_STA_INFO_RX_BYTES) |
173 			BIT(NL80211_STA_INFO_TX_BYTES) |
174 			BIT(NL80211_STA_INFO_RX_PACKETS) |
175 			BIT(NL80211_STA_INFO_TX_PACKETS) |
176 			BIT(NL80211_STA_INFO_RX_BITRATE) |
177 			BIT(NL80211_STA_INFO_TX_BITRATE) |
178 			BIT(NL80211_STA_INFO_RX_DROP_MISC) |
179 			BIT(NL80211_STA_INFO_TX_FAILED);
180 
181 	sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
182 	sinfo->txrate.mcs = le16_to_cpu(reply.evt.bf_mcs);
183 	sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
184 	sinfo->rxrate.mcs = stats->last_mcs_rx;
185 	sinfo->rx_bytes = stats->rx_bytes;
186 	sinfo->rx_packets = stats->rx_packets;
187 	sinfo->rx_dropped_misc = stats->rx_dropped;
188 	sinfo->tx_bytes = stats->tx_bytes;
189 	sinfo->tx_packets = stats->tx_packets;
190 	sinfo->tx_failed = stats->tx_errors;
191 
192 	if (test_bit(wil_status_fwconnected, wil->status)) {
193 		sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
194 		sinfo->signal = reply.evt.sqi;
195 	}
196 
197 	return rc;
198 }
199 
200 static int wil_cfg80211_get_station(struct wiphy *wiphy,
201 				    struct net_device *ndev,
202 				    const u8 *mac, struct station_info *sinfo)
203 {
204 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
205 	int rc;
206 
207 	int cid = wil_find_cid(wil, mac);
208 
209 	wil_dbg_misc(wil, "get_station: %pM CID %d\n", mac, cid);
210 	if (cid < 0)
211 		return cid;
212 
213 	rc = wil_cid_fill_sinfo(wil, cid, sinfo);
214 
215 	return rc;
216 }
217 
218 /*
219  * Find @idx-th active STA for station dump.
220  */
221 static int wil_find_cid_by_idx(struct wil6210_priv *wil, int idx)
222 {
223 	int i;
224 
225 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
226 		if (wil->sta[i].status == wil_sta_unused)
227 			continue;
228 		if (idx == 0)
229 			return i;
230 		idx--;
231 	}
232 
233 	return -ENOENT;
234 }
235 
236 static int wil_cfg80211_dump_station(struct wiphy *wiphy,
237 				     struct net_device *dev, int idx,
238 				     u8 *mac, struct station_info *sinfo)
239 {
240 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
241 	int rc;
242 	int cid = wil_find_cid_by_idx(wil, idx);
243 
244 	if (cid < 0)
245 		return -ENOENT;
246 
247 	ether_addr_copy(mac, wil->sta[cid].addr);
248 	wil_dbg_misc(wil, "dump_station: %pM CID %d\n", mac, cid);
249 
250 	rc = wil_cid_fill_sinfo(wil, cid, sinfo);
251 
252 	return rc;
253 }
254 
255 static struct wireless_dev *
256 wil_cfg80211_add_iface(struct wiphy *wiphy, const char *name,
257 		       unsigned char name_assign_type,
258 		       enum nl80211_iftype type,
259 		       u32 *flags, struct vif_params *params)
260 {
261 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
262 	struct net_device *ndev = wil_to_ndev(wil);
263 	struct wireless_dev *p2p_wdev;
264 
265 	wil_dbg_misc(wil, "add_iface\n");
266 
267 	if (type != NL80211_IFTYPE_P2P_DEVICE) {
268 		wil_err(wil, "unsupported iftype %d\n", type);
269 		return ERR_PTR(-EINVAL);
270 	}
271 
272 	if (wil->p2p_wdev) {
273 		wil_err(wil, "P2P_DEVICE interface already created\n");
274 		return ERR_PTR(-EINVAL);
275 	}
276 
277 	p2p_wdev = kzalloc(sizeof(*p2p_wdev), GFP_KERNEL);
278 	if (!p2p_wdev)
279 		return ERR_PTR(-ENOMEM);
280 
281 	p2p_wdev->iftype = type;
282 	p2p_wdev->wiphy = wiphy;
283 	/* use our primary ethernet address */
284 	ether_addr_copy(p2p_wdev->address, ndev->perm_addr);
285 
286 	wil->p2p_wdev = p2p_wdev;
287 
288 	return p2p_wdev;
289 }
290 
291 static int wil_cfg80211_del_iface(struct wiphy *wiphy,
292 				  struct wireless_dev *wdev)
293 {
294 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
295 
296 	wil_dbg_misc(wil, "del_iface\n");
297 
298 	if (wdev != wil->p2p_wdev) {
299 		wil_err(wil, "delete of incorrect interface 0x%p\n", wdev);
300 		return -EINVAL;
301 	}
302 
303 	wil_p2p_wdev_free(wil);
304 
305 	return 0;
306 }
307 
308 static int wil_cfg80211_change_iface(struct wiphy *wiphy,
309 				     struct net_device *ndev,
310 				     enum nl80211_iftype type, u32 *flags,
311 				     struct vif_params *params)
312 {
313 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
314 	struct wireless_dev *wdev = wil_to_wdev(wil);
315 	int rc;
316 
317 	wil_dbg_misc(wil, "change_iface: type=%d\n", type);
318 
319 	if (netif_running(wil_to_ndev(wil)) && !wil_is_recovery_blocked(wil)) {
320 		wil_dbg_misc(wil, "interface is up. resetting...\n");
321 		mutex_lock(&wil->mutex);
322 		__wil_down(wil);
323 		rc = __wil_up(wil);
324 		mutex_unlock(&wil->mutex);
325 
326 		if (rc)
327 			return rc;
328 	}
329 
330 	switch (type) {
331 	case NL80211_IFTYPE_STATION:
332 	case NL80211_IFTYPE_AP:
333 	case NL80211_IFTYPE_P2P_CLIENT:
334 	case NL80211_IFTYPE_P2P_GO:
335 		break;
336 	case NL80211_IFTYPE_MONITOR:
337 		if (flags)
338 			wil->monitor_flags = *flags;
339 		else
340 			wil->monitor_flags = 0;
341 
342 		break;
343 	default:
344 		return -EOPNOTSUPP;
345 	}
346 
347 	wdev->iftype = type;
348 
349 	return 0;
350 }
351 
352 static int wil_cfg80211_scan(struct wiphy *wiphy,
353 			     struct cfg80211_scan_request *request)
354 {
355 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
356 	struct wireless_dev *wdev = request->wdev;
357 	struct {
358 		struct wmi_start_scan_cmd cmd;
359 		u16 chnl[4];
360 	} __packed cmd;
361 	uint i, n;
362 	int rc;
363 
364 	wil_dbg_misc(wil, "scan: wdev=0x%p iftype=%d\n", wdev, wdev->iftype);
365 
366 	/* check we are client side */
367 	switch (wdev->iftype) {
368 	case NL80211_IFTYPE_STATION:
369 	case NL80211_IFTYPE_P2P_CLIENT:
370 	case NL80211_IFTYPE_P2P_DEVICE:
371 		break;
372 	default:
373 		return -EOPNOTSUPP;
374 	}
375 
376 	/* FW don't support scan after connection attempt */
377 	if (test_bit(wil_status_dontscan, wil->status)) {
378 		wil_err(wil, "Can't scan now\n");
379 		return -EBUSY;
380 	}
381 
382 	mutex_lock(&wil->mutex);
383 
384 	mutex_lock(&wil->p2p_wdev_mutex);
385 	if (wil->scan_request || wil->p2p.discovery_started) {
386 		wil_err(wil, "Already scanning\n");
387 		mutex_unlock(&wil->p2p_wdev_mutex);
388 		rc = -EAGAIN;
389 		goto out;
390 	}
391 	mutex_unlock(&wil->p2p_wdev_mutex);
392 
393 	/* social scan on P2P_DEVICE is handled as p2p search */
394 	if (wdev->iftype == NL80211_IFTYPE_P2P_DEVICE &&
395 	    wil_p2p_is_social_scan(request)) {
396 		if (!wil->p2p.p2p_dev_started) {
397 			wil_err(wil, "P2P search requested on stopped P2P device\n");
398 			rc = -EIO;
399 			goto out;
400 		}
401 		wil->scan_request = request;
402 		wil->radio_wdev = wdev;
403 		rc = wil_p2p_search(wil, request);
404 		if (rc) {
405 			wil->radio_wdev = wil_to_wdev(wil);
406 			wil->scan_request = NULL;
407 		}
408 		goto out;
409 	}
410 
411 	(void)wil_p2p_stop_discovery(wil);
412 
413 	wil_dbg_misc(wil, "Start scan_request 0x%p\n", request);
414 	wil_dbg_misc(wil, "SSID count: %d", request->n_ssids);
415 
416 	for (i = 0; i < request->n_ssids; i++) {
417 		wil_dbg_misc(wil, "SSID[%d]", i);
418 		print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
419 				     request->ssids[i].ssid,
420 				     request->ssids[i].ssid_len);
421 	}
422 
423 	if (request->n_ssids)
424 		rc = wmi_set_ssid(wil, request->ssids[0].ssid_len,
425 				  request->ssids[0].ssid);
426 	else
427 		rc = wmi_set_ssid(wil, 0, NULL);
428 
429 	if (rc) {
430 		wil_err(wil, "set SSID for scan request failed: %d\n", rc);
431 		goto out;
432 	}
433 
434 	wil->scan_request = request;
435 	mod_timer(&wil->scan_timer, jiffies + WIL6210_SCAN_TO);
436 
437 	memset(&cmd, 0, sizeof(cmd));
438 	cmd.cmd.scan_type = WMI_ACTIVE_SCAN;
439 	cmd.cmd.num_channels = 0;
440 	n = min(request->n_channels, 4U);
441 	for (i = 0; i < n; i++) {
442 		int ch = request->channels[i]->hw_value;
443 
444 		if (ch == 0) {
445 			wil_err(wil,
446 				"Scan requested for unknown frequency %dMhz\n",
447 				request->channels[i]->center_freq);
448 			continue;
449 		}
450 		/* 0-based channel indexes */
451 		cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
452 		wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
453 			     request->channels[i]->center_freq);
454 	}
455 
456 	if (request->ie_len)
457 		print_hex_dump_bytes("Scan IE ", DUMP_PREFIX_OFFSET,
458 				     request->ie, request->ie_len);
459 	else
460 		wil_dbg_misc(wil, "Scan has no IE's\n");
461 
462 	rc = wmi_set_ie(wil, WMI_FRAME_PROBE_REQ, request->ie_len, request->ie);
463 	if (rc)
464 		goto out_restore;
465 
466 	if (wil->discovery_mode && cmd.cmd.scan_type == WMI_ACTIVE_SCAN) {
467 		cmd.cmd.discovery_mode = 1;
468 		wil_dbg_misc(wil, "active scan with discovery_mode=1\n");
469 	}
470 
471 	wil->radio_wdev = wdev;
472 	rc = wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
473 			cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
474 
475 out_restore:
476 	if (rc) {
477 		del_timer_sync(&wil->scan_timer);
478 		wil->radio_wdev = wil_to_wdev(wil);
479 		wil->scan_request = NULL;
480 	}
481 out:
482 	mutex_unlock(&wil->mutex);
483 	return rc;
484 }
485 
486 static void wil_cfg80211_abort_scan(struct wiphy *wiphy,
487 				    struct wireless_dev *wdev)
488 {
489 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
490 
491 	wil_dbg_misc(wil, "wdev=0x%p iftype=%d\n", wdev, wdev->iftype);
492 
493 	mutex_lock(&wil->mutex);
494 	mutex_lock(&wil->p2p_wdev_mutex);
495 
496 	if (!wil->scan_request)
497 		goto out;
498 
499 	if (wdev != wil->scan_request->wdev) {
500 		wil_dbg_misc(wil, "abort scan was called on the wrong iface\n");
501 		goto out;
502 	}
503 
504 	if (wil->radio_wdev == wil->p2p_wdev)
505 		wil_p2p_stop_radio_operations(wil);
506 	else
507 		wil_abort_scan(wil, true);
508 
509 out:
510 	mutex_unlock(&wil->p2p_wdev_mutex);
511 	mutex_unlock(&wil->mutex);
512 }
513 
514 static void wil_print_crypto(struct wil6210_priv *wil,
515 			     struct cfg80211_crypto_settings *c)
516 {
517 	int i, n;
518 
519 	wil_dbg_misc(wil, "WPA versions: 0x%08x cipher group 0x%08x\n",
520 		     c->wpa_versions, c->cipher_group);
521 	wil_dbg_misc(wil, "Pairwise ciphers [%d] {\n", c->n_ciphers_pairwise);
522 	n = min_t(int, c->n_ciphers_pairwise, ARRAY_SIZE(c->ciphers_pairwise));
523 	for (i = 0; i < n; i++)
524 		wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
525 			     c->ciphers_pairwise[i]);
526 	wil_dbg_misc(wil, "}\n");
527 	wil_dbg_misc(wil, "AKM suites [%d] {\n", c->n_akm_suites);
528 	n = min_t(int, c->n_akm_suites, ARRAY_SIZE(c->akm_suites));
529 	for (i = 0; i < n; i++)
530 		wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
531 			     c->akm_suites[i]);
532 	wil_dbg_misc(wil, "}\n");
533 	wil_dbg_misc(wil, "Control port : %d, eth_type 0x%04x no_encrypt %d\n",
534 		     c->control_port, be16_to_cpu(c->control_port_ethertype),
535 		     c->control_port_no_encrypt);
536 }
537 
538 static void wil_print_connect_params(struct wil6210_priv *wil,
539 				     struct cfg80211_connect_params *sme)
540 {
541 	wil_info(wil, "Connecting to:\n");
542 	if (sme->channel) {
543 		wil_info(wil, "  Channel: %d freq %d\n",
544 			 sme->channel->hw_value, sme->channel->center_freq);
545 	}
546 	if (sme->bssid)
547 		wil_info(wil, "  BSSID: %pM\n", sme->bssid);
548 	if (sme->ssid)
549 		print_hex_dump(KERN_INFO, "  SSID: ", DUMP_PREFIX_OFFSET,
550 			       16, 1, sme->ssid, sme->ssid_len, true);
551 	wil_info(wil, "  Privacy: %s\n", sme->privacy ? "secure" : "open");
552 	wil_info(wil, "  PBSS: %d\n", sme->pbss);
553 	wil_print_crypto(wil, &sme->crypto);
554 }
555 
556 static int wil_cfg80211_connect(struct wiphy *wiphy,
557 				struct net_device *ndev,
558 				struct cfg80211_connect_params *sme)
559 {
560 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
561 	struct cfg80211_bss *bss;
562 	struct wmi_connect_cmd conn;
563 	const u8 *ssid_eid;
564 	const u8 *rsn_eid;
565 	int ch;
566 	int rc = 0;
567 	enum ieee80211_bss_type bss_type = IEEE80211_BSS_TYPE_ESS;
568 
569 	wil_dbg_misc(wil, "connect\n");
570 	wil_print_connect_params(wil, sme);
571 
572 	if (test_bit(wil_status_fwconnecting, wil->status) ||
573 	    test_bit(wil_status_fwconnected, wil->status))
574 		return -EALREADY;
575 
576 	if (sme->ie_len > WMI_MAX_IE_LEN) {
577 		wil_err(wil, "IE too large (%td bytes)\n", sme->ie_len);
578 		return -ERANGE;
579 	}
580 
581 	rsn_eid = sme->ie ?
582 			cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
583 			NULL;
584 	if (sme->privacy && !rsn_eid)
585 		wil_info(wil, "WSC connection\n");
586 
587 	if (sme->pbss)
588 		bss_type = IEEE80211_BSS_TYPE_PBSS;
589 
590 	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
591 			       sme->ssid, sme->ssid_len,
592 			       bss_type, IEEE80211_PRIVACY_ANY);
593 	if (!bss) {
594 		wil_err(wil, "Unable to find BSS\n");
595 		return -ENOENT;
596 	}
597 
598 	ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
599 	if (!ssid_eid) {
600 		wil_err(wil, "No SSID\n");
601 		rc = -ENOENT;
602 		goto out;
603 	}
604 	wil->privacy = sme->privacy;
605 	wil->pbss = sme->pbss;
606 
607 	if (wil->privacy) {
608 		/* For secure assoc, remove old keys */
609 		rc = wmi_del_cipher_key(wil, 0, bss->bssid,
610 					WMI_KEY_USE_PAIRWISE);
611 		if (rc) {
612 			wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(PTK) failed\n");
613 			goto out;
614 		}
615 		rc = wmi_del_cipher_key(wil, 0, bss->bssid,
616 					WMI_KEY_USE_RX_GROUP);
617 		if (rc) {
618 			wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(GTK) failed\n");
619 			goto out;
620 		}
621 	}
622 
623 	/* WMI_SET_APPIE_CMD. ie may contain rsn info as well as other info
624 	 * elements. Send it also in case it's empty, to erase previously set
625 	 * ies in FW.
626 	 */
627 	rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
628 	if (rc)
629 		goto out;
630 
631 	/* WMI_CONNECT_CMD */
632 	memset(&conn, 0, sizeof(conn));
633 	switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
634 	case WLAN_CAPABILITY_DMG_TYPE_AP:
635 		conn.network_type = WMI_NETTYPE_INFRA;
636 		break;
637 	case WLAN_CAPABILITY_DMG_TYPE_PBSS:
638 		conn.network_type = WMI_NETTYPE_P2P;
639 		break;
640 	default:
641 		wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
642 			bss->capability);
643 		goto out;
644 	}
645 	if (wil->privacy) {
646 		if (rsn_eid) { /* regular secure connection */
647 			conn.dot11_auth_mode = WMI_AUTH11_SHARED;
648 			conn.auth_mode = WMI_AUTH_WPA2_PSK;
649 			conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
650 			conn.pairwise_crypto_len = 16;
651 			conn.group_crypto_type = WMI_CRYPT_AES_GCMP;
652 			conn.group_crypto_len = 16;
653 		} else { /* WSC */
654 			conn.dot11_auth_mode = WMI_AUTH11_WSC;
655 			conn.auth_mode = WMI_AUTH_NONE;
656 		}
657 	} else { /* insecure connection */
658 		conn.dot11_auth_mode = WMI_AUTH11_OPEN;
659 		conn.auth_mode = WMI_AUTH_NONE;
660 	}
661 
662 	conn.ssid_len = min_t(u8, ssid_eid[1], 32);
663 	memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
664 
665 	ch = bss->channel->hw_value;
666 	if (ch == 0) {
667 		wil_err(wil, "BSS at unknown frequency %dMhz\n",
668 			bss->channel->center_freq);
669 		rc = -EOPNOTSUPP;
670 		goto out;
671 	}
672 	conn.channel = ch - 1;
673 
674 	ether_addr_copy(conn.bssid, bss->bssid);
675 	ether_addr_copy(conn.dst_mac, bss->bssid);
676 
677 	set_bit(wil_status_fwconnecting, wil->status);
678 
679 	rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
680 	if (rc == 0) {
681 		netif_carrier_on(ndev);
682 		/* Connect can take lots of time */
683 		mod_timer(&wil->connect_timer,
684 			  jiffies + msecs_to_jiffies(2000));
685 	} else {
686 		clear_bit(wil_status_fwconnecting, wil->status);
687 	}
688 
689  out:
690 	cfg80211_put_bss(wiphy, bss);
691 
692 	return rc;
693 }
694 
695 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
696 				   struct net_device *ndev,
697 				   u16 reason_code)
698 {
699 	int rc;
700 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
701 
702 	wil_dbg_misc(wil, "disconnect: reason=%d\n", reason_code);
703 
704 	if (!(test_bit(wil_status_fwconnecting, wil->status) ||
705 	      test_bit(wil_status_fwconnected, wil->status))) {
706 		wil_err(wil, "Disconnect was called while disconnected\n");
707 		return 0;
708 	}
709 
710 	rc = wmi_call(wil, WMI_DISCONNECT_CMDID, NULL, 0,
711 		      WMI_DISCONNECT_EVENTID, NULL, 0,
712 		      WIL6210_DISCONNECT_TO_MS);
713 	if (rc)
714 		wil_err(wil, "disconnect error %d\n", rc);
715 
716 	return rc;
717 }
718 
719 static int wil_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
720 {
721 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
722 	int rc;
723 
724 	/* these parameters are explicitly not supported */
725 	if (changed & (WIPHY_PARAM_RETRY_LONG |
726 		       WIPHY_PARAM_FRAG_THRESHOLD |
727 		       WIPHY_PARAM_RTS_THRESHOLD))
728 		return -ENOTSUPP;
729 
730 	if (changed & WIPHY_PARAM_RETRY_SHORT) {
731 		rc = wmi_set_mgmt_retry(wil, wiphy->retry_short);
732 		if (rc)
733 			return rc;
734 	}
735 
736 	return 0;
737 }
738 
739 int wil_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
740 			 struct cfg80211_mgmt_tx_params *params,
741 			 u64 *cookie)
742 {
743 	const u8 *buf = params->buf;
744 	size_t len = params->len;
745 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
746 	int rc;
747 	bool tx_status = false;
748 	struct ieee80211_mgmt *mgmt_frame = (void *)buf;
749 	struct wmi_sw_tx_req_cmd *cmd;
750 	struct {
751 		struct wmi_cmd_hdr wmi;
752 		struct wmi_sw_tx_complete_event evt;
753 	} __packed evt;
754 
755 	/* Note, currently we do not support the "wait" parameter, user-space
756 	 * must call remain_on_channel before mgmt_tx or listen on a channel
757 	 * another way (AP/PCP or connected station)
758 	 * in addition we need to check if specified "chan" argument is
759 	 * different from currently "listened" channel and fail if it is.
760 	 */
761 
762 	wil_dbg_misc(wil, "mgmt_tx\n");
763 	print_hex_dump_bytes("mgmt tx frame ", DUMP_PREFIX_OFFSET, buf, len);
764 
765 	cmd = kmalloc(sizeof(*cmd) + len, GFP_KERNEL);
766 	if (!cmd) {
767 		rc = -ENOMEM;
768 		goto out;
769 	}
770 
771 	memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
772 	cmd->len = cpu_to_le16(len);
773 	memcpy(cmd->payload, buf, len);
774 
775 	rc = wmi_call(wil, WMI_SW_TX_REQ_CMDID, cmd, sizeof(*cmd) + len,
776 		      WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
777 	if (rc == 0)
778 		tx_status = !evt.evt.status;
779 
780 	kfree(cmd);
781  out:
782 	cfg80211_mgmt_tx_status(wdev, cookie ? *cookie : 0, buf, len,
783 				tx_status, GFP_KERNEL);
784 	return rc;
785 }
786 
787 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
788 				    struct cfg80211_chan_def *chandef)
789 {
790 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
791 	struct wireless_dev *wdev = wil_to_wdev(wil);
792 
793 	wdev->preset_chandef = *chandef;
794 
795 	return 0;
796 }
797 
798 static enum wmi_key_usage wil_detect_key_usage(struct wil6210_priv *wil,
799 					       bool pairwise)
800 {
801 	struct wireless_dev *wdev = wil_to_wdev(wil);
802 	enum wmi_key_usage rc;
803 
804 	if (pairwise) {
805 		rc = WMI_KEY_USE_PAIRWISE;
806 	} else {
807 		switch (wdev->iftype) {
808 		case NL80211_IFTYPE_STATION:
809 		case NL80211_IFTYPE_P2P_CLIENT:
810 			rc = WMI_KEY_USE_RX_GROUP;
811 			break;
812 		case NL80211_IFTYPE_AP:
813 		case NL80211_IFTYPE_P2P_GO:
814 			rc = WMI_KEY_USE_TX_GROUP;
815 			break;
816 		default:
817 			/* TODO: Rx GTK or Tx GTK? */
818 			wil_err(wil, "Can't determine GTK type\n");
819 			rc = WMI_KEY_USE_RX_GROUP;
820 			break;
821 		}
822 	}
823 	wil_dbg_misc(wil, "detect_key_usage: -> %s\n", key_usage_str[rc]);
824 
825 	return rc;
826 }
827 
828 static struct wil_sta_info *
829 wil_find_sta_by_key_usage(struct wil6210_priv *wil,
830 			  enum wmi_key_usage key_usage, const u8 *mac_addr)
831 {
832 	int cid = -EINVAL;
833 
834 	if (key_usage == WMI_KEY_USE_TX_GROUP)
835 		return NULL; /* not needed */
836 
837 	/* supplicant provides Rx group key in STA mode with NULL MAC address */
838 	if (mac_addr)
839 		cid = wil_find_cid(wil, mac_addr);
840 	else if (key_usage == WMI_KEY_USE_RX_GROUP)
841 		cid = wil_find_cid_by_idx(wil, 0);
842 	if (cid < 0) {
843 		wil_err(wil, "No CID for %pM %s\n", mac_addr,
844 			key_usage_str[key_usage]);
845 		return ERR_PTR(cid);
846 	}
847 
848 	return &wil->sta[cid];
849 }
850 
851 static void wil_set_crypto_rx(u8 key_index, enum wmi_key_usage key_usage,
852 			      struct wil_sta_info *cs,
853 			      struct key_params *params)
854 {
855 	struct wil_tid_crypto_rx_single *cc;
856 	int tid;
857 
858 	if (!cs)
859 		return;
860 
861 	switch (key_usage) {
862 	case WMI_KEY_USE_PAIRWISE:
863 		for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
864 			cc = &cs->tid_crypto_rx[tid].key_id[key_index];
865 			if (params->seq)
866 				memcpy(cc->pn, params->seq,
867 				       IEEE80211_GCMP_PN_LEN);
868 			else
869 				memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
870 			cc->key_set = true;
871 		}
872 		break;
873 	case WMI_KEY_USE_RX_GROUP:
874 		cc = &cs->group_crypto_rx.key_id[key_index];
875 		if (params->seq)
876 			memcpy(cc->pn, params->seq, IEEE80211_GCMP_PN_LEN);
877 		else
878 			memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
879 		cc->key_set = true;
880 		break;
881 	default:
882 		break;
883 	}
884 }
885 
886 static void wil_del_rx_key(u8 key_index, enum wmi_key_usage key_usage,
887 			   struct wil_sta_info *cs)
888 {
889 	struct wil_tid_crypto_rx_single *cc;
890 	int tid;
891 
892 	if (!cs)
893 		return;
894 
895 	switch (key_usage) {
896 	case WMI_KEY_USE_PAIRWISE:
897 		for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
898 			cc = &cs->tid_crypto_rx[tid].key_id[key_index];
899 			cc->key_set = false;
900 		}
901 		break;
902 	case WMI_KEY_USE_RX_GROUP:
903 		cc = &cs->group_crypto_rx.key_id[key_index];
904 		cc->key_set = false;
905 		break;
906 	default:
907 		break;
908 	}
909 }
910 
911 static int wil_cfg80211_add_key(struct wiphy *wiphy,
912 				struct net_device *ndev,
913 				u8 key_index, bool pairwise,
914 				const u8 *mac_addr,
915 				struct key_params *params)
916 {
917 	int rc;
918 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
919 	enum wmi_key_usage key_usage = wil_detect_key_usage(wil, pairwise);
920 	struct wil_sta_info *cs = wil_find_sta_by_key_usage(wil, key_usage,
921 							    mac_addr);
922 
923 	if (!params) {
924 		wil_err(wil, "NULL params\n");
925 		return -EINVAL;
926 	}
927 
928 	wil_dbg_misc(wil, "add_key: %pM %s[%d] PN %*phN\n",
929 		     mac_addr, key_usage_str[key_usage], key_index,
930 		     params->seq_len, params->seq);
931 
932 	if (IS_ERR(cs)) {
933 		wil_err(wil, "Not connected, %pM %s[%d] PN %*phN\n",
934 			mac_addr, key_usage_str[key_usage], key_index,
935 			params->seq_len, params->seq);
936 		return -EINVAL;
937 	}
938 
939 	wil_del_rx_key(key_index, key_usage, cs);
940 
941 	if (params->seq && params->seq_len != IEEE80211_GCMP_PN_LEN) {
942 		wil_err(wil,
943 			"Wrong PN len %d, %pM %s[%d] PN %*phN\n",
944 			params->seq_len, mac_addr,
945 			key_usage_str[key_usage], key_index,
946 			params->seq_len, params->seq);
947 		return -EINVAL;
948 	}
949 
950 	rc = wmi_add_cipher_key(wil, key_index, mac_addr, params->key_len,
951 				params->key, key_usage);
952 	if (!rc)
953 		wil_set_crypto_rx(key_index, key_usage, cs, params);
954 
955 	return rc;
956 }
957 
958 static int wil_cfg80211_del_key(struct wiphy *wiphy,
959 				struct net_device *ndev,
960 				u8 key_index, bool pairwise,
961 				const u8 *mac_addr)
962 {
963 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
964 	enum wmi_key_usage key_usage = wil_detect_key_usage(wil, pairwise);
965 	struct wil_sta_info *cs = wil_find_sta_by_key_usage(wil, key_usage,
966 							    mac_addr);
967 
968 	wil_dbg_misc(wil, "del_key: %pM %s[%d]\n", mac_addr,
969 		     key_usage_str[key_usage], key_index);
970 
971 	if (IS_ERR(cs))
972 		wil_info(wil, "Not connected, %pM %s[%d]\n",
973 			 mac_addr, key_usage_str[key_usage], key_index);
974 
975 	if (!IS_ERR_OR_NULL(cs))
976 		wil_del_rx_key(key_index, key_usage, cs);
977 
978 	return wmi_del_cipher_key(wil, key_index, mac_addr, key_usage);
979 }
980 
981 /* Need to be present or wiphy_new() will WARN */
982 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
983 					struct net_device *ndev,
984 					u8 key_index, bool unicast,
985 					bool multicast)
986 {
987 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
988 
989 	wil_dbg_misc(wil, "set_default_key: entered\n");
990 	return 0;
991 }
992 
993 static int wil_remain_on_channel(struct wiphy *wiphy,
994 				 struct wireless_dev *wdev,
995 				 struct ieee80211_channel *chan,
996 				 unsigned int duration,
997 				 u64 *cookie)
998 {
999 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1000 	int rc;
1001 
1002 	wil_dbg_misc(wil,
1003 		     "remain_on_channel: center_freq=%d, duration=%d iftype=%d\n",
1004 		     chan->center_freq, duration, wdev->iftype);
1005 
1006 	rc = wil_p2p_listen(wil, wdev, duration, chan, cookie);
1007 	return rc;
1008 }
1009 
1010 static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
1011 					struct wireless_dev *wdev,
1012 					u64 cookie)
1013 {
1014 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1015 
1016 	wil_dbg_misc(wil, "cancel_remain_on_channel\n");
1017 
1018 	return wil_p2p_cancel_listen(wil, cookie);
1019 }
1020 
1021 /**
1022  * find a specific IE in a list of IEs
1023  * return a pointer to the beginning of IE in the list
1024  * or NULL if not found
1025  */
1026 static const u8 *_wil_cfg80211_find_ie(const u8 *ies, u16 ies_len, const u8 *ie,
1027 				       u16 ie_len)
1028 {
1029 	struct ieee80211_vendor_ie *vie;
1030 	u32 oui;
1031 
1032 	/* IE tag at offset 0, length at offset 1 */
1033 	if (ie_len < 2 || 2 + ie[1] > ie_len)
1034 		return NULL;
1035 
1036 	if (ie[0] != WLAN_EID_VENDOR_SPECIFIC)
1037 		return cfg80211_find_ie(ie[0], ies, ies_len);
1038 
1039 	/* make sure there is room for 3 bytes OUI + 1 byte OUI type */
1040 	if (ie[1] < 4)
1041 		return NULL;
1042 	vie = (struct ieee80211_vendor_ie *)ie;
1043 	oui = vie->oui[0] << 16 | vie->oui[1] << 8 | vie->oui[2];
1044 	return cfg80211_find_vendor_ie(oui, vie->oui_type, ies,
1045 				       ies_len);
1046 }
1047 
1048 /**
1049  * merge the IEs in two lists into a single list.
1050  * do not include IEs from the second list which exist in the first list.
1051  * add only vendor specific IEs from second list to keep
1052  * the merged list sorted (since vendor-specific IE has the
1053  * highest tag number)
1054  * caller must free the allocated memory for merged IEs
1055  */
1056 static int _wil_cfg80211_merge_extra_ies(const u8 *ies1, u16 ies1_len,
1057 					 const u8 *ies2, u16 ies2_len,
1058 					 u8 **merged_ies, u16 *merged_len)
1059 {
1060 	u8 *buf, *dpos;
1061 	const u8 *spos;
1062 
1063 	if (ies1_len == 0 && ies2_len == 0) {
1064 		*merged_ies = NULL;
1065 		*merged_len = 0;
1066 		return 0;
1067 	}
1068 
1069 	buf = kmalloc(ies1_len + ies2_len, GFP_KERNEL);
1070 	if (!buf)
1071 		return -ENOMEM;
1072 	memcpy(buf, ies1, ies1_len);
1073 	dpos = buf + ies1_len;
1074 	spos = ies2;
1075 	while (spos + 1 < ies2 + ies2_len) {
1076 		/* IE tag at offset 0, length at offset 1 */
1077 		u16 ielen = 2 + spos[1];
1078 
1079 		if (spos + ielen > ies2 + ies2_len)
1080 			break;
1081 		if (spos[0] == WLAN_EID_VENDOR_SPECIFIC &&
1082 		    !_wil_cfg80211_find_ie(ies1, ies1_len, spos, ielen)) {
1083 			memcpy(dpos, spos, ielen);
1084 			dpos += ielen;
1085 		}
1086 		spos += ielen;
1087 	}
1088 
1089 	*merged_ies = buf;
1090 	*merged_len = dpos - buf;
1091 	return 0;
1092 }
1093 
1094 static void wil_print_bcon_data(struct cfg80211_beacon_data *b)
1095 {
1096 	print_hex_dump_bytes("head     ", DUMP_PREFIX_OFFSET,
1097 			     b->head, b->head_len);
1098 	print_hex_dump_bytes("tail     ", DUMP_PREFIX_OFFSET,
1099 			     b->tail, b->tail_len);
1100 	print_hex_dump_bytes("BCON IE  ", DUMP_PREFIX_OFFSET,
1101 			     b->beacon_ies, b->beacon_ies_len);
1102 	print_hex_dump_bytes("PROBE    ", DUMP_PREFIX_OFFSET,
1103 			     b->probe_resp, b->probe_resp_len);
1104 	print_hex_dump_bytes("PROBE IE ", DUMP_PREFIX_OFFSET,
1105 			     b->proberesp_ies, b->proberesp_ies_len);
1106 	print_hex_dump_bytes("ASSOC IE ", DUMP_PREFIX_OFFSET,
1107 			     b->assocresp_ies, b->assocresp_ies_len);
1108 }
1109 
1110 /* internal functions for device reset and starting AP */
1111 static int _wil_cfg80211_set_ies(struct wiphy *wiphy,
1112 				 struct cfg80211_beacon_data *bcon)
1113 {
1114 	int rc;
1115 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1116 	u16 len = 0, proberesp_len = 0;
1117 	u8 *ies = NULL, *proberesp = NULL;
1118 
1119 	if (bcon->probe_resp) {
1120 		struct ieee80211_mgmt *f =
1121 			(struct ieee80211_mgmt *)bcon->probe_resp;
1122 		size_t hlen = offsetof(struct ieee80211_mgmt,
1123 				       u.probe_resp.variable);
1124 		proberesp = f->u.probe_resp.variable;
1125 		proberesp_len = bcon->probe_resp_len - hlen;
1126 	}
1127 	rc = _wil_cfg80211_merge_extra_ies(proberesp,
1128 					   proberesp_len,
1129 					   bcon->proberesp_ies,
1130 					   bcon->proberesp_ies_len,
1131 					   &ies, &len);
1132 
1133 	if (rc)
1134 		goto out;
1135 
1136 	rc = wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, len, ies);
1137 	if (rc)
1138 		goto out;
1139 
1140 	if (bcon->assocresp_ies)
1141 		rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP,
1142 				bcon->assocresp_ies_len, bcon->assocresp_ies);
1143 	else
1144 		rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, len, ies);
1145 #if 0 /* to use beacon IE's, remove this #if 0 */
1146 	if (rc)
1147 		goto out;
1148 
1149 	rc = wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->tail_len, bcon->tail);
1150 #endif
1151 out:
1152 	kfree(ies);
1153 	return rc;
1154 }
1155 
1156 static int _wil_cfg80211_start_ap(struct wiphy *wiphy,
1157 				  struct net_device *ndev,
1158 				  const u8 *ssid, size_t ssid_len, u32 privacy,
1159 				  int bi, u8 chan,
1160 				  struct cfg80211_beacon_data *bcon,
1161 				  u8 hidden_ssid, u32 pbss)
1162 {
1163 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1164 	int rc;
1165 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
1166 	u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
1167 	u8 is_go = (wdev->iftype == NL80211_IFTYPE_P2P_GO);
1168 
1169 	if (pbss)
1170 		wmi_nettype = WMI_NETTYPE_P2P;
1171 
1172 	wil_dbg_misc(wil, "start_ap: is_go=%d\n", is_go);
1173 	if (is_go && !pbss) {
1174 		wil_err(wil, "P2P GO must be in PBSS\n");
1175 		return -ENOTSUPP;
1176 	}
1177 
1178 	wil_set_recovery_state(wil, fw_recovery_idle);
1179 
1180 	mutex_lock(&wil->mutex);
1181 
1182 	__wil_down(wil);
1183 	rc = __wil_up(wil);
1184 	if (rc)
1185 		goto out;
1186 
1187 	rc = wmi_set_ssid(wil, ssid_len, ssid);
1188 	if (rc)
1189 		goto out;
1190 
1191 	rc = _wil_cfg80211_set_ies(wiphy, bcon);
1192 	if (rc)
1193 		goto out;
1194 
1195 	wil->privacy = privacy;
1196 	wil->channel = chan;
1197 	wil->hidden_ssid = hidden_ssid;
1198 	wil->pbss = pbss;
1199 
1200 	netif_carrier_on(ndev);
1201 
1202 	rc = wmi_pcp_start(wil, bi, wmi_nettype, chan, hidden_ssid, is_go);
1203 	if (rc)
1204 		goto err_pcp_start;
1205 
1206 	rc = wil_bcast_init(wil);
1207 	if (rc)
1208 		goto err_bcast;
1209 
1210 	goto out; /* success */
1211 
1212 err_bcast:
1213 	wmi_pcp_stop(wil);
1214 err_pcp_start:
1215 	netif_carrier_off(ndev);
1216 out:
1217 	mutex_unlock(&wil->mutex);
1218 	return rc;
1219 }
1220 
1221 static int wil_cfg80211_change_beacon(struct wiphy *wiphy,
1222 				      struct net_device *ndev,
1223 				      struct cfg80211_beacon_data *bcon)
1224 {
1225 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1226 	int rc;
1227 	u32 privacy = 0;
1228 
1229 	wil_dbg_misc(wil, "change_beacon\n");
1230 	wil_print_bcon_data(bcon);
1231 
1232 	if (bcon->tail &&
1233 	    cfg80211_find_ie(WLAN_EID_RSN, bcon->tail,
1234 			     bcon->tail_len))
1235 		privacy = 1;
1236 
1237 	/* in case privacy has changed, need to restart the AP */
1238 	if (wil->privacy != privacy) {
1239 		struct wireless_dev *wdev = ndev->ieee80211_ptr;
1240 
1241 		wil_dbg_misc(wil, "privacy changed %d=>%d. Restarting AP\n",
1242 			     wil->privacy, privacy);
1243 
1244 		rc = _wil_cfg80211_start_ap(wiphy, ndev, wdev->ssid,
1245 					    wdev->ssid_len, privacy,
1246 					    wdev->beacon_interval,
1247 					    wil->channel, bcon,
1248 					    wil->hidden_ssid,
1249 					    wil->pbss);
1250 	} else {
1251 		rc = _wil_cfg80211_set_ies(wiphy, bcon);
1252 	}
1253 
1254 	return rc;
1255 }
1256 
1257 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
1258 				 struct net_device *ndev,
1259 				 struct cfg80211_ap_settings *info)
1260 {
1261 	int rc;
1262 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1263 	struct ieee80211_channel *channel = info->chandef.chan;
1264 	struct cfg80211_beacon_data *bcon = &info->beacon;
1265 	struct cfg80211_crypto_settings *crypto = &info->crypto;
1266 	u8 hidden_ssid;
1267 
1268 	wil_dbg_misc(wil, "start_ap\n");
1269 
1270 	if (!channel) {
1271 		wil_err(wil, "AP: No channel???\n");
1272 		return -EINVAL;
1273 	}
1274 
1275 	switch (info->hidden_ssid) {
1276 	case NL80211_HIDDEN_SSID_NOT_IN_USE:
1277 		hidden_ssid = WMI_HIDDEN_SSID_DISABLED;
1278 		break;
1279 
1280 	case NL80211_HIDDEN_SSID_ZERO_LEN:
1281 		hidden_ssid = WMI_HIDDEN_SSID_SEND_EMPTY;
1282 		break;
1283 
1284 	case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1285 		hidden_ssid = WMI_HIDDEN_SSID_CLEAR;
1286 		break;
1287 
1288 	default:
1289 		wil_err(wil, "AP: Invalid hidden SSID %d\n", info->hidden_ssid);
1290 		return -EOPNOTSUPP;
1291 	}
1292 	wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
1293 		     channel->center_freq, info->privacy ? "secure" : "open");
1294 	wil_dbg_misc(wil, "Privacy: %d auth_type %d\n",
1295 		     info->privacy, info->auth_type);
1296 	wil_dbg_misc(wil, "Hidden SSID mode: %d\n",
1297 		     info->hidden_ssid);
1298 	wil_dbg_misc(wil, "BI %d DTIM %d\n", info->beacon_interval,
1299 		     info->dtim_period);
1300 	wil_dbg_misc(wil, "PBSS %d\n", info->pbss);
1301 	print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
1302 			     info->ssid, info->ssid_len);
1303 	wil_print_bcon_data(bcon);
1304 	wil_print_crypto(wil, crypto);
1305 
1306 	rc = _wil_cfg80211_start_ap(wiphy, ndev,
1307 				    info->ssid, info->ssid_len, info->privacy,
1308 				    info->beacon_interval, channel->hw_value,
1309 				    bcon, hidden_ssid, info->pbss);
1310 
1311 	return rc;
1312 }
1313 
1314 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
1315 				struct net_device *ndev)
1316 {
1317 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1318 
1319 	wil_dbg_misc(wil, "stop_ap\n");
1320 
1321 	netif_carrier_off(ndev);
1322 	wil_set_recovery_state(wil, fw_recovery_idle);
1323 
1324 	mutex_lock(&wil->mutex);
1325 
1326 	wmi_pcp_stop(wil);
1327 
1328 	__wil_down(wil);
1329 
1330 	mutex_unlock(&wil->mutex);
1331 
1332 	return 0;
1333 }
1334 
1335 static int wil_cfg80211_add_station(struct wiphy *wiphy,
1336 				    struct net_device *dev,
1337 				    const u8 *mac,
1338 				    struct station_parameters *params)
1339 {
1340 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1341 
1342 	wil_dbg_misc(wil, "add station %pM aid %d\n", mac, params->aid);
1343 
1344 	if (!disable_ap_sme) {
1345 		wil_err(wil, "not supported with AP SME enabled\n");
1346 		return -EOPNOTSUPP;
1347 	}
1348 
1349 	if (params->aid > WIL_MAX_DMG_AID) {
1350 		wil_err(wil, "invalid aid\n");
1351 		return -EINVAL;
1352 	}
1353 
1354 	return wmi_new_sta(wil, mac, params->aid);
1355 }
1356 
1357 static int wil_cfg80211_del_station(struct wiphy *wiphy,
1358 				    struct net_device *dev,
1359 				    struct station_del_parameters *params)
1360 {
1361 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1362 
1363 	wil_dbg_misc(wil, "del_station: %pM, reason=%d\n", params->mac,
1364 		     params->reason_code);
1365 
1366 	mutex_lock(&wil->mutex);
1367 	wil6210_disconnect(wil, params->mac, params->reason_code, false);
1368 	mutex_unlock(&wil->mutex);
1369 
1370 	return 0;
1371 }
1372 
1373 static int wil_cfg80211_change_station(struct wiphy *wiphy,
1374 				       struct net_device *dev,
1375 				       const u8 *mac,
1376 				       struct station_parameters *params)
1377 {
1378 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1379 	int authorize;
1380 	int cid, i;
1381 	struct vring_tx_data *txdata = NULL;
1382 
1383 	wil_dbg_misc(wil, "change station %pM mask 0x%x set 0x%x\n", mac,
1384 		     params->sta_flags_mask, params->sta_flags_set);
1385 
1386 	if (!disable_ap_sme) {
1387 		wil_dbg_misc(wil, "not supported with AP SME enabled\n");
1388 		return -EOPNOTSUPP;
1389 	}
1390 
1391 	if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
1392 		return 0;
1393 
1394 	cid = wil_find_cid(wil, mac);
1395 	if (cid < 0) {
1396 		wil_err(wil, "station not found\n");
1397 		return -ENOLINK;
1398 	}
1399 
1400 	for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++)
1401 		if (wil->vring2cid_tid[i][0] == cid) {
1402 			txdata = &wil->vring_tx_data[i];
1403 			break;
1404 		}
1405 
1406 	if (!txdata) {
1407 		wil_err(wil, "vring data not found\n");
1408 		return -ENOLINK;
1409 	}
1410 
1411 	authorize = params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED);
1412 	txdata->dot1x_open = authorize ? 1 : 0;
1413 	wil_dbg_misc(wil, "cid %d vring %d authorize %d\n", cid, i,
1414 		     txdata->dot1x_open);
1415 
1416 	return 0;
1417 }
1418 
1419 /* probe_client handling */
1420 static void wil_probe_client_handle(struct wil6210_priv *wil,
1421 				    struct wil_probe_client_req *req)
1422 {
1423 	struct net_device *ndev = wil_to_ndev(wil);
1424 	struct wil_sta_info *sta = &wil->sta[req->cid];
1425 	/* assume STA is alive if it is still connected,
1426 	 * else FW will disconnect it
1427 	 */
1428 	bool alive = (sta->status == wil_sta_connected);
1429 
1430 	cfg80211_probe_status(ndev, sta->addr, req->cookie, alive, GFP_KERNEL);
1431 }
1432 
1433 static struct list_head *next_probe_client(struct wil6210_priv *wil)
1434 {
1435 	struct list_head *ret = NULL;
1436 
1437 	mutex_lock(&wil->probe_client_mutex);
1438 
1439 	if (!list_empty(&wil->probe_client_pending)) {
1440 		ret = wil->probe_client_pending.next;
1441 		list_del(ret);
1442 	}
1443 
1444 	mutex_unlock(&wil->probe_client_mutex);
1445 
1446 	return ret;
1447 }
1448 
1449 void wil_probe_client_worker(struct work_struct *work)
1450 {
1451 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
1452 						probe_client_worker);
1453 	struct wil_probe_client_req *req;
1454 	struct list_head *lh;
1455 
1456 	while ((lh = next_probe_client(wil)) != NULL) {
1457 		req = list_entry(lh, struct wil_probe_client_req, list);
1458 
1459 		wil_probe_client_handle(wil, req);
1460 		kfree(req);
1461 	}
1462 }
1463 
1464 void wil_probe_client_flush(struct wil6210_priv *wil)
1465 {
1466 	struct wil_probe_client_req *req, *t;
1467 
1468 	wil_dbg_misc(wil, "probe_client_flush\n");
1469 
1470 	mutex_lock(&wil->probe_client_mutex);
1471 
1472 	list_for_each_entry_safe(req, t, &wil->probe_client_pending, list) {
1473 		list_del(&req->list);
1474 		kfree(req);
1475 	}
1476 
1477 	mutex_unlock(&wil->probe_client_mutex);
1478 }
1479 
1480 static int wil_cfg80211_probe_client(struct wiphy *wiphy,
1481 				     struct net_device *dev,
1482 				     const u8 *peer, u64 *cookie)
1483 {
1484 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1485 	struct wil_probe_client_req *req;
1486 	int cid = wil_find_cid(wil, peer);
1487 
1488 	wil_dbg_misc(wil, "probe_client: %pM => CID %d\n", peer, cid);
1489 
1490 	if (cid < 0)
1491 		return -ENOLINK;
1492 
1493 	req = kzalloc(sizeof(*req), GFP_KERNEL);
1494 	if (!req)
1495 		return -ENOMEM;
1496 
1497 	req->cid = cid;
1498 	req->cookie = cid;
1499 
1500 	mutex_lock(&wil->probe_client_mutex);
1501 	list_add_tail(&req->list, &wil->probe_client_pending);
1502 	mutex_unlock(&wil->probe_client_mutex);
1503 
1504 	*cookie = req->cookie;
1505 	queue_work(wil->wq_service, &wil->probe_client_worker);
1506 	return 0;
1507 }
1508 
1509 static int wil_cfg80211_change_bss(struct wiphy *wiphy,
1510 				   struct net_device *dev,
1511 				   struct bss_parameters *params)
1512 {
1513 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1514 
1515 	if (params->ap_isolate >= 0) {
1516 		wil_dbg_misc(wil, "change_bss: ap_isolate %d => %d\n",
1517 			     wil->ap_isolate, params->ap_isolate);
1518 		wil->ap_isolate = params->ap_isolate;
1519 	}
1520 
1521 	return 0;
1522 }
1523 
1524 static int wil_cfg80211_start_p2p_device(struct wiphy *wiphy,
1525 					 struct wireless_dev *wdev)
1526 {
1527 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1528 
1529 	wil_dbg_misc(wil, "start_p2p_device: entered\n");
1530 	wil->p2p.p2p_dev_started = 1;
1531 	return 0;
1532 }
1533 
1534 static void wil_cfg80211_stop_p2p_device(struct wiphy *wiphy,
1535 					 struct wireless_dev *wdev)
1536 {
1537 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1538 	struct wil_p2p_info *p2p = &wil->p2p;
1539 
1540 	if (!p2p->p2p_dev_started)
1541 		return;
1542 
1543 	wil_dbg_misc(wil, "stop_p2p_device: entered\n");
1544 	mutex_lock(&wil->mutex);
1545 	mutex_lock(&wil->p2p_wdev_mutex);
1546 	wil_p2p_stop_radio_operations(wil);
1547 	p2p->p2p_dev_started = 0;
1548 	mutex_unlock(&wil->p2p_wdev_mutex);
1549 	mutex_unlock(&wil->mutex);
1550 }
1551 
1552 static int wil_cfg80211_set_power_mgmt(struct wiphy *wiphy,
1553 				       struct net_device *dev,
1554 				       bool enabled, int timeout)
1555 {
1556 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1557 	enum wmi_ps_profile_type ps_profile;
1558 	int rc;
1559 
1560 	if (!test_bit(WMI_FW_CAPABILITY_PS_CONFIG, wil->fw_capabilities)) {
1561 		wil_err(wil, "set_power_mgmt not supported\n");
1562 		return -EOPNOTSUPP;
1563 	}
1564 
1565 	wil_dbg_misc(wil, "enabled=%d, timeout=%d\n",
1566 		     enabled, timeout);
1567 
1568 	if (enabled)
1569 		ps_profile = WMI_PS_PROFILE_TYPE_DEFAULT;
1570 	else
1571 		ps_profile = WMI_PS_PROFILE_TYPE_PS_DISABLED;
1572 
1573 	rc  = wmi_ps_dev_profile_cfg(wil, ps_profile);
1574 	if (rc)
1575 		wil_err(wil, "wmi_ps_dev_profile_cfg failed (%d)\n", rc);
1576 
1577 	return rc;
1578 }
1579 
1580 static const struct cfg80211_ops wil_cfg80211_ops = {
1581 	.add_virtual_intf = wil_cfg80211_add_iface,
1582 	.del_virtual_intf = wil_cfg80211_del_iface,
1583 	.scan = wil_cfg80211_scan,
1584 	.abort_scan = wil_cfg80211_abort_scan,
1585 	.connect = wil_cfg80211_connect,
1586 	.disconnect = wil_cfg80211_disconnect,
1587 	.set_wiphy_params = wil_cfg80211_set_wiphy_params,
1588 	.change_virtual_intf = wil_cfg80211_change_iface,
1589 	.get_station = wil_cfg80211_get_station,
1590 	.dump_station = wil_cfg80211_dump_station,
1591 	.remain_on_channel = wil_remain_on_channel,
1592 	.cancel_remain_on_channel = wil_cancel_remain_on_channel,
1593 	.mgmt_tx = wil_cfg80211_mgmt_tx,
1594 	.set_monitor_channel = wil_cfg80211_set_channel,
1595 	.add_key = wil_cfg80211_add_key,
1596 	.del_key = wil_cfg80211_del_key,
1597 	.set_default_key = wil_cfg80211_set_default_key,
1598 	/* AP mode */
1599 	.change_beacon = wil_cfg80211_change_beacon,
1600 	.start_ap = wil_cfg80211_start_ap,
1601 	.stop_ap = wil_cfg80211_stop_ap,
1602 	.add_station = wil_cfg80211_add_station,
1603 	.del_station = wil_cfg80211_del_station,
1604 	.change_station = wil_cfg80211_change_station,
1605 	.probe_client = wil_cfg80211_probe_client,
1606 	.change_bss = wil_cfg80211_change_bss,
1607 	/* P2P device */
1608 	.start_p2p_device = wil_cfg80211_start_p2p_device,
1609 	.stop_p2p_device = wil_cfg80211_stop_p2p_device,
1610 	.set_power_mgmt = wil_cfg80211_set_power_mgmt,
1611 };
1612 
1613 static void wil_wiphy_init(struct wiphy *wiphy)
1614 {
1615 	wiphy->max_scan_ssids = 1;
1616 	wiphy->max_scan_ie_len = WMI_MAX_IE_LEN;
1617 	wiphy->max_remain_on_channel_duration = WIL_MAX_ROC_DURATION_MS;
1618 	wiphy->max_num_pmkids = 0 /* TODO: */;
1619 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1620 				 BIT(NL80211_IFTYPE_AP) |
1621 				 BIT(NL80211_IFTYPE_P2P_CLIENT) |
1622 				 BIT(NL80211_IFTYPE_P2P_GO) |
1623 				 BIT(NL80211_IFTYPE_P2P_DEVICE) |
1624 				 BIT(NL80211_IFTYPE_MONITOR);
1625 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
1626 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
1627 			WIPHY_FLAG_PS_ON_BY_DEFAULT;
1628 	if (!disable_ap_sme)
1629 		wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME;
1630 	dev_dbg(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
1631 		__func__, wiphy->flags);
1632 	wiphy->probe_resp_offload =
1633 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
1634 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
1635 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
1636 
1637 	wiphy->bands[NL80211_BAND_60GHZ] = &wil_band_60ghz;
1638 
1639 	/* TODO: figure this out */
1640 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
1641 
1642 	wiphy->cipher_suites = wil_cipher_suites;
1643 	wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
1644 	wiphy->mgmt_stypes = wil_mgmt_stypes;
1645 	wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
1646 }
1647 
1648 struct wireless_dev *wil_cfg80211_init(struct device *dev)
1649 {
1650 	int rc = 0;
1651 	struct wireless_dev *wdev;
1652 
1653 	dev_dbg(dev, "%s()\n", __func__);
1654 
1655 	wdev = kzalloc(sizeof(*wdev), GFP_KERNEL);
1656 	if (!wdev)
1657 		return ERR_PTR(-ENOMEM);
1658 
1659 	wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
1660 				sizeof(struct wil6210_priv));
1661 	if (!wdev->wiphy) {
1662 		rc = -ENOMEM;
1663 		goto out;
1664 	}
1665 
1666 	set_wiphy_dev(wdev->wiphy, dev);
1667 	wil_wiphy_init(wdev->wiphy);
1668 
1669 	return wdev;
1670 
1671 out:
1672 	kfree(wdev);
1673 
1674 	return ERR_PTR(rc);
1675 }
1676 
1677 void wil_wdev_free(struct wil6210_priv *wil)
1678 {
1679 	struct wireless_dev *wdev = wil_to_wdev(wil);
1680 
1681 	dev_dbg(wil_to_dev(wil), "%s()\n", __func__);
1682 
1683 	if (!wdev)
1684 		return;
1685 
1686 	wiphy_free(wdev->wiphy);
1687 	kfree(wdev);
1688 }
1689 
1690 void wil_p2p_wdev_free(struct wil6210_priv *wil)
1691 {
1692 	struct wireless_dev *p2p_wdev;
1693 
1694 	mutex_lock(&wil->p2p_wdev_mutex);
1695 	p2p_wdev = wil->p2p_wdev;
1696 	wil->p2p_wdev = NULL;
1697 	wil->radio_wdev = wil_to_wdev(wil);
1698 	mutex_unlock(&wil->p2p_wdev_mutex);
1699 	if (p2p_wdev) {
1700 		cfg80211_unregister_wdev(p2p_wdev);
1701 		kfree(p2p_wdev);
1702 	}
1703 }
1704