wil6210: remove unused including <linux/version.h>
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / wil6210 / cfg80211.c
1 /*
2  * Copyright (c) 2012 Qualcomm Atheros, Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/netdevice.h>
19 #include <linux/sched.h>
20 #include <linux/etherdevice.h>
21 #include <linux/wireless.h>
22 #include <linux/ieee80211.h>
23 #include <linux/slab.h>
24 #include <net/cfg80211.h>
25
26 #include "wil6210.h"
27 #include "wmi.h"
28
29 #define CHAN60G(_channel, _flags) {                             \
30         .band                   = IEEE80211_BAND_60GHZ,         \
31         .center_freq            = 56160 + (2160 * (_channel)),  \
32         .hw_value               = (_channel),                   \
33         .flags                  = (_flags),                     \
34         .max_antenna_gain       = 0,                            \
35         .max_power              = 40,                           \
36 }
37
38 static struct ieee80211_channel wil_60ghz_channels[] = {
39         CHAN60G(1, 0),
40         CHAN60G(2, 0),
41         CHAN60G(3, 0),
42 /* channel 4 not supported yet */
43 };
44
45 static struct ieee80211_supported_band wil_band_60ghz = {
46         .channels = wil_60ghz_channels,
47         .n_channels = ARRAY_SIZE(wil_60ghz_channels),
48         .ht_cap = {
49                 .ht_supported = true,
50                 .cap = 0, /* TODO */
51                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
52                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
53                 .mcs = {
54                                 /* MCS 1..12 - SC PHY */
55                         .rx_mask = {0xfe, 0x1f}, /* 1..12 */
56                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
57                 },
58         },
59 };
60
61 static const struct ieee80211_txrx_stypes
62 wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
63         [NL80211_IFTYPE_STATION] = {
64                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
65                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
66                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
67                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
68         },
69         [NL80211_IFTYPE_AP] = {
70                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
71                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
72                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
73                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
74         },
75         [NL80211_IFTYPE_P2P_CLIENT] = {
76                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
77                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
78                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
79                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
80         },
81         [NL80211_IFTYPE_P2P_GO] = {
82                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
83                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
84                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
85                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
86         },
87 };
88
89 static const u32 wil_cipher_suites[] = {
90         WLAN_CIPHER_SUITE_GCMP,
91 };
92
93 int wil_iftype_nl2wmi(enum nl80211_iftype type)
94 {
95         static const struct {
96                 enum nl80211_iftype nl;
97                 enum wmi_network_type wmi;
98         } __nl2wmi[] = {
99                 {NL80211_IFTYPE_ADHOC,          WMI_NETTYPE_ADHOC},
100                 {NL80211_IFTYPE_STATION,        WMI_NETTYPE_INFRA},
101                 {NL80211_IFTYPE_AP,             WMI_NETTYPE_AP},
102                 {NL80211_IFTYPE_P2P_CLIENT,     WMI_NETTYPE_P2P},
103                 {NL80211_IFTYPE_P2P_GO,         WMI_NETTYPE_P2P},
104                 {NL80211_IFTYPE_MONITOR,        WMI_NETTYPE_ADHOC}, /* FIXME */
105         };
106         uint i;
107
108         for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
109                 if (__nl2wmi[i].nl == type)
110                         return __nl2wmi[i].wmi;
111         }
112
113         return -EOPNOTSUPP;
114 }
115
116 static int wil_cfg80211_get_station(struct wiphy *wiphy,
117                                     struct net_device *ndev,
118                                     u8 *mac, struct station_info *sinfo)
119 {
120         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
121         int rc;
122         struct wmi_notify_req_cmd cmd = {
123                 .cid = 0,
124                 .interval_usec = 0,
125         };
126
127         if (memcmp(mac, wil->dst_addr[0], ETH_ALEN))
128                 return -ENOENT;
129
130         /* WMI_NOTIFY_REQ_DONE_EVENTID handler fills wil->stats.bf_mcs */
131         rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
132                       WMI_NOTIFY_REQ_DONE_EVENTID, NULL, 0, 20);
133         if (rc)
134                 return rc;
135
136         sinfo->generation = wil->sinfo_gen;
137
138         sinfo->filled |= STATION_INFO_TX_BITRATE;
139         sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
140         sinfo->txrate.mcs = wil->stats.bf_mcs;
141         sinfo->filled |= STATION_INFO_RX_BITRATE;
142         sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
143         sinfo->rxrate.mcs = wil->stats.last_mcs_rx;
144
145         if (test_bit(wil_status_fwconnected, &wil->status)) {
146                 sinfo->filled |= STATION_INFO_SIGNAL;
147                 sinfo->signal = 12; /* TODO: provide real value */
148         }
149
150         return 0;
151 }
152
153 static int wil_cfg80211_change_iface(struct wiphy *wiphy,
154                                      struct net_device *ndev,
155                                      enum nl80211_iftype type, u32 *flags,
156                                      struct vif_params *params)
157 {
158         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
159         struct wireless_dev *wdev = wil->wdev;
160
161         switch (type) {
162         case NL80211_IFTYPE_STATION:
163         case NL80211_IFTYPE_AP:
164         case NL80211_IFTYPE_P2P_CLIENT:
165         case NL80211_IFTYPE_P2P_GO:
166                 break;
167         case NL80211_IFTYPE_MONITOR:
168                 if (flags)
169                         wil->monitor_flags = *flags;
170                 else
171                         wil->monitor_flags = 0;
172
173                 break;
174         default:
175                 return -EOPNOTSUPP;
176         }
177
178         wdev->iftype = type;
179
180         return 0;
181 }
182
183 static int wil_cfg80211_scan(struct wiphy *wiphy,
184                              struct cfg80211_scan_request *request)
185 {
186         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
187         struct wireless_dev *wdev = wil->wdev;
188         struct {
189                 struct wmi_start_scan_cmd cmd;
190                 u16 chnl[4];
191         } __packed cmd;
192         uint i, n;
193
194         if (wil->scan_request) {
195                 wil_err(wil, "Already scanning\n");
196                 return -EAGAIN;
197         }
198
199         /* check we are client side */
200         switch (wdev->iftype) {
201         case NL80211_IFTYPE_STATION:
202         case NL80211_IFTYPE_P2P_CLIENT:
203                 break;
204         default:
205                 return -EOPNOTSUPP;
206         }
207
208         /* FW don't support scan after connection attempt */
209         if (test_bit(wil_status_dontscan, &wil->status)) {
210                 wil_err(wil, "Scan after connect attempt not supported\n");
211                 return -EBUSY;
212         }
213
214         wil->scan_request = request;
215
216         memset(&cmd, 0, sizeof(cmd));
217         cmd.cmd.num_channels = 0;
218         n = min(request->n_channels, 4U);
219         for (i = 0; i < n; i++) {
220                 int ch = request->channels[i]->hw_value;
221                 if (ch == 0) {
222                         wil_err(wil,
223                                 "Scan requested for unknown frequency %dMhz\n",
224                                 request->channels[i]->center_freq);
225                         continue;
226                 }
227                 /* 0-based channel indexes */
228                 cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
229                 wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
230                              request->channels[i]->center_freq);
231         }
232
233         return wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
234                         cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
235 }
236
237 static int wil_cfg80211_connect(struct wiphy *wiphy,
238                                 struct net_device *ndev,
239                                 struct cfg80211_connect_params *sme)
240 {
241         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
242         struct cfg80211_bss *bss;
243         struct wmi_connect_cmd conn;
244         const u8 *ssid_eid;
245         const u8 *rsn_eid;
246         int ch;
247         int rc = 0;
248
249         bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
250                                sme->ssid, sme->ssid_len,
251                                WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
252         if (!bss) {
253                 wil_err(wil, "Unable to find BSS\n");
254                 return -ENOENT;
255         }
256
257         ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
258         if (!ssid_eid) {
259                 wil_err(wil, "No SSID\n");
260                 rc = -ENOENT;
261                 goto out;
262         }
263
264         rsn_eid = sme->ie ?
265                         cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
266                         NULL;
267         if (rsn_eid) {
268                 if (sme->ie_len > WMI_MAX_IE_LEN) {
269                         rc = -ERANGE;
270                         wil_err(wil, "IE too large (%td bytes)\n",
271                                 sme->ie_len);
272                         goto out;
273                 }
274                 /*
275                  * For secure assoc, send:
276                  * (1) WMI_DELETE_CIPHER_KEY_CMD
277                  * (2) WMI_SET_APPIE_CMD
278                  */
279                 rc = wmi_del_cipher_key(wil, 0, bss->bssid);
280                 if (rc) {
281                         wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD failed\n");
282                         goto out;
283                 }
284                 /* WMI_SET_APPIE_CMD */
285                 rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
286                 if (rc) {
287                         wil_err(wil, "WMI_SET_APPIE_CMD failed\n");
288                         goto out;
289                 }
290         }
291
292         /* WMI_CONNECT_CMD */
293         memset(&conn, 0, sizeof(conn));
294         switch (bss->capability & 0x03) {
295         case WLAN_CAPABILITY_DMG_TYPE_AP:
296                 conn.network_type = WMI_NETTYPE_INFRA;
297                 break;
298         case WLAN_CAPABILITY_DMG_TYPE_PBSS:
299                 conn.network_type = WMI_NETTYPE_P2P;
300                 break;
301         default:
302                 wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
303                         bss->capability);
304                 goto out;
305         }
306         if (rsn_eid) {
307                 conn.dot11_auth_mode = WMI_AUTH11_SHARED;
308                 conn.auth_mode = WMI_AUTH_WPA2_PSK;
309                 conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
310                 conn.pairwise_crypto_len = 16;
311         } else {
312                 conn.dot11_auth_mode = WMI_AUTH11_OPEN;
313                 conn.auth_mode = WMI_AUTH_NONE;
314         }
315
316         conn.ssid_len = min_t(u8, ssid_eid[1], 32);
317         memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
318
319         ch = bss->channel->hw_value;
320         if (ch == 0) {
321                 wil_err(wil, "BSS at unknown frequency %dMhz\n",
322                         bss->channel->center_freq);
323                 rc = -EOPNOTSUPP;
324                 goto out;
325         }
326         conn.channel = ch - 1;
327
328         memcpy(conn.bssid, bss->bssid, 6);
329         memcpy(conn.dst_mac, bss->bssid, 6);
330         /*
331          * FW don't support scan after connection attempt
332          */
333         set_bit(wil_status_dontscan, &wil->status);
334
335         rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
336         if (rc == 0) {
337                 /* Connect can take lots of time */
338                 mod_timer(&wil->connect_timer,
339                           jiffies + msecs_to_jiffies(2000));
340         }
341
342  out:
343         cfg80211_put_bss(wiphy, bss);
344
345         return rc;
346 }
347
348 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
349                                    struct net_device *ndev,
350                                    u16 reason_code)
351 {
352         int rc;
353         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
354
355         rc = wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
356
357         return rc;
358 }
359
360 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
361                                     struct cfg80211_chan_def *chandef)
362 {
363         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
364         struct wireless_dev *wdev = wil->wdev;
365
366         wdev->preset_chandef = *chandef;
367
368         return 0;
369 }
370
371 static int wil_cfg80211_add_key(struct wiphy *wiphy,
372                                 struct net_device *ndev,
373                                 u8 key_index, bool pairwise,
374                                 const u8 *mac_addr,
375                                 struct key_params *params)
376 {
377         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
378
379         /* group key is not used */
380         if (!pairwise)
381                 return 0;
382
383         return wmi_add_cipher_key(wil, key_index, mac_addr,
384                                   params->key_len, params->key);
385 }
386
387 static int wil_cfg80211_del_key(struct wiphy *wiphy,
388                                 struct net_device *ndev,
389                                 u8 key_index, bool pairwise,
390                                 const u8 *mac_addr)
391 {
392         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
393
394         /* group key is not used */
395         if (!pairwise)
396                 return 0;
397
398         return wmi_del_cipher_key(wil, key_index, mac_addr);
399 }
400
401 /* Need to be present or wiphy_new() will WARN */
402 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
403                                         struct net_device *ndev,
404                                         u8 key_index, bool unicast,
405                                         bool multicast)
406 {
407         return 0;
408 }
409
410 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
411                                  struct net_device *ndev,
412                                  struct cfg80211_ap_settings *info)
413 {
414         int rc = 0;
415         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
416         struct wireless_dev *wdev = ndev->ieee80211_ptr;
417         struct ieee80211_channel *channel = info->chandef.chan;
418         struct cfg80211_beacon_data *bcon = &info->beacon;
419         u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
420
421         if (!channel) {
422                 wil_err(wil, "AP: No channel???\n");
423                 return -EINVAL;
424         }
425
426         wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
427                      channel->center_freq, info->privacy ? "secure" : "open");
428         print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
429                              info->ssid, info->ssid_len);
430
431         rc = wil_reset(wil);
432         if (rc)
433                 return rc;
434
435         rc = wmi_set_ssid(wil, info->ssid_len, info->ssid);
436         if (rc)
437                 return rc;
438
439         rc = wmi_set_channel(wil, channel->hw_value);
440         if (rc)
441                 return rc;
442
443         /* MAC address - pre-requisite for other commands */
444         wmi_set_mac_address(wil, ndev->dev_addr);
445
446         /* IE's */
447         /* bcon 'head IE's are not relevant for 60g band */
448         wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->beacon_ies_len,
449                    bcon->beacon_ies);
450         wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, bcon->proberesp_ies_len,
451                    bcon->proberesp_ies);
452         wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, bcon->assocresp_ies_len,
453                    bcon->assocresp_ies);
454
455         wil->secure_pcp = info->privacy;
456
457         rc = wmi_set_bcon(wil, info->beacon_interval, wmi_nettype);
458         if (rc)
459                 return rc;
460
461         /* Rx VRING. After MAC and beacon */
462         rc = wil_rx_init(wil);
463
464         netif_carrier_on(ndev);
465
466         return rc;
467 }
468
469 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
470                                 struct net_device *ndev)
471 {
472         int rc = 0;
473         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
474         struct wireless_dev *wdev = ndev->ieee80211_ptr;
475         u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
476
477         /* To stop beaconing, set BI to 0 */
478         rc = wmi_set_bcon(wil, 0, wmi_nettype);
479
480         return rc;
481 }
482
483 static struct cfg80211_ops wil_cfg80211_ops = {
484         .scan = wil_cfg80211_scan,
485         .connect = wil_cfg80211_connect,
486         .disconnect = wil_cfg80211_disconnect,
487         .change_virtual_intf = wil_cfg80211_change_iface,
488         .get_station = wil_cfg80211_get_station,
489         .set_monitor_channel = wil_cfg80211_set_channel,
490         .add_key = wil_cfg80211_add_key,
491         .del_key = wil_cfg80211_del_key,
492         .set_default_key = wil_cfg80211_set_default_key,
493         /* AP mode */
494         .start_ap = wil_cfg80211_start_ap,
495         .stop_ap = wil_cfg80211_stop_ap,
496 };
497
498 static void wil_wiphy_init(struct wiphy *wiphy)
499 {
500         /* TODO: set real value */
501         wiphy->max_scan_ssids = 10;
502         wiphy->max_num_pmkids = 0 /* TODO: */;
503         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
504                                  BIT(NL80211_IFTYPE_AP) |
505                                  BIT(NL80211_IFTYPE_MONITOR);
506         /* TODO: enable P2P when integrated with supplicant:
507          * BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO)
508          */
509         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
510                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
511         dev_warn(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
512                  __func__, wiphy->flags);
513         wiphy->probe_resp_offload =
514                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
515                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
516                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
517
518         wiphy->bands[IEEE80211_BAND_60GHZ] = &wil_band_60ghz;
519
520         /* TODO: figure this out */
521         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
522
523         wiphy->cipher_suites = wil_cipher_suites;
524         wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
525         wiphy->mgmt_stypes = wil_mgmt_stypes;
526 }
527
528 struct wireless_dev *wil_cfg80211_init(struct device *dev)
529 {
530         int rc = 0;
531         struct wireless_dev *wdev;
532
533         wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
534         if (!wdev)
535                 return ERR_PTR(-ENOMEM);
536
537         wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
538                                 sizeof(struct wil6210_priv));
539         if (!wdev->wiphy) {
540                 rc = -ENOMEM;
541                 goto out;
542         }
543
544         set_wiphy_dev(wdev->wiphy, dev);
545         wil_wiphy_init(wdev->wiphy);
546
547         rc = wiphy_register(wdev->wiphy);
548         if (rc < 0)
549                 goto out_failed_reg;
550
551         return wdev;
552
553 out_failed_reg:
554         wiphy_free(wdev->wiphy);
555 out:
556         kfree(wdev);
557
558         return ERR_PTR(rc);
559 }
560
561 void wil_wdev_free(struct wil6210_priv *wil)
562 {
563         struct wireless_dev *wdev = wil_to_wdev(wil);
564
565         if (!wdev)
566                 return;
567
568         wiphy_unregister(wdev->wiphy);
569         wiphy_free(wdev->wiphy);
570         kfree(wdev);
571 }