linux/drivers/net/wireless/ath/wil6210/main.c
<<
>>
Prefs
   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/moduleparam.h>
  19#include <linux/if_arp.h>
  20#include <linux/etherdevice.h>
  21
  22#include "wil6210.h"
  23#include "txrx.h"
  24#include "wmi.h"
  25#include "boot_loader.h"
  26
  27#define WAIT_FOR_HALP_VOTE_MS 100
  28#define WAIT_FOR_SCAN_ABORT_MS 1000
  29
  30bool debug_fw; /* = false; */
  31module_param(debug_fw, bool, 0444);
  32MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
  33
  34static u8 oob_mode;
  35module_param(oob_mode, byte, 0444);
  36MODULE_PARM_DESC(oob_mode,
  37                 " enable out of the box (OOB) mode in FW, for diagnostics and certification");
  38
  39bool no_fw_recovery;
  40module_param(no_fw_recovery, bool, 0644);
  41MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
  42
  43/* if not set via modparam, will be set to default value of 1/8 of
  44 * rx ring size during init flow
  45 */
  46unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
  47module_param(rx_ring_overflow_thrsh, ushort, 0444);
  48MODULE_PARM_DESC(rx_ring_overflow_thrsh,
  49                 " RX ring overflow threshold in descriptors.");
  50
  51/* We allow allocation of more than 1 page buffers to support large packets.
  52 * It is suboptimal behavior performance wise in case MTU above page size.
  53 */
  54unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
  55static int mtu_max_set(const char *val, const struct kernel_param *kp)
  56{
  57        int ret;
  58
  59        /* sets mtu_max directly. no need to restore it in case of
  60         * illegal value since we assume this will fail insmod
  61         */
  62        ret = param_set_uint(val, kp);
  63        if (ret)
  64                return ret;
  65
  66        if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
  67                ret = -EINVAL;
  68
  69        return ret;
  70}
  71
  72static const struct kernel_param_ops mtu_max_ops = {
  73        .set = mtu_max_set,
  74        .get = param_get_uint,
  75};
  76
  77module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, 0444);
  78MODULE_PARM_DESC(mtu_max, " Max MTU value.");
  79
  80static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
  81static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
  82static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
  83
  84static int ring_order_set(const char *val, const struct kernel_param *kp)
  85{
  86        int ret;
  87        uint x;
  88
  89        ret = kstrtouint(val, 0, &x);
  90        if (ret)
  91                return ret;
  92
  93        if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
  94                return -EINVAL;
  95
  96        *((uint *)kp->arg) = x;
  97
  98        return 0;
  99}
 100
 101static const struct kernel_param_ops ring_order_ops = {
 102        .set = ring_order_set,
 103        .get = param_get_uint,
 104};
 105
 106module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, 0444);
 107MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
 108module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, 0444);
 109MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
 110module_param_cb(bcast_ring_order, &ring_order_ops, &bcast_ring_order, 0444);
 111MODULE_PARM_DESC(bcast_ring_order, " Bcast ring order; size = 1 << order");
 112
 113#define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
 114#define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
 115
 116/*
 117 * Due to a hardware issue,
 118 * one has to read/write to/from NIC in 32-bit chunks;
 119 * regular memcpy_fromio and siblings will
 120 * not work on 64-bit platform - it uses 64-bit transactions
 121 *
 122 * Force 32-bit transactions to enable NIC on 64-bit platforms
 123 *
 124 * To avoid byte swap on big endian host, __raw_{read|write}l
 125 * should be used - {read|write}l would swap bytes to provide
 126 * little endian on PCI value in host endianness.
 127 */
 128void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
 129                          size_t count)
 130{
 131        u32 *d = dst;
 132        const volatile u32 __iomem *s = src;
 133
 134        for (; count >= 4; count -= 4)
 135                *d++ = __raw_readl(s++);
 136
 137        if (unlikely(count)) {
 138                /* count can be 1..3 */
 139                u32 tmp = __raw_readl(s);
 140
 141                memcpy(d, &tmp, count);
 142        }
 143}
 144
 145void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
 146                        size_t count)
 147{
 148        volatile u32 __iomem *d = dst;
 149        const u32 *s = src;
 150
 151        for (; count >= 4; count -= 4)
 152                __raw_writel(*s++, d++);
 153
 154        if (unlikely(count)) {
 155                /* count can be 1..3 */
 156                u32 tmp = 0;
 157
 158                memcpy(&tmp, s, count);
 159                __raw_writel(tmp, d);
 160        }
 161}
 162
 163static void wil_disconnect_cid(struct wil6210_priv *wil, int cid,
 164                               u16 reason_code, bool from_event)
 165__acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
 166{
 167        uint i;
 168        struct net_device *ndev = wil_to_ndev(wil);
 169        struct wireless_dev *wdev = wil->wdev;
 170        struct wil_sta_info *sta = &wil->sta[cid];
 171
 172        might_sleep();
 173        wil_dbg_misc(wil, "disconnect_cid: CID %d, status %d\n",
 174                     cid, sta->status);
 175        /* inform upper/lower layers */
 176        if (sta->status != wil_sta_unused) {
 177                if (!from_event) {
 178                        bool del_sta = (wdev->iftype == NL80211_IFTYPE_AP) ?
 179                                                disable_ap_sme : false;
 180                        wmi_disconnect_sta(wil, sta->addr, reason_code,
 181                                           true, del_sta);
 182                }
 183
 184                switch (wdev->iftype) {
 185                case NL80211_IFTYPE_AP:
 186                case NL80211_IFTYPE_P2P_GO:
 187                        /* AP-like interface */
 188                        cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
 189                        break;
 190                default:
 191                        break;
 192                }
 193                sta->status = wil_sta_unused;
 194        }
 195        /* reorder buffers */
 196        for (i = 0; i < WIL_STA_TID_NUM; i++) {
 197                struct wil_tid_ampdu_rx *r;
 198
 199                spin_lock_bh(&sta->tid_rx_lock);
 200
 201                r = sta->tid_rx[i];
 202                sta->tid_rx[i] = NULL;
 203                wil_tid_ampdu_rx_free(wil, r);
 204
 205                spin_unlock_bh(&sta->tid_rx_lock);
 206        }
 207        /* crypto context */
 208        memset(sta->tid_crypto_rx, 0, sizeof(sta->tid_crypto_rx));
 209        memset(&sta->group_crypto_rx, 0, sizeof(sta->group_crypto_rx));
 210        /* release vrings */
 211        for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
 212                if (wil->vring2cid_tid[i][0] == cid)
 213                        wil_vring_fini_tx(wil, i);
 214        }
 215        /* statistics */
 216        memset(&sta->stats, 0, sizeof(sta->stats));
 217}
 218
 219static bool wil_is_connected(struct wil6210_priv *wil)
 220{
 221        int i;
 222
 223        for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
 224                if (wil->sta[i].status == wil_sta_connected)
 225                        return true;
 226        }
 227
 228        return false;
 229}
 230
 231static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
 232                                u16 reason_code, bool from_event)
 233{
 234        int cid = -ENOENT;
 235        struct net_device *ndev = wil_to_ndev(wil);
 236        struct wireless_dev *wdev = wil->wdev;
 237
 238        if (unlikely(!ndev))
 239                return;
 240
 241        might_sleep();
 242        wil_info(wil, "bssid=%pM, reason=%d, ev%s\n", bssid,
 243                 reason_code, from_event ? "+" : "-");
 244
 245        /* Cases are:
 246         * - disconnect single STA, still connected
 247         * - disconnect single STA, already disconnected
 248         * - disconnect all
 249         *
 250         * For "disconnect all", there are 3 options:
 251         * - bssid == NULL
 252         * - bssid is broadcast address (ff:ff:ff:ff:ff:ff)
 253         * - bssid is our MAC address
 254         */
 255        if (bssid && !is_broadcast_ether_addr(bssid) &&
 256            !ether_addr_equal_unaligned(ndev->dev_addr, bssid)) {
 257                cid = wil_find_cid(wil, bssid);
 258                wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
 259                             bssid, cid, reason_code);
 260                if (cid >= 0) /* disconnect 1 peer */
 261                        wil_disconnect_cid(wil, cid, reason_code, from_event);
 262        } else { /* all */
 263                wil_dbg_misc(wil, "Disconnect all\n");
 264                for (cid = 0; cid < WIL6210_MAX_CID; cid++)
 265                        wil_disconnect_cid(wil, cid, reason_code, from_event);
 266        }
 267
 268        /* link state */
 269        switch (wdev->iftype) {
 270        case NL80211_IFTYPE_STATION:
 271        case NL80211_IFTYPE_P2P_CLIENT:
 272                wil_bcast_fini(wil);
 273                wil_update_net_queues_bh(wil, NULL, true);
 274                netif_carrier_off(ndev);
 275                wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
 276
 277                if (test_bit(wil_status_fwconnected, wil->status)) {
 278                        clear_bit(wil_status_fwconnected, wil->status);
 279                        cfg80211_disconnected(ndev, reason_code,
 280                                              NULL, 0,
 281                                              wil->locally_generated_disc,
 282                                              GFP_KERNEL);
 283                        wil->locally_generated_disc = false;
 284                } else if (test_bit(wil_status_fwconnecting, wil->status)) {
 285                        cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
 286                                                WLAN_STATUS_UNSPECIFIED_FAILURE,
 287                                                GFP_KERNEL);
 288                        wil->bss = NULL;
 289                }
 290                clear_bit(wil_status_fwconnecting, wil->status);
 291                break;
 292        case NL80211_IFTYPE_AP:
 293        case NL80211_IFTYPE_P2P_GO:
 294                if (!wil_is_connected(wil)) {
 295                        wil_update_net_queues_bh(wil, NULL, true);
 296                        clear_bit(wil_status_fwconnected, wil->status);
 297                } else {
 298                        wil_update_net_queues_bh(wil, NULL, false);
 299                }
 300                break;
 301        default:
 302                break;
 303        }
 304}
 305
 306static void wil_disconnect_worker(struct work_struct *work)
 307{
 308        struct wil6210_priv *wil = container_of(work,
 309                        struct wil6210_priv, disconnect_worker);
 310        struct net_device *ndev = wil_to_ndev(wil);
 311        int rc;
 312        struct {
 313                struct wmi_cmd_hdr wmi;
 314                struct wmi_disconnect_event evt;
 315        } __packed reply;
 316
 317        if (test_bit(wil_status_fwconnected, wil->status))
 318                /* connect succeeded after all */
 319                return;
 320
 321        if (!test_bit(wil_status_fwconnecting, wil->status))
 322                /* already disconnected */
 323                return;
 324
 325        rc = wmi_call(wil, WMI_DISCONNECT_CMDID, NULL, 0,
 326                      WMI_DISCONNECT_EVENTID, &reply, sizeof(reply),
 327                      WIL6210_DISCONNECT_TO_MS);
 328        if (rc) {
 329                wil_err(wil, "disconnect error %d\n", rc);
 330                return;
 331        }
 332
 333        wil_update_net_queues_bh(wil, NULL, true);
 334        netif_carrier_off(ndev);
 335        cfg80211_connect_result(ndev, NULL, NULL, 0, NULL, 0,
 336                                WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_KERNEL);
 337        clear_bit(wil_status_fwconnecting, wil->status);
 338}
 339
 340static void wil_connect_timer_fn(struct timer_list *t)
 341{
 342        struct wil6210_priv *wil = from_timer(wil, t, connect_timer);
 343        bool q;
 344
 345        wil_err(wil, "Connect timeout detected, disconnect station\n");
 346
 347        /* reschedule to thread context - disconnect won't
 348         * run from atomic context.
 349         * queue on wmi_wq to prevent race with connect event.
 350         */
 351        q = queue_work(wil->wmi_wq, &wil->disconnect_worker);
 352        wil_dbg_wmi(wil, "queue_work of disconnect_worker -> %d\n", q);
 353}
 354
 355static void wil_scan_timer_fn(struct timer_list *t)
 356{
 357        struct wil6210_priv *wil = from_timer(wil, t, scan_timer);
 358
 359        clear_bit(wil_status_fwready, wil->status);
 360        wil_err(wil, "Scan timeout detected, start fw error recovery\n");
 361        wil_fw_error_recovery(wil);
 362}
 363
 364static int wil_wait_for_recovery(struct wil6210_priv *wil)
 365{
 366        if (wait_event_interruptible(wil->wq, wil->recovery_state !=
 367                                     fw_recovery_pending)) {
 368                wil_err(wil, "Interrupt, canceling recovery\n");
 369                return -ERESTARTSYS;
 370        }
 371        if (wil->recovery_state != fw_recovery_running) {
 372                wil_info(wil, "Recovery cancelled\n");
 373                return -EINTR;
 374        }
 375        wil_info(wil, "Proceed with recovery\n");
 376        return 0;
 377}
 378
 379void wil_set_recovery_state(struct wil6210_priv *wil, int state)
 380{
 381        wil_dbg_misc(wil, "set_recovery_state: %d -> %d\n",
 382                     wil->recovery_state, state);
 383
 384        wil->recovery_state = state;
 385        wake_up_interruptible(&wil->wq);
 386}
 387
 388bool wil_is_recovery_blocked(struct wil6210_priv *wil)
 389{
 390        return no_fw_recovery && (wil->recovery_state == fw_recovery_pending);
 391}
 392
 393static void wil_fw_error_worker(struct work_struct *work)
 394{
 395        struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
 396                                                fw_error_worker);
 397        struct wireless_dev *wdev = wil->wdev;
 398        struct net_device *ndev = wil_to_ndev(wil);
 399
 400        wil_dbg_misc(wil, "fw error worker\n");
 401
 402        if (!(ndev->flags & IFF_UP)) {
 403                wil_info(wil, "No recovery - interface is down\n");
 404                return;
 405        }
 406
 407        /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
 408         * passed since last recovery attempt
 409         */
 410        if (time_is_after_jiffies(wil->last_fw_recovery +
 411                                  WIL6210_FW_RECOVERY_TO))
 412                wil->recovery_count++;
 413        else
 414                wil->recovery_count = 1; /* fw was alive for a long time */
 415
 416        if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
 417                wil_err(wil, "too many recovery attempts (%d), giving up\n",
 418                        wil->recovery_count);
 419                return;
 420        }
 421
 422        wil->last_fw_recovery = jiffies;
 423
 424        wil_info(wil, "fw error recovery requested (try %d)...\n",
 425                 wil->recovery_count);
 426        if (!no_fw_recovery)
 427                wil->recovery_state = fw_recovery_running;
 428        if (wil_wait_for_recovery(wil) != 0)
 429                return;
 430
 431        mutex_lock(&wil->mutex);
 432        switch (wdev->iftype) {
 433        case NL80211_IFTYPE_STATION:
 434        case NL80211_IFTYPE_P2P_CLIENT:
 435        case NL80211_IFTYPE_MONITOR:
 436                /* silent recovery, upper layers will see disconnect */
 437                __wil_down(wil);
 438                __wil_up(wil);
 439                break;
 440        case NL80211_IFTYPE_AP:
 441        case NL80211_IFTYPE_P2P_GO:
 442                wil_info(wil, "No recovery for AP-like interface\n");
 443                /* recovery in these modes is done by upper layers */
 444                break;
 445        default:
 446                wil_err(wil, "No recovery - unknown interface type %d\n",
 447                        wdev->iftype);
 448                break;
 449        }
 450        mutex_unlock(&wil->mutex);
 451}
 452
 453static int wil_find_free_vring(struct wil6210_priv *wil)
 454{
 455        int i;
 456
 457        for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
 458                if (!wil->vring_tx[i].va)
 459                        return i;
 460        }
 461        return -EINVAL;
 462}
 463
 464int wil_tx_init(struct wil6210_priv *wil, int cid)
 465{
 466        int rc = -EINVAL, ringid;
 467
 468        if (cid < 0) {
 469                wil_err(wil, "No connection pending\n");
 470                goto out;
 471        }
 472        ringid = wil_find_free_vring(wil);
 473        if (ringid < 0) {
 474                wil_err(wil, "No free vring found\n");
 475                goto out;
 476        }
 477
 478        wil_dbg_wmi(wil, "Configure for connection CID %d vring %d\n",
 479                    cid, ringid);
 480
 481        rc = wil_vring_init_tx(wil, ringid, 1 << tx_ring_order, cid, 0);
 482        if (rc)
 483                wil_err(wil, "wil_vring_init_tx for CID %d vring %d failed\n",
 484                        cid, ringid);
 485
 486out:
 487        return rc;
 488}
 489
 490int wil_bcast_init(struct wil6210_priv *wil)
 491{
 492        int ri = wil->bcast_vring, rc;
 493
 494        if ((ri >= 0) && wil->vring_tx[ri].va)
 495                return 0;
 496
 497        ri = wil_find_free_vring(wil);
 498        if (ri < 0)
 499                return ri;
 500
 501        wil->bcast_vring = ri;
 502        rc = wil_vring_init_bcast(wil, ri, 1 << bcast_ring_order);
 503        if (rc)
 504                wil->bcast_vring = -1;
 505
 506        return rc;
 507}
 508
 509void wil_bcast_fini(struct wil6210_priv *wil)
 510{
 511        int ri = wil->bcast_vring;
 512
 513        if (ri < 0)
 514                return;
 515
 516        wil->bcast_vring = -1;
 517        wil_vring_fini_tx(wil, ri);
 518}
 519
 520int wil_priv_init(struct wil6210_priv *wil)
 521{
 522        uint i;
 523
 524        wil_dbg_misc(wil, "priv_init\n");
 525
 526        memset(wil->sta, 0, sizeof(wil->sta));
 527        for (i = 0; i < WIL6210_MAX_CID; i++)
 528                spin_lock_init(&wil->sta[i].tid_rx_lock);
 529
 530        for (i = 0; i < WIL6210_MAX_TX_RINGS; i++)
 531                spin_lock_init(&wil->vring_tx_data[i].lock);
 532
 533        mutex_init(&wil->mutex);
 534        mutex_init(&wil->wmi_mutex);
 535        mutex_init(&wil->probe_client_mutex);
 536        mutex_init(&wil->p2p_wdev_mutex);
 537        mutex_init(&wil->halp.lock);
 538
 539        init_completion(&wil->wmi_ready);
 540        init_completion(&wil->wmi_call);
 541        init_completion(&wil->halp.comp);
 542
 543        wil->bcast_vring = -1;
 544        timer_setup(&wil->connect_timer, wil_connect_timer_fn, 0);
 545        timer_setup(&wil->scan_timer, wil_scan_timer_fn, 0);
 546        timer_setup(&wil->p2p.discovery_timer, wil_p2p_discovery_timer_fn, 0);
 547
 548        INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
 549        INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
 550        INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
 551        INIT_WORK(&wil->probe_client_worker, wil_probe_client_worker);
 552        INIT_WORK(&wil->p2p.delayed_listen_work, wil_p2p_delayed_listen_work);
 553
 554        INIT_LIST_HEAD(&wil->pending_wmi_ev);
 555        INIT_LIST_HEAD(&wil->probe_client_pending);
 556        spin_lock_init(&wil->wmi_ev_lock);
 557        spin_lock_init(&wil->net_queue_lock);
 558        wil->net_queue_stopped = 1;
 559        init_waitqueue_head(&wil->wq);
 560
 561        wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
 562        if (!wil->wmi_wq)
 563                return -EAGAIN;
 564
 565        wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
 566        if (!wil->wq_service)
 567                goto out_wmi_wq;
 568
 569        wil->last_fw_recovery = jiffies;
 570        wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
 571        wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
 572        wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
 573        wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
 574
 575        if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
 576                rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
 577
 578        wil->ps_profile =  WMI_PS_PROFILE_TYPE_DEFAULT;
 579
 580        wil->wakeup_trigger = WMI_WAKEUP_TRIGGER_UCAST |
 581                              WMI_WAKEUP_TRIGGER_BCAST;
 582        memset(&wil->suspend_stats, 0, sizeof(wil->suspend_stats));
 583        wil->vring_idle_trsh = 16;
 584
 585        return 0;
 586
 587out_wmi_wq:
 588        destroy_workqueue(wil->wmi_wq);
 589
 590        return -EAGAIN;
 591}
 592
 593void wil6210_bus_request(struct wil6210_priv *wil, u32 kbps)
 594{
 595        if (wil->platform_ops.bus_request) {
 596                wil->bus_request_kbps = kbps;
 597                wil->platform_ops.bus_request(wil->platform_handle, kbps);
 598        }
 599}
 600
 601/**
 602 * wil6210_disconnect - disconnect one connection
 603 * @wil: driver context
 604 * @bssid: peer to disconnect, NULL to disconnect all
 605 * @reason_code: Reason code for the Disassociation frame
 606 * @from_event: whether is invoked from FW event handler
 607 *
 608 * Disconnect and release associated resources. If invoked not from the
 609 * FW event handler, issue WMI command(s) to trigger MAC disconnect.
 610 */
 611void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
 612                        u16 reason_code, bool from_event)
 613{
 614        wil_dbg_misc(wil, "disconnect\n");
 615
 616        del_timer_sync(&wil->connect_timer);
 617        _wil6210_disconnect(wil, bssid, reason_code, from_event);
 618}
 619
 620void wil_priv_deinit(struct wil6210_priv *wil)
 621{
 622        wil_dbg_misc(wil, "priv_deinit\n");
 623
 624        wil_set_recovery_state(wil, fw_recovery_idle);
 625        del_timer_sync(&wil->scan_timer);
 626        del_timer_sync(&wil->p2p.discovery_timer);
 627        cancel_work_sync(&wil->disconnect_worker);
 628        cancel_work_sync(&wil->fw_error_worker);
 629        cancel_work_sync(&wil->p2p.discovery_expired_work);
 630        cancel_work_sync(&wil->p2p.delayed_listen_work);
 631        mutex_lock(&wil->mutex);
 632        wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
 633        mutex_unlock(&wil->mutex);
 634        wmi_event_flush(wil);
 635        wil_probe_client_flush(wil);
 636        cancel_work_sync(&wil->probe_client_worker);
 637        destroy_workqueue(wil->wq_service);
 638        destroy_workqueue(wil->wmi_wq);
 639}
 640
 641static void wil_shutdown_bl(struct wil6210_priv *wil)
 642{
 643        u32 val;
 644
 645        wil_s(wil, RGF_USER_BL +
 646              offsetof(struct bl_dedicated_registers_v1,
 647                       bl_shutdown_handshake), BL_SHUTDOWN_HS_GRTD);
 648
 649        usleep_range(100, 150);
 650
 651        val = wil_r(wil, RGF_USER_BL +
 652                    offsetof(struct bl_dedicated_registers_v1,
 653                             bl_shutdown_handshake));
 654        if (val & BL_SHUTDOWN_HS_RTD) {
 655                wil_dbg_misc(wil, "BL is ready for halt\n");
 656                return;
 657        }
 658
 659        wil_err(wil, "BL did not report ready for halt\n");
 660}
 661
 662/* this format is used by ARC embedded CPU for instruction memory */
 663static inline u32 ARC_me_imm32(u32 d)
 664{
 665        return ((d & 0xffff0000) >> 16) | ((d & 0x0000ffff) << 16);
 666}
 667
 668/* defines access to interrupt vectors for wil_freeze_bl */
 669#define ARC_IRQ_VECTOR_OFFSET(N)        ((N) * 8)
 670/* ARC long jump instruction */
 671#define ARC_JAL_INST                    (0x20200f80)
 672
 673static void wil_freeze_bl(struct wil6210_priv *wil)
 674{
 675        u32 jal, upc, saved;
 676        u32 ivt3 = ARC_IRQ_VECTOR_OFFSET(3);
 677
 678        jal = wil_r(wil, wil->iccm_base + ivt3);
 679        if (jal != ARC_me_imm32(ARC_JAL_INST)) {
 680                wil_dbg_misc(wil, "invalid IVT entry found, skipping\n");
 681                return;
 682        }
 683
 684        /* prevent the target from entering deep sleep
 685         * and disabling memory access
 686         */
 687        saved = wil_r(wil, RGF_USER_USAGE_8);
 688        wil_w(wil, RGF_USER_USAGE_8, saved | BIT_USER_PREVENT_DEEP_SLEEP);
 689        usleep_range(20, 25); /* let the BL process the bit */
 690
 691        /* redirect to endless loop in the INT_L1 context and let it trap */
 692        wil_w(wil, wil->iccm_base + ivt3 + 4, ARC_me_imm32(ivt3));
 693        usleep_range(20, 25); /* let the BL get into the trap */
 694
 695        /* verify the BL is frozen */
 696        upc = wil_r(wil, RGF_USER_CPU_PC);
 697        if (upc < ivt3 || (upc > (ivt3 + 8)))
 698                wil_dbg_misc(wil, "BL freeze failed, PC=0x%08X\n", upc);
 699
 700        wil_w(wil, RGF_USER_USAGE_8, saved);
 701}
 702
 703static void wil_bl_prepare_halt(struct wil6210_priv *wil)
 704{
 705        u32 tmp, ver;
 706
 707        /* before halting device CPU driver must make sure BL is not accessing
 708         * host memory. This is done differently depending on BL version:
 709         * 1. For very old BL versions the procedure is skipped
 710         * (not supported).
 711         * 2. For old BL version we use a special trick to freeze the BL
 712         * 3. For new BL versions we shutdown the BL using handshake procedure.
 713         */
 714        tmp = wil_r(wil, RGF_USER_BL +
 715                    offsetof(struct bl_dedicated_registers_v0,
 716                             boot_loader_struct_version));
 717        if (!tmp) {
 718                wil_dbg_misc(wil, "old BL, skipping halt preperation\n");
 719                return;
 720        }
 721
 722        tmp = wil_r(wil, RGF_USER_BL +
 723                    offsetof(struct bl_dedicated_registers_v1,
 724                             bl_shutdown_handshake));
 725        ver = BL_SHUTDOWN_HS_PROT_VER(tmp);
 726
 727        if (ver > 0)
 728                wil_shutdown_bl(wil);
 729        else
 730                wil_freeze_bl(wil);
 731}
 732
 733static inline void wil_halt_cpu(struct wil6210_priv *wil)
 734{
 735        wil_w(wil, RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
 736        wil_w(wil, RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
 737}
 738
 739static inline void wil_release_cpu(struct wil6210_priv *wil)
 740{
 741        /* Start CPU */
 742        wil_w(wil, RGF_USER_USER_CPU_0, 1);
 743}
 744
 745static void wil_set_oob_mode(struct wil6210_priv *wil, u8 mode)
 746{
 747        wil_info(wil, "oob_mode to %d\n", mode);
 748        switch (mode) {
 749        case 0:
 750                wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE |
 751                      BIT_USER_OOB_R2_MODE);
 752                break;
 753        case 1:
 754                wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
 755                wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
 756                break;
 757        case 2:
 758                wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
 759                wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
 760                break;
 761        default:
 762                wil_err(wil, "invalid oob_mode: %d\n", mode);
 763        }
 764}
 765
 766static int wil_target_reset(struct wil6210_priv *wil, int no_flash)
 767{
 768        int delay = 0;
 769        u32 x, x1 = 0;
 770
 771        wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
 772
 773        /* Clear MAC link up */
 774        wil_s(wil, RGF_HP_CTRL, BIT(15));
 775        wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
 776        wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
 777
 778        wil_halt_cpu(wil);
 779
 780        if (!no_flash) {
 781                /* clear all boot loader "ready" bits */
 782                wil_w(wil, RGF_USER_BL +
 783                      offsetof(struct bl_dedicated_registers_v0,
 784                               boot_loader_ready), 0);
 785                /* this should be safe to write even with old BLs */
 786                wil_w(wil, RGF_USER_BL +
 787                      offsetof(struct bl_dedicated_registers_v1,
 788                               bl_shutdown_handshake), 0);
 789        }
 790        /* Clear Fw Download notification */
 791        wil_c(wil, RGF_USER_USAGE_6, BIT(0));
 792
 793        wil_s(wil, RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
 794        /* XTAL stabilization should take about 3ms */
 795        usleep_range(5000, 7000);
 796        x = wil_r(wil, RGF_CAF_PLL_LOCK_STATUS);
 797        if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
 798                wil_err(wil, "Xtal stabilization timeout\n"
 799                        "RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
 800                return -ETIME;
 801        }
 802        /* switch 10k to XTAL*/
 803        wil_c(wil, RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
 804        /* 40 MHz */
 805        wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
 806
 807        wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
 808        wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
 809
 810        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
 811        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
 812        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
 813        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
 814
 815        wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
 816        wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
 817
 818        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
 819        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
 820        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
 821        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
 822
 823        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
 824        /* reset A2 PCIE AHB */
 825        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
 826
 827        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
 828
 829        /* wait until device ready. typical time is 20..80 msec */
 830        if (no_flash)
 831                do {
 832                        msleep(RST_DELAY);
 833                        x = wil_r(wil, USER_EXT_USER_PMU_3);
 834                        if (delay++ > RST_COUNT) {
 835                                wil_err(wil, "Reset not completed, PMU_3 0x%08x\n",
 836                                        x);
 837                                return -ETIME;
 838                        }
 839                } while ((x & BIT_PMU_DEVICE_RDY) == 0);
 840        else
 841                do {
 842                        msleep(RST_DELAY);
 843                        x = wil_r(wil, RGF_USER_BL +
 844                                  offsetof(struct bl_dedicated_registers_v0,
 845                                           boot_loader_ready));
 846                        if (x1 != x) {
 847                                wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n",
 848                                             x1, x);
 849                                x1 = x;
 850                        }
 851                        if (delay++ > RST_COUNT) {
 852                                wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
 853                                        x);
 854                                return -ETIME;
 855                        }
 856                } while (x != BL_READY);
 857
 858        wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
 859
 860        /* enable fix for HW bug related to the SA/DA swap in AP Rx */
 861        wil_s(wil, RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
 862              BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
 863
 864        if (no_flash) {
 865                /* Reset OTP HW vectors to fit 40MHz */
 866                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME1, 0x60001);
 867                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME2, 0x20027);
 868                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME3, 0x1);
 869                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME4, 0x20027);
 870                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME5, 0x30003);
 871                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME6, 0x20002);
 872                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME7, 0x60001);
 873                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME8, 0x60001);
 874                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME9, 0x60001);
 875                wil_w(wil, RGF_USER_XPM_IFC_RD_TIME10, 0x60001);
 876                wil_w(wil, RGF_USER_XPM_RD_DOUT_SAMPLE_TIME, 0x57);
 877        }
 878
 879        wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
 880        return 0;
 881}
 882
 883static void wil_collect_fw_info(struct wil6210_priv *wil)
 884{
 885        struct wiphy *wiphy = wil_to_wiphy(wil);
 886        u8 retry_short;
 887        int rc;
 888
 889        wil_refresh_fw_capabilities(wil);
 890
 891        rc = wmi_get_mgmt_retry(wil, &retry_short);
 892        if (!rc) {
 893                wiphy->retry_short = retry_short;
 894                wil_dbg_misc(wil, "FW retry_short: %d\n", retry_short);
 895        }
 896}
 897
 898void wil_refresh_fw_capabilities(struct wil6210_priv *wil)
 899{
 900        struct wiphy *wiphy = wil_to_wiphy(wil);
 901        int features;
 902
 903        wil->keep_radio_on_during_sleep =
 904                test_bit(WIL_PLATFORM_CAPA_RADIO_ON_IN_SUSPEND,
 905                         wil->platform_capa) &&
 906                test_bit(WMI_FW_CAPABILITY_D3_SUSPEND, wil->fw_capabilities);
 907
 908        wil_info(wil, "keep_radio_on_during_sleep (%d)\n",
 909                 wil->keep_radio_on_during_sleep);
 910
 911        if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
 912                wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
 913        else
 914                wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
 915
 916        if (test_bit(WMI_FW_CAPABILITY_PNO, wil->fw_capabilities)) {
 917                wiphy->max_sched_scan_reqs = 1;
 918                wiphy->max_sched_scan_ssids = WMI_MAX_PNO_SSID_NUM;
 919                wiphy->max_match_sets = WMI_MAX_PNO_SSID_NUM;
 920                wiphy->max_sched_scan_ie_len = WMI_MAX_IE_LEN;
 921                wiphy->max_sched_scan_plans = WMI_MAX_PLANS_NUM;
 922        }
 923
 924        if (wil->platform_ops.set_features) {
 925                features = (test_bit(WMI_FW_CAPABILITY_REF_CLOCK_CONTROL,
 926                                     wil->fw_capabilities) &&
 927                            test_bit(WIL_PLATFORM_CAPA_EXT_CLK,
 928                                     wil->platform_capa)) ?
 929                        BIT(WIL_PLATFORM_FEATURE_FW_EXT_CLK_CONTROL) : 0;
 930
 931                wil->platform_ops.set_features(wil->platform_handle, features);
 932        }
 933}
 934
 935void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
 936{
 937        le32_to_cpus(&r->base);
 938        le16_to_cpus(&r->entry_size);
 939        le16_to_cpus(&r->size);
 940        le32_to_cpus(&r->tail);
 941        le32_to_cpus(&r->head);
 942}
 943
 944static int wil_get_bl_info(struct wil6210_priv *wil)
 945{
 946        struct net_device *ndev = wil_to_ndev(wil);
 947        struct wiphy *wiphy = wil_to_wiphy(wil);
 948        union {
 949                struct bl_dedicated_registers_v0 bl0;
 950                struct bl_dedicated_registers_v1 bl1;
 951        } bl;
 952        u32 bl_ver;
 953        u8 *mac;
 954        u16 rf_status;
 955
 956        wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL),
 957                             sizeof(bl));
 958        bl_ver = le32_to_cpu(bl.bl0.boot_loader_struct_version);
 959        mac = bl.bl0.mac_address;
 960
 961        if (bl_ver == 0) {
 962                le32_to_cpus(&bl.bl0.rf_type);
 963                le32_to_cpus(&bl.bl0.baseband_type);
 964                rf_status = 0; /* actually, unknown */
 965                wil_info(wil,
 966                         "Boot Loader struct v%d: MAC = %pM RF = 0x%08x bband = 0x%08x\n",
 967                         bl_ver, mac,
 968                         bl.bl0.rf_type, bl.bl0.baseband_type);
 969                wil_info(wil, "Boot Loader build unknown for struct v0\n");
 970        } else {
 971                le16_to_cpus(&bl.bl1.rf_type);
 972                rf_status = le16_to_cpu(bl.bl1.rf_status);
 973                le32_to_cpus(&bl.bl1.baseband_type);
 974                le16_to_cpus(&bl.bl1.bl_version_subminor);
 975                le16_to_cpus(&bl.bl1.bl_version_build);
 976                wil_info(wil,
 977                         "Boot Loader struct v%d: MAC = %pM RF = 0x%04x (status 0x%04x) bband = 0x%08x\n",
 978                         bl_ver, mac,
 979                         bl.bl1.rf_type, rf_status,
 980                         bl.bl1.baseband_type);
 981                wil_info(wil, "Boot Loader build %d.%d.%d.%d\n",
 982                         bl.bl1.bl_version_major, bl.bl1.bl_version_minor,
 983                         bl.bl1.bl_version_subminor, bl.bl1.bl_version_build);
 984        }
 985
 986        if (!is_valid_ether_addr(mac)) {
 987                wil_err(wil, "BL: Invalid MAC %pM\n", mac);
 988                return -EINVAL;
 989        }
 990
 991        ether_addr_copy(ndev->perm_addr, mac);
 992        ether_addr_copy(wiphy->perm_addr, mac);
 993        if (!is_valid_ether_addr(ndev->dev_addr))
 994                ether_addr_copy(ndev->dev_addr, mac);
 995
 996        if (rf_status) {/* bad RF cable? */
 997                wil_err(wil, "RF communication error 0x%04x",
 998                        rf_status);
 999                return -EAGAIN;
1000        }
1001
1002        return 0;
1003}
1004
1005static void wil_bl_crash_info(struct wil6210_priv *wil, bool is_err)
1006{
1007        u32 bl_assert_code, bl_assert_blink, bl_magic_number;
1008        u32 bl_ver = wil_r(wil, RGF_USER_BL +
1009                           offsetof(struct bl_dedicated_registers_v0,
1010                                    boot_loader_struct_version));
1011
1012        if (bl_ver < 2)
1013                return;
1014
1015        bl_assert_code = wil_r(wil, RGF_USER_BL +
1016                               offsetof(struct bl_dedicated_registers_v1,
1017                                        bl_assert_code));
1018        bl_assert_blink = wil_r(wil, RGF_USER_BL +
1019                                offsetof(struct bl_dedicated_registers_v1,
1020                                         bl_assert_blink));
1021        bl_magic_number = wil_r(wil, RGF_USER_BL +
1022                                offsetof(struct bl_dedicated_registers_v1,
1023                                         bl_magic_number));
1024
1025        if (is_err) {
1026                wil_err(wil,
1027                        "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
1028                        bl_assert_code, bl_assert_blink, bl_magic_number);
1029        } else {
1030                wil_dbg_misc(wil,
1031                             "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
1032                             bl_assert_code, bl_assert_blink, bl_magic_number);
1033        }
1034}
1035
1036static int wil_get_otp_info(struct wil6210_priv *wil)
1037{
1038        struct net_device *ndev = wil_to_ndev(wil);
1039        struct wiphy *wiphy = wil_to_wiphy(wil);
1040        u8 mac[8];
1041
1042        wil_memcpy_fromio_32(mac, wil->csr + HOSTADDR(RGF_OTP_MAC),
1043                             sizeof(mac));
1044        if (!is_valid_ether_addr(mac)) {
1045                wil_err(wil, "Invalid MAC %pM\n", mac);
1046                return -EINVAL;
1047        }
1048
1049        ether_addr_copy(ndev->perm_addr, mac);
1050        ether_addr_copy(wiphy->perm_addr, mac);
1051        if (!is_valid_ether_addr(ndev->dev_addr))
1052                ether_addr_copy(ndev->dev_addr, mac);
1053
1054        return 0;
1055}
1056
1057static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
1058{
1059        ulong to = msecs_to_jiffies(1000);
1060        ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
1061
1062        if (0 == left) {
1063                wil_err(wil, "Firmware not ready\n");
1064                return -ETIME;
1065        } else {
1066                wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
1067                         jiffies_to_msecs(to-left), wil->hw_version);
1068        }
1069        return 0;
1070}
1071
1072void wil_abort_scan(struct wil6210_priv *wil, bool sync)
1073{
1074        int rc;
1075        struct cfg80211_scan_info info = {
1076                .aborted = true,
1077        };
1078
1079        lockdep_assert_held(&wil->p2p_wdev_mutex);
1080
1081        if (!wil->scan_request)
1082                return;
1083
1084        wil_dbg_misc(wil, "Abort scan_request 0x%p\n", wil->scan_request);
1085        del_timer_sync(&wil->scan_timer);
1086        mutex_unlock(&wil->p2p_wdev_mutex);
1087        rc = wmi_abort_scan(wil);
1088        if (!rc && sync)
1089                wait_event_interruptible_timeout(wil->wq, !wil->scan_request,
1090                                                 msecs_to_jiffies(
1091                                                 WAIT_FOR_SCAN_ABORT_MS));
1092
1093        mutex_lock(&wil->p2p_wdev_mutex);
1094        if (wil->scan_request) {
1095                cfg80211_scan_done(wil->scan_request, &info);
1096                wil->scan_request = NULL;
1097        }
1098}
1099
1100int wil_ps_update(struct wil6210_priv *wil, enum wmi_ps_profile_type ps_profile)
1101{
1102        int rc;
1103
1104        if (!test_bit(WMI_FW_CAPABILITY_PS_CONFIG, wil->fw_capabilities)) {
1105                wil_err(wil, "set_power_mgmt not supported\n");
1106                return -EOPNOTSUPP;
1107        }
1108
1109        rc  = wmi_ps_dev_profile_cfg(wil, ps_profile);
1110        if (rc)
1111                wil_err(wil, "wmi_ps_dev_profile_cfg failed (%d)\n", rc);
1112        else
1113                wil->ps_profile = ps_profile;
1114
1115        return rc;
1116}
1117
1118static void wil_pre_fw_config(struct wil6210_priv *wil)
1119{
1120        /* Mark FW as loaded from host */
1121        wil_s(wil, RGF_USER_USAGE_6, 1);
1122
1123        /* clear any interrupts which on-card-firmware
1124         * may have set
1125         */
1126        wil6210_clear_irq(wil);
1127        /* CAF_ICR - clear and mask */
1128        /* it is W1C, clear by writing back same value */
1129        wil_s(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
1130        wil_w(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
1131        /* clear PAL_UNIT_ICR (potential D0->D3 leftover) */
1132        wil_s(wil, RGF_PAL_UNIT_ICR + offsetof(struct RGF_ICR, ICR), 0);
1133
1134        if (wil->fw_calib_result > 0) {
1135                __le32 val = cpu_to_le32(wil->fw_calib_result |
1136                                                (CALIB_RESULT_SIGNATURE << 8));
1137                wil_w(wil, RGF_USER_FW_CALIB_RESULT, (u32 __force)val);
1138        }
1139}
1140
1141/*
1142 * We reset all the structures, and we reset the UMAC.
1143 * After calling this routine, you're expected to reload
1144 * the firmware.
1145 */
1146int wil_reset(struct wil6210_priv *wil, bool load_fw)
1147{
1148        int rc;
1149        unsigned long status_flags = BIT(wil_status_resetting);
1150        int no_flash;
1151
1152        wil_dbg_misc(wil, "reset\n");
1153
1154        WARN_ON(!mutex_is_locked(&wil->mutex));
1155        WARN_ON(test_bit(wil_status_napi_en, wil->status));
1156
1157        if (debug_fw) {
1158                static const u8 mac[ETH_ALEN] = {
1159                        0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
1160                };
1161                struct net_device *ndev = wil_to_ndev(wil);
1162
1163                ether_addr_copy(ndev->perm_addr, mac);
1164                ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
1165                return 0;
1166        }
1167
1168        if (wil->hw_version == HW_VER_UNKNOWN)
1169                return -ENODEV;
1170
1171        if (test_bit(WIL_PLATFORM_CAPA_T_PWR_ON_0, wil->platform_capa)) {
1172                wil_dbg_misc(wil, "Notify FW to set T_POWER_ON=0\n");
1173                wil_s(wil, RGF_USER_USAGE_8, BIT_USER_SUPPORT_T_POWER_ON_0);
1174        }
1175
1176        if (test_bit(WIL_PLATFORM_CAPA_EXT_CLK, wil->platform_capa)) {
1177                wil_dbg_misc(wil, "Notify FW on ext clock configuration\n");
1178                wil_s(wil, RGF_USER_USAGE_8, BIT_USER_EXT_CLK);
1179        }
1180
1181        if (wil->platform_ops.notify) {
1182                rc = wil->platform_ops.notify(wil->platform_handle,
1183                                              WIL_PLATFORM_EVT_PRE_RESET);
1184                if (rc)
1185                        wil_err(wil, "PRE_RESET platform notify failed, rc %d\n",
1186                                rc);
1187        }
1188
1189        set_bit(wil_status_resetting, wil->status);
1190        if (test_bit(wil_status_collecting_dumps, wil->status)) {
1191                /* Device collects crash dump, cancel the reset.
1192                 * following crash dump collection, reset would take place.
1193                 */
1194                wil_dbg_misc(wil, "reject reset while collecting crash dump\n");
1195                rc = -EBUSY;
1196                goto out;
1197        }
1198
1199        cancel_work_sync(&wil->disconnect_worker);
1200        wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
1201        wil_bcast_fini(wil);
1202
1203        /* Disable device led before reset*/
1204        wmi_led_cfg(wil, false);
1205
1206        mutex_lock(&wil->p2p_wdev_mutex);
1207        wil_abort_scan(wil, false);
1208        mutex_unlock(&wil->p2p_wdev_mutex);
1209
1210        /* prevent NAPI from being scheduled and prevent wmi commands */
1211        mutex_lock(&wil->wmi_mutex);
1212        if (test_bit(wil_status_suspending, wil->status))
1213                status_flags |= BIT(wil_status_suspending);
1214        bitmap_and(wil->status, wil->status, &status_flags,
1215                   wil_status_last);
1216        wil_dbg_misc(wil, "wil->status (0x%lx)\n", *wil->status);
1217        mutex_unlock(&wil->wmi_mutex);
1218
1219        wil_mask_irq(wil);
1220
1221        wmi_event_flush(wil);
1222
1223        flush_workqueue(wil->wq_service);
1224        flush_workqueue(wil->wmi_wq);
1225
1226        no_flash = test_bit(hw_capa_no_flash, wil->hw_capa);
1227        if (!no_flash)
1228                wil_bl_crash_info(wil, false);
1229        wil_disable_irq(wil);
1230        rc = wil_target_reset(wil, no_flash);
1231        wil6210_clear_irq(wil);
1232        wil_enable_irq(wil);
1233        wil_rx_fini(wil);
1234        if (rc) {
1235                if (!no_flash)
1236                        wil_bl_crash_info(wil, true);
1237                goto out;
1238        }
1239
1240        if (no_flash) {
1241                rc = wil_get_otp_info(wil);
1242        } else {
1243                rc = wil_get_bl_info(wil);
1244                if (rc == -EAGAIN && !load_fw)
1245                        /* ignore RF error if not going up */
1246                        rc = 0;
1247        }
1248        if (rc)
1249                goto out;
1250
1251        wil_set_oob_mode(wil, oob_mode);
1252        if (load_fw) {
1253                wil_info(wil, "Use firmware <%s> + board <%s>\n",
1254                         wil->wil_fw_name, WIL_BOARD_FILE_NAME);
1255
1256                if (!no_flash)
1257                        wil_bl_prepare_halt(wil);
1258
1259                wil_halt_cpu(wil);
1260                memset(wil->fw_version, 0, sizeof(wil->fw_version));
1261                /* Loading f/w from the file */
1262                rc = wil_request_firmware(wil, wil->wil_fw_name, true);
1263                if (rc)
1264                        goto out;
1265                if (wil->brd_file_addr)
1266                        rc = wil_request_board(wil, WIL_BOARD_FILE_NAME);
1267                else
1268                        rc = wil_request_firmware(wil,
1269                                                  WIL_BOARD_FILE_NAME,
1270                                                  true);
1271                if (rc)
1272                        goto out;
1273
1274                wil_pre_fw_config(wil);
1275                wil_release_cpu(wil);
1276        }
1277
1278        /* init after reset */
1279        wil->ap_isolate = 0;
1280        reinit_completion(&wil->wmi_ready);
1281        reinit_completion(&wil->wmi_call);
1282        reinit_completion(&wil->halp.comp);
1283
1284        clear_bit(wil_status_resetting, wil->status);
1285
1286        if (load_fw) {
1287                wil_configure_interrupt_moderation(wil);
1288                wil_unmask_irq(wil);
1289
1290                /* we just started MAC, wait for FW ready */
1291                rc = wil_wait_for_fw_ready(wil);
1292                if (rc)
1293                        return rc;
1294
1295                /* check FW is responsive */
1296                rc = wmi_echo(wil);
1297                if (rc) {
1298                        wil_err(wil, "wmi_echo failed, rc %d\n", rc);
1299                        return rc;
1300                }
1301
1302                wil_collect_fw_info(wil);
1303
1304                if (wil->ps_profile != WMI_PS_PROFILE_TYPE_DEFAULT)
1305                        wil_ps_update(wil, wil->ps_profile);
1306
1307                if (wil->platform_ops.notify) {
1308                        rc = wil->platform_ops.notify(wil->platform_handle,
1309                                                      WIL_PLATFORM_EVT_FW_RDY);
1310                        if (rc) {
1311                                wil_err(wil, "FW_RDY notify failed, rc %d\n",
1312                                        rc);
1313                                rc = 0;
1314                        }
1315                }
1316        }
1317
1318        return rc;
1319
1320out:
1321        clear_bit(wil_status_resetting, wil->status);
1322        return rc;
1323}
1324
1325void wil_fw_error_recovery(struct wil6210_priv *wil)
1326{
1327        wil_dbg_misc(wil, "starting fw error recovery\n");
1328
1329        if (test_bit(wil_status_resetting, wil->status)) {
1330                wil_info(wil, "Reset already in progress\n");
1331                return;
1332        }
1333
1334        wil->recovery_state = fw_recovery_pending;
1335        schedule_work(&wil->fw_error_worker);
1336}
1337
1338int __wil_up(struct wil6210_priv *wil)
1339{
1340        struct net_device *ndev = wil_to_ndev(wil);
1341        struct wireless_dev *wdev = wil->wdev;
1342        int rc;
1343
1344        WARN_ON(!mutex_is_locked(&wil->mutex));
1345
1346        rc = wil_reset(wil, true);
1347        if (rc)
1348                return rc;
1349
1350        /* Rx VRING. After MAC and beacon */
1351        rc = wil_rx_init(wil, 1 << rx_ring_order);
1352        if (rc)
1353                return rc;
1354
1355        switch (wdev->iftype) {
1356        case NL80211_IFTYPE_STATION:
1357                wil_dbg_misc(wil, "type: STATION\n");
1358                ndev->type = ARPHRD_ETHER;
1359                break;
1360        case NL80211_IFTYPE_AP:
1361                wil_dbg_misc(wil, "type: AP\n");
1362                ndev->type = ARPHRD_ETHER;
1363                break;
1364        case NL80211_IFTYPE_P2P_CLIENT:
1365                wil_dbg_misc(wil, "type: P2P_CLIENT\n");
1366                ndev->type = ARPHRD_ETHER;
1367                break;
1368        case NL80211_IFTYPE_P2P_GO:
1369                wil_dbg_misc(wil, "type: P2P_GO\n");
1370                ndev->type = ARPHRD_ETHER;
1371                break;
1372        case NL80211_IFTYPE_MONITOR:
1373                wil_dbg_misc(wil, "type: Monitor\n");
1374                ndev->type = ARPHRD_IEEE80211_RADIOTAP;
1375                /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
1376                break;
1377        default:
1378                return -EOPNOTSUPP;
1379        }
1380
1381        /* MAC address - pre-requisite for other commands */
1382        wmi_set_mac_address(wil, ndev->dev_addr);
1383
1384        wil_dbg_misc(wil, "NAPI enable\n");
1385        napi_enable(&wil->napi_rx);
1386        napi_enable(&wil->napi_tx);
1387        set_bit(wil_status_napi_en, wil->status);
1388
1389        wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
1390
1391        return 0;
1392}
1393
1394int wil_up(struct wil6210_priv *wil)
1395{
1396        int rc;
1397
1398        wil_dbg_misc(wil, "up\n");
1399
1400        mutex_lock(&wil->mutex);
1401        rc = __wil_up(wil);
1402        mutex_unlock(&wil->mutex);
1403
1404        return rc;
1405}
1406
1407int __wil_down(struct wil6210_priv *wil)
1408{
1409        WARN_ON(!mutex_is_locked(&wil->mutex));
1410
1411        set_bit(wil_status_resetting, wil->status);
1412
1413        wil6210_bus_request(wil, 0);
1414
1415        wil_disable_irq(wil);
1416        if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
1417                napi_disable(&wil->napi_rx);
1418                napi_disable(&wil->napi_tx);
1419                wil_dbg_misc(wil, "NAPI disable\n");
1420        }
1421        wil_enable_irq(wil);
1422
1423        mutex_lock(&wil->p2p_wdev_mutex);
1424        wil_p2p_stop_radio_operations(wil);
1425        wil_abort_scan(wil, false);
1426        mutex_unlock(&wil->p2p_wdev_mutex);
1427
1428        return wil_reset(wil, false);
1429}
1430
1431int wil_down(struct wil6210_priv *wil)
1432{
1433        int rc;
1434
1435        wil_dbg_misc(wil, "down\n");
1436
1437        wil_set_recovery_state(wil, fw_recovery_idle);
1438        mutex_lock(&wil->mutex);
1439        rc = __wil_down(wil);
1440        mutex_unlock(&wil->mutex);
1441
1442        return rc;
1443}
1444
1445int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
1446{
1447        int i;
1448        int rc = -ENOENT;
1449
1450        for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1451                if ((wil->sta[i].status != wil_sta_unused) &&
1452                    ether_addr_equal(wil->sta[i].addr, mac)) {
1453                        rc = i;
1454                        break;
1455                }
1456        }
1457
1458        return rc;
1459}
1460
1461void wil_halp_vote(struct wil6210_priv *wil)
1462{
1463        unsigned long rc;
1464        unsigned long to_jiffies = msecs_to_jiffies(WAIT_FOR_HALP_VOTE_MS);
1465
1466        mutex_lock(&wil->halp.lock);
1467
1468        wil_dbg_irq(wil, "halp_vote: start, HALP ref_cnt (%d)\n",
1469                    wil->halp.ref_cnt);
1470
1471        if (++wil->halp.ref_cnt == 1) {
1472                reinit_completion(&wil->halp.comp);
1473                wil6210_set_halp(wil);
1474                rc = wait_for_completion_timeout(&wil->halp.comp, to_jiffies);
1475                if (!rc) {
1476                        wil_err(wil, "HALP vote timed out\n");
1477                        /* Mask HALP as done in case the interrupt is raised */
1478                        wil6210_mask_halp(wil);
1479                } else {
1480                        wil_dbg_irq(wil,
1481                                    "halp_vote: HALP vote completed after %d ms\n",
1482                                    jiffies_to_msecs(to_jiffies - rc));
1483                }
1484        }
1485
1486        wil_dbg_irq(wil, "halp_vote: end, HALP ref_cnt (%d)\n",
1487                    wil->halp.ref_cnt);
1488
1489        mutex_unlock(&wil->halp.lock);
1490}
1491
1492void wil_halp_unvote(struct wil6210_priv *wil)
1493{
1494        WARN_ON(wil->halp.ref_cnt == 0);
1495
1496        mutex_lock(&wil->halp.lock);
1497
1498        wil_dbg_irq(wil, "halp_unvote: start, HALP ref_cnt (%d)\n",
1499                    wil->halp.ref_cnt);
1500
1501        if (--wil->halp.ref_cnt == 0) {
1502                wil6210_clear_halp(wil);
1503                wil_dbg_irq(wil, "HALP unvote\n");
1504        }
1505
1506        wil_dbg_irq(wil, "halp_unvote:end, HALP ref_cnt (%d)\n",
1507                    wil->halp.ref_cnt);
1508
1509        mutex_unlock(&wil->halp.lock);
1510}
1511