mwifiex: fix link error against sdio
[cascardo/linux.git] / drivers / net / wireless / ath / wil6210 / cfg80211.c
1 /*
2  * Copyright (c) 2012-2016 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/etherdevice.h>
18 #include "wil6210.h"
19 #include "wmi.h"
20
21 #define WIL_MAX_ROC_DURATION_MS 5000
22
23 #define CHAN60G(_channel, _flags) {                             \
24         .band                   = NL80211_BAND_60GHZ,           \
25         .center_freq            = 56160 + (2160 * (_channel)),  \
26         .hw_value               = (_channel),                   \
27         .flags                  = (_flags),                     \
28         .max_antenna_gain       = 0,                            \
29         .max_power              = 40,                           \
30 }
31
32 static struct ieee80211_channel wil_60ghz_channels[] = {
33         CHAN60G(1, 0),
34         CHAN60G(2, 0),
35         CHAN60G(3, 0),
36 /* channel 4 not supported yet */
37 };
38
39 static struct ieee80211_supported_band wil_band_60ghz = {
40         .channels = wil_60ghz_channels,
41         .n_channels = ARRAY_SIZE(wil_60ghz_channels),
42         .ht_cap = {
43                 .ht_supported = true,
44                 .cap = 0, /* TODO */
45                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
46                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
47                 .mcs = {
48                                 /* MCS 1..12 - SC PHY */
49                         .rx_mask = {0xfe, 0x1f}, /* 1..12 */
50                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
51                 },
52         },
53 };
54
55 static const struct ieee80211_txrx_stypes
56 wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
57         [NL80211_IFTYPE_STATION] = {
58                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
59                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
60                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
61                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
62         },
63         [NL80211_IFTYPE_AP] = {
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_P2P_CLIENT] = {
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_GO] = {
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_DEVICE] = {
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 static const char * const key_usage_str[] = {
94         [WMI_KEY_USE_PAIRWISE]  = "PTK",
95         [WMI_KEY_USE_RX_GROUP]  = "RX_GTK",
96         [WMI_KEY_USE_TX_GROUP]  = "TX_GTK",
97 };
98
99 int wil_iftype_nl2wmi(enum nl80211_iftype type)
100 {
101         static const struct {
102                 enum nl80211_iftype nl;
103                 enum wmi_network_type wmi;
104         } __nl2wmi[] = {
105                 {NL80211_IFTYPE_ADHOC,          WMI_NETTYPE_ADHOC},
106                 {NL80211_IFTYPE_STATION,        WMI_NETTYPE_INFRA},
107                 {NL80211_IFTYPE_AP,             WMI_NETTYPE_AP},
108                 {NL80211_IFTYPE_P2P_CLIENT,     WMI_NETTYPE_P2P},
109                 {NL80211_IFTYPE_P2P_GO,         WMI_NETTYPE_P2P},
110                 {NL80211_IFTYPE_MONITOR,        WMI_NETTYPE_ADHOC}, /* FIXME */
111         };
112         uint i;
113
114         for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
115                 if (__nl2wmi[i].nl == type)
116                         return __nl2wmi[i].wmi;
117         }
118
119         return -EOPNOTSUPP;
120 }
121
122 int wil_cid_fill_sinfo(struct wil6210_priv *wil, int cid,
123                        struct station_info *sinfo)
124 {
125         struct wmi_notify_req_cmd cmd = {
126                 .cid = cid,
127                 .interval_usec = 0,
128         };
129         struct {
130                 struct wmi_cmd_hdr wmi;
131                 struct wmi_notify_req_done_event evt;
132         } __packed reply;
133         struct wil_net_stats *stats = &wil->sta[cid].stats;
134         int rc;
135
136         rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
137                       WMI_NOTIFY_REQ_DONE_EVENTID, &reply, sizeof(reply), 20);
138         if (rc)
139                 return rc;
140
141         wil_dbg_wmi(wil, "Link status for CID %d: {\n"
142                     "  MCS %d TSF 0x%016llx\n"
143                     "  BF status 0x%08x SNR 0x%08x SQI %d%%\n"
144                     "  Tx Tpt %d goodput %d Rx goodput %d\n"
145                     "  Sectors(rx:tx) my %d:%d peer %d:%d\n""}\n",
146                     cid, le16_to_cpu(reply.evt.bf_mcs),
147                     le64_to_cpu(reply.evt.tsf), reply.evt.status,
148                     le32_to_cpu(reply.evt.snr_val),
149                     reply.evt.sqi,
150                     le32_to_cpu(reply.evt.tx_tpt),
151                     le32_to_cpu(reply.evt.tx_goodput),
152                     le32_to_cpu(reply.evt.rx_goodput),
153                     le16_to_cpu(reply.evt.my_rx_sector),
154                     le16_to_cpu(reply.evt.my_tx_sector),
155                     le16_to_cpu(reply.evt.other_rx_sector),
156                     le16_to_cpu(reply.evt.other_tx_sector));
157
158         sinfo->generation = wil->sinfo_gen;
159
160         sinfo->filled = BIT(NL80211_STA_INFO_RX_BYTES) |
161                         BIT(NL80211_STA_INFO_TX_BYTES) |
162                         BIT(NL80211_STA_INFO_RX_PACKETS) |
163                         BIT(NL80211_STA_INFO_TX_PACKETS) |
164                         BIT(NL80211_STA_INFO_RX_BITRATE) |
165                         BIT(NL80211_STA_INFO_TX_BITRATE) |
166                         BIT(NL80211_STA_INFO_RX_DROP_MISC) |
167                         BIT(NL80211_STA_INFO_TX_FAILED);
168
169         sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
170         sinfo->txrate.mcs = le16_to_cpu(reply.evt.bf_mcs);
171         sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
172         sinfo->rxrate.mcs = stats->last_mcs_rx;
173         sinfo->rx_bytes = stats->rx_bytes;
174         sinfo->rx_packets = stats->rx_packets;
175         sinfo->rx_dropped_misc = stats->rx_dropped;
176         sinfo->tx_bytes = stats->tx_bytes;
177         sinfo->tx_packets = stats->tx_packets;
178         sinfo->tx_failed = stats->tx_errors;
179
180         if (test_bit(wil_status_fwconnected, wil->status)) {
181                 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
182                 sinfo->signal = reply.evt.sqi;
183         }
184
185         return rc;
186 }
187
188 static int wil_cfg80211_get_station(struct wiphy *wiphy,
189                                     struct net_device *ndev,
190                                     const u8 *mac, struct station_info *sinfo)
191 {
192         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
193         int rc;
194
195         int cid = wil_find_cid(wil, mac);
196
197         wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
198         if (cid < 0)
199                 return cid;
200
201         rc = wil_cid_fill_sinfo(wil, cid, sinfo);
202
203         return rc;
204 }
205
206 /*
207  * Find @idx-th active STA for station dump.
208  */
209 static int wil_find_cid_by_idx(struct wil6210_priv *wil, int idx)
210 {
211         int i;
212
213         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
214                 if (wil->sta[i].status == wil_sta_unused)
215                         continue;
216                 if (idx == 0)
217                         return i;
218                 idx--;
219         }
220
221         return -ENOENT;
222 }
223
224 static int wil_cfg80211_dump_station(struct wiphy *wiphy,
225                                      struct net_device *dev, int idx,
226                                      u8 *mac, struct station_info *sinfo)
227 {
228         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
229         int rc;
230         int cid = wil_find_cid_by_idx(wil, idx);
231
232         if (cid < 0)
233                 return -ENOENT;
234
235         ether_addr_copy(mac, wil->sta[cid].addr);
236         wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
237
238         rc = wil_cid_fill_sinfo(wil, cid, sinfo);
239
240         return rc;
241 }
242
243 static struct wireless_dev *
244 wil_cfg80211_add_iface(struct wiphy *wiphy, const char *name,
245                        unsigned char name_assign_type,
246                        enum nl80211_iftype type,
247                        u32 *flags, struct vif_params *params)
248 {
249         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
250         struct net_device *ndev = wil_to_ndev(wil);
251         struct wireless_dev *p2p_wdev;
252
253         wil_dbg_misc(wil, "%s()\n", __func__);
254
255         if (type != NL80211_IFTYPE_P2P_DEVICE) {
256                 wil_err(wil, "%s: unsupported iftype %d\n", __func__, type);
257                 return ERR_PTR(-EINVAL);
258         }
259
260         if (wil->p2p_wdev) {
261                 wil_err(wil, "%s: P2P_DEVICE interface already created\n",
262                         __func__);
263                 return ERR_PTR(-EINVAL);
264         }
265
266         p2p_wdev = kzalloc(sizeof(*p2p_wdev), GFP_KERNEL);
267         if (!p2p_wdev)
268                 return ERR_PTR(-ENOMEM);
269
270         p2p_wdev->iftype = type;
271         p2p_wdev->wiphy = wiphy;
272         /* use our primary ethernet address */
273         ether_addr_copy(p2p_wdev->address, ndev->perm_addr);
274
275         wil->p2p_wdev = p2p_wdev;
276
277         return p2p_wdev;
278 }
279
280 static int wil_cfg80211_del_iface(struct wiphy *wiphy,
281                                   struct wireless_dev *wdev)
282 {
283         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
284
285         wil_dbg_misc(wil, "%s()\n", __func__);
286
287         if (wdev != wil->p2p_wdev) {
288                 wil_err(wil, "%s: delete of incorrect interface 0x%p\n",
289                         __func__, wdev);
290                 return -EINVAL;
291         }
292
293         wil_p2p_wdev_free(wil);
294
295         return 0;
296 }
297
298 static int wil_cfg80211_change_iface(struct wiphy *wiphy,
299                                      struct net_device *ndev,
300                                      enum nl80211_iftype type, u32 *flags,
301                                      struct vif_params *params)
302 {
303         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
304         struct wireless_dev *wdev = wil_to_wdev(wil);
305         int rc;
306
307         wil_dbg_misc(wil, "%s() type=%d\n", __func__, type);
308
309         if (netif_running(wil_to_ndev(wil)) && !wil_is_recovery_blocked(wil)) {
310                 wil_dbg_misc(wil, "interface is up. resetting...\n");
311                 mutex_lock(&wil->mutex);
312                 __wil_down(wil);
313                 rc = __wil_up(wil);
314                 mutex_unlock(&wil->mutex);
315
316                 if (rc)
317                         return rc;
318         }
319
320         switch (type) {
321         case NL80211_IFTYPE_STATION:
322         case NL80211_IFTYPE_AP:
323         case NL80211_IFTYPE_P2P_CLIENT:
324         case NL80211_IFTYPE_P2P_GO:
325                 break;
326         case NL80211_IFTYPE_MONITOR:
327                 if (flags)
328                         wil->monitor_flags = *flags;
329                 else
330                         wil->monitor_flags = 0;
331
332                 break;
333         default:
334                 return -EOPNOTSUPP;
335         }
336
337         wdev->iftype = type;
338
339         return 0;
340 }
341
342 static int wil_cfg80211_scan(struct wiphy *wiphy,
343                              struct cfg80211_scan_request *request)
344 {
345         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
346         struct wireless_dev *wdev = request->wdev;
347         struct {
348                 struct wmi_start_scan_cmd cmd;
349                 u16 chnl[4];
350         } __packed cmd;
351         uint i, n;
352         int rc;
353
354         wil_dbg_misc(wil, "%s(), wdev=0x%p iftype=%d\n",
355                      __func__, wdev, wdev->iftype);
356
357         if (wil->scan_request) {
358                 wil_err(wil, "Already scanning\n");
359                 return -EAGAIN;
360         }
361
362         /* check we are client side */
363         switch (wdev->iftype) {
364         case NL80211_IFTYPE_STATION:
365         case NL80211_IFTYPE_P2P_CLIENT:
366         case NL80211_IFTYPE_P2P_DEVICE:
367                 break;
368         default:
369                 return -EOPNOTSUPP;
370         }
371
372         /* FW don't support scan after connection attempt */
373         if (test_bit(wil_status_dontscan, wil->status)) {
374                 wil_err(wil, "Can't scan now\n");
375                 return -EBUSY;
376         }
377
378         /* social scan on P2P_DEVICE is handled as p2p search */
379         if (wdev->iftype == NL80211_IFTYPE_P2P_DEVICE &&
380             wil_p2p_is_social_scan(request)) {
381                 wil->scan_request = request;
382                 wil->radio_wdev = wdev;
383                 rc = wil_p2p_search(wil, request);
384                 if (rc) {
385                         wil->radio_wdev = wil_to_wdev(wil);
386                         wil->scan_request = NULL;
387                 }
388                 return rc;
389         }
390
391         (void)wil_p2p_stop_discovery(wil);
392
393         wil_dbg_misc(wil, "Start scan_request 0x%p\n", request);
394         wil_dbg_misc(wil, "SSID count: %d", request->n_ssids);
395
396         for (i = 0; i < request->n_ssids; i++) {
397                 wil_dbg_misc(wil, "SSID[%d]", i);
398                 print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
399                                      request->ssids[i].ssid,
400                                      request->ssids[i].ssid_len);
401         }
402
403         if (request->n_ssids)
404                 rc = wmi_set_ssid(wil, request->ssids[0].ssid_len,
405                                   request->ssids[0].ssid);
406         else
407                 rc = wmi_set_ssid(wil, 0, NULL);
408
409         if (rc) {
410                 wil_err(wil, "set SSID for scan request failed: %d\n", rc);
411                 return rc;
412         }
413
414         wil->scan_request = request;
415         mod_timer(&wil->scan_timer, jiffies + WIL6210_SCAN_TO);
416
417         memset(&cmd, 0, sizeof(cmd));
418         cmd.cmd.scan_type = WMI_ACTIVE_SCAN;
419         cmd.cmd.num_channels = 0;
420         n = min(request->n_channels, 4U);
421         for (i = 0; i < n; i++) {
422                 int ch = request->channels[i]->hw_value;
423
424                 if (ch == 0) {
425                         wil_err(wil,
426                                 "Scan requested for unknown frequency %dMhz\n",
427                                 request->channels[i]->center_freq);
428                         continue;
429                 }
430                 /* 0-based channel indexes */
431                 cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
432                 wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
433                              request->channels[i]->center_freq);
434         }
435
436         if (request->ie_len)
437                 print_hex_dump_bytes("Scan IE ", DUMP_PREFIX_OFFSET,
438                                      request->ie, request->ie_len);
439         else
440                 wil_dbg_misc(wil, "Scan has no IE's\n");
441
442         rc = wmi_set_ie(wil, WMI_FRAME_PROBE_REQ, request->ie_len, request->ie);
443         if (rc)
444                 goto out;
445
446         if (wil->discovery_mode && cmd.cmd.scan_type == WMI_ACTIVE_SCAN) {
447                 cmd.cmd.discovery_mode = 1;
448                 wil_dbg_misc(wil, "active scan with discovery_mode=1\n");
449         }
450
451         wil->radio_wdev = wdev;
452         rc = wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
453                         cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
454
455 out:
456         if (rc) {
457                 del_timer_sync(&wil->scan_timer);
458                 wil->radio_wdev = wil_to_wdev(wil);
459                 wil->scan_request = NULL;
460         }
461
462         return rc;
463 }
464
465 static void wil_print_crypto(struct wil6210_priv *wil,
466                              struct cfg80211_crypto_settings *c)
467 {
468         int i, n;
469
470         wil_dbg_misc(wil, "WPA versions: 0x%08x cipher group 0x%08x\n",
471                      c->wpa_versions, c->cipher_group);
472         wil_dbg_misc(wil, "Pairwise ciphers [%d] {\n", c->n_ciphers_pairwise);
473         n = min_t(int, c->n_ciphers_pairwise, ARRAY_SIZE(c->ciphers_pairwise));
474         for (i = 0; i < n; i++)
475                 wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
476                              c->ciphers_pairwise[i]);
477         wil_dbg_misc(wil, "}\n");
478         wil_dbg_misc(wil, "AKM suites [%d] {\n", c->n_akm_suites);
479         n = min_t(int, c->n_akm_suites, ARRAY_SIZE(c->akm_suites));
480         for (i = 0; i < n; i++)
481                 wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
482                              c->akm_suites[i]);
483         wil_dbg_misc(wil, "}\n");
484         wil_dbg_misc(wil, "Control port : %d, eth_type 0x%04x no_encrypt %d\n",
485                      c->control_port, be16_to_cpu(c->control_port_ethertype),
486                      c->control_port_no_encrypt);
487 }
488
489 static void wil_print_connect_params(struct wil6210_priv *wil,
490                                      struct cfg80211_connect_params *sme)
491 {
492         wil_info(wil, "Connecting to:\n");
493         if (sme->channel) {
494                 wil_info(wil, "  Channel: %d freq %d\n",
495                          sme->channel->hw_value, sme->channel->center_freq);
496         }
497         if (sme->bssid)
498                 wil_info(wil, "  BSSID: %pM\n", sme->bssid);
499         if (sme->ssid)
500                 print_hex_dump(KERN_INFO, "  SSID: ", DUMP_PREFIX_OFFSET,
501                                16, 1, sme->ssid, sme->ssid_len, true);
502         wil_info(wil, "  Privacy: %s\n", sme->privacy ? "secure" : "open");
503         wil_info(wil, "  PBSS: %d\n", sme->pbss);
504         wil_print_crypto(wil, &sme->crypto);
505 }
506
507 static int wil_cfg80211_connect(struct wiphy *wiphy,
508                                 struct net_device *ndev,
509                                 struct cfg80211_connect_params *sme)
510 {
511         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
512         struct cfg80211_bss *bss;
513         struct wmi_connect_cmd conn;
514         const u8 *ssid_eid;
515         const u8 *rsn_eid;
516         int ch;
517         int rc = 0;
518         enum ieee80211_bss_type bss_type = IEEE80211_BSS_TYPE_ESS;
519
520         wil_dbg_misc(wil, "%s()\n", __func__);
521         wil_print_connect_params(wil, sme);
522
523         if (test_bit(wil_status_fwconnecting, wil->status) ||
524             test_bit(wil_status_fwconnected, wil->status))
525                 return -EALREADY;
526
527         if (sme->ie_len > WMI_MAX_IE_LEN) {
528                 wil_err(wil, "IE too large (%td bytes)\n", sme->ie_len);
529                 return -ERANGE;
530         }
531
532         rsn_eid = sme->ie ?
533                         cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
534                         NULL;
535         if (sme->privacy && !rsn_eid)
536                 wil_info(wil, "WSC connection\n");
537
538         if (sme->pbss)
539                 bss_type = IEEE80211_BSS_TYPE_PBSS;
540
541         bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
542                                sme->ssid, sme->ssid_len,
543                                bss_type, IEEE80211_PRIVACY_ANY);
544         if (!bss) {
545                 wil_err(wil, "Unable to find BSS\n");
546                 return -ENOENT;
547         }
548
549         ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
550         if (!ssid_eid) {
551                 wil_err(wil, "No SSID\n");
552                 rc = -ENOENT;
553                 goto out;
554         }
555         wil->privacy = sme->privacy;
556
557         if (wil->privacy) {
558                 /* For secure assoc, remove old keys */
559                 rc = wmi_del_cipher_key(wil, 0, bss->bssid,
560                                         WMI_KEY_USE_PAIRWISE);
561                 if (rc) {
562                         wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(PTK) failed\n");
563                         goto out;
564                 }
565                 rc = wmi_del_cipher_key(wil, 0, bss->bssid,
566                                         WMI_KEY_USE_RX_GROUP);
567                 if (rc) {
568                         wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(GTK) failed\n");
569                         goto out;
570                 }
571         }
572
573         /* WMI_SET_APPIE_CMD. ie may contain rsn info as well as other info
574          * elements. Send it also in case it's empty, to erase previously set
575          * ies in FW.
576          */
577         rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
578         if (rc)
579                 goto out;
580
581         /* WMI_CONNECT_CMD */
582         memset(&conn, 0, sizeof(conn));
583         switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
584         case WLAN_CAPABILITY_DMG_TYPE_AP:
585                 conn.network_type = WMI_NETTYPE_INFRA;
586                 break;
587         case WLAN_CAPABILITY_DMG_TYPE_PBSS:
588                 conn.network_type = WMI_NETTYPE_P2P;
589                 break;
590         default:
591                 wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
592                         bss->capability);
593                 goto out;
594         }
595         if (wil->privacy) {
596                 if (rsn_eid) { /* regular secure connection */
597                         conn.dot11_auth_mode = WMI_AUTH11_SHARED;
598                         conn.auth_mode = WMI_AUTH_WPA2_PSK;
599                         conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
600                         conn.pairwise_crypto_len = 16;
601                         conn.group_crypto_type = WMI_CRYPT_AES_GCMP;
602                         conn.group_crypto_len = 16;
603                 } else { /* WSC */
604                         conn.dot11_auth_mode = WMI_AUTH11_WSC;
605                         conn.auth_mode = WMI_AUTH_NONE;
606                 }
607         } else { /* insecure connection */
608                 conn.dot11_auth_mode = WMI_AUTH11_OPEN;
609                 conn.auth_mode = WMI_AUTH_NONE;
610         }
611
612         conn.ssid_len = min_t(u8, ssid_eid[1], 32);
613         memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
614
615         ch = bss->channel->hw_value;
616         if (ch == 0) {
617                 wil_err(wil, "BSS at unknown frequency %dMhz\n",
618                         bss->channel->center_freq);
619                 rc = -EOPNOTSUPP;
620                 goto out;
621         }
622         conn.channel = ch - 1;
623
624         ether_addr_copy(conn.bssid, bss->bssid);
625         ether_addr_copy(conn.dst_mac, bss->bssid);
626
627         set_bit(wil_status_fwconnecting, wil->status);
628
629         rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
630         if (rc == 0) {
631                 netif_carrier_on(ndev);
632                 /* Connect can take lots of time */
633                 mod_timer(&wil->connect_timer,
634                           jiffies + msecs_to_jiffies(2000));
635         } else {
636                 clear_bit(wil_status_fwconnecting, wil->status);
637         }
638
639  out:
640         cfg80211_put_bss(wiphy, bss);
641
642         return rc;
643 }
644
645 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
646                                    struct net_device *ndev,
647                                    u16 reason_code)
648 {
649         int rc;
650         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
651
652         wil_dbg_misc(wil, "%s(reason=%d)\n", __func__, reason_code);
653
654         if (!(test_bit(wil_status_fwconnecting, wil->status) ||
655               test_bit(wil_status_fwconnected, wil->status))) {
656                 wil_err(wil, "%s: Disconnect was called while disconnected\n",
657                         __func__);
658                 return 0;
659         }
660
661         rc = wmi_call(wil, WMI_DISCONNECT_CMDID, NULL, 0,
662                       WMI_DISCONNECT_EVENTID, NULL, 0,
663                       WIL6210_DISCONNECT_TO_MS);
664         if (rc)
665                 wil_err(wil, "%s: disconnect error %d\n", __func__, rc);
666
667         return rc;
668 }
669
670 int wil_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
671                          struct cfg80211_mgmt_tx_params *params,
672                          u64 *cookie)
673 {
674         const u8 *buf = params->buf;
675         size_t len = params->len;
676         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
677         int rc;
678         bool tx_status = false;
679         struct ieee80211_mgmt *mgmt_frame = (void *)buf;
680         struct wmi_sw_tx_req_cmd *cmd;
681         struct {
682                 struct wmi_cmd_hdr wmi;
683                 struct wmi_sw_tx_complete_event evt;
684         } __packed evt;
685
686         /* Note, currently we do not support the "wait" parameter, user-space
687          * must call remain_on_channel before mgmt_tx or listen on a channel
688          * another way (AP/PCP or connected station)
689          * in addition we need to check if specified "chan" argument is
690          * different from currently "listened" channel and fail if it is.
691          */
692
693         wil_dbg_misc(wil, "%s()\n", __func__);
694         print_hex_dump_bytes("mgmt tx frame ", DUMP_PREFIX_OFFSET, buf, len);
695
696         cmd = kmalloc(sizeof(*cmd) + len, GFP_KERNEL);
697         if (!cmd) {
698                 rc = -ENOMEM;
699                 goto out;
700         }
701
702         memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
703         cmd->len = cpu_to_le16(len);
704         memcpy(cmd->payload, buf, len);
705
706         rc = wmi_call(wil, WMI_SW_TX_REQ_CMDID, cmd, sizeof(*cmd) + len,
707                       WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
708         if (rc == 0)
709                 tx_status = !evt.evt.status;
710
711         kfree(cmd);
712  out:
713         cfg80211_mgmt_tx_status(wdev, cookie ? *cookie : 0, buf, len,
714                                 tx_status, GFP_KERNEL);
715         return rc;
716 }
717
718 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
719                                     struct cfg80211_chan_def *chandef)
720 {
721         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
722         struct wireless_dev *wdev = wil_to_wdev(wil);
723
724         wdev->preset_chandef = *chandef;
725
726         return 0;
727 }
728
729 static enum wmi_key_usage wil_detect_key_usage(struct wil6210_priv *wil,
730                                                bool pairwise)
731 {
732         struct wireless_dev *wdev = wil_to_wdev(wil);
733         enum wmi_key_usage rc;
734
735         if (pairwise) {
736                 rc = WMI_KEY_USE_PAIRWISE;
737         } else {
738                 switch (wdev->iftype) {
739                 case NL80211_IFTYPE_STATION:
740                 case NL80211_IFTYPE_P2P_CLIENT:
741                         rc = WMI_KEY_USE_RX_GROUP;
742                         break;
743                 case NL80211_IFTYPE_AP:
744                 case NL80211_IFTYPE_P2P_GO:
745                         rc = WMI_KEY_USE_TX_GROUP;
746                         break;
747                 default:
748                         /* TODO: Rx GTK or Tx GTK? */
749                         wil_err(wil, "Can't determine GTK type\n");
750                         rc = WMI_KEY_USE_RX_GROUP;
751                         break;
752                 }
753         }
754         wil_dbg_misc(wil, "%s() -> %s\n", __func__, key_usage_str[rc]);
755
756         return rc;
757 }
758
759 static struct wil_tid_crypto_rx_single *
760 wil_find_crypto_ctx(struct wil6210_priv *wil, u8 key_index,
761                     enum wmi_key_usage key_usage, const u8 *mac_addr)
762 {
763         int cid = -EINVAL;
764         int tid = 0;
765         struct wil_sta_info *s;
766         struct wil_tid_crypto_rx *c;
767
768         if (key_usage == WMI_KEY_USE_TX_GROUP)
769                 return NULL; /* not needed */
770
771         /* supplicant provides Rx group key in STA mode with NULL MAC address */
772         if (mac_addr)
773                 cid = wil_find_cid(wil, mac_addr);
774         else if (key_usage == WMI_KEY_USE_RX_GROUP)
775                 cid = wil_find_cid_by_idx(wil, 0);
776         if (cid < 0) {
777                 wil_err(wil, "No CID for %pM %s[%d]\n", mac_addr,
778                         key_usage_str[key_usage], key_index);
779                 return ERR_PTR(cid);
780         }
781
782         s = &wil->sta[cid];
783         if (key_usage == WMI_KEY_USE_PAIRWISE)
784                 c = &s->tid_crypto_rx[tid];
785         else
786                 c = &s->group_crypto_rx;
787
788         return &c->key_id[key_index];
789 }
790
791 static int wil_cfg80211_add_key(struct wiphy *wiphy,
792                                 struct net_device *ndev,
793                                 u8 key_index, bool pairwise,
794                                 const u8 *mac_addr,
795                                 struct key_params *params)
796 {
797         int rc;
798         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
799         enum wmi_key_usage key_usage = wil_detect_key_usage(wil, pairwise);
800         struct wil_tid_crypto_rx_single *cc = wil_find_crypto_ctx(wil,
801                                                                   key_index,
802                                                                   key_usage,
803                                                                   mac_addr);
804
805         wil_dbg_misc(wil, "%s(%pM %s[%d] PN %*phN)\n", __func__,
806                      mac_addr, key_usage_str[key_usage], key_index,
807                      params->seq_len, params->seq);
808
809         if (IS_ERR(cc)) {
810                 wil_err(wil, "Not connected, %s(%pM %s[%d] PN %*phN)\n",
811                         __func__, mac_addr, key_usage_str[key_usage], key_index,
812                         params->seq_len, params->seq);
813                 return -EINVAL;
814         }
815
816         if (cc)
817                 cc->key_set = false;
818
819         if (params->seq && params->seq_len != IEEE80211_GCMP_PN_LEN) {
820                 wil_err(wil,
821                         "Wrong PN len %d, %s(%pM %s[%d] PN %*phN)\n",
822                         params->seq_len, __func__, mac_addr,
823                         key_usage_str[key_usage], key_index,
824                         params->seq_len, params->seq);
825                 return -EINVAL;
826         }
827
828         rc = wmi_add_cipher_key(wil, key_index, mac_addr, params->key_len,
829                                 params->key, key_usage);
830         if ((rc == 0) && cc) {
831                 if (params->seq)
832                         memcpy(cc->pn, params->seq, IEEE80211_GCMP_PN_LEN);
833                 else
834                         memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
835                 cc->key_set = true;
836         }
837
838         return rc;
839 }
840
841 static int wil_cfg80211_del_key(struct wiphy *wiphy,
842                                 struct net_device *ndev,
843                                 u8 key_index, bool pairwise,
844                                 const u8 *mac_addr)
845 {
846         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
847         enum wmi_key_usage key_usage = wil_detect_key_usage(wil, pairwise);
848         struct wil_tid_crypto_rx_single *cc = wil_find_crypto_ctx(wil,
849                                                                   key_index,
850                                                                   key_usage,
851                                                                   mac_addr);
852
853         wil_dbg_misc(wil, "%s(%pM %s[%d])\n", __func__, mac_addr,
854                      key_usage_str[key_usage], key_index);
855
856         if (IS_ERR(cc))
857                 wil_info(wil, "Not connected, %s(%pM %s[%d])\n", __func__,
858                          mac_addr, key_usage_str[key_usage], key_index);
859
860         if (!IS_ERR_OR_NULL(cc))
861                 cc->key_set = false;
862
863         return wmi_del_cipher_key(wil, key_index, mac_addr, key_usage);
864 }
865
866 /* Need to be present or wiphy_new() will WARN */
867 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
868                                         struct net_device *ndev,
869                                         u8 key_index, bool unicast,
870                                         bool multicast)
871 {
872         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
873
874         wil_dbg_misc(wil, "%s: entered\n", __func__);
875         return 0;
876 }
877
878 static int wil_remain_on_channel(struct wiphy *wiphy,
879                                  struct wireless_dev *wdev,
880                                  struct ieee80211_channel *chan,
881                                  unsigned int duration,
882                                  u64 *cookie)
883 {
884         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
885         int rc;
886
887         wil_dbg_misc(wil, "%s() center_freq=%d, duration=%d iftype=%d\n",
888                      __func__, chan->center_freq, duration, wdev->iftype);
889
890         rc = wil_p2p_listen(wil, duration, chan, cookie);
891         if (rc)
892                 return rc;
893
894         wil->radio_wdev = wdev;
895
896         cfg80211_ready_on_channel(wdev, *cookie, chan, duration,
897                                   GFP_KERNEL);
898
899         return 0;
900 }
901
902 static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
903                                         struct wireless_dev *wdev,
904                                         u64 cookie)
905 {
906         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
907
908         wil_dbg_misc(wil, "%s()\n", __func__);
909
910         return wil_p2p_cancel_listen(wil, cookie);
911 }
912
913 /**
914  * find a specific IE in a list of IEs
915  * return a pointer to the beginning of IE in the list
916  * or NULL if not found
917  */
918 static const u8 *_wil_cfg80211_find_ie(const u8 *ies, u16 ies_len, const u8 *ie,
919                                        u16 ie_len)
920 {
921         struct ieee80211_vendor_ie *vie;
922         u32 oui;
923
924         /* IE tag at offset 0, length at offset 1 */
925         if (ie_len < 2 || 2 + ie[1] > ie_len)
926                 return NULL;
927
928         if (ie[0] != WLAN_EID_VENDOR_SPECIFIC)
929                 return cfg80211_find_ie(ie[0], ies, ies_len);
930
931         /* make sure there is room for 3 bytes OUI + 1 byte OUI type */
932         if (ie[1] < 4)
933                 return NULL;
934         vie = (struct ieee80211_vendor_ie *)ie;
935         oui = vie->oui[0] << 16 | vie->oui[1] << 8 | vie->oui[2];
936         return cfg80211_find_vendor_ie(oui, vie->oui_type, ies,
937                                        ies_len);
938 }
939
940 /**
941  * merge the IEs in two lists into a single list.
942  * do not include IEs from the second list which exist in the first list.
943  * add only vendor specific IEs from second list to keep
944  * the merged list sorted (since vendor-specific IE has the
945  * highest tag number)
946  * caller must free the allocated memory for merged IEs
947  */
948 static int _wil_cfg80211_merge_extra_ies(const u8 *ies1, u16 ies1_len,
949                                          const u8 *ies2, u16 ies2_len,
950                                          u8 **merged_ies, u16 *merged_len)
951 {
952         u8 *buf, *dpos;
953         const u8 *spos;
954
955         if (ies1_len == 0 && ies2_len == 0) {
956                 *merged_ies = NULL;
957                 *merged_len = 0;
958                 return 0;
959         }
960
961         buf = kmalloc(ies1_len + ies2_len, GFP_KERNEL);
962         if (!buf)
963                 return -ENOMEM;
964         memcpy(buf, ies1, ies1_len);
965         dpos = buf + ies1_len;
966         spos = ies2;
967         while (spos + 1 < ies2 + ies2_len) {
968                 /* IE tag at offset 0, length at offset 1 */
969                 u16 ielen = 2 + spos[1];
970
971                 if (spos + ielen > ies2 + ies2_len)
972                         break;
973                 if (spos[0] == WLAN_EID_VENDOR_SPECIFIC &&
974                     !_wil_cfg80211_find_ie(ies1, ies1_len, spos, ielen)) {
975                         memcpy(dpos, spos, ielen);
976                         dpos += ielen;
977                 }
978                 spos += ielen;
979         }
980
981         *merged_ies = buf;
982         *merged_len = dpos - buf;
983         return 0;
984 }
985
986 static void wil_print_bcon_data(struct cfg80211_beacon_data *b)
987 {
988         print_hex_dump_bytes("head     ", DUMP_PREFIX_OFFSET,
989                              b->head, b->head_len);
990         print_hex_dump_bytes("tail     ", DUMP_PREFIX_OFFSET,
991                              b->tail, b->tail_len);
992         print_hex_dump_bytes("BCON IE  ", DUMP_PREFIX_OFFSET,
993                              b->beacon_ies, b->beacon_ies_len);
994         print_hex_dump_bytes("PROBE    ", DUMP_PREFIX_OFFSET,
995                              b->probe_resp, b->probe_resp_len);
996         print_hex_dump_bytes("PROBE IE ", DUMP_PREFIX_OFFSET,
997                              b->proberesp_ies, b->proberesp_ies_len);
998         print_hex_dump_bytes("ASSOC IE ", DUMP_PREFIX_OFFSET,
999                              b->assocresp_ies, b->assocresp_ies_len);
1000 }
1001
1002 /* internal functions for device reset and starting AP */
1003 static int _wil_cfg80211_set_ies(struct wiphy *wiphy,
1004                                  struct cfg80211_beacon_data *bcon)
1005 {
1006         int rc;
1007         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1008         u16 len = 0, proberesp_len = 0;
1009         u8 *ies = NULL, *proberesp = NULL;
1010
1011         if (bcon->probe_resp) {
1012                 struct ieee80211_mgmt *f =
1013                         (struct ieee80211_mgmt *)bcon->probe_resp;
1014                 size_t hlen = offsetof(struct ieee80211_mgmt,
1015                                        u.probe_resp.variable);
1016                 proberesp = f->u.probe_resp.variable;
1017                 proberesp_len = bcon->probe_resp_len - hlen;
1018         }
1019         rc = _wil_cfg80211_merge_extra_ies(proberesp,
1020                                            proberesp_len,
1021                                            bcon->proberesp_ies,
1022                                            bcon->proberesp_ies_len,
1023                                            &ies, &len);
1024
1025         if (rc)
1026                 goto out;
1027
1028         rc = wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, len, ies);
1029         if (rc)
1030                 goto out;
1031
1032         if (bcon->assocresp_ies)
1033                 rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP,
1034                                 bcon->assocresp_ies_len, bcon->assocresp_ies);
1035         else
1036                 rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, len, ies);
1037 #if 0 /* to use beacon IE's, remove this #if 0 */
1038         if (rc)
1039                 goto out;
1040
1041         rc = wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->tail_len, bcon->tail);
1042 #endif
1043 out:
1044         kfree(ies);
1045         return rc;
1046 }
1047
1048 static int _wil_cfg80211_start_ap(struct wiphy *wiphy,
1049                                   struct net_device *ndev,
1050                                   const u8 *ssid, size_t ssid_len, u32 privacy,
1051                                   int bi, u8 chan,
1052                                   struct cfg80211_beacon_data *bcon,
1053                                   u8 hidden_ssid, u32 pbss)
1054 {
1055         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1056         int rc;
1057         struct wireless_dev *wdev = ndev->ieee80211_ptr;
1058         u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
1059         u8 is_go = (wdev->iftype == NL80211_IFTYPE_P2P_GO);
1060
1061         if (pbss)
1062                 wmi_nettype = WMI_NETTYPE_P2P;
1063
1064         wil_dbg_misc(wil, "%s: is_go=%d\n", __func__, is_go);
1065         if (is_go && !pbss) {
1066                 wil_err(wil, "%s: P2P GO must be in PBSS\n", __func__);
1067                 return -ENOTSUPP;
1068         }
1069
1070         wil_set_recovery_state(wil, fw_recovery_idle);
1071
1072         mutex_lock(&wil->mutex);
1073
1074         __wil_down(wil);
1075         rc = __wil_up(wil);
1076         if (rc)
1077                 goto out;
1078
1079         rc = wmi_set_ssid(wil, ssid_len, ssid);
1080         if (rc)
1081                 goto out;
1082
1083         rc = _wil_cfg80211_set_ies(wiphy, bcon);
1084         if (rc)
1085                 goto out;
1086
1087         wil->privacy = privacy;
1088         wil->channel = chan;
1089         wil->hidden_ssid = hidden_ssid;
1090         wil->pbss = pbss;
1091
1092         netif_carrier_on(ndev);
1093
1094         rc = wmi_pcp_start(wil, bi, wmi_nettype, chan, hidden_ssid, is_go);
1095         if (rc)
1096                 goto err_pcp_start;
1097
1098         rc = wil_bcast_init(wil);
1099         if (rc)
1100                 goto err_bcast;
1101
1102         goto out; /* success */
1103
1104 err_bcast:
1105         wmi_pcp_stop(wil);
1106 err_pcp_start:
1107         netif_carrier_off(ndev);
1108 out:
1109         mutex_unlock(&wil->mutex);
1110         return rc;
1111 }
1112
1113 static int wil_cfg80211_change_beacon(struct wiphy *wiphy,
1114                                       struct net_device *ndev,
1115                                       struct cfg80211_beacon_data *bcon)
1116 {
1117         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1118         int rc;
1119         u32 privacy = 0;
1120
1121         wil_dbg_misc(wil, "%s()\n", __func__);
1122         wil_print_bcon_data(bcon);
1123
1124         if (bcon->tail &&
1125             cfg80211_find_ie(WLAN_EID_RSN, bcon->tail,
1126                              bcon->tail_len))
1127                 privacy = 1;
1128
1129         /* in case privacy has changed, need to restart the AP */
1130         if (wil->privacy != privacy) {
1131                 struct wireless_dev *wdev = ndev->ieee80211_ptr;
1132
1133                 wil_dbg_misc(wil, "privacy changed %d=>%d. Restarting AP\n",
1134                              wil->privacy, privacy);
1135
1136                 rc = _wil_cfg80211_start_ap(wiphy, ndev, wdev->ssid,
1137                                             wdev->ssid_len, privacy,
1138                                             wdev->beacon_interval,
1139                                             wil->channel, bcon,
1140                                             wil->hidden_ssid,
1141                                             wil->pbss);
1142         } else {
1143                 rc = _wil_cfg80211_set_ies(wiphy, bcon);
1144         }
1145
1146         return rc;
1147 }
1148
1149 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
1150                                  struct net_device *ndev,
1151                                  struct cfg80211_ap_settings *info)
1152 {
1153         int rc;
1154         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1155         struct ieee80211_channel *channel = info->chandef.chan;
1156         struct cfg80211_beacon_data *bcon = &info->beacon;
1157         struct cfg80211_crypto_settings *crypto = &info->crypto;
1158         u8 hidden_ssid;
1159
1160         wil_dbg_misc(wil, "%s()\n", __func__);
1161
1162         if (!channel) {
1163                 wil_err(wil, "AP: No channel???\n");
1164                 return -EINVAL;
1165         }
1166
1167         switch (info->hidden_ssid) {
1168         case NL80211_HIDDEN_SSID_NOT_IN_USE:
1169                 hidden_ssid = WMI_HIDDEN_SSID_DISABLED;
1170                 break;
1171
1172         case NL80211_HIDDEN_SSID_ZERO_LEN:
1173                 hidden_ssid = WMI_HIDDEN_SSID_SEND_EMPTY;
1174                 break;
1175
1176         case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1177                 hidden_ssid = WMI_HIDDEN_SSID_CLEAR;
1178                 break;
1179
1180         default:
1181                 wil_err(wil, "AP: Invalid hidden SSID %d\n", info->hidden_ssid);
1182                 return -EOPNOTSUPP;
1183         }
1184         wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
1185                      channel->center_freq, info->privacy ? "secure" : "open");
1186         wil_dbg_misc(wil, "Privacy: %d auth_type %d\n",
1187                      info->privacy, info->auth_type);
1188         wil_dbg_misc(wil, "Hidden SSID mode: %d\n",
1189                      info->hidden_ssid);
1190         wil_dbg_misc(wil, "BI %d DTIM %d\n", info->beacon_interval,
1191                      info->dtim_period);
1192         wil_dbg_misc(wil, "PBSS %d\n", info->pbss);
1193         print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
1194                              info->ssid, info->ssid_len);
1195         wil_print_bcon_data(bcon);
1196         wil_print_crypto(wil, crypto);
1197
1198         rc = _wil_cfg80211_start_ap(wiphy, ndev,
1199                                     info->ssid, info->ssid_len, info->privacy,
1200                                     info->beacon_interval, channel->hw_value,
1201                                     bcon, hidden_ssid, info->pbss);
1202
1203         return rc;
1204 }
1205
1206 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
1207                                 struct net_device *ndev)
1208 {
1209         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1210
1211         wil_dbg_misc(wil, "%s()\n", __func__);
1212
1213         netif_carrier_off(ndev);
1214         wil_set_recovery_state(wil, fw_recovery_idle);
1215
1216         mutex_lock(&wil->mutex);
1217
1218         wmi_pcp_stop(wil);
1219
1220         __wil_down(wil);
1221
1222         mutex_unlock(&wil->mutex);
1223
1224         return 0;
1225 }
1226
1227 static int wil_cfg80211_del_station(struct wiphy *wiphy,
1228                                     struct net_device *dev,
1229                                     struct station_del_parameters *params)
1230 {
1231         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1232
1233         wil_dbg_misc(wil, "%s(%pM, reason=%d)\n", __func__, params->mac,
1234                      params->reason_code);
1235
1236         mutex_lock(&wil->mutex);
1237         wil6210_disconnect(wil, params->mac, params->reason_code, false);
1238         mutex_unlock(&wil->mutex);
1239
1240         return 0;
1241 }
1242
1243 /* probe_client handling */
1244 static void wil_probe_client_handle(struct wil6210_priv *wil,
1245                                     struct wil_probe_client_req *req)
1246 {
1247         struct net_device *ndev = wil_to_ndev(wil);
1248         struct wil_sta_info *sta = &wil->sta[req->cid];
1249         /* assume STA is alive if it is still connected,
1250          * else FW will disconnect it
1251          */
1252         bool alive = (sta->status == wil_sta_connected);
1253
1254         cfg80211_probe_status(ndev, sta->addr, req->cookie, alive, GFP_KERNEL);
1255 }
1256
1257 static struct list_head *next_probe_client(struct wil6210_priv *wil)
1258 {
1259         struct list_head *ret = NULL;
1260
1261         mutex_lock(&wil->probe_client_mutex);
1262
1263         if (!list_empty(&wil->probe_client_pending)) {
1264                 ret = wil->probe_client_pending.next;
1265                 list_del(ret);
1266         }
1267
1268         mutex_unlock(&wil->probe_client_mutex);
1269
1270         return ret;
1271 }
1272
1273 void wil_probe_client_worker(struct work_struct *work)
1274 {
1275         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
1276                                                 probe_client_worker);
1277         struct wil_probe_client_req *req;
1278         struct list_head *lh;
1279
1280         while ((lh = next_probe_client(wil)) != NULL) {
1281                 req = list_entry(lh, struct wil_probe_client_req, list);
1282
1283                 wil_probe_client_handle(wil, req);
1284                 kfree(req);
1285         }
1286 }
1287
1288 void wil_probe_client_flush(struct wil6210_priv *wil)
1289 {
1290         struct wil_probe_client_req *req, *t;
1291
1292         wil_dbg_misc(wil, "%s()\n", __func__);
1293
1294         mutex_lock(&wil->probe_client_mutex);
1295
1296         list_for_each_entry_safe(req, t, &wil->probe_client_pending, list) {
1297                 list_del(&req->list);
1298                 kfree(req);
1299         }
1300
1301         mutex_unlock(&wil->probe_client_mutex);
1302 }
1303
1304 static int wil_cfg80211_probe_client(struct wiphy *wiphy,
1305                                      struct net_device *dev,
1306                                      const u8 *peer, u64 *cookie)
1307 {
1308         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1309         struct wil_probe_client_req *req;
1310         int cid = wil_find_cid(wil, peer);
1311
1312         wil_dbg_misc(wil, "%s(%pM => CID %d)\n", __func__, peer, cid);
1313
1314         if (cid < 0)
1315                 return -ENOLINK;
1316
1317         req = kzalloc(sizeof(*req), GFP_KERNEL);
1318         if (!req)
1319                 return -ENOMEM;
1320
1321         req->cid = cid;
1322         req->cookie = cid;
1323
1324         mutex_lock(&wil->probe_client_mutex);
1325         list_add_tail(&req->list, &wil->probe_client_pending);
1326         mutex_unlock(&wil->probe_client_mutex);
1327
1328         *cookie = req->cookie;
1329         queue_work(wil->wq_service, &wil->probe_client_worker);
1330         return 0;
1331 }
1332
1333 static int wil_cfg80211_change_bss(struct wiphy *wiphy,
1334                                    struct net_device *dev,
1335                                    struct bss_parameters *params)
1336 {
1337         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1338
1339         if (params->ap_isolate >= 0) {
1340                 wil_dbg_misc(wil, "%s(ap_isolate %d => %d)\n", __func__,
1341                              wil->ap_isolate, params->ap_isolate);
1342                 wil->ap_isolate = params->ap_isolate;
1343         }
1344
1345         return 0;
1346 }
1347
1348 static int wil_cfg80211_start_p2p_device(struct wiphy *wiphy,
1349                                          struct wireless_dev *wdev)
1350 {
1351         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1352
1353         wil_dbg_misc(wil, "%s: entered\n", __func__);
1354         return 0;
1355 }
1356
1357 static void wil_cfg80211_stop_p2p_device(struct wiphy *wiphy,
1358                                          struct wireless_dev *wdev)
1359 {
1360         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1361
1362         wil_dbg_misc(wil, "%s: entered\n", __func__);
1363 }
1364
1365 static struct cfg80211_ops wil_cfg80211_ops = {
1366         .add_virtual_intf = wil_cfg80211_add_iface,
1367         .del_virtual_intf = wil_cfg80211_del_iface,
1368         .scan = wil_cfg80211_scan,
1369         .connect = wil_cfg80211_connect,
1370         .disconnect = wil_cfg80211_disconnect,
1371         .change_virtual_intf = wil_cfg80211_change_iface,
1372         .get_station = wil_cfg80211_get_station,
1373         .dump_station = wil_cfg80211_dump_station,
1374         .remain_on_channel = wil_remain_on_channel,
1375         .cancel_remain_on_channel = wil_cancel_remain_on_channel,
1376         .mgmt_tx = wil_cfg80211_mgmt_tx,
1377         .set_monitor_channel = wil_cfg80211_set_channel,
1378         .add_key = wil_cfg80211_add_key,
1379         .del_key = wil_cfg80211_del_key,
1380         .set_default_key = wil_cfg80211_set_default_key,
1381         /* AP mode */
1382         .change_beacon = wil_cfg80211_change_beacon,
1383         .start_ap = wil_cfg80211_start_ap,
1384         .stop_ap = wil_cfg80211_stop_ap,
1385         .del_station = wil_cfg80211_del_station,
1386         .probe_client = wil_cfg80211_probe_client,
1387         .change_bss = wil_cfg80211_change_bss,
1388         /* P2P device */
1389         .start_p2p_device = wil_cfg80211_start_p2p_device,
1390         .stop_p2p_device = wil_cfg80211_stop_p2p_device,
1391 };
1392
1393 static void wil_wiphy_init(struct wiphy *wiphy)
1394 {
1395         wiphy->max_scan_ssids = 1;
1396         wiphy->max_scan_ie_len = WMI_MAX_IE_LEN;
1397         wiphy->max_remain_on_channel_duration = WIL_MAX_ROC_DURATION_MS;
1398         wiphy->max_num_pmkids = 0 /* TODO: */;
1399         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1400                                  BIT(NL80211_IFTYPE_AP) |
1401                                  BIT(NL80211_IFTYPE_P2P_CLIENT) |
1402                                  BIT(NL80211_IFTYPE_P2P_GO) |
1403                                  BIT(NL80211_IFTYPE_P2P_DEVICE) |
1404                                  BIT(NL80211_IFTYPE_MONITOR);
1405         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
1406                         WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
1407                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
1408         dev_dbg(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
1409                 __func__, wiphy->flags);
1410         wiphy->probe_resp_offload =
1411                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
1412                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
1413                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
1414
1415         wiphy->bands[NL80211_BAND_60GHZ] = &wil_band_60ghz;
1416
1417         /* TODO: figure this out */
1418         wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
1419
1420         wiphy->cipher_suites = wil_cipher_suites;
1421         wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
1422         wiphy->mgmt_stypes = wil_mgmt_stypes;
1423         wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
1424 }
1425
1426 struct wireless_dev *wil_cfg80211_init(struct device *dev)
1427 {
1428         int rc = 0;
1429         struct wireless_dev *wdev;
1430
1431         dev_dbg(dev, "%s()\n", __func__);
1432
1433         wdev = kzalloc(sizeof(*wdev), GFP_KERNEL);
1434         if (!wdev)
1435                 return ERR_PTR(-ENOMEM);
1436
1437         wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
1438                                 sizeof(struct wil6210_priv));
1439         if (!wdev->wiphy) {
1440                 rc = -ENOMEM;
1441                 goto out;
1442         }
1443
1444         set_wiphy_dev(wdev->wiphy, dev);
1445         wil_wiphy_init(wdev->wiphy);
1446
1447         rc = wiphy_register(wdev->wiphy);
1448         if (rc < 0)
1449                 goto out_failed_reg;
1450
1451         return wdev;
1452
1453 out_failed_reg:
1454         wiphy_free(wdev->wiphy);
1455 out:
1456         kfree(wdev);
1457
1458         return ERR_PTR(rc);
1459 }
1460
1461 void wil_wdev_free(struct wil6210_priv *wil)
1462 {
1463         struct wireless_dev *wdev = wil_to_wdev(wil);
1464
1465         dev_dbg(wil_to_dev(wil), "%s()\n", __func__);
1466
1467         if (!wdev)
1468                 return;
1469
1470         wiphy_unregister(wdev->wiphy);
1471         wiphy_free(wdev->wiphy);
1472         kfree(wdev);
1473 }
1474
1475 void wil_p2p_wdev_free(struct wil6210_priv *wil)
1476 {
1477         struct wireless_dev *p2p_wdev;
1478
1479         mutex_lock(&wil->p2p_wdev_mutex);
1480         p2p_wdev = wil->p2p_wdev;
1481         if (p2p_wdev) {
1482                 wil->p2p_wdev = NULL;
1483                 wil->radio_wdev = wil_to_wdev(wil);
1484                 cfg80211_unregister_wdev(p2p_wdev);
1485                 kfree(p2p_wdev);
1486         }
1487         mutex_unlock(&wil->p2p_wdev_mutex);
1488 }