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