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