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