1 // SPDX-License-Identifier: BSD-3-Clause-Clear 2 /* 3 * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved. 4 * Copyright (c) 2021-2023 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 bd_api; 364 int ret; 365 366 ret = ath12k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE); 367 if (ret) { 368 ath12k_err(ab, "failed to create board name: %d", ret); 369 return ret; 370 } 371 372 bd_api = 2; 373 ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname); 374 if (!ret) 375 goto success; 376 377 bd_api = 1; 378 ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_DEFAULT_BOARD_FILE); 379 if (ret) { 380 ath12k_err(ab, "failed to fetch board-2.bin or board.bin from %s\n", 381 ab->hw_params->fw.dir); 382 return ret; 383 } 384 385 success: 386 ath12k_dbg(ab, ATH12K_DBG_BOOT, "using board api %d\n", bd_api); 387 return 0; 388 } 389 390 static void ath12k_core_stop(struct ath12k_base *ab) 391 { 392 if (!test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags)) 393 ath12k_qmi_firmware_stop(ab); 394 395 ath12k_hif_stop(ab); 396 ath12k_wmi_detach(ab); 397 ath12k_dp_rx_pdev_reo_cleanup(ab); 398 399 /* De-Init of components as needed */ 400 } 401 402 static void ath12k_core_check_bdfext(const struct dmi_header *hdr, void *data) 403 { 404 struct ath12k_base *ab = data; 405 const char *magic = ATH12K_SMBIOS_BDF_EXT_MAGIC; 406 struct ath12k_smbios_bdf *smbios = (struct ath12k_smbios_bdf *)hdr; 407 ssize_t copied; 408 size_t len; 409 int i; 410 411 if (ab->qmi.target.bdf_ext[0] != '\0') 412 return; 413 414 if (hdr->type != ATH12K_SMBIOS_BDF_EXT_TYPE) 415 return; 416 417 if (hdr->length != ATH12K_SMBIOS_BDF_EXT_LENGTH) { 418 ath12k_dbg(ab, ATH12K_DBG_BOOT, 419 "wrong smbios bdf ext type length (%d).\n", 420 hdr->length); 421 return; 422 } 423 424 if (!smbios->bdf_enabled) { 425 ath12k_dbg(ab, ATH12K_DBG_BOOT, "bdf variant name not found.\n"); 426 return; 427 } 428 429 /* Only one string exists (per spec) */ 430 if (memcmp(smbios->bdf_ext, magic, strlen(magic)) != 0) { 431 ath12k_dbg(ab, ATH12K_DBG_BOOT, 432 "bdf variant magic does not match.\n"); 433 return; 434 } 435 436 len = min_t(size_t, 437 strlen(smbios->bdf_ext), sizeof(ab->qmi.target.bdf_ext)); 438 for (i = 0; i < len; i++) { 439 if (!isascii(smbios->bdf_ext[i]) || !isprint(smbios->bdf_ext[i])) { 440 ath12k_dbg(ab, ATH12K_DBG_BOOT, 441 "bdf variant name contains non ascii chars.\n"); 442 return; 443 } 444 } 445 446 /* Copy extension name without magic prefix */ 447 copied = strscpy(ab->qmi.target.bdf_ext, smbios->bdf_ext + strlen(magic), 448 sizeof(ab->qmi.target.bdf_ext)); 449 if (copied < 0) { 450 ath12k_dbg(ab, ATH12K_DBG_BOOT, 451 "bdf variant string is longer than the buffer can accommodate\n"); 452 return; 453 } 454 455 ath12k_dbg(ab, ATH12K_DBG_BOOT, 456 "found and validated bdf variant smbios_type 0x%x bdf %s\n", 457 ATH12K_SMBIOS_BDF_EXT_TYPE, ab->qmi.target.bdf_ext); 458 } 459 460 int ath12k_core_check_smbios(struct ath12k_base *ab) 461 { 462 ab->qmi.target.bdf_ext[0] = '\0'; 463 dmi_walk(ath12k_core_check_bdfext, ab); 464 465 if (ab->qmi.target.bdf_ext[0] == '\0') 466 return -ENODATA; 467 468 return 0; 469 } 470 471 static int ath12k_core_soc_create(struct ath12k_base *ab) 472 { 473 int ret; 474 475 ret = ath12k_qmi_init_service(ab); 476 if (ret) { 477 ath12k_err(ab, "failed to initialize qmi :%d\n", ret); 478 return ret; 479 } 480 481 ret = ath12k_hif_power_up(ab); 482 if (ret) { 483 ath12k_err(ab, "failed to power up :%d\n", ret); 484 goto err_qmi_deinit; 485 } 486 487 return 0; 488 489 err_qmi_deinit: 490 ath12k_qmi_deinit_service(ab); 491 return ret; 492 } 493 494 static void ath12k_core_soc_destroy(struct ath12k_base *ab) 495 { 496 ath12k_dp_free(ab); 497 ath12k_reg_free(ab); 498 ath12k_qmi_deinit_service(ab); 499 } 500 501 static int ath12k_core_pdev_create(struct ath12k_base *ab) 502 { 503 int ret; 504 505 ret = ath12k_mac_register(ab); 506 if (ret) { 507 ath12k_err(ab, "failed register the radio with mac80211: %d\n", ret); 508 return ret; 509 } 510 511 ret = ath12k_dp_pdev_alloc(ab); 512 if (ret) { 513 ath12k_err(ab, "failed to attach DP pdev: %d\n", ret); 514 goto err_mac_unregister; 515 } 516 517 return 0; 518 519 err_mac_unregister: 520 ath12k_mac_unregister(ab); 521 522 return ret; 523 } 524 525 static void ath12k_core_pdev_destroy(struct ath12k_base *ab) 526 { 527 ath12k_mac_unregister(ab); 528 ath12k_hif_irq_disable(ab); 529 ath12k_dp_pdev_free(ab); 530 } 531 532 static int ath12k_core_start(struct ath12k_base *ab, 533 enum ath12k_firmware_mode mode) 534 { 535 int ret; 536 537 ret = ath12k_wmi_attach(ab); 538 if (ret) { 539 ath12k_err(ab, "failed to attach wmi: %d\n", ret); 540 return ret; 541 } 542 543 ret = ath12k_htc_init(ab); 544 if (ret) { 545 ath12k_err(ab, "failed to init htc: %d\n", ret); 546 goto err_wmi_detach; 547 } 548 549 ret = ath12k_hif_start(ab); 550 if (ret) { 551 ath12k_err(ab, "failed to start HIF: %d\n", ret); 552 goto err_wmi_detach; 553 } 554 555 ret = ath12k_htc_wait_target(&ab->htc); 556 if (ret) { 557 ath12k_err(ab, "failed to connect to HTC: %d\n", ret); 558 goto err_hif_stop; 559 } 560 561 ret = ath12k_dp_htt_connect(&ab->dp); 562 if (ret) { 563 ath12k_err(ab, "failed to connect to HTT: %d\n", ret); 564 goto err_hif_stop; 565 } 566 567 ret = ath12k_wmi_connect(ab); 568 if (ret) { 569 ath12k_err(ab, "failed to connect wmi: %d\n", ret); 570 goto err_hif_stop; 571 } 572 573 ret = ath12k_htc_start(&ab->htc); 574 if (ret) { 575 ath12k_err(ab, "failed to start HTC: %d\n", ret); 576 goto err_hif_stop; 577 } 578 579 ret = ath12k_wmi_wait_for_service_ready(ab); 580 if (ret) { 581 ath12k_err(ab, "failed to receive wmi service ready event: %d\n", 582 ret); 583 goto err_hif_stop; 584 } 585 586 ret = ath12k_mac_allocate(ab); 587 if (ret) { 588 ath12k_err(ab, "failed to create new hw device with mac80211 :%d\n", 589 ret); 590 goto err_hif_stop; 591 } 592 593 ath12k_dp_cc_config(ab); 594 595 ath12k_dp_pdev_pre_alloc(ab); 596 597 ret = ath12k_dp_rx_pdev_reo_setup(ab); 598 if (ret) { 599 ath12k_err(ab, "failed to initialize reo destination rings: %d\n", ret); 600 goto err_mac_destroy; 601 } 602 603 ret = ath12k_wmi_cmd_init(ab); 604 if (ret) { 605 ath12k_err(ab, "failed to send wmi init cmd: %d\n", ret); 606 goto err_reo_cleanup; 607 } 608 609 ret = ath12k_wmi_wait_for_unified_ready(ab); 610 if (ret) { 611 ath12k_err(ab, "failed to receive wmi unified ready event: %d\n", 612 ret); 613 goto err_reo_cleanup; 614 } 615 616 /* put hardware to DBS mode */ 617 if (ab->hw_params->single_pdev_only) { 618 ret = ath12k_wmi_set_hw_mode(ab, WMI_HOST_HW_MODE_DBS); 619 if (ret) { 620 ath12k_err(ab, "failed to send dbs mode: %d\n", ret); 621 goto err_reo_cleanup; 622 } 623 } 624 625 ret = ath12k_dp_tx_htt_h2t_ver_req_msg(ab); 626 if (ret) { 627 ath12k_err(ab, "failed to send htt version request message: %d\n", 628 ret); 629 goto err_reo_cleanup; 630 } 631 632 return 0; 633 634 err_reo_cleanup: 635 ath12k_dp_rx_pdev_reo_cleanup(ab); 636 err_mac_destroy: 637 ath12k_mac_destroy(ab); 638 err_hif_stop: 639 ath12k_hif_stop(ab); 640 err_wmi_detach: 641 ath12k_wmi_detach(ab); 642 return ret; 643 } 644 645 static int ath12k_core_start_firmware(struct ath12k_base *ab, 646 enum ath12k_firmware_mode mode) 647 { 648 int ret; 649 650 ath12k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v3, 651 &ab->qmi.ce_cfg.shadow_reg_v3_len); 652 653 ret = ath12k_qmi_firmware_start(ab, mode); 654 if (ret) { 655 ath12k_err(ab, "failed to send firmware start: %d\n", ret); 656 return ret; 657 } 658 659 return ret; 660 } 661 662 int ath12k_core_qmi_firmware_ready(struct ath12k_base *ab) 663 { 664 int ret; 665 666 ret = ath12k_core_start_firmware(ab, ATH12K_FIRMWARE_MODE_NORMAL); 667 if (ret) { 668 ath12k_err(ab, "failed to start firmware: %d\n", ret); 669 return ret; 670 } 671 672 ret = ath12k_ce_init_pipes(ab); 673 if (ret) { 674 ath12k_err(ab, "failed to initialize CE: %d\n", ret); 675 goto err_firmware_stop; 676 } 677 678 ret = ath12k_dp_alloc(ab); 679 if (ret) { 680 ath12k_err(ab, "failed to init DP: %d\n", ret); 681 goto err_firmware_stop; 682 } 683 684 mutex_lock(&ab->core_lock); 685 ret = ath12k_core_start(ab, ATH12K_FIRMWARE_MODE_NORMAL); 686 if (ret) { 687 ath12k_err(ab, "failed to start core: %d\n", ret); 688 goto err_dp_free; 689 } 690 691 ret = ath12k_core_pdev_create(ab); 692 if (ret) { 693 ath12k_err(ab, "failed to create pdev core: %d\n", ret); 694 goto err_core_stop; 695 } 696 ath12k_hif_irq_enable(ab); 697 698 ret = ath12k_core_rfkill_config(ab); 699 if (ret && ret != -EOPNOTSUPP) { 700 ath12k_err(ab, "failed to config rfkill: %d\n", ret); 701 goto err_core_pdev_destroy; 702 } 703 704 mutex_unlock(&ab->core_lock); 705 706 return 0; 707 708 err_core_pdev_destroy: 709 ath12k_core_pdev_destroy(ab); 710 err_core_stop: 711 ath12k_core_stop(ab); 712 ath12k_mac_destroy(ab); 713 err_dp_free: 714 ath12k_dp_free(ab); 715 mutex_unlock(&ab->core_lock); 716 err_firmware_stop: 717 ath12k_qmi_firmware_stop(ab); 718 719 return ret; 720 } 721 722 static int ath12k_core_reconfigure_on_crash(struct ath12k_base *ab) 723 { 724 int ret; 725 726 mutex_lock(&ab->core_lock); 727 ath12k_hif_irq_disable(ab); 728 ath12k_dp_pdev_free(ab); 729 ath12k_hif_stop(ab); 730 ath12k_wmi_detach(ab); 731 ath12k_dp_rx_pdev_reo_cleanup(ab); 732 mutex_unlock(&ab->core_lock); 733 734 ath12k_dp_free(ab); 735 ath12k_hal_srng_deinit(ab); 736 737 ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1; 738 739 ret = ath12k_hal_srng_init(ab); 740 if (ret) 741 return ret; 742 743 clear_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags); 744 745 ret = ath12k_core_qmi_firmware_ready(ab); 746 if (ret) 747 goto err_hal_srng_deinit; 748 749 clear_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags); 750 751 return 0; 752 753 err_hal_srng_deinit: 754 ath12k_hal_srng_deinit(ab); 755 return ret; 756 } 757 758 static void ath12k_rfkill_work(struct work_struct *work) 759 { 760 struct ath12k_base *ab = container_of(work, struct ath12k_base, rfkill_work); 761 struct ath12k *ar; 762 bool rfkill_radio_on; 763 int i; 764 765 spin_lock_bh(&ab->base_lock); 766 rfkill_radio_on = ab->rfkill_radio_on; 767 spin_unlock_bh(&ab->base_lock); 768 769 for (i = 0; i < ab->num_radios; i++) { 770 ar = ab->pdevs[i].ar; 771 if (!ar) 772 continue; 773 774 ath12k_mac_rfkill_enable_radio(ar, rfkill_radio_on); 775 wiphy_rfkill_set_hw_state(ar->hw->wiphy, !rfkill_radio_on); 776 } 777 } 778 779 void ath12k_core_halt(struct ath12k *ar) 780 { 781 struct ath12k_base *ab = ar->ab; 782 783 lockdep_assert_held(&ar->conf_mutex); 784 785 ar->num_created_vdevs = 0; 786 ar->allocated_vdev_map = 0; 787 788 ath12k_mac_scan_finish(ar); 789 ath12k_mac_peer_cleanup_all(ar); 790 cancel_delayed_work_sync(&ar->scan.timeout); 791 cancel_work_sync(&ar->regd_update_work); 792 cancel_work_sync(&ab->rfkill_work); 793 794 rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL); 795 synchronize_rcu(); 796 INIT_LIST_HEAD(&ar->arvifs); 797 idr_init(&ar->txmgmt_idr); 798 } 799 800 static void ath12k_core_pre_reconfigure_recovery(struct ath12k_base *ab) 801 { 802 struct ath12k *ar; 803 struct ath12k_pdev *pdev; 804 int i; 805 806 spin_lock_bh(&ab->base_lock); 807 ab->stats.fw_crash_counter++; 808 spin_unlock_bh(&ab->base_lock); 809 810 if (ab->is_reset) 811 set_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags); 812 813 for (i = 0; i < ab->num_radios; i++) { 814 pdev = &ab->pdevs[i]; 815 ar = pdev->ar; 816 if (!ar || ar->state == ATH12K_STATE_OFF) 817 continue; 818 819 ieee80211_stop_queues(ar->hw); 820 ath12k_mac_drain_tx(ar); 821 complete(&ar->scan.started); 822 complete(&ar->scan.completed); 823 complete(&ar->peer_assoc_done); 824 complete(&ar->peer_delete_done); 825 complete(&ar->install_key_done); 826 complete(&ar->vdev_setup_done); 827 complete(&ar->vdev_delete_done); 828 complete(&ar->bss_survey_done); 829 830 wake_up(&ar->dp.tx_empty_waitq); 831 idr_for_each(&ar->txmgmt_idr, 832 ath12k_mac_tx_mgmt_pending_free, ar); 833 idr_destroy(&ar->txmgmt_idr); 834 wake_up(&ar->txmgmt_empty_waitq); 835 } 836 837 wake_up(&ab->wmi_ab.tx_credits_wq); 838 wake_up(&ab->peer_mapping_wq); 839 } 840 841 static void ath12k_core_post_reconfigure_recovery(struct ath12k_base *ab) 842 { 843 struct ath12k *ar; 844 struct ath12k_pdev *pdev; 845 int i; 846 847 for (i = 0; i < ab->num_radios; i++) { 848 pdev = &ab->pdevs[i]; 849 ar = pdev->ar; 850 if (!ar || ar->state == ATH12K_STATE_OFF) 851 continue; 852 853 mutex_lock(&ar->conf_mutex); 854 855 switch (ar->state) { 856 case ATH12K_STATE_ON: 857 ar->state = ATH12K_STATE_RESTARTING; 858 ath12k_core_halt(ar); 859 ieee80211_restart_hw(ar->hw); 860 break; 861 case ATH12K_STATE_OFF: 862 ath12k_warn(ab, 863 "cannot restart radio %d that hasn't been started\n", 864 i); 865 break; 866 case ATH12K_STATE_RESTARTING: 867 break; 868 case ATH12K_STATE_RESTARTED: 869 ar->state = ATH12K_STATE_WEDGED; 870 fallthrough; 871 case ATH12K_STATE_WEDGED: 872 ath12k_warn(ab, 873 "device is wedged, will not restart radio %d\n", i); 874 break; 875 } 876 mutex_unlock(&ar->conf_mutex); 877 } 878 complete(&ab->driver_recovery); 879 } 880 881 static void ath12k_core_restart(struct work_struct *work) 882 { 883 struct ath12k_base *ab = container_of(work, struct ath12k_base, restart_work); 884 int ret; 885 886 if (!ab->is_reset) 887 ath12k_core_pre_reconfigure_recovery(ab); 888 889 ret = ath12k_core_reconfigure_on_crash(ab); 890 if (ret) { 891 ath12k_err(ab, "failed to reconfigure driver on crash recovery\n"); 892 return; 893 } 894 895 if (ab->is_reset) 896 complete_all(&ab->reconfigure_complete); 897 898 if (!ab->is_reset) 899 ath12k_core_post_reconfigure_recovery(ab); 900 } 901 902 static void ath12k_core_reset(struct work_struct *work) 903 { 904 struct ath12k_base *ab = container_of(work, struct ath12k_base, reset_work); 905 int reset_count, fail_cont_count; 906 long time_left; 907 908 if (!(test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))) { 909 ath12k_warn(ab, "ignore reset dev flags 0x%lx\n", ab->dev_flags); 910 return; 911 } 912 913 /* Sometimes the recovery will fail and then the next all recovery fail, 914 * this is to avoid infinite recovery since it can not recovery success 915 */ 916 fail_cont_count = atomic_read(&ab->fail_cont_count); 917 918 if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FINAL) 919 return; 920 921 if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FIRST && 922 time_before(jiffies, ab->reset_fail_timeout)) 923 return; 924 925 reset_count = atomic_inc_return(&ab->reset_count); 926 927 if (reset_count > 1) { 928 /* Sometimes it happened another reset worker before the previous one 929 * completed, then the second reset worker will destroy the previous one, 930 * thus below is to avoid that. 931 */ 932 ath12k_warn(ab, "already resetting count %d\n", reset_count); 933 934 reinit_completion(&ab->reset_complete); 935 time_left = wait_for_completion_timeout(&ab->reset_complete, 936 ATH12K_RESET_TIMEOUT_HZ); 937 if (time_left) { 938 ath12k_dbg(ab, ATH12K_DBG_BOOT, "to skip reset\n"); 939 atomic_dec(&ab->reset_count); 940 return; 941 } 942 943 ab->reset_fail_timeout = jiffies + ATH12K_RESET_FAIL_TIMEOUT_HZ; 944 /* Record the continuous recovery fail count when recovery failed*/ 945 fail_cont_count = atomic_inc_return(&ab->fail_cont_count); 946 } 947 948 ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset starting\n"); 949 950 ab->is_reset = true; 951 atomic_set(&ab->recovery_start_count, 0); 952 reinit_completion(&ab->recovery_start); 953 atomic_set(&ab->recovery_count, 0); 954 955 ath12k_core_pre_reconfigure_recovery(ab); 956 957 reinit_completion(&ab->reconfigure_complete); 958 ath12k_core_post_reconfigure_recovery(ab); 959 960 ath12k_dbg(ab, ATH12K_DBG_BOOT, "waiting recovery start...\n"); 961 962 time_left = wait_for_completion_timeout(&ab->recovery_start, 963 ATH12K_RECOVER_START_TIMEOUT_HZ); 964 965 ath12k_hif_power_down(ab); 966 ath12k_qmi_free_resource(ab); 967 ath12k_hif_power_up(ab); 968 969 ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset started\n"); 970 } 971 972 int ath12k_core_pre_init(struct ath12k_base *ab) 973 { 974 int ret; 975 976 ret = ath12k_hw_init(ab); 977 if (ret) { 978 ath12k_err(ab, "failed to init hw params: %d\n", ret); 979 return ret; 980 } 981 982 return 0; 983 } 984 985 int ath12k_core_init(struct ath12k_base *ab) 986 { 987 int ret; 988 989 ret = ath12k_core_soc_create(ab); 990 if (ret) { 991 ath12k_err(ab, "failed to create soc core: %d\n", ret); 992 return ret; 993 } 994 995 return 0; 996 } 997 998 void ath12k_core_deinit(struct ath12k_base *ab) 999 { 1000 mutex_lock(&ab->core_lock); 1001 1002 ath12k_core_pdev_destroy(ab); 1003 ath12k_core_stop(ab); 1004 1005 mutex_unlock(&ab->core_lock); 1006 1007 ath12k_hif_power_down(ab); 1008 ath12k_mac_destroy(ab); 1009 ath12k_core_soc_destroy(ab); 1010 } 1011 1012 void ath12k_core_free(struct ath12k_base *ab) 1013 { 1014 timer_delete_sync(&ab->rx_replenish_retry); 1015 destroy_workqueue(ab->workqueue_aux); 1016 destroy_workqueue(ab->workqueue); 1017 kfree(ab); 1018 } 1019 1020 struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, 1021 enum ath12k_bus bus) 1022 { 1023 struct ath12k_base *ab; 1024 1025 ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL); 1026 if (!ab) 1027 return NULL; 1028 1029 init_completion(&ab->driver_recovery); 1030 1031 ab->workqueue = create_singlethread_workqueue("ath12k_wq"); 1032 if (!ab->workqueue) 1033 goto err_sc_free; 1034 1035 ab->workqueue_aux = create_singlethread_workqueue("ath12k_aux_wq"); 1036 if (!ab->workqueue_aux) 1037 goto err_free_wq; 1038 1039 mutex_init(&ab->core_lock); 1040 spin_lock_init(&ab->base_lock); 1041 init_completion(&ab->reset_complete); 1042 init_completion(&ab->reconfigure_complete); 1043 init_completion(&ab->recovery_start); 1044 1045 INIT_LIST_HEAD(&ab->peers); 1046 init_waitqueue_head(&ab->peer_mapping_wq); 1047 init_waitqueue_head(&ab->wmi_ab.tx_credits_wq); 1048 INIT_WORK(&ab->restart_work, ath12k_core_restart); 1049 INIT_WORK(&ab->reset_work, ath12k_core_reset); 1050 INIT_WORK(&ab->rfkill_work, ath12k_rfkill_work); 1051 1052 timer_setup(&ab->rx_replenish_retry, ath12k_ce_rx_replenish_retry, 0); 1053 init_completion(&ab->htc_suspend); 1054 1055 ab->dev = dev; 1056 ab->hif.bus = bus; 1057 1058 return ab; 1059 1060 err_free_wq: 1061 destroy_workqueue(ab->workqueue); 1062 err_sc_free: 1063 kfree(ab); 1064 return NULL; 1065 } 1066 1067 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11be wireless LAN cards."); 1068 MODULE_LICENSE("Dual BSD/GPL"); 1069