GNU Linux-libre 4.14.290-gnu1
[releases.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         },
78 };
79
80 static int
81 qtnf_change_virtual_intf(struct wiphy *wiphy,
82                          struct net_device *dev,
83                          enum nl80211_iftype type,
84                          struct vif_params *params)
85 {
86         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
87         u8 *mac_addr;
88         int ret;
89
90         if (params)
91                 mac_addr = params->macaddr;
92         else
93                 mac_addr = NULL;
94
95         qtnf_scan_done(vif->mac, true);
96
97         ret = qtnf_cmd_send_change_intf_type(vif, type, mac_addr);
98         if (ret) {
99                 pr_err("VIF%u.%u: failed to change VIF type: %d\n",
100                        vif->mac->macid, vif->vifid, ret);
101                 return ret;
102         }
103
104         vif->wdev.iftype = type;
105         return 0;
106 }
107
108 int qtnf_del_virtual_intf(struct wiphy *wiphy, struct wireless_dev *wdev)
109 {
110         struct net_device *netdev =  wdev->netdev;
111         struct qtnf_vif *vif;
112
113         if (WARN_ON(!netdev))
114                 return -EFAULT;
115
116         vif = qtnf_netdev_get_priv(wdev->netdev);
117
118         qtnf_scan_done(vif->mac, true);
119
120         if (qtnf_cmd_send_del_intf(vif))
121                 pr_err("VIF%u.%u: failed to delete VIF\n", vif->mac->macid,
122                        vif->vifid);
123
124         /* Stop data */
125         netif_tx_stop_all_queues(netdev);
126         if (netif_carrier_ok(netdev))
127                 netif_carrier_off(netdev);
128
129         if (netdev->reg_state == NETREG_REGISTERED)
130                 unregister_netdevice(netdev);
131
132         vif->netdev->ieee80211_ptr = NULL;
133         vif->netdev = NULL;
134         vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
135         eth_zero_addr(vif->mac_addr);
136
137         return 0;
138 }
139
140 static struct wireless_dev *qtnf_add_virtual_intf(struct wiphy *wiphy,
141                                                   const char *name,
142                                                   unsigned char name_assign_t,
143                                                   enum nl80211_iftype type,
144                                                   struct vif_params *params)
145 {
146         struct qtnf_wmac *mac;
147         struct qtnf_vif *vif;
148         u8 *mac_addr = NULL;
149
150         mac = wiphy_priv(wiphy);
151
152         if (!mac)
153                 return ERR_PTR(-EFAULT);
154
155         switch (type) {
156         case NL80211_IFTYPE_STATION:
157         case NL80211_IFTYPE_AP:
158                 vif = qtnf_mac_get_free_vif(mac);
159                 if (!vif) {
160                         pr_err("MAC%u: no free VIF available\n", mac->macid);
161                         return ERR_PTR(-EFAULT);
162                 }
163
164                 eth_zero_addr(vif->mac_addr);
165                 vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
166                 vif->wdev.wiphy = wiphy;
167                 vif->wdev.iftype = type;
168                 vif->sta_state = QTNF_STA_DISCONNECTED;
169                 break;
170         default:
171                 pr_err("MAC%u: unsupported IF type %d\n", mac->macid, type);
172                 return ERR_PTR(-ENOTSUPP);
173         }
174
175         if (params)
176                 mac_addr = params->macaddr;
177
178         if (qtnf_cmd_send_add_intf(vif, type, mac_addr)) {
179                 pr_err("VIF%u.%u: failed to add VIF\n", mac->macid, vif->vifid);
180                 goto err_cmd;
181         }
182
183         if (!is_valid_ether_addr(vif->mac_addr)) {
184                 pr_err("VIF%u.%u: FW reported bad MAC: %pM\n",
185                        mac->macid, vif->vifid, vif->mac_addr);
186                 goto err_mac;
187         }
188
189         if (qtnf_core_net_attach(mac, vif, name, name_assign_t, type)) {
190                 pr_err("VIF%u.%u: failed to attach netdev\n", mac->macid,
191                        vif->vifid);
192                 goto err_net;
193         }
194
195         vif->wdev.netdev = vif->netdev;
196         return &vif->wdev;
197
198 err_net:
199         vif->netdev = NULL;
200 err_mac:
201         qtnf_cmd_send_del_intf(vif);
202 err_cmd:
203         vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
204
205         return ERR_PTR(-EFAULT);
206 }
207
208 static int qtnf_mgmt_set_appie(struct qtnf_vif *vif,
209                                const struct cfg80211_beacon_data *info)
210 {
211         int ret = 0;
212
213         if (!info->beacon_ies || !info->beacon_ies_len) {
214                 ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_MGMT_FRAME_BEACON,
215                                                    NULL, 0);
216         } else {
217                 ret = qtnf_cmd_send_mgmt_set_appie(vif, QLINK_MGMT_FRAME_BEACON,
218                                                    info->beacon_ies,
219                                                    info->beacon_ies_len);
220         }
221
222         if (ret)
223                 goto out;
224
225         if (!info->proberesp_ies || !info->proberesp_ies_len) {
226                 ret = qtnf_cmd_send_mgmt_set_appie(vif,
227                                                    QLINK_MGMT_FRAME_PROBE_RESP,
228                                                    NULL, 0);
229         } else {
230                 ret = qtnf_cmd_send_mgmt_set_appie(vif,
231                                                    QLINK_MGMT_FRAME_PROBE_RESP,
232                                                    info->proberesp_ies,
233                                                    info->proberesp_ies_len);
234         }
235
236         if (ret)
237                 goto out;
238
239         if (!info->assocresp_ies || !info->assocresp_ies_len) {
240                 ret = qtnf_cmd_send_mgmt_set_appie(vif,
241                                                    QLINK_MGMT_FRAME_ASSOC_RESP,
242                                                    NULL, 0);
243         } else {
244                 ret = qtnf_cmd_send_mgmt_set_appie(vif,
245                                                    QLINK_MGMT_FRAME_ASSOC_RESP,
246                                                    info->assocresp_ies,
247                                                    info->assocresp_ies_len);
248         }
249
250 out:
251         return ret;
252 }
253
254 static int qtnf_change_beacon(struct wiphy *wiphy, struct net_device *dev,
255                               struct cfg80211_beacon_data *info)
256 {
257         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
258
259         if (!(vif->bss_status & QTNF_STATE_AP_START)) {
260                 pr_err("VIF%u.%u: not started\n", vif->mac->macid, vif->vifid);
261                 return -EFAULT;
262         }
263
264         return qtnf_mgmt_set_appie(vif, info);
265 }
266
267 static int qtnf_start_ap(struct wiphy *wiphy, struct net_device *dev,
268                          struct cfg80211_ap_settings *settings)
269 {
270         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
271         struct qtnf_wmac *mac = wiphy_priv(wiphy);
272         struct qtnf_bss_config *bss_cfg;
273         int ret;
274
275         if (!cfg80211_chandef_identical(&mac->chandef, &settings->chandef)) {
276                 memcpy(&mac->chandef, &settings->chandef, sizeof(mac->chandef));
277                 if (vif->vifid != 0)
278                         pr_warn("%s: unexpected chan %u (%u MHz)\n", dev->name,
279                                 settings->chandef.chan->hw_value,
280                                 settings->chandef.chan->center_freq);
281         }
282
283         bss_cfg = &vif->bss_cfg;
284         memset(bss_cfg, 0, sizeof(*bss_cfg));
285
286         bss_cfg->bcn_period = settings->beacon_interval;
287         bss_cfg->dtim = settings->dtim_period;
288         bss_cfg->auth_type = settings->auth_type;
289         bss_cfg->privacy = settings->privacy;
290
291         bss_cfg->ssid_len = settings->ssid_len;
292         memcpy(&bss_cfg->ssid, settings->ssid, bss_cfg->ssid_len);
293
294         memcpy(&bss_cfg->crypto, &settings->crypto,
295                sizeof(struct cfg80211_crypto_settings));
296
297         ret = qtnf_cmd_send_config_ap(vif);
298         if (ret) {
299                 pr_err("VIF%u.%u: failed to push config to FW\n",
300                        vif->mac->macid, vif->vifid);
301                 goto out;
302         }
303
304         if (!(vif->bss_status & QTNF_STATE_AP_CONFIG)) {
305                 pr_err("VIF%u.%u: AP config failed in FW\n", vif->mac->macid,
306                        vif->vifid);
307                 ret = -EFAULT;
308                 goto out;
309         }
310
311         ret = qtnf_mgmt_set_appie(vif, &settings->beacon);
312         if (ret) {
313                 pr_err("VIF%u.%u: failed to add IEs to beacon\n",
314                        vif->mac->macid, vif->vifid);
315                 goto out;
316         }
317
318         ret = qtnf_cmd_send_start_ap(vif);
319         if (ret) {
320                 pr_err("VIF%u.%u: failed to start AP\n", vif->mac->macid,
321                        vif->vifid);
322                 goto out;
323         }
324
325         if (!(vif->bss_status & QTNF_STATE_AP_START)) {
326                 pr_err("VIF%u.%u: FW failed to start AP operation\n",
327                        vif->mac->macid, vif->vifid);
328                 ret = -EFAULT;
329         }
330
331 out:
332         return ret;
333 }
334
335 static int qtnf_stop_ap(struct wiphy *wiphy, struct net_device *dev)
336 {
337         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
338         int ret;
339
340         qtnf_scan_done(vif->mac, true);
341
342         ret = qtnf_cmd_send_stop_ap(vif);
343         if (ret) {
344                 pr_err("VIF%u.%u: failed to stop AP operation in FW\n",
345                        vif->mac->macid, vif->vifid);
346                 vif->bss_status &= ~QTNF_STATE_AP_START;
347                 vif->bss_status &= ~QTNF_STATE_AP_CONFIG;
348
349                 netif_carrier_off(vif->netdev);
350         }
351
352         return ret;
353 }
354
355 static int qtnf_set_wiphy_params(struct wiphy *wiphy, u32 changed)
356 {
357         struct qtnf_wmac *mac = wiphy_priv(wiphy);
358         struct qtnf_vif *vif;
359         int ret;
360
361         vif = qtnf_mac_get_base_vif(mac);
362         if (!vif) {
363                 pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
364                 return -EFAULT;
365         }
366
367         if (changed & (WIPHY_PARAM_RETRY_LONG | WIPHY_PARAM_RETRY_SHORT)) {
368                 pr_err("MAC%u: can't modify retry params\n", mac->macid);
369                 return -EOPNOTSUPP;
370         }
371
372         ret = qtnf_cmd_send_update_phy_params(mac, changed);
373         if (ret)
374                 pr_err("MAC%u: failed to update PHY params\n", mac->macid);
375
376         return ret;
377 }
378
379 static void
380 qtnf_mgmt_frame_register(struct wiphy *wiphy, struct wireless_dev *wdev,
381                          u16 frame_type, bool reg)
382 {
383         struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
384         u16 mgmt_type;
385         u16 new_mask;
386         u16 qlink_frame_type = 0;
387
388         mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
389
390         if (reg)
391                 new_mask = vif->mgmt_frames_bitmask | BIT(mgmt_type);
392         else
393                 new_mask = vif->mgmt_frames_bitmask & ~BIT(mgmt_type);
394
395         if (new_mask == vif->mgmt_frames_bitmask)
396                 return;
397
398         switch (frame_type & IEEE80211_FCTL_STYPE) {
399         case IEEE80211_STYPE_PROBE_REQ:
400                 qlink_frame_type = QLINK_MGMT_FRAME_PROBE_REQ;
401                 break;
402         case IEEE80211_STYPE_ACTION:
403                 qlink_frame_type = QLINK_MGMT_FRAME_ACTION;
404                 break;
405         default:
406                 pr_warn("VIF%u.%u: unsupported frame type: %X\n",
407                         vif->mac->macid, vif->vifid,
408                         (frame_type & IEEE80211_FCTL_STYPE) >> 4);
409                 return;
410         }
411
412         if (qtnf_cmd_send_register_mgmt(vif, qlink_frame_type, reg)) {
413                 pr_warn("VIF%u.%u: failed to %sregister mgmt frame type 0x%x\n",
414                         vif->mac->macid, vif->vifid, reg ? "" : "un",
415                         frame_type);
416                 return;
417         }
418
419         vif->mgmt_frames_bitmask = new_mask;
420         pr_debug("VIF%u.%u: %sregistered mgmt frame type 0x%x\n",
421                  vif->mac->macid, vif->vifid, reg ? "" : "un", frame_type);
422 }
423
424 static int
425 qtnf_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
426              struct cfg80211_mgmt_tx_params *params, u64 *cookie)
427 {
428         struct qtnf_vif *vif = qtnf_netdev_get_priv(wdev->netdev);
429         const struct ieee80211_mgmt *mgmt_frame = (void *)params->buf;
430         u32 short_cookie = prandom_u32();
431         u16 flags = 0;
432
433         *cookie = short_cookie;
434
435         if (params->offchan)
436                 flags |= QLINK_MGMT_FRAME_TX_FLAG_OFFCHAN;
437
438         if (params->no_cck)
439                 flags |= QLINK_MGMT_FRAME_TX_FLAG_NO_CCK;
440
441         if (params->dont_wait_for_ack)
442                 flags |= QLINK_MGMT_FRAME_TX_FLAG_ACK_NOWAIT;
443
444         pr_debug("%s freq:%u; FC:%.4X; DA:%pM; len:%zu; C:%.8X; FL:%.4X\n",
445                  wdev->netdev->name, params->chan->center_freq,
446                  le16_to_cpu(mgmt_frame->frame_control), mgmt_frame->da,
447                  params->len, short_cookie, flags);
448
449         return qtnf_cmd_send_mgmt_frame(vif, short_cookie, flags,
450                                         params->chan->center_freq,
451                                         params->buf, params->len);
452 }
453
454 static int
455 qtnf_get_station(struct wiphy *wiphy, struct net_device *dev,
456                  const u8 *mac, struct station_info *sinfo)
457 {
458         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
459
460         return qtnf_cmd_get_sta_info(vif, mac, sinfo);
461 }
462
463 static int
464 qtnf_dump_station(struct wiphy *wiphy, struct net_device *dev,
465                   int idx, u8 *mac, struct station_info *sinfo)
466 {
467         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
468         const struct qtnf_sta_node *sta_node;
469         int ret;
470
471         sta_node = qtnf_sta_list_lookup_index(&vif->sta_list, idx);
472
473         if (unlikely(!sta_node))
474                 return -ENOENT;
475
476         ether_addr_copy(mac, sta_node->mac_addr);
477
478         ret = qtnf_cmd_get_sta_info(vif, sta_node->mac_addr, sinfo);
479
480         if (unlikely(ret == -ENOENT)) {
481                 qtnf_sta_list_del(&vif->sta_list, mac);
482                 cfg80211_del_sta(vif->netdev, mac, GFP_KERNEL);
483                 sinfo->filled = 0;
484         }
485
486         return ret;
487 }
488
489 static int qtnf_add_key(struct wiphy *wiphy, struct net_device *dev,
490                         u8 key_index, bool pairwise, const u8 *mac_addr,
491                         struct key_params *params)
492 {
493         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
494         int ret;
495
496         ret = qtnf_cmd_send_add_key(vif, key_index, pairwise, mac_addr, params);
497         if (ret)
498                 pr_err("VIF%u.%u: failed to add key: cipher=%x idx=%u pw=%u\n",
499                        vif->mac->macid, vif->vifid, params->cipher, key_index,
500                        pairwise);
501
502         return ret;
503 }
504
505 static int qtnf_del_key(struct wiphy *wiphy, struct net_device *dev,
506                         u8 key_index, bool pairwise, const u8 *mac_addr)
507 {
508         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
509         int ret;
510
511         ret = qtnf_cmd_send_del_key(vif, key_index, pairwise, mac_addr);
512         if (ret) {
513                 if (ret == -ENOENT) {
514                         pr_debug("VIF%u.%u: key index %d out of bounds\n",
515                                  vif->mac->macid, vif->vifid, key_index);
516                 } else {
517                         pr_err("VIF%u.%u: failed to delete key: idx=%u pw=%u\n",
518                                vif->mac->macid, vif->vifid,
519                                key_index, pairwise);
520                 }
521         }
522
523         return ret;
524 }
525
526 static int qtnf_set_default_key(struct wiphy *wiphy, struct net_device *dev,
527                                 u8 key_index, bool unicast, bool multicast)
528 {
529         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
530         int ret;
531
532         ret = qtnf_cmd_send_set_default_key(vif, key_index, unicast, multicast);
533         if (ret)
534                 pr_err("VIF%u.%u: failed to set dflt key: idx=%u uc=%u mc=%u\n",
535                        vif->mac->macid, vif->vifid, key_index, unicast,
536                        multicast);
537
538         return ret;
539 }
540
541 static int
542 qtnf_set_default_mgmt_key(struct wiphy *wiphy, struct net_device *dev,
543                           u8 key_index)
544 {
545         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
546         int ret;
547
548         ret = qtnf_cmd_send_set_default_mgmt_key(vif, key_index);
549         if (ret)
550                 pr_err("VIF%u.%u: failed to set default MGMT key: idx=%u\n",
551                        vif->mac->macid, vif->vifid, key_index);
552
553         return ret;
554 }
555
556 static int
557 qtnf_change_station(struct wiphy *wiphy, struct net_device *dev,
558                     const u8 *mac, struct station_parameters *params)
559 {
560         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
561         int ret;
562
563         ret = qtnf_cmd_send_change_sta(vif, mac, params);
564         if (ret)
565                 pr_err("VIF%u.%u: failed to change STA %pM\n",
566                        vif->mac->macid, vif->vifid, mac);
567
568         return ret;
569 }
570
571 static int
572 qtnf_del_station(struct wiphy *wiphy, struct net_device *dev,
573                  struct station_del_parameters *params)
574 {
575         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
576         int ret;
577
578         if (params->mac &&
579             (vif->wdev.iftype == NL80211_IFTYPE_AP) &&
580             !is_broadcast_ether_addr(params->mac) &&
581             !qtnf_sta_list_lookup(&vif->sta_list, params->mac))
582                 return 0;
583
584         ret = qtnf_cmd_send_del_sta(vif, params);
585         if (ret)
586                 pr_err("VIF%u.%u: failed to delete STA %pM\n",
587                        vif->mac->macid, vif->vifid, params->mac);
588         return ret;
589 }
590
591 static void qtnf_scan_timeout(unsigned long data)
592 {
593         struct qtnf_wmac *mac = (struct qtnf_wmac *)data;
594
595         pr_warn("mac%d scan timed out\n", mac->macid);
596         qtnf_scan_done(mac, true);
597 }
598
599 static int
600 qtnf_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
601 {
602         struct qtnf_wmac *mac = wiphy_priv(wiphy);
603
604         mac->scan_req = request;
605
606         if (qtnf_cmd_send_scan(mac)) {
607                 pr_err("MAC%u: failed to start scan\n", mac->macid);
608                 mac->scan_req = NULL;
609                 return -EFAULT;
610         }
611
612         mac->scan_timeout.data = (unsigned long)mac;
613         mac->scan_timeout.function = qtnf_scan_timeout;
614         mod_timer(&mac->scan_timeout,
615                   jiffies + QTNF_SCAN_TIMEOUT_SEC * HZ);
616
617         return 0;
618 }
619
620 static int
621 qtnf_connect(struct wiphy *wiphy, struct net_device *dev,
622              struct cfg80211_connect_params *sme)
623 {
624         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
625         struct qtnf_wmac *mac = wiphy_priv(wiphy);
626         struct cfg80211_chan_def chandef;
627         struct qtnf_bss_config *bss_cfg;
628         int ret;
629
630         if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
631                 return -EOPNOTSUPP;
632
633         if (vif->sta_state != QTNF_STA_DISCONNECTED)
634                 return -EBUSY;
635
636         bss_cfg = &vif->bss_cfg;
637         memset(bss_cfg, 0, sizeof(*bss_cfg));
638
639         if (sme->channel) {
640                 /* FIXME: need to set proper nl80211_channel_type value */
641                 cfg80211_chandef_create(&chandef, sme->channel,
642                                         NL80211_CHAN_HT20);
643                 /* fall-back to minimal safe chandef description */
644                 if (!cfg80211_chandef_valid(&chandef))
645                         cfg80211_chandef_create(&chandef, sme->channel,
646                                                 NL80211_CHAN_HT20);
647
648                 memcpy(&mac->chandef, &chandef, sizeof(mac->chandef));
649         }
650
651         bss_cfg->ssid_len = sme->ssid_len;
652         memcpy(&bss_cfg->ssid, sme->ssid, bss_cfg->ssid_len);
653         bss_cfg->auth_type = sme->auth_type;
654         bss_cfg->privacy = sme->privacy;
655         bss_cfg->mfp = sme->mfp;
656
657         if ((sme->bg_scan_period > 0) &&
658             (sme->bg_scan_period <= QTNF_MAX_BG_SCAN_PERIOD))
659                 bss_cfg->bg_scan_period = sme->bg_scan_period;
660         else if (sme->bg_scan_period == -1)
661                 bss_cfg->bg_scan_period = QTNF_DEFAULT_BG_SCAN_PERIOD;
662         else
663                 bss_cfg->bg_scan_period = 0; /* disabled */
664
665         bss_cfg->connect_flags = 0;
666
667         if (sme->flags & ASSOC_REQ_DISABLE_HT)
668                 bss_cfg->connect_flags |= QLINK_STA_CONNECT_DISABLE_HT;
669         if (sme->flags & ASSOC_REQ_DISABLE_VHT)
670                 bss_cfg->connect_flags |= QLINK_STA_CONNECT_DISABLE_VHT;
671         if (sme->flags & ASSOC_REQ_USE_RRM)
672                 bss_cfg->connect_flags |= QLINK_STA_CONNECT_USE_RRM;
673
674         memcpy(&bss_cfg->crypto, &sme->crypto, sizeof(bss_cfg->crypto));
675         if (sme->bssid)
676                 ether_addr_copy(bss_cfg->bssid, sme->bssid);
677         else
678                 eth_zero_addr(bss_cfg->bssid);
679
680         ret = qtnf_cmd_send_connect(vif, sme);
681         if (ret) {
682                 pr_err("VIF%u.%u: failed to connect\n", vif->mac->macid,
683                        vif->vifid);
684                 return ret;
685         }
686
687         vif->sta_state = QTNF_STA_CONNECTING;
688         return 0;
689 }
690
691 static int
692 qtnf_disconnect(struct wiphy *wiphy, struct net_device *dev,
693                 u16 reason_code)
694 {
695         struct qtnf_wmac *mac = wiphy_priv(wiphy);
696         struct qtnf_vif *vif;
697         int ret;
698
699         vif = qtnf_mac_get_base_vif(mac);
700         if (!vif) {
701                 pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
702                 return -EFAULT;
703         }
704
705         if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
706                 return -EOPNOTSUPP;
707
708         if (vif->sta_state == QTNF_STA_DISCONNECTED)
709                 return 0;
710
711         ret = qtnf_cmd_send_disconnect(vif, reason_code);
712         if (ret) {
713                 pr_err("VIF%u.%u: failed to disconnect\n", mac->macid,
714                        vif->vifid);
715                 return ret;
716         }
717
718         vif->sta_state = QTNF_STA_DISCONNECTED;
719         return 0;
720 }
721
722 static int
723 qtnf_dump_survey(struct wiphy *wiphy, struct net_device *dev,
724                  int idx, struct survey_info *survey)
725 {
726         struct qtnf_wmac *mac = wiphy_priv(wiphy);
727         struct ieee80211_supported_band *sband;
728         struct cfg80211_chan_def *chandef;
729         struct ieee80211_channel *chan;
730         struct qtnf_chan_stats stats;
731         struct qtnf_vif *vif;
732         int ret;
733
734         vif = qtnf_netdev_get_priv(dev);
735         chandef = &mac->chandef;
736
737         sband = wiphy->bands[NL80211_BAND_2GHZ];
738         if (sband && idx >= sband->n_channels) {
739                 idx -= sband->n_channels;
740                 sband = NULL;
741         }
742
743         if (!sband)
744                 sband = wiphy->bands[NL80211_BAND_5GHZ];
745
746         if (!sband || idx >= sband->n_channels)
747                 return -ENOENT;
748
749         chan = &sband->channels[idx];
750         memset(&stats, 0, sizeof(stats));
751
752         survey->channel = chan;
753         survey->filled = 0x0;
754
755         if (chandef->chan) {
756                 if (chan->hw_value == chandef->chan->hw_value)
757                         survey->filled = SURVEY_INFO_IN_USE;
758         }
759
760         ret = qtnf_cmd_get_chan_stats(mac, chan->hw_value, &stats);
761         switch (ret) {
762         case 0:
763                 if (unlikely(stats.chan_num != chan->hw_value)) {
764                         pr_err("received stats for channel %d instead of %d\n",
765                                stats.chan_num, chan->hw_value);
766                         ret = -EINVAL;
767                         break;
768                 }
769
770                 survey->filled |= SURVEY_INFO_TIME |
771                                  SURVEY_INFO_TIME_SCAN |
772                                  SURVEY_INFO_TIME_BUSY |
773                                  SURVEY_INFO_TIME_RX |
774                                  SURVEY_INFO_TIME_TX |
775                                  SURVEY_INFO_NOISE_DBM;
776
777                 survey->time_scan = stats.cca_try;
778                 survey->time = stats.cca_try;
779                 survey->time_tx = stats.cca_tx;
780                 survey->time_rx = stats.cca_rx;
781                 survey->time_busy = stats.cca_busy;
782                 survey->noise = stats.chan_noise;
783                 break;
784         case -ENOENT:
785                 pr_debug("no stats for channel %u\n", chan->hw_value);
786                 ret = 0;
787                 break;
788         default:
789                 pr_debug("failed to get chan(%d) stats from card\n",
790                          chan->hw_value);
791                 ret = -EINVAL;
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 qtnf_wmac *mac = wiphy_priv(wiphy);
803         struct net_device *ndev = wdev->netdev;
804         struct qtnf_vif *vif;
805
806         if (!ndev)
807                 return -ENODEV;
808
809         vif = qtnf_netdev_get_priv(wdev->netdev);
810
811         switch (vif->wdev.iftype) {
812         case NL80211_IFTYPE_STATION:
813                 if (vif->sta_state == QTNF_STA_DISCONNECTED) {
814                         pr_warn("%s: STA disconnected\n", ndev->name);
815                         return -ENODATA;
816                 }
817                 break;
818         case NL80211_IFTYPE_AP:
819                 if (!(vif->bss_status & QTNF_STATE_AP_START)) {
820                         pr_warn("%s: AP not started\n", ndev->name);
821                         return -ENODATA;
822                 }
823                 break;
824         default:
825                 pr_err("unsupported vif type (%d)\n", vif->wdev.iftype);
826                 return -ENODATA;
827         }
828
829         if (!cfg80211_chandef_valid(&mac->chandef)) {
830                 pr_err("invalid channel settings on %s\n", ndev->name);
831                 return -ENODATA;
832         }
833
834         memcpy(chandef, &mac->chandef, sizeof(*chandef));
835         return 0;
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_wmac *mac = wiphy_priv(wiphy);
842         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
843         int ret;
844
845         pr_debug("%s: chan(%u) count(%u) radar(%u) block_tx(%u)\n", dev->name,
846                  params->chandef.chan->hw_value, params->count,
847                  params->radar_required, params->block_tx);
848
849         switch (vif->wdev.iftype) {
850         case NL80211_IFTYPE_AP:
851                 if (!(vif->bss_status & QTNF_STATE_AP_START)) {
852                         pr_warn("AP not started on %s\n", dev->name);
853                         return -ENOTCONN;
854                 }
855                 break;
856         default:
857                 pr_err("unsupported vif type (%d) on %s\n",
858                        vif->wdev.iftype, dev->name);
859                 return -EOPNOTSUPP;
860         }
861
862         if (vif->vifid != 0) {
863                 if (!(mac->status & QTNF_MAC_CSA_ACTIVE))
864                         return -EOPNOTSUPP;
865
866                 if (!cfg80211_chandef_identical(&params->chandef,
867                                                 &mac->csa_chandef))
868                         return -EINVAL;
869
870                 return 0;
871         }
872
873         if (!cfg80211_chandef_valid(&params->chandef)) {
874                 pr_err("%s: invalid channel\n", dev->name);
875                 return -EINVAL;
876         }
877
878         if (cfg80211_chandef_identical(&params->chandef, &mac->chandef)) {
879                 pr_err("%s: switch request to the same channel\n", dev->name);
880                 return -EALREADY;
881         }
882
883         ret = qtnf_cmd_send_chan_switch(mac, params);
884         if (ret)
885                 pr_warn("%s: failed to switch to channel (%u)\n",
886                         dev->name, params->chandef.chan->hw_value);
887
888         return ret;
889 }
890
891 static struct cfg80211_ops qtn_cfg80211_ops = {
892         .add_virtual_intf       = qtnf_add_virtual_intf,
893         .change_virtual_intf    = qtnf_change_virtual_intf,
894         .del_virtual_intf       = qtnf_del_virtual_intf,
895         .start_ap               = qtnf_start_ap,
896         .change_beacon          = qtnf_change_beacon,
897         .stop_ap                = qtnf_stop_ap,
898         .set_wiphy_params       = qtnf_set_wiphy_params,
899         .mgmt_frame_register    = qtnf_mgmt_frame_register,
900         .mgmt_tx                = qtnf_mgmt_tx,
901         .change_station         = qtnf_change_station,
902         .del_station            = qtnf_del_station,
903         .get_station            = qtnf_get_station,
904         .dump_station           = qtnf_dump_station,
905         .add_key                = qtnf_add_key,
906         .del_key                = qtnf_del_key,
907         .set_default_key        = qtnf_set_default_key,
908         .set_default_mgmt_key   = qtnf_set_default_mgmt_key,
909         .scan                   = qtnf_scan,
910         .connect                = qtnf_connect,
911         .disconnect             = qtnf_disconnect,
912         .dump_survey            = qtnf_dump_survey,
913         .get_channel            = qtnf_get_channel,
914         .channel_switch         = qtnf_channel_switch
915 };
916
917 static void qtnf_cfg80211_reg_notifier(struct wiphy *wiphy_in,
918                                        struct regulatory_request *req)
919 {
920         struct qtnf_wmac *mac = wiphy_priv(wiphy_in);
921         struct qtnf_bus *bus = mac->bus;
922         struct wiphy *wiphy;
923         unsigned int mac_idx;
924         enum nl80211_band band;
925         int ret;
926
927         pr_debug("MAC%u: initiator=%d alpha=%c%c\n", mac->macid, req->initiator,
928                  req->alpha2[0], req->alpha2[1]);
929
930         ret = qtnf_cmd_reg_notify(bus, req);
931         if (ret) {
932                 if (ret != -EOPNOTSUPP && ret != -EALREADY)
933                         pr_err("failed to update reg domain to %c%c\n",
934                                req->alpha2[0], req->alpha2[1]);
935                 return;
936         }
937
938         for (mac_idx = 0; mac_idx < QTNF_MAX_MAC; ++mac_idx) {
939                 if (!(bus->hw_info.mac_bitmap & (1 << mac_idx)))
940                         continue;
941
942                 mac = bus->mac[mac_idx];
943                 wiphy = priv_to_wiphy(mac);
944
945                 for (band = 0; band < NUM_NL80211_BANDS; ++band) {
946                         if (!wiphy->bands[band])
947                                 continue;
948
949                         ret = qtnf_cmd_get_mac_chan_info(mac,
950                                                          wiphy->bands[band]);
951                         if (ret)
952                                 pr_err("failed to get chan info for mac %u band %u\n",
953                                        mac_idx, band);
954                 }
955         }
956 }
957
958 void qtnf_band_setup_htvht_caps(struct qtnf_mac_info *macinfo,
959                                 struct ieee80211_supported_band *band)
960 {
961         struct ieee80211_sta_ht_cap *ht_cap;
962         struct ieee80211_sta_vht_cap *vht_cap;
963
964         ht_cap = &band->ht_cap;
965         ht_cap->ht_supported = true;
966         memcpy(&ht_cap->cap, &macinfo->ht_cap.cap_info,
967                sizeof(u16));
968         ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
969         ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
970         memcpy(&ht_cap->mcs, &macinfo->ht_cap.mcs,
971                sizeof(ht_cap->mcs));
972
973         if (macinfo->phymode_cap & QLINK_PHYMODE_AC) {
974                 vht_cap = &band->vht_cap;
975                 vht_cap->vht_supported = true;
976                 memcpy(&vht_cap->cap,
977                        &macinfo->vht_cap.vht_cap_info, sizeof(u32));
978                 /* Update MCS support for VHT */
979                 memcpy(&vht_cap->vht_mcs,
980                        &macinfo->vht_cap.supp_mcs,
981                        sizeof(struct ieee80211_vht_mcs_info));
982         }
983 }
984
985 struct wiphy *qtnf_wiphy_allocate(struct qtnf_bus *bus)
986 {
987         struct wiphy *wiphy;
988
989         wiphy = wiphy_new(&qtn_cfg80211_ops, sizeof(struct qtnf_wmac));
990         if (!wiphy)
991                 return NULL;
992
993         set_wiphy_dev(wiphy, bus->dev);
994
995         return wiphy;
996 }
997
998 static int qtnf_wiphy_setup_if_comb(struct wiphy *wiphy,
999                                     struct ieee80211_iface_combination *if_comb,
1000                                     const struct qtnf_mac_info *mac_info)
1001 {
1002         size_t max_interfaces = 0;
1003         u16 interface_modes = 0;
1004         size_t i;
1005
1006         if (unlikely(!mac_info->limits || !mac_info->n_limits))
1007                 return -ENOENT;
1008
1009         if_comb->limits = mac_info->limits;
1010         if_comb->n_limits = mac_info->n_limits;
1011
1012         for (i = 0; i < mac_info->n_limits; i++) {
1013                 max_interfaces += mac_info->limits[i].max;
1014                 interface_modes |= mac_info->limits[i].types;
1015         }
1016
1017         if_comb->num_different_channels = 1;
1018         if_comb->beacon_int_infra_match = true;
1019         if_comb->max_interfaces = max_interfaces;
1020         if_comb->radar_detect_widths = mac_info->radar_detect_widths;
1021         wiphy->interface_modes = interface_modes;
1022
1023         return 0;
1024 }
1025
1026 int qtnf_wiphy_register(struct qtnf_hw_info *hw_info, struct qtnf_wmac *mac)
1027 {
1028         struct wiphy *wiphy = priv_to_wiphy(mac);
1029         struct ieee80211_iface_combination *iface_comb = NULL;
1030         int ret;
1031
1032         if (!wiphy) {
1033                 pr_err("invalid wiphy pointer\n");
1034                 return -EFAULT;
1035         }
1036
1037         iface_comb = kzalloc(sizeof(*iface_comb), GFP_KERNEL);
1038         if (!iface_comb)
1039                 return -ENOMEM;
1040
1041         ret = qtnf_wiphy_setup_if_comb(wiphy, iface_comb, &mac->macinfo);
1042         if (ret)
1043                 goto out;
1044
1045         pr_info("MAC%u: phymode=%#x radar=%#x\n", mac->macid,
1046                 mac->macinfo.phymode_cap, mac->macinfo.radar_detect_widths);
1047
1048         wiphy->frag_threshold = mac->macinfo.frag_thr;
1049         wiphy->rts_threshold = mac->macinfo.rts_thr;
1050         wiphy->retry_short = mac->macinfo.sretry_limit;
1051         wiphy->retry_long = mac->macinfo.lretry_limit;
1052         wiphy->coverage_class = mac->macinfo.coverage_class;
1053
1054         wiphy->max_scan_ssids = QTNF_MAX_SSID_LIST_LENGTH;
1055         wiphy->max_scan_ie_len = QTNF_MAX_VSIE_LEN;
1056         wiphy->mgmt_stypes = qtnf_mgmt_stypes;
1057         wiphy->max_remain_on_channel_duration = 5000;
1058
1059         wiphy->iface_combinations = iface_comb;
1060         wiphy->n_iface_combinations = 1;
1061         wiphy->max_num_csa_counters = 2;
1062
1063         /* Initialize cipher suits */
1064         wiphy->cipher_suites = qtnf_cipher_suites;
1065         wiphy->n_cipher_suites = ARRAY_SIZE(qtnf_cipher_suites);
1066         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1067         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
1068                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
1069                         WIPHY_FLAG_AP_UAPSD |
1070                         WIPHY_FLAG_HAS_CHANNEL_SWITCH;
1071
1072         wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
1073                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2;
1074
1075         wiphy->available_antennas_tx = mac->macinfo.num_tx_chain;
1076         wiphy->available_antennas_rx = mac->macinfo.num_rx_chain;
1077
1078         wiphy->max_ap_assoc_sta = mac->macinfo.max_ap_assoc_sta;
1079
1080         ether_addr_copy(wiphy->perm_addr, mac->macaddr);
1081
1082         if (hw_info->hw_capab & QLINK_HW_SUPPORTS_REG_UPDATE) {
1083                 wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
1084                         REGULATORY_CUSTOM_REG;
1085                 wiphy->reg_notifier = qtnf_cfg80211_reg_notifier;
1086                 wiphy_apply_custom_regulatory(wiphy, hw_info->rd);
1087         } else {
1088                 wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
1089         }
1090
1091         ret = wiphy_register(wiphy);
1092         if (ret < 0)
1093                 goto out;
1094
1095         if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1096                 ret = regulatory_set_wiphy_regd(wiphy, hw_info->rd);
1097         else if (isalpha(hw_info->rd->alpha2[0]) &&
1098                  isalpha(hw_info->rd->alpha2[1]))
1099                 ret = regulatory_hint(wiphy, hw_info->rd->alpha2);
1100
1101 out:
1102         if (ret) {
1103                 kfree(iface_comb);
1104                 return ret;
1105         }
1106
1107         return 0;
1108 }
1109
1110 void qtnf_netdev_updown(struct net_device *ndev, bool up)
1111 {
1112         struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
1113
1114         if (qtnf_cmd_send_updown_intf(vif, up))
1115                 pr_err("failed to send up/down command to FW\n");
1116 }
1117
1118 void qtnf_virtual_intf_cleanup(struct net_device *ndev)
1119 {
1120         struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
1121         struct qtnf_wmac *mac = wiphy_priv(vif->wdev.wiphy);
1122
1123         if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
1124                 switch (vif->sta_state) {
1125                 case QTNF_STA_DISCONNECTED:
1126                         break;
1127                 case QTNF_STA_CONNECTING:
1128                         cfg80211_connect_result(vif->netdev,
1129                                                 vif->bss_cfg.bssid, NULL, 0,
1130                                                 NULL, 0,
1131                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
1132                                                 GFP_KERNEL);
1133                         qtnf_disconnect(vif->wdev.wiphy, ndev,
1134                                         WLAN_REASON_DEAUTH_LEAVING);
1135                         break;
1136                 case QTNF_STA_CONNECTED:
1137                         cfg80211_disconnected(vif->netdev,
1138                                               WLAN_REASON_DEAUTH_LEAVING,
1139                                               NULL, 0, 1, GFP_KERNEL);
1140                         qtnf_disconnect(vif->wdev.wiphy, ndev,
1141                                         WLAN_REASON_DEAUTH_LEAVING);
1142                         break;
1143                 }
1144
1145                 vif->sta_state = QTNF_STA_DISCONNECTED;
1146         }
1147
1148         qtnf_scan_done(mac, true);
1149 }
1150
1151 void qtnf_cfg80211_vif_reset(struct qtnf_vif *vif)
1152 {
1153         if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
1154                 switch (vif->sta_state) {
1155                 case QTNF_STA_CONNECTING:
1156                         cfg80211_connect_result(vif->netdev,
1157                                                 vif->bss_cfg.bssid, NULL, 0,
1158                                                 NULL, 0,
1159                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
1160                                                 GFP_KERNEL);
1161                         break;
1162                 case QTNF_STA_CONNECTED:
1163                         cfg80211_disconnected(vif->netdev,
1164                                               WLAN_REASON_DEAUTH_LEAVING,
1165                                               NULL, 0, 1, GFP_KERNEL);
1166                         break;
1167                 case QTNF_STA_DISCONNECTED:
1168                         break;
1169                 }
1170         }
1171
1172         cfg80211_shutdown_all_interfaces(vif->wdev.wiphy);
1173         vif->sta_state = QTNF_STA_DISCONNECTED;
1174 }
1175
1176 void qtnf_band_init_rates(struct ieee80211_supported_band *band)
1177 {
1178         switch (band->band) {
1179         case NL80211_BAND_2GHZ:
1180                 band->bitrates = qtnf_rates_2g;
1181                 band->n_bitrates = ARRAY_SIZE(qtnf_rates_2g);
1182                 break;
1183         case NL80211_BAND_5GHZ:
1184                 band->bitrates = qtnf_rates_5g;
1185                 band->n_bitrates = ARRAY_SIZE(qtnf_rates_5g);
1186                 break;
1187         default:
1188                 band->bitrates = NULL;
1189                 band->n_bitrates = 0;
1190                 break;
1191         }
1192 }