xref: /linux/drivers/net/wireless/quantenna/qtnfmac/cfg80211.c (revision 995231c820e3bd3633cb38bf4ea6f2541e1da331)
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 	qtnf_scan_done(vif->mac, true);
119 
120 	if (qtnf_cmd_send_del_intf(vif))
121 		pr_err("VIF%u.%u: failed to delete VIF\n", vif->mac->macid,
122 		       vif->vifid);
123 
124 	/* Stop data */
125 	netif_tx_stop_all_queues(netdev);
126 	if (netif_carrier_ok(netdev))
127 		netif_carrier_off(netdev);
128 
129 	if (netdev->reg_state == NETREG_REGISTERED)
130 		unregister_netdevice(netdev);
131 
132 	vif->netdev->ieee80211_ptr = NULL;
133 	vif->netdev = NULL;
134 	vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
135 	eth_zero_addr(vif->mac_addr);
136 	eth_zero_addr(vif->bssid);
137 
138 	return 0;
139 }
140 
141 static struct wireless_dev *qtnf_add_virtual_intf(struct wiphy *wiphy,
142 						  const char *name,
143 						  unsigned char name_assign_t,
144 						  enum nl80211_iftype type,
145 						  struct vif_params *params)
146 {
147 	struct qtnf_wmac *mac;
148 	struct qtnf_vif *vif;
149 	u8 *mac_addr = NULL;
150 
151 	mac = wiphy_priv(wiphy);
152 
153 	if (!mac)
154 		return ERR_PTR(-EFAULT);
155 
156 	switch (type) {
157 	case NL80211_IFTYPE_STATION:
158 	case NL80211_IFTYPE_AP:
159 		vif = qtnf_mac_get_free_vif(mac);
160 		if (!vif) {
161 			pr_err("MAC%u: no free VIF available\n", mac->macid);
162 			return ERR_PTR(-EFAULT);
163 		}
164 
165 		eth_zero_addr(vif->mac_addr);
166 		vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
167 		vif->wdev.wiphy = wiphy;
168 		vif->wdev.iftype = type;
169 		vif->sta_state = QTNF_STA_DISCONNECTED;
170 		break;
171 	default:
172 		pr_err("MAC%u: unsupported IF type %d\n", mac->macid, type);
173 		return ERR_PTR(-ENOTSUPP);
174 	}
175 
176 	if (params)
177 		mac_addr = params->macaddr;
178 
179 	if (qtnf_cmd_send_add_intf(vif, type, mac_addr)) {
180 		pr_err("VIF%u.%u: failed to add VIF\n", mac->macid, vif->vifid);
181 		goto err_cmd;
182 	}
183 
184 	if (!is_valid_ether_addr(vif->mac_addr)) {
185 		pr_err("VIF%u.%u: FW reported bad MAC: %pM\n",
186 		       mac->macid, vif->vifid, vif->mac_addr);
187 		goto err_mac;
188 	}
189 
190 	if (qtnf_core_net_attach(mac, vif, name, name_assign_t, type)) {
191 		pr_err("VIF%u.%u: failed to attach netdev\n", mac->macid,
192 		       vif->vifid);
193 		goto err_net;
194 	}
195 
196 	vif->wdev.netdev = vif->netdev;
197 	return &vif->wdev;
198 
199 err_net:
200 	vif->netdev = NULL;
201 err_mac:
202 	qtnf_cmd_send_del_intf(vif);
203 err_cmd:
204 	vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
205 	eth_zero_addr(vif->mac_addr);
206 	eth_zero_addr(vif->bssid);
207 
208 	return ERR_PTR(-EFAULT);
209 }
210 
211 static int qtnf_mgmt_set_appie(struct qtnf_vif *vif,
212 			       const struct cfg80211_beacon_data *info)
213 {
214 	int ret = 0;
215 
216 	if (!info->beacon_ies || !info->beacon_ies_len) {
217 		ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_MGMT_FRAME_BEACON,
218 						   NULL, 0);
219 	} else {
220 		ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_MGMT_FRAME_BEACON,
221 						   info->beacon_ies,
222 						   info->beacon_ies_len);
223 	}
224 
225 	if (ret)
226 		goto out;
227 
228 	if (!info->proberesp_ies || !info->proberesp_ies_len) {
229 		ret = qtnf_cmd_send_mgmt_set_appie(vif,
230 						   QLINK_MGMT_FRAME_PROBE_RESP,
231 						   NULL, 0);
232 	} else {
233 		ret = qtnf_cmd_send_mgmt_set_appie(vif,
234 						   QLINK_MGMT_FRAME_PROBE_RESP,
235 						   info->proberesp_ies,
236 						   info->proberesp_ies_len);
237 	}
238 
239 	if (ret)
240 		goto out;
241 
242 	if (!info->assocresp_ies || !info->assocresp_ies_len) {
243 		ret = qtnf_cmd_send_mgmt_set_appie(vif,
244 						   QLINK_MGMT_FRAME_ASSOC_RESP,
245 						   NULL, 0);
246 	} else {
247 		ret = qtnf_cmd_send_mgmt_set_appie(vif,
248 						   QLINK_MGMT_FRAME_ASSOC_RESP,
249 						   info->assocresp_ies,
250 						   info->assocresp_ies_len);
251 	}
252 
253 out:
254 	return ret;
255 }
256 
257 static int qtnf_change_beacon(struct wiphy *wiphy, struct net_device *dev,
258 			      struct cfg80211_beacon_data *info)
259 {
260 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
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 	int ret;
270 
271 	ret = qtnf_cmd_send_config_ap(vif, settings);
272 	if (ret) {
273 		pr_err("VIF%u.%u: failed to push config to FW\n",
274 		       vif->mac->macid, vif->vifid);
275 		goto out;
276 	}
277 
278 	ret = qtnf_mgmt_set_appie(vif, &settings->beacon);
279 	if (ret) {
280 		pr_err("VIF%u.%u: failed to add IEs to beacon\n",
281 		       vif->mac->macid, vif->vifid);
282 		goto out;
283 	}
284 
285 	ret = qtnf_cmd_send_start_ap(vif);
286 	if (ret)
287 		pr_err("VIF%u.%u: failed to start AP\n", vif->mac->macid,
288 		       vif->vifid);
289 
290 out:
291 	return ret;
292 }
293 
294 static int qtnf_stop_ap(struct wiphy *wiphy, struct net_device *dev)
295 {
296 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
297 	int ret;
298 
299 	qtnf_scan_done(vif->mac, true);
300 
301 	ret = qtnf_cmd_send_stop_ap(vif);
302 	if (ret) {
303 		pr_err("VIF%u.%u: failed to stop AP operation in FW\n",
304 		       vif->mac->macid, vif->vifid);
305 
306 		netif_carrier_off(vif->netdev);
307 	}
308 
309 	return ret;
310 }
311 
312 static int qtnf_set_wiphy_params(struct wiphy *wiphy, u32 changed)
313 {
314 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
315 	struct qtnf_vif *vif;
316 	int ret;
317 
318 	vif = qtnf_mac_get_base_vif(mac);
319 	if (!vif) {
320 		pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
321 		return -EFAULT;
322 	}
323 
324 	if (changed & (WIPHY_PARAM_RETRY_LONG | WIPHY_PARAM_RETRY_SHORT)) {
325 		pr_err("MAC%u: can't modify retry params\n", mac->macid);
326 		return -EOPNOTSUPP;
327 	}
328 
329 	ret = qtnf_cmd_send_update_phy_params(mac, changed);
330 	if (ret)
331 		pr_err("MAC%u: failed to update PHY params\n", mac->macid);
332 
333 	return ret;
334 }
335 
336 static void
337 qtnf_mgmt_frame_register(struct wiphy *wiphy, struct wireless_dev *wdev,
338 			 u16 frame_type, bool reg)
339 {
340 	struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
341 	u16 mgmt_type;
342 	u16 new_mask;
343 	u16 qlink_frame_type = 0;
344 
345 	mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
346 
347 	if (reg)
348 		new_mask = vif->mgmt_frames_bitmask | BIT(mgmt_type);
349 	else
350 		new_mask = vif->mgmt_frames_bitmask & ~BIT(mgmt_type);
351 
352 	if (new_mask == vif->mgmt_frames_bitmask)
353 		return;
354 
355 	switch (frame_type & IEEE80211_FCTL_STYPE) {
356 	case IEEE80211_STYPE_PROBE_REQ:
357 		qlink_frame_type = QLINK_MGMT_FRAME_PROBE_REQ;
358 		break;
359 	case IEEE80211_STYPE_ACTION:
360 		qlink_frame_type = QLINK_MGMT_FRAME_ACTION;
361 		break;
362 	default:
363 		pr_warn("VIF%u.%u: unsupported frame type: %X\n",
364 			vif->mac->macid, vif->vifid,
365 			(frame_type & IEEE80211_FCTL_STYPE) >> 4);
366 		return;
367 	}
368 
369 	if (qtnf_cmd_send_register_mgmt(vif, qlink_frame_type, reg)) {
370 		pr_warn("VIF%u.%u: failed to %sregister mgmt frame type 0x%x\n",
371 			vif->mac->macid, vif->vifid, reg ? "" : "un",
372 			frame_type);
373 		return;
374 	}
375 
376 	vif->mgmt_frames_bitmask = new_mask;
377 	pr_debug("VIF%u.%u: %sregistered mgmt frame type 0x%x\n",
378 		 vif->mac->macid, vif->vifid, reg ? "" : "un", frame_type);
379 }
380 
381 static int
382 qtnf_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
383 	     struct cfg80211_mgmt_tx_params *params, u64 *cookie)
384 {
385 	struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
386 	const struct ieee80211_mgmt *mgmt_frame = (void *)params->buf;
387 	u32 short_cookie = prandom_u32();
388 	u16 flags = 0;
389 
390 	*cookie = short_cookie;
391 
392 	if (params->offchan)
393 		flags |= QLINK_MGMT_FRAME_TX_FLAG_OFFCHAN;
394 
395 	if (params->no_cck)
396 		flags |= QLINK_MGMT_FRAME_TX_FLAG_NO_CCK;
397 
398 	if (params->dont_wait_for_ack)
399 		flags |= QLINK_MGMT_FRAME_TX_FLAG_ACK_NOWAIT;
400 
401 	pr_debug("%s freq:%u; FC:%.4X; DA:%pM; len:%zu; C:%.8X; FL:%.4X\n",
402 		 wdev->netdev->name, params->chan->center_freq,
403 		 le16_to_cpu(mgmt_frame->frame_control), mgmt_frame->da,
404 		 params->len, short_cookie, flags);
405 
406 	return qtnf_cmd_send_mgmt_frame(vif, short_cookie, flags,
407 					params->chan->center_freq,
408 					params->buf, params->len);
409 }
410 
411 static int
412 qtnf_get_station(struct wiphy *wiphy, struct net_device *dev,
413 		 const u8 *mac, struct station_info *sinfo)
414 {
415 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
416 
417 	return qtnf_cmd_get_sta_info(vif, mac, sinfo);
418 }
419 
420 static int
421 qtnf_dump_station(struct wiphy *wiphy, struct net_device *dev,
422 		  int idx, u8 *mac, struct station_info *sinfo)
423 {
424 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
425 	const struct qtnf_sta_node *sta_node;
426 	int ret;
427 
428 	sta_node = qtnf_sta_list_lookup_index(&vif->sta_list, idx);
429 
430 	if (unlikely(!sta_node))
431 		return -ENOENT;
432 
433 	ether_addr_copy(mac, sta_node->mac_addr);
434 
435 	ret = qtnf_cmd_get_sta_info(vif, sta_node->mac_addr, sinfo);
436 
437 	if (unlikely(ret == -ENOENT)) {
438 		qtnf_sta_list_del(&vif->sta_list, mac);
439 		cfg80211_del_sta(vif->netdev, mac, GFP_KERNEL);
440 		sinfo->filled = 0;
441 	}
442 
443 	return ret;
444 }
445 
446 static int qtnf_add_key(struct wiphy *wiphy, struct net_device *dev,
447 			u8 key_index, bool pairwise, const u8 *mac_addr,
448 			struct key_params *params)
449 {
450 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
451 	int ret;
452 
453 	ret = qtnf_cmd_send_add_key(vif, key_index, pairwise, mac_addr, params);
454 	if (ret)
455 		pr_err("VIF%u.%u: failed to add key: cipher=%x idx=%u pw=%u\n",
456 		       vif->mac->macid, vif->vifid, params->cipher, key_index,
457 		       pairwise);
458 
459 	return ret;
460 }
461 
462 static int qtnf_del_key(struct wiphy *wiphy, struct net_device *dev,
463 			u8 key_index, bool pairwise, const u8 *mac_addr)
464 {
465 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
466 	int ret;
467 
468 	ret = qtnf_cmd_send_del_key(vif, key_index, pairwise, mac_addr);
469 	if (ret)
470 		pr_err("VIF%u.%u: failed to delete key: idx=%u pw=%u\n",
471 		       vif->mac->macid, vif->vifid, key_index, pairwise);
472 
473 	return ret;
474 }
475 
476 static int qtnf_set_default_key(struct wiphy *wiphy, struct net_device *dev,
477 				u8 key_index, bool unicast, bool multicast)
478 {
479 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
480 	int ret;
481 
482 	ret = qtnf_cmd_send_set_default_key(vif, key_index, unicast, multicast);
483 	if (ret)
484 		pr_err("VIF%u.%u: failed to set dflt key: idx=%u uc=%u mc=%u\n",
485 		       vif->mac->macid, vif->vifid, key_index, unicast,
486 		       multicast);
487 
488 	return ret;
489 }
490 
491 static int
492 qtnf_set_default_mgmt_key(struct wiphy *wiphy, struct net_device *dev,
493 			  u8 key_index)
494 {
495 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
496 	int ret;
497 
498 	ret = qtnf_cmd_send_set_default_mgmt_key(vif, key_index);
499 	if (ret)
500 		pr_err("VIF%u.%u: failed to set default MGMT key: idx=%u\n",
501 		       vif->mac->macid, vif->vifid, key_index);
502 
503 	return ret;
504 }
505 
506 static int
507 qtnf_change_station(struct wiphy *wiphy, struct net_device *dev,
508 		    const u8 *mac, struct station_parameters *params)
509 {
510 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
511 	int ret;
512 
513 	ret = qtnf_cmd_send_change_sta(vif, mac, params);
514 	if (ret)
515 		pr_err("VIF%u.%u: failed to change STA %pM\n",
516 		       vif->mac->macid, vif->vifid, mac);
517 
518 	return ret;
519 }
520 
521 static int
522 qtnf_del_station(struct wiphy *wiphy, struct net_device *dev,
523 		 struct station_del_parameters *params)
524 {
525 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
526 	int ret;
527 
528 	if (params->mac &&
529 	    (vif->wdev.iftype == NL80211_IFTYPE_AP) &&
530 	    !is_broadcast_ether_addr(params->mac) &&
531 	    !qtnf_sta_list_lookup(&vif->sta_list, params->mac))
532 		return 0;
533 
534 	ret = qtnf_cmd_send_del_sta(vif, params);
535 	if (ret)
536 		pr_err("VIF%u.%u: failed to delete STA %pM\n",
537 		       vif->mac->macid, vif->vifid, params->mac);
538 	return ret;
539 }
540 
541 static void qtnf_scan_timeout(unsigned long data)
542 {
543 	struct qtnf_wmac *mac = (struct qtnf_wmac *)data;
544 
545 	pr_warn("mac%d scan timed out\n", mac->macid);
546 	qtnf_scan_done(mac, true);
547 }
548 
549 static int
550 qtnf_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
551 {
552 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
553 
554 	mac->scan_req = request;
555 
556 	if (qtnf_cmd_send_scan(mac)) {
557 		pr_err("MAC%u: failed to start scan\n", mac->macid);
558 		mac->scan_req = NULL;
559 		return -EFAULT;
560 	}
561 
562 	mac->scan_timeout.data = (unsigned long)mac;
563 	mac->scan_timeout.function = qtnf_scan_timeout;
564 	mod_timer(&mac->scan_timeout,
565 		  jiffies + QTNF_SCAN_TIMEOUT_SEC * HZ);
566 
567 	return 0;
568 }
569 
570 static int
571 qtnf_connect(struct wiphy *wiphy, struct net_device *dev,
572 	     struct cfg80211_connect_params *sme)
573 {
574 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
575 	int ret;
576 
577 	if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
578 		return -EOPNOTSUPP;
579 
580 	if (vif->sta_state != QTNF_STA_DISCONNECTED)
581 		return -EBUSY;
582 
583 	if (sme->bssid)
584 		ether_addr_copy(vif->bssid, sme->bssid);
585 	else
586 		eth_zero_addr(vif->bssid);
587 
588 	ret = qtnf_cmd_send_connect(vif, sme);
589 	if (ret) {
590 		pr_err("VIF%u.%u: failed to connect\n", vif->mac->macid,
591 		       vif->vifid);
592 		return ret;
593 	}
594 
595 	vif->sta_state = QTNF_STA_CONNECTING;
596 	return 0;
597 }
598 
599 static int
600 qtnf_disconnect(struct wiphy *wiphy, struct net_device *dev,
601 		u16 reason_code)
602 {
603 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
604 	struct qtnf_vif *vif;
605 	int ret;
606 
607 	vif = qtnf_mac_get_base_vif(mac);
608 	if (!vif) {
609 		pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
610 		return -EFAULT;
611 	}
612 
613 	if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
614 		return -EOPNOTSUPP;
615 
616 	if (vif->sta_state == QTNF_STA_DISCONNECTED)
617 		return 0;
618 
619 	ret = qtnf_cmd_send_disconnect(vif, reason_code);
620 	if (ret) {
621 		pr_err("VIF%u.%u: failed to disconnect\n", mac->macid,
622 		       vif->vifid);
623 		return ret;
624 	}
625 
626 	vif->sta_state = QTNF_STA_DISCONNECTED;
627 	return 0;
628 }
629 
630 static int
631 qtnf_dump_survey(struct wiphy *wiphy, struct net_device *dev,
632 		 int idx, struct survey_info *survey)
633 {
634 	struct qtnf_wmac *mac = wiphy_priv(wiphy);
635 	struct wireless_dev *wdev = dev->ieee80211_ptr;
636 	struct ieee80211_supported_band *sband;
637 	const struct cfg80211_chan_def *chandef = &wdev->chandef;
638 	struct ieee80211_channel *chan;
639 	struct qtnf_chan_stats stats;
640 	struct qtnf_vif *vif;
641 	int ret;
642 
643 	vif = qtnf_netdev_get_priv(dev);
644 
645 	sband = wiphy->bands[NL80211_BAND_2GHZ];
646 	if (sband && idx >= sband->n_channels) {
647 		idx -= sband->n_channels;
648 		sband = NULL;
649 	}
650 
651 	if (!sband)
652 		sband = wiphy->bands[NL80211_BAND_5GHZ];
653 
654 	if (!sband || idx >= sband->n_channels)
655 		return -ENOENT;
656 
657 	chan = &sband->channels[idx];
658 	memset(&stats, 0, sizeof(stats));
659 
660 	survey->channel = chan;
661 	survey->filled = 0x0;
662 
663 	if (chandef->chan) {
664 		if (chan->hw_value == chandef->chan->hw_value)
665 			survey->filled = SURVEY_INFO_IN_USE;
666 	}
667 
668 	ret = qtnf_cmd_get_chan_stats(mac, chan->hw_value, &stats);
669 	switch (ret) {
670 	case 0:
671 		if (unlikely(stats.chan_num != chan->hw_value)) {
672 			pr_err("received stats for channel %d instead of %d\n",
673 			       stats.chan_num, chan->hw_value);
674 			ret = -EINVAL;
675 			break;
676 		}
677 
678 		survey->filled |= SURVEY_INFO_TIME |
679 				 SURVEY_INFO_TIME_SCAN |
680 				 SURVEY_INFO_TIME_BUSY |
681 				 SURVEY_INFO_TIME_RX |
682 				 SURVEY_INFO_TIME_TX |
683 				 SURVEY_INFO_NOISE_DBM;
684 
685 		survey->time_scan = stats.cca_try;
686 		survey->time = stats.cca_try;
687 		survey->time_tx = stats.cca_tx;
688 		survey->time_rx = stats.cca_rx;
689 		survey->time_busy = stats.cca_busy;
690 		survey->noise = stats.chan_noise;
691 		break;
692 	case -ENOENT:
693 		pr_debug("no stats for channel %u\n", chan->hw_value);
694 		ret = 0;
695 		break;
696 	default:
697 		pr_debug("failed to get chan(%d) stats from card\n",
698 			 chan->hw_value);
699 		ret = -EINVAL;
700 		break;
701 	}
702 
703 	return ret;
704 }
705 
706 static int
707 qtnf_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
708 		 struct cfg80211_chan_def *chandef)
709 {
710 	struct net_device *ndev = wdev->netdev;
711 	struct qtnf_vif *vif;
712 	int ret;
713 
714 	if (!ndev)
715 		return -ENODEV;
716 
717 	vif = qtnf_netdev_get_priv(wdev->netdev);
718 
719 	ret = qtnf_cmd_get_channel(vif, chandef);
720 	if (ret) {
721 		pr_err("%s: failed to get channel: %d\n", ndev->name, ret);
722 		goto out;
723 	}
724 
725 	if (!cfg80211_chandef_valid(chandef)) {
726 		pr_err("%s: bad chan freq1=%u freq2=%u bw=%u\n", ndev->name,
727 		       chandef->center_freq1, chandef->center_freq2,
728 		       chandef->width);
729 		ret = -ENODATA;
730 	}
731 
732 out:
733 	return ret;
734 }
735 
736 static int qtnf_channel_switch(struct wiphy *wiphy, struct net_device *dev,
737 			       struct cfg80211_csa_settings *params)
738 {
739 	struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
740 	int ret;
741 
742 	pr_debug("%s: chan(%u) count(%u) radar(%u) block_tx(%u)\n", dev->name,
743 		 params->chandef.chan->hw_value, params->count,
744 		 params->radar_required, params->block_tx);
745 
746 	if (!cfg80211_chandef_valid(&params->chandef)) {
747 		pr_err("%s: invalid channel\n", dev->name);
748 		return -EINVAL;
749 	}
750 
751 	ret = qtnf_cmd_send_chan_switch(vif, params);
752 	if (ret)
753 		pr_warn("%s: failed to switch to channel (%u)\n",
754 			dev->name, params->chandef.chan->hw_value);
755 
756 	return ret;
757 }
758 
759 static struct cfg80211_ops qtn_cfg80211_ops = {
760 	.add_virtual_intf	= qtnf_add_virtual_intf,
761 	.change_virtual_intf	= qtnf_change_virtual_intf,
762 	.del_virtual_intf	= qtnf_del_virtual_intf,
763 	.start_ap		= qtnf_start_ap,
764 	.change_beacon		= qtnf_change_beacon,
765 	.stop_ap		= qtnf_stop_ap,
766 	.set_wiphy_params	= qtnf_set_wiphy_params,
767 	.mgmt_frame_register	= qtnf_mgmt_frame_register,
768 	.mgmt_tx		= qtnf_mgmt_tx,
769 	.change_station		= qtnf_change_station,
770 	.del_station		= qtnf_del_station,
771 	.get_station		= qtnf_get_station,
772 	.dump_station		= qtnf_dump_station,
773 	.add_key		= qtnf_add_key,
774 	.del_key		= qtnf_del_key,
775 	.set_default_key	= qtnf_set_default_key,
776 	.set_default_mgmt_key	= qtnf_set_default_mgmt_key,
777 	.scan			= qtnf_scan,
778 	.connect		= qtnf_connect,
779 	.disconnect		= qtnf_disconnect,
780 	.dump_survey		= qtnf_dump_survey,
781 	.get_channel		= qtnf_get_channel,
782 	.channel_switch		= qtnf_channel_switch
783 };
784 
785 static void qtnf_cfg80211_reg_notifier(struct wiphy *wiphy_in,
786 				       struct regulatory_request *req)
787 {
788 	struct qtnf_wmac *mac = wiphy_priv(wiphy_in);
789 	struct qtnf_bus *bus = mac->bus;
790 	struct wiphy *wiphy;
791 	unsigned int mac_idx;
792 	enum nl80211_band band;
793 	int ret;
794 
795 	pr_debug("MAC%u: initiator=%d alpha=%c%c\n", mac->macid, req->initiator,
796 		 req->alpha2[0], req->alpha2[1]);
797 
798 	ret = qtnf_cmd_reg_notify(bus, req);
799 	if (ret) {
800 		if (ret != -EOPNOTSUPP && ret != -EALREADY)
801 			pr_err("failed to update reg domain to %c%c\n",
802 			       req->alpha2[0], req->alpha2[1]);
803 		return;
804 	}
805 
806 	for (mac_idx = 0; mac_idx < QTNF_MAX_MAC; ++mac_idx) {
807 		if (!(bus->hw_info.mac_bitmap & (1 << mac_idx)))
808 			continue;
809 
810 		mac = bus->mac[mac_idx];
811 		wiphy = priv_to_wiphy(mac);
812 
813 		for (band = 0; band < NUM_NL80211_BANDS; ++band) {
814 			if (!wiphy->bands[band])
815 				continue;
816 
817 			ret = qtnf_cmd_get_mac_chan_info(mac,
818 							 wiphy->bands[band]);
819 			if (ret)
820 				pr_err("failed to get chan info for mac %u band %u\n",
821 				       mac_idx, band);
822 		}
823 	}
824 }
825 
826 void qtnf_band_setup_htvht_caps(struct qtnf_mac_info *macinfo,
827 				struct ieee80211_supported_band *band)
828 {
829 	struct ieee80211_sta_ht_cap *ht_cap;
830 	struct ieee80211_sta_vht_cap *vht_cap;
831 
832 	ht_cap = &band->ht_cap;
833 	ht_cap->ht_supported = true;
834 	memcpy(&ht_cap->cap, &macinfo->ht_cap.cap_info,
835 	       sizeof(u16));
836 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
837 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
838 	memcpy(&ht_cap->mcs, &macinfo->ht_cap.mcs,
839 	       sizeof(ht_cap->mcs));
840 
841 	if (macinfo->phymode_cap & QLINK_PHYMODE_AC) {
842 		vht_cap = &band->vht_cap;
843 		vht_cap->vht_supported = true;
844 		memcpy(&vht_cap->cap,
845 		       &macinfo->vht_cap.vht_cap_info, sizeof(u32));
846 		/* Update MCS support for VHT */
847 		memcpy(&vht_cap->vht_mcs,
848 		       &macinfo->vht_cap.supp_mcs,
849 		       sizeof(struct ieee80211_vht_mcs_info));
850 	}
851 }
852 
853 struct wiphy *qtnf_wiphy_allocate(struct qtnf_bus *bus)
854 {
855 	struct wiphy *wiphy;
856 
857 	wiphy = wiphy_new(&qtn_cfg80211_ops, sizeof(struct qtnf_wmac));
858 	if (!wiphy)
859 		return NULL;
860 
861 	set_wiphy_dev(wiphy, bus->dev);
862 
863 	return wiphy;
864 }
865 
866 static int qtnf_wiphy_setup_if_comb(struct wiphy *wiphy,
867 				    struct ieee80211_iface_combination *if_comb,
868 				    const struct qtnf_mac_info *mac_info)
869 {
870 	size_t max_interfaces = 0;
871 	u16 interface_modes = 0;
872 	size_t i;
873 
874 	if (unlikely(!mac_info->limits || !mac_info->n_limits))
875 		return -ENOENT;
876 
877 	if_comb->limits = mac_info->limits;
878 	if_comb->n_limits = mac_info->n_limits;
879 
880 	for (i = 0; i < mac_info->n_limits; i++) {
881 		max_interfaces += mac_info->limits[i].max;
882 		interface_modes |= mac_info->limits[i].types;
883 	}
884 
885 	if_comb->num_different_channels = 1;
886 	if_comb->beacon_int_infra_match = true;
887 	if_comb->max_interfaces = max_interfaces;
888 	if_comb->radar_detect_widths = mac_info->radar_detect_widths;
889 	wiphy->interface_modes = interface_modes;
890 
891 	return 0;
892 }
893 
894 int qtnf_wiphy_register(struct qtnf_hw_info *hw_info, struct qtnf_wmac *mac)
895 {
896 	struct wiphy *wiphy = priv_to_wiphy(mac);
897 	struct ieee80211_iface_combination *iface_comb = NULL;
898 	int ret;
899 
900 	if (!wiphy) {
901 		pr_err("invalid wiphy pointer\n");
902 		return -EFAULT;
903 	}
904 
905 	iface_comb = kzalloc(sizeof(*iface_comb), GFP_KERNEL);
906 	if (!iface_comb)
907 		return -ENOMEM;
908 
909 	ret = qtnf_wiphy_setup_if_comb(wiphy, iface_comb, &mac->macinfo);
910 	if (ret)
911 		goto out;
912 
913 	pr_info("MAC%u: phymode=%#x radar=%#x\n", mac->macid,
914 		mac->macinfo.phymode_cap, mac->macinfo.radar_detect_widths);
915 
916 	wiphy->frag_threshold = mac->macinfo.frag_thr;
917 	wiphy->rts_threshold = mac->macinfo.rts_thr;
918 	wiphy->retry_short = mac->macinfo.sretry_limit;
919 	wiphy->retry_long = mac->macinfo.lretry_limit;
920 	wiphy->coverage_class = mac->macinfo.coverage_class;
921 
922 	wiphy->max_scan_ssids = QTNF_MAX_SSID_LIST_LENGTH;
923 	wiphy->max_scan_ie_len = QTNF_MAX_VSIE_LEN;
924 	wiphy->mgmt_stypes = qtnf_mgmt_stypes;
925 	wiphy->max_remain_on_channel_duration = 5000;
926 
927 	wiphy->iface_combinations = iface_comb;
928 	wiphy->n_iface_combinations = 1;
929 	wiphy->max_num_csa_counters = 2;
930 
931 	/* Initialize cipher suits */
932 	wiphy->cipher_suites = qtnf_cipher_suites;
933 	wiphy->n_cipher_suites = ARRAY_SIZE(qtnf_cipher_suites);
934 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
935 	wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
936 			WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
937 			WIPHY_FLAG_AP_UAPSD |
938 			WIPHY_FLAG_HAS_CHANNEL_SWITCH;
939 
940 	wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
941 				    NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2;
942 
943 	wiphy->available_antennas_tx = mac->macinfo.num_tx_chain;
944 	wiphy->available_antennas_rx = mac->macinfo.num_rx_chain;
945 
946 	wiphy->max_ap_assoc_sta = mac->macinfo.max_ap_assoc_sta;
947 
948 	ether_addr_copy(wiphy->perm_addr, mac->macaddr);
949 
950 	if (hw_info->hw_capab & QLINK_HW_SUPPORTS_REG_UPDATE) {
951 		wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
952 			REGULATORY_CUSTOM_REG;
953 		wiphy->reg_notifier = qtnf_cfg80211_reg_notifier;
954 		wiphy_apply_custom_regulatory(wiphy, hw_info->rd);
955 	} else {
956 		wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
957 	}
958 
959 	ret = wiphy_register(wiphy);
960 	if (ret < 0)
961 		goto out;
962 
963 	if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
964 		ret = regulatory_set_wiphy_regd(wiphy, hw_info->rd);
965 	else if (isalpha(hw_info->rd->alpha2[0]) &&
966 		 isalpha(hw_info->rd->alpha2[1]))
967 		ret = regulatory_hint(wiphy, hw_info->rd->alpha2);
968 
969 out:
970 	if (ret) {
971 		kfree(iface_comb);
972 		return ret;
973 	}
974 
975 	return 0;
976 }
977 
978 void qtnf_netdev_updown(struct net_device *ndev, bool up)
979 {
980 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
981 
982 	if (qtnf_cmd_send_updown_intf(vif, up))
983 		pr_err("failed to send up/down command to FW\n");
984 }
985 
986 void qtnf_virtual_intf_cleanup(struct net_device *ndev)
987 {
988 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
989 	struct qtnf_wmac *mac = wiphy_priv(vif->wdev.wiphy);
990 
991 	if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
992 		switch (vif->sta_state) {
993 		case QTNF_STA_DISCONNECTED:
994 			break;
995 		case QTNF_STA_CONNECTING:
996 			cfg80211_connect_result(vif->netdev,
997 						vif->bssid, NULL, 0,
998 						NULL, 0,
999 						WLAN_STATUS_UNSPECIFIED_FAILURE,
1000 						GFP_KERNEL);
1001 			qtnf_disconnect(vif->wdev.wiphy, ndev,
1002 					WLAN_REASON_DEAUTH_LEAVING);
1003 			break;
1004 		case QTNF_STA_CONNECTED:
1005 			cfg80211_disconnected(vif->netdev,
1006 					      WLAN_REASON_DEAUTH_LEAVING,
1007 					      NULL, 0, 1, GFP_KERNEL);
1008 			qtnf_disconnect(vif->wdev.wiphy, ndev,
1009 					WLAN_REASON_DEAUTH_LEAVING);
1010 			break;
1011 		}
1012 
1013 		vif->sta_state = QTNF_STA_DISCONNECTED;
1014 	}
1015 
1016 	qtnf_scan_done(mac, true);
1017 }
1018 
1019 void qtnf_cfg80211_vif_reset(struct qtnf_vif *vif)
1020 {
1021 	if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
1022 		switch (vif->sta_state) {
1023 		case QTNF_STA_CONNECTING:
1024 			cfg80211_connect_result(vif->netdev,
1025 						vif->bssid, NULL, 0,
1026 						NULL, 0,
1027 						WLAN_STATUS_UNSPECIFIED_FAILURE,
1028 						GFP_KERNEL);
1029 			break;
1030 		case QTNF_STA_CONNECTED:
1031 			cfg80211_disconnected(vif->netdev,
1032 					      WLAN_REASON_DEAUTH_LEAVING,
1033 					      NULL, 0, 1, GFP_KERNEL);
1034 			break;
1035 		case QTNF_STA_DISCONNECTED:
1036 			break;
1037 		}
1038 	}
1039 
1040 	cfg80211_shutdown_all_interfaces(vif->wdev.wiphy);
1041 	vif->sta_state = QTNF_STA_DISCONNECTED;
1042 }
1043 
1044 void qtnf_band_init_rates(struct ieee80211_supported_band *band)
1045 {
1046 	switch (band->band) {
1047 	case NL80211_BAND_2GHZ:
1048 		band->bitrates = qtnf_rates_2g;
1049 		band->n_bitrates = ARRAY_SIZE(qtnf_rates_2g);
1050 		break;
1051 	case NL80211_BAND_5GHZ:
1052 		band->bitrates = qtnf_rates_5g;
1053 		band->n_bitrates = ARRAY_SIZE(qtnf_rates_5g);
1054 		break;
1055 	default:
1056 		band->bitrates = NULL;
1057 		band->n_bitrates = 0;
1058 		break;
1059 	}
1060 }
1061