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