qed_selftest.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* QLogic qed NIC Driver
  2. * Copyright (c) 2015-2016 QLogic Corporation
  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 <linux/crc32.h>
  33. #include "qed.h"
  34. #include "qed_dev_api.h"
  35. #include "qed_mcp.h"
  36. #include "qed_sp.h"
  37. #include "qed_selftest.h"
  38. int qed_selftest_memory(struct qed_dev *cdev)
  39. {
  40. int rc = 0, i;
  41. for_each_hwfn(cdev, i) {
  42. rc = qed_sp_heartbeat_ramrod(&cdev->hwfns[i]);
  43. if (rc)
  44. return rc;
  45. }
  46. return rc;
  47. }
  48. int qed_selftest_interrupt(struct qed_dev *cdev)
  49. {
  50. int rc = 0, i;
  51. for_each_hwfn(cdev, i) {
  52. rc = qed_sp_heartbeat_ramrod(&cdev->hwfns[i]);
  53. if (rc)
  54. return rc;
  55. }
  56. return rc;
  57. }
  58. int qed_selftest_register(struct qed_dev *cdev)
  59. {
  60. struct qed_hwfn *p_hwfn;
  61. struct qed_ptt *p_ptt;
  62. int rc = 0, i;
  63. /* although performed by MCP, this test is per engine */
  64. for_each_hwfn(cdev, i) {
  65. p_hwfn = &cdev->hwfns[i];
  66. p_ptt = qed_ptt_acquire(p_hwfn);
  67. if (!p_ptt) {
  68. DP_ERR(p_hwfn, "failed to acquire ptt\n");
  69. return -EBUSY;
  70. }
  71. rc = qed_mcp_bist_register_test(p_hwfn, p_ptt);
  72. qed_ptt_release(p_hwfn, p_ptt);
  73. if (rc)
  74. break;
  75. }
  76. return rc;
  77. }
  78. int qed_selftest_clock(struct qed_dev *cdev)
  79. {
  80. struct qed_hwfn *p_hwfn;
  81. struct qed_ptt *p_ptt;
  82. int rc = 0, i;
  83. /* although performed by MCP, this test is per engine */
  84. for_each_hwfn(cdev, i) {
  85. p_hwfn = &cdev->hwfns[i];
  86. p_ptt = qed_ptt_acquire(p_hwfn);
  87. if (!p_ptt) {
  88. DP_ERR(p_hwfn, "failed to acquire ptt\n");
  89. return -EBUSY;
  90. }
  91. rc = qed_mcp_bist_clock_test(p_hwfn, p_ptt);
  92. qed_ptt_release(p_hwfn, p_ptt);
  93. if (rc)
  94. break;
  95. }
  96. return rc;
  97. }
  98. int qed_selftest_nvram(struct qed_dev *cdev)
  99. {
  100. struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
  101. struct qed_ptt *p_ptt = qed_ptt_acquire(p_hwfn);
  102. u32 num_images, i, j, nvm_crc, calc_crc;
  103. struct bist_nvm_image_att image_att;
  104. u8 *buf = NULL;
  105. __be32 val;
  106. int rc;
  107. if (!p_ptt) {
  108. DP_ERR(p_hwfn, "failed to acquire ptt\n");
  109. return -EBUSY;
  110. }
  111. /* Acquire from MFW the amount of available images */
  112. rc = qed_mcp_bist_nvm_get_num_images(p_hwfn, p_ptt, &num_images);
  113. if (rc || !num_images) {
  114. DP_ERR(p_hwfn, "Failed getting number of images\n");
  115. rc = -EINVAL;
  116. goto err0;
  117. }
  118. /* Iterate over images and validate CRC */
  119. for (i = 0; i < num_images; i++) {
  120. /* This mailbox returns information about the image required for
  121. * reading it.
  122. */
  123. rc = qed_mcp_bist_nvm_get_image_att(p_hwfn, p_ptt,
  124. &image_att, i);
  125. if (rc) {
  126. DP_ERR(p_hwfn,
  127. "Failed getting image index %d attributes\n",
  128. i);
  129. goto err0;
  130. }
  131. /* After MFW crash dump is collected - the image's CRC stops
  132. * being valid.
  133. */
  134. if (image_att.image_type == NVM_TYPE_MDUMP)
  135. continue;
  136. DP_VERBOSE(p_hwfn, QED_MSG_SP, "image index %d, size %x\n",
  137. i, image_att.len);
  138. /* Allocate a buffer for holding the nvram image */
  139. buf = kzalloc(image_att.len, GFP_KERNEL);
  140. if (!buf) {
  141. rc = -ENOMEM;
  142. goto err0;
  143. }
  144. /* Read image into buffer */
  145. rc = qed_mcp_nvm_read(p_hwfn->cdev, image_att.nvm_start_addr,
  146. buf, image_att.len);
  147. if (rc) {
  148. DP_ERR(p_hwfn,
  149. "Failed reading image index %d from nvm.\n", i);
  150. goto err1;
  151. }
  152. /* Convert the buffer into big-endian format (excluding the
  153. * closing 4 bytes of CRC).
  154. */
  155. for (j = 0; j < image_att.len - 4; j += 4) {
  156. val = cpu_to_be32(*(u32 *)&buf[j]);
  157. *(u32 *)&buf[j] = (__force u32)val;
  158. }
  159. /* Calc CRC for the "actual" image buffer, i.e. not including
  160. * the last 4 CRC bytes.
  161. */
  162. nvm_crc = *(u32 *)(buf + image_att.len - 4);
  163. calc_crc = crc32(0xffffffff, buf, image_att.len - 4);
  164. calc_crc = (__force u32)~cpu_to_be32(calc_crc);
  165. DP_VERBOSE(p_hwfn, QED_MSG_SP,
  166. "nvm crc 0x%x, calc_crc 0x%x\n", nvm_crc, calc_crc);
  167. if (calc_crc != nvm_crc) {
  168. rc = -EINVAL;
  169. goto err1;
  170. }
  171. /* Done with this image; Free to prevent double release
  172. * on subsequent failure.
  173. */
  174. kfree(buf);
  175. buf = NULL;
  176. }
  177. qed_ptt_release(p_hwfn, p_ptt);
  178. return 0;
  179. err1:
  180. kfree(buf);
  181. err0:
  182. qed_ptt_release(p_hwfn, p_ptt);
  183. return rc;
  184. }