GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / net / wireless / ath / wil6210 / cfg80211.c
1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <linux/etherdevice.h>
19 #include <linux/moduleparam.h>
20 #include <net/netlink.h>
21 #include <net/cfg80211.h>
22 #include "wil6210.h"
23 #include "wmi.h"
24 #include "fw.h"
25
26 #define WIL_MAX_ROC_DURATION_MS 5000
27
28 bool disable_ap_sme;
29 module_param(disable_ap_sme, bool, 0444);
30 MODULE_PARM_DESC(disable_ap_sme, " let user space handle AP mode SME");
31
32 #ifdef CONFIG_PM
33 static struct wiphy_wowlan_support wil_wowlan_support = {
34         .flags = WIPHY_WOWLAN_ANY | WIPHY_WOWLAN_DISCONNECT,
35 };
36 #endif
37
38 #define CHAN60G(_channel, _flags) {                             \
39         .band                   = NL80211_BAND_60GHZ,           \
40         .center_freq            = 56160 + (2160 * (_channel)),  \
41         .hw_value               = (_channel),                   \
42         .flags                  = (_flags),                     \
43         .max_antenna_gain       = 0,                            \
44         .max_power              = 40,                           \
45 }
46
47 static struct ieee80211_channel wil_60ghz_channels[] = {
48         CHAN60G(1, 0),
49         CHAN60G(2, 0),
50         CHAN60G(3, 0),
51 /* channel 4 not supported yet */
52 };
53
54 /* Vendor id to be used in vendor specific command and events
55  * to user space.
56  * NOTE: The authoritative place for definition of QCA_NL80211_VENDOR_ID,
57  * vendor subcmd definitions prefixed with QCA_NL80211_VENDOR_SUBCMD, and
58  * qca_wlan_vendor_attr is open source file src/common/qca-vendor.h in
59  * git://w1.fi/srv/git/hostap.git; the values here are just a copy of that
60  */
61
62 #define QCA_NL80211_VENDOR_ID   0x001374
63
64 #define WIL_MAX_RF_SECTORS (128)
65 #define WIL_CID_ALL (0xff)
66
67 enum qca_wlan_vendor_attr_rf_sector {
68         QCA_ATTR_MAC_ADDR = 6,
69         QCA_ATTR_PAD = 13,
70         QCA_ATTR_TSF = 29,
71         QCA_ATTR_DMG_RF_SECTOR_INDEX = 30,
72         QCA_ATTR_DMG_RF_SECTOR_TYPE = 31,
73         QCA_ATTR_DMG_RF_MODULE_MASK = 32,
74         QCA_ATTR_DMG_RF_SECTOR_CFG = 33,
75         QCA_ATTR_DMG_RF_SECTOR_MAX,
76 };
77
78 enum qca_wlan_vendor_attr_dmg_rf_sector_type {
79         QCA_ATTR_DMG_RF_SECTOR_TYPE_RX,
80         QCA_ATTR_DMG_RF_SECTOR_TYPE_TX,
81         QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX
82 };
83
84 enum qca_wlan_vendor_attr_dmg_rf_sector_cfg {
85         QCA_ATTR_DMG_RF_SECTOR_CFG_INVALID = 0,
86         QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX,
87         QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0,
88         QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1,
89         QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2,
90         QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI,
91         QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO,
92         QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16,
93
94         /* keep last */
95         QCA_ATTR_DMG_RF_SECTOR_CFG_AFTER_LAST,
96         QCA_ATTR_DMG_RF_SECTOR_CFG_MAX =
97         QCA_ATTR_DMG_RF_SECTOR_CFG_AFTER_LAST - 1
98 };
99
100 static const struct
101 nla_policy wil_rf_sector_policy[QCA_ATTR_DMG_RF_SECTOR_MAX + 1] = {
102         [QCA_ATTR_MAC_ADDR] = { .len = ETH_ALEN },
103         [QCA_ATTR_DMG_RF_SECTOR_INDEX] = { .type = NLA_U16 },
104         [QCA_ATTR_DMG_RF_SECTOR_TYPE] = { .type = NLA_U8 },
105         [QCA_ATTR_DMG_RF_MODULE_MASK] = { .type = NLA_U32 },
106         [QCA_ATTR_DMG_RF_SECTOR_CFG] = { .type = NLA_NESTED },
107 };
108
109 static const struct
110 nla_policy wil_rf_sector_cfg_policy[QCA_ATTR_DMG_RF_SECTOR_CFG_MAX + 1] = {
111         [QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX] = { .type = NLA_U8 },
112         [QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0] = { .type = NLA_U32 },
113         [QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1] = { .type = NLA_U32 },
114         [QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2] = { .type = NLA_U32 },
115         [QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI] = { .type = NLA_U32 },
116         [QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO] = { .type = NLA_U32 },
117         [QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16] = { .type = NLA_U32 },
118 };
119
120 enum qca_nl80211_vendor_subcmds {
121         QCA_NL80211_VENDOR_SUBCMD_DMG_RF_GET_SECTOR_CFG = 139,
122         QCA_NL80211_VENDOR_SUBCMD_DMG_RF_SET_SECTOR_CFG = 140,
123         QCA_NL80211_VENDOR_SUBCMD_DMG_RF_GET_SELECTED_SECTOR = 141,
124         QCA_NL80211_VENDOR_SUBCMD_DMG_RF_SET_SELECTED_SECTOR = 142,
125 };
126
127 static int wil_rf_sector_get_cfg(struct wiphy *wiphy,
128                                  struct wireless_dev *wdev,
129                                  const void *data, int data_len);
130 static int wil_rf_sector_set_cfg(struct wiphy *wiphy,
131                                  struct wireless_dev *wdev,
132                                  const void *data, int data_len);
133 static int wil_rf_sector_get_selected(struct wiphy *wiphy,
134                                       struct wireless_dev *wdev,
135                                       const void *data, int data_len);
136 static int wil_rf_sector_set_selected(struct wiphy *wiphy,
137                                       struct wireless_dev *wdev,
138                                       const void *data, int data_len);
139
140 /* vendor specific commands */
141 static const struct wiphy_vendor_command wil_nl80211_vendor_commands[] = {
142         {
143                 .info.vendor_id = QCA_NL80211_VENDOR_ID,
144                 .info.subcmd = QCA_NL80211_VENDOR_SUBCMD_DMG_RF_GET_SECTOR_CFG,
145                 .flags = WIPHY_VENDOR_CMD_NEED_WDEV |
146                          WIPHY_VENDOR_CMD_NEED_RUNNING,
147                 .doit = wil_rf_sector_get_cfg
148         },
149         {
150                 .info.vendor_id = QCA_NL80211_VENDOR_ID,
151                 .info.subcmd = QCA_NL80211_VENDOR_SUBCMD_DMG_RF_SET_SECTOR_CFG,
152                 .flags = WIPHY_VENDOR_CMD_NEED_WDEV |
153                          WIPHY_VENDOR_CMD_NEED_RUNNING,
154                 .doit = wil_rf_sector_set_cfg
155         },
156         {
157                 .info.vendor_id = QCA_NL80211_VENDOR_ID,
158                 .info.subcmd =
159                         QCA_NL80211_VENDOR_SUBCMD_DMG_RF_GET_SELECTED_SECTOR,
160                 .flags = WIPHY_VENDOR_CMD_NEED_WDEV |
161                          WIPHY_VENDOR_CMD_NEED_RUNNING,
162                 .doit = wil_rf_sector_get_selected
163         },
164         {
165                 .info.vendor_id = QCA_NL80211_VENDOR_ID,
166                 .info.subcmd =
167                         QCA_NL80211_VENDOR_SUBCMD_DMG_RF_SET_SELECTED_SECTOR,
168                 .flags = WIPHY_VENDOR_CMD_NEED_WDEV |
169                          WIPHY_VENDOR_CMD_NEED_RUNNING,
170                 .doit = wil_rf_sector_set_selected
171         },
172 };
173
174 static struct ieee80211_supported_band wil_band_60ghz = {
175         .channels = wil_60ghz_channels,
176         .n_channels = ARRAY_SIZE(wil_60ghz_channels),
177         .ht_cap = {
178                 .ht_supported = true,
179                 .cap = 0, /* TODO */
180                 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
181                 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
182                 .mcs = {
183                                 /* MCS 1..12 - SC PHY */
184                         .rx_mask = {0xfe, 0x1f}, /* 1..12 */
185                         .tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
186                 },
187         },
188 };
189
190 static const struct ieee80211_txrx_stypes
191 wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
192         [NL80211_IFTYPE_STATION] = {
193                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
194                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
195                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
196                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
197         },
198         [NL80211_IFTYPE_AP] = {
199                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
200                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4) |
201                 BIT(IEEE80211_STYPE_ASSOC_RESP >> 4) |
202                 BIT(IEEE80211_STYPE_DISASSOC >> 4),
203                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
204                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
205                 BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
206                 BIT(IEEE80211_STYPE_DISASSOC >> 4) |
207                 BIT(IEEE80211_STYPE_AUTH >> 4) |
208                 BIT(IEEE80211_STYPE_DEAUTH >> 4) |
209                 BIT(IEEE80211_STYPE_REASSOC_REQ >> 4)
210         },
211         [NL80211_IFTYPE_P2P_CLIENT] = {
212                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
213                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
214                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
215                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
216         },
217         [NL80211_IFTYPE_P2P_GO] = {
218                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
219                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
220                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
221                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
222         },
223         [NL80211_IFTYPE_P2P_DEVICE] = {
224                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
225                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
226                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
227                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
228         },
229 };
230
231 static const u32 wil_cipher_suites[] = {
232         WLAN_CIPHER_SUITE_GCMP,
233 };
234
235 static const char * const key_usage_str[] = {
236         [WMI_KEY_USE_PAIRWISE]  = "PTK",
237         [WMI_KEY_USE_RX_GROUP]  = "RX_GTK",
238         [WMI_KEY_USE_TX_GROUP]  = "TX_GTK",
239 };
240
241 int wil_iftype_nl2wmi(enum nl80211_iftype type)
242 {
243         static const struct {
244                 enum nl80211_iftype nl;
245                 enum wmi_network_type wmi;
246         } __nl2wmi[] = {
247                 {NL80211_IFTYPE_ADHOC,          WMI_NETTYPE_ADHOC},
248                 {NL80211_IFTYPE_STATION,        WMI_NETTYPE_INFRA},
249                 {NL80211_IFTYPE_AP,             WMI_NETTYPE_AP},
250                 {NL80211_IFTYPE_P2P_CLIENT,     WMI_NETTYPE_P2P},
251                 {NL80211_IFTYPE_P2P_GO,         WMI_NETTYPE_P2P},
252                 {NL80211_IFTYPE_MONITOR,        WMI_NETTYPE_ADHOC}, /* FIXME */
253         };
254         uint i;
255
256         for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
257                 if (__nl2wmi[i].nl == type)
258                         return __nl2wmi[i].wmi;
259         }
260
261         return -EOPNOTSUPP;
262 }
263
264 int wil_cid_fill_sinfo(struct wil6210_vif *vif, int cid,
265                        struct station_info *sinfo)
266 {
267         struct wil6210_priv *wil = vif_to_wil(vif);
268         struct wmi_notify_req_cmd cmd = {
269                 .cid = cid,
270                 .interval_usec = 0,
271         };
272         struct {
273                 struct wmi_cmd_hdr wmi;
274                 struct wmi_notify_req_done_event evt;
275         } __packed reply;
276         struct wil_net_stats *stats = &wil->sta[cid].stats;
277         int rc;
278
279         memset(&reply, 0, sizeof(reply));
280
281         rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, vif->mid, &cmd, sizeof(cmd),
282                       WMI_NOTIFY_REQ_DONE_EVENTID, &reply, sizeof(reply), 20);
283         if (rc)
284                 return rc;
285
286         wil_dbg_wmi(wil, "Link status for CID %d MID %d: {\n"
287                     "  MCS %d TSF 0x%016llx\n"
288                     "  BF status 0x%08x RSSI %d SQI %d%%\n"
289                     "  Tx Tpt %d goodput %d Rx goodput %d\n"
290                     "  Sectors(rx:tx) my %d:%d peer %d:%d\n""}\n",
291                     cid, vif->mid, le16_to_cpu(reply.evt.bf_mcs),
292                     le64_to_cpu(reply.evt.tsf), reply.evt.status,
293                     reply.evt.rssi,
294                     reply.evt.sqi,
295                     le32_to_cpu(reply.evt.tx_tpt),
296                     le32_to_cpu(reply.evt.tx_goodput),
297                     le32_to_cpu(reply.evt.rx_goodput),
298                     le16_to_cpu(reply.evt.my_rx_sector),
299                     le16_to_cpu(reply.evt.my_tx_sector),
300                     le16_to_cpu(reply.evt.other_rx_sector),
301                     le16_to_cpu(reply.evt.other_tx_sector));
302
303         sinfo->generation = wil->sinfo_gen;
304
305         sinfo->filled = BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
306                         BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
307                         BIT_ULL(NL80211_STA_INFO_RX_PACKETS) |
308                         BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
309                         BIT_ULL(NL80211_STA_INFO_RX_BITRATE) |
310                         BIT_ULL(NL80211_STA_INFO_TX_BITRATE) |
311                         BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC) |
312                         BIT_ULL(NL80211_STA_INFO_TX_FAILED);
313
314         sinfo->txrate.flags = RATE_INFO_FLAGS_60G;
315         sinfo->txrate.mcs = le16_to_cpu(reply.evt.bf_mcs);
316         sinfo->rxrate.mcs = stats->last_mcs_rx;
317         sinfo->rx_bytes = stats->rx_bytes;
318         sinfo->rx_packets = stats->rx_packets;
319         sinfo->rx_dropped_misc = stats->rx_dropped;
320         sinfo->tx_bytes = stats->tx_bytes;
321         sinfo->tx_packets = stats->tx_packets;
322         sinfo->tx_failed = stats->tx_errors;
323
324         if (test_bit(wil_vif_fwconnected, vif->status)) {
325                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
326                 if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING,
327                              wil->fw_capabilities))
328                         sinfo->signal = reply.evt.rssi;
329                 else
330                         sinfo->signal = reply.evt.sqi;
331         }
332
333         return rc;
334 }
335
336 static int wil_cfg80211_get_station(struct wiphy *wiphy,
337                                     struct net_device *ndev,
338                                     const u8 *mac, struct station_info *sinfo)
339 {
340         struct wil6210_vif *vif = ndev_to_vif(ndev);
341         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
342         int rc;
343
344         int cid = wil_find_cid(wil, vif->mid, mac);
345
346         wil_dbg_misc(wil, "get_station: %pM CID %d MID %d\n", mac, cid,
347                      vif->mid);
348         if (cid < 0)
349                 return cid;
350
351         rc = wil_cid_fill_sinfo(vif, cid, sinfo);
352
353         return rc;
354 }
355
356 /*
357  * Find @idx-th active STA for specific MID for station dump.
358  */
359 static int wil_find_cid_by_idx(struct wil6210_priv *wil, u8 mid, int idx)
360 {
361         int i;
362
363         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
364                 if (wil->sta[i].status == wil_sta_unused)
365                         continue;
366                 if (wil->sta[i].mid != mid)
367                         continue;
368                 if (idx == 0)
369                         return i;
370                 idx--;
371         }
372
373         return -ENOENT;
374 }
375
376 static int wil_cfg80211_dump_station(struct wiphy *wiphy,
377                                      struct net_device *dev, int idx,
378                                      u8 *mac, struct station_info *sinfo)
379 {
380         struct wil6210_vif *vif = ndev_to_vif(dev);
381         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
382         int rc;
383         int cid = wil_find_cid_by_idx(wil, vif->mid, idx);
384
385         if (cid < 0)
386                 return -ENOENT;
387
388         ether_addr_copy(mac, wil->sta[cid].addr);
389         wil_dbg_misc(wil, "dump_station: %pM CID %d MID %d\n", mac, cid,
390                      vif->mid);
391
392         rc = wil_cid_fill_sinfo(vif, cid, sinfo);
393
394         return rc;
395 }
396
397 static int wil_cfg80211_start_p2p_device(struct wiphy *wiphy,
398                                          struct wireless_dev *wdev)
399 {
400         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
401
402         wil_dbg_misc(wil, "start_p2p_device: entered\n");
403         wil->p2p_dev_started = 1;
404         return 0;
405 }
406
407 static void wil_cfg80211_stop_p2p_device(struct wiphy *wiphy,
408                                          struct wireless_dev *wdev)
409 {
410         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
411
412         if (!wil->p2p_dev_started)
413                 return;
414
415         wil_dbg_misc(wil, "stop_p2p_device: entered\n");
416         mutex_lock(&wil->mutex);
417         mutex_lock(&wil->vif_mutex);
418         wil_p2p_stop_radio_operations(wil);
419         wil->p2p_dev_started = 0;
420         mutex_unlock(&wil->vif_mutex);
421         mutex_unlock(&wil->mutex);
422 }
423
424 static int wil_cfg80211_validate_add_iface(struct wil6210_priv *wil,
425                                            enum nl80211_iftype new_type)
426 {
427         int i;
428         struct wireless_dev *wdev;
429         struct iface_combination_params params = {
430                 .num_different_channels = 1,
431         };
432
433         for (i = 0; i < wil->max_vifs; i++) {
434                 if (wil->vifs[i]) {
435                         wdev = vif_to_wdev(wil->vifs[i]);
436                         params.iftype_num[wdev->iftype]++;
437                 }
438         }
439         params.iftype_num[new_type]++;
440         return cfg80211_check_combinations(wil->wiphy, &params);
441 }
442
443 static int wil_cfg80211_validate_change_iface(struct wil6210_priv *wil,
444                                               struct wil6210_vif *vif,
445                                               enum nl80211_iftype new_type)
446 {
447         int i, ret = 0;
448         struct wireless_dev *wdev;
449         struct iface_combination_params params = {
450                 .num_different_channels = 1,
451         };
452         bool check_combos = false;
453
454         for (i = 0; i < wil->max_vifs; i++) {
455                 struct wil6210_vif *vif_pos = wil->vifs[i];
456
457                 if (vif_pos && vif != vif_pos) {
458                         wdev = vif_to_wdev(vif_pos);
459                         params.iftype_num[wdev->iftype]++;
460                         check_combos = true;
461                 }
462         }
463
464         if (check_combos) {
465                 params.iftype_num[new_type]++;
466                 ret = cfg80211_check_combinations(wil->wiphy, &params);
467         }
468         return ret;
469 }
470
471 static struct wireless_dev *
472 wil_cfg80211_add_iface(struct wiphy *wiphy, const char *name,
473                        unsigned char name_assign_type,
474                        enum nl80211_iftype type,
475                        struct vif_params *params)
476 {
477         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
478         struct net_device *ndev_main = wil->main_ndev, *ndev;
479         struct wil6210_vif *vif;
480         struct wireless_dev *p2p_wdev, *wdev;
481         int rc;
482
483         wil_dbg_misc(wil, "add_iface, type %d\n", type);
484
485         /* P2P device is not a real virtual interface, it is a management-only
486          * interface that shares the main interface.
487          * Skip concurrency checks here.
488          */
489         if (type == NL80211_IFTYPE_P2P_DEVICE) {
490                 if (wil->p2p_wdev) {
491                         wil_err(wil, "P2P_DEVICE interface already created\n");
492                         return ERR_PTR(-EINVAL);
493                 }
494
495                 p2p_wdev = kzalloc(sizeof(*p2p_wdev), GFP_KERNEL);
496                 if (!p2p_wdev)
497                         return ERR_PTR(-ENOMEM);
498
499                 p2p_wdev->iftype = type;
500                 p2p_wdev->wiphy = wiphy;
501                 /* use our primary ethernet address */
502                 ether_addr_copy(p2p_wdev->address, ndev_main->perm_addr);
503
504                 wil->p2p_wdev = p2p_wdev;
505
506                 return p2p_wdev;
507         }
508
509         if (!wil->wiphy->n_iface_combinations) {
510                 wil_err(wil, "virtual interfaces not supported\n");
511                 return ERR_PTR(-EINVAL);
512         }
513
514         rc = wil_cfg80211_validate_add_iface(wil, type);
515         if (rc) {
516                 wil_err(wil, "iface validation failed, err=%d\n", rc);
517                 return ERR_PTR(rc);
518         }
519
520         vif = wil_vif_alloc(wil, name, name_assign_type, type);
521         if (IS_ERR(vif))
522                 return ERR_CAST(vif);
523
524         ndev = vif_to_ndev(vif);
525         ether_addr_copy(ndev->perm_addr, ndev_main->perm_addr);
526         if (is_valid_ether_addr(params->macaddr)) {
527                 ether_addr_copy(ndev->dev_addr, params->macaddr);
528         } else {
529                 ether_addr_copy(ndev->dev_addr, ndev_main->perm_addr);
530                 ndev->dev_addr[0] = (ndev->dev_addr[0] ^ (1 << vif->mid)) |
531                         0x2; /* locally administered */
532         }
533         wdev = vif_to_wdev(vif);
534         ether_addr_copy(wdev->address, ndev->dev_addr);
535
536         rc = wil_vif_add(wil, vif);
537         if (rc)
538                 goto out;
539
540         wil_info(wil, "added VIF, mid %d iftype %d MAC %pM\n",
541                  vif->mid, type, wdev->address);
542         return wdev;
543 out:
544         wil_vif_free(vif);
545         return ERR_PTR(rc);
546 }
547
548 int wil_vif_prepare_stop(struct wil6210_vif *vif)
549 {
550         struct wil6210_priv *wil = vif_to_wil(vif);
551         struct wireless_dev *wdev = vif_to_wdev(vif);
552         struct net_device *ndev;
553         int rc;
554
555         if (wdev->iftype != NL80211_IFTYPE_AP)
556                 return 0;
557
558         ndev = vif_to_ndev(vif);
559         if (netif_carrier_ok(ndev)) {
560                 rc = wmi_pcp_stop(vif);
561                 if (rc) {
562                         wil_info(wil, "failed to stop AP, status %d\n",
563                                  rc);
564                         /* continue */
565                 }
566                 wil_bcast_fini(vif);
567                 netif_carrier_off(ndev);
568         }
569
570         return 0;
571 }
572
573 static int wil_cfg80211_del_iface(struct wiphy *wiphy,
574                                   struct wireless_dev *wdev)
575 {
576         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
577         struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
578         int rc;
579
580         wil_dbg_misc(wil, "del_iface\n");
581
582         if (wdev->iftype == NL80211_IFTYPE_P2P_DEVICE) {
583                 if (wdev != wil->p2p_wdev) {
584                         wil_err(wil, "delete of incorrect interface 0x%p\n",
585                                 wdev);
586                         return -EINVAL;
587                 }
588
589                 wil_cfg80211_stop_p2p_device(wiphy, wdev);
590                 wil_p2p_wdev_free(wil);
591                 return 0;
592         }
593
594         if (vif->mid == 0) {
595                 wil_err(wil, "cannot remove the main interface\n");
596                 return -EINVAL;
597         }
598
599         rc = wil_vif_prepare_stop(vif);
600         if (rc)
601                 goto out;
602
603         wil_info(wil, "deleted VIF, mid %d iftype %d MAC %pM\n",
604                  vif->mid, wdev->iftype, wdev->address);
605
606         wil_vif_remove(wil, vif->mid);
607 out:
608         return rc;
609 }
610
611 static int wil_cfg80211_change_iface(struct wiphy *wiphy,
612                                      struct net_device *ndev,
613                                      enum nl80211_iftype type,
614                                      struct vif_params *params)
615 {
616         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
617         struct wil6210_vif *vif = ndev_to_vif(ndev);
618         struct wireless_dev *wdev = vif_to_wdev(vif);
619         int rc;
620         bool fw_reset = false;
621
622         wil_dbg_misc(wil, "change_iface: type=%d\n", type);
623
624         if (wiphy->n_iface_combinations) {
625                 rc = wil_cfg80211_validate_change_iface(wil, vif, type);
626                 if (rc) {
627                         wil_err(wil, "iface validation failed, err=%d\n", rc);
628                         return rc;
629                 }
630         }
631
632         /* do not reset FW when there are active VIFs,
633          * because it can cause significant disruption
634          */
635         if (!wil_has_other_active_ifaces(wil, ndev, true, false) &&
636             netif_running(ndev) && !wil_is_recovery_blocked(wil)) {
637                 wil_dbg_misc(wil, "interface is up. resetting...\n");
638                 mutex_lock(&wil->mutex);
639                 __wil_down(wil);
640                 rc = __wil_up(wil);
641                 mutex_unlock(&wil->mutex);
642
643                 if (rc)
644                         return rc;
645                 fw_reset = true;
646         }
647
648         switch (type) {
649         case NL80211_IFTYPE_STATION:
650         case NL80211_IFTYPE_AP:
651         case NL80211_IFTYPE_P2P_CLIENT:
652         case NL80211_IFTYPE_P2P_GO:
653                 break;
654         case NL80211_IFTYPE_MONITOR:
655                 if (params->flags)
656                         wil->monitor_flags = params->flags;
657                 break;
658         default:
659                 return -EOPNOTSUPP;
660         }
661
662         if (vif->mid != 0 && wil_has_active_ifaces(wil, true, false)) {
663                 if (!fw_reset)
664                         wil_vif_prepare_stop(vif);
665                 rc = wmi_port_delete(wil, vif->mid);
666                 if (rc)
667                         return rc;
668                 rc = wmi_port_allocate(wil, vif->mid, ndev->dev_addr, type);
669                 if (rc)
670                         return rc;
671         }
672
673         wdev->iftype = type;
674         return 0;
675 }
676
677 static int wil_cfg80211_scan(struct wiphy *wiphy,
678                              struct cfg80211_scan_request *request)
679 {
680         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
681         struct wireless_dev *wdev = request->wdev;
682         struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
683         struct {
684                 struct wmi_start_scan_cmd cmd;
685                 u16 chnl[4];
686         } __packed cmd;
687         uint i, n;
688         int rc;
689
690         wil_dbg_misc(wil, "scan: wdev=0x%p iftype=%d\n", wdev, wdev->iftype);
691
692         /* scan is supported on client interfaces and on AP interface */
693         switch (wdev->iftype) {
694         case NL80211_IFTYPE_STATION:
695         case NL80211_IFTYPE_P2P_CLIENT:
696         case NL80211_IFTYPE_P2P_DEVICE:
697         case NL80211_IFTYPE_AP:
698                 break;
699         default:
700                 return -EOPNOTSUPP;
701         }
702
703         /* FW don't support scan after connection attempt */
704         if (test_bit(wil_status_dontscan, wil->status)) {
705                 wil_err(wil, "Can't scan now\n");
706                 return -EBUSY;
707         }
708
709         mutex_lock(&wil->mutex);
710
711         mutex_lock(&wil->vif_mutex);
712         if (vif->scan_request || vif->p2p.discovery_started) {
713                 wil_err(wil, "Already scanning\n");
714                 mutex_unlock(&wil->vif_mutex);
715                 rc = -EAGAIN;
716                 goto out;
717         }
718         mutex_unlock(&wil->vif_mutex);
719
720         if (wdev->iftype == NL80211_IFTYPE_P2P_DEVICE) {
721                 if (!wil->p2p_dev_started) {
722                         wil_err(wil, "P2P search requested on stopped P2P device\n");
723                         rc = -EIO;
724                         goto out;
725                 }
726                 /* social scan on P2P_DEVICE is handled as p2p search */
727                 if (wil_p2p_is_social_scan(request)) {
728                         vif->scan_request = request;
729                         if (vif->mid == 0)
730                                 wil->radio_wdev = wdev;
731                         rc = wil_p2p_search(vif, request);
732                         if (rc) {
733                                 if (vif->mid == 0)
734                                         wil->radio_wdev =
735                                                 wil->main_ndev->ieee80211_ptr;
736                                 vif->scan_request = NULL;
737                         }
738                         goto out;
739                 }
740         }
741
742         (void)wil_p2p_stop_discovery(vif);
743
744         wil_dbg_misc(wil, "Start scan_request 0x%p\n", request);
745         wil_dbg_misc(wil, "SSID count: %d", request->n_ssids);
746
747         for (i = 0; i < request->n_ssids; i++) {
748                 wil_dbg_misc(wil, "SSID[%d]", i);
749                 wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
750                                   request->ssids[i].ssid,
751                                   request->ssids[i].ssid_len, true);
752         }
753
754         if (request->n_ssids)
755                 rc = wmi_set_ssid(vif, request->ssids[0].ssid_len,
756                                   request->ssids[0].ssid);
757         else
758                 rc = wmi_set_ssid(vif, 0, NULL);
759
760         if (rc) {
761                 wil_err(wil, "set SSID for scan request failed: %d\n", rc);
762                 goto out;
763         }
764
765         vif->scan_request = request;
766         mod_timer(&vif->scan_timer, jiffies + WIL6210_SCAN_TO);
767
768         memset(&cmd, 0, sizeof(cmd));
769         cmd.cmd.scan_type = WMI_ACTIVE_SCAN;
770         cmd.cmd.num_channels = 0;
771         n = min(request->n_channels, 4U);
772         for (i = 0; i < n; i++) {
773                 int ch = request->channels[i]->hw_value;
774
775                 if (ch == 0) {
776                         wil_err(wil,
777                                 "Scan requested for unknown frequency %dMhz\n",
778                                 request->channels[i]->center_freq);
779                         continue;
780                 }
781                 /* 0-based channel indexes */
782                 cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
783                 wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
784                              request->channels[i]->center_freq);
785         }
786
787         if (request->ie_len)
788                 wil_hex_dump_misc("Scan IE ", DUMP_PREFIX_OFFSET, 16, 1,
789                                   request->ie, request->ie_len, true);
790         else
791                 wil_dbg_misc(wil, "Scan has no IE's\n");
792
793         rc = wmi_set_ie(vif, WMI_FRAME_PROBE_REQ,
794                         request->ie_len, request->ie);
795         if (rc)
796                 goto out_restore;
797
798         if (wil->discovery_mode && cmd.cmd.scan_type == WMI_ACTIVE_SCAN) {
799                 cmd.cmd.discovery_mode = 1;
800                 wil_dbg_misc(wil, "active scan with discovery_mode=1\n");
801         }
802
803         if (vif->mid == 0)
804                 wil->radio_wdev = wdev;
805         rc = wmi_send(wil, WMI_START_SCAN_CMDID, vif->mid,
806                       &cmd, sizeof(cmd.cmd) +
807                       cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
808
809 out_restore:
810         if (rc) {
811                 del_timer_sync(&vif->scan_timer);
812                 if (vif->mid == 0)
813                         wil->radio_wdev = wil->main_ndev->ieee80211_ptr;
814                 vif->scan_request = NULL;
815         }
816 out:
817         mutex_unlock(&wil->mutex);
818         return rc;
819 }
820
821 static void wil_cfg80211_abort_scan(struct wiphy *wiphy,
822                                     struct wireless_dev *wdev)
823 {
824         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
825         struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
826
827         wil_dbg_misc(wil, "wdev=0x%p iftype=%d\n", wdev, wdev->iftype);
828
829         mutex_lock(&wil->mutex);
830         mutex_lock(&wil->vif_mutex);
831
832         if (!vif->scan_request)
833                 goto out;
834
835         if (wdev != vif->scan_request->wdev) {
836                 wil_dbg_misc(wil, "abort scan was called on the wrong iface\n");
837                 goto out;
838         }
839
840         if (wdev == wil->p2p_wdev && wil->radio_wdev == wil->p2p_wdev)
841                 wil_p2p_stop_radio_operations(wil);
842         else
843                 wil_abort_scan(vif, true);
844
845 out:
846         mutex_unlock(&wil->vif_mutex);
847         mutex_unlock(&wil->mutex);
848 }
849
850 static void wil_print_crypto(struct wil6210_priv *wil,
851                              struct cfg80211_crypto_settings *c)
852 {
853         int i, n;
854
855         wil_dbg_misc(wil, "WPA versions: 0x%08x cipher group 0x%08x\n",
856                      c->wpa_versions, c->cipher_group);
857         wil_dbg_misc(wil, "Pairwise ciphers [%d] {\n", c->n_ciphers_pairwise);
858         n = min_t(int, c->n_ciphers_pairwise, ARRAY_SIZE(c->ciphers_pairwise));
859         for (i = 0; i < n; i++)
860                 wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
861                              c->ciphers_pairwise[i]);
862         wil_dbg_misc(wil, "}\n");
863         wil_dbg_misc(wil, "AKM suites [%d] {\n", c->n_akm_suites);
864         n = min_t(int, c->n_akm_suites, ARRAY_SIZE(c->akm_suites));
865         for (i = 0; i < n; i++)
866                 wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
867                              c->akm_suites[i]);
868         wil_dbg_misc(wil, "}\n");
869         wil_dbg_misc(wil, "Control port : %d, eth_type 0x%04x no_encrypt %d\n",
870                      c->control_port, be16_to_cpu(c->control_port_ethertype),
871                      c->control_port_no_encrypt);
872 }
873
874 static void wil_print_connect_params(struct wil6210_priv *wil,
875                                      struct cfg80211_connect_params *sme)
876 {
877         wil_info(wil, "Connecting to:\n");
878         if (sme->channel) {
879                 wil_info(wil, "  Channel: %d freq %d\n",
880                          sme->channel->hw_value, sme->channel->center_freq);
881         }
882         if (sme->bssid)
883                 wil_info(wil, "  BSSID: %pM\n", sme->bssid);
884         if (sme->ssid)
885                 print_hex_dump(KERN_INFO, "  SSID: ", DUMP_PREFIX_OFFSET,
886                                16, 1, sme->ssid, sme->ssid_len, true);
887         wil_info(wil, "  Privacy: %s\n", sme->privacy ? "secure" : "open");
888         wil_info(wil, "  PBSS: %d\n", sme->pbss);
889         wil_print_crypto(wil, &sme->crypto);
890 }
891
892 static int wil_cfg80211_connect(struct wiphy *wiphy,
893                                 struct net_device *ndev,
894                                 struct cfg80211_connect_params *sme)
895 {
896         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
897         struct wil6210_vif *vif = ndev_to_vif(ndev);
898         struct cfg80211_bss *bss;
899         struct wmi_connect_cmd conn;
900         const u8 *ssid_eid;
901         const u8 *rsn_eid;
902         int ch;
903         int rc = 0;
904         enum ieee80211_bss_type bss_type = IEEE80211_BSS_TYPE_ESS;
905
906         wil_dbg_misc(wil, "connect, mid=%d\n", vif->mid);
907         wil_print_connect_params(wil, sme);
908
909         if (test_bit(wil_vif_fwconnecting, vif->status) ||
910             test_bit(wil_vif_fwconnected, vif->status))
911                 return -EALREADY;
912
913         if (sme->ie_len > WMI_MAX_IE_LEN) {
914                 wil_err(wil, "IE too large (%td bytes)\n", sme->ie_len);
915                 return -ERANGE;
916         }
917
918         rsn_eid = sme->ie ?
919                         cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
920                         NULL;
921         if (sme->privacy && !rsn_eid)
922                 wil_info(wil, "WSC connection\n");
923
924         if (sme->pbss)
925                 bss_type = IEEE80211_BSS_TYPE_PBSS;
926
927         bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
928                                sme->ssid, sme->ssid_len,
929                                bss_type, IEEE80211_PRIVACY_ANY);
930         if (!bss) {
931                 wil_err(wil, "Unable to find BSS\n");
932                 return -ENOENT;
933         }
934
935         ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
936         if (!ssid_eid) {
937                 wil_err(wil, "No SSID\n");
938                 rc = -ENOENT;
939                 goto out;
940         }
941         vif->privacy = sme->privacy;
942         vif->pbss = sme->pbss;
943
944         if (vif->privacy) {
945                 /* For secure assoc, remove old keys */
946                 rc = wmi_del_cipher_key(vif, 0, bss->bssid,
947                                         WMI_KEY_USE_PAIRWISE);
948                 if (rc) {
949                         wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(PTK) failed\n");
950                         goto out;
951                 }
952                 rc = wmi_del_cipher_key(vif, 0, bss->bssid,
953                                         WMI_KEY_USE_RX_GROUP);
954                 if (rc) {
955                         wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(GTK) failed\n");
956                         goto out;
957                 }
958         }
959
960         /* WMI_SET_APPIE_CMD. ie may contain rsn info as well as other info
961          * elements. Send it also in case it's empty, to erase previously set
962          * ies in FW.
963          */
964         rc = wmi_set_ie(vif, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
965         if (rc)
966                 goto out;
967
968         /* WMI_CONNECT_CMD */
969         memset(&conn, 0, sizeof(conn));
970         switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
971         case WLAN_CAPABILITY_DMG_TYPE_AP:
972                 conn.network_type = WMI_NETTYPE_INFRA;
973                 break;
974         case WLAN_CAPABILITY_DMG_TYPE_PBSS:
975                 conn.network_type = WMI_NETTYPE_P2P;
976                 break;
977         default:
978                 wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
979                         bss->capability);
980                 goto out;
981         }
982         if (vif->privacy) {
983                 if (rsn_eid) { /* regular secure connection */
984                         conn.dot11_auth_mode = WMI_AUTH11_SHARED;
985                         conn.auth_mode = WMI_AUTH_WPA2_PSK;
986                         conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
987                         conn.pairwise_crypto_len = 16;
988                         conn.group_crypto_type = WMI_CRYPT_AES_GCMP;
989                         conn.group_crypto_len = 16;
990                 } else { /* WSC */
991                         conn.dot11_auth_mode = WMI_AUTH11_WSC;
992                         conn.auth_mode = WMI_AUTH_NONE;
993                 }
994         } else { /* insecure connection */
995                 conn.dot11_auth_mode = WMI_AUTH11_OPEN;
996                 conn.auth_mode = WMI_AUTH_NONE;
997         }
998
999         conn.ssid_len = min_t(u8, ssid_eid[1], 32);
1000         memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
1001
1002         ch = bss->channel->hw_value;
1003         if (ch == 0) {
1004                 wil_err(wil, "BSS at unknown frequency %dMhz\n",
1005                         bss->channel->center_freq);
1006                 rc = -EOPNOTSUPP;
1007                 goto out;
1008         }
1009         conn.channel = ch - 1;
1010
1011         ether_addr_copy(conn.bssid, bss->bssid);
1012         ether_addr_copy(conn.dst_mac, bss->bssid);
1013
1014         set_bit(wil_vif_fwconnecting, vif->status);
1015
1016         rc = wmi_send(wil, WMI_CONNECT_CMDID, vif->mid, &conn, sizeof(conn));
1017         if (rc == 0) {
1018                 netif_carrier_on(ndev);
1019                 if (!wil_has_other_active_ifaces(wil, ndev, false, true))
1020                         wil6210_bus_request(wil, WIL_MAX_BUS_REQUEST_KBPS);
1021                 vif->bss = bss;
1022                 /* Connect can take lots of time */
1023                 mod_timer(&vif->connect_timer,
1024                           jiffies + msecs_to_jiffies(5000));
1025         } else {
1026                 clear_bit(wil_vif_fwconnecting, vif->status);
1027         }
1028
1029  out:
1030         cfg80211_put_bss(wiphy, bss);
1031
1032         return rc;
1033 }
1034
1035 static int wil_cfg80211_disconnect(struct wiphy *wiphy,
1036                                    struct net_device *ndev,
1037                                    u16 reason_code)
1038 {
1039         int rc;
1040         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1041         struct wil6210_vif *vif = ndev_to_vif(ndev);
1042
1043         wil_dbg_misc(wil, "disconnect: reason=%d, mid=%d\n",
1044                      reason_code, vif->mid);
1045
1046         if (!(test_bit(wil_vif_fwconnecting, vif->status) ||
1047               test_bit(wil_vif_fwconnected, vif->status))) {
1048                 wil_err(wil, "Disconnect was called while disconnected\n");
1049                 return 0;
1050         }
1051
1052         vif->locally_generated_disc = true;
1053         rc = wmi_call(wil, WMI_DISCONNECT_CMDID, vif->mid, NULL, 0,
1054                       WMI_DISCONNECT_EVENTID, NULL, 0,
1055                       WIL6210_DISCONNECT_TO_MS);
1056         if (rc)
1057                 wil_err(wil, "disconnect error %d\n", rc);
1058
1059         return rc;
1060 }
1061
1062 static int wil_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1063 {
1064         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1065         int rc;
1066
1067         /* these parameters are explicitly not supported */
1068         if (changed & (WIPHY_PARAM_RETRY_LONG |
1069                        WIPHY_PARAM_FRAG_THRESHOLD |
1070                        WIPHY_PARAM_RTS_THRESHOLD))
1071                 return -ENOTSUPP;
1072
1073         if (changed & WIPHY_PARAM_RETRY_SHORT) {
1074                 rc = wmi_set_mgmt_retry(wil, wiphy->retry_short);
1075                 if (rc)
1076                         return rc;
1077         }
1078
1079         return 0;
1080 }
1081
1082 int wil_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
1083                          struct cfg80211_mgmt_tx_params *params,
1084                          u64 *cookie)
1085 {
1086         const u8 *buf = params->buf;
1087         size_t len = params->len;
1088         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1089         struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
1090         int rc;
1091         bool tx_status;
1092
1093         wil_dbg_misc(wil, "mgmt_tx: channel %d offchan %d, wait %d\n",
1094                      params->chan ? params->chan->hw_value : -1,
1095                      params->offchan,
1096                      params->wait);
1097
1098         /* Note, currently we support the "wait" parameter only on AP mode.
1099          * In other modes, user-space must call remain_on_channel before
1100          * mgmt_tx or listen on a channel other than active one.
1101          */
1102
1103         if (params->chan && params->chan->hw_value == 0) {
1104                 wil_err(wil, "invalid channel\n");
1105                 return -EINVAL;
1106         }
1107
1108         if (wdev->iftype != NL80211_IFTYPE_AP) {
1109                 wil_dbg_misc(wil,
1110                              "send WMI_SW_TX_REQ_CMDID on non-AP interfaces\n");
1111                 rc = wmi_mgmt_tx(vif, buf, len);
1112                 goto out;
1113         }
1114
1115         if (!params->chan || params->chan->hw_value == vif->channel) {
1116                 wil_dbg_misc(wil,
1117                              "send WMI_SW_TX_REQ_CMDID for on-channel\n");
1118                 rc = wmi_mgmt_tx(vif, buf, len);
1119                 goto out;
1120         }
1121
1122         if (params->offchan == 0) {
1123                 wil_err(wil,
1124                         "invalid channel params: current %d requested %d, off-channel not allowed\n",
1125                         vif->channel, params->chan->hw_value);
1126                 return -EBUSY;
1127         }
1128
1129         /* use the wmi_mgmt_tx_ext only on AP mode and off-channel */
1130         rc = wmi_mgmt_tx_ext(vif, buf, len, params->chan->hw_value,
1131                              params->wait);
1132
1133 out:
1134         /* when the sent packet was not acked by receiver(ACK=0), rc will
1135          * be -EAGAIN. In this case this function needs to return success,
1136          * the ACK=0 will be reflected in tx_status.
1137          */
1138         tx_status = (rc == 0);
1139         rc = (rc == -EAGAIN) ? 0 : rc;
1140         cfg80211_mgmt_tx_status(wdev, cookie ? *cookie : 0, buf, len,
1141                                 tx_status, GFP_KERNEL);
1142
1143         return rc;
1144 }
1145
1146 static int wil_cfg80211_set_channel(struct wiphy *wiphy,
1147                                     struct cfg80211_chan_def *chandef)
1148 {
1149         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1150
1151         wil->monitor_chandef = *chandef;
1152
1153         return 0;
1154 }
1155
1156 static enum wmi_key_usage wil_detect_key_usage(struct wireless_dev *wdev,
1157                                                bool pairwise)
1158 {
1159         struct wil6210_priv *wil = wdev_to_wil(wdev);
1160         enum wmi_key_usage rc;
1161
1162         if (pairwise) {
1163                 rc = WMI_KEY_USE_PAIRWISE;
1164         } else {
1165                 switch (wdev->iftype) {
1166                 case NL80211_IFTYPE_STATION:
1167                 case NL80211_IFTYPE_P2P_CLIENT:
1168                         rc = WMI_KEY_USE_RX_GROUP;
1169                         break;
1170                 case NL80211_IFTYPE_AP:
1171                 case NL80211_IFTYPE_P2P_GO:
1172                         rc = WMI_KEY_USE_TX_GROUP;
1173                         break;
1174                 default:
1175                         /* TODO: Rx GTK or Tx GTK? */
1176                         wil_err(wil, "Can't determine GTK type\n");
1177                         rc = WMI_KEY_USE_RX_GROUP;
1178                         break;
1179                 }
1180         }
1181         wil_dbg_misc(wil, "detect_key_usage: -> %s\n", key_usage_str[rc]);
1182
1183         return rc;
1184 }
1185
1186 static struct wil_sta_info *
1187 wil_find_sta_by_key_usage(struct wil6210_priv *wil, u8 mid,
1188                           enum wmi_key_usage key_usage, const u8 *mac_addr)
1189 {
1190         int cid = -EINVAL;
1191
1192         if (key_usage == WMI_KEY_USE_TX_GROUP)
1193                 return NULL; /* not needed */
1194
1195         /* supplicant provides Rx group key in STA mode with NULL MAC address */
1196         if (mac_addr)
1197                 cid = wil_find_cid(wil, mid, mac_addr);
1198         else if (key_usage == WMI_KEY_USE_RX_GROUP)
1199                 cid = wil_find_cid_by_idx(wil, mid, 0);
1200         if (cid < 0) {
1201                 wil_err(wil, "No CID for %pM %s\n", mac_addr,
1202                         key_usage_str[key_usage]);
1203                 return ERR_PTR(cid);
1204         }
1205
1206         return &wil->sta[cid];
1207 }
1208
1209 static void wil_set_crypto_rx(u8 key_index, enum wmi_key_usage key_usage,
1210                               struct wil_sta_info *cs,
1211                               struct key_params *params)
1212 {
1213         struct wil_tid_crypto_rx_single *cc;
1214         int tid;
1215
1216         if (!cs)
1217                 return;
1218
1219         switch (key_usage) {
1220         case WMI_KEY_USE_PAIRWISE:
1221                 for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1222                         cc = &cs->tid_crypto_rx[tid].key_id[key_index];
1223                         if (params->seq)
1224                                 memcpy(cc->pn, params->seq,
1225                                        IEEE80211_GCMP_PN_LEN);
1226                         else
1227                                 memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
1228                         cc->key_set = true;
1229                 }
1230                 break;
1231         case WMI_KEY_USE_RX_GROUP:
1232                 cc = &cs->group_crypto_rx.key_id[key_index];
1233                 if (params->seq)
1234                         memcpy(cc->pn, params->seq, IEEE80211_GCMP_PN_LEN);
1235                 else
1236                         memset(cc->pn, 0, IEEE80211_GCMP_PN_LEN);
1237                 cc->key_set = true;
1238                 break;
1239         default:
1240                 break;
1241         }
1242 }
1243
1244 static void wil_del_rx_key(u8 key_index, enum wmi_key_usage key_usage,
1245                            struct wil_sta_info *cs)
1246 {
1247         struct wil_tid_crypto_rx_single *cc;
1248         int tid;
1249
1250         if (!cs)
1251                 return;
1252
1253         switch (key_usage) {
1254         case WMI_KEY_USE_PAIRWISE:
1255                 for (tid = 0; tid < WIL_STA_TID_NUM; tid++) {
1256                         cc = &cs->tid_crypto_rx[tid].key_id[key_index];
1257                         cc->key_set = false;
1258                 }
1259                 break;
1260         case WMI_KEY_USE_RX_GROUP:
1261                 cc = &cs->group_crypto_rx.key_id[key_index];
1262                 cc->key_set = false;
1263                 break;
1264         default:
1265                 break;
1266         }
1267 }
1268
1269 static int wil_cfg80211_add_key(struct wiphy *wiphy,
1270                                 struct net_device *ndev,
1271                                 u8 key_index, bool pairwise,
1272                                 const u8 *mac_addr,
1273                                 struct key_params *params)
1274 {
1275         int rc;
1276         struct wil6210_vif *vif = ndev_to_vif(ndev);
1277         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1278         struct wireless_dev *wdev = vif_to_wdev(vif);
1279         enum wmi_key_usage key_usage = wil_detect_key_usage(wdev, pairwise);
1280         struct wil_sta_info *cs = wil_find_sta_by_key_usage(wil, vif->mid,
1281                                                             key_usage,
1282                                                             mac_addr);
1283
1284         if (!params) {
1285                 wil_err(wil, "NULL params\n");
1286                 return -EINVAL;
1287         }
1288
1289         wil_dbg_misc(wil, "add_key: %pM %s[%d] PN %*phN\n",
1290                      mac_addr, key_usage_str[key_usage], key_index,
1291                      params->seq_len, params->seq);
1292
1293         if (IS_ERR(cs)) {
1294                 wil_err(wil, "Not connected, %pM %s[%d] PN %*phN\n",
1295                         mac_addr, key_usage_str[key_usage], key_index,
1296                         params->seq_len, params->seq);
1297                 return -EINVAL;
1298         }
1299
1300         wil_del_rx_key(key_index, key_usage, cs);
1301
1302         if (params->seq && params->seq_len != IEEE80211_GCMP_PN_LEN) {
1303                 wil_err(wil,
1304                         "Wrong PN len %d, %pM %s[%d] PN %*phN\n",
1305                         params->seq_len, mac_addr,
1306                         key_usage_str[key_usage], key_index,
1307                         params->seq_len, params->seq);
1308                 return -EINVAL;
1309         }
1310
1311         rc = wmi_add_cipher_key(vif, key_index, mac_addr, params->key_len,
1312                                 params->key, key_usage);
1313         if (!rc)
1314                 wil_set_crypto_rx(key_index, key_usage, cs, params);
1315
1316         return rc;
1317 }
1318
1319 static int wil_cfg80211_del_key(struct wiphy *wiphy,
1320                                 struct net_device *ndev,
1321                                 u8 key_index, bool pairwise,
1322                                 const u8 *mac_addr)
1323 {
1324         struct wil6210_vif *vif = ndev_to_vif(ndev);
1325         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1326         struct wireless_dev *wdev = vif_to_wdev(vif);
1327         enum wmi_key_usage key_usage = wil_detect_key_usage(wdev, pairwise);
1328         struct wil_sta_info *cs = wil_find_sta_by_key_usage(wil, vif->mid,
1329                                                             key_usage,
1330                                                             mac_addr);
1331
1332         wil_dbg_misc(wil, "del_key: %pM %s[%d]\n", mac_addr,
1333                      key_usage_str[key_usage], key_index);
1334
1335         if (IS_ERR(cs))
1336                 wil_info(wil, "Not connected, %pM %s[%d]\n",
1337                          mac_addr, key_usage_str[key_usage], key_index);
1338
1339         if (!IS_ERR_OR_NULL(cs))
1340                 wil_del_rx_key(key_index, key_usage, cs);
1341
1342         return wmi_del_cipher_key(vif, key_index, mac_addr, key_usage);
1343 }
1344
1345 /* Need to be present or wiphy_new() will WARN */
1346 static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
1347                                         struct net_device *ndev,
1348                                         u8 key_index, bool unicast,
1349                                         bool multicast)
1350 {
1351         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1352
1353         wil_dbg_misc(wil, "set_default_key: entered\n");
1354         return 0;
1355 }
1356
1357 static int wil_remain_on_channel(struct wiphy *wiphy,
1358                                  struct wireless_dev *wdev,
1359                                  struct ieee80211_channel *chan,
1360                                  unsigned int duration,
1361                                  u64 *cookie)
1362 {
1363         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1364         int rc;
1365
1366         wil_dbg_misc(wil,
1367                      "remain_on_channel: center_freq=%d, duration=%d iftype=%d\n",
1368                      chan->center_freq, duration, wdev->iftype);
1369
1370         rc = wil_p2p_listen(wil, wdev, duration, chan, cookie);
1371         return rc;
1372 }
1373
1374 static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
1375                                         struct wireless_dev *wdev,
1376                                         u64 cookie)
1377 {
1378         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1379         struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
1380
1381         wil_dbg_misc(wil, "cancel_remain_on_channel\n");
1382
1383         return wil_p2p_cancel_listen(vif, cookie);
1384 }
1385
1386 /**
1387  * find a specific IE in a list of IEs
1388  * return a pointer to the beginning of IE in the list
1389  * or NULL if not found
1390  */
1391 static const u8 *_wil_cfg80211_find_ie(const u8 *ies, u16 ies_len, const u8 *ie,
1392                                        u16 ie_len)
1393 {
1394         struct ieee80211_vendor_ie *vie;
1395         u32 oui;
1396
1397         /* IE tag at offset 0, length at offset 1 */
1398         if (ie_len < 2 || 2 + ie[1] > ie_len)
1399                 return NULL;
1400
1401         if (ie[0] != WLAN_EID_VENDOR_SPECIFIC)
1402                 return cfg80211_find_ie(ie[0], ies, ies_len);
1403
1404         /* make sure there is room for 3 bytes OUI + 1 byte OUI type */
1405         if (ie[1] < 4)
1406                 return NULL;
1407         vie = (struct ieee80211_vendor_ie *)ie;
1408         oui = vie->oui[0] << 16 | vie->oui[1] << 8 | vie->oui[2];
1409         return cfg80211_find_vendor_ie(oui, vie->oui_type, ies,
1410                                        ies_len);
1411 }
1412
1413 /**
1414  * merge the IEs in two lists into a single list.
1415  * do not include IEs from the second list which exist in the first list.
1416  * add only vendor specific IEs from second list to keep
1417  * the merged list sorted (since vendor-specific IE has the
1418  * highest tag number)
1419  * caller must free the allocated memory for merged IEs
1420  */
1421 static int _wil_cfg80211_merge_extra_ies(const u8 *ies1, u16 ies1_len,
1422                                          const u8 *ies2, u16 ies2_len,
1423                                          u8 **merged_ies, u16 *merged_len)
1424 {
1425         u8 *buf, *dpos;
1426         const u8 *spos;
1427
1428         if (!ies1)
1429                 ies1_len = 0;
1430
1431         if (!ies2)
1432                 ies2_len = 0;
1433
1434         if (ies1_len == 0 && ies2_len == 0) {
1435                 *merged_ies = NULL;
1436                 *merged_len = 0;
1437                 return 0;
1438         }
1439
1440         buf = kmalloc(ies1_len + ies2_len, GFP_KERNEL);
1441         if (!buf)
1442                 return -ENOMEM;
1443         if (ies1)
1444                 memcpy(buf, ies1, ies1_len);
1445         dpos = buf + ies1_len;
1446         spos = ies2;
1447         while (spos && (spos + 1 < ies2 + ies2_len)) {
1448                 /* IE tag at offset 0, length at offset 1 */
1449                 u16 ielen = 2 + spos[1];
1450
1451                 if (spos + ielen > ies2 + ies2_len)
1452                         break;
1453                 if (spos[0] == WLAN_EID_VENDOR_SPECIFIC &&
1454                     (!ies1 || !_wil_cfg80211_find_ie(ies1, ies1_len,
1455                                                      spos, ielen))) {
1456                         memcpy(dpos, spos, ielen);
1457                         dpos += ielen;
1458                 }
1459                 spos += ielen;
1460         }
1461
1462         *merged_ies = buf;
1463         *merged_len = dpos - buf;
1464         return 0;
1465 }
1466
1467 static void wil_print_bcon_data(struct cfg80211_beacon_data *b)
1468 {
1469         wil_hex_dump_misc("head     ", DUMP_PREFIX_OFFSET, 16, 1,
1470                           b->head, b->head_len, true);
1471         wil_hex_dump_misc("tail     ", DUMP_PREFIX_OFFSET, 16, 1,
1472                           b->tail, b->tail_len, true);
1473         wil_hex_dump_misc("BCON IE  ", DUMP_PREFIX_OFFSET, 16, 1,
1474                           b->beacon_ies, b->beacon_ies_len, true);
1475         wil_hex_dump_misc("PROBE    ", DUMP_PREFIX_OFFSET, 16, 1,
1476                           b->probe_resp, b->probe_resp_len, true);
1477         wil_hex_dump_misc("PROBE IE ", DUMP_PREFIX_OFFSET, 16, 1,
1478                           b->proberesp_ies, b->proberesp_ies_len, true);
1479         wil_hex_dump_misc("ASSOC IE ", DUMP_PREFIX_OFFSET, 16, 1,
1480                           b->assocresp_ies, b->assocresp_ies_len, true);
1481 }
1482
1483 /* internal functions for device reset and starting AP */
1484 static int _wil_cfg80211_set_ies(struct wil6210_vif *vif,
1485                                  struct cfg80211_beacon_data *bcon)
1486 {
1487         int rc;
1488         u16 len = 0, proberesp_len = 0;
1489         u8 *ies = NULL, *proberesp = NULL;
1490
1491         if (bcon->probe_resp) {
1492                 struct ieee80211_mgmt *f =
1493                         (struct ieee80211_mgmt *)bcon->probe_resp;
1494                 size_t hlen = offsetof(struct ieee80211_mgmt,
1495                                        u.probe_resp.variable);
1496                 proberesp = f->u.probe_resp.variable;
1497                 proberesp_len = bcon->probe_resp_len - hlen;
1498         }
1499         rc = _wil_cfg80211_merge_extra_ies(proberesp,
1500                                            proberesp_len,
1501                                            bcon->proberesp_ies,
1502                                            bcon->proberesp_ies_len,
1503                                            &ies, &len);
1504
1505         if (rc)
1506                 goto out;
1507
1508         rc = wmi_set_ie(vif, WMI_FRAME_PROBE_RESP, len, ies);
1509         if (rc)
1510                 goto out;
1511
1512         if (bcon->assocresp_ies)
1513                 rc = wmi_set_ie(vif, WMI_FRAME_ASSOC_RESP,
1514                                 bcon->assocresp_ies_len, bcon->assocresp_ies);
1515         else
1516                 rc = wmi_set_ie(vif, WMI_FRAME_ASSOC_RESP, len, ies);
1517 #if 0 /* to use beacon IE's, remove this #if 0 */
1518         if (rc)
1519                 goto out;
1520
1521         rc = wmi_set_ie(vif, WMI_FRAME_BEACON,
1522                         bcon->tail_len, bcon->tail);
1523 #endif
1524 out:
1525         kfree(ies);
1526         return rc;
1527 }
1528
1529 static int _wil_cfg80211_start_ap(struct wiphy *wiphy,
1530                                   struct net_device *ndev,
1531                                   const u8 *ssid, size_t ssid_len, u32 privacy,
1532                                   int bi, u8 chan,
1533                                   struct cfg80211_beacon_data *bcon,
1534                                   u8 hidden_ssid, u32 pbss)
1535 {
1536         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1537         struct wil6210_vif *vif = ndev_to_vif(ndev);
1538         int rc;
1539         struct wireless_dev *wdev = ndev->ieee80211_ptr;
1540         u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
1541         u8 is_go = (wdev->iftype == NL80211_IFTYPE_P2P_GO);
1542
1543         if (pbss)
1544                 wmi_nettype = WMI_NETTYPE_P2P;
1545
1546         wil_dbg_misc(wil, "start_ap: mid=%d, is_go=%d\n", vif->mid, is_go);
1547         if (is_go && !pbss) {
1548                 wil_err(wil, "P2P GO must be in PBSS\n");
1549                 return -ENOTSUPP;
1550         }
1551
1552         wil_set_recovery_state(wil, fw_recovery_idle);
1553
1554         mutex_lock(&wil->mutex);
1555
1556         if (!wil_has_other_active_ifaces(wil, ndev, true, false)) {
1557                 __wil_down(wil);
1558                 rc = __wil_up(wil);
1559                 if (rc)
1560                         goto out;
1561         }
1562
1563         rc = wmi_set_ssid(vif, ssid_len, ssid);
1564         if (rc)
1565                 goto out;
1566
1567         rc = _wil_cfg80211_set_ies(vif, bcon);
1568         if (rc)
1569                 goto out;
1570
1571         vif->privacy = privacy;
1572         vif->channel = chan;
1573         vif->hidden_ssid = hidden_ssid;
1574         vif->pbss = pbss;
1575
1576         netif_carrier_on(ndev);
1577         if (!wil_has_other_active_ifaces(wil, ndev, false, true))
1578                 wil6210_bus_request(wil, WIL_MAX_BUS_REQUEST_KBPS);
1579
1580         rc = wmi_pcp_start(vif, bi, wmi_nettype, chan, hidden_ssid, is_go);
1581         if (rc)
1582                 goto err_pcp_start;
1583
1584         rc = wil_bcast_init(vif);
1585         if (rc)
1586                 goto err_bcast;
1587
1588         goto out; /* success */
1589
1590 err_bcast:
1591         wmi_pcp_stop(vif);
1592 err_pcp_start:
1593         netif_carrier_off(ndev);
1594         if (!wil_has_other_active_ifaces(wil, ndev, false, true))
1595                 wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
1596 out:
1597         mutex_unlock(&wil->mutex);
1598         return rc;
1599 }
1600
1601 static int wil_cfg80211_change_beacon(struct wiphy *wiphy,
1602                                       struct net_device *ndev,
1603                                       struct cfg80211_beacon_data *bcon)
1604 {
1605         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1606         struct wil6210_vif *vif = ndev_to_vif(ndev);
1607         int rc;
1608         u32 privacy = 0;
1609
1610         wil_dbg_misc(wil, "change_beacon, mid=%d\n", vif->mid);
1611         wil_print_bcon_data(bcon);
1612
1613         if (bcon->tail &&
1614             cfg80211_find_ie(WLAN_EID_RSN, bcon->tail,
1615                              bcon->tail_len))
1616                 privacy = 1;
1617
1618         /* in case privacy has changed, need to restart the AP */
1619         if (vif->privacy != privacy) {
1620                 struct wireless_dev *wdev = ndev->ieee80211_ptr;
1621
1622                 wil_dbg_misc(wil, "privacy changed %d=>%d. Restarting AP\n",
1623                              vif->privacy, privacy);
1624
1625                 rc = _wil_cfg80211_start_ap(wiphy, ndev, wdev->ssid,
1626                                             wdev->ssid_len, privacy,
1627                                             wdev->beacon_interval,
1628                                             vif->channel, bcon,
1629                                             vif->hidden_ssid,
1630                                             vif->pbss);
1631         } else {
1632                 rc = _wil_cfg80211_set_ies(vif, bcon);
1633         }
1634
1635         return rc;
1636 }
1637
1638 static int wil_cfg80211_start_ap(struct wiphy *wiphy,
1639                                  struct net_device *ndev,
1640                                  struct cfg80211_ap_settings *info)
1641 {
1642         int rc;
1643         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1644         struct ieee80211_channel *channel = info->chandef.chan;
1645         struct cfg80211_beacon_data *bcon = &info->beacon;
1646         struct cfg80211_crypto_settings *crypto = &info->crypto;
1647         u8 hidden_ssid;
1648
1649         wil_dbg_misc(wil, "start_ap\n");
1650
1651         if (!channel) {
1652                 wil_err(wil, "AP: No channel???\n");
1653                 return -EINVAL;
1654         }
1655
1656         switch (info->hidden_ssid) {
1657         case NL80211_HIDDEN_SSID_NOT_IN_USE:
1658                 hidden_ssid = WMI_HIDDEN_SSID_DISABLED;
1659                 break;
1660
1661         case NL80211_HIDDEN_SSID_ZERO_LEN:
1662                 hidden_ssid = WMI_HIDDEN_SSID_SEND_EMPTY;
1663                 break;
1664
1665         case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
1666                 hidden_ssid = WMI_HIDDEN_SSID_CLEAR;
1667                 break;
1668
1669         default:
1670                 wil_err(wil, "AP: Invalid hidden SSID %d\n", info->hidden_ssid);
1671                 return -EOPNOTSUPP;
1672         }
1673         wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
1674                      channel->center_freq, info->privacy ? "secure" : "open");
1675         wil_dbg_misc(wil, "Privacy: %d auth_type %d\n",
1676                      info->privacy, info->auth_type);
1677         wil_dbg_misc(wil, "Hidden SSID mode: %d\n",
1678                      info->hidden_ssid);
1679         wil_dbg_misc(wil, "BI %d DTIM %d\n", info->beacon_interval,
1680                      info->dtim_period);
1681         wil_dbg_misc(wil, "PBSS %d\n", info->pbss);
1682         wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
1683                           info->ssid, info->ssid_len, true);
1684         wil_print_bcon_data(bcon);
1685         wil_print_crypto(wil, crypto);
1686
1687         rc = _wil_cfg80211_start_ap(wiphy, ndev,
1688                                     info->ssid, info->ssid_len, info->privacy,
1689                                     info->beacon_interval, channel->hw_value,
1690                                     bcon, hidden_ssid, info->pbss);
1691
1692         return rc;
1693 }
1694
1695 static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
1696                                 struct net_device *ndev)
1697 {
1698         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1699         struct wil6210_vif *vif = ndev_to_vif(ndev);
1700         bool last;
1701
1702         wil_dbg_misc(wil, "stop_ap, mid=%d\n", vif->mid);
1703
1704         netif_carrier_off(ndev);
1705         last = !wil_has_other_active_ifaces(wil, ndev, false, true);
1706         if (last) {
1707                 wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
1708                 wil_set_recovery_state(wil, fw_recovery_idle);
1709                 set_bit(wil_status_resetting, wil->status);
1710         }
1711
1712         mutex_lock(&wil->mutex);
1713
1714         wmi_pcp_stop(vif);
1715
1716         if (last)
1717                 __wil_down(wil);
1718         else
1719                 wil_bcast_fini(vif);
1720
1721         mutex_unlock(&wil->mutex);
1722
1723         return 0;
1724 }
1725
1726 static int wil_cfg80211_add_station(struct wiphy *wiphy,
1727                                     struct net_device *dev,
1728                                     const u8 *mac,
1729                                     struct station_parameters *params)
1730 {
1731         struct wil6210_vif *vif = ndev_to_vif(dev);
1732         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1733
1734         wil_dbg_misc(wil, "add station %pM aid %d mid %d\n",
1735                      mac, params->aid, vif->mid);
1736
1737         if (!disable_ap_sme) {
1738                 wil_err(wil, "not supported with AP SME enabled\n");
1739                 return -EOPNOTSUPP;
1740         }
1741
1742         if (params->aid > WIL_MAX_DMG_AID) {
1743                 wil_err(wil, "invalid aid\n");
1744                 return -EINVAL;
1745         }
1746
1747         return wmi_new_sta(vif, mac, params->aid);
1748 }
1749
1750 static int wil_cfg80211_del_station(struct wiphy *wiphy,
1751                                     struct net_device *dev,
1752                                     struct station_del_parameters *params)
1753 {
1754         struct wil6210_vif *vif = ndev_to_vif(dev);
1755         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1756
1757         wil_dbg_misc(wil, "del_station: %pM, reason=%d mid=%d\n",
1758                      params->mac, params->reason_code, vif->mid);
1759
1760         mutex_lock(&wil->mutex);
1761         wil6210_disconnect(vif, params->mac, params->reason_code, false);
1762         mutex_unlock(&wil->mutex);
1763
1764         return 0;
1765 }
1766
1767 static int wil_cfg80211_change_station(struct wiphy *wiphy,
1768                                        struct net_device *dev,
1769                                        const u8 *mac,
1770                                        struct station_parameters *params)
1771 {
1772         struct wil6210_vif *vif = ndev_to_vif(dev);
1773         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1774         int authorize;
1775         int cid, i;
1776         struct wil_ring_tx_data *txdata = NULL;
1777
1778         wil_dbg_misc(wil, "change station %pM mask 0x%x set 0x%x mid %d\n",
1779                      mac, params->sta_flags_mask, params->sta_flags_set,
1780                      vif->mid);
1781
1782         if (!disable_ap_sme) {
1783                 wil_dbg_misc(wil, "not supported with AP SME enabled\n");
1784                 return -EOPNOTSUPP;
1785         }
1786
1787         if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
1788                 return 0;
1789
1790         cid = wil_find_cid(wil, vif->mid, mac);
1791         if (cid < 0) {
1792                 wil_err(wil, "station not found\n");
1793                 return -ENOLINK;
1794         }
1795
1796         for (i = 0; i < ARRAY_SIZE(wil->ring2cid_tid); i++)
1797                 if (wil->ring2cid_tid[i][0] == cid) {
1798                         txdata = &wil->ring_tx_data[i];
1799                         break;
1800                 }
1801
1802         if (!txdata) {
1803                 wil_err(wil, "ring data not found\n");
1804                 return -ENOLINK;
1805         }
1806
1807         authorize = params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED);
1808         txdata->dot1x_open = authorize ? 1 : 0;
1809         wil_dbg_misc(wil, "cid %d ring %d authorize %d\n", cid, i,
1810                      txdata->dot1x_open);
1811
1812         return 0;
1813 }
1814
1815 /* probe_client handling */
1816 static void wil_probe_client_handle(struct wil6210_priv *wil,
1817                                     struct wil6210_vif *vif,
1818                                     struct wil_probe_client_req *req)
1819 {
1820         struct net_device *ndev = vif_to_ndev(vif);
1821         struct wil_sta_info *sta = &wil->sta[req->cid];
1822         /* assume STA is alive if it is still connected,
1823          * else FW will disconnect it
1824          */
1825         bool alive = (sta->status == wil_sta_connected);
1826
1827         cfg80211_probe_status(ndev, sta->addr, req->cookie, alive,
1828                               0, false, GFP_KERNEL);
1829 }
1830
1831 static struct list_head *next_probe_client(struct wil6210_vif *vif)
1832 {
1833         struct list_head *ret = NULL;
1834
1835         mutex_lock(&vif->probe_client_mutex);
1836
1837         if (!list_empty(&vif->probe_client_pending)) {
1838                 ret = vif->probe_client_pending.next;
1839                 list_del(ret);
1840         }
1841
1842         mutex_unlock(&vif->probe_client_mutex);
1843
1844         return ret;
1845 }
1846
1847 void wil_probe_client_worker(struct work_struct *work)
1848 {
1849         struct wil6210_vif *vif = container_of(work, struct wil6210_vif,
1850                                                probe_client_worker);
1851         struct wil6210_priv *wil = vif_to_wil(vif);
1852         struct wil_probe_client_req *req;
1853         struct list_head *lh;
1854
1855         while ((lh = next_probe_client(vif)) != NULL) {
1856                 req = list_entry(lh, struct wil_probe_client_req, list);
1857
1858                 wil_probe_client_handle(wil, vif, req);
1859                 kfree(req);
1860         }
1861 }
1862
1863 void wil_probe_client_flush(struct wil6210_vif *vif)
1864 {
1865         struct wil_probe_client_req *req, *t;
1866         struct wil6210_priv *wil = vif_to_wil(vif);
1867
1868         wil_dbg_misc(wil, "probe_client_flush\n");
1869
1870         mutex_lock(&vif->probe_client_mutex);
1871
1872         list_for_each_entry_safe(req, t, &vif->probe_client_pending, list) {
1873                 list_del(&req->list);
1874                 kfree(req);
1875         }
1876
1877         mutex_unlock(&vif->probe_client_mutex);
1878 }
1879
1880 static int wil_cfg80211_probe_client(struct wiphy *wiphy,
1881                                      struct net_device *dev,
1882                                      const u8 *peer, u64 *cookie)
1883 {
1884         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1885         struct wil6210_vif *vif = ndev_to_vif(dev);
1886         struct wil_probe_client_req *req;
1887         int cid = wil_find_cid(wil, vif->mid, peer);
1888
1889         wil_dbg_misc(wil, "probe_client: %pM => CID %d MID %d\n",
1890                      peer, cid, vif->mid);
1891
1892         if (cid < 0)
1893                 return -ENOLINK;
1894
1895         req = kzalloc(sizeof(*req), GFP_KERNEL);
1896         if (!req)
1897                 return -ENOMEM;
1898
1899         req->cid = cid;
1900         req->cookie = cid;
1901
1902         mutex_lock(&vif->probe_client_mutex);
1903         list_add_tail(&req->list, &vif->probe_client_pending);
1904         mutex_unlock(&vif->probe_client_mutex);
1905
1906         *cookie = req->cookie;
1907         queue_work(wil->wq_service, &vif->probe_client_worker);
1908         return 0;
1909 }
1910
1911 static int wil_cfg80211_change_bss(struct wiphy *wiphy,
1912                                    struct net_device *dev,
1913                                    struct bss_parameters *params)
1914 {
1915         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1916         struct wil6210_vif *vif = ndev_to_vif(dev);
1917
1918         if (params->ap_isolate >= 0) {
1919                 wil_dbg_misc(wil, "change_bss: ap_isolate MID %d, %d => %d\n",
1920                              vif->mid, vif->ap_isolate, params->ap_isolate);
1921                 vif->ap_isolate = params->ap_isolate;
1922         }
1923
1924         return 0;
1925 }
1926
1927 static int wil_cfg80211_set_power_mgmt(struct wiphy *wiphy,
1928                                        struct net_device *dev,
1929                                        bool enabled, int timeout)
1930 {
1931         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1932         enum wmi_ps_profile_type ps_profile;
1933
1934         wil_dbg_misc(wil, "enabled=%d, timeout=%d\n",
1935                      enabled, timeout);
1936
1937         if (enabled)
1938                 ps_profile = WMI_PS_PROFILE_TYPE_DEFAULT;
1939         else
1940                 ps_profile = WMI_PS_PROFILE_TYPE_PS_DISABLED;
1941
1942         return wil_ps_update(wil, ps_profile);
1943 }
1944
1945 static int wil_cfg80211_suspend(struct wiphy *wiphy,
1946                                 struct cfg80211_wowlan *wow)
1947 {
1948         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1949         int rc;
1950
1951         /* Setting the wakeup trigger based on wow is TBD */
1952
1953         if (test_bit(wil_status_suspended, wil->status)) {
1954                 wil_dbg_pm(wil, "trying to suspend while suspended\n");
1955                 return 0;
1956         }
1957
1958         rc = wil_can_suspend(wil, false);
1959         if (rc)
1960                 goto out;
1961
1962         wil_dbg_pm(wil, "suspending\n");
1963
1964         mutex_lock(&wil->mutex);
1965         mutex_lock(&wil->vif_mutex);
1966         wil_p2p_stop_radio_operations(wil);
1967         wil_abort_scan_all_vifs(wil, true);
1968         mutex_unlock(&wil->vif_mutex);
1969         mutex_unlock(&wil->mutex);
1970
1971 out:
1972         return rc;
1973 }
1974
1975 static int wil_cfg80211_resume(struct wiphy *wiphy)
1976 {
1977         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1978
1979         wil_dbg_pm(wil, "resuming\n");
1980
1981         return 0;
1982 }
1983
1984 static int
1985 wil_cfg80211_sched_scan_start(struct wiphy *wiphy,
1986                               struct net_device *dev,
1987                               struct cfg80211_sched_scan_request *request)
1988 {
1989         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1990         struct wil6210_vif *vif = ndev_to_vif(dev);
1991         int i, rc;
1992
1993         if (vif->mid != 0)
1994                 return -EOPNOTSUPP;
1995
1996         wil_dbg_misc(wil,
1997                      "sched scan start: n_ssids %d, ie_len %zu, flags 0x%x\n",
1998                      request->n_ssids, request->ie_len, request->flags);
1999         for (i = 0; i < request->n_ssids; i++) {
2000                 wil_dbg_misc(wil, "SSID[%d]:", i);
2001                 wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
2002                                   request->ssids[i].ssid,
2003                                   request->ssids[i].ssid_len, true);
2004         }
2005         wil_dbg_misc(wil, "channels:");
2006         for (i = 0; i < request->n_channels; i++)
2007                 wil_dbg_misc(wil, " %d%s", request->channels[i]->hw_value,
2008                              i == request->n_channels - 1 ? "\n" : "");
2009         wil_dbg_misc(wil, "n_match_sets %d, min_rssi_thold %d, delay %d\n",
2010                      request->n_match_sets, request->min_rssi_thold,
2011                      request->delay);
2012         for (i = 0; i < request->n_match_sets; i++) {
2013                 struct cfg80211_match_set *ms = &request->match_sets[i];
2014
2015                 wil_dbg_misc(wil, "MATCHSET[%d]: rssi_thold %d\n",
2016                              i, ms->rssi_thold);
2017                 wil_hex_dump_misc("SSID ", DUMP_PREFIX_OFFSET, 16, 1,
2018                                   ms->ssid.ssid,
2019                                   ms->ssid.ssid_len, true);
2020         }
2021         wil_dbg_misc(wil, "n_scan_plans %d\n", request->n_scan_plans);
2022         for (i = 0; i < request->n_scan_plans; i++) {
2023                 struct cfg80211_sched_scan_plan *sp = &request->scan_plans[i];
2024
2025                 wil_dbg_misc(wil, "SCAN PLAN[%d]: interval %d iterations %d\n",
2026                              i, sp->interval, sp->iterations);
2027         }
2028
2029         rc = wmi_set_ie(vif, WMI_FRAME_PROBE_REQ,
2030                         request->ie_len, request->ie);
2031         if (rc)
2032                 return rc;
2033         return wmi_start_sched_scan(wil, request);
2034 }
2035
2036 static int
2037 wil_cfg80211_sched_scan_stop(struct wiphy *wiphy, struct net_device *dev,
2038                              u64 reqid)
2039 {
2040         struct wil6210_priv *wil = wiphy_to_wil(wiphy);
2041         struct wil6210_vif *vif = ndev_to_vif(dev);
2042         int rc;
2043
2044         if (vif->mid != 0)
2045                 return -EOPNOTSUPP;
2046
2047         rc = wmi_stop_sched_scan(wil);
2048         /* device would return error if it thinks PNO is already stopped.
2049          * ignore the return code so user space and driver gets back in-sync
2050          */
2051         wil_dbg_misc(wil, "sched scan stopped (%d)\n", rc);
2052
2053         return 0;
2054 }
2055
2056 static const struct cfg80211_ops wil_cfg80211_ops = {
2057         .add_virtual_intf = wil_cfg80211_add_iface,
2058         .del_virtual_intf = wil_cfg80211_del_iface,
2059         .scan = wil_cfg80211_scan,
2060         .abort_scan = wil_cfg80211_abort_scan,
2061         .connect = wil_cfg80211_connect,
2062         .disconnect = wil_cfg80211_disconnect,
2063         .set_wiphy_params = wil_cfg80211_set_wiphy_params,
2064         .change_virtual_intf = wil_cfg80211_change_iface,
2065         .get_station = wil_cfg80211_get_station,
2066         .dump_station = wil_cfg80211_dump_station,
2067         .remain_on_channel = wil_remain_on_channel,
2068         .cancel_remain_on_channel = wil_cancel_remain_on_channel,
2069         .mgmt_tx = wil_cfg80211_mgmt_tx,
2070         .set_monitor_channel = wil_cfg80211_set_channel,
2071         .add_key = wil_cfg80211_add_key,
2072         .del_key = wil_cfg80211_del_key,
2073         .set_default_key = wil_cfg80211_set_default_key,
2074         /* AP mode */
2075         .change_beacon = wil_cfg80211_change_beacon,
2076         .start_ap = wil_cfg80211_start_ap,
2077         .stop_ap = wil_cfg80211_stop_ap,
2078         .add_station = wil_cfg80211_add_station,
2079         .del_station = wil_cfg80211_del_station,
2080         .change_station = wil_cfg80211_change_station,
2081         .probe_client = wil_cfg80211_probe_client,
2082         .change_bss = wil_cfg80211_change_bss,
2083         /* P2P device */
2084         .start_p2p_device = wil_cfg80211_start_p2p_device,
2085         .stop_p2p_device = wil_cfg80211_stop_p2p_device,
2086         .set_power_mgmt = wil_cfg80211_set_power_mgmt,
2087         .suspend = wil_cfg80211_suspend,
2088         .resume = wil_cfg80211_resume,
2089         .sched_scan_start = wil_cfg80211_sched_scan_start,
2090         .sched_scan_stop = wil_cfg80211_sched_scan_stop,
2091 };
2092
2093 static void wil_wiphy_init(struct wiphy *wiphy)
2094 {
2095         wiphy->max_scan_ssids = 1;
2096         wiphy->max_scan_ie_len = WMI_MAX_IE_LEN;
2097         wiphy->max_remain_on_channel_duration = WIL_MAX_ROC_DURATION_MS;
2098         wiphy->max_num_pmkids = 0 /* TODO: */;
2099         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
2100                                  BIT(NL80211_IFTYPE_AP) |
2101                                  BIT(NL80211_IFTYPE_P2P_CLIENT) |
2102                                  BIT(NL80211_IFTYPE_P2P_GO) |
2103                                  BIT(NL80211_IFTYPE_P2P_DEVICE) |
2104                                  BIT(NL80211_IFTYPE_MONITOR);
2105         wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
2106                         WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD |
2107                         WIPHY_FLAG_PS_ON_BY_DEFAULT;
2108         if (!disable_ap_sme)
2109                 wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME;
2110         dev_dbg(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
2111                 __func__, wiphy->flags);
2112         wiphy->probe_resp_offload =
2113                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
2114                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
2115                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
2116
2117         wiphy->bands[NL80211_BAND_60GHZ] = &wil_band_60ghz;
2118
2119         /* may change after reading FW capabilities */
2120         wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
2121
2122         wiphy->cipher_suites = wil_cipher_suites;
2123         wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
2124         wiphy->mgmt_stypes = wil_mgmt_stypes;
2125         wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
2126
2127         wiphy->n_vendor_commands = ARRAY_SIZE(wil_nl80211_vendor_commands);
2128         wiphy->vendor_commands = wil_nl80211_vendor_commands;
2129
2130 #ifdef CONFIG_PM
2131         wiphy->wowlan = &wil_wowlan_support;
2132 #endif
2133 }
2134
2135 int wil_cfg80211_iface_combinations_from_fw(
2136         struct wil6210_priv *wil, const struct wil_fw_record_concurrency *conc)
2137 {
2138         struct wiphy *wiphy = wil_to_wiphy(wil);
2139         u32 total_limits = 0;
2140         u16 n_combos;
2141         const struct wil_fw_concurrency_combo *combo;
2142         const struct wil_fw_concurrency_limit *limit;
2143         struct ieee80211_iface_combination *iface_combinations;
2144         struct ieee80211_iface_limit *iface_limit;
2145         int i, j;
2146
2147         if (wiphy->iface_combinations) {
2148                 wil_dbg_misc(wil, "iface_combinations already set, skipping\n");
2149                 return 0;
2150         }
2151
2152         combo = conc->combos;
2153         n_combos = le16_to_cpu(conc->n_combos);
2154         for (i = 0; i < n_combos; i++) {
2155                 total_limits += combo->n_limits;
2156                 limit = combo->limits + combo->n_limits;
2157                 combo = (struct wil_fw_concurrency_combo *)limit;
2158         }
2159
2160         iface_combinations =
2161                 kzalloc(n_combos * sizeof(struct ieee80211_iface_combination) +
2162                         total_limits * sizeof(struct ieee80211_iface_limit),
2163                         GFP_KERNEL);
2164         if (!iface_combinations)
2165                 return -ENOMEM;
2166         iface_limit = (struct ieee80211_iface_limit *)(iface_combinations +
2167                                                        n_combos);
2168         combo = conc->combos;
2169         for (i = 0; i < n_combos; i++) {
2170                 iface_combinations[i].max_interfaces = combo->max_interfaces;
2171                 iface_combinations[i].num_different_channels =
2172                         combo->n_diff_channels;
2173                 iface_combinations[i].beacon_int_infra_match =
2174                         combo->same_bi;
2175                 iface_combinations[i].n_limits = combo->n_limits;
2176                 wil_dbg_misc(wil,
2177                              "iface_combination %d: max_if %d, num_ch %d, bi_match %d\n",
2178                              i, iface_combinations[i].max_interfaces,
2179                              iface_combinations[i].num_different_channels,
2180                              iface_combinations[i].beacon_int_infra_match);
2181                 limit = combo->limits;
2182                 for (j = 0; j < combo->n_limits; j++) {
2183                         iface_limit[j].max = le16_to_cpu(limit[j].max);
2184                         iface_limit[j].types = le16_to_cpu(limit[j].types);
2185                         wil_dbg_misc(wil,
2186                                      "limit %d: max %d types 0x%x\n", j,
2187                                      iface_limit[j].max, iface_limit[j].types);
2188                 }
2189                 iface_combinations[i].limits = iface_limit;
2190                 iface_limit += combo->n_limits;
2191                 limit += combo->n_limits;
2192                 combo = (struct wil_fw_concurrency_combo *)limit;
2193         }
2194
2195         wil_dbg_misc(wil, "multiple VIFs supported, n_mids %d\n", conc->n_mids);
2196         wil->max_vifs = conc->n_mids + 1; /* including main interface */
2197         if (wil->max_vifs > WIL_MAX_VIFS) {
2198                 wil_info(wil, "limited number of VIFs supported(%d, FW %d)\n",
2199                          WIL_MAX_VIFS, wil->max_vifs);
2200                 wil->max_vifs = WIL_MAX_VIFS;
2201         }
2202         wiphy->n_iface_combinations = n_combos;
2203         wiphy->iface_combinations = iface_combinations;
2204         return 0;
2205 }
2206
2207 struct wil6210_priv *wil_cfg80211_init(struct device *dev)
2208 {
2209         struct wiphy *wiphy;
2210         struct wil6210_priv *wil;
2211         struct ieee80211_channel *ch;
2212
2213         dev_dbg(dev, "%s()\n", __func__);
2214
2215         /* Note: the wireless_dev structure is no longer allocated here.
2216          * Instead, it is allocated as part of the net_device structure
2217          * for main interface and each VIF.
2218          */
2219         wiphy = wiphy_new(&wil_cfg80211_ops, sizeof(struct wil6210_priv));
2220         if (!wiphy)
2221                 return ERR_PTR(-ENOMEM);
2222
2223         set_wiphy_dev(wiphy, dev);
2224         wil_wiphy_init(wiphy);
2225
2226         wil = wiphy_to_wil(wiphy);
2227         wil->wiphy = wiphy;
2228
2229         /* default monitor channel */
2230         ch = wiphy->bands[NL80211_BAND_60GHZ]->channels;
2231         cfg80211_chandef_create(&wil->monitor_chandef, ch, NL80211_CHAN_NO_HT);
2232
2233         return wil;
2234 }
2235
2236 void wil_cfg80211_deinit(struct wil6210_priv *wil)
2237 {
2238         struct wiphy *wiphy = wil_to_wiphy(wil);
2239
2240         dev_dbg(wil_to_dev(wil), "%s()\n", __func__);
2241
2242         if (!wiphy)
2243                 return;
2244
2245         kfree(wiphy->iface_combinations);
2246         wiphy->iface_combinations = NULL;
2247
2248         wiphy_free(wiphy);
2249         /* do not access wil6210_priv after returning from here */
2250 }
2251
2252 void wil_p2p_wdev_free(struct wil6210_priv *wil)
2253 {
2254         struct wireless_dev *p2p_wdev;
2255
2256         mutex_lock(&wil->vif_mutex);
2257         p2p_wdev = wil->p2p_wdev;
2258         wil->p2p_wdev = NULL;
2259         wil->radio_wdev = wil->main_ndev->ieee80211_ptr;
2260         mutex_unlock(&wil->vif_mutex);
2261         if (p2p_wdev) {
2262                 cfg80211_unregister_wdev(p2p_wdev);
2263                 kfree(p2p_wdev);
2264         }
2265 }
2266
2267 static int wil_rf_sector_status_to_rc(u8 status)
2268 {
2269         switch (status) {
2270         case WMI_RF_SECTOR_STATUS_SUCCESS:
2271                 return 0;
2272         case WMI_RF_SECTOR_STATUS_BAD_PARAMETERS_ERROR:
2273                 return -EINVAL;
2274         case WMI_RF_SECTOR_STATUS_BUSY_ERROR:
2275                 return -EAGAIN;
2276         case WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR:
2277                 return -EOPNOTSUPP;
2278         default:
2279                 return -EINVAL;
2280         }
2281 }
2282
2283 static int wil_rf_sector_get_cfg(struct wiphy *wiphy,
2284                                  struct wireless_dev *wdev,
2285                                  const void *data, int data_len)
2286 {
2287         struct wil6210_priv *wil = wdev_to_wil(wdev);
2288         struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2289         int rc;
2290         struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2291         u16 sector_index;
2292         u8 sector_type;
2293         u32 rf_modules_vec;
2294         struct wmi_get_rf_sector_params_cmd cmd;
2295         struct {
2296                 struct wmi_cmd_hdr wmi;
2297                 struct wmi_get_rf_sector_params_done_event evt;
2298         } __packed reply = {
2299                 .evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2300         };
2301         struct sk_buff *msg;
2302         struct nlattr *nl_cfgs, *nl_cfg;
2303         u32 i;
2304         struct wmi_rf_sector_info *si;
2305
2306         if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2307                 return -EOPNOTSUPP;
2308
2309         rc = nla_parse(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data, data_len,
2310                        wil_rf_sector_policy, NULL);
2311         if (rc) {
2312                 wil_err(wil, "Invalid rf sector ATTR\n");
2313                 return rc;
2314         }
2315
2316         if (!tb[QCA_ATTR_DMG_RF_SECTOR_INDEX] ||
2317             !tb[QCA_ATTR_DMG_RF_SECTOR_TYPE] ||
2318             !tb[QCA_ATTR_DMG_RF_MODULE_MASK]) {
2319                 wil_err(wil, "Invalid rf sector spec\n");
2320                 return -EINVAL;
2321         }
2322
2323         sector_index = nla_get_u16(
2324                 tb[QCA_ATTR_DMG_RF_SECTOR_INDEX]);
2325         if (sector_index >= WIL_MAX_RF_SECTORS) {
2326                 wil_err(wil, "Invalid sector index %d\n", sector_index);
2327                 return -EINVAL;
2328         }
2329
2330         sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2331         if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2332                 wil_err(wil, "Invalid sector type %d\n", sector_type);
2333                 return -EINVAL;
2334         }
2335
2336         rf_modules_vec = nla_get_u32(
2337                 tb[QCA_ATTR_DMG_RF_MODULE_MASK]);
2338         if (rf_modules_vec >= BIT(WMI_MAX_RF_MODULES_NUM)) {
2339                 wil_err(wil, "Invalid rf module mask 0x%x\n", rf_modules_vec);
2340                 return -EINVAL;
2341         }
2342
2343         cmd.sector_idx = cpu_to_le16(sector_index);
2344         cmd.sector_type = sector_type;
2345         cmd.rf_modules_vec = rf_modules_vec & 0xFF;
2346         rc = wmi_call(wil, WMI_GET_RF_SECTOR_PARAMS_CMDID, vif->mid,
2347                       &cmd, sizeof(cmd), WMI_GET_RF_SECTOR_PARAMS_DONE_EVENTID,
2348                       &reply, sizeof(reply),
2349                       500);
2350         if (rc)
2351                 return rc;
2352         if (reply.evt.status) {
2353                 wil_err(wil, "get rf sector cfg failed with status %d\n",
2354                         reply.evt.status);
2355                 return wil_rf_sector_status_to_rc(reply.evt.status);
2356         }
2357
2358         msg = cfg80211_vendor_cmd_alloc_reply_skb(
2359                 wiphy, 64 * WMI_MAX_RF_MODULES_NUM);
2360         if (!msg)
2361                 return -ENOMEM;
2362
2363         if (nla_put_u64_64bit(msg, QCA_ATTR_TSF,
2364                               le64_to_cpu(reply.evt.tsf),
2365                               QCA_ATTR_PAD))
2366                 goto nla_put_failure;
2367
2368         nl_cfgs = nla_nest_start(msg, QCA_ATTR_DMG_RF_SECTOR_CFG);
2369         if (!nl_cfgs)
2370                 goto nla_put_failure;
2371         for (i = 0; i < WMI_MAX_RF_MODULES_NUM; i++) {
2372                 if (!(rf_modules_vec & BIT(i)))
2373                         continue;
2374                 nl_cfg = nla_nest_start(msg, i);
2375                 if (!nl_cfg)
2376                         goto nla_put_failure;
2377                 si = &reply.evt.sectors_info[i];
2378                 if (nla_put_u8(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX,
2379                                i) ||
2380                     nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0,
2381                                 le32_to_cpu(si->etype0)) ||
2382                     nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1,
2383                                 le32_to_cpu(si->etype1)) ||
2384                     nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2,
2385                                 le32_to_cpu(si->etype2)) ||
2386                     nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI,
2387                                 le32_to_cpu(si->psh_hi)) ||
2388                     nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO,
2389                                 le32_to_cpu(si->psh_lo)) ||
2390                     nla_put_u32(msg, QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16,
2391                                 le32_to_cpu(si->dtype_swch_off)))
2392                         goto nla_put_failure;
2393                 nla_nest_end(msg, nl_cfg);
2394         }
2395
2396         nla_nest_end(msg, nl_cfgs);
2397         rc = cfg80211_vendor_cmd_reply(msg);
2398         return rc;
2399 nla_put_failure:
2400         kfree_skb(msg);
2401         return -ENOBUFS;
2402 }
2403
2404 static int wil_rf_sector_set_cfg(struct wiphy *wiphy,
2405                                  struct wireless_dev *wdev,
2406                                  const void *data, int data_len)
2407 {
2408         struct wil6210_priv *wil = wdev_to_wil(wdev);
2409         struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2410         int rc, tmp;
2411         struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2412         struct nlattr *tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_MAX + 1];
2413         u16 sector_index, rf_module_index;
2414         u8 sector_type;
2415         u32 rf_modules_vec = 0;
2416         struct wmi_set_rf_sector_params_cmd cmd;
2417         struct {
2418                 struct wmi_cmd_hdr wmi;
2419                 struct wmi_set_rf_sector_params_done_event evt;
2420         } __packed reply = {
2421                 .evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2422         };
2423         struct nlattr *nl_cfg;
2424         struct wmi_rf_sector_info *si;
2425
2426         if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2427                 return -EOPNOTSUPP;
2428
2429         rc = nla_parse(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data, data_len,
2430                        wil_rf_sector_policy, NULL);
2431         if (rc) {
2432                 wil_err(wil, "Invalid rf sector ATTR\n");
2433                 return rc;
2434         }
2435
2436         if (!tb[QCA_ATTR_DMG_RF_SECTOR_INDEX] ||
2437             !tb[QCA_ATTR_DMG_RF_SECTOR_TYPE] ||
2438             !tb[QCA_ATTR_DMG_RF_SECTOR_CFG]) {
2439                 wil_err(wil, "Invalid rf sector spec\n");
2440                 return -EINVAL;
2441         }
2442
2443         sector_index = nla_get_u16(
2444                 tb[QCA_ATTR_DMG_RF_SECTOR_INDEX]);
2445         if (sector_index >= WIL_MAX_RF_SECTORS) {
2446                 wil_err(wil, "Invalid sector index %d\n", sector_index);
2447                 return -EINVAL;
2448         }
2449
2450         sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2451         if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2452                 wil_err(wil, "Invalid sector type %d\n", sector_type);
2453                 return -EINVAL;
2454         }
2455
2456         memset(&cmd, 0, sizeof(cmd));
2457
2458         cmd.sector_idx = cpu_to_le16(sector_index);
2459         cmd.sector_type = sector_type;
2460         nla_for_each_nested(nl_cfg, tb[QCA_ATTR_DMG_RF_SECTOR_CFG],
2461                             tmp) {
2462                 rc = nla_parse_nested(tb2, QCA_ATTR_DMG_RF_SECTOR_CFG_MAX,
2463                                       nl_cfg, wil_rf_sector_cfg_policy,
2464                                       NULL);
2465                 if (rc) {
2466                         wil_err(wil, "invalid sector cfg\n");
2467                         return -EINVAL;
2468                 }
2469
2470                 if (!tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX] ||
2471                     !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0] ||
2472                     !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1] ||
2473                     !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2] ||
2474                     !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI] ||
2475                     !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO] ||
2476                     !tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16]) {
2477                         wil_err(wil, "missing cfg params\n");
2478                         return -EINVAL;
2479                 }
2480
2481                 rf_module_index = nla_get_u8(
2482                         tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_MODULE_INDEX]);
2483                 if (rf_module_index >= WMI_MAX_RF_MODULES_NUM) {
2484                         wil_err(wil, "invalid RF module index %d\n",
2485                                 rf_module_index);
2486                         return -EINVAL;
2487                 }
2488                 rf_modules_vec |= BIT(rf_module_index);
2489                 si = &cmd.sectors_info[rf_module_index];
2490                 si->etype0 = cpu_to_le32(nla_get_u32(
2491                         tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE0]));
2492                 si->etype1 = cpu_to_le32(nla_get_u32(
2493                         tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE1]));
2494                 si->etype2 = cpu_to_le32(nla_get_u32(
2495                         tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_ETYPE2]));
2496                 si->psh_hi = cpu_to_le32(nla_get_u32(
2497                         tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_HI]));
2498                 si->psh_lo = cpu_to_le32(nla_get_u32(
2499                         tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_PSH_LO]));
2500                 si->dtype_swch_off = cpu_to_le32(nla_get_u32(
2501                         tb2[QCA_ATTR_DMG_RF_SECTOR_CFG_DTYPE_X16]));
2502         }
2503
2504         cmd.rf_modules_vec = rf_modules_vec & 0xFF;
2505         rc = wmi_call(wil, WMI_SET_RF_SECTOR_PARAMS_CMDID, vif->mid,
2506                       &cmd, sizeof(cmd), WMI_SET_RF_SECTOR_PARAMS_DONE_EVENTID,
2507                       &reply, sizeof(reply),
2508                       500);
2509         if (rc)
2510                 return rc;
2511         return wil_rf_sector_status_to_rc(reply.evt.status);
2512 }
2513
2514 static int wil_rf_sector_get_selected(struct wiphy *wiphy,
2515                                       struct wireless_dev *wdev,
2516                                       const void *data, int data_len)
2517 {
2518         struct wil6210_priv *wil = wdev_to_wil(wdev);
2519         struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2520         int rc;
2521         struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2522         u8 sector_type, mac_addr[ETH_ALEN];
2523         int cid = 0;
2524         struct wmi_get_selected_rf_sector_index_cmd cmd;
2525         struct {
2526                 struct wmi_cmd_hdr wmi;
2527                 struct wmi_get_selected_rf_sector_index_done_event evt;
2528         } __packed reply = {
2529                 .evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2530         };
2531         struct sk_buff *msg;
2532
2533         if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2534                 return -EOPNOTSUPP;
2535
2536         rc = nla_parse(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data, data_len,
2537                        wil_rf_sector_policy, NULL);
2538         if (rc) {
2539                 wil_err(wil, "Invalid rf sector ATTR\n");
2540                 return rc;
2541         }
2542
2543         if (!tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]) {
2544                 wil_err(wil, "Invalid rf sector spec\n");
2545                 return -EINVAL;
2546         }
2547         sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2548         if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2549                 wil_err(wil, "Invalid sector type %d\n", sector_type);
2550                 return -EINVAL;
2551         }
2552
2553         if (tb[QCA_ATTR_MAC_ADDR]) {
2554                 ether_addr_copy(mac_addr, nla_data(tb[QCA_ATTR_MAC_ADDR]));
2555                 cid = wil_find_cid(wil, vif->mid, mac_addr);
2556                 if (cid < 0) {
2557                         wil_err(wil, "invalid MAC address %pM\n", mac_addr);
2558                         return -ENOENT;
2559                 }
2560         } else {
2561                 if (test_bit(wil_vif_fwconnected, vif->status)) {
2562                         wil_err(wil, "must specify MAC address when connected\n");
2563                         return -EINVAL;
2564                 }
2565         }
2566
2567         memset(&cmd, 0, sizeof(cmd));
2568         cmd.cid = (u8)cid;
2569         cmd.sector_type = sector_type;
2570         rc = wmi_call(wil, WMI_GET_SELECTED_RF_SECTOR_INDEX_CMDID, vif->mid,
2571                       &cmd, sizeof(cmd),
2572                       WMI_GET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID,
2573                       &reply, sizeof(reply),
2574                       500);
2575         if (rc)
2576                 return rc;
2577         if (reply.evt.status) {
2578                 wil_err(wil, "get rf selected sector cfg failed with status %d\n",
2579                         reply.evt.status);
2580                 return wil_rf_sector_status_to_rc(reply.evt.status);
2581         }
2582
2583         msg = cfg80211_vendor_cmd_alloc_reply_skb(
2584                 wiphy, 64 * WMI_MAX_RF_MODULES_NUM);
2585         if (!msg)
2586                 return -ENOMEM;
2587
2588         if (nla_put_u64_64bit(msg, QCA_ATTR_TSF,
2589                               le64_to_cpu(reply.evt.tsf),
2590                               QCA_ATTR_PAD) ||
2591             nla_put_u16(msg, QCA_ATTR_DMG_RF_SECTOR_INDEX,
2592                         le16_to_cpu(reply.evt.sector_idx)))
2593                 goto nla_put_failure;
2594
2595         rc = cfg80211_vendor_cmd_reply(msg);
2596         return rc;
2597 nla_put_failure:
2598         kfree_skb(msg);
2599         return -ENOBUFS;
2600 }
2601
2602 static int wil_rf_sector_wmi_set_selected(struct wil6210_priv *wil,
2603                                           u8 mid, u16 sector_index,
2604                                           u8 sector_type, u8 cid)
2605 {
2606         struct wmi_set_selected_rf_sector_index_cmd cmd;
2607         struct {
2608                 struct wmi_cmd_hdr wmi;
2609                 struct wmi_set_selected_rf_sector_index_done_event evt;
2610         } __packed reply = {
2611                 .evt = {.status = WMI_RF_SECTOR_STATUS_NOT_SUPPORTED_ERROR},
2612         };
2613         int rc;
2614
2615         memset(&cmd, 0, sizeof(cmd));
2616         cmd.sector_idx = cpu_to_le16(sector_index);
2617         cmd.sector_type = sector_type;
2618         cmd.cid = (u8)cid;
2619         rc = wmi_call(wil, WMI_SET_SELECTED_RF_SECTOR_INDEX_CMDID, mid,
2620                       &cmd, sizeof(cmd),
2621                       WMI_SET_SELECTED_RF_SECTOR_INDEX_DONE_EVENTID,
2622                       &reply, sizeof(reply),
2623                       500);
2624         if (rc)
2625                 return rc;
2626         return wil_rf_sector_status_to_rc(reply.evt.status);
2627 }
2628
2629 static int wil_rf_sector_set_selected(struct wiphy *wiphy,
2630                                       struct wireless_dev *wdev,
2631                                       const void *data, int data_len)
2632 {
2633         struct wil6210_priv *wil = wdev_to_wil(wdev);
2634         struct wil6210_vif *vif = wdev_to_vif(wil, wdev);
2635         int rc;
2636         struct nlattr *tb[QCA_ATTR_DMG_RF_SECTOR_MAX + 1];
2637         u16 sector_index;
2638         u8 sector_type, mac_addr[ETH_ALEN], i;
2639         int cid = 0;
2640
2641         if (!test_bit(WMI_FW_CAPABILITY_RF_SECTORS, wil->fw_capabilities))
2642                 return -EOPNOTSUPP;
2643
2644         rc = nla_parse(tb, QCA_ATTR_DMG_RF_SECTOR_MAX, data, data_len,
2645                        wil_rf_sector_policy, NULL);
2646         if (rc) {
2647                 wil_err(wil, "Invalid rf sector ATTR\n");
2648                 return rc;
2649         }
2650
2651         if (!tb[QCA_ATTR_DMG_RF_SECTOR_INDEX] ||
2652             !tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]) {
2653                 wil_err(wil, "Invalid rf sector spec\n");
2654                 return -EINVAL;
2655         }
2656
2657         sector_index = nla_get_u16(
2658                 tb[QCA_ATTR_DMG_RF_SECTOR_INDEX]);
2659         if (sector_index >= WIL_MAX_RF_SECTORS &&
2660             sector_index != WMI_INVALID_RF_SECTOR_INDEX) {
2661                 wil_err(wil, "Invalid sector index %d\n", sector_index);
2662                 return -EINVAL;
2663         }
2664
2665         sector_type = nla_get_u8(tb[QCA_ATTR_DMG_RF_SECTOR_TYPE]);
2666         if (sector_type >= QCA_ATTR_DMG_RF_SECTOR_TYPE_MAX) {
2667                 wil_err(wil, "Invalid sector type %d\n", sector_type);
2668                 return -EINVAL;
2669         }
2670
2671         if (tb[QCA_ATTR_MAC_ADDR]) {
2672                 ether_addr_copy(mac_addr, nla_data(tb[QCA_ATTR_MAC_ADDR]));
2673                 if (!is_broadcast_ether_addr(mac_addr)) {
2674                         cid = wil_find_cid(wil, vif->mid, mac_addr);
2675                         if (cid < 0) {
2676                                 wil_err(wil, "invalid MAC address %pM\n",
2677                                         mac_addr);
2678                                 return -ENOENT;
2679                         }
2680                 } else {
2681                         if (sector_index != WMI_INVALID_RF_SECTOR_INDEX) {
2682                                 wil_err(wil, "broadcast MAC valid only with unlocking\n");
2683                                 return -EINVAL;
2684                         }
2685                         cid = -1;
2686                 }
2687         } else {
2688                 if (test_bit(wil_vif_fwconnected, vif->status)) {
2689                         wil_err(wil, "must specify MAC address when connected\n");
2690                         return -EINVAL;
2691                 }
2692                 /* otherwise, using cid=0 for unassociated station */
2693         }
2694
2695         if (cid >= 0) {
2696                 rc = wil_rf_sector_wmi_set_selected(wil, vif->mid, sector_index,
2697                                                     sector_type, cid);
2698         } else {
2699                 /* unlock all cids */
2700                 rc = wil_rf_sector_wmi_set_selected(
2701                         wil, vif->mid, WMI_INVALID_RF_SECTOR_INDEX,
2702                         sector_type, WIL_CID_ALL);
2703                 if (rc == -EINVAL) {
2704                         for (i = 0; i < WIL6210_MAX_CID; i++) {
2705                                 if (wil->sta[i].mid != vif->mid)
2706                                         continue;
2707                                 rc = wil_rf_sector_wmi_set_selected(
2708                                         wil, vif->mid,
2709                                         WMI_INVALID_RF_SECTOR_INDEX,
2710                                         sector_type, i);
2711                                 /* the FW will silently ignore and return
2712                                  * success for unused cid, so abort the loop
2713                                  * on any other error
2714                                  */
2715                                 if (rc) {
2716                                         wil_err(wil, "unlock cid %d failed with status %d\n",
2717                                                 i, rc);
2718                                         break;
2719                                 }
2720                         }
2721                 }
2722         }
2723
2724         return rc;
2725 }