Merge tag 'actions-arm-dt-for-4.15' of ssh://gitolite.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.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                 pr_err("VIF%u.%u: failed to delete key: idx=%u pw=%u\n",
514                        vif->mac->macid, vif->vifid, key_index, pairwise);
515
516         return ret;
517 }
518
519 static int qtnf_set_default_key(struct wiphy *wiphy, struct net_device *dev,
520                                 u8 key_index, bool unicast, bool multicast)
521 {
522         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
523         int ret;
524
525         ret = qtnf_cmd_send_set_default_key(vif, key_index, unicast, multicast);
526         if (ret)
527                 pr_err("VIF%u.%u: failed to set dflt key: idx=%u uc=%u mc=%u\n",
528                        vif->mac->macid, vif->vifid, key_index, unicast,
529                        multicast);
530
531         return ret;
532 }
533
534 static int
535 qtnf_set_default_mgmt_key(struct wiphy *wiphy, struct net_device *dev,
536                           u8 key_index)
537 {
538         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
539         int ret;
540
541         ret = qtnf_cmd_send_set_default_mgmt_key(vif, key_index);
542         if (ret)
543                 pr_err("VIF%u.%u: failed to set default MGMT key: idx=%u\n",
544                        vif->mac->macid, vif->vifid, key_index);
545
546         return ret;
547 }
548
549 static int
550 qtnf_change_station(struct wiphy *wiphy, struct net_device *dev,
551                     const u8 *mac, struct station_parameters *params)
552 {
553         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
554         int ret;
555
556         ret = qtnf_cmd_send_change_sta(vif, mac, params);
557         if (ret)
558                 pr_err("VIF%u.%u: failed to change STA %pM\n",
559                        vif->mac->macid, vif->vifid, mac);
560
561         return ret;
562 }
563
564 static int
565 qtnf_del_station(struct wiphy *wiphy, struct net_device *dev,
566                  struct station_del_parameters *params)
567 {
568         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
569         int ret;
570
571         if (params->mac &&
572             (vif->wdev.iftype == NL80211_IFTYPE_AP) &&
573             !is_broadcast_ether_addr(params->mac) &&
574             !qtnf_sta_list_lookup(&vif->sta_list, params->mac))
575                 return 0;
576
577         ret = qtnf_cmd_send_del_sta(vif, params);
578         if (ret)
579                 pr_err("VIF%u.%u: failed to delete STA %pM\n",
580                        vif->mac->macid, vif->vifid, params->mac);
581         return ret;
582 }
583
584 static void qtnf_scan_timeout(unsigned long data)
585 {
586         struct qtnf_wmac *mac = (struct qtnf_wmac *)data;
587
588         pr_warn("mac%d scan timed out\n", mac->macid);
589         qtnf_scan_done(mac, true);
590 }
591
592 static int
593 qtnf_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
594 {
595         struct qtnf_wmac *mac = wiphy_priv(wiphy);
596
597         mac->scan_req = request;
598
599         if (qtnf_cmd_send_scan(mac)) {
600                 pr_err("MAC%u: failed to start scan\n", mac->macid);
601                 mac->scan_req = NULL;
602                 return -EFAULT;
603         }
604
605         mac->scan_timeout.data = (unsigned long)mac;
606         mac->scan_timeout.function = qtnf_scan_timeout;
607         mod_timer(&mac->scan_timeout,
608                   jiffies + QTNF_SCAN_TIMEOUT_SEC * HZ);
609
610         return 0;
611 }
612
613 static int
614 qtnf_connect(struct wiphy *wiphy, struct net_device *dev,
615              struct cfg80211_connect_params *sme)
616 {
617         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
618         struct qtnf_wmac *mac = wiphy_priv(wiphy);
619         struct cfg80211_chan_def chandef;
620         struct qtnf_bss_config *bss_cfg;
621         int ret;
622
623         if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
624                 return -EOPNOTSUPP;
625
626         if (vif->sta_state != QTNF_STA_DISCONNECTED)
627                 return -EBUSY;
628
629         bss_cfg = &vif->bss_cfg;
630         memset(bss_cfg, 0, sizeof(*bss_cfg));
631
632         if (sme->channel) {
633                 /* FIXME: need to set proper nl80211_channel_type value */
634                 cfg80211_chandef_create(&chandef, sme->channel,
635                                         NL80211_CHAN_HT20);
636                 /* fall-back to minimal safe chandef description */
637                 if (!cfg80211_chandef_valid(&chandef))
638                         cfg80211_chandef_create(&chandef, sme->channel,
639                                                 NL80211_CHAN_HT20);
640
641                 memcpy(&mac->chandef, &chandef, sizeof(mac->chandef));
642         }
643
644         bss_cfg->ssid_len = sme->ssid_len;
645         memcpy(&bss_cfg->ssid, sme->ssid, bss_cfg->ssid_len);
646         bss_cfg->auth_type = sme->auth_type;
647         bss_cfg->privacy = sme->privacy;
648         bss_cfg->mfp = sme->mfp;
649
650         if ((sme->bg_scan_period > 0) &&
651             (sme->bg_scan_period <= QTNF_MAX_BG_SCAN_PERIOD))
652                 bss_cfg->bg_scan_period = sme->bg_scan_period;
653         else if (sme->bg_scan_period == -1)
654                 bss_cfg->bg_scan_period = QTNF_DEFAULT_BG_SCAN_PERIOD;
655         else
656                 bss_cfg->bg_scan_period = 0; /* disabled */
657
658         bss_cfg->connect_flags = 0;
659
660         if (sme->flags & ASSOC_REQ_DISABLE_HT)
661                 bss_cfg->connect_flags |= QLINK_STA_CONNECT_DISABLE_HT;
662         if (sme->flags & ASSOC_REQ_DISABLE_VHT)
663                 bss_cfg->connect_flags |= QLINK_STA_CONNECT_DISABLE_VHT;
664         if (sme->flags & ASSOC_REQ_USE_RRM)
665                 bss_cfg->connect_flags |= QLINK_STA_CONNECT_USE_RRM;
666
667         memcpy(&bss_cfg->crypto, &sme->crypto, sizeof(bss_cfg->crypto));
668         if (sme->bssid)
669                 ether_addr_copy(bss_cfg->bssid, sme->bssid);
670         else
671                 eth_zero_addr(bss_cfg->bssid);
672
673         ret = qtnf_cmd_send_connect(vif, sme);
674         if (ret) {
675                 pr_err("VIF%u.%u: failed to connect\n", vif->mac->macid,
676                        vif->vifid);
677                 return ret;
678         }
679
680         vif->sta_state = QTNF_STA_CONNECTING;
681         return 0;
682 }
683
684 static int
685 qtnf_disconnect(struct wiphy *wiphy, struct net_device *dev,
686                 u16 reason_code)
687 {
688         struct qtnf_wmac *mac = wiphy_priv(wiphy);
689         struct qtnf_vif *vif;
690         int ret;
691
692         vif = qtnf_mac_get_base_vif(mac);
693         if (!vif) {
694                 pr_err("MAC%u: primary VIF is not configured\n", mac->macid);
695                 return -EFAULT;
696         }
697
698         if (vif->wdev.iftype != NL80211_IFTYPE_STATION)
699                 return -EOPNOTSUPP;
700
701         if (vif->sta_state == QTNF_STA_DISCONNECTED)
702                 return 0;
703
704         ret = qtnf_cmd_send_disconnect(vif, reason_code);
705         if (ret) {
706                 pr_err("VIF%u.%u: failed to disconnect\n", mac->macid,
707                        vif->vifid);
708                 return ret;
709         }
710
711         vif->sta_state = QTNF_STA_DISCONNECTED;
712         return 0;
713 }
714
715 static int
716 qtnf_dump_survey(struct wiphy *wiphy, struct net_device *dev,
717                  int idx, struct survey_info *survey)
718 {
719         struct qtnf_wmac *mac = wiphy_priv(wiphy);
720         struct ieee80211_supported_band *sband;
721         struct cfg80211_chan_def *chandef;
722         struct ieee80211_channel *chan;
723         struct qtnf_chan_stats stats;
724         struct qtnf_vif *vif;
725         int ret;
726
727         vif = qtnf_netdev_get_priv(dev);
728         chandef = &mac->chandef;
729
730         sband = wiphy->bands[NL80211_BAND_2GHZ];
731         if (sband && idx >= sband->n_channels) {
732                 idx -= sband->n_channels;
733                 sband = NULL;
734         }
735
736         if (!sband)
737                 sband = wiphy->bands[NL80211_BAND_5GHZ];
738
739         if (!sband || idx >= sband->n_channels)
740                 return -ENOENT;
741
742         chan = &sband->channels[idx];
743         memset(&stats, 0, sizeof(stats));
744
745         survey->channel = chan;
746         survey->filled = 0x0;
747
748         if (chandef->chan) {
749                 if (chan->hw_value == chandef->chan->hw_value)
750                         survey->filled = SURVEY_INFO_IN_USE;
751         }
752
753         ret = qtnf_cmd_get_chan_stats(mac, chan->hw_value, &stats);
754         switch (ret) {
755         case 0:
756                 if (unlikely(stats.chan_num != chan->hw_value)) {
757                         pr_err("received stats for channel %d instead of %d\n",
758                                stats.chan_num, chan->hw_value);
759                         ret = -EINVAL;
760                         break;
761                 }
762
763                 survey->filled |= SURVEY_INFO_TIME |
764                                  SURVEY_INFO_TIME_SCAN |
765                                  SURVEY_INFO_TIME_BUSY |
766                                  SURVEY_INFO_TIME_RX |
767                                  SURVEY_INFO_TIME_TX |
768                                  SURVEY_INFO_NOISE_DBM;
769
770                 survey->time_scan = stats.cca_try;
771                 survey->time = stats.cca_try;
772                 survey->time_tx = stats.cca_tx;
773                 survey->time_rx = stats.cca_rx;
774                 survey->time_busy = stats.cca_busy;
775                 survey->noise = stats.chan_noise;
776                 break;
777         case -ENOENT:
778                 pr_debug("no stats for channel %u\n", chan->hw_value);
779                 ret = 0;
780                 break;
781         default:
782                 pr_debug("failed to get chan(%d) stats from card\n",
783                          chan->hw_value);
784                 ret = -EINVAL;
785                 break;
786         }
787
788         return ret;
789 }
790
791 static int
792 qtnf_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
793                  struct cfg80211_chan_def *chandef)
794 {
795         struct qtnf_wmac *mac = wiphy_priv(wiphy);
796         struct net_device *ndev = wdev->netdev;
797         struct qtnf_vif *vif;
798
799         if (!ndev)
800                 return -ENODEV;
801
802         vif = qtnf_netdev_get_priv(wdev->netdev);
803
804         switch (vif->wdev.iftype) {
805         case NL80211_IFTYPE_STATION:
806                 if (vif->sta_state == QTNF_STA_DISCONNECTED) {
807                         pr_warn("%s: STA disconnected\n", ndev->name);
808                         return -ENODATA;
809                 }
810                 break;
811         case NL80211_IFTYPE_AP:
812                 if (!(vif->bss_status & QTNF_STATE_AP_START)) {
813                         pr_warn("%s: AP not started\n", ndev->name);
814                         return -ENODATA;
815                 }
816                 break;
817         default:
818                 pr_err("unsupported vif type (%d)\n", vif->wdev.iftype);
819                 return -ENODATA;
820         }
821
822         if (!cfg80211_chandef_valid(&mac->chandef)) {
823                 pr_err("invalid channel settings on %s\n", ndev->name);
824                 return -ENODATA;
825         }
826
827         memcpy(chandef, &mac->chandef, sizeof(*chandef));
828         return 0;
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_wmac *mac = wiphy_priv(wiphy);
835         struct qtnf_vif *vif = qtnf_netdev_get_priv(dev);
836         int ret;
837
838         pr_debug("%s: chan(%u) count(%u) radar(%u) block_tx(%u)\n", dev->name,
839                  params->chandef.chan->hw_value, params->count,
840                  params->radar_required, params->block_tx);
841
842         switch (vif->wdev.iftype) {
843         case NL80211_IFTYPE_AP:
844                 if (!(vif->bss_status & QTNF_STATE_AP_START)) {
845                         pr_warn("AP not started on %s\n", dev->name);
846                         return -ENOTCONN;
847                 }
848                 break;
849         default:
850                 pr_err("unsupported vif type (%d) on %s\n",
851                        vif->wdev.iftype, dev->name);
852                 return -EOPNOTSUPP;
853         }
854
855         if (vif->vifid != 0) {
856                 if (!(mac->status & QTNF_MAC_CSA_ACTIVE))
857                         return -EOPNOTSUPP;
858
859                 if (!cfg80211_chandef_identical(&params->chandef,
860                                                 &mac->csa_chandef))
861                         return -EINVAL;
862
863                 return 0;
864         }
865
866         if (!cfg80211_chandef_valid(&params->chandef)) {
867                 pr_err("%s: invalid channel\n", dev->name);
868                 return -EINVAL;
869         }
870
871         if (cfg80211_chandef_identical(&params->chandef, &mac->chandef)) {
872                 pr_err("%s: switch request to the same channel\n", dev->name);
873                 return -EALREADY;
874         }
875
876         ret = qtnf_cmd_send_chan_switch(mac, params);
877         if (ret)
878                 pr_warn("%s: failed to switch to channel (%u)\n",
879                         dev->name, params->chandef.chan->hw_value);
880
881         return ret;
882 }
883
884 static struct cfg80211_ops qtn_cfg80211_ops = {
885         .add_virtual_intf       = qtnf_add_virtual_intf,
886         .change_virtual_intf    = qtnf_change_virtual_intf,
887         .del_virtual_intf       = qtnf_del_virtual_intf,
888         .start_ap               = qtnf_start_ap,
889         .change_beacon          = qtnf_change_beacon,
890         .stop_ap                = qtnf_stop_ap,
891         .set_wiphy_params       = qtnf_set_wiphy_params,
892         .mgmt_frame_register    = qtnf_mgmt_frame_register,
893         .mgmt_tx                = qtnf_mgmt_tx,
894         .change_station         = qtnf_change_station,
895         .del_station            = qtnf_del_station,
896         .get_station            = qtnf_get_station,
897         .dump_station           = qtnf_dump_station,
898         .add_key                = qtnf_add_key,
899         .del_key                = qtnf_del_key,
900         .set_default_key        = qtnf_set_default_key,
901         .set_default_mgmt_key   = qtnf_set_default_mgmt_key,
902         .scan                   = qtnf_scan,
903         .connect                = qtnf_connect,
904         .disconnect             = qtnf_disconnect,
905         .dump_survey            = qtnf_dump_survey,
906         .get_channel            = qtnf_get_channel,
907         .channel_switch         = qtnf_channel_switch
908 };
909
910 static void qtnf_cfg80211_reg_notifier(struct wiphy *wiphy_in,
911                                        struct regulatory_request *req)
912 {
913         struct qtnf_wmac *mac = wiphy_priv(wiphy_in);
914         struct qtnf_bus *bus = mac->bus;
915         struct wiphy *wiphy;
916         unsigned int mac_idx;
917         enum nl80211_band band;
918         int ret;
919
920         pr_debug("MAC%u: initiator=%d alpha=%c%c\n", mac->macid, req->initiator,
921                  req->alpha2[0], req->alpha2[1]);
922
923         ret = qtnf_cmd_reg_notify(bus, req);
924         if (ret) {
925                 if (ret != -EOPNOTSUPP && ret != -EALREADY)
926                         pr_err("failed to update reg domain to %c%c\n",
927                                req->alpha2[0], req->alpha2[1]);
928                 return;
929         }
930
931         for (mac_idx = 0; mac_idx < QTNF_MAX_MAC; ++mac_idx) {
932                 if (!(bus->hw_info.mac_bitmap & (1 << mac_idx)))
933                         continue;
934
935                 mac = bus->mac[mac_idx];
936                 wiphy = priv_to_wiphy(mac);
937
938                 for (band = 0; band < NUM_NL80211_BANDS; ++band) {
939                         if (!wiphy->bands[band])
940                                 continue;
941
942                         ret = qtnf_cmd_get_mac_chan_info(mac,
943                                                          wiphy->bands[band]);
944                         if (ret)
945                                 pr_err("failed to get chan info for mac %u band %u\n",
946                                        mac_idx, band);
947                 }
948         }
949 }
950
951 void qtnf_band_setup_htvht_caps(struct qtnf_mac_info *macinfo,
952                                 struct ieee80211_supported_band *band)
953 {
954         struct ieee80211_sta_ht_cap *ht_cap;
955         struct ieee80211_sta_vht_cap *vht_cap;
956
957         ht_cap = &band->ht_cap;
958         ht_cap->ht_supported = true;
959         memcpy(&ht_cap->cap, &macinfo->ht_cap.cap_info,
960                sizeof(u16));
961         ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
962         ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
963         memcpy(&ht_cap->mcs, &macinfo->ht_cap.mcs,
964                sizeof(ht_cap->mcs));
965
966         if (macinfo->phymode_cap & QLINK_PHYMODE_AC) {
967                 vht_cap = &band->vht_cap;
968                 vht_cap->vht_supported = true;
969                 memcpy(&vht_cap->cap,
970                        &macinfo->vht_cap.vht_cap_info, sizeof(u32));
971                 /* Update MCS support for VHT */
972                 memcpy(&vht_cap->vht_mcs,
973                        &macinfo->vht_cap.supp_mcs,
974                        sizeof(struct ieee80211_vht_mcs_info));
975         }
976 }
977
978 struct wiphy *qtnf_wiphy_allocate(struct qtnf_bus *bus)
979 {
980         struct wiphy *wiphy;
981
982         wiphy = wiphy_new(&qtn_cfg80211_ops, sizeof(struct qtnf_wmac));
983         if (!wiphy)
984                 return NULL;
985
986         set_wiphy_dev(wiphy, bus->dev);
987
988         return wiphy;
989 }
990
991 static int qtnf_wiphy_setup_if_comb(struct wiphy *wiphy,
992                                     struct ieee80211_iface_combination *if_comb,
993                                     const struct qtnf_mac_info *mac_info)
994 {
995         size_t max_interfaces = 0;
996         u16 interface_modes = 0;
997         size_t i;
998
999         if (unlikely(!mac_info->limits || !mac_info->n_limits))
1000                 return -ENOENT;
1001
1002         if_comb->limits = mac_info->limits;
1003         if_comb->n_limits = mac_info->n_limits;
1004
1005         for (i = 0; i < mac_info->n_limits; i++) {
1006                 max_interfaces += mac_info->limits[i].max;
1007                 interface_modes |= mac_info->limits[i].types;
1008         }
1009
1010         if_comb->num_different_channels = 1;
1011         if_comb->beacon_int_infra_match = true;
1012         if_comb->max_interfaces = max_interfaces;
1013         if_comb->radar_detect_widths = mac_info->radar_detect_widths;
1014         wiphy->interface_modes = interface_modes;
1015
1016         return 0;
1017 }
1018
1019 int qtnf_wiphy_register(struct qtnf_hw_info *hw_info, struct qtnf_wmac *mac)
1020 {
1021         struct wiphy *wiphy = priv_to_wiphy(mac);
1022         struct ieee80211_iface_combination *iface_comb = NULL;
1023         int ret;
1024
1025         if (!wiphy) {
1026                 pr_err("invalid wiphy pointer\n");
1027                 return -EFAULT;
1028         }
1029
1030         iface_comb = kzalloc(sizeof(*iface_comb), GFP_KERNEL);
1031         if (!iface_comb)
1032                 return -ENOMEM;
1033
1034         ret = qtnf_wiphy_setup_if_comb(wiphy, iface_comb, &mac->macinfo);
1035         if (ret)
1036                 goto out;
1037
1038         pr_info("MAC%u: phymode=%#x radar=%#x\n", mac->macid,
1039                 mac->macinfo.phymode_cap, mac->macinfo.radar_detect_widths);
1040
1041         wiphy->frag_threshold = mac->macinfo.frag_thr;
1042         wiphy->rts_threshold = mac->macinfo.rts_thr;
1043         wiphy->retry_short = mac->macinfo.sretry_limit;
1044         wiphy->retry_long = mac->macinfo.lretry_limit;
1045         wiphy->coverage_class = mac->macinfo.coverage_class;
1046
1047         wiphy->max_scan_ssids = QTNF_MAX_SSID_LIST_LENGTH;
1048         wiphy->max_scan_ie_len = QTNF_MAX_VSIE_LEN;
1049         wiphy->mgmt_stypes = qtnf_mgmt_stypes;
1050         wiphy->max_remain_on_channel_duration = 5000;
1051
1052         wiphy->iface_combinations = iface_comb;
1053         wiphy->n_iface_combinations = 1;
1054         wiphy->max_num_csa_counters = 2;
1055
1056         /* Initialize cipher suits */
1057         wiphy->cipher_suites = qtnf_cipher_suites;
1058         wiphy->n_cipher_suites = ARRAY_SIZE(qtnf_cipher_suites);
1059         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1060         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
1061                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
1062                         WIPHY_FLAG_AP_UAPSD |
1063                         WIPHY_FLAG_HAS_CHANNEL_SWITCH;
1064
1065         wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
1066                                     NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2;
1067
1068         wiphy->available_antennas_tx = mac->macinfo.num_tx_chain;
1069         wiphy->available_antennas_rx = mac->macinfo.num_rx_chain;
1070
1071         wiphy->max_ap_assoc_sta = mac->macinfo.max_ap_assoc_sta;
1072
1073         ether_addr_copy(wiphy->perm_addr, mac->macaddr);
1074
1075         if (hw_info->hw_capab & QLINK_HW_SUPPORTS_REG_UPDATE) {
1076                 wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
1077                         REGULATORY_CUSTOM_REG;
1078                 wiphy->reg_notifier = qtnf_cfg80211_reg_notifier;
1079                 wiphy_apply_custom_regulatory(wiphy, hw_info->rd);
1080         } else {
1081                 wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
1082         }
1083
1084         ret = wiphy_register(wiphy);
1085         if (ret < 0)
1086                 goto out;
1087
1088         if (wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED)
1089                 ret = regulatory_set_wiphy_regd(wiphy, hw_info->rd);
1090         else if (isalpha(hw_info->rd->alpha2[0]) &&
1091                  isalpha(hw_info->rd->alpha2[1]))
1092                 ret = regulatory_hint(wiphy, hw_info->rd->alpha2);
1093
1094 out:
1095         if (ret) {
1096                 kfree(iface_comb);
1097                 return ret;
1098         }
1099
1100         return 0;
1101 }
1102
1103 void qtnf_netdev_updown(struct net_device *ndev, bool up)
1104 {
1105         struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
1106
1107         if (qtnf_cmd_send_updown_intf(vif, up))
1108                 pr_err("failed to send up/down command to FW\n");
1109 }
1110
1111 void qtnf_virtual_intf_cleanup(struct net_device *ndev)
1112 {
1113         struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
1114         struct qtnf_wmac *mac = wiphy_priv(vif->wdev.wiphy);
1115
1116         if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
1117                 switch (vif->sta_state) {
1118                 case QTNF_STA_DISCONNECTED:
1119                         break;
1120                 case QTNF_STA_CONNECTING:
1121                         cfg80211_connect_result(vif->netdev,
1122                                                 vif->bss_cfg.bssid, NULL, 0,
1123                                                 NULL, 0,
1124                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
1125                                                 GFP_KERNEL);
1126                         qtnf_disconnect(vif->wdev.wiphy, ndev,
1127                                         WLAN_REASON_DEAUTH_LEAVING);
1128                         break;
1129                 case QTNF_STA_CONNECTED:
1130                         cfg80211_disconnected(vif->netdev,
1131                                               WLAN_REASON_DEAUTH_LEAVING,
1132                                               NULL, 0, 1, GFP_KERNEL);
1133                         qtnf_disconnect(vif->wdev.wiphy, ndev,
1134                                         WLAN_REASON_DEAUTH_LEAVING);
1135                         break;
1136                 }
1137
1138                 vif->sta_state = QTNF_STA_DISCONNECTED;
1139         }
1140
1141         qtnf_scan_done(mac, true);
1142 }
1143
1144 void qtnf_cfg80211_vif_reset(struct qtnf_vif *vif)
1145 {
1146         if (vif->wdev.iftype == NL80211_IFTYPE_STATION) {
1147                 switch (vif->sta_state) {
1148                 case QTNF_STA_CONNECTING:
1149                         cfg80211_connect_result(vif->netdev,
1150                                                 vif->bss_cfg.bssid, NULL, 0,
1151                                                 NULL, 0,
1152                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
1153                                                 GFP_KERNEL);
1154                         break;
1155                 case QTNF_STA_CONNECTED:
1156                         cfg80211_disconnected(vif->netdev,
1157                                               WLAN_REASON_DEAUTH_LEAVING,
1158                                               NULL, 0, 1, GFP_KERNEL);
1159                         break;
1160                 case QTNF_STA_DISCONNECTED:
1161                         break;
1162                 }
1163         }
1164
1165         cfg80211_shutdown_all_interfaces(vif->wdev.wiphy);
1166         vif->sta_state = QTNF_STA_DISCONNECTED;
1167 }
1168
1169 void qtnf_band_init_rates(struct ieee80211_supported_band *band)
1170 {
1171         switch (band->band) {
1172         case NL80211_BAND_2GHZ:
1173                 band->bitrates = qtnf_rates_2g;
1174                 band->n_bitrates = ARRAY_SIZE(qtnf_rates_2g);
1175                 break;
1176         case NL80211_BAND_5GHZ:
1177                 band->bitrates = qtnf_rates_5g;
1178                 band->n_bitrates = ARRAY_SIZE(qtnf_rates_5g);
1179                 break;
1180         default:
1181                 band->bitrates = NULL;
1182                 band->n_bitrates = 0;
1183                 break;
1184         }
1185 }