linux/drivers/net/wireless/ath/wil6210/cfg80211.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2012-2015 Qualcomm Atheros, Inc.
   3 *
   4 * Permission to use, copy, modify, and/or distribute this software for any
   5 * purpose with or without fee is hereby granted, provided that the above
   6 * copyright notice and this permission notice appear in all copies.
   7 *
   8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15 */
  16
  17#include <linux/etherdevice.h>
  18#include "wil6210.h"
  19#include "wmi.h"
  20
  21#define CHAN60G(_channel, _flags) {                             \
  22        .band                   = IEEE80211_BAND_60GHZ,         \
  23        .center_freq            = 56160 + (2160 * (_channel)),  \
  24        .hw_value               = (_channel),                   \
  25        .flags                  = (_flags),                     \
  26        .max_antenna_gain       = 0,                            \
  27        .max_power              = 40,                           \
  28}
  29
  30static struct ieee80211_channel wil_60ghz_channels[] = {
  31        CHAN60G(1, 0),
  32        CHAN60G(2, 0),
  33        CHAN60G(3, 0),
  34/* channel 4 not supported yet */
  35};
  36
  37static struct ieee80211_supported_band wil_band_60ghz = {
  38        .channels = wil_60ghz_channels,
  39        .n_channels = ARRAY_SIZE(wil_60ghz_channels),
  40        .ht_cap = {
  41                .ht_supported = true,
  42                .cap = 0, /* TODO */
  43                .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
  44                .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
  45                .mcs = {
  46                                /* MCS 1..12 - SC PHY */
  47                        .rx_mask = {0xfe, 0x1f}, /* 1..12 */
  48                        .tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
  49                },
  50        },
  51};
  52
  53static const struct ieee80211_txrx_stypes
  54wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
  55        [NL80211_IFTYPE_STATION] = {
  56                .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  57                BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
  58                .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  59                BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
  60        },
  61        [NL80211_IFTYPE_AP] = {
  62                .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  63                BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
  64                .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  65                BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
  66        },
  67        [NL80211_IFTYPE_P2P_CLIENT] = {
  68                .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  69                BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
  70                .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  71                BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
  72        },
  73        [NL80211_IFTYPE_P2P_GO] = {
  74                .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  75                BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
  76                .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  77                BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
  78        },
  79};
  80
  81static const u32 wil_cipher_suites[] = {
  82        WLAN_CIPHER_SUITE_GCMP,
  83};
  84
  85int wil_iftype_nl2wmi(enum nl80211_iftype type)
  86{
  87        static const struct {
  88                enum nl80211_iftype nl;
  89                enum wmi_network_type wmi;
  90        } __nl2wmi[] = {
  91                {NL80211_IFTYPE_ADHOC,          WMI_NETTYPE_ADHOC},
  92                {NL80211_IFTYPE_STATION,        WMI_NETTYPE_INFRA},
  93                {NL80211_IFTYPE_AP,             WMI_NETTYPE_AP},
  94                {NL80211_IFTYPE_P2P_CLIENT,     WMI_NETTYPE_P2P},
  95                {NL80211_IFTYPE_P2P_GO,         WMI_NETTYPE_P2P},
  96                {NL80211_IFTYPE_MONITOR,        WMI_NETTYPE_ADHOC}, /* FIXME */
  97        };
  98        uint i;
  99
 100        for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
 101                if (__nl2wmi[i].nl == type)
 102                        return __nl2wmi[i].wmi;
 103        }
 104
 105        return -EOPNOTSUPP;
 106}
 107
 108int wil_cid_fill_sinfo(struct wil6210_priv *wil, int cid,
 109                       struct station_info *sinfo)
 110{
 111        struct wmi_notify_req_cmd cmd = {
 112                .cid = cid,
 113                .interval_usec = 0,
 114        };
 115        struct {
 116                struct wil6210_mbox_hdr_wmi wmi;
 117                struct wmi_notify_req_done_event evt;
 118        } __packed reply;
 119        struct wil_net_stats *stats = &wil->sta[cid].stats;
 120        int rc;
 121
 122        rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
 123                      WMI_NOTIFY_REQ_DONE_EVENTID, &reply, sizeof(reply), 20);
 124        if (rc)
 125                return rc;
 126
 127        wil_dbg_wmi(wil, "Link status for CID %d: {\n"
 128                    "  MCS %d TSF 0x%016llx\n"
 129                    "  BF status 0x%08x SNR 0x%08x SQI %d%%\n"
 130                    "  Tx Tpt %d goodput %d Rx goodput %d\n"
 131                    "  Sectors(rx:tx) my %d:%d peer %d:%d\n""}\n",
 132                    cid, le16_to_cpu(reply.evt.bf_mcs),
 133                    le64_to_cpu(reply.evt.tsf), reply.evt.status,
 134                    le32_to_cpu(reply.evt.snr_val),
 135                    reply.evt.sqi,
 136                    le32_to_cpu(reply.evt.tx_tpt),
 137                    le32_to_cpu(reply.evt.tx_goodput),
 138                    le32_to_cpu(reply.evt.rx_goodput),
 139                    le16_to_cpu(reply.evt.my_rx_sector),
 140                    le16_to_cpu(reply.evt.my_tx_sector),
 141                    le16_to_cpu(reply.evt.other_rx_sector),
 142                    le16_to_cpu(reply.evt.other_tx_sector));
 143
 144        sinfo->generation = wil->sinfo_gen;
 145
 146        sinfo->filled = BIT(NL80211_STA_INFO_RX_BYTES) |
 147                        BIT(NL80211_STA_INFO_TX_BYTES) |
 148                        BIT(NL80211_STA_INFO_RX_PACKETS) |
 149                        BIT(NL80211_STA_INFO_TX_PACKETS) |
 150                        BIT(NL80211_STA_INFO_RX_BITRATE) |
 151                        BIT(NL80211_STA_INFO_TX_BITRATE) |
 152                        BIT(NL80211_STA_INFO_RX_DROP_MISC) |
 153                        BIT(NL80211_STA_INFO_TX_FAILED);
 154
 155        sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
 156        sinfo->txrate.mcs = le16_to_cpu(reply.evt.bf_mcs);
 157        sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
 158        sinfo->rxrate.mcs = stats->last_mcs_rx;
 159        sinfo->rx_bytes = stats->rx_bytes;
 160        sinfo->rx_packets = stats->rx_packets;
 161        sinfo->rx_dropped_misc = stats->rx_dropped;
 162        sinfo->tx_bytes = stats->tx_bytes;
 163        sinfo->tx_packets = stats->tx_packets;
 164        sinfo->tx_failed = stats->tx_errors;
 165
 166        if (test_bit(wil_status_fwconnected, wil->status)) {
 167                sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
 168                sinfo->signal = reply.evt.sqi;
 169        }
 170
 171        return rc;
 172}
 173
 174static int wil_cfg80211_get_station(struct wiphy *wiphy,
 175                                    struct net_device *ndev,
 176                                    const u8 *mac, struct station_info *sinfo)
 177{
 178        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 179        int rc;
 180
 181        int cid = wil_find_cid(wil, mac);
 182
 183        wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
 184        if (cid < 0)
 185                return cid;
 186
 187        rc = wil_cid_fill_sinfo(wil, cid, sinfo);
 188
 189        return rc;
 190}
 191
 192/*
 193 * Find @idx-th active STA for station dump.
 194 */
 195static int wil_find_cid_by_idx(struct wil6210_priv *wil, int idx)
 196{
 197        int i;
 198
 199        for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
 200                if (wil->sta[i].status == wil_sta_unused)
 201                        continue;
 202                if (idx == 0)
 203                        return i;
 204                idx--;
 205        }
 206
 207        return -ENOENT;
 208}
 209
 210static int wil_cfg80211_dump_station(struct wiphy *wiphy,
 211                                     struct net_device *dev, int idx,
 212                                     u8 *mac, struct station_info *sinfo)
 213{
 214        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 215        int rc;
 216        int cid = wil_find_cid_by_idx(wil, idx);
 217
 218        if (cid < 0)
 219                return -ENOENT;
 220
 221        ether_addr_copy(mac, wil->sta[cid].addr);
 222        wil_dbg_misc(wil, "%s(%pM) CID %d\n", __func__, mac, cid);
 223
 224        rc = wil_cid_fill_sinfo(wil, cid, sinfo);
 225
 226        return rc;
 227}
 228
 229static int wil_cfg80211_change_iface(struct wiphy *wiphy,
 230                                     struct net_device *ndev,
 231                                     enum nl80211_iftype type, u32 *flags,
 232                                     struct vif_params *params)
 233{
 234        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 235        struct wireless_dev *wdev = wil->wdev;
 236
 237        switch (type) {
 238        case NL80211_IFTYPE_STATION:
 239        case NL80211_IFTYPE_AP:
 240        case NL80211_IFTYPE_P2P_CLIENT:
 241        case NL80211_IFTYPE_P2P_GO:
 242                break;
 243        case NL80211_IFTYPE_MONITOR:
 244                if (flags)
 245                        wil->monitor_flags = *flags;
 246                else
 247                        wil->monitor_flags = 0;
 248
 249                break;
 250        default:
 251                return -EOPNOTSUPP;
 252        }
 253
 254        wdev->iftype = type;
 255
 256        return 0;
 257}
 258
 259static int wil_cfg80211_scan(struct wiphy *wiphy,
 260                             struct cfg80211_scan_request *request)
 261{
 262        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 263        struct wireless_dev *wdev = wil->wdev;
 264        struct {
 265                struct wmi_start_scan_cmd cmd;
 266                u16 chnl[4];
 267        } __packed cmd;
 268        uint i, n;
 269        int rc;
 270
 271        if (wil->scan_request) {
 272                wil_err(wil, "Already scanning\n");
 273                return -EAGAIN;
 274        }
 275
 276        /* check we are client side */
 277        switch (wdev->iftype) {
 278        case NL80211_IFTYPE_STATION:
 279        case NL80211_IFTYPE_P2P_CLIENT:
 280                break;
 281        default:
 282                return -EOPNOTSUPP;
 283        }
 284
 285        /* FW don't support scan after connection attempt */
 286        if (test_bit(wil_status_dontscan, wil->status)) {
 287                wil_err(wil, "Can't scan now\n");
 288                return -EBUSY;
 289        }
 290
 291        wil_dbg_misc(wil, "Start scan_request 0x%p\n", request);
 292        wil_dbg_misc(wil, "SSID count: %d", request->n_ssids);
 293
 294        for (i = 0; i < request->n_ssids; i++) {
 295                wil_dbg_misc(wil, "SSID[%d]", i);
 296                print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
 297                                     request->ssids[i].ssid,
 298                                     request->ssids[i].ssid_len);
 299        }
 300
 301        if (request->n_ssids)
 302                rc = wmi_set_ssid(wil, request->ssids[0].ssid_len,
 303                                  request->ssids[0].ssid);
 304        else
 305                rc = wmi_set_ssid(wil, 0, NULL);
 306
 307        if (rc) {
 308                wil_err(wil, "set SSID for scan request failed: %d\n", rc);
 309                return rc;
 310        }
 311
 312        wil->scan_request = request;
 313        mod_timer(&wil->scan_timer, jiffies + WIL6210_SCAN_TO);
 314
 315        memset(&cmd, 0, sizeof(cmd));
 316        cmd.cmd.num_channels = 0;
 317        n = min(request->n_channels, 4U);
 318        for (i = 0; i < n; i++) {
 319                int ch = request->channels[i]->hw_value;
 320
 321                if (ch == 0) {
 322                        wil_err(wil,
 323                                "Scan requested for unknown frequency %dMhz\n",
 324                                request->channels[i]->center_freq);
 325                        continue;
 326                }
 327                /* 0-based channel indexes */
 328                cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
 329                wil_dbg_misc(wil, "Scan for ch %d  : %d MHz\n", ch,
 330                             request->channels[i]->center_freq);
 331        }
 332
 333        if (request->ie_len)
 334                print_hex_dump_bytes("Scan IE ", DUMP_PREFIX_OFFSET,
 335                                     request->ie, request->ie_len);
 336        else
 337                wil_dbg_misc(wil, "Scan has no IE's\n");
 338
 339        rc = wmi_set_ie(wil, WMI_FRAME_PROBE_REQ, request->ie_len, request->ie);
 340        if (rc)
 341                goto out;
 342
 343        rc = wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
 344                        cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
 345
 346out:
 347        if (rc) {
 348                del_timer_sync(&wil->scan_timer);
 349                wil->scan_request = NULL;
 350        }
 351
 352        return rc;
 353}
 354
 355static void wil_print_crypto(struct wil6210_priv *wil,
 356                             struct cfg80211_crypto_settings *c)
 357{
 358        int i, n;
 359
 360        wil_dbg_misc(wil, "WPA versions: 0x%08x cipher group 0x%08x\n",
 361                     c->wpa_versions, c->cipher_group);
 362        wil_dbg_misc(wil, "Pairwise ciphers [%d] {\n", c->n_ciphers_pairwise);
 363        n = min_t(int, c->n_ciphers_pairwise, ARRAY_SIZE(c->ciphers_pairwise));
 364        for (i = 0; i < n; i++)
 365                wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
 366                             c->ciphers_pairwise[i]);
 367        wil_dbg_misc(wil, "}\n");
 368        wil_dbg_misc(wil, "AKM suites [%d] {\n", c->n_akm_suites);
 369        n = min_t(int, c->n_akm_suites, ARRAY_SIZE(c->akm_suites));
 370        for (i = 0; i < n; i++)
 371                wil_dbg_misc(wil, "  [%d] = 0x%08x\n", i,
 372                             c->akm_suites[i]);
 373        wil_dbg_misc(wil, "}\n");
 374        wil_dbg_misc(wil, "Control port : %d, eth_type 0x%04x no_encrypt %d\n",
 375                     c->control_port, be16_to_cpu(c->control_port_ethertype),
 376                     c->control_port_no_encrypt);
 377}
 378
 379static void wil_print_connect_params(struct wil6210_priv *wil,
 380                                     struct cfg80211_connect_params *sme)
 381{
 382        wil_info(wil, "Connecting to:\n");
 383        if (sme->channel) {
 384                wil_info(wil, "  Channel: %d freq %d\n",
 385                         sme->channel->hw_value, sme->channel->center_freq);
 386        }
 387        if (sme->bssid)
 388                wil_info(wil, "  BSSID: %pM\n", sme->bssid);
 389        if (sme->ssid)
 390                print_hex_dump(KERN_INFO, "  SSID: ", DUMP_PREFIX_OFFSET,
 391                               16, 1, sme->ssid, sme->ssid_len, true);
 392        wil_info(wil, "  Privacy: %s\n", sme->privacy ? "secure" : "open");
 393        wil_print_crypto(wil, &sme->crypto);
 394}
 395
 396static int wil_cfg80211_connect(struct wiphy *wiphy,
 397                                struct net_device *ndev,
 398                                struct cfg80211_connect_params *sme)
 399{
 400        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 401        struct cfg80211_bss *bss;
 402        struct wmi_connect_cmd conn;
 403        const u8 *ssid_eid;
 404        const u8 *rsn_eid;
 405        int ch;
 406        int rc = 0;
 407
 408        wil_print_connect_params(wil, sme);
 409
 410        if (test_bit(wil_status_fwconnecting, wil->status) ||
 411            test_bit(wil_status_fwconnected, wil->status))
 412                return -EALREADY;
 413
 414        if (sme->ie_len > WMI_MAX_IE_LEN) {
 415                wil_err(wil, "IE too large (%td bytes)\n", sme->ie_len);
 416                return -ERANGE;
 417        }
 418
 419        rsn_eid = sme->ie ?
 420                        cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
 421                        NULL;
 422        if (sme->privacy && !rsn_eid)
 423                wil_info(wil, "WSC connection\n");
 424
 425        bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
 426                               sme->ssid, sme->ssid_len,
 427                               IEEE80211_BSS_TYPE_ESS, IEEE80211_PRIVACY_ANY);
 428        if (!bss) {
 429                wil_err(wil, "Unable to find BSS\n");
 430                return -ENOENT;
 431        }
 432
 433        ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
 434        if (!ssid_eid) {
 435                wil_err(wil, "No SSID\n");
 436                rc = -ENOENT;
 437                goto out;
 438        }
 439        wil->privacy = sme->privacy;
 440
 441        if (wil->privacy) {
 442                /* For secure assoc, remove old keys */
 443                rc = wmi_del_cipher_key(wil, 0, bss->bssid,
 444                                        WMI_KEY_USE_PAIRWISE);
 445                if (rc) {
 446                        wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(PTK) failed\n");
 447                        goto out;
 448                }
 449                rc = wmi_del_cipher_key(wil, 0, bss->bssid,
 450                                        WMI_KEY_USE_RX_GROUP);
 451                if (rc) {
 452                        wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD(GTK) failed\n");
 453                        goto out;
 454                }
 455        }
 456
 457        /* WMI_SET_APPIE_CMD. ie may contain rsn info as well as other info
 458         * elements. Send it also in case it's empty, to erase previously set
 459         * ies in FW.
 460         */
 461        rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
 462        if (rc)
 463                goto out;
 464
 465        /* WMI_CONNECT_CMD */
 466        memset(&conn, 0, sizeof(conn));
 467        switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
 468        case WLAN_CAPABILITY_DMG_TYPE_AP:
 469                conn.network_type = WMI_NETTYPE_INFRA;
 470                break;
 471        case WLAN_CAPABILITY_DMG_TYPE_PBSS:
 472                conn.network_type = WMI_NETTYPE_P2P;
 473                break;
 474        default:
 475                wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
 476                        bss->capability);
 477                goto out;
 478        }
 479        if (wil->privacy) {
 480                if (rsn_eid) { /* regular secure connection */
 481                        conn.dot11_auth_mode = WMI_AUTH11_SHARED;
 482                        conn.auth_mode = WMI_AUTH_WPA2_PSK;
 483                        conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
 484                        conn.pairwise_crypto_len = 16;
 485                        conn.group_crypto_type = WMI_CRYPT_AES_GCMP;
 486                        conn.group_crypto_len = 16;
 487                } else { /* WSC */
 488                        conn.dot11_auth_mode = WMI_AUTH11_WSC;
 489                        conn.auth_mode = WMI_AUTH_NONE;
 490                }
 491        } else { /* insecure connection */
 492                conn.dot11_auth_mode = WMI_AUTH11_OPEN;
 493                conn.auth_mode = WMI_AUTH_NONE;
 494        }
 495
 496        conn.ssid_len = min_t(u8, ssid_eid[1], 32);
 497        memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
 498
 499        ch = bss->channel->hw_value;
 500        if (ch == 0) {
 501                wil_err(wil, "BSS at unknown frequency %dMhz\n",
 502                        bss->channel->center_freq);
 503                rc = -EOPNOTSUPP;
 504                goto out;
 505        }
 506        conn.channel = ch - 1;
 507
 508        ether_addr_copy(conn.bssid, bss->bssid);
 509        ether_addr_copy(conn.dst_mac, bss->bssid);
 510
 511        set_bit(wil_status_fwconnecting, wil->status);
 512
 513        rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
 514        if (rc == 0) {
 515                netif_carrier_on(ndev);
 516                /* Connect can take lots of time */
 517                mod_timer(&wil->connect_timer,
 518                          jiffies + msecs_to_jiffies(2000));
 519        } else {
 520                clear_bit(wil_status_fwconnecting, wil->status);
 521        }
 522
 523 out:
 524        cfg80211_put_bss(wiphy, bss);
 525
 526        return rc;
 527}
 528
 529static int wil_cfg80211_disconnect(struct wiphy *wiphy,
 530                                   struct net_device *ndev,
 531                                   u16 reason_code)
 532{
 533        int rc;
 534        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 535
 536        wil_dbg_misc(wil, "%s(reason=%d)\n", __func__, reason_code);
 537
 538        rc = wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
 539
 540        return rc;
 541}
 542
 543int wil_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
 544                         struct cfg80211_mgmt_tx_params *params,
 545                         u64 *cookie)
 546{
 547        const u8 *buf = params->buf;
 548        size_t len = params->len;
 549        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 550        int rc;
 551        bool tx_status = false;
 552        struct ieee80211_mgmt *mgmt_frame = (void *)buf;
 553        struct wmi_sw_tx_req_cmd *cmd;
 554        struct {
 555                struct wil6210_mbox_hdr_wmi wmi;
 556                struct wmi_sw_tx_complete_event evt;
 557        } __packed evt;
 558
 559        cmd = kmalloc(sizeof(*cmd) + len, GFP_KERNEL);
 560        if (!cmd) {
 561                rc = -ENOMEM;
 562                goto out;
 563        }
 564
 565        memcpy(cmd->dst_mac, mgmt_frame->da, WMI_MAC_LEN);
 566        cmd->len = cpu_to_le16(len);
 567        memcpy(cmd->payload, buf, len);
 568
 569        rc = wmi_call(wil, WMI_SW_TX_REQ_CMDID, cmd, sizeof(*cmd) + len,
 570                      WMI_SW_TX_COMPLETE_EVENTID, &evt, sizeof(evt), 2000);
 571        if (rc == 0)
 572                tx_status = !evt.evt.status;
 573
 574        kfree(cmd);
 575 out:
 576        cfg80211_mgmt_tx_status(wdev, cookie ? *cookie : 0, buf, len,
 577                                tx_status, GFP_KERNEL);
 578        return rc;
 579}
 580
 581static int wil_cfg80211_set_channel(struct wiphy *wiphy,
 582                                    struct cfg80211_chan_def *chandef)
 583{
 584        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 585        struct wireless_dev *wdev = wil->wdev;
 586
 587        wdev->preset_chandef = *chandef;
 588
 589        return 0;
 590}
 591
 592static enum wmi_key_usage wil_detect_key_usage(struct wil6210_priv *wil,
 593                                               bool pairwise)
 594{
 595        struct wireless_dev *wdev = wil->wdev;
 596        enum wmi_key_usage rc;
 597        static const char * const key_usage_str[] = {
 598                [WMI_KEY_USE_PAIRWISE]  = "WMI_KEY_USE_PAIRWISE",
 599                [WMI_KEY_USE_RX_GROUP]  = "WMI_KEY_USE_RX_GROUP",
 600                [WMI_KEY_USE_TX_GROUP]  = "WMI_KEY_USE_TX_GROUP",
 601        };
 602
 603        if (pairwise) {
 604                rc = WMI_KEY_USE_PAIRWISE;
 605        } else {
 606                switch (wdev->iftype) {
 607                case NL80211_IFTYPE_STATION:
 608                        rc = WMI_KEY_USE_RX_GROUP;
 609                        break;
 610                case NL80211_IFTYPE_AP:
 611                        rc = WMI_KEY_USE_TX_GROUP;
 612                        break;
 613                default:
 614                        /* TODO: Rx GTK or Tx GTK? */
 615                        wil_err(wil, "Can't determine GTK type\n");
 616                        rc = WMI_KEY_USE_RX_GROUP;
 617                        break;
 618                }
 619        }
 620        wil_dbg_misc(wil, "%s() -> %s\n", __func__, key_usage_str[rc]);
 621
 622        return rc;
 623}
 624
 625static int wil_cfg80211_add_key(struct wiphy *wiphy,
 626                                struct net_device *ndev,
 627                                u8 key_index, bool pairwise,
 628                                const u8 *mac_addr,
 629                                struct key_params *params)
 630{
 631        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 632        enum wmi_key_usage key_usage = wil_detect_key_usage(wil, pairwise);
 633
 634        wil_dbg_misc(wil, "%s(%pM[%d] %s)\n", __func__, mac_addr, key_index,
 635                     pairwise ? "PTK" : "GTK");
 636
 637        return wmi_add_cipher_key(wil, key_index, mac_addr, params->key_len,
 638                                  params->key, key_usage);
 639}
 640
 641static int wil_cfg80211_del_key(struct wiphy *wiphy,
 642                                struct net_device *ndev,
 643                                u8 key_index, bool pairwise,
 644                                const u8 *mac_addr)
 645{
 646        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 647        enum wmi_key_usage key_usage = wil_detect_key_usage(wil, pairwise);
 648
 649        wil_dbg_misc(wil, "%s(%pM[%d] %s)\n", __func__, mac_addr, key_index,
 650                     pairwise ? "PTK" : "GTK");
 651
 652        return wmi_del_cipher_key(wil, key_index, mac_addr, key_usage);
 653}
 654
 655/* Need to be present or wiphy_new() will WARN */
 656static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
 657                                        struct net_device *ndev,
 658                                        u8 key_index, bool unicast,
 659                                        bool multicast)
 660{
 661        return 0;
 662}
 663
 664static int wil_remain_on_channel(struct wiphy *wiphy,
 665                                 struct wireless_dev *wdev,
 666                                 struct ieee80211_channel *chan,
 667                                 unsigned int duration,
 668                                 u64 *cookie)
 669{
 670        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 671        int rc;
 672
 673        /* TODO: handle duration */
 674        wil_info(wil, "%s(%d, %d ms)\n", __func__, chan->center_freq, duration);
 675
 676        rc = wmi_set_channel(wil, chan->hw_value);
 677        if (rc)
 678                return rc;
 679
 680        rc = wmi_rxon(wil, true);
 681
 682        return rc;
 683}
 684
 685static int wil_cancel_remain_on_channel(struct wiphy *wiphy,
 686                                        struct wireless_dev *wdev,
 687                                        u64 cookie)
 688{
 689        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 690        int rc;
 691
 692        wil_info(wil, "%s()\n", __func__);
 693
 694        rc = wmi_rxon(wil, false);
 695
 696        return rc;
 697}
 698
 699static void wil_print_bcon_data(struct cfg80211_beacon_data *b)
 700{
 701        print_hex_dump_bytes("head     ", DUMP_PREFIX_OFFSET,
 702                             b->head, b->head_len);
 703        print_hex_dump_bytes("tail     ", DUMP_PREFIX_OFFSET,
 704                             b->tail, b->tail_len);
 705        print_hex_dump_bytes("BCON IE  ", DUMP_PREFIX_OFFSET,
 706                             b->beacon_ies, b->beacon_ies_len);
 707        print_hex_dump_bytes("PROBE    ", DUMP_PREFIX_OFFSET,
 708                             b->probe_resp, b->probe_resp_len);
 709        print_hex_dump_bytes("PROBE IE ", DUMP_PREFIX_OFFSET,
 710                             b->proberesp_ies, b->proberesp_ies_len);
 711        print_hex_dump_bytes("ASSOC IE ", DUMP_PREFIX_OFFSET,
 712                             b->assocresp_ies, b->assocresp_ies_len);
 713}
 714
 715static int wil_fix_bcon(struct wil6210_priv *wil,
 716                        struct cfg80211_beacon_data *bcon)
 717{
 718        struct ieee80211_mgmt *f = (struct ieee80211_mgmt *)bcon->probe_resp;
 719        size_t hlen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
 720
 721        if (bcon->probe_resp_len <= hlen)
 722                return 0;
 723
 724/* always use IE's from full probe frame, they has more info
 725 * notable RSN
 726 */
 727        bcon->proberesp_ies = f->u.probe_resp.variable;
 728        bcon->proberesp_ies_len = bcon->probe_resp_len - hlen;
 729        if (!bcon->assocresp_ies) {
 730                bcon->assocresp_ies = bcon->proberesp_ies;
 731                bcon->assocresp_ies_len = bcon->proberesp_ies_len;
 732        }
 733
 734        return 1;
 735}
 736
 737/* internal functions for device reset and starting AP */
 738static int _wil_cfg80211_set_ies(struct wiphy *wiphy,
 739                                 struct cfg80211_beacon_data *bcon)
 740{
 741        int rc;
 742        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 743
 744        rc = wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, bcon->proberesp_ies_len,
 745                        bcon->proberesp_ies);
 746        if (rc)
 747                return rc;
 748
 749        rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, bcon->assocresp_ies_len,
 750                        bcon->assocresp_ies);
 751#if 0 /* to use beacon IE's, remove this #if 0 */
 752        if (rc)
 753                return rc;
 754
 755        rc = wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->tail_len, bcon->tail);
 756#endif
 757
 758        return rc;
 759}
 760
 761static int _wil_cfg80211_start_ap(struct wiphy *wiphy,
 762                                  struct net_device *ndev,
 763                                  const u8 *ssid, size_t ssid_len, u32 privacy,
 764                                  int bi, u8 chan,
 765                                  struct cfg80211_beacon_data *bcon,
 766                                  u8 hidden_ssid)
 767{
 768        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 769        int rc;
 770        struct wireless_dev *wdev = ndev->ieee80211_ptr;
 771        u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
 772
 773        wil_set_recovery_state(wil, fw_recovery_idle);
 774
 775        mutex_lock(&wil->mutex);
 776
 777        __wil_down(wil);
 778        rc = __wil_up(wil);
 779        if (rc)
 780                goto out;
 781
 782        rc = wmi_set_ssid(wil, ssid_len, ssid);
 783        if (rc)
 784                goto out;
 785
 786        rc = _wil_cfg80211_set_ies(wiphy, bcon);
 787        if (rc)
 788                goto out;
 789
 790        wil->privacy = privacy;
 791        wil->channel = chan;
 792        wil->hidden_ssid = hidden_ssid;
 793
 794        netif_carrier_on(ndev);
 795
 796        rc = wmi_pcp_start(wil, bi, wmi_nettype, chan, hidden_ssid);
 797        if (rc)
 798                goto err_pcp_start;
 799
 800        rc = wil_bcast_init(wil);
 801        if (rc)
 802                goto err_bcast;
 803
 804        goto out; /* success */
 805
 806err_bcast:
 807        wmi_pcp_stop(wil);
 808err_pcp_start:
 809        netif_carrier_off(ndev);
 810out:
 811        mutex_unlock(&wil->mutex);
 812        return rc;
 813}
 814
 815static int wil_cfg80211_change_beacon(struct wiphy *wiphy,
 816                                      struct net_device *ndev,
 817                                      struct cfg80211_beacon_data *bcon)
 818{
 819        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 820        int rc;
 821        u32 privacy = 0;
 822
 823        wil_dbg_misc(wil, "%s()\n", __func__);
 824        wil_print_bcon_data(bcon);
 825
 826        if (wil_fix_bcon(wil, bcon)) {
 827                wil_dbg_misc(wil, "Fixed bcon\n");
 828                wil_print_bcon_data(bcon);
 829        }
 830
 831        if (bcon->proberesp_ies &&
 832            cfg80211_find_ie(WLAN_EID_RSN, bcon->proberesp_ies,
 833                             bcon->proberesp_ies_len))
 834                privacy = 1;
 835
 836        /* in case privacy has changed, need to restart the AP */
 837        if (wil->privacy != privacy) {
 838                struct wireless_dev *wdev = ndev->ieee80211_ptr;
 839
 840                wil_dbg_misc(wil, "privacy changed %d=>%d. Restarting AP\n",
 841                             wil->privacy, privacy);
 842
 843                rc = _wil_cfg80211_start_ap(wiphy, ndev, wdev->ssid,
 844                                            wdev->ssid_len, privacy,
 845                                            wdev->beacon_interval,
 846                                            wil->channel, bcon,
 847                                            wil->hidden_ssid);
 848        } else {
 849                rc = _wil_cfg80211_set_ies(wiphy, bcon);
 850        }
 851
 852        return rc;
 853}
 854
 855static int wil_cfg80211_start_ap(struct wiphy *wiphy,
 856                                 struct net_device *ndev,
 857                                 struct cfg80211_ap_settings *info)
 858{
 859        int rc;
 860        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 861        struct ieee80211_channel *channel = info->chandef.chan;
 862        struct cfg80211_beacon_data *bcon = &info->beacon;
 863        struct cfg80211_crypto_settings *crypto = &info->crypto;
 864        u8 hidden_ssid;
 865
 866        wil_dbg_misc(wil, "%s()\n", __func__);
 867
 868        if (!channel) {
 869                wil_err(wil, "AP: No channel???\n");
 870                return -EINVAL;
 871        }
 872
 873        switch (info->hidden_ssid) {
 874        case NL80211_HIDDEN_SSID_NOT_IN_USE:
 875                hidden_ssid = WMI_HIDDEN_SSID_DISABLED;
 876                break;
 877
 878        case NL80211_HIDDEN_SSID_ZERO_LEN:
 879                hidden_ssid = WMI_HIDDEN_SSID_SEND_EMPTY;
 880                break;
 881
 882        case NL80211_HIDDEN_SSID_ZERO_CONTENTS:
 883                hidden_ssid = WMI_HIDDEN_SSID_CLEAR;
 884                break;
 885
 886        default:
 887                wil_err(wil, "AP: Invalid hidden SSID %d\n", info->hidden_ssid);
 888                return -EOPNOTSUPP;
 889        }
 890        wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
 891                     channel->center_freq, info->privacy ? "secure" : "open");
 892        wil_dbg_misc(wil, "Privacy: %d auth_type %d\n",
 893                     info->privacy, info->auth_type);
 894        wil_dbg_misc(wil, "Hidden SSID mode: %d\n",
 895                     info->hidden_ssid);
 896        wil_dbg_misc(wil, "BI %d DTIM %d\n", info->beacon_interval,
 897                     info->dtim_period);
 898        print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
 899                             info->ssid, info->ssid_len);
 900        wil_print_bcon_data(bcon);
 901        wil_print_crypto(wil, crypto);
 902
 903        if (wil_fix_bcon(wil, bcon)) {
 904                wil_dbg_misc(wil, "Fixed bcon\n");
 905                wil_print_bcon_data(bcon);
 906        }
 907
 908        rc = _wil_cfg80211_start_ap(wiphy, ndev,
 909                                    info->ssid, info->ssid_len, info->privacy,
 910                                    info->beacon_interval, channel->hw_value,
 911                                    bcon, hidden_ssid);
 912
 913        return rc;
 914}
 915
 916static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
 917                                struct net_device *ndev)
 918{
 919        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 920
 921        wil_dbg_misc(wil, "%s()\n", __func__);
 922
 923        netif_carrier_off(ndev);
 924        wil_set_recovery_state(wil, fw_recovery_idle);
 925
 926        mutex_lock(&wil->mutex);
 927
 928        wmi_pcp_stop(wil);
 929
 930        __wil_down(wil);
 931
 932        mutex_unlock(&wil->mutex);
 933
 934        return 0;
 935}
 936
 937static int wil_cfg80211_del_station(struct wiphy *wiphy,
 938                                    struct net_device *dev,
 939                                    struct station_del_parameters *params)
 940{
 941        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
 942
 943        wil_dbg_misc(wil, "%s(%pM, reason=%d)\n", __func__, params->mac,
 944                     params->reason_code);
 945
 946        mutex_lock(&wil->mutex);
 947        wil6210_disconnect(wil, params->mac, params->reason_code, false);
 948        mutex_unlock(&wil->mutex);
 949
 950        return 0;
 951}
 952
 953/* probe_client handling */
 954static void wil_probe_client_handle(struct wil6210_priv *wil,
 955                                    struct wil_probe_client_req *req)
 956{
 957        struct net_device *ndev = wil_to_ndev(wil);
 958        struct wil_sta_info *sta = &wil->sta[req->cid];
 959        /* assume STA is alive if it is still connected,
 960         * else FW will disconnect it
 961         */
 962        bool alive = (sta->status == wil_sta_connected);
 963
 964        cfg80211_probe_status(ndev, sta->addr, req->cookie, alive, GFP_KERNEL);
 965}
 966
 967static struct list_head *next_probe_client(struct wil6210_priv *wil)
 968{
 969        struct list_head *ret = NULL;
 970
 971        mutex_lock(&wil->probe_client_mutex);
 972
 973        if (!list_empty(&wil->probe_client_pending)) {
 974                ret = wil->probe_client_pending.next;
 975                list_del(ret);
 976        }
 977
 978        mutex_unlock(&wil->probe_client_mutex);
 979
 980        return ret;
 981}
 982
 983void wil_probe_client_worker(struct work_struct *work)
 984{
 985        struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
 986                                                probe_client_worker);
 987        struct wil_probe_client_req *req;
 988        struct list_head *lh;
 989
 990        while ((lh = next_probe_client(wil)) != NULL) {
 991                req = list_entry(lh, struct wil_probe_client_req, list);
 992
 993                wil_probe_client_handle(wil, req);
 994                kfree(req);
 995        }
 996}
 997
 998void wil_probe_client_flush(struct wil6210_priv *wil)
 999{
1000        struct wil_probe_client_req *req, *t;
1001
1002        wil_dbg_misc(wil, "%s()\n", __func__);
1003
1004        mutex_lock(&wil->probe_client_mutex);
1005
1006        list_for_each_entry_safe(req, t, &wil->probe_client_pending, list) {
1007                list_del(&req->list);
1008                kfree(req);
1009        }
1010
1011        mutex_unlock(&wil->probe_client_mutex);
1012}
1013
1014static int wil_cfg80211_probe_client(struct wiphy *wiphy,
1015                                     struct net_device *dev,
1016                                     const u8 *peer, u64 *cookie)
1017{
1018        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1019        struct wil_probe_client_req *req;
1020        int cid = wil_find_cid(wil, peer);
1021
1022        wil_dbg_misc(wil, "%s(%pM => CID %d)\n", __func__, peer, cid);
1023
1024        if (cid < 0)
1025                return -ENOLINK;
1026
1027        req = kzalloc(sizeof(*req), GFP_KERNEL);
1028        if (!req)
1029                return -ENOMEM;
1030
1031        req->cid = cid;
1032        req->cookie = cid;
1033
1034        mutex_lock(&wil->probe_client_mutex);
1035        list_add_tail(&req->list, &wil->probe_client_pending);
1036        mutex_unlock(&wil->probe_client_mutex);
1037
1038        *cookie = req->cookie;
1039        queue_work(wil->wq_service, &wil->probe_client_worker);
1040        return 0;
1041}
1042
1043static int wil_cfg80211_change_bss(struct wiphy *wiphy,
1044                                   struct net_device *dev,
1045                                   struct bss_parameters *params)
1046{
1047        struct wil6210_priv *wil = wiphy_to_wil(wiphy);
1048
1049        if (params->ap_isolate >= 0) {
1050                wil_dbg_misc(wil, "%s(ap_isolate %d => %d)\n", __func__,
1051                             wil->ap_isolate, params->ap_isolate);
1052                wil->ap_isolate = params->ap_isolate;
1053        }
1054
1055        return 0;
1056}
1057
1058static struct cfg80211_ops wil_cfg80211_ops = {
1059        .scan = wil_cfg80211_scan,
1060        .connect = wil_cfg80211_connect,
1061        .disconnect = wil_cfg80211_disconnect,
1062        .change_virtual_intf = wil_cfg80211_change_iface,
1063        .get_station = wil_cfg80211_get_station,
1064        .dump_station = wil_cfg80211_dump_station,
1065        .remain_on_channel = wil_remain_on_channel,
1066        .cancel_remain_on_channel = wil_cancel_remain_on_channel,
1067        .mgmt_tx = wil_cfg80211_mgmt_tx,
1068        .set_monitor_channel = wil_cfg80211_set_channel,
1069        .add_key = wil_cfg80211_add_key,
1070        .del_key = wil_cfg80211_del_key,
1071        .set_default_key = wil_cfg80211_set_default_key,
1072        /* AP mode */
1073        .change_beacon = wil_cfg80211_change_beacon,
1074        .start_ap = wil_cfg80211_start_ap,
1075        .stop_ap = wil_cfg80211_stop_ap,
1076        .del_station = wil_cfg80211_del_station,
1077        .probe_client = wil_cfg80211_probe_client,
1078        .change_bss = wil_cfg80211_change_bss,
1079};
1080
1081static void wil_wiphy_init(struct wiphy *wiphy)
1082{
1083        wiphy->max_scan_ssids = 1;
1084        wiphy->max_scan_ie_len = WMI_MAX_IE_LEN;
1085        wiphy->max_num_pmkids = 0 /* TODO: */;
1086        wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1087                                 BIT(NL80211_IFTYPE_AP) |
1088                                 BIT(NL80211_IFTYPE_MONITOR);
1089        /* TODO: enable P2P when integrated with supplicant:
1090         * BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO)
1091         */
1092        wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
1093                        WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
1094        dev_dbg(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
1095                __func__, wiphy->flags);
1096        wiphy->probe_resp_offload =
1097                NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
1098                NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
1099                NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
1100
1101        wiphy->bands[IEEE80211_BAND_60GHZ] = &wil_band_60ghz;
1102
1103        /* TODO: figure this out */
1104        wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
1105
1106        wiphy->cipher_suites = wil_cipher_suites;
1107        wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
1108        wiphy->mgmt_stypes = wil_mgmt_stypes;
1109        wiphy->features |= NL80211_FEATURE_SK_TX_STATUS;
1110}
1111
1112struct wireless_dev *wil_cfg80211_init(struct device *dev)
1113{
1114        int rc = 0;
1115        struct wireless_dev *wdev;
1116
1117        dev_dbg(dev, "%s()\n", __func__);
1118
1119        wdev = kzalloc(sizeof(*wdev), GFP_KERNEL);
1120        if (!wdev)
1121                return ERR_PTR(-ENOMEM);
1122
1123        wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
1124                                sizeof(struct wil6210_priv));
1125        if (!wdev->wiphy) {
1126                rc = -ENOMEM;
1127                goto out;
1128        }
1129
1130        set_wiphy_dev(wdev->wiphy, dev);
1131        wil_wiphy_init(wdev->wiphy);
1132
1133        rc = wiphy_register(wdev->wiphy);
1134        if (rc < 0)
1135                goto out_failed_reg;
1136
1137        return wdev;
1138
1139out_failed_reg:
1140        wiphy_free(wdev->wiphy);
1141out:
1142        kfree(wdev);
1143
1144        return ERR_PTR(rc);
1145}
1146
1147void wil_wdev_free(struct wil6210_priv *wil)
1148{
1149        struct wireless_dev *wdev = wil_to_wdev(wil);
1150
1151        dev_dbg(wil_to_dev(wil), "%s()\n", __func__);
1152
1153        if (!wdev)
1154                return;
1155
1156        wiphy_unregister(wdev->wiphy);
1157        wiphy_free(wdev->wiphy);
1158        kfree(wdev);
1159}
1160