]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/ath/wil6210/cfg80211.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless
[karo-tx-linux.git] / drivers / net / wireless / ath / wil6210 / cfg80211.c
1 /*
2  * Copyright (c) 2012-2014 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 "wil6210.h"
18 #include "wmi.h"
19
20 #define CHAN60G(_channel, _flags) {                             \
21         .band                   = IEEE80211_BAND_60GHZ,         \
22         .center_freq            = 56160 + (2160 * (_channel)),  \
23         .hw_value               = (_channel),                   \
24         .flags                  = (_flags),                     \
25         .max_antenna_gain       = 0,                            \
26         .max_power              = 40,                           \
27 }
28
29 static struct ieee80211_channel wil_60ghz_channels[] = {
30         CHAN60G(1, 0),
31         CHAN60G(2, 0),
32         CHAN60G(3, 0),
33 /* channel 4 not supported yet */
34 };
35
36 static struct ieee80211_supported_band wil_band_60ghz = {
37         .channels = wil_60ghz_channels,
38         .n_channels = ARRAY_SIZE(wil_60ghz_channels),
39         .ht_cap = {
40                 .ht_supported = true,
41                 .cap = 0, /* TODO */
42                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
43                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
44                 .mcs = {
45                                 /* MCS 1..12 - SC PHY */
46                         .rx_mask = {0xfe, 0x1f}, /* 1..12 */
47                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
48                 },
49         },
50 };
51
52 static const struct ieee80211_txrx_stypes
53 wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
54         [NL80211_IFTYPE_STATION] = {
55                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
56                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
57                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
58                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
59         },
60         [NL80211_IFTYPE_AP] = {
61                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
62                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
63                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
64                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
65         },
66         [NL80211_IFTYPE_P2P_CLIENT] = {
67                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
68                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
69                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
70                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
71         },
72         [NL80211_IFTYPE_P2P_GO] = {
73                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
74                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
75                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
76                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
77         },
78 };
79
80 static const u32 wil_cipher_suites[] = {
81         WLAN_CIPHER_SUITE_GCMP,
82 };
83
84 int wil_iftype_nl2wmi(enum nl80211_iftype type)
85 {
86         static const struct {
87                 enum nl80211_iftype nl;
88                 enum wmi_network_type wmi;
89         } __nl2wmi[] = {
90                 {NL80211_IFTYPE_ADHOC,          WMI_NETTYPE_ADHOC},
91                 {NL80211_IFTYPE_STATION,        WMI_NETTYPE_INFRA},
92                 {NL80211_IFTYPE_AP,             WMI_NETTYPE_AP},
93                 {NL80211_IFTYPE_P2P_CLIENT,     WMI_NETTYPE_P2P},
94                 {NL80211_IFTYPE_P2P_GO,         WMI_NETTYPE_P2P},
95                 {NL80211_IFTYPE_MONITOR,        WMI_NETTYPE_ADHOC}, /* FIXME */
96         };
97         uint i;
98
99         for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
100                 if (__nl2wmi[i].nl == type)
101                         return __nl2wmi[i].wmi;
102         }
103
104         return -EOPNOTSUPP;
105 }
106
107 int wil_cid_fill_sinfo(struct wil6210_priv *wil, int cid,
108                        struct station_info *sinfo)
109 {
110         struct wmi_notify_req_cmd cmd = {
111                 .cid = cid,
112                 .interval_usec = 0,
113         };
114         struct {
115                 struct wil6210_mbox_hdr_wmi wmi;
116                 struct wmi_notify_req_done_event evt;
117         } __packed reply;
118         struct wil_net_stats *stats = &wil->sta[cid].stats;
119         int rc;
120
121         rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
122                       WMI_NOTIFY_REQ_DONE_EVENTID, &reply, sizeof(reply), 20);
123         if (rc)
124                 return rc;
125
126         wil_dbg_wmi(wil, "Link status for CID %d: {\n"
127                     "  MCS %d TSF 0x%016llx\n"
128                     "  BF status 0x%08x SNR 0x%08x SQI %d%%\n"
129                     "  Tx Tpt %d goodput %d Rx goodput %d\n"
130                     "  Sectors(rx:tx) my %d:%d peer %d:%d\n""}\n",
131                     cid, le16_to_cpu(reply.evt.bf_mcs),
132                     le64_to_cpu(reply.evt.tsf), reply.evt.status,
133                     le32_to_cpu(reply.evt.snr_val),
134                     reply.evt.sqi,
135                     le32_to_cpu(reply.evt.tx_tpt),
136                     le32_to_cpu(reply.evt.tx_goodput),
137                     le32_to_cpu(reply.evt.rx_goodput),
138                     le16_to_cpu(reply.evt.my_rx_sector),
139                     le16_to_cpu(reply.evt.my_tx_sector),
140                     le16_to_cpu(reply.evt.other_rx_sector),
141                     le16_to_cpu(reply.evt.other_tx_sector));
142
143         sinfo->generation = wil->sinfo_gen;
144
145         sinfo->filled = STATION_INFO_RX_BYTES |
146                         STATION_INFO_TX_BYTES |
147                         STATION_INFO_RX_PACKETS |
148                         STATION_INFO_TX_PACKETS |
149                         STATION_INFO_RX_BITRATE |
150                         STATION_INFO_TX_BITRATE |
151                         STATION_INFO_RX_DROP_MISC |
152                         STATION_INFO_TX_FAILED;
153
154         sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
155         sinfo->txrate.mcs = le16_to_cpu(reply.evt.bf_mcs);
156         sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
157         sinfo->rxrate.mcs = stats->last_mcs_rx;
158         sinfo->rx_bytes = stats->rx_bytes;
159         sinfo->rx_packets = stats->rx_packets;
160         sinfo->rx_dropped_misc = stats->rx_dropped;
161         sinfo->tx_bytes = stats->tx_bytes;
162         sinfo->tx_packets = stats->tx_packets;
163         sinfo->tx_failed = stats->tx_errors;
164
165         if (test_bit(wil_status_fwconnected, &wil->status)) {
166                 sinfo->filled |= STATION_INFO_SIGNAL;
167                 sinfo->signal = reply.evt.sqi;
168         }
169
170         return rc;
171 }
172
173 static int wil_cfg80211_get_station(struct wiphy *wiphy,
174                                     struct net_device *ndev,
175                                     const u8 *mac, struct station_info *sinfo)
176 {
177         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
178         int rc;
179
180         int cid = wil_find_cid(wil, mac);
181
182         wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
183         if (cid < 0)
184                 return cid;
185
186         rc = wil_cid_fill_sinfo(wil, cid, sinfo);
187
188         return rc;
189 }
190
191 /*
192  * Find @idx-th active STA for station dump.
193  */
194 static int wil_find_cid_by_idx(struct wil6210_priv *wil, int idx)
195 {
196         int i;
197
198         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
199                 if (wil->sta[i].status == wil_sta_unused)
200                         continue;
201                 if (idx == 0)
202                         return i;
203                 idx--;
204         }
205
206         return -ENOENT;
207 }
208
209 static int wil_cfg80211_dump_station(struct wiphy *wiphy,
210                                      struct net_device *dev, int idx,
211                                      u8 *mac, struct station_info *sinfo)
212 {
213         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
214         int rc;
215         int cid = wil_find_cid_by_idx(wil, idx);
216
217         if (cid < 0)
218                 return -ENOENT;
219
220         memcpy(mac, wil->sta[cid].addr, ETH_ALEN);
221         wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
222
223         rc = wil_cid_fill_sinfo(wil, cid, sinfo);
224
225         return rc;
226 }
227
228 static int wil_cfg80211_change_iface(struct wiphy *wiphy,
229                                      struct net_device *ndev,
230                                      enum nl80211_iftype type, u32 *flags,
231                                      struct vif_params *params)
232 {
233         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
234         struct wireless_dev *wdev = wil->wdev;
235
236         switch (type) {
237         case NL80211_IFTYPE_STATION:
238         case NL80211_IFTYPE_AP:
239         case NL80211_IFTYPE_P2P_CLIENT:
240         case NL80211_IFTYPE_P2P_GO:
241                 break;
242         case NL80211_IFTYPE_MONITOR:
243                 if (flags)
244                         wil->monitor_flags = *flags;
245                 else
246                         wil->monitor_flags = 0;
247
248                 break;
249         default:
250                 return -EOPNOTSUPP;
251         }
252
253         wdev->iftype = type;
254
255         return 0;
256 }
257
258 static int wil_cfg80211_scan(struct wiphy *wiphy,
259                              struct cfg80211_scan_request *request)
260 {
261         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
262         struct wireless_dev *wdev = wil->wdev;
263         struct {
264                 struct wmi_start_scan_cmd cmd;
265                 u16 chnl[4];
266         } __packed cmd;
267         uint i, n;
268         int rc;
269
270         if (wil->scan_request) {
271                 wil_err(wil, "Already scanning\n");
272                 return -EAGAIN;
273         }
274
275         /* check we are client side */
276         switch (wdev->iftype) {
277         case NL80211_IFTYPE_STATION:
278         case NL80211_IFTYPE_P2P_CLIENT:
279                 break;
280         default:
281                 return -EOPNOTSUPP;
282         }
283
284         /* FW don't support scan after connection attempt */
285         if (test_bit(wil_status_dontscan, &wil->status)) {
286                 wil_err(wil, "Can't scan now\n");
287                 return -EBUSY;
288         }
289
290         wil_dbg_misc(wil, "Start scan_request 0x%p\n", request);
291         wil->scan_request = request;
292         mod_timer(&wil->scan_timer, jiffies + WIL6210_SCAN_TO);
293
294         memset(&cmd, 0, sizeof(cmd));
295         cmd.cmd.num_channels = 0;
296         n = min(request->n_channels, 4U);
297         for (i = 0; i < n; i++) {
298                 int ch = request->channels[i]->hw_value;
299                 if (ch == 0) {
300                         wil_err(wil,
301                                 "Scan requested for unknown frequency %dMhz\n",
302                                 request->channels[i]->center_freq);
303                         continue;
304                 }
305                 /* 0-based channel indexes */
306                 cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
307                 wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
308                              request->channels[i]->center_freq);
309         }
310
311         rc = wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
312                         cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
313
314         if (rc) {
315                 del_timer_sync(&wil->scan_timer);
316                 wil->scan_request = NULL;
317         }
318
319         return rc;
320 }
321
322 static int wil_cfg80211_connect(struct wiphy *wiphy,
323                                 struct net_device *ndev,
324                                 struct cfg80211_connect_params *sme)
325 {
326         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
327         struct cfg80211_bss *bss;
328         struct wmi_connect_cmd conn;
329         const u8 *ssid_eid;
330         const u8 *rsn_eid;
331         int ch;
332         int rc = 0;
333
334         if (test_bit(wil_status_fwconnecting, &wil->status) ||
335             test_bit(wil_status_fwconnected, &wil->status))
336                 return -EALREADY;
337
338         bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
339                                sme->ssid, sme->ssid_len,
340                                WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
341         if (!bss) {
342                 wil_err(wil, "Unable to find BSS\n");
343                 return -ENOENT;
344         }
345
346         ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
347         if (!ssid_eid) {
348                 wil_err(wil, "No SSID\n");
349                 rc = -ENOENT;
350                 goto out;
351         }
352
353         rsn_eid = sme->ie ?
354                         cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
355                         NULL;
356         if (rsn_eid) {
357                 if (sme->ie_len > WMI_MAX_IE_LEN) {
358                         rc = -ERANGE;
359                         wil_err(wil, "IE too large (%td bytes)\n",
360                                 sme->ie_len);
361                         goto out;
362                 }
363                 /*
364                  * For secure assoc, send:
365                  * (1) WMI_DELETE_CIPHER_KEY_CMD
366                  * (2) WMI_SET_APPIE_CMD
367                  */
368                 rc = wmi_del_cipher_key(wil, 0, bss->bssid);
369                 if (rc) {
370                         wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD failed\n");
371                         goto out;
372                 }
373                 /* WMI_SET_APPIE_CMD */
374                 rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
375                 if (rc) {
376                         wil_err(wil, "WMI_SET_APPIE_CMD failed\n");
377                         goto out;
378                 }
379         }
380
381         /* WMI_CONNECT_CMD */
382         memset(&conn, 0, sizeof(conn));
383         switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
384         case WLAN_CAPABILITY_DMG_TYPE_AP:
385                 conn.network_type = WMI_NETTYPE_INFRA;
386                 break;
387         case WLAN_CAPABILITY_DMG_TYPE_PBSS:
388                 conn.network_type = WMI_NETTYPE_P2P;
389                 break;
390         default:
391                 wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
392                         bss->capability);
393                 goto out;
394         }
395         if (rsn_eid) {
396                 conn.dot11_auth_mode = WMI_AUTH11_SHARED;
397                 conn.auth_mode = WMI_AUTH_WPA2_PSK;
398                 conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
399                 conn.pairwise_crypto_len = 16;
400         } else {
401                 conn.dot11_auth_mode = WMI_AUTH11_OPEN;
402                 conn.auth_mode = WMI_AUTH_NONE;
403         }
404
405         conn.ssid_len = min_t(u8, ssid_eid[1], 32);
406         memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
407
408         ch = bss->channel->hw_value;
409         if (ch == 0) {
410                 wil_err(wil, "BSS at unknown frequency %dMhz\n",
411                         bss->channel->center_freq);
412                 rc = -EOPNOTSUPP;
413                 goto out;
414         }
415         conn.channel = ch - 1;
416
417         memcpy(conn.bssid, bss->bssid, ETH_ALEN);
418         memcpy(conn.dst_mac, bss->bssid, ETH_ALEN);
419
420         set_bit(wil_status_fwconnecting, &wil->status);
421
422         rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
423         if (rc == 0) {
424                 /* Connect can take lots of time */
425                 mod_timer(&wil->connect_timer,
426                           jiffies + msecs_to_jiffies(2000));
427         } else {
428                 clear_bit(wil_status_fwconnecting, &wil->status);
429         }
430
431  out:
432         cfg80211_put_bss(wiphy, bss);
433
434         return rc;
435 }
436
437 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
438                                    struct net_device *ndev,
439                                    u16 reason_code)
440 {
441         int rc;
442         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
443
444         rc = wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
445
446         return rc;
447 }
448
449 int wil_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
450                          struct cfg80211_mgmt_tx_params *params,
451                          u64 *cookie)
452 {
453         const u8 *buf = params->buf;
454         size_t len = params->len;
455         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
456         int rc;
457         bool tx_status = false;
458         struct ieee80211_mgmt *mgmt_frame = (void *)buf;
459         struct wmi_sw_tx_req_cmd *cmd;
460         struct {
461                 struct wil6210_mbox_hdr_wmi wmi;
462                 struct wmi_sw_tx_complete_event evt;
463         } __packed evt;
464
465         cmd = kmalloc(sizeof(*cmd) + len, GFP_KERNEL);
466         if (!cmd) {
467                 rc = -ENOMEM;
468                 goto out;
469         }
470
471         memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
472         cmd->len = cpu_to_le16(len);
473         memcpy(cmd->payload, buf, len);
474
475         rc = wmi_call(wil, WMI_SW_TX_REQ_CMDID, cmd, sizeof(*cmd) + len,
476                       WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
477         if (rc == 0)
478                 tx_status = !evt.evt.status;
479
480         kfree(cmd);
481  out:
482         cfg80211_mgmt_tx_status(wdev, cookie ? *cookie : 0, buf, len,
483                                 tx_status, GFP_KERNEL);
484         return rc;
485 }
486
487 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
488                                     struct cfg80211_chan_def *chandef)
489 {
490         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
491         struct wireless_dev *wdev = wil->wdev;
492
493         wdev->preset_chandef = *chandef;
494
495         return 0;
496 }
497
498 static int wil_cfg80211_add_key(struct wiphy *wiphy,
499                                 struct net_device *ndev,
500                                 u8 key_index, bool pairwise,
501                                 const u8 *mac_addr,
502                                 struct key_params *params)
503 {
504         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
505
506         /* group key is not used */
507         if (!pairwise)
508                 return 0;
509
510         return wmi_add_cipher_key(wil, key_index, mac_addr,
511                                   params->key_len, params->key);
512 }
513
514 static int wil_cfg80211_del_key(struct wiphy *wiphy,
515                                 struct net_device *ndev,
516                                 u8 key_index, bool pairwise,
517                                 const u8 *mac_addr)
518 {
519         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
520
521         /* group key is not used */
522         if (!pairwise)
523                 return 0;
524
525         return wmi_del_cipher_key(wil, key_index, mac_addr);
526 }
527
528 /* Need to be present or wiphy_new() will WARN */
529 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
530                                         struct net_device *ndev,
531                                         u8 key_index, bool unicast,
532                                         bool multicast)
533 {
534         return 0;
535 }
536
537 static int wil_remain_on_channel(struct wiphy *wiphy,
538                                  struct wireless_dev *wdev,
539                                  struct ieee80211_channel *chan,
540                                  unsigned int duration,
541                                  u64 *cookie)
542 {
543         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
544         int rc;
545
546         /* TODO: handle duration */
547         wil_info(wil, "%s(%d, %d ms)\n", __func__, chan->center_freq, duration);
548
549         rc = wmi_set_channel(wil, chan->hw_value);
550         if (rc)
551                 return rc;
552
553         rc = wmi_rxon(wil, true);
554
555         return rc;
556 }
557
558 static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
559                                         struct wireless_dev *wdev,
560                                         u64 cookie)
561 {
562         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
563         int rc;
564
565         wil_info(wil, "%s()\n", __func__);
566
567         rc = wmi_rxon(wil, false);
568
569         return rc;
570 }
571
572 static void wil_print_bcon_data(struct cfg80211_beacon_data *b)
573 {
574         print_hex_dump_bytes("head     ", DUMP_PREFIX_OFFSET,
575                              b->head, b->head_len);
576         print_hex_dump_bytes("tail     ", DUMP_PREFIX_OFFSET,
577                              b->tail, b->tail_len);
578         print_hex_dump_bytes("BCON IE  ", DUMP_PREFIX_OFFSET,
579                              b->beacon_ies, b->beacon_ies_len);
580         print_hex_dump_bytes("PROBE    ", DUMP_PREFIX_OFFSET,
581                              b->probe_resp, b->probe_resp_len);
582         print_hex_dump_bytes("PROBE IE ", DUMP_PREFIX_OFFSET,
583                              b->proberesp_ies, b->proberesp_ies_len);
584         print_hex_dump_bytes("ASSOC IE ", DUMP_PREFIX_OFFSET,
585                              b->assocresp_ies, b->assocresp_ies_len);
586 }
587
588 static void wil_print_crypto(struct wil6210_priv *wil,
589                              struct cfg80211_crypto_settings *c)
590 {
591         wil_dbg_misc(wil, "WPA versions: 0x%08x cipher group 0x%08x\n",
592                      c->wpa_versions, c->cipher_group);
593         wil_dbg_misc(wil, "Pairwise ciphers [%d]\n", c->n_ciphers_pairwise);
594         wil_dbg_misc(wil, "AKM suites [%d]\n", c->n_akm_suites);
595         wil_dbg_misc(wil, "Control port : %d, eth_type 0x%04x no_encrypt %d\n",
596                      c->control_port, be16_to_cpu(c->control_port_ethertype),
597                      c->control_port_no_encrypt);
598 }
599
600 static int wil_fix_bcon(struct wil6210_priv *wil,
601                         struct cfg80211_beacon_data *bcon)
602 {
603         struct ieee80211_mgmt *f = (struct ieee80211_mgmt *)bcon->probe_resp;
604         size_t hlen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
605         int rc = 0;
606
607         if (bcon->probe_resp_len <= hlen)
608                 return 0;
609
610         if (!bcon->proberesp_ies) {
611                 bcon->proberesp_ies = f->u.probe_resp.variable;
612                 bcon->proberesp_ies_len = bcon->probe_resp_len - hlen;
613                 rc = 1;
614         }
615         if (!bcon->assocresp_ies) {
616                 bcon->assocresp_ies = f->u.probe_resp.variable;
617                 bcon->assocresp_ies_len = bcon->probe_resp_len - hlen;
618                 rc = 1;
619         }
620
621         return rc;
622 }
623
624 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
625                                  struct net_device *ndev,
626                                  struct cfg80211_ap_settings *info)
627 {
628         int rc = 0;
629         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
630         struct wireless_dev *wdev = ndev->ieee80211_ptr;
631         struct ieee80211_channel *channel = info->chandef.chan;
632         struct cfg80211_beacon_data *bcon = &info->beacon;
633         struct cfg80211_crypto_settings *crypto = &info->crypto;
634         u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
635
636         wil_dbg_misc(wil, "%s()\n", __func__);
637
638         if (!channel) {
639                 wil_err(wil, "AP: No channel???\n");
640                 return -EINVAL;
641         }
642
643         wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
644                      channel->center_freq, info->privacy ? "secure" : "open");
645         wil_dbg_misc(wil, "Privacy: %d auth_type %d\n",
646                      info->privacy, info->auth_type);
647         wil_dbg_misc(wil, "BI %d DTIM %d\n", info->beacon_interval,
648                      info->dtim_period);
649         print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
650                              info->ssid, info->ssid_len);
651         wil_print_bcon_data(bcon);
652         wil_print_crypto(wil, crypto);
653
654         if (wil_fix_bcon(wil, bcon)) {
655                 wil_dbg_misc(wil, "Fixed bcon\n");
656                 wil_print_bcon_data(bcon);
657         }
658
659         mutex_lock(&wil->mutex);
660
661         rc = wil_reset(wil);
662         if (rc)
663                 goto out;
664
665         /* Rx VRING. */
666         rc = wil_rx_init(wil);
667         if (rc)
668                 goto out;
669
670         rc = wmi_set_ssid(wil, info->ssid_len, info->ssid);
671         if (rc)
672                 goto out;
673
674         /* MAC address - pre-requisite for other commands */
675         wmi_set_mac_address(wil, ndev->dev_addr);
676
677         /* IE's */
678         /* bcon 'head IE's are not relevant for 60g band */
679         /*
680          * FW do not form regular beacon, so bcon IE's are not set
681          * For the DMG bcon, when it will be supported, bcon IE's will
682          * be reused; add something like:
683          * wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->beacon_ies_len,
684          * bcon->beacon_ies);
685          */
686         wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, bcon->proberesp_ies_len,
687                    bcon->proberesp_ies);
688         wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, bcon->assocresp_ies_len,
689                    bcon->assocresp_ies);
690
691         wil->secure_pcp = info->privacy;
692
693         rc = wmi_pcp_start(wil, info->beacon_interval, wmi_nettype,
694                            channel->hw_value);
695         if (rc)
696                 goto out;
697
698
699         netif_carrier_on(ndev);
700
701 out:
702         mutex_unlock(&wil->mutex);
703         return rc;
704 }
705
706 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
707                                 struct net_device *ndev)
708 {
709         int rc = 0;
710         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
711
712         wil_dbg_misc(wil, "%s()\n", __func__);
713
714         mutex_lock(&wil->mutex);
715
716         rc = wmi_pcp_stop(wil);
717
718         mutex_unlock(&wil->mutex);
719         return rc;
720 }
721
722 static int wil_cfg80211_del_station(struct wiphy *wiphy,
723                                     struct net_device *dev, const u8 *mac)
724 {
725         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
726
727         mutex_lock(&wil->mutex);
728         wil6210_disconnect(wil, mac);
729         mutex_unlock(&wil->mutex);
730
731         return 0;
732 }
733
734 static struct cfg80211_ops wil_cfg80211_ops = {
735         .scan = wil_cfg80211_scan,
736         .connect = wil_cfg80211_connect,
737         .disconnect = wil_cfg80211_disconnect,
738         .change_virtual_intf = wil_cfg80211_change_iface,
739         .get_station = wil_cfg80211_get_station,
740         .dump_station = wil_cfg80211_dump_station,
741         .remain_on_channel = wil_remain_on_channel,
742         .cancel_remain_on_channel = wil_cancel_remain_on_channel,
743         .mgmt_tx = wil_cfg80211_mgmt_tx,
744         .set_monitor_channel = wil_cfg80211_set_channel,
745         .add_key = wil_cfg80211_add_key,
746         .del_key = wil_cfg80211_del_key,
747         .set_default_key = wil_cfg80211_set_default_key,
748         /* AP mode */
749         .start_ap = wil_cfg80211_start_ap,
750         .stop_ap = wil_cfg80211_stop_ap,
751         .del_station = wil_cfg80211_del_station,
752 };
753
754 static void wil_wiphy_init(struct wiphy *wiphy)
755 {
756         /* TODO: set real value */
757         wiphy->max_scan_ssids = 10;
758         wiphy->max_num_pmkids = 0 /* TODO: */;
759         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
760                                  BIT(NL80211_IFTYPE_AP) |
761                                  BIT(NL80211_IFTYPE_MONITOR);
762         /* TODO: enable P2P when integrated with supplicant:
763          * BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO)
764          */
765         wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
766                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
767         dev_warn(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
768                  __func__, wiphy->flags);
769         wiphy->probe_resp_offload =
770                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
771                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
772                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
773
774         wiphy->bands[IEEE80211_BAND_60GHZ] = &wil_band_60ghz;
775
776         /* TODO: figure this out */
777         wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
778
779         wiphy->cipher_suites = wil_cipher_suites;
780         wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
781         wiphy->mgmt_stypes = wil_mgmt_stypes;
782 }
783
784 struct wireless_dev *wil_cfg80211_init(struct device *dev)
785 {
786         int rc = 0;
787         struct wireless_dev *wdev;
788
789         wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
790         if (!wdev)
791                 return ERR_PTR(-ENOMEM);
792
793         wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
794                                 sizeof(struct wil6210_priv));
795         if (!wdev->wiphy) {
796                 rc = -ENOMEM;
797                 goto out;
798         }
799
800         set_wiphy_dev(wdev->wiphy, dev);
801         wil_wiphy_init(wdev->wiphy);
802
803         rc = wiphy_register(wdev->wiphy);
804         if (rc < 0)
805                 goto out_failed_reg;
806
807         return wdev;
808
809 out_failed_reg:
810         wiphy_free(wdev->wiphy);
811 out:
812         kfree(wdev);
813
814         return ERR_PTR(rc);
815 }
816
817 void wil_wdev_free(struct wil6210_priv *wil)
818 {
819         struct wireless_dev *wdev = wil_to_wdev(wil);
820
821         if (!wdev)
822                 return;
823
824         wiphy_unregister(wdev->wiphy);
825         wiphy_free(wdev->wiphy);
826         kfree(wdev);
827 }