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