1 // SPDX-License-Identifier: BSD-3-Clause-Clear 2 /* 3 * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved. 4 * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved. 5 */ 6 7 #include <linux/module.h> 8 #include <linux/slab.h> 9 #include <linux/remoteproc.h> 10 #include <linux/firmware.h> 11 #include <linux/of.h> 12 #include "core.h" 13 #include "dp_tx.h" 14 #include "dp_rx.h" 15 #include "debug.h" 16 #include "hif.h" 17 18 unsigned int ath12k_debug_mask; 19 module_param_named(debug_mask, ath12k_debug_mask, uint, 0644); 20 MODULE_PARM_DESC(debug_mask, "Debugging mask"); 21 22 static int ath12k_core_rfkill_config(struct ath12k_base *ab) 23 { 24 struct ath12k *ar; 25 int ret = 0, i; 26 27 if (!(ab->target_caps.sys_cap_info & WMI_SYS_CAP_INFO_RFKILL)) 28 return 0; 29 30 for (i = 0; i < ab->num_radios; i++) { 31 ar = ab->pdevs[i].ar; 32 33 ret = ath12k_mac_rfkill_config(ar); 34 if (ret && ret != -EOPNOTSUPP) { 35 ath12k_warn(ab, "failed to configure rfkill: %d", ret); 36 return ret; 37 } 38 } 39 40 return ret; 41 } 42 43 int ath12k_core_suspend(struct ath12k_base *ab) 44 { 45 int ret; 46 47 if (!ab->hw_params->supports_suspend) 48 return -EOPNOTSUPP; 49 50 /* TODO: there can frames in queues so for now add delay as a hack. 51 * Need to implement to handle and remove this delay. 52 */ 53 msleep(500); 54 55 ret = ath12k_dp_rx_pktlog_stop(ab, true); 56 if (ret) { 57 ath12k_warn(ab, "failed to stop dp rx (and timer) pktlog during suspend: %d\n", 58 ret); 59 return ret; 60 } 61 62 ret = ath12k_dp_rx_pktlog_stop(ab, false); 63 if (ret) { 64 ath12k_warn(ab, "failed to stop dp rx pktlog during suspend: %d\n", 65 ret); 66 return ret; 67 } 68 69 ath12k_hif_irq_disable(ab); 70 ath12k_hif_ce_irq_disable(ab); 71 72 ret = ath12k_hif_suspend(ab); 73 if (ret) { 74 ath12k_warn(ab, "failed to suspend hif: %d\n", ret); 75 return ret; 76 } 77 78 return 0; 79 } 80 81 int ath12k_core_resume(struct ath12k_base *ab) 82 { 83 int ret; 84 85 if (!ab->hw_params->supports_suspend) 86 return -EOPNOTSUPP; 87 88 ret = ath12k_hif_resume(ab); 89 if (ret) { 90 ath12k_warn(ab, "failed to resume hif during resume: %d\n", ret); 91 return ret; 92 } 93 94 ath12k_hif_ce_irq_enable(ab); 95 ath12k_hif_irq_enable(ab); 96 97 ret = ath12k_dp_rx_pktlog_start(ab); 98 if (ret) { 99 ath12k_warn(ab, "failed to start rx pktlog during resume: %d\n", 100 ret); 101 return ret; 102 } 103 104 return 0; 105 } 106 107 static int ath12k_core_create_board_name(struct ath12k_base *ab, char *name, 108 size_t name_len) 109 { 110 /* strlen(',variant=') + strlen(ab->qmi.target.bdf_ext) */ 111 char variant[9 + ATH12K_QMI_BDF_EXT_STR_LENGTH] = { 0 }; 112 113 if (ab->qmi.target.bdf_ext[0] != '\0') 114 scnprintf(variant, sizeof(variant), ",variant=%s", 115 ab->qmi.target.bdf_ext); 116 117 scnprintf(name, name_len, 118 "bus=%s,qmi-chip-id=%d,qmi-board-id=%d%s", 119 ath12k_bus_str(ab->hif.bus), 120 ab->qmi.target.chip_id, 121 ab->qmi.target.board_id, variant); 122 123 ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot using board name '%s'\n", name); 124 125 return 0; 126 } 127 128 const struct firmware *ath12k_core_firmware_request(struct ath12k_base *ab, 129 const char *file) 130 { 131 const struct firmware *fw; 132 char path[100]; 133 int ret; 134 135 if (!file) 136 return ERR_PTR(-ENOENT); 137 138 ath12k_core_create_firmware_path(ab, file, path, sizeof(path)); 139 140 ret = firmware_request_nowarn(&fw, path, ab->dev); 141 if (ret) 142 return ERR_PTR(ret); 143 144 ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot firmware request %s size %zu\n", 145 path, fw->size); 146 147 return fw; 148 } 149 150 void ath12k_core_free_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd) 151 { 152 if (!IS_ERR(bd->fw)) 153 release_firmware(bd->fw); 154 155 memset(bd, 0, sizeof(*bd)); 156 } 157 158 static int ath12k_core_parse_bd_ie_board(struct ath12k_base *ab, 159 struct ath12k_board_data *bd, 160 const void *buf, size_t buf_len, 161 const char *boardname, 162 int bd_ie_type) 163 { 164 const struct ath12k_fw_ie *hdr; 165 bool name_match_found; 166 int ret, board_ie_id; 167 size_t board_ie_len; 168 const void *board_ie_data; 169 170 name_match_found = false; 171 172 /* go through ATH12K_BD_IE_BOARD_ elements */ 173 while (buf_len > sizeof(struct ath12k_fw_ie)) { 174 hdr = buf; 175 board_ie_id = le32_to_cpu(hdr->id); 176 board_ie_len = le32_to_cpu(hdr->len); 177 board_ie_data = hdr->data; 178 179 buf_len -= sizeof(*hdr); 180 buf += sizeof(*hdr); 181 182 if (buf_len < ALIGN(board_ie_len, 4)) { 183 ath12k_err(ab, "invalid ATH12K_BD_IE_BOARD length: %zu < %zu\n", 184 buf_len, ALIGN(board_ie_len, 4)); 185 ret = -EINVAL; 186 goto out; 187 } 188 189 switch (board_ie_id) { 190 case ATH12K_BD_IE_BOARD_NAME: 191 ath12k_dbg_dump(ab, ATH12K_DBG_BOOT, "board name", "", 192 board_ie_data, board_ie_len); 193 194 if (board_ie_len != strlen(boardname)) 195 break; 196 197 ret = memcmp(board_ie_data, boardname, strlen(boardname)); 198 if (ret) 199 break; 200 201 name_match_found = true; 202 ath12k_dbg(ab, ATH12K_DBG_BOOT, 203 "boot found match for name '%s'", 204 boardname); 205 break; 206 case ATH12K_BD_IE_BOARD_DATA: 207 if (!name_match_found) 208 /* no match found */ 209 break; 210 211 ath12k_dbg(ab, ATH12K_DBG_BOOT, 212 "boot found board data for '%s'", boardname); 213 214 bd->data = board_ie_data; 215 bd->len = board_ie_len; 216 217 ret = 0; 218 goto out; 219 default: 220 ath12k_warn(ab, "unknown ATH12K_BD_IE_BOARD found: %d\n", 221 board_ie_id); 222 break; 223 } 224 225 /* jump over the padding */ 226 board_ie_len = ALIGN(board_ie_len, 4); 227 228 buf_len -= board_ie_len; 229 buf += board_ie_len; 230 } 231 232 /* no match found */ 233 ret = -ENOENT; 234 235 out: 236 return ret; 237 } 238 239 static int ath12k_core_fetch_board_data_api_n(struct ath12k_base *ab, 240 struct ath12k_board_data *bd, 241 const char *boardname) 242 { 243 size_t len, magic_len; 244 const u8 *data; 245 char *filename, filepath[100]; 246 size_t ie_len; 247 struct ath12k_fw_ie *hdr; 248 int ret, ie_id; 249 250 filename = ATH12K_BOARD_API2_FILE; 251 252 if (!bd->fw) 253 bd->fw = ath12k_core_firmware_request(ab, filename); 254 255 if (IS_ERR(bd->fw)) 256 return PTR_ERR(bd->fw); 257 258 data = bd->fw->data; 259 len = bd->fw->size; 260 261 ath12k_core_create_firmware_path(ab, filename, 262 filepath, sizeof(filepath)); 263 264 /* magic has extra null byte padded */ 265 magic_len = strlen(ATH12K_BOARD_MAGIC) + 1; 266 if (len < magic_len) { 267 ath12k_err(ab, "failed to find magic value in %s, file too short: %zu\n", 268 filepath, len); 269 ret = -EINVAL; 270 goto err; 271 } 272 273 if (memcmp(data, ATH12K_BOARD_MAGIC, magic_len)) { 274 ath12k_err(ab, "found invalid board magic\n"); 275 ret = -EINVAL; 276 goto err; 277 } 278 279 /* magic is padded to 4 bytes */ 280 magic_len = ALIGN(magic_len, 4); 281 if (len < magic_len) { 282 ath12k_err(ab, "failed: %s too small to contain board data, len: %zu\n", 283 filepath, len); 284 ret = -EINVAL; 285 goto err; 286 } 287 288 data += magic_len; 289 len -= magic_len; 290 291 while (len > sizeof(struct ath12k_fw_ie)) { 292 hdr = (struct ath12k_fw_ie *)data; 293 ie_id = le32_to_cpu(hdr->id); 294 ie_len = le32_to_cpu(hdr->len); 295 296 len -= sizeof(*hdr); 297 data = hdr->data; 298 299 if (len < ALIGN(ie_len, 4)) { 300 ath12k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n", 301 ie_id, ie_len, len); 302 ret = -EINVAL; 303 goto err; 304 } 305 306 switch (ie_id) { 307 case ATH12K_BD_IE_BOARD: 308 ret = ath12k_core_parse_bd_ie_board(ab, bd, data, 309 ie_len, 310 boardname, 311 ATH12K_BD_IE_BOARD); 312 if (ret == -ENOENT) 313 /* no match found, continue */ 314 break; 315 else if (ret) 316 /* there was an error, bail out */ 317 goto err; 318 /* either found or error, so stop searching */ 319 goto out; 320 } 321 322 /* jump over the padding */ 323 ie_len = ALIGN(ie_len, 4); 324 325 len -= ie_len; 326 data += ie_len; 327 } 328 329 out: 330 if (!bd->data || !bd->len) { 331 ath12k_err(ab, 332 "failed to fetch board data for %s from %s\n", 333 boardname, filepath); 334 ret = -ENODATA; 335 goto err; 336 } 337 338 return 0; 339 340 err: 341 ath12k_core_free_bdf(ab, bd); 342 return ret; 343 } 344 345 int ath12k_core_fetch_board_data_api_1(struct ath12k_base *ab, 346 struct ath12k_board_data *bd, 347 char *filename) 348 { 349 bd->fw = ath12k_core_firmware_request(ab, filename); 350 if (IS_ERR(bd->fw)) 351 return PTR_ERR(bd->fw); 352 353 bd->data = bd->fw->data; 354 bd->len = bd->fw->size; 355 356 return 0; 357 } 358 359 #define BOARD_NAME_SIZE 100 360 int ath12k_core_fetch_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd) 361 { 362 char boardname[BOARD_NAME_SIZE]; 363 int ret; 364 365 ret = ath12k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE); 366 if (ret) { 367 ath12k_err(ab, "failed to create board name: %d", ret); 368 return ret; 369 } 370 371 ab->bd_api = 2; 372 ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname); 373 if (!ret) 374 goto success; 375 376 ab->bd_api = 1; 377 ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_DEFAULT_BOARD_FILE); 378 if (ret) { 379 ath12k_err(ab, "failed to fetch board-2.bin or board.bin from %s\n", 380 ab->hw_params->fw.dir); 381 return ret; 382 } 383 384 success: 385 ath12k_dbg(ab, ATH12K_DBG_BOOT, "using board api %d\n", ab->bd_api); 386 return 0; 387 } 388 389 static void ath12k_core_stop(struct ath12k_base *ab) 390 { 391 if (!test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags)) 392 ath12k_qmi_firmware_stop(ab); 393 394 ath12k_hif_stop(ab); 395 ath12k_wmi_detach(ab); 396 ath12k_dp_rx_pdev_reo_cleanup(ab); 397 398 /* De-Init of components as needed */ 399 } 400 401 static int ath12k_core_soc_create(struct ath12k_base *ab) 402 { 403 int ret; 404 405 ret = ath12k_qmi_init_service(ab); 406 if (ret) { 407 ath12k_err(ab, "failed to initialize qmi :%d\n", ret); 408 return ret; 409 } 410 411 ret = ath12k_hif_power_up(ab); 412 if (ret) { 413 ath12k_err(ab, "failed to power up :%d\n", ret); 414 goto err_qmi_deinit; 415 } 416 417 return 0; 418 419 err_qmi_deinit: 420 ath12k_qmi_deinit_service(ab); 421 return ret; 422 } 423 424 static void ath12k_core_soc_destroy(struct ath12k_base *ab) 425 { 426 ath12k_dp_free(ab); 427 ath12k_reg_free(ab); 428 ath12k_qmi_deinit_service(ab); 429 } 430 431 static int ath12k_core_pdev_create(struct ath12k_base *ab) 432 { 433 int ret; 434 435 ret = ath12k_mac_register(ab); 436 if (ret) { 437 ath12k_err(ab, "failed register the radio with mac80211: %d\n", ret); 438 return ret; 439 } 440 441 ret = ath12k_dp_pdev_alloc(ab); 442 if (ret) { 443 ath12k_err(ab, "failed to attach DP pdev: %d\n", ret); 444 goto err_mac_unregister; 445 } 446 447 return 0; 448 449 err_mac_unregister: 450 ath12k_mac_unregister(ab); 451 452 return ret; 453 } 454 455 static void ath12k_core_pdev_destroy(struct ath12k_base *ab) 456 { 457 ath12k_mac_unregister(ab); 458 ath12k_hif_irq_disable(ab); 459 ath12k_dp_pdev_free(ab); 460 } 461 462 static int ath12k_core_start(struct ath12k_base *ab, 463 enum ath12k_firmware_mode mode) 464 { 465 int ret; 466 467 ret = ath12k_wmi_attach(ab); 468 if (ret) { 469 ath12k_err(ab, "failed to attach wmi: %d\n", ret); 470 return ret; 471 } 472 473 ret = ath12k_htc_init(ab); 474 if (ret) { 475 ath12k_err(ab, "failed to init htc: %d\n", ret); 476 goto err_wmi_detach; 477 } 478 479 ret = ath12k_hif_start(ab); 480 if (ret) { 481 ath12k_err(ab, "failed to start HIF: %d\n", ret); 482 goto err_wmi_detach; 483 } 484 485 ret = ath12k_htc_wait_target(&ab->htc); 486 if (ret) { 487 ath12k_err(ab, "failed to connect to HTC: %d\n", ret); 488 goto err_hif_stop; 489 } 490 491 ret = ath12k_dp_htt_connect(&ab->dp); 492 if (ret) { 493 ath12k_err(ab, "failed to connect to HTT: %d\n", ret); 494 goto err_hif_stop; 495 } 496 497 ret = ath12k_wmi_connect(ab); 498 if (ret) { 499 ath12k_err(ab, "failed to connect wmi: %d\n", ret); 500 goto err_hif_stop; 501 } 502 503 ret = ath12k_htc_start(&ab->htc); 504 if (ret) { 505 ath12k_err(ab, "failed to start HTC: %d\n", ret); 506 goto err_hif_stop; 507 } 508 509 ret = ath12k_wmi_wait_for_service_ready(ab); 510 if (ret) { 511 ath12k_err(ab, "failed to receive wmi service ready event: %d\n", 512 ret); 513 goto err_hif_stop; 514 } 515 516 ret = ath12k_mac_allocate(ab); 517 if (ret) { 518 ath12k_err(ab, "failed to create new hw device with mac80211 :%d\n", 519 ret); 520 goto err_hif_stop; 521 } 522 523 ath12k_dp_cc_config(ab); 524 525 ath12k_dp_pdev_pre_alloc(ab); 526 527 ret = ath12k_dp_rx_pdev_reo_setup(ab); 528 if (ret) { 529 ath12k_err(ab, "failed to initialize reo destination rings: %d\n", ret); 530 goto err_mac_destroy; 531 } 532 533 ret = ath12k_wmi_cmd_init(ab); 534 if (ret) { 535 ath12k_err(ab, "failed to send wmi init cmd: %d\n", ret); 536 goto err_reo_cleanup; 537 } 538 539 ret = ath12k_wmi_wait_for_unified_ready(ab); 540 if (ret) { 541 ath12k_err(ab, "failed to receive wmi unified ready event: %d\n", 542 ret); 543 goto err_reo_cleanup; 544 } 545 546 /* put hardware to DBS mode */ 547 if (ab->hw_params->single_pdev_only) { 548 ret = ath12k_wmi_set_hw_mode(ab, WMI_HOST_HW_MODE_DBS); 549 if (ret) { 550 ath12k_err(ab, "failed to send dbs mode: %d\n", ret); 551 goto err_reo_cleanup; 552 } 553 } 554 555 ret = ath12k_dp_tx_htt_h2t_ver_req_msg(ab); 556 if (ret) { 557 ath12k_err(ab, "failed to send htt version request message: %d\n", 558 ret); 559 goto err_reo_cleanup; 560 } 561 562 return 0; 563 564 err_reo_cleanup: 565 ath12k_dp_rx_pdev_reo_cleanup(ab); 566 err_mac_destroy: 567 ath12k_mac_destroy(ab); 568 err_hif_stop: 569 ath12k_hif_stop(ab); 570 err_wmi_detach: 571 ath12k_wmi_detach(ab); 572 return ret; 573 } 574 575 static int ath12k_core_start_firmware(struct ath12k_base *ab, 576 enum ath12k_firmware_mode mode) 577 { 578 int ret; 579 580 ath12k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v3, 581 &ab->qmi.ce_cfg.shadow_reg_v3_len); 582 583 ret = ath12k_qmi_firmware_start(ab, mode); 584 if (ret) { 585 ath12k_err(ab, "failed to send firmware start: %d\n", ret); 586 return ret; 587 } 588 589 return ret; 590 } 591 592 int ath12k_core_qmi_firmware_ready(struct ath12k_base *ab) 593 { 594 int ret; 595 596 ret = ath12k_core_start_firmware(ab, ATH12K_FIRMWARE_MODE_NORMAL); 597 if (ret) { 598 ath12k_err(ab, "failed to start firmware: %d\n", ret); 599 return ret; 600 } 601 602 ret = ath12k_ce_init_pipes(ab); 603 if (ret) { 604 ath12k_err(ab, "failed to initialize CE: %d\n", ret); 605 goto err_firmware_stop; 606 } 607 608 ret = ath12k_dp_alloc(ab); 609 if (ret) { 610 ath12k_err(ab, "failed to init DP: %d\n", ret); 611 goto err_firmware_stop; 612 } 613 614 mutex_lock(&ab->core_lock); 615 ret = ath12k_core_start(ab, ATH12K_FIRMWARE_MODE_NORMAL); 616 if (ret) { 617 ath12k_err(ab, "failed to start core: %d\n", ret); 618 goto err_dp_free; 619 } 620 621 ret = ath12k_core_pdev_create(ab); 622 if (ret) { 623 ath12k_err(ab, "failed to create pdev core: %d\n", ret); 624 goto err_core_stop; 625 } 626 ath12k_hif_irq_enable(ab); 627 628 ret = ath12k_core_rfkill_config(ab); 629 if (ret && ret != -EOPNOTSUPP) { 630 ath12k_err(ab, "failed to config rfkill: %d\n", ret); 631 goto err_core_stop; 632 } 633 634 mutex_unlock(&ab->core_lock); 635 636 return 0; 637 638 err_core_stop: 639 ath12k_core_stop(ab); 640 ath12k_mac_destroy(ab); 641 err_dp_free: 642 ath12k_dp_free(ab); 643 mutex_unlock(&ab->core_lock); 644 err_firmware_stop: 645 ath12k_qmi_firmware_stop(ab); 646 647 return ret; 648 } 649 650 static int ath12k_core_reconfigure_on_crash(struct ath12k_base *ab) 651 { 652 int ret; 653 654 mutex_lock(&ab->core_lock); 655 ath12k_hif_irq_disable(ab); 656 ath12k_dp_pdev_free(ab); 657 ath12k_hif_stop(ab); 658 ath12k_wmi_detach(ab); 659 ath12k_dp_rx_pdev_reo_cleanup(ab); 660 mutex_unlock(&ab->core_lock); 661 662 ath12k_dp_free(ab); 663 ath12k_hal_srng_deinit(ab); 664 665 ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1; 666 667 ret = ath12k_hal_srng_init(ab); 668 if (ret) 669 return ret; 670 671 clear_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags); 672 673 ret = ath12k_core_qmi_firmware_ready(ab); 674 if (ret) 675 goto err_hal_srng_deinit; 676 677 clear_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags); 678 679 return 0; 680 681 err_hal_srng_deinit: 682 ath12k_hal_srng_deinit(ab); 683 return ret; 684 } 685 686 static void ath12k_rfkill_work(struct work_struct *work) 687 { 688 struct ath12k_base *ab = container_of(work, struct ath12k_base, rfkill_work); 689 struct ath12k *ar; 690 bool rfkill_radio_on; 691 int i; 692 693 spin_lock_bh(&ab->base_lock); 694 rfkill_radio_on = ab->rfkill_radio_on; 695 spin_unlock_bh(&ab->base_lock); 696 697 for (i = 0; i < ab->num_radios; i++) { 698 ar = ab->pdevs[i].ar; 699 if (!ar) 700 continue; 701 702 ath12k_mac_rfkill_enable_radio(ar, rfkill_radio_on); 703 wiphy_rfkill_set_hw_state(ar->hw->wiphy, !rfkill_radio_on); 704 } 705 } 706 707 void ath12k_core_halt(struct ath12k *ar) 708 { 709 struct ath12k_base *ab = ar->ab; 710 711 lockdep_assert_held(&ar->conf_mutex); 712 713 ar->num_created_vdevs = 0; 714 ar->allocated_vdev_map = 0; 715 716 ath12k_mac_scan_finish(ar); 717 ath12k_mac_peer_cleanup_all(ar); 718 cancel_delayed_work_sync(&ar->scan.timeout); 719 cancel_work_sync(&ar->regd_update_work); 720 cancel_work_sync(&ab->rfkill_work); 721 722 rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL); 723 synchronize_rcu(); 724 INIT_LIST_HEAD(&ar->arvifs); 725 idr_init(&ar->txmgmt_idr); 726 } 727 728 static void ath12k_core_pre_reconfigure_recovery(struct ath12k_base *ab) 729 { 730 struct ath12k *ar; 731 struct ath12k_pdev *pdev; 732 int i; 733 734 spin_lock_bh(&ab->base_lock); 735 ab->stats.fw_crash_counter++; 736 spin_unlock_bh(&ab->base_lock); 737 738 if (ab->is_reset) 739 set_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags); 740 741 for (i = 0; i < ab->num_radios; i++) { 742 pdev = &ab->pdevs[i]; 743 ar = pdev->ar; 744 if (!ar || ar->state == ATH12K_STATE_OFF) 745 continue; 746 747 ieee80211_stop_queues(ar->hw); 748 ath12k_mac_drain_tx(ar); 749 complete(&ar->scan.started); 750 complete(&ar->scan.completed); 751 complete(&ar->peer_assoc_done); 752 complete(&ar->peer_delete_done); 753 complete(&ar->install_key_done); 754 complete(&ar->vdev_setup_done); 755 complete(&ar->vdev_delete_done); 756 complete(&ar->bss_survey_done); 757 758 wake_up(&ar->dp.tx_empty_waitq); 759 idr_for_each(&ar->txmgmt_idr, 760 ath12k_mac_tx_mgmt_pending_free, ar); 761 idr_destroy(&ar->txmgmt_idr); 762 wake_up(&ar->txmgmt_empty_waitq); 763 } 764 765 wake_up(&ab->wmi_ab.tx_credits_wq); 766 wake_up(&ab->peer_mapping_wq); 767 } 768 769 static void ath12k_core_post_reconfigure_recovery(struct ath12k_base *ab) 770 { 771 struct ath12k *ar; 772 struct ath12k_pdev *pdev; 773 int i; 774 775 for (i = 0; i < ab->num_radios; i++) { 776 pdev = &ab->pdevs[i]; 777 ar = pdev->ar; 778 if (!ar || ar->state == ATH12K_STATE_OFF) 779 continue; 780 781 mutex_lock(&ar->conf_mutex); 782 783 switch (ar->state) { 784 case ATH12K_STATE_ON: 785 ar->state = ATH12K_STATE_RESTARTING; 786 ath12k_core_halt(ar); 787 ieee80211_restart_hw(ar->hw); 788 break; 789 case ATH12K_STATE_OFF: 790 ath12k_warn(ab, 791 "cannot restart radio %d that hasn't been started\n", 792 i); 793 break; 794 case ATH12K_STATE_RESTARTING: 795 break; 796 case ATH12K_STATE_RESTARTED: 797 ar->state = ATH12K_STATE_WEDGED; 798 fallthrough; 799 case ATH12K_STATE_WEDGED: 800 ath12k_warn(ab, 801 "device is wedged, will not restart radio %d\n", i); 802 break; 803 } 804 mutex_unlock(&ar->conf_mutex); 805 } 806 complete(&ab->driver_recovery); 807 } 808 809 static void ath12k_core_restart(struct work_struct *work) 810 { 811 struct ath12k_base *ab = container_of(work, struct ath12k_base, restart_work); 812 int ret; 813 814 if (!ab->is_reset) 815 ath12k_core_pre_reconfigure_recovery(ab); 816 817 ret = ath12k_core_reconfigure_on_crash(ab); 818 if (ret) { 819 ath12k_err(ab, "failed to reconfigure driver on crash recovery\n"); 820 return; 821 } 822 823 if (ab->is_reset) 824 complete_all(&ab->reconfigure_complete); 825 826 if (!ab->is_reset) 827 ath12k_core_post_reconfigure_recovery(ab); 828 } 829 830 static void ath12k_core_reset(struct work_struct *work) 831 { 832 struct ath12k_base *ab = container_of(work, struct ath12k_base, reset_work); 833 int reset_count, fail_cont_count; 834 long time_left; 835 836 if (!(test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))) { 837 ath12k_warn(ab, "ignore reset dev flags 0x%lx\n", ab->dev_flags); 838 return; 839 } 840 841 /* Sometimes the recovery will fail and then the next all recovery fail, 842 * this is to avoid infinite recovery since it can not recovery success 843 */ 844 fail_cont_count = atomic_read(&ab->fail_cont_count); 845 846 if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FINAL) 847 return; 848 849 if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FIRST && 850 time_before(jiffies, ab->reset_fail_timeout)) 851 return; 852 853 reset_count = atomic_inc_return(&ab->reset_count); 854 855 if (reset_count > 1) { 856 /* Sometimes it happened another reset worker before the previous one 857 * completed, then the second reset worker will destroy the previous one, 858 * thus below is to avoid that. 859 */ 860 ath12k_warn(ab, "already resetting count %d\n", reset_count); 861 862 reinit_completion(&ab->reset_complete); 863 time_left = wait_for_completion_timeout(&ab->reset_complete, 864 ATH12K_RESET_TIMEOUT_HZ); 865 if (time_left) { 866 ath12k_dbg(ab, ATH12K_DBG_BOOT, "to skip reset\n"); 867 atomic_dec(&ab->reset_count); 868 return; 869 } 870 871 ab->reset_fail_timeout = jiffies + ATH12K_RESET_FAIL_TIMEOUT_HZ; 872 /* Record the continuous recovery fail count when recovery failed*/ 873 fail_cont_count = atomic_inc_return(&ab->fail_cont_count); 874 } 875 876 ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset starting\n"); 877 878 ab->is_reset = true; 879 atomic_set(&ab->recovery_start_count, 0); 880 reinit_completion(&ab->recovery_start); 881 atomic_set(&ab->recovery_count, 0); 882 883 ath12k_core_pre_reconfigure_recovery(ab); 884 885 reinit_completion(&ab->reconfigure_complete); 886 ath12k_core_post_reconfigure_recovery(ab); 887 888 ath12k_dbg(ab, ATH12K_DBG_BOOT, "waiting recovery start...\n"); 889 890 time_left = wait_for_completion_timeout(&ab->recovery_start, 891 ATH12K_RECOVER_START_TIMEOUT_HZ); 892 893 ath12k_hif_power_down(ab); 894 ath12k_hif_power_up(ab); 895 896 ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset started\n"); 897 } 898 899 int ath12k_core_pre_init(struct ath12k_base *ab) 900 { 901 int ret; 902 903 ret = ath12k_hw_init(ab); 904 if (ret) { 905 ath12k_err(ab, "failed to init hw params: %d\n", ret); 906 return ret; 907 } 908 909 return 0; 910 } 911 912 int ath12k_core_init(struct ath12k_base *ab) 913 { 914 int ret; 915 916 ret = ath12k_core_soc_create(ab); 917 if (ret) { 918 ath12k_err(ab, "failed to create soc core: %d\n", ret); 919 return ret; 920 } 921 922 return 0; 923 } 924 925 void ath12k_core_deinit(struct ath12k_base *ab) 926 { 927 mutex_lock(&ab->core_lock); 928 929 ath12k_core_pdev_destroy(ab); 930 ath12k_core_stop(ab); 931 932 mutex_unlock(&ab->core_lock); 933 934 ath12k_hif_power_down(ab); 935 ath12k_mac_destroy(ab); 936 ath12k_core_soc_destroy(ab); 937 } 938 939 void ath12k_core_free(struct ath12k_base *ab) 940 { 941 timer_delete_sync(&ab->rx_replenish_retry); 942 destroy_workqueue(ab->workqueue_aux); 943 destroy_workqueue(ab->workqueue); 944 kfree(ab); 945 } 946 947 struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, 948 enum ath12k_bus bus) 949 { 950 struct ath12k_base *ab; 951 952 ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL); 953 if (!ab) 954 return NULL; 955 956 init_completion(&ab->driver_recovery); 957 958 ab->workqueue = create_singlethread_workqueue("ath12k_wq"); 959 if (!ab->workqueue) 960 goto err_sc_free; 961 962 ab->workqueue_aux = create_singlethread_workqueue("ath12k_aux_wq"); 963 if (!ab->workqueue_aux) 964 goto err_free_wq; 965 966 mutex_init(&ab->core_lock); 967 spin_lock_init(&ab->base_lock); 968 init_completion(&ab->reset_complete); 969 init_completion(&ab->reconfigure_complete); 970 init_completion(&ab->recovery_start); 971 972 INIT_LIST_HEAD(&ab->peers); 973 init_waitqueue_head(&ab->peer_mapping_wq); 974 init_waitqueue_head(&ab->wmi_ab.tx_credits_wq); 975 INIT_WORK(&ab->restart_work, ath12k_core_restart); 976 INIT_WORK(&ab->reset_work, ath12k_core_reset); 977 INIT_WORK(&ab->rfkill_work, ath12k_rfkill_work); 978 979 timer_setup(&ab->rx_replenish_retry, ath12k_ce_rx_replenish_retry, 0); 980 init_completion(&ab->htc_suspend); 981 982 ab->dev = dev; 983 ab->hif.bus = bus; 984 985 return ab; 986 987 err_free_wq: 988 destroy_workqueue(ab->workqueue); 989 err_sc_free: 990 kfree(ab); 991 return NULL; 992 } 993 994 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11be wireless LAN cards."); 995 MODULE_LICENSE("Dual BSD/GPL"); 996