1 // SPDX-License-Identifier: BSD-3-Clause-Clear 2 /* 3 * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved. 4 * Copyright (c) 2021-2024 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 #include "fw.h" 18 #include "debugfs.h" 19 20 unsigned int ath12k_debug_mask; 21 module_param_named(debug_mask, ath12k_debug_mask, uint, 0644); 22 MODULE_PARM_DESC(debug_mask, "Debugging mask"); 23 24 static int ath12k_core_rfkill_config(struct ath12k_base *ab) 25 { 26 struct ath12k *ar; 27 int ret = 0, i; 28 29 if (!(ab->target_caps.sys_cap_info & WMI_SYS_CAP_INFO_RFKILL)) 30 return 0; 31 32 for (i = 0; i < ab->num_radios; i++) { 33 ar = ab->pdevs[i].ar; 34 35 ret = ath12k_mac_rfkill_config(ar); 36 if (ret && ret != -EOPNOTSUPP) { 37 ath12k_warn(ab, "failed to configure rfkill: %d", ret); 38 return ret; 39 } 40 } 41 42 return ret; 43 } 44 45 int ath12k_core_suspend(struct ath12k_base *ab) 46 { 47 struct ath12k *ar; 48 int ret, i; 49 50 if (!ab->hw_params->supports_suspend) 51 return -EOPNOTSUPP; 52 53 for (i = 0; i < ab->num_radios; i++) { 54 ar = ab->pdevs[i].ar; 55 if (!ar) 56 continue; 57 ret = ath12k_mac_wait_tx_complete(ar); 58 if (ret) { 59 ath12k_warn(ab, "failed to wait tx complete: %d\n", ret); 60 return ret; 61 } 62 } 63 64 /* PM framework skips suspend_late/resume_early callbacks 65 * if other devices report errors in their suspend callbacks. 66 * However ath12k_core_resume() would still be called because 67 * here we return success thus kernel put us on dpm_suspended_list. 68 * Since we won't go through a power down/up cycle, there is 69 * no chance to call complete(&ab->restart_completed) in 70 * ath12k_core_restart(), making ath12k_core_resume() timeout. 71 * So call it here to avoid this issue. This also works in case 72 * no error happens thus suspend_late/resume_early get called, 73 * because it will be reinitialized in ath12k_core_resume_early(). 74 */ 75 complete(&ab->restart_completed); 76 77 return 0; 78 } 79 EXPORT_SYMBOL(ath12k_core_suspend); 80 81 int ath12k_core_suspend_late(struct ath12k_base *ab) 82 { 83 if (!ab->hw_params->supports_suspend) 84 return -EOPNOTSUPP; 85 86 ath12k_hif_irq_disable(ab); 87 ath12k_hif_ce_irq_disable(ab); 88 89 ath12k_hif_power_down(ab, true); 90 91 return 0; 92 } 93 EXPORT_SYMBOL(ath12k_core_suspend_late); 94 95 int ath12k_core_resume_early(struct ath12k_base *ab) 96 { 97 int ret; 98 99 if (!ab->hw_params->supports_suspend) 100 return -EOPNOTSUPP; 101 102 reinit_completion(&ab->restart_completed); 103 ret = ath12k_hif_power_up(ab); 104 if (ret) 105 ath12k_warn(ab, "failed to power up hif during resume: %d\n", ret); 106 107 return ret; 108 } 109 EXPORT_SYMBOL(ath12k_core_resume_early); 110 111 int ath12k_core_resume(struct ath12k_base *ab) 112 { 113 long time_left; 114 115 if (!ab->hw_params->supports_suspend) 116 return -EOPNOTSUPP; 117 118 time_left = wait_for_completion_timeout(&ab->restart_completed, 119 ATH12K_RESET_TIMEOUT_HZ); 120 if (time_left == 0) { 121 ath12k_warn(ab, "timeout while waiting for restart complete"); 122 return -ETIMEDOUT; 123 } 124 125 return 0; 126 } 127 EXPORT_SYMBOL(ath12k_core_resume); 128 129 static int __ath12k_core_create_board_name(struct ath12k_base *ab, char *name, 130 size_t name_len, bool with_variant, 131 bool bus_type_mode) 132 { 133 /* strlen(',variant=') + strlen(ab->qmi.target.bdf_ext) */ 134 char variant[9 + ATH12K_QMI_BDF_EXT_STR_LENGTH] = { 0 }; 135 136 if (with_variant && ab->qmi.target.bdf_ext[0] != '\0') 137 scnprintf(variant, sizeof(variant), ",variant=%s", 138 ab->qmi.target.bdf_ext); 139 140 switch (ab->id.bdf_search) { 141 case ATH12K_BDF_SEARCH_BUS_AND_BOARD: 142 if (bus_type_mode) 143 scnprintf(name, name_len, 144 "bus=%s", 145 ath12k_bus_str(ab->hif.bus)); 146 else 147 scnprintf(name, name_len, 148 "bus=%s,vendor=%04x,device=%04x,subsystem-vendor=%04x,subsystem-device=%04x,qmi-chip-id=%d,qmi-board-id=%d%s", 149 ath12k_bus_str(ab->hif.bus), 150 ab->id.vendor, ab->id.device, 151 ab->id.subsystem_vendor, 152 ab->id.subsystem_device, 153 ab->qmi.target.chip_id, 154 ab->qmi.target.board_id, 155 variant); 156 break; 157 default: 158 scnprintf(name, name_len, 159 "bus=%s,qmi-chip-id=%d,qmi-board-id=%d%s", 160 ath12k_bus_str(ab->hif.bus), 161 ab->qmi.target.chip_id, 162 ab->qmi.target.board_id, variant); 163 break; 164 } 165 166 ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot using board name '%s'\n", name); 167 168 return 0; 169 } 170 171 static int ath12k_core_create_board_name(struct ath12k_base *ab, char *name, 172 size_t name_len) 173 { 174 return __ath12k_core_create_board_name(ab, name, name_len, true, false); 175 } 176 177 static int ath12k_core_create_fallback_board_name(struct ath12k_base *ab, char *name, 178 size_t name_len) 179 { 180 return __ath12k_core_create_board_name(ab, name, name_len, false, false); 181 } 182 183 static int ath12k_core_create_bus_type_board_name(struct ath12k_base *ab, char *name, 184 size_t name_len) 185 { 186 return __ath12k_core_create_board_name(ab, name, name_len, false, true); 187 } 188 189 const struct firmware *ath12k_core_firmware_request(struct ath12k_base *ab, 190 const char *file) 191 { 192 const struct firmware *fw; 193 char path[100]; 194 int ret; 195 196 if (!file) 197 return ERR_PTR(-ENOENT); 198 199 ath12k_core_create_firmware_path(ab, file, path, sizeof(path)); 200 201 ret = firmware_request_nowarn(&fw, path, ab->dev); 202 if (ret) 203 return ERR_PTR(ret); 204 205 ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot firmware request %s size %zu\n", 206 path, fw->size); 207 208 return fw; 209 } 210 211 void ath12k_core_free_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd) 212 { 213 if (!IS_ERR(bd->fw)) 214 release_firmware(bd->fw); 215 216 memset(bd, 0, sizeof(*bd)); 217 } 218 219 static int ath12k_core_parse_bd_ie_board(struct ath12k_base *ab, 220 struct ath12k_board_data *bd, 221 const void *buf, size_t buf_len, 222 const char *boardname, 223 int ie_id, 224 int name_id, 225 int data_id) 226 { 227 const struct ath12k_fw_ie *hdr; 228 bool name_match_found; 229 int ret, board_ie_id; 230 size_t board_ie_len; 231 const void *board_ie_data; 232 233 name_match_found = false; 234 235 /* go through ATH12K_BD_IE_BOARD_/ATH12K_BD_IE_REGDB_ elements */ 236 while (buf_len > sizeof(struct ath12k_fw_ie)) { 237 hdr = buf; 238 board_ie_id = le32_to_cpu(hdr->id); 239 board_ie_len = le32_to_cpu(hdr->len); 240 board_ie_data = hdr->data; 241 242 buf_len -= sizeof(*hdr); 243 buf += sizeof(*hdr); 244 245 if (buf_len < ALIGN(board_ie_len, 4)) { 246 ath12k_err(ab, "invalid %s length: %zu < %zu\n", 247 ath12k_bd_ie_type_str(ie_id), 248 buf_len, ALIGN(board_ie_len, 4)); 249 ret = -EINVAL; 250 goto out; 251 } 252 253 if (board_ie_id == name_id) { 254 ath12k_dbg_dump(ab, ATH12K_DBG_BOOT, "board name", "", 255 board_ie_data, board_ie_len); 256 257 if (board_ie_len != strlen(boardname)) 258 goto next; 259 260 ret = memcmp(board_ie_data, boardname, strlen(boardname)); 261 if (ret) 262 goto next; 263 264 name_match_found = true; 265 ath12k_dbg(ab, ATH12K_DBG_BOOT, 266 "boot found match %s for name '%s'", 267 ath12k_bd_ie_type_str(ie_id), 268 boardname); 269 } else if (board_ie_id == data_id) { 270 if (!name_match_found) 271 /* no match found */ 272 goto next; 273 274 ath12k_dbg(ab, ATH12K_DBG_BOOT, 275 "boot found %s for '%s'", 276 ath12k_bd_ie_type_str(ie_id), 277 boardname); 278 279 bd->data = board_ie_data; 280 bd->len = board_ie_len; 281 282 ret = 0; 283 goto out; 284 } else { 285 ath12k_warn(ab, "unknown %s id found: %d\n", 286 ath12k_bd_ie_type_str(ie_id), 287 board_ie_id); 288 } 289 next: 290 /* jump over the padding */ 291 board_ie_len = ALIGN(board_ie_len, 4); 292 293 buf_len -= board_ie_len; 294 buf += board_ie_len; 295 } 296 297 /* no match found */ 298 ret = -ENOENT; 299 300 out: 301 return ret; 302 } 303 304 static int ath12k_core_fetch_board_data_api_n(struct ath12k_base *ab, 305 struct ath12k_board_data *bd, 306 const char *boardname, 307 int ie_id_match, 308 int name_id, 309 int data_id) 310 { 311 size_t len, magic_len; 312 const u8 *data; 313 char *filename, filepath[100]; 314 size_t ie_len; 315 struct ath12k_fw_ie *hdr; 316 int ret, ie_id; 317 318 filename = ATH12K_BOARD_API2_FILE; 319 320 if (!bd->fw) 321 bd->fw = ath12k_core_firmware_request(ab, filename); 322 323 if (IS_ERR(bd->fw)) 324 return PTR_ERR(bd->fw); 325 326 data = bd->fw->data; 327 len = bd->fw->size; 328 329 ath12k_core_create_firmware_path(ab, filename, 330 filepath, sizeof(filepath)); 331 332 /* magic has extra null byte padded */ 333 magic_len = strlen(ATH12K_BOARD_MAGIC) + 1; 334 if (len < magic_len) { 335 ath12k_err(ab, "failed to find magic value in %s, file too short: %zu\n", 336 filepath, len); 337 ret = -EINVAL; 338 goto err; 339 } 340 341 if (memcmp(data, ATH12K_BOARD_MAGIC, magic_len)) { 342 ath12k_err(ab, "found invalid board magic\n"); 343 ret = -EINVAL; 344 goto err; 345 } 346 347 /* magic is padded to 4 bytes */ 348 magic_len = ALIGN(magic_len, 4); 349 if (len < magic_len) { 350 ath12k_err(ab, "failed: %s too small to contain board data, len: %zu\n", 351 filepath, len); 352 ret = -EINVAL; 353 goto err; 354 } 355 356 data += magic_len; 357 len -= magic_len; 358 359 while (len > sizeof(struct ath12k_fw_ie)) { 360 hdr = (struct ath12k_fw_ie *)data; 361 ie_id = le32_to_cpu(hdr->id); 362 ie_len = le32_to_cpu(hdr->len); 363 364 len -= sizeof(*hdr); 365 data = hdr->data; 366 367 if (len < ALIGN(ie_len, 4)) { 368 ath12k_err(ab, "invalid length for board ie_id %d ie_len %zu len %zu\n", 369 ie_id, ie_len, len); 370 ret = -EINVAL; 371 goto err; 372 } 373 374 if (ie_id == ie_id_match) { 375 ret = ath12k_core_parse_bd_ie_board(ab, bd, data, 376 ie_len, 377 boardname, 378 ie_id_match, 379 name_id, 380 data_id); 381 if (ret == -ENOENT) 382 /* no match found, continue */ 383 goto next; 384 else if (ret) 385 /* there was an error, bail out */ 386 goto err; 387 /* either found or error, so stop searching */ 388 goto out; 389 } 390 next: 391 /* jump over the padding */ 392 ie_len = ALIGN(ie_len, 4); 393 394 len -= ie_len; 395 data += ie_len; 396 } 397 398 out: 399 if (!bd->data || !bd->len) { 400 ath12k_dbg(ab, ATH12K_DBG_BOOT, 401 "failed to fetch %s for %s from %s\n", 402 ath12k_bd_ie_type_str(ie_id_match), 403 boardname, filepath); 404 ret = -ENODATA; 405 goto err; 406 } 407 408 return 0; 409 410 err: 411 ath12k_core_free_bdf(ab, bd); 412 return ret; 413 } 414 415 int ath12k_core_fetch_board_data_api_1(struct ath12k_base *ab, 416 struct ath12k_board_data *bd, 417 char *filename) 418 { 419 bd->fw = ath12k_core_firmware_request(ab, filename); 420 if (IS_ERR(bd->fw)) 421 return PTR_ERR(bd->fw); 422 423 bd->data = bd->fw->data; 424 bd->len = bd->fw->size; 425 426 return 0; 427 } 428 429 #define BOARD_NAME_SIZE 200 430 int ath12k_core_fetch_bdf(struct ath12k_base *ab, struct ath12k_board_data *bd) 431 { 432 char boardname[BOARD_NAME_SIZE], fallback_boardname[BOARD_NAME_SIZE]; 433 char *filename, filepath[100]; 434 int bd_api; 435 int ret; 436 437 filename = ATH12K_BOARD_API2_FILE; 438 439 ret = ath12k_core_create_board_name(ab, boardname, sizeof(boardname)); 440 if (ret) { 441 ath12k_err(ab, "failed to create board name: %d", ret); 442 return ret; 443 } 444 445 bd_api = 2; 446 ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname, 447 ATH12K_BD_IE_BOARD, 448 ATH12K_BD_IE_BOARD_NAME, 449 ATH12K_BD_IE_BOARD_DATA); 450 if (!ret) 451 goto success; 452 453 ret = ath12k_core_create_fallback_board_name(ab, fallback_boardname, 454 sizeof(fallback_boardname)); 455 if (ret) { 456 ath12k_err(ab, "failed to create fallback board name: %d", ret); 457 return ret; 458 } 459 460 ret = ath12k_core_fetch_board_data_api_n(ab, bd, fallback_boardname, 461 ATH12K_BD_IE_BOARD, 462 ATH12K_BD_IE_BOARD_NAME, 463 ATH12K_BD_IE_BOARD_DATA); 464 if (!ret) 465 goto success; 466 467 bd_api = 1; 468 ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_DEFAULT_BOARD_FILE); 469 if (ret) { 470 ath12k_core_create_firmware_path(ab, filename, 471 filepath, sizeof(filepath)); 472 ath12k_err(ab, "failed to fetch board data for %s from %s\n", 473 boardname, filepath); 474 if (memcmp(boardname, fallback_boardname, strlen(boardname))) 475 ath12k_err(ab, "failed to fetch board data for %s from %s\n", 476 fallback_boardname, filepath); 477 478 ath12k_err(ab, "failed to fetch board.bin from %s\n", 479 ab->hw_params->fw.dir); 480 return ret; 481 } 482 483 success: 484 ath12k_dbg(ab, ATH12K_DBG_BOOT, "using board api %d\n", bd_api); 485 return 0; 486 } 487 488 int ath12k_core_fetch_regdb(struct ath12k_base *ab, struct ath12k_board_data *bd) 489 { 490 char boardname[BOARD_NAME_SIZE], default_boardname[BOARD_NAME_SIZE]; 491 int ret; 492 493 ret = ath12k_core_create_board_name(ab, boardname, BOARD_NAME_SIZE); 494 if (ret) { 495 ath12k_dbg(ab, ATH12K_DBG_BOOT, 496 "failed to create board name for regdb: %d", ret); 497 goto exit; 498 } 499 500 ret = ath12k_core_fetch_board_data_api_n(ab, bd, boardname, 501 ATH12K_BD_IE_REGDB, 502 ATH12K_BD_IE_REGDB_NAME, 503 ATH12K_BD_IE_REGDB_DATA); 504 if (!ret) 505 goto exit; 506 507 ret = ath12k_core_create_bus_type_board_name(ab, default_boardname, 508 BOARD_NAME_SIZE); 509 if (ret) { 510 ath12k_dbg(ab, ATH12K_DBG_BOOT, 511 "failed to create default board name for regdb: %d", ret); 512 goto exit; 513 } 514 515 ret = ath12k_core_fetch_board_data_api_n(ab, bd, default_boardname, 516 ATH12K_BD_IE_REGDB, 517 ATH12K_BD_IE_REGDB_NAME, 518 ATH12K_BD_IE_REGDB_DATA); 519 if (!ret) 520 goto exit; 521 522 ret = ath12k_core_fetch_board_data_api_1(ab, bd, ATH12K_REGDB_FILE_NAME); 523 if (ret) 524 ath12k_dbg(ab, ATH12K_DBG_BOOT, "failed to fetch %s from %s\n", 525 ATH12K_REGDB_FILE_NAME, ab->hw_params->fw.dir); 526 527 exit: 528 if (!ret) 529 ath12k_dbg(ab, ATH12K_DBG_BOOT, "fetched regdb\n"); 530 531 return ret; 532 } 533 534 u32 ath12k_core_get_max_station_per_radio(struct ath12k_base *ab) 535 { 536 if (ab->num_radios == 2) 537 return TARGET_NUM_STATIONS_DBS; 538 else if (ab->num_radios == 3) 539 return TARGET_NUM_PEERS_PDEV_DBS_SBS; 540 return TARGET_NUM_STATIONS_SINGLE; 541 } 542 543 u32 ath12k_core_get_max_peers_per_radio(struct ath12k_base *ab) 544 { 545 if (ab->num_radios == 2) 546 return TARGET_NUM_PEERS_PDEV_DBS; 547 else if (ab->num_radios == 3) 548 return TARGET_NUM_PEERS_PDEV_DBS_SBS; 549 return TARGET_NUM_PEERS_PDEV_SINGLE; 550 } 551 552 u32 ath12k_core_get_max_num_tids(struct ath12k_base *ab) 553 { 554 if (ab->num_radios == 2) 555 return TARGET_NUM_TIDS(DBS); 556 else if (ab->num_radios == 3) 557 return TARGET_NUM_TIDS(DBS_SBS); 558 return TARGET_NUM_TIDS(SINGLE); 559 } 560 561 static void ath12k_core_stop(struct ath12k_base *ab) 562 { 563 if (!test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags)) 564 ath12k_qmi_firmware_stop(ab); 565 566 ath12k_acpi_stop(ab); 567 568 ath12k_hif_stop(ab); 569 ath12k_wmi_detach(ab); 570 ath12k_dp_rx_pdev_reo_cleanup(ab); 571 572 /* De-Init of components as needed */ 573 } 574 575 static void ath12k_core_check_bdfext(const struct dmi_header *hdr, void *data) 576 { 577 struct ath12k_base *ab = data; 578 const char *magic = ATH12K_SMBIOS_BDF_EXT_MAGIC; 579 struct ath12k_smbios_bdf *smbios = (struct ath12k_smbios_bdf *)hdr; 580 ssize_t copied; 581 size_t len; 582 int i; 583 584 if (ab->qmi.target.bdf_ext[0] != '\0') 585 return; 586 587 if (hdr->type != ATH12K_SMBIOS_BDF_EXT_TYPE) 588 return; 589 590 if (hdr->length != ATH12K_SMBIOS_BDF_EXT_LENGTH) { 591 ath12k_dbg(ab, ATH12K_DBG_BOOT, 592 "wrong smbios bdf ext type length (%d).\n", 593 hdr->length); 594 return; 595 } 596 597 if (!smbios->bdf_enabled) { 598 ath12k_dbg(ab, ATH12K_DBG_BOOT, "bdf variant name not found.\n"); 599 return; 600 } 601 602 /* Only one string exists (per spec) */ 603 if (memcmp(smbios->bdf_ext, magic, strlen(magic)) != 0) { 604 ath12k_dbg(ab, ATH12K_DBG_BOOT, 605 "bdf variant magic does not match.\n"); 606 return; 607 } 608 609 len = min_t(size_t, 610 strlen(smbios->bdf_ext), sizeof(ab->qmi.target.bdf_ext)); 611 for (i = 0; i < len; i++) { 612 if (!isascii(smbios->bdf_ext[i]) || !isprint(smbios->bdf_ext[i])) { 613 ath12k_dbg(ab, ATH12K_DBG_BOOT, 614 "bdf variant name contains non ascii chars.\n"); 615 return; 616 } 617 } 618 619 /* Copy extension name without magic prefix */ 620 copied = strscpy(ab->qmi.target.bdf_ext, smbios->bdf_ext + strlen(magic), 621 sizeof(ab->qmi.target.bdf_ext)); 622 if (copied < 0) { 623 ath12k_dbg(ab, ATH12K_DBG_BOOT, 624 "bdf variant string is longer than the buffer can accommodate\n"); 625 return; 626 } 627 628 ath12k_dbg(ab, ATH12K_DBG_BOOT, 629 "found and validated bdf variant smbios_type 0x%x bdf %s\n", 630 ATH12K_SMBIOS_BDF_EXT_TYPE, ab->qmi.target.bdf_ext); 631 } 632 633 int ath12k_core_check_smbios(struct ath12k_base *ab) 634 { 635 ab->qmi.target.bdf_ext[0] = '\0'; 636 dmi_walk(ath12k_core_check_bdfext, ab); 637 638 if (ab->qmi.target.bdf_ext[0] == '\0') 639 return -ENODATA; 640 641 return 0; 642 } 643 644 static int ath12k_core_soc_create(struct ath12k_base *ab) 645 { 646 int ret; 647 648 ret = ath12k_qmi_init_service(ab); 649 if (ret) { 650 ath12k_err(ab, "failed to initialize qmi :%d\n", ret); 651 return ret; 652 } 653 654 ath12k_debugfs_soc_create(ab); 655 656 ret = ath12k_hif_power_up(ab); 657 if (ret) { 658 ath12k_err(ab, "failed to power up :%d\n", ret); 659 goto err_qmi_deinit; 660 } 661 662 return 0; 663 664 err_qmi_deinit: 665 ath12k_debugfs_soc_destroy(ab); 666 ath12k_qmi_deinit_service(ab); 667 return ret; 668 } 669 670 static void ath12k_core_soc_destroy(struct ath12k_base *ab) 671 { 672 ath12k_dp_free(ab); 673 ath12k_reg_free(ab); 674 ath12k_debugfs_soc_destroy(ab); 675 ath12k_qmi_deinit_service(ab); 676 } 677 678 static int ath12k_core_pdev_create(struct ath12k_base *ab) 679 { 680 int ret; 681 682 ret = ath12k_mac_register(ab); 683 if (ret) { 684 ath12k_err(ab, "failed register the radio with mac80211: %d\n", ret); 685 return ret; 686 } 687 688 ret = ath12k_dp_pdev_alloc(ab); 689 if (ret) { 690 ath12k_err(ab, "failed to attach DP pdev: %d\n", ret); 691 goto err_mac_unregister; 692 } 693 694 return 0; 695 696 err_mac_unregister: 697 ath12k_mac_unregister(ab); 698 699 return ret; 700 } 701 702 static void ath12k_core_pdev_destroy(struct ath12k_base *ab) 703 { 704 ath12k_mac_unregister(ab); 705 ath12k_hif_irq_disable(ab); 706 ath12k_dp_pdev_free(ab); 707 } 708 709 static int ath12k_core_start(struct ath12k_base *ab, 710 enum ath12k_firmware_mode mode) 711 { 712 int ret; 713 714 ret = ath12k_wmi_attach(ab); 715 if (ret) { 716 ath12k_err(ab, "failed to attach wmi: %d\n", ret); 717 return ret; 718 } 719 720 ret = ath12k_htc_init(ab); 721 if (ret) { 722 ath12k_err(ab, "failed to init htc: %d\n", ret); 723 goto err_wmi_detach; 724 } 725 726 ret = ath12k_hif_start(ab); 727 if (ret) { 728 ath12k_err(ab, "failed to start HIF: %d\n", ret); 729 goto err_wmi_detach; 730 } 731 732 ret = ath12k_htc_wait_target(&ab->htc); 733 if (ret) { 734 ath12k_err(ab, "failed to connect to HTC: %d\n", ret); 735 goto err_hif_stop; 736 } 737 738 ret = ath12k_dp_htt_connect(&ab->dp); 739 if (ret) { 740 ath12k_err(ab, "failed to connect to HTT: %d\n", ret); 741 goto err_hif_stop; 742 } 743 744 ret = ath12k_wmi_connect(ab); 745 if (ret) { 746 ath12k_err(ab, "failed to connect wmi: %d\n", ret); 747 goto err_hif_stop; 748 } 749 750 ret = ath12k_htc_start(&ab->htc); 751 if (ret) { 752 ath12k_err(ab, "failed to start HTC: %d\n", ret); 753 goto err_hif_stop; 754 } 755 756 ret = ath12k_wmi_wait_for_service_ready(ab); 757 if (ret) { 758 ath12k_err(ab, "failed to receive wmi service ready event: %d\n", 759 ret); 760 goto err_hif_stop; 761 } 762 763 ret = ath12k_mac_allocate(ab); 764 if (ret) { 765 ath12k_err(ab, "failed to create new hw device with mac80211 :%d\n", 766 ret); 767 goto err_hif_stop; 768 } 769 770 ath12k_dp_cc_config(ab); 771 772 ret = ath12k_dp_rx_pdev_reo_setup(ab); 773 if (ret) { 774 ath12k_err(ab, "failed to initialize reo destination rings: %d\n", ret); 775 goto err_mac_destroy; 776 } 777 778 ath12k_dp_hal_rx_desc_init(ab); 779 780 ret = ath12k_wmi_cmd_init(ab); 781 if (ret) { 782 ath12k_err(ab, "failed to send wmi init cmd: %d\n", ret); 783 goto err_reo_cleanup; 784 } 785 786 ret = ath12k_wmi_wait_for_unified_ready(ab); 787 if (ret) { 788 ath12k_err(ab, "failed to receive wmi unified ready event: %d\n", 789 ret); 790 goto err_reo_cleanup; 791 } 792 793 /* put hardware to DBS mode */ 794 if (ab->hw_params->single_pdev_only) { 795 ret = ath12k_wmi_set_hw_mode(ab, WMI_HOST_HW_MODE_DBS); 796 if (ret) { 797 ath12k_err(ab, "failed to send dbs mode: %d\n", ret); 798 goto err_reo_cleanup; 799 } 800 } 801 802 ret = ath12k_dp_tx_htt_h2t_ver_req_msg(ab); 803 if (ret) { 804 ath12k_err(ab, "failed to send htt version request message: %d\n", 805 ret); 806 goto err_reo_cleanup; 807 } 808 809 ret = ath12k_acpi_start(ab); 810 if (ret) 811 /* ACPI is optional so continue in case of an error */ 812 ath12k_dbg(ab, ATH12K_DBG_BOOT, "acpi failed: %d\n", ret); 813 814 return 0; 815 816 err_reo_cleanup: 817 ath12k_dp_rx_pdev_reo_cleanup(ab); 818 err_mac_destroy: 819 ath12k_mac_destroy(ab); 820 err_hif_stop: 821 ath12k_hif_stop(ab); 822 err_wmi_detach: 823 ath12k_wmi_detach(ab); 824 return ret; 825 } 826 827 static int ath12k_core_start_firmware(struct ath12k_base *ab, 828 enum ath12k_firmware_mode mode) 829 { 830 int ret; 831 832 ath12k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v3, 833 &ab->qmi.ce_cfg.shadow_reg_v3_len); 834 835 ret = ath12k_qmi_firmware_start(ab, mode); 836 if (ret) { 837 ath12k_err(ab, "failed to send firmware start: %d\n", ret); 838 return ret; 839 } 840 841 return ret; 842 } 843 844 int ath12k_core_qmi_firmware_ready(struct ath12k_base *ab) 845 { 846 int ret; 847 848 ret = ath12k_core_start_firmware(ab, ATH12K_FIRMWARE_MODE_NORMAL); 849 if (ret) { 850 ath12k_err(ab, "failed to start firmware: %d\n", ret); 851 return ret; 852 } 853 854 ret = ath12k_ce_init_pipes(ab); 855 if (ret) { 856 ath12k_err(ab, "failed to initialize CE: %d\n", ret); 857 goto err_firmware_stop; 858 } 859 860 ret = ath12k_dp_alloc(ab); 861 if (ret) { 862 ath12k_err(ab, "failed to init DP: %d\n", ret); 863 goto err_firmware_stop; 864 } 865 866 mutex_lock(&ab->core_lock); 867 ret = ath12k_core_start(ab, ATH12K_FIRMWARE_MODE_NORMAL); 868 if (ret) { 869 ath12k_err(ab, "failed to start core: %d\n", ret); 870 goto err_dp_free; 871 } 872 873 ret = ath12k_core_pdev_create(ab); 874 if (ret) { 875 ath12k_err(ab, "failed to create pdev core: %d\n", ret); 876 goto err_core_stop; 877 } 878 ath12k_hif_irq_enable(ab); 879 880 ret = ath12k_core_rfkill_config(ab); 881 if (ret && ret != -EOPNOTSUPP) { 882 ath12k_err(ab, "failed to config rfkill: %d\n", ret); 883 goto err_core_pdev_destroy; 884 } 885 886 mutex_unlock(&ab->core_lock); 887 888 return 0; 889 890 err_core_pdev_destroy: 891 ath12k_core_pdev_destroy(ab); 892 err_core_stop: 893 ath12k_core_stop(ab); 894 ath12k_mac_destroy(ab); 895 err_dp_free: 896 ath12k_dp_free(ab); 897 mutex_unlock(&ab->core_lock); 898 err_firmware_stop: 899 ath12k_qmi_firmware_stop(ab); 900 901 return ret; 902 } 903 904 static int ath12k_core_reconfigure_on_crash(struct ath12k_base *ab) 905 { 906 int ret; 907 908 mutex_lock(&ab->core_lock); 909 ath12k_dp_pdev_free(ab); 910 ath12k_ce_cleanup_pipes(ab); 911 ath12k_wmi_detach(ab); 912 ath12k_dp_rx_pdev_reo_cleanup(ab); 913 mutex_unlock(&ab->core_lock); 914 915 ath12k_dp_free(ab); 916 ath12k_hal_srng_deinit(ab); 917 918 ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1; 919 920 ret = ath12k_hal_srng_init(ab); 921 if (ret) 922 return ret; 923 924 clear_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags); 925 926 ret = ath12k_core_qmi_firmware_ready(ab); 927 if (ret) 928 goto err_hal_srng_deinit; 929 930 clear_bit(ATH12K_FLAG_RECOVERY, &ab->dev_flags); 931 932 return 0; 933 934 err_hal_srng_deinit: 935 ath12k_hal_srng_deinit(ab); 936 return ret; 937 } 938 939 static void ath12k_rfkill_work(struct work_struct *work) 940 { 941 struct ath12k_base *ab = container_of(work, struct ath12k_base, rfkill_work); 942 struct ath12k *ar; 943 struct ath12k_hw *ah; 944 struct ieee80211_hw *hw; 945 bool rfkill_radio_on; 946 int i, j; 947 948 spin_lock_bh(&ab->base_lock); 949 rfkill_radio_on = ab->rfkill_radio_on; 950 spin_unlock_bh(&ab->base_lock); 951 952 for (i = 0; i < ab->num_hw; i++) { 953 ah = ab->ah[i]; 954 if (!ah) 955 continue; 956 957 for (j = 0; j < ah->num_radio; j++) { 958 ar = &ah->radio[j]; 959 if (!ar) 960 continue; 961 962 ath12k_mac_rfkill_enable_radio(ar, rfkill_radio_on); 963 } 964 965 hw = ah->hw; 966 wiphy_rfkill_set_hw_state(hw->wiphy, !rfkill_radio_on); 967 } 968 } 969 970 void ath12k_core_halt(struct ath12k *ar) 971 { 972 struct ath12k_base *ab = ar->ab; 973 974 lockdep_assert_held(&ar->conf_mutex); 975 976 ar->num_created_vdevs = 0; 977 ar->allocated_vdev_map = 0; 978 979 ath12k_mac_scan_finish(ar); 980 ath12k_mac_peer_cleanup_all(ar); 981 cancel_delayed_work_sync(&ar->scan.timeout); 982 cancel_work_sync(&ar->regd_update_work); 983 cancel_work_sync(&ab->rfkill_work); 984 985 rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx], NULL); 986 synchronize_rcu(); 987 INIT_LIST_HEAD(&ar->arvifs); 988 idr_init(&ar->txmgmt_idr); 989 } 990 991 static void ath12k_core_pre_reconfigure_recovery(struct ath12k_base *ab) 992 { 993 struct ath12k *ar; 994 struct ath12k_hw *ah; 995 int i, j; 996 997 spin_lock_bh(&ab->base_lock); 998 ab->stats.fw_crash_counter++; 999 spin_unlock_bh(&ab->base_lock); 1000 1001 if (ab->is_reset) 1002 set_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags); 1003 1004 for (i = 0; i < ab->num_hw; i++) { 1005 ah = ab->ah[i]; 1006 if (!ah || ah->state == ATH12K_HW_STATE_OFF) 1007 continue; 1008 1009 ieee80211_stop_queues(ah->hw); 1010 1011 for (j = 0; j < ah->num_radio; j++) { 1012 ar = &ah->radio[j]; 1013 1014 ath12k_mac_drain_tx(ar); 1015 complete(&ar->scan.started); 1016 complete(&ar->scan.completed); 1017 complete(&ar->scan.on_channel); 1018 complete(&ar->peer_assoc_done); 1019 complete(&ar->peer_delete_done); 1020 complete(&ar->install_key_done); 1021 complete(&ar->vdev_setup_done); 1022 complete(&ar->vdev_delete_done); 1023 complete(&ar->bss_survey_done); 1024 1025 wake_up(&ar->dp.tx_empty_waitq); 1026 idr_for_each(&ar->txmgmt_idr, 1027 ath12k_mac_tx_mgmt_pending_free, ar); 1028 idr_destroy(&ar->txmgmt_idr); 1029 wake_up(&ar->txmgmt_empty_waitq); 1030 } 1031 } 1032 1033 wake_up(&ab->wmi_ab.tx_credits_wq); 1034 wake_up(&ab->peer_mapping_wq); 1035 } 1036 1037 static void ath12k_core_post_reconfigure_recovery(struct ath12k_base *ab) 1038 { 1039 struct ath12k_hw *ah; 1040 struct ath12k *ar; 1041 int i, j; 1042 1043 for (i = 0; i < ab->num_hw; i++) { 1044 ah = ab->ah[i]; 1045 if (!ah || ah->state == ATH12K_HW_STATE_OFF) 1046 continue; 1047 1048 mutex_lock(&ah->hw_mutex); 1049 1050 switch (ah->state) { 1051 case ATH12K_HW_STATE_ON: 1052 ah->state = ATH12K_HW_STATE_RESTARTING; 1053 1054 for (j = 0; j < ah->num_radio; j++) { 1055 ar = &ah->radio[j]; 1056 1057 mutex_lock(&ar->conf_mutex); 1058 ath12k_core_halt(ar); 1059 mutex_unlock(&ar->conf_mutex); 1060 } 1061 1062 /* Restart after all the link/radio halt */ 1063 ieee80211_restart_hw(ah->hw); 1064 break; 1065 case ATH12K_HW_STATE_OFF: 1066 ath12k_warn(ab, 1067 "cannot restart hw %d that hasn't been started\n", 1068 i); 1069 break; 1070 case ATH12K_HW_STATE_RESTARTING: 1071 break; 1072 case ATH12K_HW_STATE_RESTARTED: 1073 ah->state = ATH12K_HW_STATE_WEDGED; 1074 fallthrough; 1075 case ATH12K_HW_STATE_WEDGED: 1076 ath12k_warn(ab, 1077 "device is wedged, will not restart hw %d\n", i); 1078 break; 1079 } 1080 1081 mutex_unlock(&ah->hw_mutex); 1082 } 1083 1084 complete(&ab->driver_recovery); 1085 } 1086 1087 static void ath12k_core_restart(struct work_struct *work) 1088 { 1089 struct ath12k_base *ab = container_of(work, struct ath12k_base, restart_work); 1090 int ret; 1091 1092 ret = ath12k_core_reconfigure_on_crash(ab); 1093 if (ret) { 1094 ath12k_err(ab, "failed to reconfigure driver on crash recovery\n"); 1095 return; 1096 } 1097 1098 if (ab->is_reset) 1099 complete_all(&ab->reconfigure_complete); 1100 1101 complete(&ab->restart_completed); 1102 } 1103 1104 static void ath12k_core_reset(struct work_struct *work) 1105 { 1106 struct ath12k_base *ab = container_of(work, struct ath12k_base, reset_work); 1107 int reset_count, fail_cont_count; 1108 long time_left; 1109 1110 if (!(test_bit(ATH12K_FLAG_REGISTERED, &ab->dev_flags))) { 1111 ath12k_warn(ab, "ignore reset dev flags 0x%lx\n", ab->dev_flags); 1112 return; 1113 } 1114 1115 /* Sometimes the recovery will fail and then the next all recovery fail, 1116 * this is to avoid infinite recovery since it can not recovery success 1117 */ 1118 fail_cont_count = atomic_read(&ab->fail_cont_count); 1119 1120 if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FINAL) 1121 return; 1122 1123 if (fail_cont_count >= ATH12K_RESET_MAX_FAIL_COUNT_FIRST && 1124 time_before(jiffies, ab->reset_fail_timeout)) 1125 return; 1126 1127 reset_count = atomic_inc_return(&ab->reset_count); 1128 1129 if (reset_count > 1) { 1130 /* Sometimes it happened another reset worker before the previous one 1131 * completed, then the second reset worker will destroy the previous one, 1132 * thus below is to avoid that. 1133 */ 1134 ath12k_warn(ab, "already resetting count %d\n", reset_count); 1135 1136 reinit_completion(&ab->reset_complete); 1137 time_left = wait_for_completion_timeout(&ab->reset_complete, 1138 ATH12K_RESET_TIMEOUT_HZ); 1139 if (time_left) { 1140 ath12k_dbg(ab, ATH12K_DBG_BOOT, "to skip reset\n"); 1141 atomic_dec(&ab->reset_count); 1142 return; 1143 } 1144 1145 ab->reset_fail_timeout = jiffies + ATH12K_RESET_FAIL_TIMEOUT_HZ; 1146 /* Record the continuous recovery fail count when recovery failed*/ 1147 fail_cont_count = atomic_inc_return(&ab->fail_cont_count); 1148 } 1149 1150 ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset starting\n"); 1151 1152 ab->is_reset = true; 1153 atomic_set(&ab->recovery_start_count, 0); 1154 reinit_completion(&ab->recovery_start); 1155 atomic_set(&ab->recovery_count, 0); 1156 1157 ath12k_core_pre_reconfigure_recovery(ab); 1158 1159 reinit_completion(&ab->reconfigure_complete); 1160 ath12k_core_post_reconfigure_recovery(ab); 1161 1162 ath12k_dbg(ab, ATH12K_DBG_BOOT, "waiting recovery start...\n"); 1163 1164 time_left = wait_for_completion_timeout(&ab->recovery_start, 1165 ATH12K_RECOVER_START_TIMEOUT_HZ); 1166 1167 ath12k_hif_irq_disable(ab); 1168 ath12k_hif_ce_irq_disable(ab); 1169 1170 ath12k_hif_power_down(ab, false); 1171 ath12k_hif_power_up(ab); 1172 1173 ath12k_dbg(ab, ATH12K_DBG_BOOT, "reset started\n"); 1174 } 1175 1176 int ath12k_core_pre_init(struct ath12k_base *ab) 1177 { 1178 int ret; 1179 1180 ret = ath12k_hw_init(ab); 1181 if (ret) { 1182 ath12k_err(ab, "failed to init hw params: %d\n", ret); 1183 return ret; 1184 } 1185 1186 ath12k_fw_map(ab); 1187 1188 return 0; 1189 } 1190 1191 static int ath12k_core_panic_handler(struct notifier_block *nb, 1192 unsigned long action, void *data) 1193 { 1194 struct ath12k_base *ab = container_of(nb, struct ath12k_base, 1195 panic_nb); 1196 1197 return ath12k_hif_panic_handler(ab); 1198 } 1199 1200 static int ath12k_core_panic_notifier_register(struct ath12k_base *ab) 1201 { 1202 ab->panic_nb.notifier_call = ath12k_core_panic_handler; 1203 1204 return atomic_notifier_chain_register(&panic_notifier_list, 1205 &ab->panic_nb); 1206 } 1207 1208 static void ath12k_core_panic_notifier_unregister(struct ath12k_base *ab) 1209 { 1210 atomic_notifier_chain_unregister(&panic_notifier_list, 1211 &ab->panic_nb); 1212 } 1213 1214 int ath12k_core_init(struct ath12k_base *ab) 1215 { 1216 int ret; 1217 1218 ret = ath12k_core_soc_create(ab); 1219 if (ret) { 1220 ath12k_err(ab, "failed to create soc core: %d\n", ret); 1221 return ret; 1222 } 1223 1224 ret = ath12k_core_panic_notifier_register(ab); 1225 if (ret) 1226 ath12k_warn(ab, "failed to register panic handler: %d\n", ret); 1227 1228 return 0; 1229 } 1230 1231 void ath12k_core_deinit(struct ath12k_base *ab) 1232 { 1233 ath12k_core_panic_notifier_unregister(ab); 1234 1235 mutex_lock(&ab->core_lock); 1236 1237 ath12k_core_pdev_destroy(ab); 1238 ath12k_core_stop(ab); 1239 1240 mutex_unlock(&ab->core_lock); 1241 1242 ath12k_hif_power_down(ab, false); 1243 ath12k_mac_destroy(ab); 1244 ath12k_core_soc_destroy(ab); 1245 ath12k_fw_unmap(ab); 1246 } 1247 1248 void ath12k_core_free(struct ath12k_base *ab) 1249 { 1250 timer_delete_sync(&ab->rx_replenish_retry); 1251 destroy_workqueue(ab->workqueue_aux); 1252 destroy_workqueue(ab->workqueue); 1253 kfree(ab); 1254 } 1255 1256 struct ath12k_base *ath12k_core_alloc(struct device *dev, size_t priv_size, 1257 enum ath12k_bus bus) 1258 { 1259 struct ath12k_base *ab; 1260 1261 ab = kzalloc(sizeof(*ab) + priv_size, GFP_KERNEL); 1262 if (!ab) 1263 return NULL; 1264 1265 init_completion(&ab->driver_recovery); 1266 1267 ab->workqueue = create_singlethread_workqueue("ath12k_wq"); 1268 if (!ab->workqueue) 1269 goto err_sc_free; 1270 1271 ab->workqueue_aux = create_singlethread_workqueue("ath12k_aux_wq"); 1272 if (!ab->workqueue_aux) 1273 goto err_free_wq; 1274 1275 mutex_init(&ab->core_lock); 1276 spin_lock_init(&ab->base_lock); 1277 init_completion(&ab->reset_complete); 1278 init_completion(&ab->reconfigure_complete); 1279 init_completion(&ab->recovery_start); 1280 1281 INIT_LIST_HEAD(&ab->peers); 1282 init_waitqueue_head(&ab->peer_mapping_wq); 1283 init_waitqueue_head(&ab->wmi_ab.tx_credits_wq); 1284 INIT_WORK(&ab->restart_work, ath12k_core_restart); 1285 INIT_WORK(&ab->reset_work, ath12k_core_reset); 1286 INIT_WORK(&ab->rfkill_work, ath12k_rfkill_work); 1287 1288 timer_setup(&ab->rx_replenish_retry, ath12k_ce_rx_replenish_retry, 0); 1289 init_completion(&ab->htc_suspend); 1290 init_completion(&ab->restart_completed); 1291 1292 ab->dev = dev; 1293 ab->hif.bus = bus; 1294 ab->qmi.num_radios = U8_MAX; 1295 ab->mlo_capable_flags = ATH12K_INTRA_DEVICE_MLO_SUPPORT; 1296 1297 /* Device index used to identify the devices in a group. 1298 * 1299 * In Intra-device MLO, only one device present in a group, 1300 * so it is always zero. 1301 * 1302 * In Inter-device MLO, Multiple device present in a group, 1303 * expect non-zero value. 1304 */ 1305 ab->device_id = 0; 1306 1307 return ab; 1308 1309 err_free_wq: 1310 destroy_workqueue(ab->workqueue); 1311 err_sc_free: 1312 kfree(ab); 1313 return NULL; 1314 } 1315 1316 MODULE_DESCRIPTION("Core module for Qualcomm Atheros 802.11be wireless LAN cards."); 1317 MODULE_LICENSE("Dual BSD/GPL"); 1318