crdump.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2018, Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include "mlx4.h"
  33. #define BAD_ACCESS 0xBADACCE5
  34. #define HEALTH_BUFFER_SIZE 0x40
  35. #define CR_ENABLE_BIT swab32(BIT(6))
  36. #define CR_ENABLE_BIT_OFFSET 0xF3F04
  37. #define MAX_NUM_OF_DUMPS_TO_STORE (8)
  38. static const char *region_cr_space_str = "cr-space";
  39. static const char *region_fw_health_str = "fw-health";
  40. /* Set to true in case cr enable bit was set to true before crdump */
  41. static bool crdump_enbale_bit_set;
  42. static void crdump_enable_crspace_access(struct mlx4_dev *dev,
  43. u8 __iomem *cr_space)
  44. {
  45. /* Get current enable bit value */
  46. crdump_enbale_bit_set =
  47. readl(cr_space + CR_ENABLE_BIT_OFFSET) & CR_ENABLE_BIT;
  48. /* Enable FW CR filter (set bit6 to 0) */
  49. if (crdump_enbale_bit_set)
  50. writel(readl(cr_space + CR_ENABLE_BIT_OFFSET) & ~CR_ENABLE_BIT,
  51. cr_space + CR_ENABLE_BIT_OFFSET);
  52. /* Enable block volatile crspace accesses */
  53. writel(swab32(1), cr_space + dev->caps.health_buffer_addrs +
  54. HEALTH_BUFFER_SIZE);
  55. }
  56. static void crdump_disable_crspace_access(struct mlx4_dev *dev,
  57. u8 __iomem *cr_space)
  58. {
  59. /* Disable block volatile crspace accesses */
  60. writel(0, cr_space + dev->caps.health_buffer_addrs +
  61. HEALTH_BUFFER_SIZE);
  62. /* Restore FW CR filter value (set bit6 to original value) */
  63. if (crdump_enbale_bit_set)
  64. writel(readl(cr_space + CR_ENABLE_BIT_OFFSET) | CR_ENABLE_BIT,
  65. cr_space + CR_ENABLE_BIT_OFFSET);
  66. }
  67. static void mlx4_crdump_collect_crspace(struct mlx4_dev *dev,
  68. u8 __iomem *cr_space,
  69. u32 id)
  70. {
  71. struct mlx4_fw_crdump *crdump = &dev->persist->crdump;
  72. struct pci_dev *pdev = dev->persist->pdev;
  73. unsigned long cr_res_size;
  74. u8 *crspace_data;
  75. int offset;
  76. int err;
  77. if (!crdump->region_crspace) {
  78. mlx4_err(dev, "crdump: cr-space region is NULL\n");
  79. return;
  80. }
  81. /* Try to collect CR space */
  82. cr_res_size = pci_resource_len(pdev, 0);
  83. crspace_data = kvmalloc(cr_res_size, GFP_KERNEL);
  84. if (crspace_data) {
  85. for (offset = 0; offset < cr_res_size; offset += 4)
  86. *(u32 *)(crspace_data + offset) =
  87. readl(cr_space + offset);
  88. err = devlink_region_snapshot_create(crdump->region_crspace,
  89. cr_res_size, crspace_data,
  90. id, &kvfree);
  91. if (err) {
  92. kvfree(crspace_data);
  93. mlx4_warn(dev, "crdump: devlink create %s snapshot id %d err %d\n",
  94. region_cr_space_str, id, err);
  95. } else {
  96. mlx4_info(dev, "crdump: added snapshot %d to devlink region %s\n",
  97. id, region_cr_space_str);
  98. }
  99. } else {
  100. mlx4_err(dev, "crdump: Failed to allocate crspace buffer\n");
  101. }
  102. }
  103. static void mlx4_crdump_collect_fw_health(struct mlx4_dev *dev,
  104. u8 __iomem *cr_space,
  105. u32 id)
  106. {
  107. struct mlx4_fw_crdump *crdump = &dev->persist->crdump;
  108. u8 *health_data;
  109. int offset;
  110. int err;
  111. if (!crdump->region_fw_health) {
  112. mlx4_err(dev, "crdump: fw-health region is NULL\n");
  113. return;
  114. }
  115. /* Try to collect health buffer */
  116. health_data = kvmalloc(HEALTH_BUFFER_SIZE, GFP_KERNEL);
  117. if (health_data) {
  118. u8 __iomem *health_buf_start =
  119. cr_space + dev->caps.health_buffer_addrs;
  120. for (offset = 0; offset < HEALTH_BUFFER_SIZE; offset += 4)
  121. *(u32 *)(health_data + offset) =
  122. readl(health_buf_start + offset);
  123. err = devlink_region_snapshot_create(crdump->region_fw_health,
  124. HEALTH_BUFFER_SIZE,
  125. health_data,
  126. id, &kvfree);
  127. if (err) {
  128. kvfree(health_data);
  129. mlx4_warn(dev, "crdump: devlink create %s snapshot id %d err %d\n",
  130. region_fw_health_str, id, err);
  131. } else {
  132. mlx4_info(dev, "crdump: added snapshot %d to devlink region %s\n",
  133. id, region_fw_health_str);
  134. }
  135. } else {
  136. mlx4_err(dev, "crdump: Failed to allocate health buffer\n");
  137. }
  138. }
  139. int mlx4_crdump_collect(struct mlx4_dev *dev)
  140. {
  141. struct devlink *devlink = priv_to_devlink(mlx4_priv(dev));
  142. struct mlx4_fw_crdump *crdump = &dev->persist->crdump;
  143. struct pci_dev *pdev = dev->persist->pdev;
  144. unsigned long cr_res_size;
  145. u8 __iomem *cr_space;
  146. u32 id;
  147. if (!dev->caps.health_buffer_addrs) {
  148. mlx4_info(dev, "crdump: FW doesn't support health buffer access, skipping\n");
  149. return 0;
  150. }
  151. if (!crdump->snapshot_enable) {
  152. mlx4_info(dev, "crdump: devlink snapshot disabled, skipping\n");
  153. return 0;
  154. }
  155. cr_res_size = pci_resource_len(pdev, 0);
  156. cr_space = ioremap(pci_resource_start(pdev, 0), cr_res_size);
  157. if (!cr_space) {
  158. mlx4_err(dev, "crdump: Failed to map pci cr region\n");
  159. return -ENODEV;
  160. }
  161. crdump_enable_crspace_access(dev, cr_space);
  162. /* Get the available snapshot ID for the dumps */
  163. id = devlink_region_shapshot_id_get(devlink);
  164. /* Try to capture dumps */
  165. mlx4_crdump_collect_crspace(dev, cr_space, id);
  166. mlx4_crdump_collect_fw_health(dev, cr_space, id);
  167. crdump_disable_crspace_access(dev, cr_space);
  168. iounmap(cr_space);
  169. return 0;
  170. }
  171. int mlx4_crdump_init(struct mlx4_dev *dev)
  172. {
  173. struct devlink *devlink = priv_to_devlink(mlx4_priv(dev));
  174. struct mlx4_fw_crdump *crdump = &dev->persist->crdump;
  175. struct pci_dev *pdev = dev->persist->pdev;
  176. crdump->snapshot_enable = false;
  177. /* Create cr-space region */
  178. crdump->region_crspace =
  179. devlink_region_create(devlink,
  180. region_cr_space_str,
  181. MAX_NUM_OF_DUMPS_TO_STORE,
  182. pci_resource_len(pdev, 0));
  183. if (IS_ERR(crdump->region_crspace))
  184. mlx4_warn(dev, "crdump: create devlink region %s err %ld\n",
  185. region_cr_space_str,
  186. PTR_ERR(crdump->region_crspace));
  187. /* Create fw-health region */
  188. crdump->region_fw_health =
  189. devlink_region_create(devlink,
  190. region_fw_health_str,
  191. MAX_NUM_OF_DUMPS_TO_STORE,
  192. HEALTH_BUFFER_SIZE);
  193. if (IS_ERR(crdump->region_fw_health))
  194. mlx4_warn(dev, "crdump: create devlink region %s err %ld\n",
  195. region_fw_health_str,
  196. PTR_ERR(crdump->region_fw_health));
  197. return 0;
  198. }
  199. void mlx4_crdump_end(struct mlx4_dev *dev)
  200. {
  201. struct mlx4_fw_crdump *crdump = &dev->persist->crdump;
  202. devlink_region_destroy(crdump->region_fw_health);
  203. devlink_region_destroy(crdump->region_crspace);
  204. }