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