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