linux/drivers/net/wireless/ath/wil6210/main.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/moduleparam.h>
  18#include <linux/if_arp.h>
  19#include <linux/etherdevice.h>
  20
  21#include "wil6210.h"
  22#include "txrx.h"
  23#include "wmi.h"
  24#include "boot_loader.h"
  25
  26#define WAIT_FOR_DISCONNECT_TIMEOUT_MS 2000
  27#define WAIT_FOR_DISCONNECT_INTERVAL_MS 10
  28
  29bool debug_fw; /* = false; */
  30module_param(debug_fw, bool, S_IRUGO);
  31MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
  32
  33bool no_fw_recovery;
  34module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
  35MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
  36
  37/* if not set via modparam, will be set to default value of 1/8 of
  38 * rx ring size during init flow
  39 */
  40unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
  41module_param(rx_ring_overflow_thrsh, ushort, S_IRUGO);
  42MODULE_PARM_DESC(rx_ring_overflow_thrsh,
  43                 " RX ring overflow threshold in descriptors.");
  44
  45/* We allow allocation of more than 1 page buffers to support large packets.
  46 * It is suboptimal behavior performance wise in case MTU above page size.
  47 */
  48unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
  49static int mtu_max_set(const char *val, const struct kernel_param *kp)
  50{
  51        int ret;
  52
  53        /* sets mtu_max directly. no need to restore it in case of
  54         * illegal value since we assume this will fail insmod
  55         */
  56        ret = param_set_uint(val, kp);
  57        if (ret)
  58                return ret;
  59
  60        if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
  61                ret = -EINVAL;
  62
  63        return ret;
  64}
  65
  66static const struct kernel_param_ops mtu_max_ops = {
  67        .set = mtu_max_set,
  68        .get = param_get_uint,
  69};
  70
  71module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, S_IRUGO);
  72MODULE_PARM_DESC(mtu_max, " Max MTU value.");
  73
  74static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
  75static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
  76static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
  77
  78static int ring_order_set(const char *val, const struct kernel_param *kp)
  79{
  80        int ret;
  81        uint x;
  82
  83        ret = kstrtouint(val, 0, &x);
  84        if (ret)
  85                return ret;
  86
  87        if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
  88                return -EINVAL;
  89
  90        *((uint *)kp->arg) = x;
  91
  92        return 0;
  93}
  94
  95static const struct kernel_param_ops ring_order_ops = {
  96        .set = ring_order_set,
  97        .get = param_get_uint,
  98};
  99
 100module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, S_IRUGO);
 101MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
 102module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, S_IRUGO);
 103MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
 104module_param_cb(bcast_ring_order, &ring_order_ops, &bcast_ring_order, S_IRUGO);
 105MODULE_PARM_DESC(bcast_ring_order, " Bcast ring order; size = 1 << order");
 106
 107#define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
 108#define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
 109
 110/*
 111 * Due to a hardware issue,
 112 * one has to read/write to/from NIC in 32-bit chunks;
 113 * regular memcpy_fromio and siblings will
 114 * not work on 64-bit platform - it uses 64-bit transactions
 115 *
 116 * Force 32-bit transactions to enable NIC on 64-bit platforms
 117 *
 118 * To avoid byte swap on big endian host, __raw_{read|write}l
 119 * should be used - {read|write}l would swap bytes to provide
 120 * little endian on PCI value in host endianness.
 121 */
 122void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
 123                          size_t count)
 124{
 125        u32 *d = dst;
 126        const volatile u32 __iomem *s = src;
 127
 128        /* size_t is unsigned, if (count%4 != 0) it will wrap */
 129        for (count += 4; count > 4; count -= 4)
 130                *d++ = __raw_readl(s++);
 131}
 132
 133void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
 134                        size_t count)
 135{
 136        volatile u32 __iomem *d = dst;
 137        const u32 *s = src;
 138
 139        for (count += 4; count > 4; count -= 4)
 140                __raw_writel(*s++, d++);
 141}
 142
 143static void wil_disconnect_cid(struct wil6210_priv *wil, int cid,
 144                               u16 reason_code, bool from_event)
 145__acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
 146{
 147        uint i;
 148        struct net_device *ndev = wil_to_ndev(wil);
 149        struct wireless_dev *wdev = wil->wdev;
 150        struct wil_sta_info *sta = &wil->sta[cid];
 151
 152        might_sleep();
 153        wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
 154                     sta->status);
 155
 156        if (sta->status != wil_sta_unused) {
 157                if (!from_event)
 158                        wmi_disconnect_sta(wil, sta->addr, reason_code);
 159
 160                switch (wdev->iftype) {
 161                case NL80211_IFTYPE_AP:
 162                case NL80211_IFTYPE_P2P_GO:
 163                        /* AP-like interface */
 164                        cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
 165                        break;
 166                default:
 167                        break;
 168                }
 169                sta->status = wil_sta_unused;
 170        }
 171
 172        for (i = 0; i < WIL_STA_TID_NUM; i++) {
 173                struct wil_tid_ampdu_rx *r;
 174
 175                spin_lock_bh(&sta->tid_rx_lock);
 176
 177                r = sta->tid_rx[i];
 178                sta->tid_rx[i] = NULL;
 179                wil_tid_ampdu_rx_free(wil, r);
 180
 181                spin_unlock_bh(&sta->tid_rx_lock);
 182        }
 183        for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
 184                if (wil->vring2cid_tid[i][0] == cid)
 185                        wil_vring_fini_tx(wil, i);
 186        }
 187        memset(&sta->stats, 0, sizeof(sta->stats));
 188}
 189
 190static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
 191                                u16 reason_code, bool from_event)
 192{
 193        int cid = -ENOENT;
 194        struct net_device *ndev = wil_to_ndev(wil);
 195        struct wireless_dev *wdev = wil->wdev;
 196
 197        might_sleep();
 198        wil_dbg_misc(wil, "%s(bssid=%pM, reason=%d, ev%s)\n", __func__, bssid,
 199                     reason_code, from_event ? "+" : "-");
 200
 201        /* Cases are:
 202         * - disconnect single STA, still connected
 203         * - disconnect single STA, already disconnected
 204         * - disconnect all
 205         *
 206         * For "disconnect all", there are 2 options:
 207         * - bssid == NULL
 208         * - bssid is our MAC address
 209         */
 210        if (bssid && memcmp(ndev->dev_addr, bssid, ETH_ALEN)) {
 211                cid = wil_find_cid(wil, bssid);
 212                wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
 213                             bssid, cid, reason_code);
 214                if (cid >= 0) /* disconnect 1 peer */
 215                        wil_disconnect_cid(wil, cid, reason_code, from_event);
 216        } else { /* all */
 217                wil_dbg_misc(wil, "Disconnect all\n");
 218                for (cid = 0; cid < WIL6210_MAX_CID; cid++)
 219                        wil_disconnect_cid(wil, cid, reason_code, from_event);
 220        }
 221
 222        /* link state */
 223        switch (wdev->iftype) {
 224        case NL80211_IFTYPE_STATION:
 225        case NL80211_IFTYPE_P2P_CLIENT:
 226                wil_bcast_fini(wil);
 227                netif_tx_stop_all_queues(ndev);
 228                netif_carrier_off(ndev);
 229
 230                if (test_bit(wil_status_fwconnected, wil->status)) {
 231                        clear_bit(wil_status_fwconnected, wil->status);
 232                        cfg80211_disconnected(ndev, reason_code,
 233                                              NULL, 0, false, GFP_KERNEL);
 234                } else if (test_bit(wil_status_fwconnecting, wil->status)) {
 235                        cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
 236                                                WLAN_STATUS_UNSPECIFIED_FAILURE,
 237                                                GFP_KERNEL);
 238                }
 239                clear_bit(wil_status_fwconnecting, wil->status);
 240                break;
 241        default:
 242                break;
 243        }
 244}
 245
 246static void wil_disconnect_worker(struct work_struct *work)
 247{
 248        struct wil6210_priv *wil = container_of(work,
 249                        struct wil6210_priv, disconnect_worker);
 250
 251        mutex_lock(&wil->mutex);
 252        _wil6210_disconnect(wil, NULL, WLAN_REASON_UNSPECIFIED, false);
 253        mutex_unlock(&wil->mutex);
 254}
 255
 256static void wil_connect_timer_fn(ulong x)
 257{
 258        struct wil6210_priv *wil = (void *)x;
 259
 260        wil_dbg_misc(wil, "Connect timeout\n");
 261
 262        /* reschedule to thread context - disconnect won't
 263         * run from atomic context
 264         */
 265        schedule_work(&wil->disconnect_worker);
 266}
 267
 268static void wil_scan_timer_fn(ulong x)
 269{
 270        struct wil6210_priv *wil = (void *)x;
 271
 272        clear_bit(wil_status_fwready, wil->status);
 273        wil_err(wil, "Scan timeout detected, start fw error recovery\n");
 274        wil_fw_error_recovery(wil);
 275}
 276
 277static int wil_wait_for_recovery(struct wil6210_priv *wil)
 278{
 279        if (wait_event_interruptible(wil->wq, wil->recovery_state !=
 280                                     fw_recovery_pending)) {
 281                wil_err(wil, "Interrupt, canceling recovery\n");
 282                return -ERESTARTSYS;
 283        }
 284        if (wil->recovery_state != fw_recovery_running) {
 285                wil_info(wil, "Recovery cancelled\n");
 286                return -EINTR;
 287        }
 288        wil_info(wil, "Proceed with recovery\n");
 289        return 0;
 290}
 291
 292void wil_set_recovery_state(struct wil6210_priv *wil, int state)
 293{
 294        wil_dbg_misc(wil, "%s(%d -> %d)\n", __func__,
 295                     wil->recovery_state, state);
 296
 297        wil->recovery_state = state;
 298        wake_up_interruptible(&wil->wq);
 299}
 300
 301static void wil_fw_error_worker(struct work_struct *work)
 302{
 303        struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
 304                                                fw_error_worker);
 305        struct wireless_dev *wdev = wil->wdev;
 306
 307        wil_dbg_misc(wil, "fw error worker\n");
 308
 309        if (!netif_running(wil_to_ndev(wil))) {
 310                wil_info(wil, "No recovery - interface is down\n");
 311                return;
 312        }
 313
 314        /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
 315         * passed since last recovery attempt
 316         */
 317        if (time_is_after_jiffies(wil->last_fw_recovery +
 318                                  WIL6210_FW_RECOVERY_TO))
 319                wil->recovery_count++;
 320        else
 321                wil->recovery_count = 1; /* fw was alive for a long time */
 322
 323        if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
 324                wil_err(wil, "too many recovery attempts (%d), giving up\n",
 325                        wil->recovery_count);
 326                return;
 327        }
 328
 329        wil->last_fw_recovery = jiffies;
 330
 331        mutex_lock(&wil->mutex);
 332        switch (wdev->iftype) {
 333        case NL80211_IFTYPE_STATION:
 334        case NL80211_IFTYPE_P2P_CLIENT:
 335        case NL80211_IFTYPE_MONITOR:
 336                wil_info(wil, "fw error recovery requested (try %d)...\n",
 337                         wil->recovery_count);
 338                if (!no_fw_recovery)
 339                        wil->recovery_state = fw_recovery_running;
 340                if (0 != wil_wait_for_recovery(wil))
 341                        break;
 342
 343                __wil_down(wil);
 344                __wil_up(wil);
 345                break;
 346        case NL80211_IFTYPE_AP:
 347        case NL80211_IFTYPE_P2P_GO:
 348                wil_info(wil, "No recovery for AP-like interface\n");
 349                /* recovery in these modes is done by upper layers */
 350                break;
 351        default:
 352                wil_err(wil, "No recovery - unknown interface type %d\n",
 353                        wdev->iftype);
 354                break;
 355        }
 356        mutex_unlock(&wil->mutex);
 357}
 358
 359static int wil_find_free_vring(struct wil6210_priv *wil)
 360{
 361        int i;
 362
 363        for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
 364                if (!wil->vring_tx[i].va)
 365                        return i;
 366        }
 367        return -EINVAL;
 368}
 369
 370int wil_bcast_init(struct wil6210_priv *wil)
 371{
 372        int ri = wil->bcast_vring, rc;
 373
 374        if ((ri >= 0) && wil->vring_tx[ri].va)
 375                return 0;
 376
 377        ri = wil_find_free_vring(wil);
 378        if (ri < 0)
 379                return ri;
 380
 381        wil->bcast_vring = ri;
 382        rc = wil_vring_init_bcast(wil, ri, 1 << bcast_ring_order);
 383        if (rc)
 384                wil->bcast_vring = -1;
 385
 386        return rc;
 387}
 388
 389void wil_bcast_fini(struct wil6210_priv *wil)
 390{
 391        int ri = wil->bcast_vring;
 392
 393        if (ri < 0)
 394                return;
 395
 396        wil->bcast_vring = -1;
 397        wil_vring_fini_tx(wil, ri);
 398}
 399
 400static void wil_connect_worker(struct work_struct *work)
 401{
 402        int rc;
 403        struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
 404                                                connect_worker);
 405        struct net_device *ndev = wil_to_ndev(wil);
 406
 407        int cid = wil->pending_connect_cid;
 408        int ringid = wil_find_free_vring(wil);
 409
 410        if (cid < 0) {
 411                wil_err(wil, "No connection pending\n");
 412                return;
 413        }
 414
 415        wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
 416
 417        rc = wil_vring_init_tx(wil, ringid, 1 << tx_ring_order, cid, 0);
 418        wil->pending_connect_cid = -1;
 419        if (rc == 0) {
 420                wil->sta[cid].status = wil_sta_connected;
 421                netif_tx_wake_all_queues(ndev);
 422        } else {
 423                wil->sta[cid].status = wil_sta_unused;
 424        }
 425}
 426
 427int wil_priv_init(struct wil6210_priv *wil)
 428{
 429        uint i;
 430
 431        wil_dbg_misc(wil, "%s()\n", __func__);
 432
 433        memset(wil->sta, 0, sizeof(wil->sta));
 434        for (i = 0; i < WIL6210_MAX_CID; i++)
 435                spin_lock_init(&wil->sta[i].tid_rx_lock);
 436
 437        mutex_init(&wil->mutex);
 438        mutex_init(&wil->wmi_mutex);
 439        mutex_init(&wil->back_rx_mutex);
 440        mutex_init(&wil->back_tx_mutex);
 441        mutex_init(&wil->probe_client_mutex);
 442
 443        init_completion(&wil->wmi_ready);
 444        init_completion(&wil->wmi_call);
 445
 446        wil->pending_connect_cid = -1;
 447        wil->bcast_vring = -1;
 448        setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
 449        setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
 450
 451        INIT_WORK(&wil->connect_worker, wil_connect_worker);
 452        INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
 453        INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
 454        INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
 455        INIT_WORK(&wil->back_rx_worker, wil_back_rx_worker);
 456        INIT_WORK(&wil->back_tx_worker, wil_back_tx_worker);
 457        INIT_WORK(&wil->probe_client_worker, wil_probe_client_worker);
 458
 459        INIT_LIST_HEAD(&wil->pending_wmi_ev);
 460        INIT_LIST_HEAD(&wil->back_rx_pending);
 461        INIT_LIST_HEAD(&wil->back_tx_pending);
 462        INIT_LIST_HEAD(&wil->probe_client_pending);
 463        spin_lock_init(&wil->wmi_ev_lock);
 464        init_waitqueue_head(&wil->wq);
 465
 466        wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
 467        if (!wil->wmi_wq)
 468                return -EAGAIN;
 469
 470        wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
 471        if (!wil->wq_service)
 472                goto out_wmi_wq;
 473
 474        wil->last_fw_recovery = jiffies;
 475        wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
 476        wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
 477        wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
 478        wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
 479
 480        if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
 481                rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
 482        return 0;
 483
 484out_wmi_wq:
 485        destroy_workqueue(wil->wmi_wq);
 486
 487        return -EAGAIN;
 488}
 489
 490/**
 491 * wil6210_disconnect - disconnect one connection
 492 * @wil: driver context
 493 * @bssid: peer to disconnect, NULL to disconnect all
 494 * @reason_code: Reason code for the Disassociation frame
 495 * @from_event: whether is invoked from FW event handler
 496 *
 497 * Disconnect and release associated resources. If invoked not from the
 498 * FW event handler, issue WMI command(s) to trigger MAC disconnect.
 499 */
 500void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
 501                        u16 reason_code, bool from_event)
 502{
 503        wil_dbg_misc(wil, "%s()\n", __func__);
 504
 505        del_timer_sync(&wil->connect_timer);
 506        _wil6210_disconnect(wil, bssid, reason_code, from_event);
 507}
 508
 509void wil_priv_deinit(struct wil6210_priv *wil)
 510{
 511        wil_dbg_misc(wil, "%s()\n", __func__);
 512
 513        wil_set_recovery_state(wil, fw_recovery_idle);
 514        del_timer_sync(&wil->scan_timer);
 515        cancel_work_sync(&wil->disconnect_worker);
 516        cancel_work_sync(&wil->fw_error_worker);
 517        mutex_lock(&wil->mutex);
 518        wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
 519        mutex_unlock(&wil->mutex);
 520        wmi_event_flush(wil);
 521        wil_back_rx_flush(wil);
 522        cancel_work_sync(&wil->back_rx_worker);
 523        wil_back_tx_flush(wil);
 524        cancel_work_sync(&wil->back_tx_worker);
 525        wil_probe_client_flush(wil);
 526        cancel_work_sync(&wil->probe_client_worker);
 527        destroy_workqueue(wil->wq_service);
 528        destroy_workqueue(wil->wmi_wq);
 529}
 530
 531static inline void wil_halt_cpu(struct wil6210_priv *wil)
 532{
 533        wil_w(wil, RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
 534        wil_w(wil, RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
 535}
 536
 537static inline void wil_release_cpu(struct wil6210_priv *wil)
 538{
 539        /* Start CPU */
 540        wil_w(wil, RGF_USER_USER_CPU_0, 1);
 541}
 542
 543static int wil_target_reset(struct wil6210_priv *wil)
 544{
 545        int delay = 0;
 546        u32 x, x1 = 0;
 547
 548        wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
 549
 550        /* Clear MAC link up */
 551        wil_s(wil, RGF_HP_CTRL, BIT(15));
 552        wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
 553        wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
 554
 555        wil_halt_cpu(wil);
 556
 557        /* clear all boot loader "ready" bits */
 558        wil_w(wil, RGF_USER_BL +
 559              offsetof(struct bl_dedicated_registers_v0, boot_loader_ready), 0);
 560        /* Clear Fw Download notification */
 561        wil_c(wil, RGF_USER_USAGE_6, BIT(0));
 562
 563        wil_s(wil, RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
 564        /* XTAL stabilization should take about 3ms */
 565        usleep_range(5000, 7000);
 566        x = wil_r(wil, RGF_CAF_PLL_LOCK_STATUS);
 567        if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
 568                wil_err(wil, "Xtal stabilization timeout\n"
 569                        "RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
 570                return -ETIME;
 571        }
 572        /* switch 10k to XTAL*/
 573        wil_c(wil, RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
 574        /* 40 MHz */
 575        wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
 576
 577        wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
 578        wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
 579
 580        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
 581        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
 582        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
 583        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
 584
 585        wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
 586        wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
 587
 588        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
 589        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
 590        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
 591        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
 592
 593        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
 594        /* reset A2 PCIE AHB */
 595        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
 596
 597        wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
 598
 599        /* wait until device ready. typical time is 20..80 msec */
 600        do {
 601                msleep(RST_DELAY);
 602                x = wil_r(wil, RGF_USER_BL +
 603                          offsetof(struct bl_dedicated_registers_v0,
 604                                   boot_loader_ready));
 605                if (x1 != x) {
 606                        wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n", x1, x);
 607                        x1 = x;
 608                }
 609                if (delay++ > RST_COUNT) {
 610                        wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
 611                                x);
 612                        return -ETIME;
 613                }
 614        } while (x != BL_READY);
 615
 616        wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
 617
 618        /* enable fix for HW bug related to the SA/DA swap in AP Rx */
 619        wil_s(wil, RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
 620              BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
 621
 622        wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
 623        return 0;
 624}
 625
 626void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
 627{
 628        le32_to_cpus(&r->base);
 629        le16_to_cpus(&r->entry_size);
 630        le16_to_cpus(&r->size);
 631        le32_to_cpus(&r->tail);
 632        le32_to_cpus(&r->head);
 633}
 634
 635static int wil_get_bl_info(struct wil6210_priv *wil)
 636{
 637        struct net_device *ndev = wil_to_ndev(wil);
 638        union {
 639                struct bl_dedicated_registers_v0 bl0;
 640                struct bl_dedicated_registers_v1 bl1;
 641        } bl;
 642        u32 bl_ver;
 643        u8 *mac;
 644        u16 rf_status;
 645
 646        wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL),
 647                             sizeof(bl));
 648        bl_ver = le32_to_cpu(bl.bl0.boot_loader_struct_version);
 649        mac = bl.bl0.mac_address;
 650
 651        if (bl_ver == 0) {
 652                le32_to_cpus(&bl.bl0.rf_type);
 653                le32_to_cpus(&bl.bl0.baseband_type);
 654                rf_status = 0; /* actually, unknown */
 655                wil_info(wil,
 656                         "Boot Loader struct v%d: MAC = %pM RF = 0x%08x bband = 0x%08x\n",
 657                         bl_ver, mac,
 658                         bl.bl0.rf_type, bl.bl0.baseband_type);
 659                wil_info(wil, "Boot Loader build unknown for struct v0\n");
 660        } else {
 661                le16_to_cpus(&bl.bl1.rf_type);
 662                rf_status = le16_to_cpu(bl.bl1.rf_status);
 663                le32_to_cpus(&bl.bl1.baseband_type);
 664                le16_to_cpus(&bl.bl1.bl_version_subminor);
 665                le16_to_cpus(&bl.bl1.bl_version_build);
 666                wil_info(wil,
 667                         "Boot Loader struct v%d: MAC = %pM RF = 0x%04x (status 0x%04x) bband = 0x%08x\n",
 668                         bl_ver, mac,
 669                         bl.bl1.rf_type, rf_status,
 670                         bl.bl1.baseband_type);
 671                wil_info(wil, "Boot Loader build %d.%d.%d.%d\n",
 672                         bl.bl1.bl_version_major, bl.bl1.bl_version_minor,
 673                         bl.bl1.bl_version_subminor, bl.bl1.bl_version_build);
 674        }
 675
 676        if (!is_valid_ether_addr(mac)) {
 677                wil_err(wil, "BL: Invalid MAC %pM\n", mac);
 678                return -EINVAL;
 679        }
 680
 681        ether_addr_copy(ndev->perm_addr, mac);
 682        if (!is_valid_ether_addr(ndev->dev_addr))
 683                ether_addr_copy(ndev->dev_addr, mac);
 684
 685        if (rf_status) {/* bad RF cable? */
 686                wil_err(wil, "RF communication error 0x%04x",
 687                        rf_status);
 688                return -EAGAIN;
 689        }
 690
 691        return 0;
 692}
 693
 694static void wil_bl_crash_info(struct wil6210_priv *wil, bool is_err)
 695{
 696        u32 bl_assert_code, bl_assert_blink, bl_magic_number;
 697        u32 bl_ver = wil_r(wil, RGF_USER_BL +
 698                           offsetof(struct bl_dedicated_registers_v0,
 699                                    boot_loader_struct_version));
 700
 701        if (bl_ver < 2)
 702                return;
 703
 704        bl_assert_code = wil_r(wil, RGF_USER_BL +
 705                               offsetof(struct bl_dedicated_registers_v1,
 706                                        bl_assert_code));
 707        bl_assert_blink = wil_r(wil, RGF_USER_BL +
 708                                offsetof(struct bl_dedicated_registers_v1,
 709                                         bl_assert_blink));
 710        bl_magic_number = wil_r(wil, RGF_USER_BL +
 711                                offsetof(struct bl_dedicated_registers_v1,
 712                                         bl_magic_number));
 713
 714        if (is_err) {
 715                wil_err(wil,
 716                        "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
 717                        bl_assert_code, bl_assert_blink, bl_magic_number);
 718        } else {
 719                wil_dbg_misc(wil,
 720                             "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
 721                             bl_assert_code, bl_assert_blink, bl_magic_number);
 722        }
 723}
 724
 725static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
 726{
 727        ulong to = msecs_to_jiffies(1000);
 728        ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
 729
 730        if (0 == left) {
 731                wil_err(wil, "Firmware not ready\n");
 732                return -ETIME;
 733        } else {
 734                wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
 735                         jiffies_to_msecs(to-left), wil->hw_version);
 736        }
 737        return 0;
 738}
 739
 740/*
 741 * We reset all the structures, and we reset the UMAC.
 742 * After calling this routine, you're expected to reload
 743 * the firmware.
 744 */
 745int wil_reset(struct wil6210_priv *wil, bool load_fw)
 746{
 747        int rc;
 748
 749        wil_dbg_misc(wil, "%s()\n", __func__);
 750
 751        WARN_ON(!mutex_is_locked(&wil->mutex));
 752        WARN_ON(test_bit(wil_status_napi_en, wil->status));
 753
 754        if (debug_fw) {
 755                static const u8 mac[ETH_ALEN] = {
 756                        0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
 757                };
 758                struct net_device *ndev = wil_to_ndev(wil);
 759
 760                ether_addr_copy(ndev->perm_addr, mac);
 761                ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
 762                return 0;
 763        }
 764
 765        if (wil->hw_version == HW_VER_UNKNOWN)
 766                return -ENODEV;
 767
 768        cancel_work_sync(&wil->disconnect_worker);
 769        wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
 770        wil_bcast_fini(wil);
 771
 772        /* prevent NAPI from being scheduled */
 773        bitmap_zero(wil->status, wil_status_last);
 774
 775        if (wil->scan_request) {
 776                wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
 777                             wil->scan_request);
 778                del_timer_sync(&wil->scan_timer);
 779                cfg80211_scan_done(wil->scan_request, true);
 780                wil->scan_request = NULL;
 781        }
 782
 783        wil_mask_irq(wil);
 784
 785        wmi_event_flush(wil);
 786
 787        flush_workqueue(wil->wq_service);
 788        flush_workqueue(wil->wmi_wq);
 789
 790        wil_bl_crash_info(wil, false);
 791        rc = wil_target_reset(wil);
 792        wil_rx_fini(wil);
 793        if (rc) {
 794                wil_bl_crash_info(wil, true);
 795                return rc;
 796        }
 797
 798        rc = wil_get_bl_info(wil);
 799        if (rc == -EAGAIN && !load_fw) /* ignore RF error if not going up */
 800                rc = 0;
 801        if (rc)
 802                return rc;
 803
 804        if (load_fw) {
 805                wil_info(wil, "Use firmware <%s> + board <%s>\n", WIL_FW_NAME,
 806                         WIL_FW2_NAME);
 807
 808                wil_halt_cpu(wil);
 809                /* Loading f/w from the file */
 810                rc = wil_request_firmware(wil, WIL_FW_NAME);
 811                if (rc)
 812                        return rc;
 813                rc = wil_request_firmware(wil, WIL_FW2_NAME);
 814                if (rc)
 815                        return rc;
 816
 817                /* Mark FW as loaded from host */
 818                wil_s(wil, RGF_USER_USAGE_6, 1);
 819
 820                /* clear any interrupts which on-card-firmware
 821                 * may have set
 822                 */
 823                wil6210_clear_irq(wil);
 824                /* CAF_ICR - clear and mask */
 825                /* it is W1C, clear by writing back same value */
 826                wil_s(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
 827                wil_w(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
 828
 829                wil_release_cpu(wil);
 830        }
 831
 832        /* init after reset */
 833        wil->pending_connect_cid = -1;
 834        wil->ap_isolate = 0;
 835        reinit_completion(&wil->wmi_ready);
 836        reinit_completion(&wil->wmi_call);
 837
 838        if (load_fw) {
 839                wil_configure_interrupt_moderation(wil);
 840                wil_unmask_irq(wil);
 841
 842                /* we just started MAC, wait for FW ready */
 843                rc = wil_wait_for_fw_ready(wil);
 844                if (rc == 0) /* check FW is responsive */
 845                        rc = wmi_echo(wil);
 846        }
 847
 848        return rc;
 849}
 850
 851void wil_fw_error_recovery(struct wil6210_priv *wil)
 852{
 853        wil_dbg_misc(wil, "starting fw error recovery\n");
 854        wil->recovery_state = fw_recovery_pending;
 855        schedule_work(&wil->fw_error_worker);
 856}
 857
 858int __wil_up(struct wil6210_priv *wil)
 859{
 860        struct net_device *ndev = wil_to_ndev(wil);
 861        struct wireless_dev *wdev = wil->wdev;
 862        int rc;
 863
 864        WARN_ON(!mutex_is_locked(&wil->mutex));
 865
 866        rc = wil_reset(wil, true);
 867        if (rc)
 868                return rc;
 869
 870        /* Rx VRING. After MAC and beacon */
 871        rc = wil_rx_init(wil, 1 << rx_ring_order);
 872        if (rc)
 873                return rc;
 874
 875        switch (wdev->iftype) {
 876        case NL80211_IFTYPE_STATION:
 877                wil_dbg_misc(wil, "type: STATION\n");
 878                ndev->type = ARPHRD_ETHER;
 879                break;
 880        case NL80211_IFTYPE_AP:
 881                wil_dbg_misc(wil, "type: AP\n");
 882                ndev->type = ARPHRD_ETHER;
 883                break;
 884        case NL80211_IFTYPE_P2P_CLIENT:
 885                wil_dbg_misc(wil, "type: P2P_CLIENT\n");
 886                ndev->type = ARPHRD_ETHER;
 887                break;
 888        case NL80211_IFTYPE_P2P_GO:
 889                wil_dbg_misc(wil, "type: P2P_GO\n");
 890                ndev->type = ARPHRD_ETHER;
 891                break;
 892        case NL80211_IFTYPE_MONITOR:
 893                wil_dbg_misc(wil, "type: Monitor\n");
 894                ndev->type = ARPHRD_IEEE80211_RADIOTAP;
 895                /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
 896                break;
 897        default:
 898                return -EOPNOTSUPP;
 899        }
 900
 901        /* MAC address - pre-requisite for other commands */
 902        wmi_set_mac_address(wil, ndev->dev_addr);
 903
 904        wil_dbg_misc(wil, "NAPI enable\n");
 905        napi_enable(&wil->napi_rx);
 906        napi_enable(&wil->napi_tx);
 907        set_bit(wil_status_napi_en, wil->status);
 908
 909        if (wil->platform_ops.bus_request)
 910                wil->platform_ops.bus_request(wil->platform_handle,
 911                                              WIL_MAX_BUS_REQUEST_KBPS);
 912
 913        return 0;
 914}
 915
 916int wil_up(struct wil6210_priv *wil)
 917{
 918        int rc;
 919
 920        wil_dbg_misc(wil, "%s()\n", __func__);
 921
 922        mutex_lock(&wil->mutex);
 923        rc = __wil_up(wil);
 924        mutex_unlock(&wil->mutex);
 925
 926        return rc;
 927}
 928
 929int __wil_down(struct wil6210_priv *wil)
 930{
 931        int iter = WAIT_FOR_DISCONNECT_TIMEOUT_MS /
 932                        WAIT_FOR_DISCONNECT_INTERVAL_MS;
 933
 934        WARN_ON(!mutex_is_locked(&wil->mutex));
 935
 936        if (wil->platform_ops.bus_request)
 937                wil->platform_ops.bus_request(wil->platform_handle, 0);
 938
 939        wil_disable_irq(wil);
 940        if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
 941                napi_disable(&wil->napi_rx);
 942                napi_disable(&wil->napi_tx);
 943                wil_dbg_misc(wil, "NAPI disable\n");
 944        }
 945        wil_enable_irq(wil);
 946
 947        if (wil->scan_request) {
 948                wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
 949                             wil->scan_request);
 950                del_timer_sync(&wil->scan_timer);
 951                cfg80211_scan_done(wil->scan_request, true);
 952                wil->scan_request = NULL;
 953        }
 954
 955        if (test_bit(wil_status_fwconnected, wil->status) ||
 956            test_bit(wil_status_fwconnecting, wil->status))
 957                wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
 958
 959        /* make sure wil is idle (not connected) */
 960        mutex_unlock(&wil->mutex);
 961        while (iter--) {
 962                int idle = !test_bit(wil_status_fwconnected, wil->status) &&
 963                           !test_bit(wil_status_fwconnecting, wil->status);
 964                if (idle)
 965                        break;
 966                msleep(WAIT_FOR_DISCONNECT_INTERVAL_MS);
 967        }
 968        mutex_lock(&wil->mutex);
 969
 970        if (!iter)
 971                wil_err(wil, "timeout waiting for idle FW/HW\n");
 972
 973        wil_reset(wil, false);
 974
 975        return 0;
 976}
 977
 978int wil_down(struct wil6210_priv *wil)
 979{
 980        int rc;
 981
 982        wil_dbg_misc(wil, "%s()\n", __func__);
 983
 984        wil_set_recovery_state(wil, fw_recovery_idle);
 985        mutex_lock(&wil->mutex);
 986        rc = __wil_down(wil);
 987        mutex_unlock(&wil->mutex);
 988
 989        return rc;
 990}
 991
 992int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
 993{
 994        int i;
 995        int rc = -ENOENT;
 996
 997        for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
 998                if ((wil->sta[i].status != wil_sta_unused) &&
 999                    ether_addr_equal(wil->sta[i].addr, mac)) {
1000                        rc = i;
1001                        break;
1002                }
1003        }
1004
1005        return rc;
1006}
1007