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