1 // SPDX-License-Identifier: ISC 2 /* 3 * Copyright (c) 2005-2011 Atheros Communications Inc. 4 * Copyright (c) 2011-2017 Qualcomm Atheros, Inc. 5 * Copyright (c) 2018, The Linux Foundation. All rights reserved. 6 */ 7 8 #include <linux/module.h> 9 #include <linux/debugfs.h> 10 #include <linux/vmalloc.h> 11 #include <linux/crc32.h> 12 #include <linux/firmware.h> 13 14 #include "core.h" 15 #include "debug.h" 16 #include "hif.h" 17 #include "wmi-ops.h" 18 19 /* ms */ 20 #define ATH10K_DEBUG_HTT_STATS_INTERVAL 1000 21 22 #define ATH10K_DEBUG_CAL_DATA_LEN 12064 23 24 void ath10k_info(struct ath10k *ar, const char *fmt, ...) 25 { 26 struct va_format vaf = { 27 .fmt = fmt, 28 }; 29 va_list args; 30 31 va_start(args, fmt); 32 vaf.va = &args; 33 dev_info(ar->dev, "%pV", &vaf); 34 trace_ath10k_log_info(ar, &vaf); 35 va_end(args); 36 } 37 EXPORT_SYMBOL(ath10k_info); 38 39 void ath10k_debug_print_hwfw_info(struct ath10k *ar) 40 { 41 const struct firmware *firmware; 42 char fw_features[128] = {}; 43 u32 crc = 0; 44 45 ath10k_core_get_fw_features_str(ar, fw_features, sizeof(fw_features)); 46 47 ath10k_info(ar, "%s target 0x%08x chip_id 0x%08x sub %04x:%04x", 48 ar->hw_params.name, 49 ar->target_version, 50 ar->bus_param.chip_id, 51 ar->id.subsystem_vendor, ar->id.subsystem_device); 52 53 ath10k_info(ar, "kconfig debug %d debugfs %d tracing %d dfs %d testmode %d\n", 54 IS_ENABLED(CONFIG_ATH10K_DEBUG), 55 IS_ENABLED(CONFIG_ATH10K_DEBUGFS), 56 IS_ENABLED(CONFIG_ATH10K_TRACING), 57 IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED), 58 IS_ENABLED(CONFIG_NL80211_TESTMODE)); 59 60 firmware = ar->normal_mode_fw.fw_file.firmware; 61 if (firmware) 62 crc = crc32_le(0, firmware->data, firmware->size); 63 64 ath10k_info(ar, "firmware ver %s api %d features %s crc32 %08x\n", 65 ar->hw->wiphy->fw_version, 66 ar->fw_api, 67 fw_features, 68 crc); 69 } 70 71 void ath10k_debug_print_board_info(struct ath10k *ar) 72 { 73 char boardinfo[100]; 74 const struct firmware *board; 75 u32 crc; 76 77 if (ar->id.bmi_ids_valid) 78 scnprintf(boardinfo, sizeof(boardinfo), "%d:%d", 79 ar->id.bmi_chip_id, ar->id.bmi_board_id); 80 else 81 scnprintf(boardinfo, sizeof(boardinfo), "N/A"); 82 83 board = ar->normal_mode_fw.board; 84 if (!IS_ERR_OR_NULL(board)) 85 crc = crc32_le(0, board->data, board->size); 86 else 87 crc = 0; 88 89 ath10k_info(ar, "board_file api %d bmi_id %s crc32 %08x", 90 ar->bd_api, 91 boardinfo, 92 crc); 93 } 94 95 void ath10k_debug_print_boot_info(struct ath10k *ar) 96 { 97 ath10k_info(ar, "htt-ver %d.%d wmi-op %d htt-op %d cal %s max-sta %d raw %d hwcrypto %d\n", 98 ar->htt.target_version_major, 99 ar->htt.target_version_minor, 100 ar->normal_mode_fw.fw_file.wmi_op_version, 101 ar->normal_mode_fw.fw_file.htt_op_version, 102 ath10k_cal_mode_str(ar->cal_mode), 103 ar->max_num_stations, 104 test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags), 105 !test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags)); 106 } 107 108 void ath10k_print_driver_info(struct ath10k *ar) 109 { 110 ath10k_debug_print_hwfw_info(ar); 111 ath10k_debug_print_board_info(ar); 112 ath10k_debug_print_boot_info(ar); 113 } 114 EXPORT_SYMBOL(ath10k_print_driver_info); 115 116 void ath10k_err(struct ath10k *ar, const char *fmt, ...) 117 { 118 struct va_format vaf = { 119 .fmt = fmt, 120 }; 121 va_list args; 122 123 va_start(args, fmt); 124 vaf.va = &args; 125 dev_err(ar->dev, "%pV", &vaf); 126 trace_ath10k_log_err(ar, &vaf); 127 va_end(args); 128 } 129 EXPORT_SYMBOL(ath10k_err); 130 131 void ath10k_warn(struct ath10k *ar, const char *fmt, ...) 132 { 133 struct va_format vaf = { 134 .fmt = fmt, 135 }; 136 va_list args; 137 138 va_start(args, fmt); 139 vaf.va = &args; 140 dev_warn_ratelimited(ar->dev, "%pV", &vaf); 141 trace_ath10k_log_warn(ar, &vaf); 142 143 va_end(args); 144 } 145 EXPORT_SYMBOL(ath10k_warn); 146 147 #ifdef CONFIG_ATH10K_DEBUGFS 148 149 static ssize_t ath10k_read_wmi_services(struct file *file, 150 char __user *user_buf, 151 size_t count, loff_t *ppos) 152 { 153 struct ath10k *ar = file->private_data; 154 char *buf; 155 size_t len = 0, buf_len = 8192; 156 const char *name; 157 ssize_t ret_cnt; 158 bool enabled; 159 int i; 160 161 buf = kzalloc(buf_len, GFP_KERNEL); 162 if (!buf) 163 return -ENOMEM; 164 165 mutex_lock(&ar->conf_mutex); 166 167 spin_lock_bh(&ar->data_lock); 168 for (i = 0; i < WMI_SERVICE_MAX; i++) { 169 enabled = test_bit(i, ar->wmi.svc_map); 170 name = wmi_service_name(i); 171 172 if (!name) { 173 if (enabled) 174 len += scnprintf(buf + len, buf_len - len, 175 "%-40s %s (bit %d)\n", 176 "unknown", "enabled", i); 177 178 continue; 179 } 180 181 len += scnprintf(buf + len, buf_len - len, 182 "%-40s %s\n", 183 name, enabled ? "enabled" : "-"); 184 } 185 spin_unlock_bh(&ar->data_lock); 186 187 ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len); 188 189 mutex_unlock(&ar->conf_mutex); 190 191 kfree(buf); 192 return ret_cnt; 193 } 194 195 static const struct file_operations fops_wmi_services = { 196 .read = ath10k_read_wmi_services, 197 .open = simple_open, 198 .owner = THIS_MODULE, 199 .llseek = default_llseek, 200 }; 201 202 static void ath10k_fw_stats_pdevs_free(struct list_head *head) 203 { 204 struct ath10k_fw_stats_pdev *i, *tmp; 205 206 list_for_each_entry_safe(i, tmp, head, list) { 207 list_del(&i->list); 208 kfree(i); 209 } 210 } 211 212 static void ath10k_fw_stats_vdevs_free(struct list_head *head) 213 { 214 struct ath10k_fw_stats_vdev *i, *tmp; 215 216 list_for_each_entry_safe(i, tmp, head, list) { 217 list_del(&i->list); 218 kfree(i); 219 } 220 } 221 222 static void ath10k_fw_stats_peers_free(struct list_head *head) 223 { 224 struct ath10k_fw_stats_peer *i, *tmp; 225 226 list_for_each_entry_safe(i, tmp, head, list) { 227 list_del(&i->list); 228 kfree(i); 229 } 230 } 231 232 static void ath10k_fw_extd_stats_peers_free(struct list_head *head) 233 { 234 struct ath10k_fw_extd_stats_peer *i, *tmp; 235 236 list_for_each_entry_safe(i, tmp, head, list) { 237 list_del(&i->list); 238 kfree(i); 239 } 240 } 241 242 static void ath10k_debug_fw_stats_reset(struct ath10k *ar) 243 { 244 spin_lock_bh(&ar->data_lock); 245 ar->debug.fw_stats_done = false; 246 ar->debug.fw_stats.extended = false; 247 ath10k_fw_stats_pdevs_free(&ar->debug.fw_stats.pdevs); 248 ath10k_fw_stats_vdevs_free(&ar->debug.fw_stats.vdevs); 249 ath10k_fw_stats_peers_free(&ar->debug.fw_stats.peers); 250 ath10k_fw_extd_stats_peers_free(&ar->debug.fw_stats.peers_extd); 251 spin_unlock_bh(&ar->data_lock); 252 } 253 254 void ath10k_debug_fw_stats_process(struct ath10k *ar, struct sk_buff *skb) 255 { 256 struct ath10k_fw_stats stats = {}; 257 bool is_start, is_started, is_end; 258 size_t num_peers; 259 size_t num_vdevs; 260 int ret; 261 262 INIT_LIST_HEAD(&stats.pdevs); 263 INIT_LIST_HEAD(&stats.vdevs); 264 INIT_LIST_HEAD(&stats.peers); 265 INIT_LIST_HEAD(&stats.peers_extd); 266 267 spin_lock_bh(&ar->data_lock); 268 ret = ath10k_wmi_pull_fw_stats(ar, skb, &stats); 269 if (ret) { 270 ath10k_warn(ar, "failed to pull fw stats: %d\n", ret); 271 goto free; 272 } 273 274 /* Stat data may exceed htc-wmi buffer limit. In such case firmware 275 * splits the stats data and delivers it in a ping-pong fashion of 276 * request cmd-update event. 277 * 278 * However there is no explicit end-of-data. Instead start-of-data is 279 * used as an implicit one. This works as follows: 280 * a) discard stat update events until one with pdev stats is 281 * delivered - this skips session started at end of (b) 282 * b) consume stat update events until another one with pdev stats is 283 * delivered which is treated as end-of-data and is itself discarded 284 */ 285 if (ath10k_peer_stats_enabled(ar)) 286 ath10k_sta_update_rx_duration(ar, &stats); 287 288 if (ar->debug.fw_stats_done) { 289 if (!ath10k_peer_stats_enabled(ar)) 290 ath10k_warn(ar, "received unsolicited stats update event\n"); 291 292 goto free; 293 } 294 295 num_peers = ath10k_wmi_fw_stats_num_peers(&ar->debug.fw_stats.peers); 296 num_vdevs = ath10k_wmi_fw_stats_num_vdevs(&ar->debug.fw_stats.vdevs); 297 is_start = (list_empty(&ar->debug.fw_stats.pdevs) && 298 !list_empty(&stats.pdevs)); 299 is_end = (!list_empty(&ar->debug.fw_stats.pdevs) && 300 !list_empty(&stats.pdevs)); 301 302 if (is_start) 303 list_splice_tail_init(&stats.pdevs, &ar->debug.fw_stats.pdevs); 304 305 if (is_end) 306 ar->debug.fw_stats_done = true; 307 308 is_started = !list_empty(&ar->debug.fw_stats.pdevs); 309 310 if (is_started && !is_end) { 311 if (num_peers >= ATH10K_MAX_NUM_PEER_IDS) { 312 /* Although this is unlikely impose a sane limit to 313 * prevent firmware from DoS-ing the host. 314 */ 315 ath10k_fw_stats_peers_free(&ar->debug.fw_stats.peers); 316 ath10k_fw_extd_stats_peers_free(&ar->debug.fw_stats.peers_extd); 317 ath10k_warn(ar, "dropping fw peer stats\n"); 318 goto free; 319 } 320 321 if (num_vdevs >= BITS_PER_LONG) { 322 ath10k_fw_stats_vdevs_free(&ar->debug.fw_stats.vdevs); 323 ath10k_warn(ar, "dropping fw vdev stats\n"); 324 goto free; 325 } 326 327 if (!list_empty(&stats.peers)) 328 list_splice_tail_init(&stats.peers_extd, 329 &ar->debug.fw_stats.peers_extd); 330 331 list_splice_tail_init(&stats.peers, &ar->debug.fw_stats.peers); 332 list_splice_tail_init(&stats.vdevs, &ar->debug.fw_stats.vdevs); 333 } 334 335 complete(&ar->debug.fw_stats_complete); 336 337 free: 338 /* In some cases lists have been spliced and cleared. Free up 339 * resources if that is not the case. 340 */ 341 ath10k_fw_stats_pdevs_free(&stats.pdevs); 342 ath10k_fw_stats_vdevs_free(&stats.vdevs); 343 ath10k_fw_stats_peers_free(&stats.peers); 344 ath10k_fw_extd_stats_peers_free(&stats.peers_extd); 345 346 spin_unlock_bh(&ar->data_lock); 347 } 348 349 static int ath10k_debug_fw_stats_request(struct ath10k *ar) 350 { 351 unsigned long timeout, time_left; 352 int ret; 353 354 lockdep_assert_held(&ar->conf_mutex); 355 356 timeout = jiffies + msecs_to_jiffies(1 * HZ); 357 358 ath10k_debug_fw_stats_reset(ar); 359 360 for (;;) { 361 if (time_after(jiffies, timeout)) 362 return -ETIMEDOUT; 363 364 reinit_completion(&ar->debug.fw_stats_complete); 365 366 ret = ath10k_wmi_request_stats(ar, ar->fw_stats_req_mask); 367 if (ret) { 368 ath10k_warn(ar, "could not request stats (%d)\n", ret); 369 return ret; 370 } 371 372 time_left = 373 wait_for_completion_timeout(&ar->debug.fw_stats_complete, 374 1 * HZ); 375 if (!time_left) 376 return -ETIMEDOUT; 377 378 spin_lock_bh(&ar->data_lock); 379 if (ar->debug.fw_stats_done) { 380 spin_unlock_bh(&ar->data_lock); 381 break; 382 } 383 spin_unlock_bh(&ar->data_lock); 384 } 385 386 return 0; 387 } 388 389 static int ath10k_fw_stats_open(struct inode *inode, struct file *file) 390 { 391 struct ath10k *ar = inode->i_private; 392 void *buf = NULL; 393 int ret; 394 395 mutex_lock(&ar->conf_mutex); 396 397 if (ar->state != ATH10K_STATE_ON) { 398 ret = -ENETDOWN; 399 goto err_unlock; 400 } 401 402 buf = vmalloc(ATH10K_FW_STATS_BUF_SIZE); 403 if (!buf) { 404 ret = -ENOMEM; 405 goto err_unlock; 406 } 407 408 ret = ath10k_debug_fw_stats_request(ar); 409 if (ret) { 410 ath10k_warn(ar, "failed to request fw stats: %d\n", ret); 411 goto err_free; 412 } 413 414 ret = ath10k_wmi_fw_stats_fill(ar, &ar->debug.fw_stats, buf); 415 if (ret) { 416 ath10k_warn(ar, "failed to fill fw stats: %d\n", ret); 417 goto err_free; 418 } 419 420 file->private_data = buf; 421 422 mutex_unlock(&ar->conf_mutex); 423 return 0; 424 425 err_free: 426 vfree(buf); 427 428 err_unlock: 429 mutex_unlock(&ar->conf_mutex); 430 return ret; 431 } 432 433 static int ath10k_fw_stats_release(struct inode *inode, struct file *file) 434 { 435 vfree(file->private_data); 436 437 return 0; 438 } 439 440 static ssize_t ath10k_fw_stats_read(struct file *file, char __user *user_buf, 441 size_t count, loff_t *ppos) 442 { 443 const char *buf = file->private_data; 444 size_t len = strlen(buf); 445 446 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 447 } 448 449 static const struct file_operations fops_fw_stats = { 450 .open = ath10k_fw_stats_open, 451 .release = ath10k_fw_stats_release, 452 .read = ath10k_fw_stats_read, 453 .owner = THIS_MODULE, 454 .llseek = default_llseek, 455 }; 456 457 static ssize_t ath10k_debug_fw_reset_stats_read(struct file *file, 458 char __user *user_buf, 459 size_t count, loff_t *ppos) 460 { 461 struct ath10k *ar = file->private_data; 462 int ret; 463 size_t len = 0, buf_len = 500; 464 char *buf; 465 466 buf = kmalloc(buf_len, GFP_KERNEL); 467 if (!buf) 468 return -ENOMEM; 469 470 spin_lock_bh(&ar->data_lock); 471 472 len += scnprintf(buf + len, buf_len - len, 473 "fw_crash_counter\t\t%d\n", ar->stats.fw_crash_counter); 474 len += scnprintf(buf + len, buf_len - len, 475 "fw_warm_reset_counter\t\t%d\n", 476 ar->stats.fw_warm_reset_counter); 477 len += scnprintf(buf + len, buf_len - len, 478 "fw_cold_reset_counter\t\t%d\n", 479 ar->stats.fw_cold_reset_counter); 480 481 spin_unlock_bh(&ar->data_lock); 482 483 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len); 484 485 kfree(buf); 486 487 return ret; 488 } 489 490 static const struct file_operations fops_fw_reset_stats = { 491 .open = simple_open, 492 .read = ath10k_debug_fw_reset_stats_read, 493 .owner = THIS_MODULE, 494 .llseek = default_llseek, 495 }; 496 497 /* This is a clean assert crash in firmware. */ 498 static int ath10k_debug_fw_assert(struct ath10k *ar) 499 { 500 struct wmi_vdev_install_key_cmd *cmd; 501 struct sk_buff *skb; 502 503 skb = ath10k_wmi_alloc_skb(ar, sizeof(*cmd) + 16); 504 if (!skb) 505 return -ENOMEM; 506 507 cmd = (struct wmi_vdev_install_key_cmd *)skb->data; 508 memset(cmd, 0, sizeof(*cmd)); 509 510 /* big enough number so that firmware asserts */ 511 cmd->vdev_id = __cpu_to_le32(0x7ffe); 512 513 return ath10k_wmi_cmd_send(ar, skb, 514 ar->wmi.cmd->vdev_install_key_cmdid); 515 } 516 517 static ssize_t ath10k_read_simulate_fw_crash(struct file *file, 518 char __user *user_buf, 519 size_t count, loff_t *ppos) 520 { 521 const char buf[] = 522 "To simulate firmware crash write one of the keywords to this file:\n" 523 "`soft` - this will send WMI_FORCE_FW_HANG_ASSERT to firmware if FW supports that command.\n" 524 "`hard` - this will send to firmware command with illegal parameters causing firmware crash.\n" 525 "`assert` - this will send special illegal parameter to firmware to cause assert failure and crash.\n" 526 "`hw-restart` - this will simply queue hw restart without fw/hw actually crashing.\n"; 527 528 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf)); 529 } 530 531 /* Simulate firmware crash: 532 * 'soft': Call wmi command causing firmware hang. This firmware hang is 533 * recoverable by warm firmware reset. 534 * 'hard': Force firmware crash by setting any vdev parameter for not allowed 535 * vdev id. This is hard firmware crash because it is recoverable only by cold 536 * firmware reset. 537 */ 538 static ssize_t ath10k_write_simulate_fw_crash(struct file *file, 539 const char __user *user_buf, 540 size_t count, loff_t *ppos) 541 { 542 struct ath10k *ar = file->private_data; 543 char buf[32] = {0}; 544 ssize_t rc; 545 int ret; 546 547 /* filter partial writes and invalid commands */ 548 if (*ppos != 0 || count >= sizeof(buf) || count == 0) 549 return -EINVAL; 550 551 rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count); 552 if (rc < 0) 553 return rc; 554 555 /* drop the possible '\n' from the end */ 556 if (buf[*ppos - 1] == '\n') 557 buf[*ppos - 1] = '\0'; 558 559 mutex_lock(&ar->conf_mutex); 560 561 if (ar->state != ATH10K_STATE_ON && 562 ar->state != ATH10K_STATE_RESTARTED) { 563 ret = -ENETDOWN; 564 goto exit; 565 } 566 567 if (!strcmp(buf, "soft")) { 568 ath10k_info(ar, "simulating soft firmware crash\n"); 569 ret = ath10k_wmi_force_fw_hang(ar, WMI_FORCE_FW_HANG_ASSERT, 0); 570 } else if (!strcmp(buf, "hard")) { 571 ath10k_info(ar, "simulating hard firmware crash\n"); 572 /* 0x7fff is vdev id, and it is always out of range for all 573 * firmware variants in order to force a firmware crash. 574 */ 575 ret = ath10k_wmi_vdev_set_param(ar, 0x7fff, 576 ar->wmi.vdev_param->rts_threshold, 577 0); 578 } else if (!strcmp(buf, "assert")) { 579 ath10k_info(ar, "simulating firmware assert crash\n"); 580 ret = ath10k_debug_fw_assert(ar); 581 } else if (!strcmp(buf, "hw-restart")) { 582 ath10k_info(ar, "user requested hw restart\n"); 583 queue_work(ar->workqueue, &ar->restart_work); 584 ret = 0; 585 } else { 586 ret = -EINVAL; 587 goto exit; 588 } 589 590 if (ret) { 591 ath10k_warn(ar, "failed to simulate firmware crash: %d\n", ret); 592 goto exit; 593 } 594 595 ret = count; 596 597 exit: 598 mutex_unlock(&ar->conf_mutex); 599 return ret; 600 } 601 602 static const struct file_operations fops_simulate_fw_crash = { 603 .read = ath10k_read_simulate_fw_crash, 604 .write = ath10k_write_simulate_fw_crash, 605 .open = simple_open, 606 .owner = THIS_MODULE, 607 .llseek = default_llseek, 608 }; 609 610 static ssize_t ath10k_read_chip_id(struct file *file, char __user *user_buf, 611 size_t count, loff_t *ppos) 612 { 613 struct ath10k *ar = file->private_data; 614 size_t len; 615 char buf[50]; 616 617 len = scnprintf(buf, sizeof(buf), "0x%08x\n", ar->bus_param.chip_id); 618 619 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 620 } 621 622 static const struct file_operations fops_chip_id = { 623 .read = ath10k_read_chip_id, 624 .open = simple_open, 625 .owner = THIS_MODULE, 626 .llseek = default_llseek, 627 }; 628 629 static ssize_t ath10k_reg_addr_read(struct file *file, 630 char __user *user_buf, 631 size_t count, loff_t *ppos) 632 { 633 struct ath10k *ar = file->private_data; 634 u8 buf[32]; 635 size_t len = 0; 636 u32 reg_addr; 637 638 mutex_lock(&ar->conf_mutex); 639 reg_addr = ar->debug.reg_addr; 640 mutex_unlock(&ar->conf_mutex); 641 642 len += scnprintf(buf + len, sizeof(buf) - len, "0x%x\n", reg_addr); 643 644 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 645 } 646 647 static ssize_t ath10k_reg_addr_write(struct file *file, 648 const char __user *user_buf, 649 size_t count, loff_t *ppos) 650 { 651 struct ath10k *ar = file->private_data; 652 u32 reg_addr; 653 int ret; 654 655 ret = kstrtou32_from_user(user_buf, count, 0, ®_addr); 656 if (ret) 657 return ret; 658 659 if (!IS_ALIGNED(reg_addr, 4)) 660 return -EFAULT; 661 662 mutex_lock(&ar->conf_mutex); 663 ar->debug.reg_addr = reg_addr; 664 mutex_unlock(&ar->conf_mutex); 665 666 return count; 667 } 668 669 static const struct file_operations fops_reg_addr = { 670 .read = ath10k_reg_addr_read, 671 .write = ath10k_reg_addr_write, 672 .open = simple_open, 673 .owner = THIS_MODULE, 674 .llseek = default_llseek, 675 }; 676 677 static ssize_t ath10k_reg_value_read(struct file *file, 678 char __user *user_buf, 679 size_t count, loff_t *ppos) 680 { 681 struct ath10k *ar = file->private_data; 682 u8 buf[48]; 683 size_t len; 684 u32 reg_addr, reg_val; 685 int ret; 686 687 mutex_lock(&ar->conf_mutex); 688 689 if (ar->state != ATH10K_STATE_ON && 690 ar->state != ATH10K_STATE_UTF) { 691 ret = -ENETDOWN; 692 goto exit; 693 } 694 695 reg_addr = ar->debug.reg_addr; 696 697 reg_val = ath10k_hif_read32(ar, reg_addr); 698 len = scnprintf(buf, sizeof(buf), "0x%08x:0x%08x\n", reg_addr, reg_val); 699 700 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len); 701 702 exit: 703 mutex_unlock(&ar->conf_mutex); 704 705 return ret; 706 } 707 708 static ssize_t ath10k_reg_value_write(struct file *file, 709 const char __user *user_buf, 710 size_t count, loff_t *ppos) 711 { 712 struct ath10k *ar = file->private_data; 713 u32 reg_addr, reg_val; 714 int ret; 715 716 mutex_lock(&ar->conf_mutex); 717 718 if (ar->state != ATH10K_STATE_ON && 719 ar->state != ATH10K_STATE_UTF) { 720 ret = -ENETDOWN; 721 goto exit; 722 } 723 724 reg_addr = ar->debug.reg_addr; 725 726 ret = kstrtou32_from_user(user_buf, count, 0, ®_val); 727 if (ret) 728 goto exit; 729 730 ath10k_hif_write32(ar, reg_addr, reg_val); 731 732 ret = count; 733 734 exit: 735 mutex_unlock(&ar->conf_mutex); 736 737 return ret; 738 } 739 740 static const struct file_operations fops_reg_value = { 741 .read = ath10k_reg_value_read, 742 .write = ath10k_reg_value_write, 743 .open = simple_open, 744 .owner = THIS_MODULE, 745 .llseek = default_llseek, 746 }; 747 748 static ssize_t ath10k_mem_value_read(struct file *file, 749 char __user *user_buf, 750 size_t count, loff_t *ppos) 751 { 752 struct ath10k *ar = file->private_data; 753 u8 *buf; 754 int ret; 755 756 if (*ppos < 0) 757 return -EINVAL; 758 759 if (!count) 760 return 0; 761 762 mutex_lock(&ar->conf_mutex); 763 764 buf = vmalloc(count); 765 if (!buf) { 766 ret = -ENOMEM; 767 goto exit; 768 } 769 770 if (ar->state != ATH10K_STATE_ON && 771 ar->state != ATH10K_STATE_UTF) { 772 ret = -ENETDOWN; 773 goto exit; 774 } 775 776 ret = ath10k_hif_diag_read(ar, *ppos, buf, count); 777 if (ret) { 778 ath10k_warn(ar, "failed to read address 0x%08x via diagnose window fnrom debugfs: %d\n", 779 (u32)(*ppos), ret); 780 goto exit; 781 } 782 783 ret = copy_to_user(user_buf, buf, count); 784 if (ret) { 785 ret = -EFAULT; 786 goto exit; 787 } 788 789 count -= ret; 790 *ppos += count; 791 ret = count; 792 793 exit: 794 vfree(buf); 795 mutex_unlock(&ar->conf_mutex); 796 797 return ret; 798 } 799 800 static ssize_t ath10k_mem_value_write(struct file *file, 801 const char __user *user_buf, 802 size_t count, loff_t *ppos) 803 { 804 struct ath10k *ar = file->private_data; 805 u8 *buf; 806 int ret; 807 808 if (*ppos < 0) 809 return -EINVAL; 810 811 if (!count) 812 return 0; 813 814 mutex_lock(&ar->conf_mutex); 815 816 buf = vmalloc(count); 817 if (!buf) { 818 ret = -ENOMEM; 819 goto exit; 820 } 821 822 if (ar->state != ATH10K_STATE_ON && 823 ar->state != ATH10K_STATE_UTF) { 824 ret = -ENETDOWN; 825 goto exit; 826 } 827 828 ret = copy_from_user(buf, user_buf, count); 829 if (ret) { 830 ret = -EFAULT; 831 goto exit; 832 } 833 834 ret = ath10k_hif_diag_write(ar, *ppos, buf, count); 835 if (ret) { 836 ath10k_warn(ar, "failed to write address 0x%08x via diagnose window from debugfs: %d\n", 837 (u32)(*ppos), ret); 838 goto exit; 839 } 840 841 *ppos += count; 842 ret = count; 843 844 exit: 845 vfree(buf); 846 mutex_unlock(&ar->conf_mutex); 847 848 return ret; 849 } 850 851 static const struct file_operations fops_mem_value = { 852 .read = ath10k_mem_value_read, 853 .write = ath10k_mem_value_write, 854 .open = simple_open, 855 .owner = THIS_MODULE, 856 .llseek = default_llseek, 857 }; 858 859 static int ath10k_debug_htt_stats_req(struct ath10k *ar) 860 { 861 u64 cookie; 862 int ret; 863 864 lockdep_assert_held(&ar->conf_mutex); 865 866 if (ar->debug.htt_stats_mask == 0) 867 /* htt stats are disabled */ 868 return 0; 869 870 if (ar->state != ATH10K_STATE_ON) 871 return 0; 872 873 cookie = get_jiffies_64(); 874 875 ret = ath10k_htt_h2t_stats_req(&ar->htt, ar->debug.htt_stats_mask, 876 cookie); 877 if (ret) { 878 ath10k_warn(ar, "failed to send htt stats request: %d\n", ret); 879 return ret; 880 } 881 882 queue_delayed_work(ar->workqueue, &ar->debug.htt_stats_dwork, 883 msecs_to_jiffies(ATH10K_DEBUG_HTT_STATS_INTERVAL)); 884 885 return 0; 886 } 887 888 static void ath10k_debug_htt_stats_dwork(struct work_struct *work) 889 { 890 struct ath10k *ar = container_of(work, struct ath10k, 891 debug.htt_stats_dwork.work); 892 893 mutex_lock(&ar->conf_mutex); 894 895 ath10k_debug_htt_stats_req(ar); 896 897 mutex_unlock(&ar->conf_mutex); 898 } 899 900 static ssize_t ath10k_read_htt_stats_mask(struct file *file, 901 char __user *user_buf, 902 size_t count, loff_t *ppos) 903 { 904 struct ath10k *ar = file->private_data; 905 char buf[32]; 906 size_t len; 907 908 len = scnprintf(buf, sizeof(buf), "%lu\n", ar->debug.htt_stats_mask); 909 910 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 911 } 912 913 static ssize_t ath10k_write_htt_stats_mask(struct file *file, 914 const char __user *user_buf, 915 size_t count, loff_t *ppos) 916 { 917 struct ath10k *ar = file->private_data; 918 unsigned long mask; 919 int ret; 920 921 ret = kstrtoul_from_user(user_buf, count, 0, &mask); 922 if (ret) 923 return ret; 924 925 /* max 8 bit masks (for now) */ 926 if (mask > 0xff) 927 return -E2BIG; 928 929 mutex_lock(&ar->conf_mutex); 930 931 ar->debug.htt_stats_mask = mask; 932 933 ret = ath10k_debug_htt_stats_req(ar); 934 if (ret) 935 goto out; 936 937 ret = count; 938 939 out: 940 mutex_unlock(&ar->conf_mutex); 941 942 return ret; 943 } 944 945 static const struct file_operations fops_htt_stats_mask = { 946 .read = ath10k_read_htt_stats_mask, 947 .write = ath10k_write_htt_stats_mask, 948 .open = simple_open, 949 .owner = THIS_MODULE, 950 .llseek = default_llseek, 951 }; 952 953 static ssize_t ath10k_read_htt_max_amsdu_ampdu(struct file *file, 954 char __user *user_buf, 955 size_t count, loff_t *ppos) 956 { 957 struct ath10k *ar = file->private_data; 958 char buf[64]; 959 u8 amsdu, ampdu; 960 size_t len; 961 962 mutex_lock(&ar->conf_mutex); 963 964 amsdu = ar->htt.max_num_amsdu; 965 ampdu = ar->htt.max_num_ampdu; 966 mutex_unlock(&ar->conf_mutex); 967 968 len = scnprintf(buf, sizeof(buf), "%u %u\n", amsdu, ampdu); 969 970 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 971 } 972 973 static ssize_t ath10k_write_htt_max_amsdu_ampdu(struct file *file, 974 const char __user *user_buf, 975 size_t count, loff_t *ppos) 976 { 977 struct ath10k *ar = file->private_data; 978 int res; 979 char buf[64] = {0}; 980 unsigned int amsdu, ampdu; 981 982 res = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, 983 user_buf, count); 984 if (res <= 0) 985 return res; 986 987 res = sscanf(buf, "%u %u", &amsdu, &du); 988 989 if (res != 2) 990 return -EINVAL; 991 992 mutex_lock(&ar->conf_mutex); 993 994 res = ath10k_htt_h2t_aggr_cfg_msg(&ar->htt, ampdu, amsdu); 995 if (res) 996 goto out; 997 998 res = count; 999 ar->htt.max_num_amsdu = amsdu; 1000 ar->htt.max_num_ampdu = ampdu; 1001 1002 out: 1003 mutex_unlock(&ar->conf_mutex); 1004 return res; 1005 } 1006 1007 static const struct file_operations fops_htt_max_amsdu_ampdu = { 1008 .read = ath10k_read_htt_max_amsdu_ampdu, 1009 .write = ath10k_write_htt_max_amsdu_ampdu, 1010 .open = simple_open, 1011 .owner = THIS_MODULE, 1012 .llseek = default_llseek, 1013 }; 1014 1015 static ssize_t ath10k_read_fw_dbglog(struct file *file, 1016 char __user *user_buf, 1017 size_t count, loff_t *ppos) 1018 { 1019 struct ath10k *ar = file->private_data; 1020 size_t len; 1021 char buf[96]; 1022 1023 len = scnprintf(buf, sizeof(buf), "0x%16llx %u\n", 1024 ar->debug.fw_dbglog_mask, ar->debug.fw_dbglog_level); 1025 1026 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 1027 } 1028 1029 static ssize_t ath10k_write_fw_dbglog(struct file *file, 1030 const char __user *user_buf, 1031 size_t count, loff_t *ppos) 1032 { 1033 struct ath10k *ar = file->private_data; 1034 int ret; 1035 char buf[96] = {0}; 1036 unsigned int log_level; 1037 u64 mask; 1038 1039 ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, 1040 user_buf, count); 1041 if (ret <= 0) 1042 return ret; 1043 1044 ret = sscanf(buf, "%llx %u", &mask, &log_level); 1045 1046 if (!ret) 1047 return -EINVAL; 1048 1049 if (ret == 1) 1050 /* default if user did not specify */ 1051 log_level = ATH10K_DBGLOG_LEVEL_WARN; 1052 1053 mutex_lock(&ar->conf_mutex); 1054 1055 ar->debug.fw_dbglog_mask = mask; 1056 ar->debug.fw_dbglog_level = log_level; 1057 1058 if (ar->state == ATH10K_STATE_ON) { 1059 ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask, 1060 ar->debug.fw_dbglog_level); 1061 if (ret) { 1062 ath10k_warn(ar, "dbglog cfg failed from debugfs: %d\n", 1063 ret); 1064 goto exit; 1065 } 1066 } 1067 1068 ret = count; 1069 1070 exit: 1071 mutex_unlock(&ar->conf_mutex); 1072 1073 return ret; 1074 } 1075 1076 /* TODO: Would be nice to always support ethtool stats, would need to 1077 * move the stats storage out of ath10k_debug, or always have ath10k_debug 1078 * struct available.. 1079 */ 1080 1081 /* This generally cooresponds to the debugfs fw_stats file */ 1082 static const char ath10k_gstrings_stats[][ETH_GSTRING_LEN] = { 1083 "tx_pkts_nic", 1084 "tx_bytes_nic", 1085 "rx_pkts_nic", 1086 "rx_bytes_nic", 1087 "d_noise_floor", 1088 "d_cycle_count", 1089 "d_phy_error", 1090 "d_rts_bad", 1091 "d_rts_good", 1092 "d_tx_power", /* in .5 dbM I think */ 1093 "d_rx_crc_err", /* fcs_bad */ 1094 "d_no_beacon", 1095 "d_tx_mpdus_queued", 1096 "d_tx_msdu_queued", 1097 "d_tx_msdu_dropped", 1098 "d_local_enqued", 1099 "d_local_freed", 1100 "d_tx_ppdu_hw_queued", 1101 "d_tx_ppdu_reaped", 1102 "d_tx_fifo_underrun", 1103 "d_tx_ppdu_abort", 1104 "d_tx_mpdu_requed", 1105 "d_tx_excessive_retries", 1106 "d_tx_hw_rate", 1107 "d_tx_dropped_sw_retries", 1108 "d_tx_illegal_rate", 1109 "d_tx_continuous_xretries", 1110 "d_tx_timeout", 1111 "d_tx_mpdu_txop_limit", 1112 "d_pdev_resets", 1113 "d_rx_mid_ppdu_route_change", 1114 "d_rx_status", 1115 "d_rx_extra_frags_ring0", 1116 "d_rx_extra_frags_ring1", 1117 "d_rx_extra_frags_ring2", 1118 "d_rx_extra_frags_ring3", 1119 "d_rx_msdu_htt", 1120 "d_rx_mpdu_htt", 1121 "d_rx_msdu_stack", 1122 "d_rx_mpdu_stack", 1123 "d_rx_phy_err", 1124 "d_rx_phy_err_drops", 1125 "d_rx_mpdu_errors", /* FCS, MIC, ENC */ 1126 "d_fw_crash_count", 1127 "d_fw_warm_reset_count", 1128 "d_fw_cold_reset_count", 1129 }; 1130 1131 #define ATH10K_SSTATS_LEN ARRAY_SIZE(ath10k_gstrings_stats) 1132 1133 void ath10k_debug_get_et_strings(struct ieee80211_hw *hw, 1134 struct ieee80211_vif *vif, 1135 u32 sset, u8 *data) 1136 { 1137 if (sset == ETH_SS_STATS) 1138 memcpy(data, *ath10k_gstrings_stats, 1139 sizeof(ath10k_gstrings_stats)); 1140 } 1141 1142 int ath10k_debug_get_et_sset_count(struct ieee80211_hw *hw, 1143 struct ieee80211_vif *vif, int sset) 1144 { 1145 if (sset == ETH_SS_STATS) 1146 return ATH10K_SSTATS_LEN; 1147 1148 return 0; 1149 } 1150 1151 void ath10k_debug_get_et_stats(struct ieee80211_hw *hw, 1152 struct ieee80211_vif *vif, 1153 struct ethtool_stats *stats, u64 *data) 1154 { 1155 struct ath10k *ar = hw->priv; 1156 static const struct ath10k_fw_stats_pdev zero_stats = {}; 1157 const struct ath10k_fw_stats_pdev *pdev_stats; 1158 int i = 0, ret; 1159 1160 mutex_lock(&ar->conf_mutex); 1161 1162 if (ar->state == ATH10K_STATE_ON) { 1163 ret = ath10k_debug_fw_stats_request(ar); 1164 if (ret) { 1165 /* just print a warning and try to use older results */ 1166 ath10k_warn(ar, 1167 "failed to get fw stats for ethtool: %d\n", 1168 ret); 1169 } 1170 } 1171 1172 pdev_stats = list_first_entry_or_null(&ar->debug.fw_stats.pdevs, 1173 struct ath10k_fw_stats_pdev, 1174 list); 1175 if (!pdev_stats) { 1176 /* no results available so just return zeroes */ 1177 pdev_stats = &zero_stats; 1178 } 1179 1180 spin_lock_bh(&ar->data_lock); 1181 1182 data[i++] = pdev_stats->hw_reaped; /* ppdu reaped */ 1183 data[i++] = 0; /* tx bytes */ 1184 data[i++] = pdev_stats->htt_mpdus; 1185 data[i++] = 0; /* rx bytes */ 1186 data[i++] = pdev_stats->ch_noise_floor; 1187 data[i++] = pdev_stats->cycle_count; 1188 data[i++] = pdev_stats->phy_err_count; 1189 data[i++] = pdev_stats->rts_bad; 1190 data[i++] = pdev_stats->rts_good; 1191 data[i++] = pdev_stats->chan_tx_power; 1192 data[i++] = pdev_stats->fcs_bad; 1193 data[i++] = pdev_stats->no_beacons; 1194 data[i++] = pdev_stats->mpdu_enqued; 1195 data[i++] = pdev_stats->msdu_enqued; 1196 data[i++] = pdev_stats->wmm_drop; 1197 data[i++] = pdev_stats->local_enqued; 1198 data[i++] = pdev_stats->local_freed; 1199 data[i++] = pdev_stats->hw_queued; 1200 data[i++] = pdev_stats->hw_reaped; 1201 data[i++] = pdev_stats->underrun; 1202 data[i++] = pdev_stats->tx_abort; 1203 data[i++] = pdev_stats->mpdus_requed; 1204 data[i++] = pdev_stats->tx_ko; 1205 data[i++] = pdev_stats->data_rc; 1206 data[i++] = pdev_stats->sw_retry_failure; 1207 data[i++] = pdev_stats->illgl_rate_phy_err; 1208 data[i++] = pdev_stats->pdev_cont_xretry; 1209 data[i++] = pdev_stats->pdev_tx_timeout; 1210 data[i++] = pdev_stats->txop_ovf; 1211 data[i++] = pdev_stats->pdev_resets; 1212 data[i++] = pdev_stats->mid_ppdu_route_change; 1213 data[i++] = pdev_stats->status_rcvd; 1214 data[i++] = pdev_stats->r0_frags; 1215 data[i++] = pdev_stats->r1_frags; 1216 data[i++] = pdev_stats->r2_frags; 1217 data[i++] = pdev_stats->r3_frags; 1218 data[i++] = pdev_stats->htt_msdus; 1219 data[i++] = pdev_stats->htt_mpdus; 1220 data[i++] = pdev_stats->loc_msdus; 1221 data[i++] = pdev_stats->loc_mpdus; 1222 data[i++] = pdev_stats->phy_errs; 1223 data[i++] = pdev_stats->phy_err_drop; 1224 data[i++] = pdev_stats->mpdu_errs; 1225 data[i++] = ar->stats.fw_crash_counter; 1226 data[i++] = ar->stats.fw_warm_reset_counter; 1227 data[i++] = ar->stats.fw_cold_reset_counter; 1228 1229 spin_unlock_bh(&ar->data_lock); 1230 1231 mutex_unlock(&ar->conf_mutex); 1232 1233 WARN_ON(i != ATH10K_SSTATS_LEN); 1234 } 1235 1236 static const struct file_operations fops_fw_dbglog = { 1237 .read = ath10k_read_fw_dbglog, 1238 .write = ath10k_write_fw_dbglog, 1239 .open = simple_open, 1240 .owner = THIS_MODULE, 1241 .llseek = default_llseek, 1242 }; 1243 1244 static int ath10k_debug_cal_data_fetch(struct ath10k *ar) 1245 { 1246 u32 hi_addr; 1247 __le32 addr; 1248 int ret; 1249 1250 lockdep_assert_held(&ar->conf_mutex); 1251 1252 if (WARN_ON(ar->hw_params.cal_data_len > ATH10K_DEBUG_CAL_DATA_LEN)) 1253 return -EINVAL; 1254 1255 if (ar->hw_params.cal_data_len == 0) 1256 return -EOPNOTSUPP; 1257 1258 hi_addr = host_interest_item_address(HI_ITEM(hi_board_data)); 1259 1260 ret = ath10k_hif_diag_read(ar, hi_addr, &addr, sizeof(addr)); 1261 if (ret) { 1262 ath10k_warn(ar, "failed to read hi_board_data address: %d\n", 1263 ret); 1264 return ret; 1265 } 1266 1267 ret = ath10k_hif_diag_read(ar, le32_to_cpu(addr), ar->debug.cal_data, 1268 ar->hw_params.cal_data_len); 1269 if (ret) { 1270 ath10k_warn(ar, "failed to read calibration data: %d\n", ret); 1271 return ret; 1272 } 1273 1274 return 0; 1275 } 1276 1277 static int ath10k_debug_cal_data_open(struct inode *inode, struct file *file) 1278 { 1279 struct ath10k *ar = inode->i_private; 1280 1281 mutex_lock(&ar->conf_mutex); 1282 1283 if (ar->state == ATH10K_STATE_ON || 1284 ar->state == ATH10K_STATE_UTF) { 1285 ath10k_debug_cal_data_fetch(ar); 1286 } 1287 1288 file->private_data = ar; 1289 mutex_unlock(&ar->conf_mutex); 1290 1291 return 0; 1292 } 1293 1294 static ssize_t ath10k_debug_cal_data_read(struct file *file, 1295 char __user *user_buf, 1296 size_t count, loff_t *ppos) 1297 { 1298 struct ath10k *ar = file->private_data; 1299 1300 mutex_lock(&ar->conf_mutex); 1301 1302 count = simple_read_from_buffer(user_buf, count, ppos, 1303 ar->debug.cal_data, 1304 ar->hw_params.cal_data_len); 1305 1306 mutex_unlock(&ar->conf_mutex); 1307 1308 return count; 1309 } 1310 1311 static ssize_t ath10k_write_ani_enable(struct file *file, 1312 const char __user *user_buf, 1313 size_t count, loff_t *ppos) 1314 { 1315 struct ath10k *ar = file->private_data; 1316 int ret; 1317 u8 enable; 1318 1319 if (kstrtou8_from_user(user_buf, count, 0, &enable)) 1320 return -EINVAL; 1321 1322 mutex_lock(&ar->conf_mutex); 1323 1324 if (ar->ani_enabled == enable) { 1325 ret = count; 1326 goto exit; 1327 } 1328 1329 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->ani_enable, 1330 enable); 1331 if (ret) { 1332 ath10k_warn(ar, "ani_enable failed from debugfs: %d\n", ret); 1333 goto exit; 1334 } 1335 ar->ani_enabled = enable; 1336 1337 ret = count; 1338 1339 exit: 1340 mutex_unlock(&ar->conf_mutex); 1341 1342 return ret; 1343 } 1344 1345 static ssize_t ath10k_read_ani_enable(struct file *file, char __user *user_buf, 1346 size_t count, loff_t *ppos) 1347 { 1348 struct ath10k *ar = file->private_data; 1349 size_t len; 1350 char buf[32]; 1351 1352 len = scnprintf(buf, sizeof(buf), "%d\n", ar->ani_enabled); 1353 1354 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 1355 } 1356 1357 static const struct file_operations fops_ani_enable = { 1358 .read = ath10k_read_ani_enable, 1359 .write = ath10k_write_ani_enable, 1360 .open = simple_open, 1361 .owner = THIS_MODULE, 1362 .llseek = default_llseek, 1363 }; 1364 1365 static const struct file_operations fops_cal_data = { 1366 .open = ath10k_debug_cal_data_open, 1367 .read = ath10k_debug_cal_data_read, 1368 .owner = THIS_MODULE, 1369 .llseek = default_llseek, 1370 }; 1371 1372 static ssize_t ath10k_read_nf_cal_period(struct file *file, 1373 char __user *user_buf, 1374 size_t count, loff_t *ppos) 1375 { 1376 struct ath10k *ar = file->private_data; 1377 size_t len; 1378 char buf[32]; 1379 1380 len = scnprintf(buf, sizeof(buf), "%d\n", ar->debug.nf_cal_period); 1381 1382 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 1383 } 1384 1385 static ssize_t ath10k_write_nf_cal_period(struct file *file, 1386 const char __user *user_buf, 1387 size_t count, loff_t *ppos) 1388 { 1389 struct ath10k *ar = file->private_data; 1390 unsigned long period; 1391 int ret; 1392 1393 ret = kstrtoul_from_user(user_buf, count, 0, &period); 1394 if (ret) 1395 return ret; 1396 1397 if (period > WMI_PDEV_PARAM_CAL_PERIOD_MAX) 1398 return -EINVAL; 1399 1400 /* there's no way to switch back to the firmware default */ 1401 if (period == 0) 1402 return -EINVAL; 1403 1404 mutex_lock(&ar->conf_mutex); 1405 1406 ar->debug.nf_cal_period = period; 1407 1408 if (ar->state != ATH10K_STATE_ON) { 1409 /* firmware is not running, nothing else to do */ 1410 ret = count; 1411 goto exit; 1412 } 1413 1414 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->cal_period, 1415 ar->debug.nf_cal_period); 1416 if (ret) { 1417 ath10k_warn(ar, "cal period cfg failed from debugfs: %d\n", 1418 ret); 1419 goto exit; 1420 } 1421 1422 ret = count; 1423 1424 exit: 1425 mutex_unlock(&ar->conf_mutex); 1426 1427 return ret; 1428 } 1429 1430 static const struct file_operations fops_nf_cal_period = { 1431 .read = ath10k_read_nf_cal_period, 1432 .write = ath10k_write_nf_cal_period, 1433 .open = simple_open, 1434 .owner = THIS_MODULE, 1435 .llseek = default_llseek, 1436 }; 1437 1438 #define ATH10K_TPC_CONFIG_BUF_SIZE (1024 * 1024) 1439 1440 static int ath10k_debug_tpc_stats_request(struct ath10k *ar) 1441 { 1442 int ret; 1443 unsigned long time_left; 1444 1445 lockdep_assert_held(&ar->conf_mutex); 1446 1447 reinit_completion(&ar->debug.tpc_complete); 1448 1449 ret = ath10k_wmi_pdev_get_tpc_config(ar, WMI_TPC_CONFIG_PARAM); 1450 if (ret) { 1451 ath10k_warn(ar, "failed to request tpc config: %d\n", ret); 1452 return ret; 1453 } 1454 1455 time_left = wait_for_completion_timeout(&ar->debug.tpc_complete, 1456 1 * HZ); 1457 if (time_left == 0) 1458 return -ETIMEDOUT; 1459 1460 return 0; 1461 } 1462 1463 void ath10k_debug_tpc_stats_process(struct ath10k *ar, 1464 struct ath10k_tpc_stats *tpc_stats) 1465 { 1466 spin_lock_bh(&ar->data_lock); 1467 1468 kfree(ar->debug.tpc_stats); 1469 ar->debug.tpc_stats = tpc_stats; 1470 complete(&ar->debug.tpc_complete); 1471 1472 spin_unlock_bh(&ar->data_lock); 1473 } 1474 1475 void 1476 ath10k_debug_tpc_stats_final_process(struct ath10k *ar, 1477 struct ath10k_tpc_stats_final *tpc_stats) 1478 { 1479 spin_lock_bh(&ar->data_lock); 1480 1481 kfree(ar->debug.tpc_stats_final); 1482 ar->debug.tpc_stats_final = tpc_stats; 1483 complete(&ar->debug.tpc_complete); 1484 1485 spin_unlock_bh(&ar->data_lock); 1486 } 1487 1488 static void ath10k_tpc_stats_print(struct ath10k_tpc_stats *tpc_stats, 1489 unsigned int j, char *buf, size_t *len) 1490 { 1491 int i; 1492 size_t buf_len; 1493 static const char table_str[][5] = { "CDD", 1494 "STBC", 1495 "TXBF" }; 1496 static const char pream_str[][6] = { "CCK", 1497 "OFDM", 1498 "HT20", 1499 "HT40", 1500 "VHT20", 1501 "VHT40", 1502 "VHT80", 1503 "HTCUP" }; 1504 1505 buf_len = ATH10K_TPC_CONFIG_BUF_SIZE; 1506 *len += scnprintf(buf + *len, buf_len - *len, 1507 "********************************\n"); 1508 *len += scnprintf(buf + *len, buf_len - *len, 1509 "******************* %s POWER TABLE ****************\n", 1510 table_str[j]); 1511 *len += scnprintf(buf + *len, buf_len - *len, 1512 "********************************\n"); 1513 *len += scnprintf(buf + *len, buf_len - *len, 1514 "No. Preamble Rate_code "); 1515 1516 for (i = 0; i < WMI_TPC_TX_N_CHAIN; i++) 1517 *len += scnprintf(buf + *len, buf_len - *len, 1518 "tpc_value%d ", i); 1519 1520 *len += scnprintf(buf + *len, buf_len - *len, "\n"); 1521 1522 for (i = 0; i < tpc_stats->rate_max; i++) { 1523 *len += scnprintf(buf + *len, buf_len - *len, 1524 "%8d %s 0x%2x %s\n", i, 1525 pream_str[tpc_stats->tpc_table[j].pream_idx[i]], 1526 tpc_stats->tpc_table[j].rate_code[i], 1527 tpc_stats->tpc_table[j].tpc_value[i]); 1528 } 1529 1530 *len += scnprintf(buf + *len, buf_len - *len, 1531 "***********************************\n"); 1532 } 1533 1534 static void ath10k_tpc_stats_fill(struct ath10k *ar, 1535 struct ath10k_tpc_stats *tpc_stats, 1536 char *buf) 1537 { 1538 int j; 1539 size_t len, buf_len; 1540 1541 len = 0; 1542 buf_len = ATH10K_TPC_CONFIG_BUF_SIZE; 1543 1544 spin_lock_bh(&ar->data_lock); 1545 1546 if (!tpc_stats) { 1547 ath10k_warn(ar, "failed to get tpc stats\n"); 1548 goto unlock; 1549 } 1550 1551 len += scnprintf(buf + len, buf_len - len, "\n"); 1552 len += scnprintf(buf + len, buf_len - len, 1553 "*************************************\n"); 1554 len += scnprintf(buf + len, buf_len - len, 1555 "TPC config for channel %4d mode %d\n", 1556 tpc_stats->chan_freq, 1557 tpc_stats->phy_mode); 1558 len += scnprintf(buf + len, buf_len - len, 1559 "*************************************\n"); 1560 len += scnprintf(buf + len, buf_len - len, 1561 "CTL = 0x%2x Reg. Domain = %2d\n", 1562 tpc_stats->ctl, 1563 tpc_stats->reg_domain); 1564 len += scnprintf(buf + len, buf_len - len, 1565 "Antenna Gain = %2d Reg. Max Antenna Gain = %2d\n", 1566 tpc_stats->twice_antenna_gain, 1567 tpc_stats->twice_antenna_reduction); 1568 len += scnprintf(buf + len, buf_len - len, 1569 "Power Limit = %2d Reg. Max Power = %2d\n", 1570 tpc_stats->power_limit, 1571 tpc_stats->twice_max_rd_power / 2); 1572 len += scnprintf(buf + len, buf_len - len, 1573 "Num tx chains = %2d Num supported rates = %2d\n", 1574 tpc_stats->num_tx_chain, 1575 tpc_stats->rate_max); 1576 1577 for (j = 0; j < WMI_TPC_FLAG; j++) { 1578 switch (j) { 1579 case WMI_TPC_TABLE_TYPE_CDD: 1580 if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) { 1581 len += scnprintf(buf + len, buf_len - len, 1582 "CDD not supported\n"); 1583 break; 1584 } 1585 1586 ath10k_tpc_stats_print(tpc_stats, j, buf, &len); 1587 break; 1588 case WMI_TPC_TABLE_TYPE_STBC: 1589 if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) { 1590 len += scnprintf(buf + len, buf_len - len, 1591 "STBC not supported\n"); 1592 break; 1593 } 1594 1595 ath10k_tpc_stats_print(tpc_stats, j, buf, &len); 1596 break; 1597 case WMI_TPC_TABLE_TYPE_TXBF: 1598 if (tpc_stats->flag[j] == ATH10K_TPC_TABLE_TYPE_FLAG) { 1599 len += scnprintf(buf + len, buf_len - len, 1600 "TXBF not supported\n***************************\n"); 1601 break; 1602 } 1603 1604 ath10k_tpc_stats_print(tpc_stats, j, buf, &len); 1605 break; 1606 default: 1607 len += scnprintf(buf + len, buf_len - len, 1608 "Invalid Type\n"); 1609 break; 1610 } 1611 } 1612 1613 unlock: 1614 spin_unlock_bh(&ar->data_lock); 1615 1616 if (len >= buf_len) 1617 buf[len - 1] = 0; 1618 else 1619 buf[len] = 0; 1620 } 1621 1622 static int ath10k_tpc_stats_open(struct inode *inode, struct file *file) 1623 { 1624 struct ath10k *ar = inode->i_private; 1625 void *buf = NULL; 1626 int ret; 1627 1628 mutex_lock(&ar->conf_mutex); 1629 1630 if (ar->state != ATH10K_STATE_ON) { 1631 ret = -ENETDOWN; 1632 goto err_unlock; 1633 } 1634 1635 buf = vmalloc(ATH10K_TPC_CONFIG_BUF_SIZE); 1636 if (!buf) { 1637 ret = -ENOMEM; 1638 goto err_unlock; 1639 } 1640 1641 ret = ath10k_debug_tpc_stats_request(ar); 1642 if (ret) { 1643 ath10k_warn(ar, "failed to request tpc config stats: %d\n", 1644 ret); 1645 goto err_free; 1646 } 1647 1648 ath10k_tpc_stats_fill(ar, ar->debug.tpc_stats, buf); 1649 file->private_data = buf; 1650 1651 mutex_unlock(&ar->conf_mutex); 1652 return 0; 1653 1654 err_free: 1655 vfree(buf); 1656 1657 err_unlock: 1658 mutex_unlock(&ar->conf_mutex); 1659 return ret; 1660 } 1661 1662 static int ath10k_tpc_stats_release(struct inode *inode, struct file *file) 1663 { 1664 vfree(file->private_data); 1665 1666 return 0; 1667 } 1668 1669 static ssize_t ath10k_tpc_stats_read(struct file *file, char __user *user_buf, 1670 size_t count, loff_t *ppos) 1671 { 1672 const char *buf = file->private_data; 1673 size_t len = strlen(buf); 1674 1675 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 1676 } 1677 1678 static const struct file_operations fops_tpc_stats = { 1679 .open = ath10k_tpc_stats_open, 1680 .release = ath10k_tpc_stats_release, 1681 .read = ath10k_tpc_stats_read, 1682 .owner = THIS_MODULE, 1683 .llseek = default_llseek, 1684 }; 1685 1686 int ath10k_debug_start(struct ath10k *ar) 1687 { 1688 int ret; 1689 1690 lockdep_assert_held(&ar->conf_mutex); 1691 1692 ret = ath10k_debug_htt_stats_req(ar); 1693 if (ret) 1694 /* continue normally anyway, this isn't serious */ 1695 ath10k_warn(ar, "failed to start htt stats workqueue: %d\n", 1696 ret); 1697 1698 if (ar->debug.fw_dbglog_mask) { 1699 ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask, 1700 ATH10K_DBGLOG_LEVEL_WARN); 1701 if (ret) 1702 /* not serious */ 1703 ath10k_warn(ar, "failed to enable dbglog during start: %d", 1704 ret); 1705 } 1706 1707 if (ar->pktlog_filter) { 1708 ret = ath10k_wmi_pdev_pktlog_enable(ar, 1709 ar->pktlog_filter); 1710 if (ret) 1711 /* not serious */ 1712 ath10k_warn(ar, 1713 "failed to enable pktlog filter %x: %d\n", 1714 ar->pktlog_filter, ret); 1715 } else { 1716 ret = ath10k_wmi_pdev_pktlog_disable(ar); 1717 if (ret) 1718 /* not serious */ 1719 ath10k_warn(ar, "failed to disable pktlog: %d\n", ret); 1720 } 1721 1722 if (ar->debug.nf_cal_period && 1723 !test_bit(ATH10K_FW_FEATURE_NON_BMI, 1724 ar->normal_mode_fw.fw_file.fw_features)) { 1725 ret = ath10k_wmi_pdev_set_param(ar, 1726 ar->wmi.pdev_param->cal_period, 1727 ar->debug.nf_cal_period); 1728 if (ret) 1729 /* not serious */ 1730 ath10k_warn(ar, "cal period cfg failed from debug start: %d\n", 1731 ret); 1732 } 1733 1734 return ret; 1735 } 1736 1737 void ath10k_debug_stop(struct ath10k *ar) 1738 { 1739 lockdep_assert_held(&ar->conf_mutex); 1740 1741 if (!test_bit(ATH10K_FW_FEATURE_NON_BMI, 1742 ar->normal_mode_fw.fw_file.fw_features)) 1743 ath10k_debug_cal_data_fetch(ar); 1744 1745 /* Must not use _sync to avoid deadlock, we do that in 1746 * ath10k_debug_destroy(). The check for htt_stats_mask is to avoid 1747 * warning from del_timer(). 1748 */ 1749 if (ar->debug.htt_stats_mask != 0) 1750 cancel_delayed_work(&ar->debug.htt_stats_dwork); 1751 1752 ath10k_wmi_pdev_pktlog_disable(ar); 1753 } 1754 1755 static ssize_t ath10k_write_simulate_radar(struct file *file, 1756 const char __user *user_buf, 1757 size_t count, loff_t *ppos) 1758 { 1759 struct ath10k *ar = file->private_data; 1760 struct ath10k_vif *arvif; 1761 1762 /* Just check for for the first vif alone, as all the vifs will be 1763 * sharing the same channel and if the channel is disabled, all the 1764 * vifs will share the same 'is_started' state. 1765 */ 1766 arvif = list_first_entry(&ar->arvifs, typeof(*arvif), list); 1767 if (!arvif->is_started) 1768 return -EINVAL; 1769 1770 ieee80211_radar_detected(ar->hw); 1771 1772 return count; 1773 } 1774 1775 static const struct file_operations fops_simulate_radar = { 1776 .write = ath10k_write_simulate_radar, 1777 .open = simple_open, 1778 .owner = THIS_MODULE, 1779 .llseek = default_llseek, 1780 }; 1781 1782 #define ATH10K_DFS_STAT(s, p) (\ 1783 len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \ 1784 ar->debug.dfs_stats.p)) 1785 1786 #define ATH10K_DFS_POOL_STAT(s, p) (\ 1787 len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \ 1788 ar->debug.dfs_pool_stats.p)) 1789 1790 static ssize_t ath10k_read_dfs_stats(struct file *file, char __user *user_buf, 1791 size_t count, loff_t *ppos) 1792 { 1793 int retval = 0, len = 0; 1794 const int size = 8000; 1795 struct ath10k *ar = file->private_data; 1796 char *buf; 1797 1798 buf = kzalloc(size, GFP_KERNEL); 1799 if (buf == NULL) 1800 return -ENOMEM; 1801 1802 if (!ar->dfs_detector) { 1803 len += scnprintf(buf + len, size - len, "DFS not enabled\n"); 1804 goto exit; 1805 } 1806 1807 ar->debug.dfs_pool_stats = 1808 ar->dfs_detector->get_stats(ar->dfs_detector); 1809 1810 len += scnprintf(buf + len, size - len, "Pulse detector statistics:\n"); 1811 1812 ATH10K_DFS_STAT("reported phy errors", phy_errors); 1813 ATH10K_DFS_STAT("pulse events reported", pulses_total); 1814 ATH10K_DFS_STAT("DFS pulses detected", pulses_detected); 1815 ATH10K_DFS_STAT("DFS pulses discarded", pulses_discarded); 1816 ATH10K_DFS_STAT("Radars detected", radar_detected); 1817 1818 len += scnprintf(buf + len, size - len, "Global Pool statistics:\n"); 1819 ATH10K_DFS_POOL_STAT("Pool references", pool_reference); 1820 ATH10K_DFS_POOL_STAT("Pulses allocated", pulse_allocated); 1821 ATH10K_DFS_POOL_STAT("Pulses alloc error", pulse_alloc_error); 1822 ATH10K_DFS_POOL_STAT("Pulses in use", pulse_used); 1823 ATH10K_DFS_POOL_STAT("Seqs. allocated", pseq_allocated); 1824 ATH10K_DFS_POOL_STAT("Seqs. alloc error", pseq_alloc_error); 1825 ATH10K_DFS_POOL_STAT("Seqs. in use", pseq_used); 1826 1827 exit: 1828 if (len > size) 1829 len = size; 1830 1831 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len); 1832 kfree(buf); 1833 1834 return retval; 1835 } 1836 1837 static const struct file_operations fops_dfs_stats = { 1838 .read = ath10k_read_dfs_stats, 1839 .open = simple_open, 1840 .owner = THIS_MODULE, 1841 .llseek = default_llseek, 1842 }; 1843 1844 static ssize_t ath10k_write_pktlog_filter(struct file *file, 1845 const char __user *ubuf, 1846 size_t count, loff_t *ppos) 1847 { 1848 struct ath10k *ar = file->private_data; 1849 u32 filter; 1850 int ret; 1851 1852 if (kstrtouint_from_user(ubuf, count, 0, &filter)) 1853 return -EINVAL; 1854 1855 mutex_lock(&ar->conf_mutex); 1856 1857 if (ar->state != ATH10K_STATE_ON) { 1858 ar->pktlog_filter = filter; 1859 ret = count; 1860 goto out; 1861 } 1862 1863 if (filter == ar->pktlog_filter) { 1864 ret = count; 1865 goto out; 1866 } 1867 1868 if (filter) { 1869 ret = ath10k_wmi_pdev_pktlog_enable(ar, filter); 1870 if (ret) { 1871 ath10k_warn(ar, "failed to enable pktlog filter %x: %d\n", 1872 ar->pktlog_filter, ret); 1873 goto out; 1874 } 1875 } else { 1876 ret = ath10k_wmi_pdev_pktlog_disable(ar); 1877 if (ret) { 1878 ath10k_warn(ar, "failed to disable pktlog: %d\n", ret); 1879 goto out; 1880 } 1881 } 1882 1883 ar->pktlog_filter = filter; 1884 ret = count; 1885 1886 out: 1887 mutex_unlock(&ar->conf_mutex); 1888 return ret; 1889 } 1890 1891 static ssize_t ath10k_read_pktlog_filter(struct file *file, char __user *ubuf, 1892 size_t count, loff_t *ppos) 1893 { 1894 char buf[32]; 1895 struct ath10k *ar = file->private_data; 1896 int len = 0; 1897 1898 mutex_lock(&ar->conf_mutex); 1899 len = scnprintf(buf, sizeof(buf) - len, "%08x\n", 1900 ar->pktlog_filter); 1901 mutex_unlock(&ar->conf_mutex); 1902 1903 return simple_read_from_buffer(ubuf, count, ppos, buf, len); 1904 } 1905 1906 static const struct file_operations fops_pktlog_filter = { 1907 .read = ath10k_read_pktlog_filter, 1908 .write = ath10k_write_pktlog_filter, 1909 .open = simple_open 1910 }; 1911 1912 static ssize_t ath10k_write_quiet_period(struct file *file, 1913 const char __user *ubuf, 1914 size_t count, loff_t *ppos) 1915 { 1916 struct ath10k *ar = file->private_data; 1917 u32 period; 1918 1919 if (kstrtouint_from_user(ubuf, count, 0, &period)) 1920 return -EINVAL; 1921 1922 if (period < ATH10K_QUIET_PERIOD_MIN) { 1923 ath10k_warn(ar, "Quiet period %u can not be lesser than 25ms\n", 1924 period); 1925 return -EINVAL; 1926 } 1927 mutex_lock(&ar->conf_mutex); 1928 ar->thermal.quiet_period = period; 1929 ath10k_thermal_set_throttling(ar); 1930 mutex_unlock(&ar->conf_mutex); 1931 1932 return count; 1933 } 1934 1935 static ssize_t ath10k_read_quiet_period(struct file *file, char __user *ubuf, 1936 size_t count, loff_t *ppos) 1937 { 1938 char buf[32]; 1939 struct ath10k *ar = file->private_data; 1940 int len = 0; 1941 1942 mutex_lock(&ar->conf_mutex); 1943 len = scnprintf(buf, sizeof(buf) - len, "%d\n", 1944 ar->thermal.quiet_period); 1945 mutex_unlock(&ar->conf_mutex); 1946 1947 return simple_read_from_buffer(ubuf, count, ppos, buf, len); 1948 } 1949 1950 static const struct file_operations fops_quiet_period = { 1951 .read = ath10k_read_quiet_period, 1952 .write = ath10k_write_quiet_period, 1953 .open = simple_open 1954 }; 1955 1956 static ssize_t ath10k_write_btcoex(struct file *file, 1957 const char __user *ubuf, 1958 size_t count, loff_t *ppos) 1959 { 1960 struct ath10k *ar = file->private_data; 1961 char buf[32]; 1962 size_t buf_size; 1963 int ret; 1964 bool val; 1965 u32 pdev_param; 1966 1967 buf_size = min(count, (sizeof(buf) - 1)); 1968 if (copy_from_user(buf, ubuf, buf_size)) 1969 return -EFAULT; 1970 1971 buf[buf_size] = '\0'; 1972 1973 if (strtobool(buf, &val) != 0) 1974 return -EINVAL; 1975 1976 mutex_lock(&ar->conf_mutex); 1977 1978 if (ar->state != ATH10K_STATE_ON && 1979 ar->state != ATH10K_STATE_RESTARTED) { 1980 ret = -ENETDOWN; 1981 goto exit; 1982 } 1983 1984 if (!(test_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags) ^ val)) { 1985 ret = count; 1986 goto exit; 1987 } 1988 1989 pdev_param = ar->wmi.pdev_param->enable_btcoex; 1990 if (test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM, 1991 ar->running_fw->fw_file.fw_features)) { 1992 ret = ath10k_wmi_pdev_set_param(ar, pdev_param, val); 1993 if (ret) { 1994 ath10k_warn(ar, "failed to enable btcoex: %d\n", ret); 1995 ret = count; 1996 goto exit; 1997 } 1998 } else { 1999 ath10k_info(ar, "restarting firmware due to btcoex change"); 2000 queue_work(ar->workqueue, &ar->restart_work); 2001 } 2002 2003 if (val) 2004 set_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags); 2005 else 2006 clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags); 2007 2008 ret = count; 2009 2010 exit: 2011 mutex_unlock(&ar->conf_mutex); 2012 2013 return ret; 2014 } 2015 2016 static ssize_t ath10k_read_btcoex(struct file *file, char __user *ubuf, 2017 size_t count, loff_t *ppos) 2018 { 2019 char buf[32]; 2020 struct ath10k *ar = file->private_data; 2021 int len = 0; 2022 2023 mutex_lock(&ar->conf_mutex); 2024 len = scnprintf(buf, sizeof(buf) - len, "%d\n", 2025 test_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags)); 2026 mutex_unlock(&ar->conf_mutex); 2027 2028 return simple_read_from_buffer(ubuf, count, ppos, buf, len); 2029 } 2030 2031 static const struct file_operations fops_btcoex = { 2032 .read = ath10k_read_btcoex, 2033 .write = ath10k_write_btcoex, 2034 .open = simple_open 2035 }; 2036 2037 static ssize_t ath10k_write_enable_extd_tx_stats(struct file *file, 2038 const char __user *ubuf, 2039 size_t count, loff_t *ppos) 2040 { 2041 struct ath10k *ar = file->private_data; 2042 u32 filter; 2043 int ret; 2044 2045 if (kstrtouint_from_user(ubuf, count, 0, &filter)) 2046 return -EINVAL; 2047 2048 mutex_lock(&ar->conf_mutex); 2049 2050 if (ar->state != ATH10K_STATE_ON) { 2051 ar->debug.enable_extd_tx_stats = filter; 2052 ret = count; 2053 goto out; 2054 } 2055 2056 if (filter == ar->debug.enable_extd_tx_stats) { 2057 ret = count; 2058 goto out; 2059 } 2060 2061 ar->debug.enable_extd_tx_stats = filter; 2062 ret = count; 2063 2064 out: 2065 mutex_unlock(&ar->conf_mutex); 2066 return ret; 2067 } 2068 2069 static ssize_t ath10k_read_enable_extd_tx_stats(struct file *file, 2070 char __user *ubuf, 2071 size_t count, loff_t *ppos) 2072 2073 { 2074 char buf[32]; 2075 struct ath10k *ar = file->private_data; 2076 int len = 0; 2077 2078 mutex_lock(&ar->conf_mutex); 2079 len = scnprintf(buf, sizeof(buf) - len, "%08x\n", 2080 ar->debug.enable_extd_tx_stats); 2081 mutex_unlock(&ar->conf_mutex); 2082 2083 return simple_read_from_buffer(ubuf, count, ppos, buf, len); 2084 } 2085 2086 static const struct file_operations fops_enable_extd_tx_stats = { 2087 .read = ath10k_read_enable_extd_tx_stats, 2088 .write = ath10k_write_enable_extd_tx_stats, 2089 .open = simple_open 2090 }; 2091 2092 static ssize_t ath10k_write_peer_stats(struct file *file, 2093 const char __user *ubuf, 2094 size_t count, loff_t *ppos) 2095 { 2096 struct ath10k *ar = file->private_data; 2097 char buf[32]; 2098 size_t buf_size; 2099 int ret; 2100 bool val; 2101 2102 buf_size = min(count, (sizeof(buf) - 1)); 2103 if (copy_from_user(buf, ubuf, buf_size)) 2104 return -EFAULT; 2105 2106 buf[buf_size] = '\0'; 2107 2108 if (strtobool(buf, &val) != 0) 2109 return -EINVAL; 2110 2111 mutex_lock(&ar->conf_mutex); 2112 2113 if (ar->state != ATH10K_STATE_ON && 2114 ar->state != ATH10K_STATE_RESTARTED) { 2115 ret = -ENETDOWN; 2116 goto exit; 2117 } 2118 2119 if (!(test_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags) ^ val)) { 2120 ret = count; 2121 goto exit; 2122 } 2123 2124 if (val) 2125 set_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags); 2126 else 2127 clear_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags); 2128 2129 ath10k_info(ar, "restarting firmware due to Peer stats change"); 2130 2131 queue_work(ar->workqueue, &ar->restart_work); 2132 ret = count; 2133 2134 exit: 2135 mutex_unlock(&ar->conf_mutex); 2136 return ret; 2137 } 2138 2139 static ssize_t ath10k_read_peer_stats(struct file *file, char __user *ubuf, 2140 size_t count, loff_t *ppos) 2141 2142 { 2143 char buf[32]; 2144 struct ath10k *ar = file->private_data; 2145 int len = 0; 2146 2147 mutex_lock(&ar->conf_mutex); 2148 len = scnprintf(buf, sizeof(buf) - len, "%d\n", 2149 test_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags)); 2150 mutex_unlock(&ar->conf_mutex); 2151 2152 return simple_read_from_buffer(ubuf, count, ppos, buf, len); 2153 } 2154 2155 static const struct file_operations fops_peer_stats = { 2156 .read = ath10k_read_peer_stats, 2157 .write = ath10k_write_peer_stats, 2158 .open = simple_open 2159 }; 2160 2161 static ssize_t ath10k_debug_fw_checksums_read(struct file *file, 2162 char __user *user_buf, 2163 size_t count, loff_t *ppos) 2164 { 2165 struct ath10k *ar = file->private_data; 2166 size_t len = 0, buf_len = 4096; 2167 ssize_t ret_cnt; 2168 char *buf; 2169 2170 buf = kzalloc(buf_len, GFP_KERNEL); 2171 if (!buf) 2172 return -ENOMEM; 2173 2174 mutex_lock(&ar->conf_mutex); 2175 2176 len += scnprintf(buf + len, buf_len - len, 2177 "firmware-N.bin\t\t%08x\n", 2178 crc32_le(0, ar->normal_mode_fw.fw_file.firmware->data, 2179 ar->normal_mode_fw.fw_file.firmware->size)); 2180 len += scnprintf(buf + len, buf_len - len, 2181 "athwlan\t\t\t%08x\n", 2182 crc32_le(0, ar->normal_mode_fw.fw_file.firmware_data, 2183 ar->normal_mode_fw.fw_file.firmware_len)); 2184 len += scnprintf(buf + len, buf_len - len, 2185 "otp\t\t\t%08x\n", 2186 crc32_le(0, ar->normal_mode_fw.fw_file.otp_data, 2187 ar->normal_mode_fw.fw_file.otp_len)); 2188 len += scnprintf(buf + len, buf_len - len, 2189 "codeswap\t\t%08x\n", 2190 crc32_le(0, ar->normal_mode_fw.fw_file.codeswap_data, 2191 ar->normal_mode_fw.fw_file.codeswap_len)); 2192 len += scnprintf(buf + len, buf_len - len, 2193 "board-N.bin\t\t%08x\n", 2194 crc32_le(0, ar->normal_mode_fw.board->data, 2195 ar->normal_mode_fw.board->size)); 2196 len += scnprintf(buf + len, buf_len - len, 2197 "board\t\t\t%08x\n", 2198 crc32_le(0, ar->normal_mode_fw.board_data, 2199 ar->normal_mode_fw.board_len)); 2200 2201 ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len); 2202 2203 mutex_unlock(&ar->conf_mutex); 2204 2205 kfree(buf); 2206 return ret_cnt; 2207 } 2208 2209 static const struct file_operations fops_fw_checksums = { 2210 .read = ath10k_debug_fw_checksums_read, 2211 .open = simple_open, 2212 .owner = THIS_MODULE, 2213 .llseek = default_llseek, 2214 }; 2215 2216 static ssize_t ath10k_sta_tid_stats_mask_read(struct file *file, 2217 char __user *user_buf, 2218 size_t count, loff_t *ppos) 2219 { 2220 struct ath10k *ar = file->private_data; 2221 char buf[32]; 2222 size_t len; 2223 2224 len = scnprintf(buf, sizeof(buf), "0x%08x\n", ar->sta_tid_stats_mask); 2225 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 2226 } 2227 2228 static ssize_t ath10k_sta_tid_stats_mask_write(struct file *file, 2229 const char __user *user_buf, 2230 size_t count, loff_t *ppos) 2231 { 2232 struct ath10k *ar = file->private_data; 2233 char buf[32]; 2234 ssize_t len; 2235 u32 mask; 2236 2237 len = min(count, sizeof(buf) - 1); 2238 if (copy_from_user(buf, user_buf, len)) 2239 return -EFAULT; 2240 2241 buf[len] = '\0'; 2242 if (kstrtoint(buf, 0, &mask)) 2243 return -EINVAL; 2244 2245 ar->sta_tid_stats_mask = mask; 2246 2247 return len; 2248 } 2249 2250 static const struct file_operations fops_sta_tid_stats_mask = { 2251 .read = ath10k_sta_tid_stats_mask_read, 2252 .write = ath10k_sta_tid_stats_mask_write, 2253 .open = simple_open, 2254 .owner = THIS_MODULE, 2255 .llseek = default_llseek, 2256 }; 2257 2258 static int ath10k_debug_tpc_stats_final_request(struct ath10k *ar) 2259 { 2260 int ret; 2261 unsigned long time_left; 2262 2263 lockdep_assert_held(&ar->conf_mutex); 2264 2265 reinit_completion(&ar->debug.tpc_complete); 2266 2267 ret = ath10k_wmi_pdev_get_tpc_table_cmdid(ar, WMI_TPC_CONFIG_PARAM); 2268 if (ret) { 2269 ath10k_warn(ar, "failed to request tpc table cmdid: %d\n", ret); 2270 return ret; 2271 } 2272 2273 time_left = wait_for_completion_timeout(&ar->debug.tpc_complete, 2274 1 * HZ); 2275 if (time_left == 0) 2276 return -ETIMEDOUT; 2277 2278 return 0; 2279 } 2280 2281 static int ath10k_tpc_stats_final_open(struct inode *inode, struct file *file) 2282 { 2283 struct ath10k *ar = inode->i_private; 2284 void *buf; 2285 int ret; 2286 2287 mutex_lock(&ar->conf_mutex); 2288 2289 if (ar->state != ATH10K_STATE_ON) { 2290 ret = -ENETDOWN; 2291 goto err_unlock; 2292 } 2293 2294 buf = vmalloc(ATH10K_TPC_CONFIG_BUF_SIZE); 2295 if (!buf) { 2296 ret = -ENOMEM; 2297 goto err_unlock; 2298 } 2299 2300 ret = ath10k_debug_tpc_stats_final_request(ar); 2301 if (ret) { 2302 ath10k_warn(ar, "failed to request tpc stats final: %d\n", 2303 ret); 2304 goto err_free; 2305 } 2306 2307 ath10k_tpc_stats_fill(ar, ar->debug.tpc_stats, buf); 2308 file->private_data = buf; 2309 2310 mutex_unlock(&ar->conf_mutex); 2311 return 0; 2312 2313 err_free: 2314 vfree(buf); 2315 2316 err_unlock: 2317 mutex_unlock(&ar->conf_mutex); 2318 return ret; 2319 } 2320 2321 static int ath10k_tpc_stats_final_release(struct inode *inode, 2322 struct file *file) 2323 { 2324 vfree(file->private_data); 2325 2326 return 0; 2327 } 2328 2329 static ssize_t ath10k_tpc_stats_final_read(struct file *file, 2330 char __user *user_buf, 2331 size_t count, loff_t *ppos) 2332 { 2333 const char *buf = file->private_data; 2334 unsigned int len = strlen(buf); 2335 2336 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 2337 } 2338 2339 static const struct file_operations fops_tpc_stats_final = { 2340 .open = ath10k_tpc_stats_final_open, 2341 .release = ath10k_tpc_stats_final_release, 2342 .read = ath10k_tpc_stats_final_read, 2343 .owner = THIS_MODULE, 2344 .llseek = default_llseek, 2345 }; 2346 2347 static ssize_t ath10k_write_warm_hw_reset(struct file *file, 2348 const char __user *user_buf, 2349 size_t count, loff_t *ppos) 2350 { 2351 struct ath10k *ar = file->private_data; 2352 int ret; 2353 bool val; 2354 2355 if (kstrtobool_from_user(user_buf, count, &val)) 2356 return -EFAULT; 2357 2358 if (!val) 2359 return -EINVAL; 2360 2361 mutex_lock(&ar->conf_mutex); 2362 2363 if (ar->state != ATH10K_STATE_ON) { 2364 ret = -ENETDOWN; 2365 goto exit; 2366 } 2367 2368 if (!(test_bit(WMI_SERVICE_RESET_CHIP, ar->wmi.svc_map))) 2369 ath10k_warn(ar, "wmi service for reset chip is not available\n"); 2370 2371 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pdev_reset, 2372 WMI_RST_MODE_WARM_RESET); 2373 2374 if (ret) { 2375 ath10k_warn(ar, "failed to enable warm hw reset: %d\n", ret); 2376 goto exit; 2377 } 2378 2379 ret = count; 2380 2381 exit: 2382 mutex_unlock(&ar->conf_mutex); 2383 return ret; 2384 } 2385 2386 static const struct file_operations fops_warm_hw_reset = { 2387 .write = ath10k_write_warm_hw_reset, 2388 .open = simple_open, 2389 .owner = THIS_MODULE, 2390 .llseek = default_llseek, 2391 }; 2392 2393 static void ath10k_peer_ps_state_disable(void *data, 2394 struct ieee80211_sta *sta) 2395 { 2396 struct ath10k *ar = data; 2397 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv; 2398 2399 spin_lock_bh(&ar->data_lock); 2400 arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED; 2401 spin_unlock_bh(&ar->data_lock); 2402 } 2403 2404 static ssize_t ath10k_write_ps_state_enable(struct file *file, 2405 const char __user *user_buf, 2406 size_t count, loff_t *ppos) 2407 { 2408 struct ath10k *ar = file->private_data; 2409 int ret; 2410 u32 param; 2411 u8 ps_state_enable; 2412 2413 if (kstrtou8_from_user(user_buf, count, 0, &ps_state_enable)) 2414 return -EINVAL; 2415 2416 if (ps_state_enable > 1) 2417 return -EINVAL; 2418 2419 mutex_lock(&ar->conf_mutex); 2420 2421 if (ar->ps_state_enable == ps_state_enable) { 2422 ret = count; 2423 goto exit; 2424 } 2425 2426 param = ar->wmi.pdev_param->peer_sta_ps_statechg_enable; 2427 ret = ath10k_wmi_pdev_set_param(ar, param, ps_state_enable); 2428 if (ret) { 2429 ath10k_warn(ar, "failed to enable ps_state_enable: %d\n", 2430 ret); 2431 goto exit; 2432 } 2433 ar->ps_state_enable = ps_state_enable; 2434 2435 if (!ar->ps_state_enable) 2436 ieee80211_iterate_stations_atomic(ar->hw, 2437 ath10k_peer_ps_state_disable, 2438 ar); 2439 2440 ret = count; 2441 2442 exit: 2443 mutex_unlock(&ar->conf_mutex); 2444 2445 return ret; 2446 } 2447 2448 static ssize_t ath10k_read_ps_state_enable(struct file *file, 2449 char __user *user_buf, 2450 size_t count, loff_t *ppos) 2451 { 2452 struct ath10k *ar = file->private_data; 2453 int len = 0; 2454 char buf[32]; 2455 2456 mutex_lock(&ar->conf_mutex); 2457 len = scnprintf(buf, sizeof(buf) - len, "%d\n", 2458 ar->ps_state_enable); 2459 mutex_unlock(&ar->conf_mutex); 2460 2461 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 2462 } 2463 2464 static const struct file_operations fops_ps_state_enable = { 2465 .read = ath10k_read_ps_state_enable, 2466 .write = ath10k_write_ps_state_enable, 2467 .open = simple_open, 2468 .owner = THIS_MODULE, 2469 .llseek = default_llseek, 2470 }; 2471 2472 int ath10k_debug_create(struct ath10k *ar) 2473 { 2474 ar->debug.cal_data = vzalloc(ATH10K_DEBUG_CAL_DATA_LEN); 2475 if (!ar->debug.cal_data) 2476 return -ENOMEM; 2477 2478 INIT_LIST_HEAD(&ar->debug.fw_stats.pdevs); 2479 INIT_LIST_HEAD(&ar->debug.fw_stats.vdevs); 2480 INIT_LIST_HEAD(&ar->debug.fw_stats.peers); 2481 INIT_LIST_HEAD(&ar->debug.fw_stats.peers_extd); 2482 2483 return 0; 2484 } 2485 2486 void ath10k_debug_destroy(struct ath10k *ar) 2487 { 2488 vfree(ar->debug.cal_data); 2489 ar->debug.cal_data = NULL; 2490 2491 ath10k_debug_fw_stats_reset(ar); 2492 2493 kfree(ar->debug.tpc_stats); 2494 } 2495 2496 int ath10k_debug_register(struct ath10k *ar) 2497 { 2498 ar->debug.debugfs_phy = debugfs_create_dir("ath10k", 2499 ar->hw->wiphy->debugfsdir); 2500 if (IS_ERR_OR_NULL(ar->debug.debugfs_phy)) { 2501 if (IS_ERR(ar->debug.debugfs_phy)) 2502 return PTR_ERR(ar->debug.debugfs_phy); 2503 2504 return -ENOMEM; 2505 } 2506 2507 INIT_DELAYED_WORK(&ar->debug.htt_stats_dwork, 2508 ath10k_debug_htt_stats_dwork); 2509 2510 init_completion(&ar->debug.tpc_complete); 2511 init_completion(&ar->debug.fw_stats_complete); 2512 2513 debugfs_create_file("fw_stats", 0400, ar->debug.debugfs_phy, ar, 2514 &fops_fw_stats); 2515 2516 debugfs_create_file("fw_reset_stats", 0400, ar->debug.debugfs_phy, ar, 2517 &fops_fw_reset_stats); 2518 2519 debugfs_create_file("wmi_services", 0400, ar->debug.debugfs_phy, ar, 2520 &fops_wmi_services); 2521 2522 debugfs_create_file("simulate_fw_crash", 0600, ar->debug.debugfs_phy, ar, 2523 &fops_simulate_fw_crash); 2524 2525 debugfs_create_file("reg_addr", 0600, ar->debug.debugfs_phy, ar, 2526 &fops_reg_addr); 2527 2528 debugfs_create_file("reg_value", 0600, ar->debug.debugfs_phy, ar, 2529 &fops_reg_value); 2530 2531 debugfs_create_file("mem_value", 0600, ar->debug.debugfs_phy, ar, 2532 &fops_mem_value); 2533 2534 debugfs_create_file("chip_id", 0400, ar->debug.debugfs_phy, ar, 2535 &fops_chip_id); 2536 2537 debugfs_create_file("htt_stats_mask", 0600, ar->debug.debugfs_phy, ar, 2538 &fops_htt_stats_mask); 2539 2540 debugfs_create_file("htt_max_amsdu_ampdu", 0600, ar->debug.debugfs_phy, ar, 2541 &fops_htt_max_amsdu_ampdu); 2542 2543 debugfs_create_file("fw_dbglog", 0600, ar->debug.debugfs_phy, ar, 2544 &fops_fw_dbglog); 2545 2546 if (!test_bit(ATH10K_FW_FEATURE_NON_BMI, 2547 ar->normal_mode_fw.fw_file.fw_features)) { 2548 debugfs_create_file("cal_data", 0400, ar->debug.debugfs_phy, ar, 2549 &fops_cal_data); 2550 2551 debugfs_create_file("nf_cal_period", 0600, ar->debug.debugfs_phy, ar, 2552 &fops_nf_cal_period); 2553 } 2554 2555 debugfs_create_file("ani_enable", 0600, ar->debug.debugfs_phy, ar, 2556 &fops_ani_enable); 2557 2558 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) { 2559 debugfs_create_file("dfs_simulate_radar", 0200, ar->debug.debugfs_phy, 2560 ar, &fops_simulate_radar); 2561 2562 debugfs_create_bool("dfs_block_radar_events", 0200, 2563 ar->debug.debugfs_phy, 2564 &ar->dfs_block_radar_events); 2565 2566 debugfs_create_file("dfs_stats", 0400, ar->debug.debugfs_phy, ar, 2567 &fops_dfs_stats); 2568 } 2569 2570 debugfs_create_file("pktlog_filter", 0644, ar->debug.debugfs_phy, ar, 2571 &fops_pktlog_filter); 2572 2573 if (test_bit(WMI_SERVICE_THERM_THROT, ar->wmi.svc_map)) 2574 debugfs_create_file("quiet_period", 0644, ar->debug.debugfs_phy, ar, 2575 &fops_quiet_period); 2576 2577 debugfs_create_file("tpc_stats", 0400, ar->debug.debugfs_phy, ar, 2578 &fops_tpc_stats); 2579 2580 if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map)) 2581 debugfs_create_file("btcoex", 0644, ar->debug.debugfs_phy, ar, 2582 &fops_btcoex); 2583 2584 if (test_bit(WMI_SERVICE_PEER_STATS, ar->wmi.svc_map)) { 2585 debugfs_create_file("peer_stats", 0644, ar->debug.debugfs_phy, ar, 2586 &fops_peer_stats); 2587 2588 debugfs_create_file("enable_extd_tx_stats", 0644, 2589 ar->debug.debugfs_phy, ar, 2590 &fops_enable_extd_tx_stats); 2591 } 2592 2593 debugfs_create_file("fw_checksums", 0400, ar->debug.debugfs_phy, ar, 2594 &fops_fw_checksums); 2595 2596 if (IS_ENABLED(CONFIG_MAC80211_DEBUGFS)) 2597 debugfs_create_file("sta_tid_stats_mask", 0600, 2598 ar->debug.debugfs_phy, 2599 ar, &fops_sta_tid_stats_mask); 2600 2601 if (test_bit(WMI_SERVICE_TPC_STATS_FINAL, ar->wmi.svc_map)) 2602 debugfs_create_file("tpc_stats_final", 0400, 2603 ar->debug.debugfs_phy, ar, 2604 &fops_tpc_stats_final); 2605 2606 debugfs_create_file("warm_hw_reset", 0600, ar->debug.debugfs_phy, ar, 2607 &fops_warm_hw_reset); 2608 2609 debugfs_create_file("ps_state_enable", 0600, ar->debug.debugfs_phy, ar, 2610 &fops_ps_state_enable); 2611 2612 return 0; 2613 } 2614 2615 void ath10k_debug_unregister(struct ath10k *ar) 2616 { 2617 cancel_delayed_work_sync(&ar->debug.htt_stats_dwork); 2618 } 2619 2620 #endif /* CONFIG_ATH10K_DEBUGFS */ 2621 2622 #ifdef CONFIG_ATH10K_DEBUG 2623 void ath10k_dbg(struct ath10k *ar, enum ath10k_debug_mask mask, 2624 const char *fmt, ...) 2625 { 2626 struct va_format vaf; 2627 va_list args; 2628 2629 va_start(args, fmt); 2630 2631 vaf.fmt = fmt; 2632 vaf.va = &args; 2633 2634 if (ath10k_debug_mask & mask) 2635 dev_printk(KERN_DEBUG, ar->dev, "%pV", &vaf); 2636 2637 trace_ath10k_log_dbg(ar, mask, &vaf); 2638 2639 va_end(args); 2640 } 2641 EXPORT_SYMBOL(ath10k_dbg); 2642 2643 void ath10k_dbg_dump(struct ath10k *ar, 2644 enum ath10k_debug_mask mask, 2645 const char *msg, const char *prefix, 2646 const void *buf, size_t len) 2647 { 2648 char linebuf[256]; 2649 size_t linebuflen; 2650 const void *ptr; 2651 2652 if (ath10k_debug_mask & mask) { 2653 if (msg) 2654 ath10k_dbg(ar, mask, "%s\n", msg); 2655 2656 for (ptr = buf; (ptr - buf) < len; ptr += 16) { 2657 linebuflen = 0; 2658 linebuflen += scnprintf(linebuf + linebuflen, 2659 sizeof(linebuf) - linebuflen, 2660 "%s%08x: ", 2661 (prefix ? prefix : ""), 2662 (unsigned int)(ptr - buf)); 2663 hex_dump_to_buffer(ptr, len - (ptr - buf), 16, 1, 2664 linebuf + linebuflen, 2665 sizeof(linebuf) - linebuflen, true); 2666 dev_printk(KERN_DEBUG, ar->dev, "%s\n", linebuf); 2667 } 2668 } 2669 2670 /* tracing code doesn't like null strings :/ */ 2671 trace_ath10k_log_dbg_dump(ar, msg ? msg : "", prefix ? prefix : "", 2672 buf, len); 2673 } 2674 EXPORT_SYMBOL(ath10k_dbg_dump); 2675 2676 #endif /* CONFIG_ATH10K_DEBUG */ 2677