Linux-libre 5.4.49-gnu
[librecmc/linux-libre.git] / drivers / net / wireless / quantenna / qtnfmac / cfg80211.c
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 err_mac;
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 err_net;
249         }
250
251         vif->wdev.netdev = vif->netdev;
252         return &vif->wdev;
253
254 err_net:
255         vif->netdev = NULL;
256 err_mac:
257         qtnf_cmd_send_del_intf(vif);
258 err_cmd:
259         vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
260
261         return ERR_PTR(ret);
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         return ret;
347 }
348
349 static int qtnf_set_wiphy_params(struct wiphy *wiphy, u32 changed)
350 {
351         struct qtnf_wmac *mac = wiphy_priv(wiphy);
352         struct qtnf_vif *vif;
353         int ret;
354
355         vif = qtnf_mac_get_base_vif(mac);
356         if (!vif) {
357                 pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
358                 return -EFAULT;
359         }
360
361         ret = qtnf_cmd_send_update_phy_params(mac, changed);
362         if (ret)
363                 pr_err("MAC%u: failed to update PHY params\n", mac->macid);
364
365         return ret;
366 }
367
368 static void
369 qtnf_mgmt_frame_register(struct wiphy *wiphy, struct wireless_dev *wdev,
370                          u16 frame_type, bool reg)
371 {
372         struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
373         u16 mgmt_type;
374         u16 new_mask;
375         u16 qlink_frame_type = 0;
376
377         mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
378
379         if (reg)
380                 new_mask = vif->mgmt_frames_bitmask | BIT(mgmt_type);
381         else
382                 new_mask = vif->mgmt_frames_bitmask & ~BIT(mgmt_type);
383
384         if (new_mask == vif->mgmt_frames_bitmask)
385                 return;
386
387         switch (frame_type & IEEE80211_FCTL_STYPE) {
388         case IEEE80211_STYPE_REASSOC_REQ:
389         case IEEE80211_STYPE_ASSOC_REQ:
390                 qlink_frame_type = QLINK_MGMT_FRAME_ASSOC_REQ;
391                 break;
392         case IEEE80211_STYPE_AUTH:
393                 qlink_frame_type = QLINK_MGMT_FRAME_AUTH;
394                 break;
395         case IEEE80211_STYPE_PROBE_REQ:
396                 qlink_frame_type = QLINK_MGMT_FRAME_PROBE_REQ;
397                 break;
398         case IEEE80211_STYPE_ACTION:
399                 qlink_frame_type = QLINK_MGMT_FRAME_ACTION;
400                 break;
401         default:
402                 pr_warn("VIF%u.%u: unsupported frame type: %X\n",
403                         vif->mac->macid, vif->vifid,
404                         (frame_type & IEEE80211_FCTL_STYPE) >> 4);
405                 return;
406         }
407
408         if (qtnf_cmd_send_register_mgmt(vif, qlink_frame_type, reg)) {
409                 pr_warn("VIF%u.%u: failed to %sregister mgmt frame type 0x%x\n",
410                         vif->mac->macid, vif->vifid, reg ? "" : "un",
411                         frame_type);
412                 return;
413         }
414
415         vif->mgmt_frames_bitmask = new_mask;
416         pr_debug("VIF%u.%u: %sregistered mgmt frame type 0x%x\n",
417                  vif->mac->macid, vif->vifid, reg ? "" : "un", frame_type);
418 }
419
420 static int
421 qtnf_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
422              struct cfg80211_mgmt_tx_params *params, u64 *cookie)
423 {
424         struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
425         const struct ieee80211_mgmt *mgmt_frame = (void *)params->buf;
426         u32 short_cookie = prandom_u32();
427         u16 flags = 0;
428         u16 freq;
429
430         *cookie = short_cookie;
431
432         if (params->offchan)
433                 flags |= QLINK_FRAME_TX_FLAG_OFFCHAN;
434
435         if (params->no_cck)
436                 flags |= QLINK_FRAME_TX_FLAG_NO_CCK;
437
438         if (params->dont_wait_for_ack)
439                 flags |= QLINK_FRAME_TX_FLAG_ACK_NOWAIT;
440
441         /* If channel is not specified, pass "freq = 0" to tell device
442          * firmware to use current channel.
443          */
444         if (params->chan)
445                 freq = params->chan->center_freq;
446         else
447                 freq = 0;
448
449         pr_debug("%s freq:%u; FC:%.4X; DA:%pM; len:%zu; C:%.8X; FL:%.4X\n",
450                  wdev->netdev->name, freq,
451                  le16_to_cpu(mgmt_frame->frame_control), mgmt_frame->da,
452                  params->len, short_cookie, flags);
453
454         return qtnf_cmd_send_frame(vif, short_cookie, flags,
455                                    freq, params->buf, params->len);
456 }
457
458 static int
459 qtnf_get_station(struct wiphy *wiphy, struct net_device *dev,
460                  const u8 *mac, struct station_info *sinfo)
461 {
462         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
463
464         sinfo->generation = vif->generation;
465         return qtnf_cmd_get_sta_info(vif, mac, sinfo);
466 }
467
468 static int
469 qtnf_dump_station(struct wiphy *wiphy, struct net_device *dev,
470                   int idx, u8 *mac, struct station_info *sinfo)
471 {
472         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
473         const struct qtnf_sta_node *sta_node;
474         int ret;
475
476         switch (vif->wdev.iftype) {
477         case NL80211_IFTYPE_STATION:
478                 if (idx != 0 || !vif->wdev.current_bss)
479                         return -ENOENT;
480
481                 ether_addr_copy(mac, vif->bssid);
482                 break;
483         case NL80211_IFTYPE_AP:
484                 sta_node = qtnf_sta_list_lookup_index(&vif->sta_list, idx);
485                 if (unlikely(!sta_node))
486                         return -ENOENT;
487
488                 ether_addr_copy(mac, sta_node->mac_addr);
489                 break;
490         default:
491                 return -ENOTSUPP;
492         }
493
494         ret = qtnf_cmd_get_sta_info(vif, mac, sinfo);
495
496         if (vif->wdev.iftype == NL80211_IFTYPE_AP) {
497                 if (ret == -ENOENT) {
498                         cfg80211_del_sta(vif->netdev, mac, GFP_KERNEL);
499                         sinfo->filled = 0;
500                 }
501         }
502
503         sinfo->generation = vif->generation;
504
505         return ret;
506 }
507
508 static int qtnf_add_key(struct wiphy *wiphy, struct net_device *dev,
509                         u8 key_index, bool pairwise, const u8 *mac_addr,
510                         struct key_params *params)
511 {
512         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
513         int ret;
514
515         ret = qtnf_cmd_send_add_key(vif, key_index, pairwise, mac_addr, params);
516         if (ret)
517                 pr_err("VIF%u.%u: failed to add key: cipher=%x idx=%u pw=%u\n",
518                        vif->mac->macid, vif->vifid, params->cipher, key_index,
519                        pairwise);
520
521         return ret;
522 }
523
524 static int qtnf_del_key(struct wiphy *wiphy, struct net_device *dev,
525                         u8 key_index, bool pairwise, const u8 *mac_addr)
526 {
527         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
528         int ret;
529
530         ret = qtnf_cmd_send_del_key(vif, key_index, pairwise, mac_addr);
531         if (ret) {
532                 if (ret == -ENOENT) {
533                         pr_debug("VIF%u.%u: key index %d out of bounds\n",
534                                  vif->mac->macid, vif->vifid, key_index);
535                 } else {
536                         pr_err("VIF%u.%u: failed to delete key: idx=%u pw=%u\n",
537                                vif->mac->macid, vif->vifid,
538                                key_index, pairwise);
539                 }
540         }
541
542         return ret;
543 }
544
545 static int qtnf_set_default_key(struct wiphy *wiphy, struct net_device *dev,
546                                 u8 key_index, bool unicast, bool multicast)
547 {
548         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
549         int ret;
550
551         ret = qtnf_cmd_send_set_default_key(vif, key_index, unicast, multicast);
552         if (ret)
553                 pr_err("VIF%u.%u: failed to set dflt key: idx=%u uc=%u mc=%u\n",
554                        vif->mac->macid, vif->vifid, key_index, unicast,
555                        multicast);
556
557         return ret;
558 }
559
560 static int
561 qtnf_set_default_mgmt_key(struct wiphy *wiphy, struct net_device *dev,
562                           u8 key_index)
563 {
564         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
565         int ret;
566
567         ret = qtnf_cmd_send_set_default_mgmt_key(vif, key_index);
568         if (ret)
569                 pr_err("VIF%u.%u: failed to set default MGMT key: idx=%u\n",
570                        vif->mac->macid, vif->vifid, key_index);
571
572         return ret;
573 }
574
575 static int
576 qtnf_change_station(struct wiphy *wiphy, struct net_device *dev,
577                     const u8 *mac, struct station_parameters *params)
578 {
579         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
580         int ret;
581
582         ret = qtnf_cmd_send_change_sta(vif, mac, params);
583         if (ret)
584                 pr_err("VIF%u.%u: failed to change STA %pM\n",
585                        vif->mac->macid, vif->vifid, mac);
586
587         return ret;
588 }
589
590 static int
591 qtnf_del_station(struct wiphy *wiphy, struct net_device *dev,
592                  struct station_del_parameters *params)
593 {
594         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
595         int ret;
596
597         if (params->mac &&
598             (vif->wdev.iftype == NL80211_IFTYPE_AP) &&
599             !is_broadcast_ether_addr(params->mac) &&
600             !qtnf_sta_list_lookup(&vif->sta_list, params->mac))
601                 return 0;
602
603         ret = qtnf_cmd_send_del_sta(vif, params);
604         if (ret)
605                 pr_err("VIF%u.%u: failed to delete STA %pM\n",
606                        vif->mac->macid, vif->vifid, params->mac);
607
608         return ret;
609 }
610
611 static int
612 qtnf_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
613 {
614         struct qtnf_wmac *mac = wiphy_priv(wiphy);
615         int ret;
616
617         cancel_delayed_work_sync(&mac->scan_timeout);
618
619         mac->scan_req = request;
620
621         ret = qtnf_cmd_send_scan(mac);
622         if (ret) {
623                 pr_err("MAC%u: failed to start scan\n", mac->macid);
624                 mac->scan_req = NULL;
625                 goto out;
626         }
627
628         pr_debug("MAC%u: scan started\n", mac->macid);
629         queue_delayed_work(mac->bus->workqueue, &mac->scan_timeout,
630                            QTNF_SCAN_TIMEOUT_SEC * HZ);
631
632 out:
633         return ret;
634 }
635
636 static int
637 qtnf_connect(struct wiphy *wiphy, struct net_device *dev,
638              struct cfg80211_connect_params *sme)
639 {
640         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
641         int ret;
642
643         if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
644                 return -EOPNOTSUPP;
645
646         if (sme->auth_type == NL80211_AUTHTYPE_SAE &&
647             !(sme->flags & CONNECT_REQ_EXTERNAL_AUTH_SUPPORT)) {
648                 pr_err("can not offload authentication to userspace\n");
649                 return -EOPNOTSUPP;
650         }
651
652         if (sme->bssid)
653                 ether_addr_copy(vif->bssid, sme->bssid);
654         else
655                 eth_zero_addr(vif->bssid);
656
657         ret = qtnf_cmd_send_connect(vif, sme);
658         if (ret) {
659                 pr_err("VIF%u.%u: failed to connect\n",
660                        vif->mac->macid, vif->vifid);
661                 goto out;
662         }
663
664 out:
665         return ret;
666 }
667
668 static int
669 qtnf_external_auth(struct wiphy *wiphy, struct net_device *dev,
670                    struct cfg80211_external_auth_params *auth)
671 {
672         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
673         int ret;
674
675         if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
676                 return -EOPNOTSUPP;
677
678         if (!ether_addr_equal(vif->bssid, auth->bssid))
679                 pr_warn("unexpected bssid: %pM", auth->bssid);
680
681         ret = qtnf_cmd_send_external_auth(vif, auth);
682         if (ret) {
683                 pr_err("VIF%u.%u: failed to report external auth\n",
684                        vif->mac->macid, vif->vifid);
685                 goto out;
686         }
687
688 out:
689         return ret;
690 }
691
692 static int
693 qtnf_disconnect(struct wiphy *wiphy, struct net_device *dev,
694                 u16 reason_code)
695 {
696         struct qtnf_wmac *mac = wiphy_priv(wiphy);
697         struct qtnf_vif *vif;
698         int ret = 0;
699
700         vif = qtnf_mac_get_base_vif(mac);
701         if (!vif) {
702                 pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
703                 return -EFAULT;
704         }
705
706         if (vif->wdev.iftype != NL80211_IFTYPE_STATION) {
707                 ret = -EOPNOTSUPP;
708                 goto out;
709         }
710
711         ret = qtnf_cmd_send_disconnect(vif, reason_code);
712         if (ret)
713                 pr_err("VIF%u.%u: failed to disconnect\n",
714                        mac->macid, vif->vifid);
715
716         if (vif->wdev.current_bss) {
717                 netif_carrier_off(vif->netdev);
718                 cfg80211_disconnected(vif->netdev, reason_code,
719                                       NULL, 0, true, GFP_KERNEL);
720         }
721
722 out:
723         return ret;
724 }
725
726 static int
727 qtnf_dump_survey(struct wiphy *wiphy, struct net_device *dev,
728                  int idx, struct survey_info *survey)
729 {
730         struct qtnf_wmac *mac = wiphy_priv(wiphy);
731         struct wireless_dev *wdev = dev->ieee80211_ptr;
732         struct ieee80211_supported_band *sband;
733         const struct cfg80211_chan_def *chandef = &wdev->chandef;
734         struct ieee80211_channel *chan;
735         struct qtnf_chan_stats stats;
736         int ret;
737
738         sband = wiphy->bands[NL80211_BAND_2GHZ];
739         if (sband && idx >= sband->n_channels) {
740                 idx -= sband->n_channels;
741                 sband = NULL;
742         }
743
744         if (!sband)
745                 sband = wiphy->bands[NL80211_BAND_5GHZ];
746
747         if (!sband || idx >= sband->n_channels)
748                 return -ENOENT;
749
750         chan = &sband->channels[idx];
751         memset(&stats, 0, sizeof(stats));
752
753         survey->channel = chan;
754         survey->filled = 0x0;
755
756         if (chandef->chan) {
757                 if (chan->hw_value == chandef->chan->hw_value)
758                         survey->filled = SURVEY_INFO_IN_USE;
759         }
760
761         ret = qtnf_cmd_get_chan_stats(mac, chan->hw_value, &stats);
762         switch (ret) {
763         case 0:
764                 if (unlikely(stats.chan_num != chan->hw_value)) {
765                         pr_err("received stats for channel %d instead of %d\n",
766                                stats.chan_num, chan->hw_value);
767                         ret = -EINVAL;
768                         break;
769                 }
770
771                 survey->filled |= SURVEY_INFO_TIME |
772                                  SURVEY_INFO_TIME_SCAN |
773                                  SURVEY_INFO_TIME_BUSY |
774                                  SURVEY_INFO_TIME_RX |
775                                  SURVEY_INFO_TIME_TX |
776                                  SURVEY_INFO_NOISE_DBM;
777
778                 survey->time_scan = stats.cca_try;
779                 survey->time = stats.cca_try;
780                 survey->time_tx = stats.cca_tx;
781                 survey->time_rx = stats.cca_rx;
782                 survey->time_busy = stats.cca_busy;
783                 survey->noise = stats.chan_noise;
784                 break;
785         case -ENOENT:
786                 pr_debug("no stats for channel %u\n", chan->hw_value);
787                 ret = 0;
788                 break;
789         default:
790                 pr_debug("failed to get chan(%d) stats from card\n",
791                          chan->hw_value);
792                 break;
793         }
794
795         return ret;
796 }
797
798 static int
799 qtnf_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
800                  struct cfg80211_chan_def *chandef)
801 {
802         struct net_device *ndev = wdev->netdev;
803         struct qtnf_vif *vif;
804         int ret;
805
806         if (!ndev)
807                 return -ENODEV;
808
809         vif = qtnf_netdev_get_priv(wdev->netdev);
810
811         ret = qtnf_cmd_get_channel(vif, chandef);
812         if (ret) {
813                 pr_err("%s: failed to get channel: %d\n", ndev->name, ret);
814                 ret = -ENODATA;
815                 goto out;
816         }
817
818         if (!cfg80211_chandef_valid(chandef)) {
819                 pr_err("%s: bad channel freq=%u cf1=%u cf2=%u bw=%u\n",
820                        ndev->name, chandef->chan->center_freq,
821                        chandef->center_freq1, chandef->center_freq2,
822                        chandef->width);
823                 ret = -ENODATA;
824                 goto out;
825         }
826
827 out:
828         return ret;
829 }
830
831 static int qtnf_channel_switch(struct wiphy *wiphy, struct net_device *dev,
832                                struct cfg80211_csa_settings *params)
833 {
834         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
835         int ret;
836
837         pr_debug("%s: chan(%u) count(%u) radar(%u) block_tx(%u)\n", dev->name,
838                  params->chandef.chan->hw_value, params->count,
839                  params->radar_required, params->block_tx);
840
841         if (!cfg80211_chandef_valid(&params->chandef)) {
842                 pr_err("%s: invalid channel\n", dev->name);
843                 return -EINVAL;
844         }
845
846         ret = qtnf_cmd_send_chan_switch(vif, params);
847         if (ret)
848                 pr_warn("%s: failed to switch to channel (%u)\n",
849                         dev->name, params->chandef.chan->hw_value);
850
851         return ret;
852 }
853
854 static int qtnf_start_radar_detection(struct wiphy *wiphy,
855                                       struct net_device *ndev,
856                                       struct cfg80211_chan_def *chandef,
857                                       u32 cac_time_ms)
858 {
859         struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
860         int ret;
861
862         if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
863                 return -ENOTSUPP;
864
865         ret = qtnf_cmd_start_cac(vif, chandef, cac_time_ms);
866         if (ret)
867                 pr_err("%s: failed to start CAC ret=%d\n", ndev->name, ret);
868
869         return ret;
870 }
871
872 static int qtnf_set_mac_acl(struct wiphy *wiphy,
873                             struct net_device *dev,
874                             const struct cfg80211_acl_data *params)
875 {
876         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
877         int ret;
878
879         ret = qtnf_cmd_set_mac_acl(vif, params);
880         if (ret)
881                 pr_err("%s: failed to set mac ACL ret=%d\n", dev->name, ret);
882
883         return ret;
884 }
885
886 static int qtnf_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
887                                bool enabled, int timeout)
888 {
889         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
890         int ret;
891
892         ret = qtnf_cmd_send_pm_set(vif, enabled ? QLINK_PM_AUTO_STANDBY :
893                                    QLINK_PM_OFF, timeout);
894         if (ret)
895                 pr_err("%s: failed to set PM mode ret=%d\n", dev->name, ret);
896
897         return ret;
898 }
899
900 #ifdef CONFIG_PM
901 static int qtnf_suspend(struct wiphy *wiphy, struct cfg80211_wowlan *wowlan)
902 {
903         struct qtnf_wmac *mac = wiphy_priv(wiphy);
904         struct qtnf_vif *vif;
905         int ret = 0;
906
907         vif = qtnf_mac_get_base_vif(mac);
908         if (!vif) {
909                 pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
910                 ret = -EFAULT;
911                 goto exit;
912         }
913
914         if (!wowlan) {
915                 pr_debug("WoWLAN triggers are not enabled\n");
916                 qtnf_virtual_intf_cleanup(vif->netdev);
917                 goto exit;
918         }
919
920         qtnf_scan_done(vif->mac, true);
921
922         ret = qtnf_cmd_send_wowlan_set(vif, wowlan);
923         if (ret) {
924                 pr_err("MAC%u: failed to set WoWLAN triggers\n",
925                        mac->macid);
926                 goto exit;
927         }
928
929 exit:
930         return ret;
931 }
932
933 static int qtnf_resume(struct wiphy *wiphy)
934 {
935         struct qtnf_wmac *mac = wiphy_priv(wiphy);
936         struct qtnf_vif *vif;
937         int ret = 0;
938
939         vif = qtnf_mac_get_base_vif(mac);
940         if (!vif) {
941                 pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
942                 ret = -EFAULT;
943                 goto exit;
944         }
945
946         ret = qtnf_cmd_send_wowlan_set(vif, NULL);
947         if (ret) {
948                 pr_err("MAC%u: failed to reset WoWLAN triggers\n",
949                        mac->macid);
950                 goto exit;
951         }
952
953 exit:
954         return ret;
955 }
956
957 static void qtnf_set_wakeup(struct wiphy *wiphy, bool enabled)
958 {
959         struct qtnf_wmac *mac = wiphy_priv(wiphy);
960         struct qtnf_bus *bus = mac->bus;
961
962         device_set_wakeup_enable(bus->dev, enabled);
963 }
964 #endif
965
966 static struct cfg80211_ops qtn_cfg80211_ops = {
967         .add_virtual_intf       = qtnf_add_virtual_intf,
968         .change_virtual_intf    = qtnf_change_virtual_intf,
969         .del_virtual_intf       = qtnf_del_virtual_intf,
970         .start_ap               = qtnf_start_ap,
971         .change_beacon          = qtnf_change_beacon,
972         .stop_ap                = qtnf_stop_ap,
973         .set_wiphy_params       = qtnf_set_wiphy_params,
974         .mgmt_frame_register    = qtnf_mgmt_frame_register,
975         .mgmt_tx                = qtnf_mgmt_tx,
976         .change_station         = qtnf_change_station,
977         .del_station            = qtnf_del_station,
978         .get_station            = qtnf_get_station,
979         .dump_station           = qtnf_dump_station,
980         .add_key                = qtnf_add_key,
981         .del_key                = qtnf_del_key,
982         .set_default_key        = qtnf_set_default_key,
983         .set_default_mgmt_key   = qtnf_set_default_mgmt_key,
984         .scan                   = qtnf_scan,
985         .connect                = qtnf_connect,
986         .external_auth          = qtnf_external_auth,
987         .disconnect             = qtnf_disconnect,
988         .dump_survey            = qtnf_dump_survey,
989         .get_channel            = qtnf_get_channel,
990         .channel_switch         = qtnf_channel_switch,
991         .start_radar_detection  = qtnf_start_radar_detection,
992         .set_mac_acl            = qtnf_set_mac_acl,
993         .set_power_mgmt         = qtnf_set_power_mgmt,
994 #ifdef CONFIG_PM
995         .suspend                = qtnf_suspend,
996         .resume                 = qtnf_resume,
997         .set_wakeup             = qtnf_set_wakeup,
998 #endif
999 };
1000
1001 static void qtnf_cfg80211_reg_notifier(struct wiphy *wiphy,
1002                                        struct regulatory_request *req)
1003 {
1004         struct qtnf_wmac *mac = wiphy_priv(wiphy);
1005         enum nl80211_band band;
1006         int ret;
1007
1008         pr_debug("MAC%u: initiator=%d alpha=%c%c\n", mac->macid, req->initiator,
1009                  req->alpha2[0], req->alpha2[1]);
1010
1011         ret = qtnf_cmd_reg_notify(mac, req, qtnf_mac_slave_radar_get(wiphy));
1012         if (ret) {
1013                 pr_err("MAC%u: failed to update region to %c%c: %d\n",
1014                        mac->macid, req->alpha2[0], req->alpha2[1], ret);
1015                 return;
1016         }
1017
1018         for (band = 0; band < NUM_NL80211_BANDS; ++band) {
1019                 if (!wiphy->bands[band])
1020                         continue;
1021
1022                 ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
1023                 if (ret)
1024                         pr_err("MAC%u: failed to update band %u\n",
1025                                mac->macid, band);
1026         }
1027 }
1028
1029 struct wiphy *qtnf_wiphy_allocate(struct qtnf_bus *bus)
1030 {
1031         struct wiphy *wiphy;
1032
1033         if (bus->hw_info.hw_capab & QLINK_HW_CAPAB_DFS_OFFLOAD)
1034                 qtn_cfg80211_ops.start_radar_detection = NULL;
1035
1036         if (!(bus->hw_info.hw_capab & QLINK_HW_CAPAB_PWR_MGMT))
1037                 qtn_cfg80211_ops.set_power_mgmt = NULL;
1038
1039         wiphy = wiphy_new(&qtn_cfg80211_ops, sizeof(struct qtnf_wmac));
1040         if (!wiphy)
1041                 return NULL;
1042
1043         set_wiphy_dev(wiphy, bus->dev);
1044
1045         return wiphy;
1046 }
1047
1048 static int
1049 qtnf_wiphy_setup_if_comb(struct wiphy *wiphy, struct qtnf_mac_info *mac_info)
1050 {
1051         struct ieee80211_iface_combination *if_comb;
1052         size_t n_if_comb;
1053         u16 interface_modes = 0;
1054         size_t i, j;
1055
1056         if_comb = mac_info->if_comb;
1057         n_if_comb = mac_info->n_if_comb;
1058
1059         if (!if_comb || !n_if_comb)
1060                 return -ENOENT;
1061
1062         for (i = 0; i < n_if_comb; i++) {
1063                 if_comb[i].radar_detect_widths = mac_info->radar_detect_widths;
1064
1065                 for (j = 0; j < if_comb[i].n_limits; j++)
1066                         interface_modes |= if_comb[i].limits[j].types;
1067         }
1068
1069         wiphy->iface_combinations = if_comb;
1070         wiphy->n_iface_combinations = n_if_comb;
1071         wiphy->interface_modes = interface_modes;
1072
1073         return 0;
1074 }
1075
1076 int qtnf_wiphy_register(struct qtnf_hw_info *hw_info, struct qtnf_wmac *mac)
1077 {
1078         struct wiphy *wiphy = priv_to_wiphy(mac);
1079         struct qtnf_mac_info *macinfo = &mac->macinfo;
1080         int ret;
1081         bool regdomain_is_known;
1082
1083         if (!wiphy) {
1084                 pr_err("invalid wiphy pointer\n");
1085                 return -EFAULT;
1086         }
1087
1088         wiphy->frag_threshold = macinfo->frag_thr;
1089         wiphy->rts_threshold = macinfo->rts_thr;
1090         wiphy->retry_short = macinfo->sretry_limit;
1091         wiphy->retry_long = macinfo->lretry_limit;
1092         wiphy->coverage_class = macinfo->coverage_class;
1093
1094         wiphy->max_scan_ssids =
1095                 (hw_info->max_scan_ssids) ? hw_info->max_scan_ssids : 1;
1096         wiphy->max_scan_ie_len = QTNF_MAX_VSIE_LEN;
1097         wiphy->mgmt_stypes = qtnf_mgmt_stypes;
1098         wiphy->max_remain_on_channel_duration = 5000;
1099         wiphy->max_acl_mac_addrs = macinfo->max_acl_mac_addrs;
1100         wiphy->max_num_csa_counters = 2;
1101
1102         ret = qtnf_wiphy_setup_if_comb(wiphy, macinfo);
1103         if (ret)
1104                 goto out;
1105
1106         /* Initialize cipher suits */
1107         wiphy->cipher_suites = qtnf_cipher_suites;
1108         wiphy->n_cipher_suites = ARRAY_SIZE(qtnf_cipher_suites);
1109         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1110         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
1111                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
1112                         WIPHY_FLAG_AP_UAPSD |
1113                         WIPHY_FLAG_HAS_CHANNEL_SWITCH |
1114                         WIPHY_FLAG_4ADDR_STATION |
1115                         WIPHY_FLAG_NETNS_OK;
1116         wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
1117
1118         if (hw_info->hw_capab & QLINK_HW_CAPAB_DFS_OFFLOAD)
1119                 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD);
1120
1121         if (hw_info->hw_capab & QLINK_HW_CAPAB_SCAN_DWELL)
1122                 wiphy_ext_feature_set(wiphy,
1123                                       NL80211_EXT_FEATURE_SET_SCAN_DWELL);
1124
1125         wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
1126                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2;
1127
1128         wiphy->available_antennas_tx = macinfo->num_tx_chain;
1129         wiphy->available_antennas_rx = macinfo->num_rx_chain;
1130
1131         wiphy->max_ap_assoc_sta = macinfo->max_ap_assoc_sta;
1132         wiphy->ht_capa_mod_mask = &macinfo->ht_cap_mod_mask;
1133         wiphy->vht_capa_mod_mask = &macinfo->vht_cap_mod_mask;
1134
1135         ether_addr_copy(wiphy->perm_addr, mac->macaddr);
1136
1137         if (hw_info->hw_capab & QLINK_HW_CAPAB_STA_INACT_TIMEOUT)
1138                 wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER;
1139
1140         if (hw_info->hw_capab & QLINK_HW_CAPAB_SCAN_RANDOM_MAC_ADDR)
1141                 wiphy->features |= NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
1142
1143         if (!(hw_info->hw_capab & QLINK_HW_CAPAB_OBSS_SCAN))
1144                 wiphy->features |= NL80211_FEATURE_NEED_OBSS_SCAN;
1145
1146         if (hw_info->hw_capab & QLINK_HW_CAPAB_SAE)
1147                 wiphy->features |= NL80211_FEATURE_SAE;
1148
1149 #ifdef CONFIG_PM
1150         if (macinfo->wowlan)
1151                 wiphy->wowlan = macinfo->wowlan;
1152 #endif
1153
1154         regdomain_is_known = isalpha(mac->rd->alpha2[0]) &&
1155                                 isalpha(mac->rd->alpha2[1]);
1156
1157         if (hw_info->hw_capab & QLINK_HW_CAPAB_REG_UPDATE) {
1158                 wiphy->reg_notifier = qtnf_cfg80211_reg_notifier;
1159
1160                 if (mac->rd->alpha2[0] == '9' && mac->rd->alpha2[1] == '9') {
1161                         wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
1162                                 REGULATORY_STRICT_REG;
1163                         wiphy_apply_custom_regulatory(wiphy, mac->rd);
1164                 } else if (regdomain_is_known) {
1165                         wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
1166                 }
1167         } else {
1168                 wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
1169         }
1170
1171         if (mac->macinfo.extended_capabilities_len) {
1172                 wiphy->extended_capabilities =
1173                         mac->macinfo.extended_capabilities;
1174                 wiphy->extended_capabilities_mask =
1175                         mac->macinfo.extended_capabilities_mask;
1176                 wiphy->extended_capabilities_len =
1177                         mac->macinfo.extended_capabilities_len;
1178         }
1179
1180         strlcpy(wiphy->fw_version, hw_info->fw_version,
1181                 sizeof(wiphy->fw_version));
1182         wiphy->hw_version = hw_info->hw_version;
1183
1184         ret = wiphy_register(wiphy);
1185         if (ret < 0)
1186                 goto out;
1187
1188         if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1189                 ret = regulatory_set_wiphy_regd(wiphy, mac->rd);
1190         else if (regdomain_is_known)
1191                 ret = regulatory_hint(wiphy, mac->rd->alpha2);
1192
1193 out:
1194         return ret;
1195 }
1196
1197 void qtnf_netdev_updown(struct net_device *ndev, bool up)
1198 {
1199         struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
1200
1201         if (qtnf_cmd_send_updown_intf(vif, up))
1202                 pr_err("failed to send %s command to VIF%u.%u\n",
1203                        up ? "UP" : "DOWN", vif->mac->macid, vif->vifid);
1204 }
1205
1206 void qtnf_virtual_intf_cleanup(struct net_device *ndev)
1207 {
1208         struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
1209         struct qtnf_wmac *mac = wiphy_priv(vif->wdev.wiphy);
1210
1211         if (vif->wdev.iftype == NL80211_IFTYPE_STATION)
1212                 qtnf_disconnect(vif->wdev.wiphy, ndev,
1213                                 WLAN_REASON_DEAUTH_LEAVING);
1214
1215         qtnf_scan_done(mac, true);
1216 }
1217
1218 void qtnf_cfg80211_vif_reset(struct qtnf_vif *vif)
1219 {
1220         if (vif->wdev.iftype == NL80211_IFTYPE_STATION)
1221                 cfg80211_disconnected(vif->netdev, WLAN_REASON_DEAUTH_LEAVING,
1222                                       NULL, 0, 1, GFP_KERNEL);
1223
1224         cfg80211_shutdown_all_interfaces(vif->wdev.wiphy);
1225 }
1226
1227 void qtnf_band_init_rates(struct ieee80211_supported_band *band)
1228 {
1229         switch (band->band) {
1230         case NL80211_BAND_2GHZ:
1231                 band->bitrates = qtnf_rates_2g;
1232                 band->n_bitrates = ARRAY_SIZE(qtnf_rates_2g);
1233                 break;
1234         case NL80211_BAND_5GHZ:
1235                 band->bitrates = qtnf_rates_5g;
1236                 band->n_bitrates = ARRAY_SIZE(qtnf_rates_5g);
1237                 break;
1238         default:
1239                 band->bitrates = NULL;
1240                 band->n_bitrates = 0;
1241                 break;
1242         }
1243 }