wil_crash_dump.c 3.5 KB

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