Linux 4.8
[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                 if (!wil->p2p.p2p_dev_started) {
382                         wil_err(wil, "P2P search requested on stopped P2P device\n");
383                         return -EIO;
384                 }
385                 wil->scan_request = request;
386                 wil->radio_wdev = wdev;
387                 rc = wil_p2p_search(wil, request);
388                 if (rc) {
389                         wil->radio_wdev = wil_to_wdev(wil);
390                         wil->scan_request = NULL;
391                 }
392                 return rc;
393         }
394
395         (void)wil_p2p_stop_discovery(wil);
396
397         wil_dbg_misc(wil, "Start scan_request 0x%p\n", request);
398         wil_dbg_misc(wil, "SSID count: %d", request->n_ssids);
399
400         for (i = 0; i < request->n_ssids; i++) {
401                 wil_dbg_misc(wil, "SSID[%d]", i);
402                 print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
403                                      request->ssids[i].ssid,
404                                      request->ssids[i].ssid_len);
405         }
406
407         if (request->n_ssids)
408                 rc = wmi_set_ssid(wil, request->ssids[0].ssid_len,
409                                   request->ssids[0].ssid);
410         else
411                 rc = wmi_set_ssid(wil, 0, NULL);
412
413         if (rc) {
414                 wil_err(wil, "set SSID for scan request failed: %d\n", rc);
415                 return rc;
416         }
417
418         wil->scan_request = request;
419         mod_timer(&wil->scan_timer, jiffies + WIL6210_SCAN_TO);
420
421         memset(&cmd, 0, sizeof(cmd));
422         cmd.cmd.scan_type = WMI_ACTIVE_SCAN;
423         cmd.cmd.num_channels = 0;
424         n = min(request->n_channels, 4U);
425         for (i = 0; i < n; i++) {
426                 int ch = request->channels[i]->hw_value;
427
428                 if (ch == 0) {
429                         wil_err(wil,
430                                 "Scan requested for unknown frequency %dMhz\n",
431                                 request->channels[i]->center_freq);
432                         continue;
433                 }
434                 /* 0-based channel indexes */
435                 cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
436                 wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
437                              request->channels[i]->center_freq);
438         }
439
440         if (request->ie_len)
441                 print_hex_dump_bytes("Scan IE ", DUMP_PREFIX_OFFSET,
442                                      request->ie, request->ie_len);
443         else
444                 wil_dbg_misc(wil, "Scan has no IE's\n");
445
446         rc = wmi_set_ie(wil, WMI_FRAME_PROBE_REQ, request->ie_len, request->ie);
447         if (rc)
448                 goto out;
449
450         if (wil->discovery_mode && cmd.cmd.scan_type == WMI_ACTIVE_SCAN) {
451                 cmd.cmd.discovery_mode = 1;
452                 wil_dbg_misc(wil, "active scan with discovery_mode=1\n");
453         }
454
455         wil->radio_wdev = wdev;
456         rc = wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
457                         cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
458
459 out:
460         if (rc) {
461                 del_timer_sync(&wil->scan_timer);
462                 wil->radio_wdev = wil_to_wdev(wil);
463                 wil->scan_request = NULL;
464         }
465
466         return rc;
467 }
468
469 static void wil_print_crypto(struct wil6210_priv *wil,
470                              struct cfg80211_crypto_settings *c)
471 {
472         int i, n;
473
474         wil_dbg_misc(wil, "WPA versions: 0x%08x cipher group 0x%08x\n",
475                      c->wpa_versions, c->cipher_group);
476         wil_dbg_misc(wil, "Pairwise ciphers [%d] {\n", c->n_ciphers_pairwise);
477         n = min_t(int, c->n_ciphers_pairwise, ARRAY_SIZE(c->ciphers_pairwise));
478         for (i = 0; i < n; i++)
479                 wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
480                              c->ciphers_pairwise[i]);
481         wil_dbg_misc(wil, "}\n");
482         wil_dbg_misc(wil, "AKM suites [%d] {\n", c->n_akm_suites);
483         n = min_t(int, c->n_akm_suites, ARRAY_SIZE(c->akm_suites));
484         for (i = 0; i < n; i++)
485                 wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
486                              c->akm_suites[i]);
487         wil_dbg_misc(wil, "}\n");
488         wil_dbg_misc(wil, "Control port : %d, eth_type 0x%04x no_encrypt %d\n",
489                      c->control_port, be16_to_cpu(c->control_port_ethertype),
490                      c->control_port_no_encrypt);
491 }
492
493 static void wil_print_connect_params(struct wil6210_priv *wil,
494                                      struct cfg80211_connect_params *sme)
495 {
496         wil_info(wil, "Connecting to:\n");
497         if (sme->channel) {
498                 wil_info(wil, "  Channel: %d freq %d\n",
499                          sme->channel->hw_value, sme->channel->center_freq);
500         }
501         if (sme->bssid)
502                 wil_info(wil, "  BSSID: %pM\n", sme->bssid);
503         if (sme->ssid)
504                 print_hex_dump(KERN_INFO, "  SSID: ", DUMP_PREFIX_OFFSET,
505                                16, 1, sme->ssid, sme->ssid_len, true);
506         wil_info(wil, "  Privacy: %s\n", sme->privacy ? "secure" : "open");
507         wil_info(wil, "  PBSS: %d\n", sme->pbss);
508         wil_print_crypto(wil, &sme->crypto);
509 }
510
511 static int wil_cfg80211_connect(struct wiphy *wiphy,
512                                 struct net_device *ndev,
513                                 struct cfg80211_connect_params *sme)
514 {
515         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
516         struct cfg80211_bss *bss;
517         struct wmi_connect_cmd conn;
518         const u8 *ssid_eid;
519         const u8 *rsn_eid;
520         int ch;
521         int rc = 0;
522         enum ieee80211_bss_type bss_type = IEEE80211_BSS_TYPE_ESS;
523
524         wil_dbg_misc(wil, "%s()\n", __func__);
525         wil_print_connect_params(wil, sme);
526
527         if (test_bit(wil_status_fwconnecting, wil->status) ||
528             test_bit(wil_status_fwconnected, wil->status))
529                 return -EALREADY;
530
531         if (sme->ie_len > WMI_MAX_IE_LEN) {
532                 wil_err(wil, "IE too large (%td bytes)\n", sme->ie_len);
533                 return -ERANGE;
534         }
535
536         rsn_eid = sme->ie ?
537                         cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
538                         NULL;
539         if (sme->privacy && !rsn_eid)
540                 wil_info(wil, "WSC connection\n");
541
542         if (sme->pbss)
543                 bss_type = IEEE80211_BSS_TYPE_PBSS;
544
545         bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
546                                sme->ssid, sme->ssid_len,
547                                bss_type, IEEE80211_PRIVACY_ANY);
548         if (!bss) {
549                 wil_err(wil, "Unable to find BSS\n");
550                 return -ENOENT;
551         }
552
553         ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
554         if (!ssid_eid) {
555                 wil_err(wil, "No SSID\n");
556                 rc = -ENOENT;
557                 goto out;
558         }
559         wil->privacy = sme->privacy;
560
561         if (wil->privacy) {
562                 /* For secure assoc, remove old keys */
563                 rc = wmi_del_cipher_key(wil, 0, bss->bssid,
564                                         WMI_KEY_USE_PAIRWISE);
565                 if (rc) {
566                         wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(PTK) failed\n");
567                         goto out;
568                 }
569                 rc = wmi_del_cipher_key(wil, 0, bss->bssid,
570                                         WMI_KEY_USE_RX_GROUP);
571                 if (rc) {
572                         wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(GTK) failed\n");
573                         goto out;
574                 }
575         }
576
577         /* WMI_SET_APPIE_CMD. ie may contain rsn info as well as other info
578          * elements. Send it also in case it's empty, to erase previously set
579          * ies in FW.
580          */
581         rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
582         if (rc)
583                 goto out;
584
585         /* WMI_CONNECT_CMD */
586         memset(&conn, 0, sizeof(conn));
587         switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
588         case WLAN_CAPABILITY_DMG_TYPE_AP:
589                 conn.network_type = WMI_NETTYPE_INFRA;
590                 break;
591         case WLAN_CAPABILITY_DMG_TYPE_PBSS:
592                 conn.network_type = WMI_NETTYPE_P2P;
593                 break;
594         default:
595                 wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
596                         bss->capability);
597                 goto out;
598         }
599         if (wil->privacy) {
600                 if (rsn_eid) { /* regular secure connection */
601                         conn.dot11_auth_mode = WMI_AUTH11_SHARED;
602                         conn.auth_mode = WMI_AUTH_WPA2_PSK;
603                         conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
604                         conn.pairwise_crypto_len = 16;
605                         conn.group_crypto_type = WMI_CRYPT_AES_GCMP;
606                         conn.group_crypto_len = 16;
607                 } else { /* WSC */
608                         conn.dot11_auth_mode = WMI_AUTH11_WSC;
609                         conn.auth_mode = WMI_AUTH_NONE;
610                 }
611         } else { /* insecure connection */
612                 conn.dot11_auth_mode = WMI_AUTH11_OPEN;
613                 conn.auth_mode = WMI_AUTH_NONE;
614         }
615
616         conn.ssid_len = min_t(u8, ssid_eid[1], 32);
617         memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
618
619         ch = bss->channel->hw_value;
620         if (ch == 0) {
621                 wil_err(wil, "BSS at unknown frequency %dMhz\n",
622                         bss->channel->center_freq);
623                 rc = -EOPNOTSUPP;
624                 goto out;
625         }
626         conn.channel = ch - 1;
627
628         ether_addr_copy(conn.bssid, bss->bssid);
629         ether_addr_copy(conn.dst_mac, bss->bssid);
630
631         set_bit(wil_status_fwconnecting, wil->status);
632
633         rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
634         if (rc == 0) {
635                 netif_carrier_on(ndev);
636                 /* Connect can take lots of time */
637                 mod_timer(&wil->connect_timer,
638                           jiffies + msecs_to_jiffies(2000));
639         } else {
640                 clear_bit(wil_status_fwconnecting, wil->status);
641         }
642
643  out:
644         cfg80211_put_bss(wiphy, bss);
645
646         return rc;
647 }
648
649 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
650                                    struct net_device *ndev,
651                                    u16 reason_code)
652 {
653         int rc;
654         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
655
656         wil_dbg_misc(wil, "%s(reason=%d)\n", __func__, reason_code);
657
658         if (!(test_bit(wil_status_fwconnecting, wil->status) ||
659               test_bit(wil_status_fwconnected, wil->status))) {
660                 wil_err(wil, "%s: Disconnect was called while disconnected\n",
661                         __func__);
662                 return 0;
663         }
664
665         rc = wmi_call(wil, WMI_DISCONNECT_CMDID, NULL, 0,
666                       WMI_DISCONNECT_EVENTID, NULL, 0,
667                       WIL6210_DISCONNECT_TO_MS);
668         if (rc)
669                 wil_err(wil, "%s: disconnect error %d\n", __func__, rc);
670
671         return rc;
672 }
673
674 int wil_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
675                          struct cfg80211_mgmt_tx_params *params,
676                          u64 *cookie)
677 {
678         const u8 *buf = params->buf;
679         size_t len = params->len;
680         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
681         int rc;
682         bool tx_status = false;
683         struct ieee80211_mgmt *mgmt_frame = (void *)buf;
684         struct wmi_sw_tx_req_cmd *cmd;
685         struct {
686                 struct wmi_cmd_hdr wmi;
687                 struct wmi_sw_tx_complete_event evt;
688         } __packed evt;
689
690         /* Note, currently we do not support the "wait" parameter, user-space
691          * must call remain_on_channel before mgmt_tx or listen on a channel
692          * another way (AP/PCP or connected station)
693          * in addition we need to check if specified "chan" argument is
694          * different from currently "listened" channel and fail if it is.
695          */
696
697         wil_dbg_misc(wil, "%s()\n", __func__);
698         print_hex_dump_bytes("mgmt tx frame ", DUMP_PREFIX_OFFSET, buf, len);
699
700         cmd = kmalloc(sizeof(*cmd) + len, GFP_KERNEL);
701         if (!cmd) {
702                 rc = -ENOMEM;
703                 goto out;
704         }
705
706         memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
707         cmd->len = cpu_to_le16(len);
708         memcpy(cmd->payload, buf, len);
709
710         rc = wmi_call(wil, WMI_SW_TX_REQ_CMDID, cmd, sizeof(*cmd) + len,
711                       WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
712         if (rc == 0)
713                 tx_status = !evt.evt.status;
714
715         kfree(cmd);
716  out:
717         cfg80211_mgmt_tx_status(wdev, cookie ? *cookie : 0, buf, len,
718                                 tx_status, GFP_KERNEL);
719         return rc;
720 }
721
722 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
723                                     struct cfg80211_chan_def *chandef)
724 {
725         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
726         struct wireless_dev *wdev = wil_to_wdev(wil);
727
728         wdev->preset_chandef = *chandef;
729
730         return 0;
731 }
732
733 static enum wmi_key_usage wil_detect_key_usage(struct wil6210_priv *wil,
734                                                bool pairwise)
735 {
736         struct wireless_dev *wdev = wil_to_wdev(wil);
737         enum wmi_key_usage rc;
738
739         if (pairwise) {
740                 rc = WMI_KEY_USE_PAIRWISE;
741         } else {
742                 switch (wdev->iftype) {
743                 case NL80211_IFTYPE_STATION:
744                 case NL80211_IFTYPE_P2P_CLIENT:
745                         rc = WMI_KEY_USE_RX_GROUP;
746                         break;
747                 case NL80211_IFTYPE_AP:
748                 case NL80211_IFTYPE_P2P_GO:
749                         rc = WMI_KEY_USE_TX_GROUP;
750                         break;
751                 default:
752                         /* TODO: Rx GTK or Tx GTK? */
753                         wil_err(wil, "Can't determine GTK type\n");
754                         rc = WMI_KEY_USE_RX_GROUP;
755                         break;
756                 }
757         }
758         wil_dbg_misc(wil, "%s() -> %s\n", __func__, key_usage_str[rc]);
759
760         return rc;
761 }
762
763 static struct wil_tid_crypto_rx_single *
764 wil_find_crypto_ctx(struct wil6210_priv *wil, u8 key_index,
765                     enum wmi_key_usage key_usage, const u8 *mac_addr)
766 {
767         int cid = -EINVAL;
768         int tid = 0;
769         struct wil_sta_info *s;
770         struct wil_tid_crypto_rx *c;
771
772         if (key_usage == WMI_KEY_USE_TX_GROUP)
773                 return NULL; /* not needed */
774
775         /* supplicant provides Rx group key in STA mode with NULL MAC address */
776         if (mac_addr)
777                 cid = wil_find_cid(wil, mac_addr);
778         else if (key_usage == WMI_KEY_USE_RX_GROUP)
779                 cid = wil_find_cid_by_idx(wil, 0);
780         if (cid < 0) {
781                 wil_err(wil, "No CID for %pM %s[%d]\n", mac_addr,
782                         key_usage_str[key_usage], key_index);
783                 return ERR_PTR(cid);
784         }
785
786         s = &wil->sta[cid];
787         if (key_usage == WMI_KEY_USE_PAIRWISE)
788                 c = &s->tid_crypto_rx[tid];
789         else
790                 c = &s->group_crypto_rx;
791
792         return &c->key_id[key_index];
793 }
794
795 static int wil_cfg80211_add_key(struct wiphy *wiphy,
796                                 struct net_device *ndev,
797                                 u8 key_index, bool pairwise,
798                                 const u8 *mac_addr,
799                                 struct key_params *params)
800 {
801         int rc;
802         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
803         enum wmi_key_usage key_usage = wil_detect_key_usage(wil, pairwise);
804         struct wil_tid_crypto_rx_single *cc = wil_find_crypto_ctx(wil,
805                                                                   key_index,
806                                                                   key_usage,
807                                                                   mac_addr);
808
809         wil_dbg_misc(wil, "%s(%pM %s[%d] PN %*phN)\n", __func__,
810                      mac_addr, key_usage_str[key_usage], key_index,
811                      params->seq_len, params->seq);
812
813         if (IS_ERR(cc)) {
814                 wil_err(wil, "Not connected, %s(%pM %s[%d] PN %*phN)\n",
815                         __func__, mac_addr, key_usage_str[key_usage], key_index,
816                         params->seq_len, params->seq);
817                 return -EINVAL;
818         }
819
820         if (cc)
821                 cc->key_set = false;
822
823         if (params->seq && params->seq_len != IEEE80211_GCMP_PN_LEN) {
824                 wil_err(wil,
825                         "Wrong PN len %d, %s(%pM %s[%d] PN %*phN)\n",
826                         params->seq_len, __func__, mac_addr,
827                         key_usage_str[key_usage], key_index,
828                         params->seq_len, params->seq);
829                 return -EINVAL;
830         }
831
832         rc = wmi_add_cipher_key(wil, key_index, mac_addr, params->key_len,
833                                 params->key, key_usage);
834         if ((rc == 0) && cc) {
835                 if (params->seq)
836                         memcpy(cc->pn, params->seq, IEEE80211_GCMP_PN_LEN);
837                 else
838                         memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
839                 cc->key_set = true;
840         }
841
842         return rc;
843 }
844
845 static int wil_cfg80211_del_key(struct wiphy *wiphy,
846                                 struct net_device *ndev,
847                                 u8 key_index, bool pairwise,
848                                 const u8 *mac_addr)
849 {
850         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
851         enum wmi_key_usage key_usage = wil_detect_key_usage(wil, pairwise);
852         struct wil_tid_crypto_rx_single *cc = wil_find_crypto_ctx(wil,
853                                                                   key_index,
854                                                                   key_usage,
855                                                                   mac_addr);
856
857         wil_dbg_misc(wil, "%s(%pM %s[%d])\n", __func__, mac_addr,
858                      key_usage_str[key_usage], key_index);
859
860         if (IS_ERR(cc))
861                 wil_info(wil, "Not connected, %s(%pM %s[%d])\n", __func__,
862                          mac_addr, key_usage_str[key_usage], key_index);
863
864         if (!IS_ERR_OR_NULL(cc))
865                 cc->key_set = false;
866
867         return wmi_del_cipher_key(wil, key_index, mac_addr, key_usage);
868 }
869
870 /* Need to be present or wiphy_new() will WARN */
871 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
872                                         struct net_device *ndev,
873                                         u8 key_index, bool unicast,
874                                         bool multicast)
875 {
876         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
877
878         wil_dbg_misc(wil, "%s: entered\n", __func__);
879         return 0;
880 }
881
882 static int wil_remain_on_channel(struct wiphy *wiphy,
883                                  struct wireless_dev *wdev,
884                                  struct ieee80211_channel *chan,
885                                  unsigned int duration,
886                                  u64 *cookie)
887 {
888         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
889         int rc;
890
891         wil_dbg_misc(wil, "%s() center_freq=%d, duration=%d iftype=%d\n",
892                      __func__, chan->center_freq, duration, wdev->iftype);
893
894         rc = wil_p2p_listen(wil, duration, chan, cookie);
895         if (rc)
896                 return rc;
897
898         wil->radio_wdev = wdev;
899
900         cfg80211_ready_on_channel(wdev, *cookie, chan, duration,
901                                   GFP_KERNEL);
902
903         return 0;
904 }
905
906 static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
907                                         struct wireless_dev *wdev,
908                                         u64 cookie)
909 {
910         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
911
912         wil_dbg_misc(wil, "%s()\n", __func__);
913
914         return wil_p2p_cancel_listen(wil, cookie);
915 }
916
917 /**
918  * find a specific IE in a list of IEs
919  * return a pointer to the beginning of IE in the list
920  * or NULL if not found
921  */
922 static const u8 *_wil_cfg80211_find_ie(const u8 *ies, u16 ies_len, const u8 *ie,
923                                        u16 ie_len)
924 {
925         struct ieee80211_vendor_ie *vie;
926         u32 oui;
927
928         /* IE tag at offset 0, length at offset 1 */
929         if (ie_len < 2 || 2 + ie[1] > ie_len)
930                 return NULL;
931
932         if (ie[0] != WLAN_EID_VENDOR_SPECIFIC)
933                 return cfg80211_find_ie(ie[0], ies, ies_len);
934
935         /* make sure there is room for 3 bytes OUI + 1 byte OUI type */
936         if (ie[1] < 4)
937                 return NULL;
938         vie = (struct ieee80211_vendor_ie *)ie;
939         oui = vie->oui[0] << 16 | vie->oui[1] << 8 | vie->oui[2];
940         return cfg80211_find_vendor_ie(oui, vie->oui_type, ies,
941                                        ies_len);
942 }
943
944 /**
945  * merge the IEs in two lists into a single list.
946  * do not include IEs from the second list which exist in the first list.
947  * add only vendor specific IEs from second list to keep
948  * the merged list sorted (since vendor-specific IE has the
949  * highest tag number)
950  * caller must free the allocated memory for merged IEs
951  */
952 static int _wil_cfg80211_merge_extra_ies(const u8 *ies1, u16 ies1_len,
953                                          const u8 *ies2, u16 ies2_len,
954                                          u8 **merged_ies, u16 *merged_len)
955 {
956         u8 *buf, *dpos;
957         const u8 *spos;
958
959         if (ies1_len == 0 && ies2_len == 0) {
960                 *merged_ies = NULL;
961                 *merged_len = 0;
962                 return 0;
963         }
964
965         buf = kmalloc(ies1_len + ies2_len, GFP_KERNEL);
966         if (!buf)
967                 return -ENOMEM;
968         memcpy(buf, ies1, ies1_len);
969         dpos = buf + ies1_len;
970         spos = ies2;
971         while (spos + 1 < ies2 + ies2_len) {
972                 /* IE tag at offset 0, length at offset 1 */
973                 u16 ielen = 2 + spos[1];
974
975                 if (spos + ielen > ies2 + ies2_len)
976                         break;
977                 if (spos[0] == WLAN_EID_VENDOR_SPECIFIC &&
978                     !_wil_cfg80211_find_ie(ies1, ies1_len, spos, ielen)) {
979                         memcpy(dpos, spos, ielen);
980                         dpos += ielen;
981                 }
982                 spos += ielen;
983         }
984
985         *merged_ies = buf;
986         *merged_len = dpos - buf;
987         return 0;
988 }
989
990 static void wil_print_bcon_data(struct cfg80211_beacon_data *b)
991 {
992         print_hex_dump_bytes("head     ", DUMP_PREFIX_OFFSET,
993                              b->head, b->head_len);
994         print_hex_dump_bytes("tail     ", DUMP_PREFIX_OFFSET,
995                              b->tail, b->tail_len);
996         print_hex_dump_bytes("BCON IE  ", DUMP_PREFIX_OFFSET,
997                              b->beacon_ies, b->beacon_ies_len);
998         print_hex_dump_bytes("PROBE    ", DUMP_PREFIX_OFFSET,
999                              b->probe_resp, b->probe_resp_len);
1000         print_hex_dump_bytes("PROBE IE ", DUMP_PREFIX_OFFSET,
1001                              b->proberesp_ies, b->proberesp_ies_len);
1002         print_hex_dump_bytes("ASSOC IE ", DUMP_PREFIX_OFFSET,
1003                              b->assocresp_ies, b->assocresp_ies_len);
1004 }
1005
1006 /* internal functions for device reset and starting AP */
1007 static int _wil_cfg80211_set_ies(struct wiphy *wiphy,
1008                                  struct cfg80211_beacon_data *bcon)
1009 {
1010         int rc;
1011         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1012         u16 len = 0, proberesp_len = 0;
1013         u8 *ies = NULL, *proberesp = NULL;
1014
1015         if (bcon->probe_resp) {
1016                 struct ieee80211_mgmt *f =
1017                         (struct ieee80211_mgmt *)bcon->probe_resp;
1018                 size_t hlen = offsetof(struct ieee80211_mgmt,
1019                                        u.probe_resp.variable);
1020                 proberesp = f->u.probe_resp.variable;
1021                 proberesp_len = bcon->probe_resp_len - hlen;
1022         }
1023         rc = _wil_cfg80211_merge_extra_ies(proberesp,
1024                                            proberesp_len,
1025                                            bcon->proberesp_ies,
1026                                            bcon->proberesp_ies_len,
1027                                            &ies, &len);
1028
1029         if (rc)
1030                 goto out;
1031
1032         rc = wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, len, ies);
1033         if (rc)
1034                 goto out;
1035
1036         if (bcon->assocresp_ies)
1037                 rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP,
1038                                 bcon->assocresp_ies_len, bcon->assocresp_ies);
1039         else
1040                 rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, len, ies);
1041 #if 0 /* to use beacon IE's, remove this #if 0 */
1042         if (rc)
1043                 goto out;
1044
1045         rc = wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->tail_len, bcon->tail);
1046 #endif
1047 out:
1048         kfree(ies);
1049         return rc;
1050 }
1051
1052 static int _wil_cfg80211_start_ap(struct wiphy *wiphy,
1053                                   struct net_device *ndev,
1054                                   const u8 *ssid, size_t ssid_len, u32 privacy,
1055                                   int bi, u8 chan,
1056                                   struct cfg80211_beacon_data *bcon,
1057                                   u8 hidden_ssid, u32 pbss)
1058 {
1059         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1060         int rc;
1061         struct wireless_dev *wdev = ndev->ieee80211_ptr;
1062         u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
1063         u8 is_go = (wdev->iftype == NL80211_IFTYPE_P2P_GO);
1064
1065         if (pbss)
1066                 wmi_nettype = WMI_NETTYPE_P2P;
1067
1068         wil_dbg_misc(wil, "%s: is_go=%d\n", __func__, is_go);
1069         if (is_go && !pbss) {
1070                 wil_err(wil, "%s: P2P GO must be in PBSS\n", __func__);
1071                 return -ENOTSUPP;
1072         }
1073
1074         wil_set_recovery_state(wil, fw_recovery_idle);
1075
1076         mutex_lock(&wil->mutex);
1077
1078         __wil_down(wil);
1079         rc = __wil_up(wil);
1080         if (rc)
1081                 goto out;
1082
1083         rc = wmi_set_ssid(wil, ssid_len, ssid);
1084         if (rc)
1085                 goto out;
1086
1087         rc = _wil_cfg80211_set_ies(wiphy, bcon);
1088         if (rc)
1089                 goto out;
1090
1091         wil->privacy = privacy;
1092         wil->channel = chan;
1093         wil->hidden_ssid = hidden_ssid;
1094         wil->pbss = pbss;
1095
1096         netif_carrier_on(ndev);
1097
1098         rc = wmi_pcp_start(wil, bi, wmi_nettype, chan, hidden_ssid, is_go);
1099         if (rc)
1100                 goto err_pcp_start;
1101
1102         rc = wil_bcast_init(wil);
1103         if (rc)
1104                 goto err_bcast;
1105
1106         goto out; /* success */
1107
1108 err_bcast:
1109         wmi_pcp_stop(wil);
1110 err_pcp_start:
1111         netif_carrier_off(ndev);
1112 out:
1113         mutex_unlock(&wil->mutex);
1114         return rc;
1115 }
1116
1117 static int wil_cfg80211_change_beacon(struct wiphy *wiphy,
1118                                       struct net_device *ndev,
1119                                       struct cfg80211_beacon_data *bcon)
1120 {
1121         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1122         int rc;
1123         u32 privacy = 0;
1124
1125         wil_dbg_misc(wil, "%s()\n", __func__);
1126         wil_print_bcon_data(bcon);
1127
1128         if (bcon->tail &&
1129             cfg80211_find_ie(WLAN_EID_RSN, bcon->tail,
1130                              bcon->tail_len))
1131                 privacy = 1;
1132
1133         /* in case privacy has changed, need to restart the AP */
1134         if (wil->privacy != privacy) {
1135                 struct wireless_dev *wdev = ndev->ieee80211_ptr;
1136
1137                 wil_dbg_misc(wil, "privacy changed %d=>%d. Restarting AP\n",
1138                              wil->privacy, privacy);
1139
1140                 rc = _wil_cfg80211_start_ap(wiphy, ndev, wdev->ssid,
1141                                             wdev->ssid_len, privacy,
1142                                             wdev->beacon_interval,
1143                                             wil->channel, bcon,
1144                                             wil->hidden_ssid,
1145                                             wil->pbss);
1146         } else {
1147                 rc = _wil_cfg80211_set_ies(wiphy, bcon);
1148         }
1149
1150         return rc;
1151 }
1152
1153 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
1154                                  struct net_device *ndev,
1155                                  struct cfg80211_ap_settings *info)
1156 {
1157         int rc;
1158         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1159         struct ieee80211_channel *channel = info->chandef.chan;
1160         struct cfg80211_beacon_data *bcon = &info->beacon;
1161         struct cfg80211_crypto_settings *crypto = &info->crypto;
1162         u8 hidden_ssid;
1163
1164         wil_dbg_misc(wil, "%s()\n", __func__);
1165
1166         if (!channel) {
1167                 wil_err(wil, "AP: No channel???\n");
1168                 return -EINVAL;
1169         }
1170
1171         switch (info->hidden_ssid) {
1172         case NL80211_HIDDEN_SSID_NOT_IN_USE:
1173                 hidden_ssid = WMI_HIDDEN_SSID_DISABLED;
1174                 break;
1175
1176         case NL80211_HIDDEN_SSID_ZERO_LEN:
1177                 hidden_ssid = WMI_HIDDEN_SSID_SEND_EMPTY;
1178                 break;
1179
1180         case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1181                 hidden_ssid = WMI_HIDDEN_SSID_CLEAR;
1182                 break;
1183
1184         default:
1185                 wil_err(wil, "AP: Invalid hidden SSID %d\n", info->hidden_ssid);
1186                 return -EOPNOTSUPP;
1187         }
1188         wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
1189                      channel->center_freq, info->privacy ? "secure" : "open");
1190         wil_dbg_misc(wil, "Privacy: %d auth_type %d\n",
1191                      info->privacy, info->auth_type);
1192         wil_dbg_misc(wil, "Hidden SSID mode: %d\n",
1193                      info->hidden_ssid);
1194         wil_dbg_misc(wil, "BI %d DTIM %d\n", info->beacon_interval,
1195                      info->dtim_period);
1196         wil_dbg_misc(wil, "PBSS %d\n", info->pbss);
1197         print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
1198                              info->ssid, info->ssid_len);
1199         wil_print_bcon_data(bcon);
1200         wil_print_crypto(wil, crypto);
1201
1202         rc = _wil_cfg80211_start_ap(wiphy, ndev,
1203                                     info->ssid, info->ssid_len, info->privacy,
1204                                     info->beacon_interval, channel->hw_value,
1205                                     bcon, hidden_ssid, info->pbss);
1206
1207         return rc;
1208 }
1209
1210 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
1211                                 struct net_device *ndev)
1212 {
1213         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1214
1215         wil_dbg_misc(wil, "%s()\n", __func__);
1216
1217         netif_carrier_off(ndev);
1218         wil_set_recovery_state(wil, fw_recovery_idle);
1219
1220         mutex_lock(&wil->mutex);
1221
1222         wmi_pcp_stop(wil);
1223
1224         __wil_down(wil);
1225
1226         mutex_unlock(&wil->mutex);
1227
1228         return 0;
1229 }
1230
1231 static int wil_cfg80211_del_station(struct wiphy *wiphy,
1232                                     struct net_device *dev,
1233                                     struct station_del_parameters *params)
1234 {
1235         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1236
1237         wil_dbg_misc(wil, "%s(%pM, reason=%d)\n", __func__, params->mac,
1238                      params->reason_code);
1239
1240         mutex_lock(&wil->mutex);
1241         wil6210_disconnect(wil, params->mac, params->reason_code, false);
1242         mutex_unlock(&wil->mutex);
1243
1244         return 0;
1245 }
1246
1247 /* probe_client handling */
1248 static void wil_probe_client_handle(struct wil6210_priv *wil,
1249                                     struct wil_probe_client_req *req)
1250 {
1251         struct net_device *ndev = wil_to_ndev(wil);
1252         struct wil_sta_info *sta = &wil->sta[req->cid];
1253         /* assume STA is alive if it is still connected,
1254          * else FW will disconnect it
1255          */
1256         bool alive = (sta->status == wil_sta_connected);
1257
1258         cfg80211_probe_status(ndev, sta->addr, req->cookie, alive, GFP_KERNEL);
1259 }
1260
1261 static struct list_head *next_probe_client(struct wil6210_priv *wil)
1262 {
1263         struct list_head *ret = NULL;
1264
1265         mutex_lock(&wil->probe_client_mutex);
1266
1267         if (!list_empty(&wil->probe_client_pending)) {
1268                 ret = wil->probe_client_pending.next;
1269                 list_del(ret);
1270         }
1271
1272         mutex_unlock(&wil->probe_client_mutex);
1273
1274         return ret;
1275 }
1276
1277 void wil_probe_client_worker(struct work_struct *work)
1278 {
1279         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
1280                                                 probe_client_worker);
1281         struct wil_probe_client_req *req;
1282         struct list_head *lh;
1283
1284         while ((lh = next_probe_client(wil)) != NULL) {
1285                 req = list_entry(lh, struct wil_probe_client_req, list);
1286
1287                 wil_probe_client_handle(wil, req);
1288                 kfree(req);
1289         }
1290 }
1291
1292 void wil_probe_client_flush(struct wil6210_priv *wil)
1293 {
1294         struct wil_probe_client_req *req, *t;
1295
1296         wil_dbg_misc(wil, "%s()\n", __func__);
1297
1298         mutex_lock(&wil->probe_client_mutex);
1299
1300         list_for_each_entry_safe(req, t, &wil->probe_client_pending, list) {
1301                 list_del(&req->list);
1302                 kfree(req);
1303         }
1304
1305         mutex_unlock(&wil->probe_client_mutex);
1306 }
1307
1308 static int wil_cfg80211_probe_client(struct wiphy *wiphy,
1309                                      struct net_device *dev,
1310                                      const u8 *peer, u64 *cookie)
1311 {
1312         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1313         struct wil_probe_client_req *req;
1314         int cid = wil_find_cid(wil, peer);
1315
1316         wil_dbg_misc(wil, "%s(%pM => CID %d)\n", __func__, peer, cid);
1317
1318         if (cid < 0)
1319                 return -ENOLINK;
1320
1321         req = kzalloc(sizeof(*req), GFP_KERNEL);
1322         if (!req)
1323                 return -ENOMEM;
1324
1325         req->cid = cid;
1326         req->cookie = cid;
1327
1328         mutex_lock(&wil->probe_client_mutex);
1329         list_add_tail(&req->list, &wil->probe_client_pending);
1330         mutex_unlock(&wil->probe_client_mutex);
1331
1332         *cookie = req->cookie;
1333         queue_work(wil->wq_service, &wil->probe_client_worker);
1334         return 0;
1335 }
1336
1337 static int wil_cfg80211_change_bss(struct wiphy *wiphy,
1338                                    struct net_device *dev,
1339                                    struct bss_parameters *params)
1340 {
1341         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1342
1343         if (params->ap_isolate >= 0) {
1344                 wil_dbg_misc(wil, "%s(ap_isolate %d => %d)\n", __func__,
1345                              wil->ap_isolate, params->ap_isolate);
1346                 wil->ap_isolate = params->ap_isolate;
1347         }
1348
1349         return 0;
1350 }
1351
1352 static int wil_cfg80211_start_p2p_device(struct wiphy *wiphy,
1353                                          struct wireless_dev *wdev)
1354 {
1355         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1356
1357         wil_dbg_misc(wil, "%s: entered\n", __func__);
1358         wil->p2p.p2p_dev_started = 1;
1359         return 0;
1360 }
1361
1362 static void wil_cfg80211_stop_p2p_device(struct wiphy *wiphy,
1363                                          struct wireless_dev *wdev)
1364 {
1365         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1366         u8 started;
1367
1368         wil_dbg_misc(wil, "%s: entered\n", __func__);
1369         mutex_lock(&wil->mutex);
1370         started = wil_p2p_stop_discovery(wil);
1371         if (started && wil->scan_request) {
1372                 struct cfg80211_scan_info info = {
1373                         .aborted = true,
1374                 };
1375
1376                 cfg80211_scan_done(wil->scan_request, &info);
1377                 wil->scan_request = NULL;
1378                 wil->radio_wdev = wil->wdev;
1379         }
1380         mutex_unlock(&wil->mutex);
1381
1382         wil->p2p.p2p_dev_started = 0;
1383 }
1384
1385 static struct cfg80211_ops wil_cfg80211_ops = {
1386         .add_virtual_intf = wil_cfg80211_add_iface,
1387         .del_virtual_intf = wil_cfg80211_del_iface,
1388         .scan = wil_cfg80211_scan,
1389         .connect = wil_cfg80211_connect,
1390         .disconnect = wil_cfg80211_disconnect,
1391         .change_virtual_intf = wil_cfg80211_change_iface,
1392         .get_station = wil_cfg80211_get_station,
1393         .dump_station = wil_cfg80211_dump_station,
1394         .remain_on_channel = wil_remain_on_channel,
1395         .cancel_remain_on_channel = wil_cancel_remain_on_channel,
1396         .mgmt_tx = wil_cfg80211_mgmt_tx,
1397         .set_monitor_channel = wil_cfg80211_set_channel,
1398         .add_key = wil_cfg80211_add_key,
1399         .del_key = wil_cfg80211_del_key,
1400         .set_default_key = wil_cfg80211_set_default_key,
1401         /* AP mode */
1402         .change_beacon = wil_cfg80211_change_beacon,
1403         .start_ap = wil_cfg80211_start_ap,
1404         .stop_ap = wil_cfg80211_stop_ap,
1405         .del_station = wil_cfg80211_del_station,
1406         .probe_client = wil_cfg80211_probe_client,
1407         .change_bss = wil_cfg80211_change_bss,
1408         /* P2P device */
1409         .start_p2p_device = wil_cfg80211_start_p2p_device,
1410         .stop_p2p_device = wil_cfg80211_stop_p2p_device,
1411 };
1412
1413 static void wil_wiphy_init(struct wiphy *wiphy)
1414 {
1415         wiphy->max_scan_ssids = 1;
1416         wiphy->max_scan_ie_len = WMI_MAX_IE_LEN;
1417         wiphy->max_remain_on_channel_duration = WIL_MAX_ROC_DURATION_MS;
1418         wiphy->max_num_pmkids = 0 /* TODO: */;
1419         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1420                                  BIT(NL80211_IFTYPE_AP) |
1421                                  BIT(NL80211_IFTYPE_P2P_CLIENT) |
1422                                  BIT(NL80211_IFTYPE_P2P_GO) |
1423                                  BIT(NL80211_IFTYPE_P2P_DEVICE) |
1424                                  BIT(NL80211_IFTYPE_MONITOR);
1425         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
1426                         WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
1427                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
1428         dev_dbg(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
1429                 __func__, wiphy->flags);
1430         wiphy->probe_resp_offload =
1431                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
1432                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
1433                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
1434
1435         wiphy->bands[NL80211_BAND_60GHZ] = &wil_band_60ghz;
1436
1437         /* TODO: figure this out */
1438         wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
1439
1440         wiphy->cipher_suites = wil_cipher_suites;
1441         wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
1442         wiphy->mgmt_stypes = wil_mgmt_stypes;
1443         wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
1444 }
1445
1446 struct wireless_dev *wil_cfg80211_init(struct device *dev)
1447 {
1448         int rc = 0;
1449         struct wireless_dev *wdev;
1450
1451         dev_dbg(dev, "%s()\n", __func__);
1452
1453         wdev = kzalloc(sizeof(*wdev), GFP_KERNEL);
1454         if (!wdev)
1455                 return ERR_PTR(-ENOMEM);
1456
1457         wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
1458                                 sizeof(struct wil6210_priv));
1459         if (!wdev->wiphy) {
1460                 rc = -ENOMEM;
1461                 goto out;
1462         }
1463
1464         set_wiphy_dev(wdev->wiphy, dev);
1465         wil_wiphy_init(wdev->wiphy);
1466
1467         rc = wiphy_register(wdev->wiphy);
1468         if (rc < 0)
1469                 goto out_failed_reg;
1470
1471         return wdev;
1472
1473 out_failed_reg:
1474         wiphy_free(wdev->wiphy);
1475 out:
1476         kfree(wdev);
1477
1478         return ERR_PTR(rc);
1479 }
1480
1481 void wil_wdev_free(struct wil6210_priv *wil)
1482 {
1483         struct wireless_dev *wdev = wil_to_wdev(wil);
1484
1485         dev_dbg(wil_to_dev(wil), "%s()\n", __func__);
1486
1487         if (!wdev)
1488                 return;
1489
1490         wiphy_unregister(wdev->wiphy);
1491         wiphy_free(wdev->wiphy);
1492         kfree(wdev);
1493 }
1494
1495 void wil_p2p_wdev_free(struct wil6210_priv *wil)
1496 {
1497         struct wireless_dev *p2p_wdev;
1498
1499         mutex_lock(&wil->p2p_wdev_mutex);
1500         p2p_wdev = wil->p2p_wdev;
1501         if (p2p_wdev) {
1502                 wil->p2p_wdev = NULL;
1503                 wil->radio_wdev = wil_to_wdev(wil);
1504                 cfg80211_unregister_wdev(p2p_wdev);
1505                 kfree(p2p_wdev);
1506         }
1507         mutex_unlock(&wil->p2p_wdev_mutex);
1508 }