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