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