wil_crash_dump.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2015,2017 Qualcomm Atheros, Inc.
  3. * Copyright (c) 2018, The Linux Foundation. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "wil6210.h"
  18. #include <linux/devcoredump.h>
  19. static int wil_fw_get_crash_dump_bounds(struct wil6210_priv *wil,
  20. u32 *out_dump_size, u32 *out_host_min)
  21. {
  22. int i;
  23. const struct fw_map *map;
  24. u32 host_min, host_max, tmp_max;
  25. if (!out_dump_size)
  26. return -EINVAL;
  27. /* calculate the total size of the unpacked crash dump */
  28. BUILD_BUG_ON(ARRAY_SIZE(fw_mapping) == 0);
  29. map = &fw_mapping[0];
  30. host_min = map->host;
  31. host_max = map->host + (map->to - map->from);
  32. for (i = 1; i < ARRAY_SIZE(fw_mapping); i++) {
  33. map = &fw_mapping[i];
  34. if (!map->crash_dump)
  35. continue;
  36. if (map->host < host_min)
  37. host_min = map->host;
  38. tmp_max = map->host + (map->to - map->from);
  39. if (tmp_max > host_max)
  40. host_max = tmp_max;
  41. }
  42. *out_dump_size = host_max - host_min;
  43. if (out_host_min)
  44. *out_host_min = host_min;
  45. return 0;
  46. }
  47. int wil_fw_copy_crash_dump(struct wil6210_priv *wil, void *dest, u32 size)
  48. {
  49. int i;
  50. const struct fw_map *map;
  51. void *data;
  52. u32 host_min, dump_size, offset, len;
  53. if (wil_fw_get_crash_dump_bounds(wil, &dump_size, &host_min)) {
  54. wil_err(wil, "fail to obtain crash dump size\n");
  55. return -EINVAL;
  56. }
  57. if (dump_size > size) {
  58. wil_err(wil, "not enough space for dump. Need %d have %d\n",
  59. dump_size, size);
  60. return -EINVAL;
  61. }
  62. set_bit(wil_status_collecting_dumps, wil->status);
  63. if (test_bit(wil_status_suspending, wil->status) ||
  64. test_bit(wil_status_suspended, wil->status) ||
  65. test_bit(wil_status_resetting, wil->status)) {
  66. wil_err(wil, "cannot collect fw dump during suspend/reset\n");
  67. clear_bit(wil_status_collecting_dumps, wil->status);
  68. return -EINVAL;
  69. }
  70. /* copy to crash dump area */
  71. for (i = 0; i < ARRAY_SIZE(fw_mapping); i++) {
  72. map = &fw_mapping[i];
  73. if (!map->crash_dump)
  74. continue;
  75. data = (void * __force)wil->csr + HOSTADDR(map->host);
  76. len = map->to - map->from;
  77. offset = map->host - host_min;
  78. wil_dbg_misc(wil,
  79. "fw_copy_crash_dump: - dump %s, size %d, offset %d\n",
  80. fw_mapping[i].name, len, offset);
  81. wil_memcpy_fromio_32((void * __force)(dest + offset),
  82. (const void __iomem * __force)data, len);
  83. }
  84. clear_bit(wil_status_collecting_dumps, wil->status);
  85. return 0;
  86. }
  87. void wil_fw_core_dump(struct wil6210_priv *wil)
  88. {
  89. void *fw_dump_data;
  90. u32 fw_dump_size;
  91. if (wil_fw_get_crash_dump_bounds(wil, &fw_dump_size, NULL)) {
  92. wil_err(wil, "fail to get fw dump size\n");
  93. return;
  94. }
  95. fw_dump_data = vzalloc(fw_dump_size);
  96. if (!fw_dump_data)
  97. return;
  98. if (wil_fw_copy_crash_dump(wil, fw_dump_data, fw_dump_size)) {
  99. vfree(fw_dump_data);
  100. return;
  101. }
  102. /* fw_dump_data will be free in device coredump release function
  103. * after 5 min
  104. */
  105. dev_coredumpv(wil_to_dev(wil), fw_dump_data, fw_dump_size, GFP_KERNEL);
  106. wil_info(wil, "fw core dumped, size %d bytes\n", fw_dump_size);
  107. }