1 /*
2  * Copyright (c) 2012 Qualcomm Atheros, Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include "wil6210.h"
18 #include "wmi.h"
19 
20 #define CHAN60G(_channel, _flags) {				\
21 	.band			= IEEE80211_BAND_60GHZ,		\
22 	.center_freq		= 56160 + (2160 * (_channel)),	\
23 	.hw_value		= (_channel),			\
24 	.flags			= (_flags),			\
25 	.max_antenna_gain	= 0,				\
26 	.max_power		= 40,				\
27 }
28 
29 static struct ieee80211_channel wil_60ghz_channels[] = {
30 	CHAN60G(1, 0),
31 	CHAN60G(2, 0),
32 	CHAN60G(3, 0),
33 /* channel 4 not supported yet */
34 };
35 
36 static struct ieee80211_supported_band wil_band_60ghz = {
37 	.channels = wil_60ghz_channels,
38 	.n_channels = ARRAY_SIZE(wil_60ghz_channels),
39 	.ht_cap = {
40 		.ht_supported = true,
41 		.cap = 0, /* TODO */
42 		.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
43 		.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
44 		.mcs = {
45 				/* MCS 1..12 - SC PHY */
46 			.rx_mask = {0xfe, 0x1f}, /* 1..12 */
47 			.tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
48 		},
49 	},
50 };
51 
52 static const struct ieee80211_txrx_stypes
53 wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
54 	[NL80211_IFTYPE_STATION] = {
55 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
56 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
57 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
58 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
59 	},
60 	[NL80211_IFTYPE_AP] = {
61 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
62 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
63 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
64 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
65 	},
66 	[NL80211_IFTYPE_P2P_CLIENT] = {
67 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
68 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
69 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
70 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
71 	},
72 	[NL80211_IFTYPE_P2P_GO] = {
73 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
74 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
75 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
76 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
77 	},
78 };
79 
80 static const u32 wil_cipher_suites[] = {
81 	WLAN_CIPHER_SUITE_GCMP,
82 };
83 
84 int wil_iftype_nl2wmi(enum nl80211_iftype type)
85 {
86 	static const struct {
87 		enum nl80211_iftype nl;
88 		enum wmi_network_type wmi;
89 	} __nl2wmi[] = {
90 		{NL80211_IFTYPE_ADHOC,		WMI_NETTYPE_ADHOC},
91 		{NL80211_IFTYPE_STATION,	WMI_NETTYPE_INFRA},
92 		{NL80211_IFTYPE_AP,		WMI_NETTYPE_AP},
93 		{NL80211_IFTYPE_P2P_CLIENT,	WMI_NETTYPE_P2P},
94 		{NL80211_IFTYPE_P2P_GO,		WMI_NETTYPE_P2P},
95 		{NL80211_IFTYPE_MONITOR,	WMI_NETTYPE_ADHOC}, /* FIXME */
96 	};
97 	uint i;
98 
99 	for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
100 		if (__nl2wmi[i].nl == type)
101 			return __nl2wmi[i].wmi;
102 	}
103 
104 	return -EOPNOTSUPP;
105 }
106 
107 static int wil_cid_fill_sinfo(struct wil6210_priv *wil, int cid,
108 			      struct station_info *sinfo)
109 {
110 	struct wmi_notify_req_cmd cmd = {
111 		.cid = cid,
112 		.interval_usec = 0,
113 	};
114 	struct {
115 		struct wil6210_mbox_hdr_wmi wmi;
116 		struct wmi_notify_req_done_event evt;
117 	} __packed reply;
118 	struct wil_net_stats *stats = &wil->sta[cid].stats;
119 	int rc;
120 
121 	rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
122 		      WMI_NOTIFY_REQ_DONE_EVENTID, &reply, sizeof(reply), 20);
123 	if (rc)
124 		return rc;
125 
126 	wil_dbg_wmi(wil, "Link status for CID %d: {\n"
127 		    "  MCS %d TSF 0x%016llx\n"
128 		    "  BF status 0x%08x SNR 0x%08x SQI %d%%\n"
129 		    "  Tx Tpt %d goodput %d Rx goodput %d\n"
130 		    "  Sectors(rx:tx) my %d:%d peer %d:%d\n""}\n",
131 		    cid, le16_to_cpu(reply.evt.bf_mcs),
132 		    le64_to_cpu(reply.evt.tsf), reply.evt.status,
133 		    le32_to_cpu(reply.evt.snr_val),
134 		    reply.evt.sqi,
135 		    le32_to_cpu(reply.evt.tx_tpt),
136 		    le32_to_cpu(reply.evt.tx_goodput),
137 		    le32_to_cpu(reply.evt.rx_goodput),
138 		    le16_to_cpu(reply.evt.my_rx_sector),
139 		    le16_to_cpu(reply.evt.my_tx_sector),
140 		    le16_to_cpu(reply.evt.other_rx_sector),
141 		    le16_to_cpu(reply.evt.other_tx_sector));
142 
143 	sinfo->generation = wil->sinfo_gen;
144 
145 	sinfo->filled = STATION_INFO_RX_BYTES |
146 			STATION_INFO_TX_BYTES |
147 			STATION_INFO_RX_PACKETS |
148 			STATION_INFO_TX_PACKETS |
149 			STATION_INFO_RX_BITRATE |
150 			STATION_INFO_TX_BITRATE |
151 			STATION_INFO_RX_DROP_MISC |
152 			STATION_INFO_TX_FAILED;
153 
154 	sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
155 	sinfo->txrate.mcs = le16_to_cpu(reply.evt.bf_mcs);
156 	sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
157 	sinfo->rxrate.mcs = stats->last_mcs_rx;
158 	sinfo->rx_bytes = stats->rx_bytes;
159 	sinfo->rx_packets = stats->rx_packets;
160 	sinfo->rx_dropped_misc = stats->rx_dropped;
161 	sinfo->tx_bytes = stats->tx_bytes;
162 	sinfo->tx_packets = stats->tx_packets;
163 	sinfo->tx_failed = stats->tx_errors;
164 
165 	if (test_bit(wil_status_fwconnected, &wil->status)) {
166 		sinfo->filled |= STATION_INFO_SIGNAL;
167 		sinfo->signal = reply.evt.sqi;
168 	}
169 
170 	return rc;
171 }
172 
173 static int wil_cfg80211_get_station(struct wiphy *wiphy,
174 				    struct net_device *ndev,
175 				    const u8 *mac, struct station_info *sinfo)
176 {
177 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
178 	int rc;
179 
180 	int cid = wil_find_cid(wil, mac);
181 
182 	wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
183 	if (cid < 0)
184 		return cid;
185 
186 	rc = wil_cid_fill_sinfo(wil, cid, sinfo);
187 
188 	return rc;
189 }
190 
191 /*
192  * Find @idx-th active STA for station dump.
193  */
194 static int wil_find_cid_by_idx(struct wil6210_priv *wil, int idx)
195 {
196 	int i;
197 
198 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
199 		if (wil->sta[i].status == wil_sta_unused)
200 			continue;
201 		if (idx == 0)
202 			return i;
203 		idx--;
204 	}
205 
206 	return -ENOENT;
207 }
208 
209 static int wil_cfg80211_dump_station(struct wiphy *wiphy,
210 				     struct net_device *dev, int idx,
211 				     u8 *mac, struct station_info *sinfo)
212 {
213 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
214 	int rc;
215 	int cid = wil_find_cid_by_idx(wil, idx);
216 
217 	if (cid < 0)
218 		return -ENOENT;
219 
220 	memcpy(mac, wil->sta[cid].addr, ETH_ALEN);
221 	wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
222 
223 	rc = wil_cid_fill_sinfo(wil, cid, sinfo);
224 
225 	return rc;
226 }
227 
228 static int wil_cfg80211_change_iface(struct wiphy *wiphy,
229 				     struct net_device *ndev,
230 				     enum nl80211_iftype type, u32 *flags,
231 				     struct vif_params *params)
232 {
233 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
234 	struct wireless_dev *wdev = wil->wdev;
235 
236 	switch (type) {
237 	case NL80211_IFTYPE_STATION:
238 	case NL80211_IFTYPE_AP:
239 	case NL80211_IFTYPE_P2P_CLIENT:
240 	case NL80211_IFTYPE_P2P_GO:
241 		break;
242 	case NL80211_IFTYPE_MONITOR:
243 		if (flags)
244 			wil->monitor_flags = *flags;
245 		else
246 			wil->monitor_flags = 0;
247 
248 		break;
249 	default:
250 		return -EOPNOTSUPP;
251 	}
252 
253 	wdev->iftype = type;
254 
255 	return 0;
256 }
257 
258 static int wil_cfg80211_scan(struct wiphy *wiphy,
259 			     struct cfg80211_scan_request *request)
260 {
261 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
262 	struct wireless_dev *wdev = wil->wdev;
263 	struct {
264 		struct wmi_start_scan_cmd cmd;
265 		u16 chnl[4];
266 	} __packed cmd;
267 	uint i, n;
268 	int rc;
269 
270 	if (wil->scan_request) {
271 		wil_err(wil, "Already scanning\n");
272 		return -EAGAIN;
273 	}
274 
275 	/* check we are client side */
276 	switch (wdev->iftype) {
277 	case NL80211_IFTYPE_STATION:
278 	case NL80211_IFTYPE_P2P_CLIENT:
279 		break;
280 	default:
281 		return -EOPNOTSUPP;
282 	}
283 
284 	/* FW don't support scan after connection attempt */
285 	if (test_bit(wil_status_dontscan, &wil->status)) {
286 		wil_err(wil, "Can't scan now\n");
287 		return -EBUSY;
288 	}
289 
290 	wil->scan_request = request;
291 	mod_timer(&wil->scan_timer, jiffies + WIL6210_SCAN_TO);
292 
293 	memset(&cmd, 0, sizeof(cmd));
294 	cmd.cmd.num_channels = 0;
295 	n = min(request->n_channels, 4U);
296 	for (i = 0; i < n; i++) {
297 		int ch = request->channels[i]->hw_value;
298 		if (ch == 0) {
299 			wil_err(wil,
300 				"Scan requested for unknown frequency %dMhz\n",
301 				request->channels[i]->center_freq);
302 			continue;
303 		}
304 		/* 0-based channel indexes */
305 		cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
306 		wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
307 			     request->channels[i]->center_freq);
308 	}
309 
310 	rc = wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
311 			cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
312 
313 	if (rc)
314 		wil->scan_request = NULL;
315 
316 	return rc;
317 }
318 
319 static int wil_cfg80211_connect(struct wiphy *wiphy,
320 				struct net_device *ndev,
321 				struct cfg80211_connect_params *sme)
322 {
323 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
324 	struct cfg80211_bss *bss;
325 	struct wmi_connect_cmd conn;
326 	const u8 *ssid_eid;
327 	const u8 *rsn_eid;
328 	int ch;
329 	int rc = 0;
330 
331 	if (test_bit(wil_status_fwconnecting, &wil->status) ||
332 	    test_bit(wil_status_fwconnected, &wil->status))
333 		return -EALREADY;
334 
335 	bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
336 			       sme->ssid, sme->ssid_len,
337 			       WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
338 	if (!bss) {
339 		wil_err(wil, "Unable to find BSS\n");
340 		return -ENOENT;
341 	}
342 
343 	ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
344 	if (!ssid_eid) {
345 		wil_err(wil, "No SSID\n");
346 		rc = -ENOENT;
347 		goto out;
348 	}
349 
350 	rsn_eid = sme->ie ?
351 			cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
352 			NULL;
353 	if (rsn_eid) {
354 		if (sme->ie_len > WMI_MAX_IE_LEN) {
355 			rc = -ERANGE;
356 			wil_err(wil, "IE too large (%td bytes)\n",
357 				sme->ie_len);
358 			goto out;
359 		}
360 		/*
361 		 * For secure assoc, send:
362 		 * (1) WMI_DELETE_CIPHER_KEY_CMD
363 		 * (2) WMI_SET_APPIE_CMD
364 		 */
365 		rc = wmi_del_cipher_key(wil, 0, bss->bssid);
366 		if (rc) {
367 			wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD failed\n");
368 			goto out;
369 		}
370 		/* WMI_SET_APPIE_CMD */
371 		rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
372 		if (rc) {
373 			wil_err(wil, "WMI_SET_APPIE_CMD failed\n");
374 			goto out;
375 		}
376 	}
377 
378 	/* WMI_CONNECT_CMD */
379 	memset(&conn, 0, sizeof(conn));
380 	switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
381 	case WLAN_CAPABILITY_DMG_TYPE_AP:
382 		conn.network_type = WMI_NETTYPE_INFRA;
383 		break;
384 	case WLAN_CAPABILITY_DMG_TYPE_PBSS:
385 		conn.network_type = WMI_NETTYPE_P2P;
386 		break;
387 	default:
388 		wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
389 			bss->capability);
390 		goto out;
391 	}
392 	if (rsn_eid) {
393 		conn.dot11_auth_mode = WMI_AUTH11_SHARED;
394 		conn.auth_mode = WMI_AUTH_WPA2_PSK;
395 		conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
396 		conn.pairwise_crypto_len = 16;
397 	} else {
398 		conn.dot11_auth_mode = WMI_AUTH11_OPEN;
399 		conn.auth_mode = WMI_AUTH_NONE;
400 	}
401 
402 	conn.ssid_len = min_t(u8, ssid_eid[1], 32);
403 	memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
404 
405 	ch = bss->channel->hw_value;
406 	if (ch == 0) {
407 		wil_err(wil, "BSS at unknown frequency %dMhz\n",
408 			bss->channel->center_freq);
409 		rc = -EOPNOTSUPP;
410 		goto out;
411 	}
412 	conn.channel = ch - 1;
413 
414 	memcpy(conn.bssid, bss->bssid, ETH_ALEN);
415 	memcpy(conn.dst_mac, bss->bssid, ETH_ALEN);
416 
417 	set_bit(wil_status_fwconnecting, &wil->status);
418 
419 	rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
420 	if (rc == 0) {
421 		/* Connect can take lots of time */
422 		mod_timer(&wil->connect_timer,
423 			  jiffies + msecs_to_jiffies(2000));
424 	} else {
425 		clear_bit(wil_status_fwconnecting, &wil->status);
426 	}
427 
428  out:
429 	cfg80211_put_bss(wiphy, bss);
430 
431 	return rc;
432 }
433 
434 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
435 				   struct net_device *ndev,
436 				   u16 reason_code)
437 {
438 	int rc;
439 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
440 
441 	rc = wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
442 
443 	return rc;
444 }
445 
446 static int wil_cfg80211_mgmt_tx(struct wiphy *wiphy,
447 				struct wireless_dev *wdev,
448 				struct cfg80211_mgmt_tx_params *params,
449 				u64 *cookie)
450 {
451 	const u8 *buf = params->buf;
452 	size_t len = params->len;
453 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
454 	int rc;
455 	struct ieee80211_mgmt *mgmt_frame = (void *)buf;
456 	struct wmi_sw_tx_req_cmd *cmd;
457 	struct {
458 		struct wil6210_mbox_hdr_wmi wmi;
459 		struct wmi_sw_tx_complete_event evt;
460 	} __packed evt;
461 
462 	cmd = kmalloc(sizeof(*cmd) + len, GFP_KERNEL);
463 	if (!cmd)
464 		return -ENOMEM;
465 
466 	memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
467 	cmd->len = cpu_to_le16(len);
468 	memcpy(cmd->payload, buf, len);
469 
470 	rc = wmi_call(wil, WMI_SW_TX_REQ_CMDID, cmd, sizeof(*cmd) + len,
471 		      WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
472 	if (rc == 0)
473 		rc = evt.evt.status;
474 
475 	kfree(cmd);
476 
477 	return rc;
478 }
479 
480 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
481 				    struct cfg80211_chan_def *chandef)
482 {
483 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
484 	struct wireless_dev *wdev = wil->wdev;
485 
486 	wdev->preset_chandef = *chandef;
487 
488 	return 0;
489 }
490 
491 static int wil_cfg80211_add_key(struct wiphy *wiphy,
492 				struct net_device *ndev,
493 				u8 key_index, bool pairwise,
494 				const u8 *mac_addr,
495 				struct key_params *params)
496 {
497 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
498 
499 	/* group key is not used */
500 	if (!pairwise)
501 		return 0;
502 
503 	return wmi_add_cipher_key(wil, key_index, mac_addr,
504 				  params->key_len, params->key);
505 }
506 
507 static int wil_cfg80211_del_key(struct wiphy *wiphy,
508 				struct net_device *ndev,
509 				u8 key_index, bool pairwise,
510 				const u8 *mac_addr)
511 {
512 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
513 
514 	/* group key is not used */
515 	if (!pairwise)
516 		return 0;
517 
518 	return wmi_del_cipher_key(wil, key_index, mac_addr);
519 }
520 
521 /* Need to be present or wiphy_new() will WARN */
522 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
523 					struct net_device *ndev,
524 					u8 key_index, bool unicast,
525 					bool multicast)
526 {
527 	return 0;
528 }
529 
530 static int wil_remain_on_channel(struct wiphy *wiphy,
531 				 struct wireless_dev *wdev,
532 				 struct ieee80211_channel *chan,
533 				 unsigned int duration,
534 				 u64 *cookie)
535 {
536 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
537 	int rc;
538 
539 	/* TODO: handle duration */
540 	wil_info(wil, "%s(%d, %d ms)\n", __func__, chan->center_freq, duration);
541 
542 	rc = wmi_set_channel(wil, chan->hw_value);
543 	if (rc)
544 		return rc;
545 
546 	rc = wmi_rxon(wil, true);
547 
548 	return rc;
549 }
550 
551 static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
552 					struct wireless_dev *wdev,
553 					u64 cookie)
554 {
555 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
556 	int rc;
557 
558 	wil_info(wil, "%s()\n", __func__);
559 
560 	rc = wmi_rxon(wil, false);
561 
562 	return rc;
563 }
564 
565 static int wil_fix_bcon(struct wil6210_priv *wil,
566 			struct cfg80211_beacon_data *bcon)
567 {
568 	struct ieee80211_mgmt *f = (struct ieee80211_mgmt *)bcon->probe_resp;
569 	size_t hlen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
570 	int rc = 0;
571 
572 	if (bcon->probe_resp_len <= hlen)
573 		return 0;
574 
575 	if (!bcon->proberesp_ies) {
576 		bcon->proberesp_ies = f->u.probe_resp.variable;
577 		bcon->proberesp_ies_len = bcon->probe_resp_len - hlen;
578 		rc = 1;
579 	}
580 	if (!bcon->assocresp_ies) {
581 		bcon->assocresp_ies = f->u.probe_resp.variable;
582 		bcon->assocresp_ies_len = bcon->probe_resp_len - hlen;
583 		rc = 1;
584 	}
585 
586 	return rc;
587 }
588 
589 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
590 				 struct net_device *ndev,
591 				 struct cfg80211_ap_settings *info)
592 {
593 	int rc = 0;
594 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
595 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
596 	struct ieee80211_channel *channel = info->chandef.chan;
597 	struct cfg80211_beacon_data *bcon = &info->beacon;
598 	u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
599 
600 	if (!channel) {
601 		wil_err(wil, "AP: No channel???\n");
602 		return -EINVAL;
603 	}
604 
605 	wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
606 		     channel->center_freq, info->privacy ? "secure" : "open");
607 	print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
608 			     info->ssid, info->ssid_len);
609 
610 	if (wil_fix_bcon(wil, bcon))
611 		wil_dbg_misc(wil, "Fixed bcon\n");
612 
613 	mutex_lock(&wil->mutex);
614 
615 	rc = wil_reset(wil);
616 	if (rc)
617 		goto out;
618 
619 	/* Rx VRING. */
620 	rc = wil_rx_init(wil);
621 	if (rc)
622 		goto out;
623 
624 	rc = wmi_set_ssid(wil, info->ssid_len, info->ssid);
625 	if (rc)
626 		goto out;
627 
628 	/* MAC address - pre-requisite for other commands */
629 	wmi_set_mac_address(wil, ndev->dev_addr);
630 
631 	/* IE's */
632 	/* bcon 'head IE's are not relevant for 60g band */
633 	/*
634 	 * FW do not form regular beacon, so bcon IE's are not set
635 	 * For the DMG bcon, when it will be supported, bcon IE's will
636 	 * be reused; add something like:
637 	 * wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->beacon_ies_len,
638 	 * bcon->beacon_ies);
639 	 */
640 	wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, bcon->proberesp_ies_len,
641 		   bcon->proberesp_ies);
642 	wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, bcon->assocresp_ies_len,
643 		   bcon->assocresp_ies);
644 
645 	wil->secure_pcp = info->privacy;
646 
647 	rc = wmi_pcp_start(wil, info->beacon_interval, wmi_nettype,
648 			   channel->hw_value);
649 	if (rc)
650 		goto out;
651 
652 
653 	netif_carrier_on(ndev);
654 
655 out:
656 	mutex_unlock(&wil->mutex);
657 	return rc;
658 }
659 
660 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
661 				struct net_device *ndev)
662 {
663 	int rc = 0;
664 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
665 
666 	mutex_lock(&wil->mutex);
667 
668 	rc = wmi_pcp_stop(wil);
669 
670 	mutex_unlock(&wil->mutex);
671 	return rc;
672 }
673 
674 static int wil_cfg80211_del_station(struct wiphy *wiphy,
675 				    struct net_device *dev, const u8 *mac)
676 {
677 	struct wil6210_priv *wil = wiphy_to_wil(wiphy);
678 
679 	mutex_lock(&wil->mutex);
680 	wil6210_disconnect(wil, mac);
681 	mutex_unlock(&wil->mutex);
682 
683 	return 0;
684 }
685 
686 static struct cfg80211_ops wil_cfg80211_ops = {
687 	.scan = wil_cfg80211_scan,
688 	.connect = wil_cfg80211_connect,
689 	.disconnect = wil_cfg80211_disconnect,
690 	.change_virtual_intf = wil_cfg80211_change_iface,
691 	.get_station = wil_cfg80211_get_station,
692 	.dump_station = wil_cfg80211_dump_station,
693 	.remain_on_channel = wil_remain_on_channel,
694 	.cancel_remain_on_channel = wil_cancel_remain_on_channel,
695 	.mgmt_tx = wil_cfg80211_mgmt_tx,
696 	.set_monitor_channel = wil_cfg80211_set_channel,
697 	.add_key = wil_cfg80211_add_key,
698 	.del_key = wil_cfg80211_del_key,
699 	.set_default_key = wil_cfg80211_set_default_key,
700 	/* AP mode */
701 	.start_ap = wil_cfg80211_start_ap,
702 	.stop_ap = wil_cfg80211_stop_ap,
703 	.del_station = wil_cfg80211_del_station,
704 };
705 
706 static void wil_wiphy_init(struct wiphy *wiphy)
707 {
708 	/* TODO: set real value */
709 	wiphy->max_scan_ssids = 10;
710 	wiphy->max_num_pmkids = 0 /* TODO: */;
711 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
712 				 BIT(NL80211_IFTYPE_AP) |
713 				 BIT(NL80211_IFTYPE_MONITOR);
714 	/* TODO: enable P2P when integrated with supplicant:
715 	 * BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO)
716 	 */
717 	wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
718 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
719 	dev_warn(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
720 		 __func__, wiphy->flags);
721 	wiphy->probe_resp_offload =
722 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
723 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
724 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
725 
726 	wiphy->bands[IEEE80211_BAND_60GHZ] = &wil_band_60ghz;
727 
728 	/* TODO: figure this out */
729 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
730 
731 	wiphy->cipher_suites = wil_cipher_suites;
732 	wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
733 	wiphy->mgmt_stypes = wil_mgmt_stypes;
734 }
735 
736 struct wireless_dev *wil_cfg80211_init(struct device *dev)
737 {
738 	int rc = 0;
739 	struct wireless_dev *wdev;
740 
741 	wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
742 	if (!wdev)
743 		return ERR_PTR(-ENOMEM);
744 
745 	wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
746 				sizeof(struct wil6210_priv));
747 	if (!wdev->wiphy) {
748 		rc = -ENOMEM;
749 		goto out;
750 	}
751 
752 	set_wiphy_dev(wdev->wiphy, dev);
753 	wil_wiphy_init(wdev->wiphy);
754 
755 	rc = wiphy_register(wdev->wiphy);
756 	if (rc < 0)
757 		goto out_failed_reg;
758 
759 	return wdev;
760 
761 out_failed_reg:
762 	wiphy_free(wdev->wiphy);
763 out:
764 	kfree(wdev);
765 
766 	return ERR_PTR(rc);
767 }
768 
769 void wil_wdev_free(struct wil6210_priv *wil)
770 {
771 	struct wireless_dev *wdev = wil_to_wdev(wil);
772 
773 	if (!wdev)
774 		return;
775 
776 	wiphy_unregister(wdev->wiphy);
777 	wiphy_free(wdev->wiphy);
778 	kfree(wdev);
779 }
780