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