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