1 /*
2  * Copyright (c) 2012-2012 Quantenna Communications, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/etherdevice.h>
19 #include <linux/vmalloc.h>
20 #include <linux/ieee80211.h>
21 #include <net/cfg80211.h>
22 #include <net/netlink.h>
23 
24 #include "cfg80211.h"
25 #include "commands.h"
26 #include "core.h"
27 #include "util.h"
28 #include "bus.h"
29 
30 /* Supported rates to be advertised to the cfg80211 */
31 static struct ieee80211_rate qtnf_rates_2g[] = {
32 	{.bitrate = 10, .hw_value = 2, },
33 	{.bitrate = 20, .hw_value = 4, },
34 	{.bitrate = 55, .hw_value = 11, },
35 	{.bitrate = 110, .hw_value = 22, },
36 	{.bitrate = 60, .hw_value = 12, },
37 	{.bitrate = 90, .hw_value = 18, },
38 	{.bitrate = 120, .hw_value = 24, },
39 	{.bitrate = 180, .hw_value = 36, },
40 	{.bitrate = 240, .hw_value = 48, },
41 	{.bitrate = 360, .hw_value = 72, },
42 	{.bitrate = 480, .hw_value = 96, },
43 	{.bitrate = 540, .hw_value = 108, },
44 };
45 
46 /* Supported rates to be advertised to the cfg80211 */
47 static struct ieee80211_rate qtnf_rates_5g[] = {
48 	{.bitrate = 60, .hw_value = 12, },
49 	{.bitrate = 90, .hw_value = 18, },
50 	{.bitrate = 120, .hw_value = 24, },
51 	{.bitrate = 180, .hw_value = 36, },
52 	{.bitrate = 240, .hw_value = 48, },
53 	{.bitrate = 360, .hw_value = 72, },
54 	{.bitrate = 480, .hw_value = 96, },
55 	{.bitrate = 540, .hw_value = 108, },
56 };
57 
58 /* Supported crypto cipher suits to be advertised to cfg80211 */
59 static const u32 qtnf_cipher_suites[] = {
60 	WLAN_CIPHER_SUITE_TKIP,
61 	WLAN_CIPHER_SUITE_CCMP,
62 	WLAN_CIPHER_SUITE_AES_CMAC,
63 };
64 
65 /* Supported mgmt frame types to be advertised to cfg80211 */
66 static const struct ieee80211_txrx_stypes
67 qtnf_mgmt_stypes[NUM_NL80211_IFTYPES] = {
68 	[NL80211_IFTYPE_STATION] = {
69 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4),
70 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
71 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
72 	},
73 	[NL80211_IFTYPE_AP] = {
74 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4),
75 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
76 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
77 	},
78 };
79 
80 static int
81 qtnf_change_virtual_intf(struct wiphy *wiphy,
82 			 struct net_device *dev,
83 			 enum nl80211_iftype type,
84 			 struct vif_params *params)
85 {
86 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
87 	u8 *mac_addr;
88 	int ret;
89 
90 	if (params)
91 		mac_addr = params->macaddr;
92 	else
93 		mac_addr = NULL;
94 
95 	qtnf_scan_done(vif->mac, true);
96 
97 	ret = qtnf_cmd_send_change_intf_type(vif, type, mac_addr);
98 	if (ret) {
99 		pr_err("VIF%u.%u: failed to change VIF type: %d\n",
100 		       vif->mac->macid, vif->vifid, ret);
101 		return ret;
102 	}
103 
104 	vif->wdev.iftype = type;
105 	return 0;
106 }
107 
108 int qtnf_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
109 {
110 	struct net_device *netdev =  wdev->netdev;
111 	struct qtnf_vif *vif;
112 
113 	if (WARN_ON(!netdev))
114 		return -EFAULT;
115 
116 	vif = qtnf_netdev_get_priv(wdev->netdev);
117 
118 	if (qtnf_cmd_send_del_intf(vif))
119 		pr_err("VIF%u.%u: failed to delete VIF\n", vif->mac->macid,
120 		       vif->vifid);
121 
122 	/* Stop data */
123 	netif_tx_stop_all_queues(netdev);
124 	if (netif_carrier_ok(netdev))
125 		netif_carrier_off(netdev);
126 
127 	if (netdev->reg_state == NETREG_REGISTERED)
128 		unregister_netdevice(netdev);
129 
130 	vif->netdev->ieee80211_ptr = NULL;
131 	vif->netdev = NULL;
132 	vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
133 	eth_zero_addr(vif->mac_addr);
134 
135 	return 0;
136 }
137 
138 static struct wireless_dev *qtnf_add_virtual_intf(struct wiphy *wiphy,
139 						  const char *name,
140 						  unsigned char name_assign_t,
141 						  enum nl80211_iftype type,
142 						  struct vif_params *params)
143 {
144 	struct qtnf_wmac *mac;
145 	struct qtnf_vif *vif;
146 	u8 *mac_addr = NULL;
147 
148 	mac = wiphy_priv(wiphy);
149 
150 	if (!mac)
151 		return ERR_PTR(-EFAULT);
152 
153 	switch (type) {
154 	case NL80211_IFTYPE_STATION:
155 	case NL80211_IFTYPE_AP:
156 		vif = qtnf_mac_get_free_vif(mac);
157 		if (!vif) {
158 			pr_err("MAC%u: no free VIF available\n", mac->macid);
159 			return ERR_PTR(-EFAULT);
160 		}
161 
162 		eth_zero_addr(vif->mac_addr);
163 		vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
164 		vif->wdev.wiphy = wiphy;
165 		vif->wdev.iftype = type;
166 		vif->sta_state = QTNF_STA_DISCONNECTED;
167 		break;
168 	default:
169 		pr_err("MAC%u: unsupported IF type %d\n", mac->macid, type);
170 		return ERR_PTR(-ENOTSUPP);
171 	}
172 
173 	if (params)
174 		mac_addr = params->macaddr;
175 
176 	if (qtnf_cmd_send_add_intf(vif, type, mac_addr)) {
177 		pr_err("VIF%u.%u: failed to add VIF\n", mac->macid, vif->vifid);
178 		goto err_cmd;
179 	}
180 
181 	if (!is_valid_ether_addr(vif->mac_addr)) {
182 		pr_err("VIF%u.%u: FW reported bad MAC: %pM\n",
183 		       mac->macid, vif->vifid, vif->mac_addr);
184 		goto err_mac;
185 	}
186 
187 	if (qtnf_core_net_attach(mac, vif, name, name_assign_t, type)) {
188 		pr_err("VIF%u.%u: failed to attach netdev\n", mac->macid,
189 		       vif->vifid);
190 		goto err_net;
191 	}
192 
193 	vif->wdev.netdev = vif->netdev;
194 	return &vif->wdev;
195 
196 err_net:
197 	vif->netdev = NULL;
198 err_mac:
199 	qtnf_cmd_send_del_intf(vif);
200 err_cmd:
201 	vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
202 
203 	return ERR_PTR(-EFAULT);
204 }
205 
206 static int qtnf_mgmt_set_appie(struct qtnf_vif *vif,
207 			       const struct cfg80211_beacon_data *info)
208 {
209 	int ret = 0;
210 
211 	if (!info->beacon_ies || !info->beacon_ies_len) {
212 		ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_MGMT_FRAME_BEACON,
213 						   NULL, 0);
214 	} else {
215 		ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_MGMT_FRAME_BEACON,
216 						   info->beacon_ies,
217 						   info->beacon_ies_len);
218 	}
219 
220 	if (ret)
221 		goto out;
222 
223 	if (!info->proberesp_ies || !info->proberesp_ies_len) {
224 		ret = qtnf_cmd_send_mgmt_set_appie(vif,
225 						   QLINK_MGMT_FRAME_PROBE_RESP,
226 						   NULL, 0);
227 	} else {
228 		ret = qtnf_cmd_send_mgmt_set_appie(vif,
229 						   QLINK_MGMT_FRAME_PROBE_RESP,
230 						   info->proberesp_ies,
231 						   info->proberesp_ies_len);
232 	}
233 
234 	if (ret)
235 		goto out;
236 
237 	if (!info->assocresp_ies || !info->assocresp_ies_len) {
238 		ret = qtnf_cmd_send_mgmt_set_appie(vif,
239 						   QLINK_MGMT_FRAME_ASSOC_RESP,
240 						   NULL, 0);
241 	} else {
242 		ret = qtnf_cmd_send_mgmt_set_appie(vif,
243 						   QLINK_MGMT_FRAME_ASSOC_RESP,
244 						   info->assocresp_ies,
245 						   info->assocresp_ies_len);
246 	}
247 
248 out:
249 	return ret;
250 }
251 
252 static int qtnf_change_beacon(struct wiphy *wiphy, struct net_device *dev,
253 			      struct cfg80211_beacon_data *info)
254 {
255 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
256 
257 	if (!(vif->bss_status & QTNF_STATE_AP_START)) {
258 		pr_err("VIF%u.%u: not started\n", vif->mac->macid, vif->vifid);
259 		return -EFAULT;
260 	}
261 
262 	return qtnf_mgmt_set_appie(vif, info);
263 }
264 
265 static int qtnf_start_ap(struct wiphy *wiphy, struct net_device *dev,
266 			 struct cfg80211_ap_settings *settings)
267 {
268 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
269 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
270 	struct qtnf_bss_config *bss_cfg;
271 	int ret;
272 
273 	if (!cfg80211_chandef_identical(&mac->chandef, &settings->chandef)) {
274 		memcpy(&mac->chandef, &settings->chandef, sizeof(mac->chandef));
275 		if (vif->vifid != 0)
276 			pr_warn("%s: unexpected chan %u (%u MHz)\n", dev->name,
277 				settings->chandef.chan->hw_value,
278 				settings->chandef.chan->center_freq);
279 	}
280 
281 	bss_cfg = &vif->bss_cfg;
282 	memset(bss_cfg, 0, sizeof(*bss_cfg));
283 
284 	bss_cfg->bcn_period = settings->beacon_interval;
285 	bss_cfg->dtim = settings->dtim_period;
286 	bss_cfg->auth_type = settings->auth_type;
287 	bss_cfg->privacy = settings->privacy;
288 
289 	bss_cfg->ssid_len = settings->ssid_len;
290 	memcpy(&bss_cfg->ssid, settings->ssid, bss_cfg->ssid_len);
291 
292 	memcpy(&bss_cfg->crypto, &settings->crypto,
293 	       sizeof(struct cfg80211_crypto_settings));
294 
295 	ret = qtnf_cmd_send_config_ap(vif);
296 	if (ret) {
297 		pr_err("VIF%u.%u: failed to push config to FW\n",
298 		       vif->mac->macid, vif->vifid);
299 		goto out;
300 	}
301 
302 	if (!(vif->bss_status & QTNF_STATE_AP_CONFIG)) {
303 		pr_err("VIF%u.%u: AP config failed in FW\n", vif->mac->macid,
304 		       vif->vifid);
305 		ret = -EFAULT;
306 		goto out;
307 	}
308 
309 	ret = qtnf_mgmt_set_appie(vif, &settings->beacon);
310 	if (ret) {
311 		pr_err("VIF%u.%u: failed to add IEs to beacon\n",
312 		       vif->mac->macid, vif->vifid);
313 		goto out;
314 	}
315 
316 	ret = qtnf_cmd_send_start_ap(vif);
317 	if (ret) {
318 		pr_err("VIF%u.%u: failed to start AP\n", vif->mac->macid,
319 		       vif->vifid);
320 		goto out;
321 	}
322 
323 	if (!(vif->bss_status & QTNF_STATE_AP_START)) {
324 		pr_err("VIF%u.%u: FW failed to start AP operation\n",
325 		       vif->mac->macid, vif->vifid);
326 		ret = -EFAULT;
327 	}
328 
329 out:
330 	return ret;
331 }
332 
333 static int qtnf_stop_ap(struct wiphy *wiphy, struct net_device *dev)
334 {
335 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
336 	int ret;
337 
338 	ret = qtnf_cmd_send_stop_ap(vif);
339 	if (ret) {
340 		pr_err("VIF%u.%u: failed to stop AP operation in FW\n",
341 		       vif->mac->macid, vif->vifid);
342 		vif->bss_status &= ~QTNF_STATE_AP_START;
343 		vif->bss_status &= ~QTNF_STATE_AP_CONFIG;
344 
345 		netif_carrier_off(vif->netdev);
346 	}
347 
348 	return ret;
349 }
350 
351 static int qtnf_set_wiphy_params(struct wiphy *wiphy, u32 changed)
352 {
353 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
354 	struct qtnf_vif *vif;
355 	int ret;
356 
357 	vif = qtnf_mac_get_base_vif(mac);
358 	if (!vif) {
359 		pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
360 		return -EFAULT;
361 	}
362 
363 	if (changed & (WIPHY_PARAM_RETRY_LONG | WIPHY_PARAM_RETRY_SHORT)) {
364 		pr_err("MAC%u: can't modify retry params\n", mac->macid);
365 		return -EOPNOTSUPP;
366 	}
367 
368 	ret = qtnf_cmd_send_update_phy_params(mac, changed);
369 	if (ret)
370 		pr_err("MAC%u: failed to update PHY params\n", mac->macid);
371 
372 	return ret;
373 }
374 
375 static void
376 qtnf_mgmt_frame_register(struct wiphy *wiphy, struct wireless_dev *wdev,
377 			 u16 frame_type, bool reg)
378 {
379 	struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
380 	u16 mgmt_type;
381 	u16 new_mask;
382 	u16 qlink_frame_type = 0;
383 
384 	mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
385 
386 	if (reg)
387 		new_mask = vif->mgmt_frames_bitmask | BIT(mgmt_type);
388 	else
389 		new_mask = vif->mgmt_frames_bitmask & ~BIT(mgmt_type);
390 
391 	if (new_mask == vif->mgmt_frames_bitmask)
392 		return;
393 
394 	switch (frame_type & IEEE80211_FCTL_STYPE) {
395 	case IEEE80211_STYPE_PROBE_REQ:
396 		qlink_frame_type = QLINK_MGMT_FRAME_PROBE_REQ;
397 		break;
398 	case IEEE80211_STYPE_ACTION:
399 		qlink_frame_type = QLINK_MGMT_FRAME_ACTION;
400 		break;
401 	default:
402 		pr_warn("VIF%u.%u: unsupported frame type: %X\n",
403 			vif->mac->macid, vif->vifid,
404 			(frame_type & IEEE80211_FCTL_STYPE) >> 4);
405 		return;
406 	}
407 
408 	if (qtnf_cmd_send_register_mgmt(vif, qlink_frame_type, reg)) {
409 		pr_warn("VIF%u.%u: failed to %sregister mgmt frame type 0x%x\n",
410 			vif->mac->macid, vif->vifid, reg ? "" : "un",
411 			frame_type);
412 		return;
413 	}
414 
415 	vif->mgmt_frames_bitmask = new_mask;
416 	pr_debug("VIF%u.%u: %sregistered mgmt frame type 0x%x\n",
417 		 vif->mac->macid, vif->vifid, reg ? "" : "un", frame_type);
418 }
419 
420 static int
421 qtnf_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
422 	     struct cfg80211_mgmt_tx_params *params, u64 *cookie)
423 {
424 	struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
425 	const struct ieee80211_mgmt *mgmt_frame = (void *)params->buf;
426 	u32 short_cookie = prandom_u32();
427 	u16 flags = 0;
428 
429 	*cookie = short_cookie;
430 
431 	if (params->offchan)
432 		flags |= QLINK_MGMT_FRAME_TX_FLAG_OFFCHAN;
433 
434 	if (params->no_cck)
435 		flags |= QLINK_MGMT_FRAME_TX_FLAG_NO_CCK;
436 
437 	if (params->dont_wait_for_ack)
438 		flags |= QLINK_MGMT_FRAME_TX_FLAG_ACK_NOWAIT;
439 
440 	pr_debug("%s freq:%u; FC:%.4X; DA:%pM; len:%zu; C:%.8X; FL:%.4X\n",
441 		 wdev->netdev->name, params->chan->center_freq,
442 		 le16_to_cpu(mgmt_frame->frame_control), mgmt_frame->da,
443 		 params->len, short_cookie, flags);
444 
445 	return qtnf_cmd_send_mgmt_frame(vif, short_cookie, flags,
446 					params->chan->center_freq,
447 					params->buf, params->len);
448 }
449 
450 static int
451 qtnf_get_station(struct wiphy *wiphy, struct net_device *dev,
452 		 const u8 *mac, struct station_info *sinfo)
453 {
454 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
455 
456 	return qtnf_cmd_get_sta_info(vif, mac, sinfo);
457 }
458 
459 static int
460 qtnf_dump_station(struct wiphy *wiphy, struct net_device *dev,
461 		  int idx, u8 *mac, struct station_info *sinfo)
462 {
463 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
464 	const struct qtnf_sta_node *sta_node;
465 	int ret;
466 
467 	sta_node = qtnf_sta_list_lookup_index(&vif->sta_list, idx);
468 
469 	if (unlikely(!sta_node))
470 		return -ENOENT;
471 
472 	ether_addr_copy(mac, sta_node->mac_addr);
473 
474 	ret = qtnf_cmd_get_sta_info(vif, sta_node->mac_addr, sinfo);
475 
476 	if (unlikely(ret == -ENOENT)) {
477 		qtnf_sta_list_del(&vif->sta_list, mac);
478 		cfg80211_del_sta(vif->netdev, mac, GFP_KERNEL);
479 		sinfo->filled = 0;
480 	}
481 
482 	return ret;
483 }
484 
485 static int qtnf_add_key(struct wiphy *wiphy, struct net_device *dev,
486 			u8 key_index, bool pairwise, const u8 *mac_addr,
487 			struct key_params *params)
488 {
489 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
490 	int ret;
491 
492 	ret = qtnf_cmd_send_add_key(vif, key_index, pairwise, mac_addr, params);
493 	if (ret)
494 		pr_err("VIF%u.%u: failed to add key: cipher=%x idx=%u pw=%u\n",
495 		       vif->mac->macid, vif->vifid, params->cipher, key_index,
496 		       pairwise);
497 
498 	return ret;
499 }
500 
501 static int qtnf_del_key(struct wiphy *wiphy, struct net_device *dev,
502 			u8 key_index, bool pairwise, const u8 *mac_addr)
503 {
504 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
505 	int ret;
506 
507 	ret = qtnf_cmd_send_del_key(vif, key_index, pairwise, mac_addr);
508 	if (ret)
509 		pr_err("VIF%u.%u: failed to delete key: idx=%u pw=%u\n",
510 		       vif->mac->macid, vif->vifid, key_index, pairwise);
511 
512 	return ret;
513 }
514 
515 static int qtnf_set_default_key(struct wiphy *wiphy, struct net_device *dev,
516 				u8 key_index, bool unicast, bool multicast)
517 {
518 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
519 	int ret;
520 
521 	ret = qtnf_cmd_send_set_default_key(vif, key_index, unicast, multicast);
522 	if (ret)
523 		pr_err("VIF%u.%u: failed to set dflt key: idx=%u uc=%u mc=%u\n",
524 		       vif->mac->macid, vif->vifid, key_index, unicast,
525 		       multicast);
526 
527 	return ret;
528 }
529 
530 static int
531 qtnf_set_default_mgmt_key(struct wiphy *wiphy, struct net_device *dev,
532 			  u8 key_index)
533 {
534 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
535 	int ret;
536 
537 	ret = qtnf_cmd_send_set_default_mgmt_key(vif, key_index);
538 	if (ret)
539 		pr_err("VIF%u.%u: failed to set default MGMT key: idx=%u\n",
540 		       vif->mac->macid, vif->vifid, key_index);
541 
542 	return ret;
543 }
544 
545 static int
546 qtnf_change_station(struct wiphy *wiphy, struct net_device *dev,
547 		    const u8 *mac, struct station_parameters *params)
548 {
549 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
550 	int ret;
551 
552 	ret = qtnf_cmd_send_change_sta(vif, mac, params);
553 	if (ret)
554 		pr_err("VIF%u.%u: failed to change STA %pM\n",
555 		       vif->mac->macid, vif->vifid, mac);
556 
557 	return ret;
558 }
559 
560 static int
561 qtnf_del_station(struct wiphy *wiphy, struct net_device *dev,
562 		 struct station_del_parameters *params)
563 {
564 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
565 	int ret;
566 
567 	if (params->mac &&
568 	    (vif->wdev.iftype == NL80211_IFTYPE_AP) &&
569 	    !is_broadcast_ether_addr(params->mac) &&
570 	    !qtnf_sta_list_lookup(&vif->sta_list, params->mac))
571 		return 0;
572 
573 	qtnf_scan_done(vif->mac, true);
574 
575 	ret = qtnf_cmd_send_del_sta(vif, params);
576 	if (ret)
577 		pr_err("VIF%u.%u: failed to delete STA %pM\n",
578 		       vif->mac->macid, vif->vifid, params->mac);
579 	return ret;
580 }
581 
582 static void qtnf_scan_timeout(unsigned long data)
583 {
584 	struct qtnf_wmac *mac = (struct qtnf_wmac *)data;
585 
586 	pr_warn("mac%d scan timed out\n", mac->macid);
587 	qtnf_scan_done(mac, true);
588 }
589 
590 static int
591 qtnf_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
592 {
593 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
594 
595 	mac->scan_req = request;
596 
597 	if (qtnf_cmd_send_scan(mac)) {
598 		pr_err("MAC%u: failed to start scan\n", mac->macid);
599 		mac->scan_req = NULL;
600 		return -EFAULT;
601 	}
602 
603 	mac->scan_timeout.data = (unsigned long)mac;
604 	mac->scan_timeout.function = qtnf_scan_timeout;
605 	mod_timer(&mac->scan_timeout,
606 		  jiffies + QTNF_SCAN_TIMEOUT_SEC * HZ);
607 
608 	return 0;
609 }
610 
611 static int
612 qtnf_connect(struct wiphy *wiphy, struct net_device *dev,
613 	     struct cfg80211_connect_params *sme)
614 {
615 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
616 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
617 	struct cfg80211_chan_def chandef;
618 	struct qtnf_bss_config *bss_cfg;
619 	int ret;
620 
621 	if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
622 		return -EOPNOTSUPP;
623 
624 	if (vif->sta_state != QTNF_STA_DISCONNECTED)
625 		return -EBUSY;
626 
627 	bss_cfg = &vif->bss_cfg;
628 	memset(bss_cfg, 0, sizeof(*bss_cfg));
629 
630 	if (sme->channel) {
631 		/* FIXME: need to set proper nl80211_channel_type value */
632 		cfg80211_chandef_create(&chandef, sme->channel,
633 					NL80211_CHAN_HT20);
634 		/* fall-back to minimal safe chandef description */
635 		if (!cfg80211_chandef_valid(&chandef))
636 			cfg80211_chandef_create(&chandef, sme->channel,
637 						NL80211_CHAN_HT20);
638 
639 		memcpy(&mac->chandef, &chandef, sizeof(mac->chandef));
640 	}
641 
642 	bss_cfg->ssid_len = sme->ssid_len;
643 	memcpy(&bss_cfg->ssid, sme->ssid, bss_cfg->ssid_len);
644 	bss_cfg->auth_type = sme->auth_type;
645 	bss_cfg->privacy = sme->privacy;
646 	bss_cfg->mfp = sme->mfp;
647 
648 	if ((sme->bg_scan_period > 0) &&
649 	    (sme->bg_scan_period <= QTNF_MAX_BG_SCAN_PERIOD))
650 		bss_cfg->bg_scan_period = sme->bg_scan_period;
651 	else if (sme->bg_scan_period == -1)
652 		bss_cfg->bg_scan_period = QTNF_DEFAULT_BG_SCAN_PERIOD;
653 	else
654 		bss_cfg->bg_scan_period = 0; /* disabled */
655 
656 	bss_cfg->connect_flags = 0;
657 
658 	if (sme->flags & ASSOC_REQ_DISABLE_HT)
659 		bss_cfg->connect_flags |= QLINK_STA_CONNECT_DISABLE_HT;
660 	if (sme->flags & ASSOC_REQ_DISABLE_VHT)
661 		bss_cfg->connect_flags |= QLINK_STA_CONNECT_DISABLE_VHT;
662 	if (sme->flags & ASSOC_REQ_USE_RRM)
663 		bss_cfg->connect_flags |= QLINK_STA_CONNECT_USE_RRM;
664 
665 	memcpy(&bss_cfg->crypto, &sme->crypto, sizeof(bss_cfg->crypto));
666 	if (sme->bssid)
667 		ether_addr_copy(bss_cfg->bssid, sme->bssid);
668 	else
669 		eth_zero_addr(bss_cfg->bssid);
670 
671 	ret = qtnf_cmd_send_connect(vif, sme);
672 	if (ret) {
673 		pr_err("VIF%u.%u: failed to connect\n", vif->mac->macid,
674 		       vif->vifid);
675 		return ret;
676 	}
677 
678 	vif->sta_state = QTNF_STA_CONNECTING;
679 	return 0;
680 }
681 
682 static int
683 qtnf_disconnect(struct wiphy *wiphy, struct net_device *dev,
684 		u16 reason_code)
685 {
686 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
687 	struct qtnf_vif *vif;
688 	int ret;
689 
690 	vif = qtnf_mac_get_base_vif(mac);
691 	if (!vif) {
692 		pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
693 		return -EFAULT;
694 	}
695 
696 	if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
697 		return -EOPNOTSUPP;
698 
699 	if (vif->sta_state == QTNF_STA_DISCONNECTED)
700 		return 0;
701 
702 	ret = qtnf_cmd_send_disconnect(vif, reason_code);
703 	if (ret) {
704 		pr_err("VIF%u.%u: failed to disconnect\n", mac->macid,
705 		       vif->vifid);
706 		return ret;
707 	}
708 
709 	vif->sta_state = QTNF_STA_DISCONNECTED;
710 	return 0;
711 }
712 
713 static int
714 qtnf_dump_survey(struct wiphy *wiphy, struct net_device *dev,
715 		 int idx, struct survey_info *survey)
716 {
717 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
718 	struct ieee80211_supported_band *sband;
719 	struct cfg80211_chan_def *chandef;
720 	struct ieee80211_channel *chan;
721 	struct qtnf_chan_stats stats;
722 	struct qtnf_vif *vif;
723 	int ret;
724 
725 	vif = qtnf_netdev_get_priv(dev);
726 	chandef = &mac->chandef;
727 
728 	sband = wiphy->bands[NL80211_BAND_2GHZ];
729 	if (sband && idx >= sband->n_channels) {
730 		idx -= sband->n_channels;
731 		sband = NULL;
732 	}
733 
734 	if (!sband)
735 		sband = wiphy->bands[NL80211_BAND_5GHZ];
736 
737 	if (!sband || idx >= sband->n_channels)
738 		return -ENOENT;
739 
740 	chan = &sband->channels[idx];
741 	memset(&stats, 0, sizeof(stats));
742 
743 	survey->channel = chan;
744 	survey->filled = 0x0;
745 
746 	if (chandef->chan) {
747 		if (chan->hw_value == chandef->chan->hw_value)
748 			survey->filled = SURVEY_INFO_IN_USE;
749 	}
750 
751 	ret = qtnf_cmd_get_chan_stats(mac, chan->hw_value, &stats);
752 	switch (ret) {
753 	case 0:
754 		if (unlikely(stats.chan_num != chan->hw_value)) {
755 			pr_err("received stats for channel %d instead of %d\n",
756 			       stats.chan_num, chan->hw_value);
757 			ret = -EINVAL;
758 			break;
759 		}
760 
761 		survey->filled |= SURVEY_INFO_TIME |
762 				 SURVEY_INFO_TIME_SCAN |
763 				 SURVEY_INFO_TIME_BUSY |
764 				 SURVEY_INFO_TIME_RX |
765 				 SURVEY_INFO_TIME_TX |
766 				 SURVEY_INFO_NOISE_DBM;
767 
768 		survey->time_scan = stats.cca_try;
769 		survey->time = stats.cca_try;
770 		survey->time_tx = stats.cca_tx;
771 		survey->time_rx = stats.cca_rx;
772 		survey->time_busy = stats.cca_busy;
773 		survey->noise = stats.chan_noise;
774 		break;
775 	case -ENOENT:
776 		pr_debug("no stats for channel %u\n", chan->hw_value);
777 		ret = 0;
778 		break;
779 	default:
780 		pr_debug("failed to get chan(%d) stats from card\n",
781 			 chan->hw_value);
782 		ret = -EINVAL;
783 		break;
784 	}
785 
786 	return ret;
787 }
788 
789 static int
790 qtnf_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
791 		 struct cfg80211_chan_def *chandef)
792 {
793 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
794 	struct net_device *ndev = wdev->netdev;
795 	struct qtnf_vif *vif;
796 
797 	if (!ndev)
798 		return -ENODEV;
799 
800 	vif = qtnf_netdev_get_priv(wdev->netdev);
801 
802 	switch (vif->wdev.iftype) {
803 	case NL80211_IFTYPE_STATION:
804 		if (vif->sta_state == QTNF_STA_DISCONNECTED) {
805 			pr_warn("%s: STA disconnected\n", ndev->name);
806 			return -ENODATA;
807 		}
808 		break;
809 	case NL80211_IFTYPE_AP:
810 		if (!(vif->bss_status & QTNF_STATE_AP_START)) {
811 			pr_warn("%s: AP not started\n", ndev->name);
812 			return -ENODATA;
813 		}
814 		break;
815 	default:
816 		pr_err("unsupported vif type (%d)\n", vif->wdev.iftype);
817 		return -ENODATA;
818 	}
819 
820 	if (!cfg80211_chandef_valid(&mac->chandef)) {
821 		pr_err("invalid channel settings on %s\n", ndev->name);
822 		return -ENODATA;
823 	}
824 
825 	memcpy(chandef, &mac->chandef, sizeof(*chandef));
826 	return 0;
827 }
828 
829 static int qtnf_channel_switch(struct wiphy *wiphy, struct net_device *dev,
830 			       struct cfg80211_csa_settings *params)
831 {
832 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
833 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
834 	int ret;
835 
836 	pr_debug("%s: chan(%u) count(%u) radar(%u) block_tx(%u)\n", dev->name,
837 		 params->chandef.chan->hw_value, params->count,
838 		 params->radar_required, params->block_tx);
839 
840 	switch (vif->wdev.iftype) {
841 	case NL80211_IFTYPE_AP:
842 		if (!(vif->bss_status & QTNF_STATE_AP_START)) {
843 			pr_warn("AP not started on %s\n", dev->name);
844 			return -ENOTCONN;
845 		}
846 		break;
847 	default:
848 		pr_err("unsupported vif type (%d) on %s\n",
849 		       vif->wdev.iftype, dev->name);
850 		return -EOPNOTSUPP;
851 	}
852 
853 	if (vif->vifid != 0) {
854 		if (!(mac->status & QTNF_MAC_CSA_ACTIVE))
855 			return -EOPNOTSUPP;
856 
857 		if (!cfg80211_chandef_identical(&params->chandef,
858 						&mac->csa_chandef))
859 			return -EINVAL;
860 
861 		return 0;
862 	}
863 
864 	if (!cfg80211_chandef_valid(&params->chandef)) {
865 		pr_err("%s: invalid channel\n", dev->name);
866 		return -EINVAL;
867 	}
868 
869 	if (cfg80211_chandef_identical(&params->chandef, &mac->chandef)) {
870 		pr_err("%s: switch request to the same channel\n", dev->name);
871 		return -EALREADY;
872 	}
873 
874 	ret = qtnf_cmd_send_chan_switch(mac, params);
875 	if (ret)
876 		pr_warn("%s: failed to switch to channel (%u)\n",
877 			dev->name, params->chandef.chan->hw_value);
878 
879 	return ret;
880 }
881 
882 static struct cfg80211_ops qtn_cfg80211_ops = {
883 	.add_virtual_intf	= qtnf_add_virtual_intf,
884 	.change_virtual_intf	= qtnf_change_virtual_intf,
885 	.del_virtual_intf	= qtnf_del_virtual_intf,
886 	.start_ap		= qtnf_start_ap,
887 	.change_beacon		= qtnf_change_beacon,
888 	.stop_ap		= qtnf_stop_ap,
889 	.set_wiphy_params	= qtnf_set_wiphy_params,
890 	.mgmt_frame_register	= qtnf_mgmt_frame_register,
891 	.mgmt_tx		= qtnf_mgmt_tx,
892 	.change_station		= qtnf_change_station,
893 	.del_station		= qtnf_del_station,
894 	.get_station		= qtnf_get_station,
895 	.dump_station		= qtnf_dump_station,
896 	.add_key		= qtnf_add_key,
897 	.del_key		= qtnf_del_key,
898 	.set_default_key	= qtnf_set_default_key,
899 	.set_default_mgmt_key	= qtnf_set_default_mgmt_key,
900 	.scan			= qtnf_scan,
901 	.connect		= qtnf_connect,
902 	.disconnect		= qtnf_disconnect,
903 	.dump_survey		= qtnf_dump_survey,
904 	.get_channel		= qtnf_get_channel,
905 	.channel_switch		= qtnf_channel_switch
906 };
907 
908 static void qtnf_cfg80211_reg_notifier(struct wiphy *wiphy_in,
909 				       struct regulatory_request *req)
910 {
911 	struct qtnf_wmac *mac = wiphy_priv(wiphy_in);
912 	struct qtnf_bus *bus = mac->bus;
913 	struct wiphy *wiphy;
914 	unsigned int mac_idx;
915 	enum nl80211_band band;
916 	int ret;
917 
918 	pr_debug("MAC%u: initiator=%d alpha=%c%c\n", mac->macid, req->initiator,
919 		 req->alpha2[0], req->alpha2[1]);
920 
921 	ret = qtnf_cmd_reg_notify(bus, req);
922 	if (ret) {
923 		if (ret != -EOPNOTSUPP && ret != -EALREADY)
924 			pr_err("failed to update reg domain to %c%c\n",
925 			       req->alpha2[0], req->alpha2[1]);
926 		return;
927 	}
928 
929 	for (mac_idx = 0; mac_idx < QTNF_MAX_MAC; ++mac_idx) {
930 		if (!(bus->hw_info.mac_bitmap & (1 << mac_idx)))
931 			continue;
932 
933 		mac = bus->mac[mac_idx];
934 		wiphy = priv_to_wiphy(mac);
935 
936 		for (band = 0; band < NUM_NL80211_BANDS; ++band) {
937 			if (!wiphy->bands[band])
938 				continue;
939 
940 			ret = qtnf_cmd_get_mac_chan_info(mac,
941 							 wiphy->bands[band]);
942 			if (ret)
943 				pr_err("failed to get chan info for mac %u band %u\n",
944 				       mac_idx, band);
945 		}
946 	}
947 }
948 
949 void qtnf_band_setup_htvht_caps(struct qtnf_mac_info *macinfo,
950 				struct ieee80211_supported_band *band)
951 {
952 	struct ieee80211_sta_ht_cap *ht_cap;
953 	struct ieee80211_sta_vht_cap *vht_cap;
954 
955 	ht_cap = &band->ht_cap;
956 	ht_cap->ht_supported = true;
957 	memcpy(&ht_cap->cap, &macinfo->ht_cap.cap_info,
958 	       sizeof(u16));
959 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
960 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
961 	memcpy(&ht_cap->mcs, &macinfo->ht_cap.mcs,
962 	       sizeof(ht_cap->mcs));
963 
964 	if (macinfo->phymode_cap & QLINK_PHYMODE_AC) {
965 		vht_cap = &band->vht_cap;
966 		vht_cap->vht_supported = true;
967 		memcpy(&vht_cap->cap,
968 		       &macinfo->vht_cap.vht_cap_info, sizeof(u32));
969 		/* Update MCS support for VHT */
970 		memcpy(&vht_cap->vht_mcs,
971 		       &macinfo->vht_cap.supp_mcs,
972 		       sizeof(struct ieee80211_vht_mcs_info));
973 	}
974 }
975 
976 struct wiphy *qtnf_wiphy_allocate(struct qtnf_bus *bus)
977 {
978 	struct wiphy *wiphy;
979 
980 	wiphy = wiphy_new(&qtn_cfg80211_ops, sizeof(struct qtnf_wmac));
981 	if (!wiphy)
982 		return NULL;
983 
984 	set_wiphy_dev(wiphy, bus->dev);
985 
986 	return wiphy;
987 }
988 
989 static int qtnf_wiphy_setup_if_comb(struct wiphy *wiphy,
990 				    struct ieee80211_iface_combination *if_comb,
991 				    const struct qtnf_mac_info *mac_info)
992 {
993 	size_t max_interfaces = 0;
994 	u16 interface_modes = 0;
995 	size_t i;
996 
997 	if (unlikely(!mac_info->limits || !mac_info->n_limits))
998 		return -ENOENT;
999 
1000 	if_comb->limits = mac_info->limits;
1001 	if_comb->n_limits = mac_info->n_limits;
1002 
1003 	for (i = 0; i < mac_info->n_limits; i++) {
1004 		max_interfaces += mac_info->limits[i].max;
1005 		interface_modes |= mac_info->limits[i].types;
1006 	}
1007 
1008 	if_comb->num_different_channels = 1;
1009 	if_comb->beacon_int_infra_match = true;
1010 	if_comb->max_interfaces = max_interfaces;
1011 	if_comb->radar_detect_widths = mac_info->radar_detect_widths;
1012 	wiphy->interface_modes = interface_modes;
1013 
1014 	return 0;
1015 }
1016 
1017 int qtnf_wiphy_register(struct qtnf_hw_info *hw_info, struct qtnf_wmac *mac)
1018 {
1019 	struct wiphy *wiphy = priv_to_wiphy(mac);
1020 	struct ieee80211_iface_combination *iface_comb = NULL;
1021 	int ret;
1022 
1023 	if (!wiphy) {
1024 		pr_err("invalid wiphy pointer\n");
1025 		return -EFAULT;
1026 	}
1027 
1028 	iface_comb = kzalloc(sizeof(*iface_comb), GFP_KERNEL);
1029 	if (!iface_comb)
1030 		return -ENOMEM;
1031 
1032 	ret = qtnf_wiphy_setup_if_comb(wiphy, iface_comb, &mac->macinfo);
1033 	if (ret)
1034 		goto out;
1035 
1036 	pr_info("MAC%u: phymode=%#x radar=%#x\n", mac->macid,
1037 		mac->macinfo.phymode_cap, mac->macinfo.radar_detect_widths);
1038 
1039 	wiphy->frag_threshold = mac->macinfo.frag_thr;
1040 	wiphy->rts_threshold = mac->macinfo.rts_thr;
1041 	wiphy->retry_short = mac->macinfo.sretry_limit;
1042 	wiphy->retry_long = mac->macinfo.lretry_limit;
1043 	wiphy->coverage_class = mac->macinfo.coverage_class;
1044 
1045 	wiphy->max_scan_ssids = QTNF_MAX_SSID_LIST_LENGTH;
1046 	wiphy->max_scan_ie_len = QTNF_MAX_VSIE_LEN;
1047 	wiphy->mgmt_stypes = qtnf_mgmt_stypes;
1048 	wiphy->max_remain_on_channel_duration = 5000;
1049 
1050 	wiphy->iface_combinations = iface_comb;
1051 	wiphy->n_iface_combinations = 1;
1052 	wiphy->max_num_csa_counters = 2;
1053 
1054 	/* Initialize cipher suits */
1055 	wiphy->cipher_suites = qtnf_cipher_suites;
1056 	wiphy->n_cipher_suites = ARRAY_SIZE(qtnf_cipher_suites);
1057 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1058 	wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
1059 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
1060 			WIPHY_FLAG_AP_UAPSD |
1061 			WIPHY_FLAG_HAS_CHANNEL_SWITCH;
1062 
1063 	wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
1064 				    NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2;
1065 
1066 	wiphy->available_antennas_tx = mac->macinfo.num_tx_chain;
1067 	wiphy->available_antennas_rx = mac->macinfo.num_rx_chain;
1068 
1069 	wiphy->max_ap_assoc_sta = mac->macinfo.max_ap_assoc_sta;
1070 
1071 	ether_addr_copy(wiphy->perm_addr, mac->macaddr);
1072 
1073 	if (hw_info->hw_capab & QLINK_HW_SUPPORTS_REG_UPDATE) {
1074 		wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
1075 			REGULATORY_CUSTOM_REG;
1076 		wiphy->reg_notifier = qtnf_cfg80211_reg_notifier;
1077 		wiphy_apply_custom_regulatory(wiphy, hw_info->rd);
1078 	} else {
1079 		wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
1080 	}
1081 
1082 	ret = wiphy_register(wiphy);
1083 	if (ret < 0)
1084 		goto out;
1085 
1086 	if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1087 		ret = regulatory_set_wiphy_regd(wiphy, hw_info->rd);
1088 	else if (isalpha(hw_info->rd->alpha2[0]) &&
1089 		 isalpha(hw_info->rd->alpha2[1]))
1090 		ret = regulatory_hint(wiphy, hw_info->rd->alpha2);
1091 
1092 out:
1093 	if (ret) {
1094 		kfree(iface_comb);
1095 		return ret;
1096 	}
1097 
1098 	return 0;
1099 }
1100 
1101 void qtnf_netdev_updown(struct net_device *ndev, bool up)
1102 {
1103 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
1104 
1105 	if (qtnf_cmd_send_updown_intf(vif, up))
1106 		pr_err("failed to send up/down command to FW\n");
1107 }
1108 
1109 void qtnf_virtual_intf_cleanup(struct net_device *ndev)
1110 {
1111 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
1112 	struct qtnf_wmac *mac = wiphy_priv(vif->wdev.wiphy);
1113 
1114 	if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
1115 		switch (vif->sta_state) {
1116 		case QTNF_STA_DISCONNECTED:
1117 			break;
1118 		case QTNF_STA_CONNECTING:
1119 			cfg80211_connect_result(vif->netdev,
1120 						vif->bss_cfg.bssid, NULL, 0,
1121 						NULL, 0,
1122 						WLAN_STATUS_UNSPECIFIED_FAILURE,
1123 						GFP_KERNEL);
1124 			qtnf_disconnect(vif->wdev.wiphy, ndev,
1125 					WLAN_REASON_DEAUTH_LEAVING);
1126 			break;
1127 		case QTNF_STA_CONNECTED:
1128 			cfg80211_disconnected(vif->netdev,
1129 					      WLAN_REASON_DEAUTH_LEAVING,
1130 					      NULL, 0, 1, GFP_KERNEL);
1131 			qtnf_disconnect(vif->wdev.wiphy, ndev,
1132 					WLAN_REASON_DEAUTH_LEAVING);
1133 			break;
1134 		}
1135 
1136 		vif->sta_state = QTNF_STA_DISCONNECTED;
1137 		qtnf_scan_done(mac, true);
1138 	}
1139 }
1140 
1141 void qtnf_cfg80211_vif_reset(struct qtnf_vif *vif)
1142 {
1143 	if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
1144 		switch (vif->sta_state) {
1145 		case QTNF_STA_CONNECTING:
1146 			cfg80211_connect_result(vif->netdev,
1147 						vif->bss_cfg.bssid, NULL, 0,
1148 						NULL, 0,
1149 						WLAN_STATUS_UNSPECIFIED_FAILURE,
1150 						GFP_KERNEL);
1151 			break;
1152 		case QTNF_STA_CONNECTED:
1153 			cfg80211_disconnected(vif->netdev,
1154 					      WLAN_REASON_DEAUTH_LEAVING,
1155 					      NULL, 0, 1, GFP_KERNEL);
1156 			break;
1157 		case QTNF_STA_DISCONNECTED:
1158 			break;
1159 		}
1160 	}
1161 
1162 	cfg80211_shutdown_all_interfaces(vif->wdev.wiphy);
1163 	vif->sta_state = QTNF_STA_DISCONNECTED;
1164 }
1165 
1166 void qtnf_band_init_rates(struct ieee80211_supported_band *band)
1167 {
1168 	switch (band->band) {
1169 	case NL80211_BAND_2GHZ:
1170 		band->bitrates = qtnf_rates_2g;
1171 		band->n_bitrates = ARRAY_SIZE(qtnf_rates_2g);
1172 		break;
1173 	case NL80211_BAND_5GHZ:
1174 		band->bitrates = qtnf_rates_5g;
1175 		band->n_bitrates = ARRAY_SIZE(qtnf_rates_5g);
1176 		break;
1177 	default:
1178 		band->bitrates = NULL;
1179 		band->n_bitrates = 0;
1180 		break;
1181 	}
1182 }
1183